poles.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. -----------------------------------------------------------------------------------------------
  2. -- Fishing Pole
  3. -----------------------------------------------------------------------------------------------
  4. local S = fishing_setting.func.S
  5. local function rod_wear(itemstack, user, pointed_thing, uses)
  6. itemstack:add_wear(65535/(uses-1))
  7. return itemstack
  8. end
  9. fishing_setting.poles = {}
  10. fishing_setting.poles.wood = {["name"] = "wood", ["max_use"] = 30, ["desc"] = S("Fishing Pole"),["bobber_max"] = 2 }
  11. fishing_setting.poles.perfect = {["name"] = "perfect", ["max_use"] = 1500, ["desc"] = S("Perfect Fishing Pole"),["bobber_max"] = 5}
  12. for _,pole in pairs(fishing_setting.poles) do
  13. local bobbermax = pole["bobber_max"]
  14. minetest.register_tool("fishing:pole_".. pole.name, {
  15. description = pole.desc,
  16. groups = {},
  17. inventory_image = "fishing_pole_".. pole.name ..".png",
  18. wield_image = "fishing_pole_".. pole.name ..".png",
  19. stack_max = 1,
  20. liquids_pointable = true,
  21. on_use = function (itemstack, user, pointed_thing)
  22. if pointed_thing and pointed_thing.under then
  23. local pt = pointed_thing
  24. local node = minetest.get_node(pt.under)
  25. if not node or string.find(node.name, "water_source") == nil then return nil end
  26. local player_name = user:get_player_name()
  27. local inv = user:get_inventory()
  28. local bait = inv:get_stack("main", user:get_wield_index()+1 ):get_name()
  29. if fishing_setting.baits[bait] == nil then return nil end
  30. local objs = minetest.get_objects_inside_radius(pt.under, 1)
  31. for m, obj in pairs(objs) do
  32. if obj:get_luaentity() ~= nil and string.find(obj:get_luaentity().name, "fishing:bobber") then
  33. if fishing_setting.settings["message"] == true then
  34. minetest.chat_send_player(player_name, S("Sorry, there is another bobber!"))
  35. end
  36. return nil
  37. end
  38. end
  39. --if contest then player must have only 2 boober
  40. local bobber_nb = 0
  41. local bobber_max
  42. if fishing_setting.contest["contest"] ~= nil and fishing_setting.contest["contest"] == true then
  43. bobber_max = fishing_setting.contest["bobber_nb"]
  44. else
  45. bobber_max = bobbermax
  46. end
  47. --player has others bobbers?
  48. for m, obj in pairs(minetest.get_objects_inside_radius(pt.under, 20)) do
  49. if obj:get_luaentity() ~= nil and string.find(obj:get_luaentity().name, "fishing:bobber") ~= nil then
  50. if obj:get_luaentity().owner == player_name then
  51. bobber_nb = bobber_nb + 1
  52. end
  53. end
  54. end
  55. if bobber_nb >= bobber_max then
  56. if fishing_setting.settings["message"] == true then
  57. minetest.chat_send_player(player_name, S("You don't have mores %s bobbers!"):format(bobber_max))
  58. end
  59. return nil
  60. end
  61. local nodes = 1
  62. for _,k in pairs({ {1, 0}, {-1,0}, {0,1}, {0,-1} }) do
  63. local node_name = minetest.get_node({x=pt.under.x+k[1], y=pt.under.y, z=pt.under.z+k[2]}).name
  64. if node_name and string.find(node_name, "water_source") ~= nil
  65. and minetest.get_node({x=pt.under.x+k[1], y=pt.under.y+1, z=pt.under.z+k[2]}).name == "air" then
  66. nodes = nodes + 1
  67. end
  68. end
  69. --if water == -3 nodes
  70. if nodes < 2 then
  71. if fishing_setting.settings["message"] == true then minetest.chat_send_player(player_name, S("You don't fishing in a bottle!")) end
  72. return nil
  73. end
  74. local new_pos = {x=pt.under.x, y=pt.under.y+(45/64), z=pt.under.z}
  75. local ent = minetest.add_entity({interval = 1,x=new_pos.x, y=new_pos.y, z=new_pos.z}, fishing_setting.baits[bait].bobber)
  76. if not ent then return nil end
  77. local luaentity = ent:get_luaentity()
  78. local node = minetest.get_node_or_nil(pt.under)
  79. if node and node.name == "default:river_water_source" then
  80. luaentity.water_type = "rivers"
  81. else
  82. luaentity.water_type = "sea"
  83. end
  84. luaentity.owner = player_name
  85. luaentity.bait = bait
  86. luaentity.old_pos = new_pos
  87. luaentity.old_pos2 = true
  88. if not fishing_setting.is_creative_mode then
  89. inv:remove_item("main", bait)
  90. end
  91. minetest.sound_play("fishing_bobber2", {pos = new_pos, gain = 0.5})
  92. if fishing_setting.settings["wear_out"] == true and not fishing_setting.is_creative_mode then
  93. return rod_wear(itemstack, user, pointed_thing, pole.max_use)
  94. else
  95. return {name="fishing:pole_".. pole.name, count=1, wear=0, metadata=""}
  96. end
  97. end
  98. return nil
  99. end,
  100. on_place = function(itemstack, placer, pointed_thing)
  101. if fishing_setting.settings["simple_deco_fishing_pole"] == false then return end
  102. local pt = pointed_thing
  103. local pt_under_name = minetest.get_node(pt.under).name
  104. if string.find(pt_under_name, "water_") == nil then
  105. local wear = itemstack:get_wear()
  106. local direction = minetest.dir_to_facedir(placer:get_look_dir())
  107. local dir = minetest.facedir_to_dir(direction)
  108. local p = vector.add(pt.above, dir)
  109. local n2 = minetest.get_node_or_nil(p)
  110. local def = n2 and minetest.registered_items[n2.name]
  111. if not def or not def.buildable_to then
  112. return nil
  113. end
  114. minetest.set_node(pt.above, {name="fishing:pole_".. pole.name .."_deco", param2=direction})
  115. local meta = minetest.get_meta(pt.above)
  116. meta:set_int("wear", wear)
  117. if not fishing_setting.is_creative_mode then
  118. itemstack:take_item()
  119. end
  120. end
  121. return itemstack
  122. end,
  123. })
  124. minetest.register_node("fishing:pole_".. pole.name .."_deco", {
  125. description = pole.desc,
  126. inventory_image = "fishing_pole_".. pole.name ..".png",
  127. wield_image = "fishing_pole.png^[transformFXR270",
  128. drawtype = "nodebox",
  129. paramtype = "light",
  130. paramtype2 = "facedir",
  131. tiles = {
  132. "fishing_pole_".. pole.name .."_simple.png",
  133. "fishing_pole_".. pole.name .."_simple.png",
  134. "fishing_pole_".. pole.name .."_simple.png",
  135. "fishing_pole_".. pole.name .."_simple.png^[transformFX",
  136. },
  137. groups = { snappy=3, flammable=2, not_in_creative_inventory=1 },
  138. node_box = {
  139. type = "fixed",
  140. fixed = {
  141. { 0 , -1/2 , 0 , 0 , 1/2 , 1 },
  142. }
  143. },
  144. selection_box = {
  145. type = "fixed",
  146. fixed = {
  147. {-1/16 , -1/2 , 0 , 1/16 , 1/2 , 1 },
  148. }
  149. },
  150. sounds = default.node_sound_wood_defaults(),
  151. on_dig = function(pos, node, digger)
  152. if digger:is_player() and digger:get_inventory() then
  153. local meta = minetest.get_meta(pos)
  154. local wear_out = meta:get_int("wear")
  155. digger:get_inventory():add_item("main", {name="fishing:pole_".. pole.name, count=1, wear=wear_out, metadata=""})
  156. end
  157. minetest.remove_node(pos)
  158. end,
  159. })
  160. end