-----------------------------------------------------------------------------------------------
local title     = "Fishing - Mossmanikin's version"
local version   = "0.2.2"
local mname     = "fishing"
-----------------------------------------------------------------------------------------------
-- original by wulfsdad (http://forum.minetest.net/viewtopic.php?id=4375)
-- this version by Mossmanikin (https://forum.minetest.net/viewtopic.php?id=6480)
-- License (code & textures):   WTFPL
-- Contains code from:      animal_clownfish, animal_fish_blue_white, fishing (original), stoneage
-- Looked at code from:     default, farming
-- Dependencies:            default
-- Supports:                animal_clownfish, animal_fish_blue_white, animal_rat, mobs
-----------------------------------------------------------------------------------------------

-- todo:    item wear                                           done
--          automatic re-baiting option                         done (but not optional)
--          different types of fish,                            done, but not finished
--          add sound                                           done
--          bobber                                              done
--          change rainworms filling inv & make 'em disappear   done

--          placable fishing rod for decoration                 done
--          make bobber move slowly while fish on hook          done
--          catch bigger fish with smaller                      done, but not finished
--          change color of bobber when fish on hook            done

-----------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- some additions made by 4aiman to make a pole be usable w/o clicking on a bobbler,
-- to make bobblers "dissapear" other bobblers only of their "owner"
-- also 1 man = 1 bobbler... or so it would seem ;)
-- Moreover, I've fixed node_drops to be compatible with enchantments.
--------------------------------------------------------------------------------
dofile(minetest.get_modpath("fishing").."/settings.txt")
dofile(minetest.get_modpath("fishing").."/bobber.lua")
dofile(minetest.get_modpath("fishing").."/bobber_shark.lua")
dofile(minetest.get_modpath("fishing").."/crafting.lua")
dofile(minetest.get_modpath("fishing").."/fishes.lua")
dofile(minetest.get_modpath("fishing").."/worm.lua")

-----------------------------------------------------------------------------------------------
-- Fishing Pole
-----------------------------------------------------------------------------------------------

local function rod_wear(itemstack, user, pointed_thing, uses)
    itemstack:add_wear(65535/(uses-1))
    return itemstack
end

pused = {}
placed_bobbler = {}

minetest.register_tool("fishing:pole", {

    description = "Fishing Pole",
    groups = {},
    inventory_image = "fishing_pole.png",
    wield_image = "fishing_pole.png^[transformFXR270",
    stack_max = 1,
    liquids_pointable = true,
    range = 7.5,
    on_place = function (itemstack, user, pointed_thing)
        if placed_bobbler[player]==true then return itemstack end
        if pointed_thing and pointed_thing.under then
            local pt = pointed_thing
            local node = minetest.get_node(pt.under)
            if string.find(node.name, "default:water") then
                local player = user:get_player_name()
                pused[player]=true
                local inv = user:get_inventory()
                if inv:get_stack("main", user:get_wield_index()+1):get_name() == "fishing:bait_worm" then
                    if not minetest.setting_getbool("creative_mode") then
                        inv:remove_item("main", "fishing:bait_worm")
                    end
                    minetest.sound_play("fishing_bobber2", {
                        pos = pt.under,
                        gain = 0.5,
                    })
                    local ent = minetest.add_entity({interval = 1,x=pt.under.x, y=pt.under.y+(45/64), z=pt.under.z}, "fishing:bobber_entity")
                    ent:get_luaentity().owner=player
                    placed_bobbler[player] = true
                    if WEAR_OUT == true
                    and not minetest.setting_getbool("creative_mode") then
                        return rod_wear(itemstack, user, pointed_thing, 60)
                    else
                        return {name="fishing:pole", count=1, wear=0, metadata=""}
                    end
                end
                if inv:get_stack("main", user:get_wield_index()+1):get_name() == "fishing:fish_raw" then
                    if not minetest.setting_getbool("creative_mode") then
                        inv:remove_item("main", "fishing:fish_raw")
                    end
                    minetest.sound_play("fishing_bobber2", {
                        pos = pt.under,
                        gain = 0.5,
                    })
                    minetest.add_entity({interval = 1,x=pt.under.x, y=pt.under.y+(45/64), z=pt.under.z}, "fishing:bobber_entity_shark")
                    placed_bobbler[player] = true
                    if WEAR_OUT == true
                    and not minetest.setting_getbool("creative_mode") then
                        return rod_wear(itemstack, user, pointed_thing, 60)
                    else
                        return {name="fishing:pole", count=1, wear=0, metadata=""}
                    end
                end
            end
        end
        return nil
    end,
    on_use = function(itemstack, placer, pointed_thing)
    end,
})

minetest.register_globalstep(function(dtime)
   local players = minetest.get_connected_players()
   for i,player in ipairs(players) do
       local pll = player:get_player_name()
       local control = player:get_player_control()
       if not control.RMB then pused[pll]=nil end
   end
end)
