-- All weapons can use this single function, the last three variables should be provided by the weapon def.
-- Sound is the sound the weapon makes when used.
-- Dist is the max_hear_distance for the sound.

lobby.on_weapon_use = function(itemstack, player, pointed_thing)
   local physics_table = player:get_physics_override()
   if physics_table.speed ~= 0 then
      local traitor = player:get_player_name()
      local obj = pointed_thing.ref
      if obj and pointed_thing.type == "object" then
         local player_name = obj:get_player_name()
         if player_name ~= '' then
            local map_id = lobby.game[traitor]
            local count = lobby.map[map_id]
            if count > 0 then
               local weapon = itemstack:get_name()
               local def = minetest.registered_nodes[weapon]
               local sound = def._sound or ''
               local max_dist = def._hear_dist or 16
               local cooldown = 20
               local chance = 100
               local data = lobby.savedata.data[map_id]
               if data then
                  if data.cooldown then
                     cooldown = data.cooldown
                  end
                  if data.chance then
                     chance = data.chance
                  end
               end
               if sound ~= '' then
                  minetest.sound_play(sound, {pos = player:get_pos(), gain = 1, max_hear_distance = max_dist})
               end
               if math.random(100) <= chance then
                  local victim = minetest.get_player_by_name(player_name)
                  local death_pos = victim:get_pos()
                  minetest.sound_play('lobby_kill', {pos = death_pos, gain = 1, max_hear_distance = 16})
                  victim:set_hp(0)
               else
                  minetest.chat_send_player(traitor, 'You missed!')
                  cooldown = math.floor(cooldown / 4)
               end
               local player_inv = player:get_inventory()
               minetest.chat_send_player(traitor, 'Weapon Cooldown for '..cooldown..' seconds.')
               minetest.after(cooldown, function()
                  local map_id = lobby.game[traitor]
                  if map_id ~= 'lobby' then
                     player_inv:add_item('main', weapon)
                  end
               end)
            elseif count == 0 then
               lobby.traitor_win(traitor, map_id)
            end
            itemstack:take_item()
            return itemstack
         end
      --else
      --The weapon could do stuff if not pointing at a player, make noise, or even trigger the cooldown. :P
      end
   end
end

minetest.register_node('lobby:sword', {
   description = 'Broadsword\nRange: 7',
   drawtype = 'mesh',
   mesh = 'lobby_sword.obj',
   tiles = {'lobby_sword.png'},
   inventory_image = 'lobby_sword_inv.png',
   range = 7,
   _sound = 'lobby_swoosh1',
   _hear_dist = 8,
   node_placement_prediction = '',
   on_use = lobby.on_weapon_use,
   on_place = function(pos)
      return nil
   end,
   on_drop = lobby.no_drop,
   groups = {not_in_creative_inventory=1, weapon=1},
})

minetest.register_node('lobby:shank', {
   description = 'Goblin Shank\nRange: 5',
   drawtype = 'mesh',
   mesh = 'lobby_shank.obj',
   tiles = {'lobby_shank.png'},
   inventory_image = 'lobby_shank_inv.png',
   range = 5,
   groups = {not_in_creative_inventory=1, weapon=1},
   on_use = lobby.on_weapon_use,
   on_place = function(pos)
      return nil
   end,
   on_drop = lobby.no_drop
})

minetest.register_node('lobby:yoyo', {
   description = 'Killer Yo-yo\nRange: 6',
   drawtype = 'mesh',
   mesh = 'lobby_yoyo.obj',
   tiles = {'lobby_yoyo.png'},
   inventory_image = 'lobby_yoyo_inv.png',
   range = 6,
   _sound = 'lobby_swoosh1',
   _hear_dist = 8,
   groups = {not_in_creative_inventory=1, weapon=1},
   on_use = lobby.on_weapon_use,
   on_place = function(pos)
      return nil
   end,
   on_drop = lobby.no_drop
})

minetest.register_node('lobby:battleaxe', {
   description = 'Battleaxe\nRange: 8',
   drawtype = 'mesh',
   mesh = 'lobby_battleaxe.obj',
   tiles = {'lobby_battleaxe_uv.png'},
   inventory_image = 'lobby_battleaxe_inv.png',
   range = 8,
   _sound = 'lobby_swoosh1',
   _hear_dist = 8,
   node_placement_prediction = '',
   on_use = lobby.on_weapon_use,
   on_place = function(pos)
      return nil
   end,
   on_drop = lobby.no_drop,
   groups = {not_in_creative_inventory=1, weapon=1},
})

minetest.create_detached_inventory('weapons_list', {
   allow_put = function(inv, listname, index, stack, player2)
      return -1
   end,
   allow_take = function(inv, listname, index, stack, player2)
      local name = player2 and player2:get_player_name() or ''
      if not creative.is_enabled_for(name) then
         return 0
      end
      return -1
   end
})

local stashable = {}
minetest.register_on_mods_loaded(function()
   for name, def in pairs(minetest.registered_items) do
      local group = def.groups or {}
      if group.weapon then
         stashable[name] = def
      end
   end
   local list = {}
   for name, def in pairs(stashable) do
      list[#list+1] = name
   end
   local stash_inv = minetest.get_inventory({type = 'detached', name = 'weapons_list'})
   stash_inv:set_size('main', #list)
   stash_inv:set_list('main', list)
end)
