bobber_shark.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. -----------------------------------------------------------------------------------------------
  2. -- Fishing - crabman77 version - Bobber Shark
  3. -- Rewrited from original Fishing - Mossmanikin's version - Bobber Shark 0.0.6
  4. -- License (code & textures): WTFPL
  5. -----------------------------------------------------------------------------------------------
  6. -- bobber shark
  7. minetest.register_node("fishing:bobber_shark_box", {
  8. drawtype = "nodebox",
  9. node_box = {
  10. type = "fixed",
  11. fixed = {
  12. -- { left, bottom, front, right, top , back}
  13. {-8/16, -8/16, 0, 8/16, 8/16, 0}, -- feathers
  14. {-2/16, -8/16, -2/16, 2/16, -4/16, 2/16}, -- bobber
  15. }
  16. },
  17. tiles = {
  18. "fishing_bobber_top.png",
  19. "fishing_bobber_bottom.png",
  20. "fishing_bobber_shark.png",
  21. "fishing_bobber_shark.png",
  22. "fishing_bobber_shark.png",
  23. "fishing_bobber_shark.png^[transformFX"
  24. },
  25. groups = {not_in_creative_inventory=1},
  26. })
  27. local FISHING_BOBBER_SHARK_ENTITY={
  28. physical = true,
  29. timer = 0,
  30. visual = "wielditem",
  31. visual_size = {x=1/3, y=1/3, z=1/3},
  32. textures = {"fishing:bobber_shark_box"},
  33. -- {left ,bottom, front, right, top , back}
  34. collisionbox = {-3/16, -4/16, -3/16, 3/16, 4/16, 3/16},
  35. randomtime = 50,
  36. baitball = 0,
  37. prize = "",
  38. bait = "",
  39. owner = nil,
  40. old_pos = nil,
  41. old_pos2 = nil,
  42. -- DESTROY BOBBER WHEN PUNCHING IT
  43. on_punch = function (self, puncher, time_from_last_punch, tool_capabilities, dir)
  44. if not puncher:is_player() then return end
  45. local player_name = puncher:get_player_name()
  46. if player_name ~= self.owner then return end
  47. if fishing_setting.settings["message"] == true then
  48. minetest.chat_send_player(player_name, fishing_setting.func.S("You didn't catch anything."), false)
  49. end
  50. if not fishing_setting.is_creative_mode then
  51. local inv = puncher:get_inventory()
  52. if inv:room_for_item("main", {name=self.bait, count=1, wear=0, metadata=""}) then
  53. inv:add_item("main", {name=self.bait, count=1, wear=0, metadata=""})
  54. if fishing_setting.settings["message"] == true then
  55. minetest.chat_send_player(player_name, fishing_setting.func.S("The bait is still there."), false)
  56. end
  57. end
  58. end
  59. -- make sound and remove bobber
  60. minetest.sound_play("fishing_bobber1", { pos = self.object:getpos(), gain = 0.5, })
  61. self.object:remove()
  62. end,
  63. -- WHEN RIGHTCLICKING THE BOBBER THE FOLLOWING HAPPENS (CLICK AT THE RIGHT TIME WHILE HOLDING A FISHING POLE)
  64. on_rightclick = function (self, clicker)
  65. local item = clicker:get_wielded_item()
  66. local player_name = clicker:get_player_name()
  67. if not player_name or not self.owner then
  68. self.object:remove()
  69. return
  70. end
  71. local inv = clicker:get_inventory()
  72. local pos = self.object:getpos()
  73. local item_name = item:get_name()
  74. if string.find(item_name, "fishing:pole_") ~= nil then
  75. if player_name ~= self.owner then return end
  76. if self.prize ~= "" then
  77. if math.random(1, 100) <= fishing_setting.settings["escape_chance"] then -- fish escaped
  78. if fishing_setting.settings["message"] == true then
  79. minetest.chat_send_player(player_name, fishing_setting.func.S("Your fish escaped."), false)
  80. end
  81. else
  82. local name = self.prize[1]..":"..self.prize[2]
  83. local desc = self.prize[4]
  84. if fishing_setting.settings["message"] == true then
  85. minetest.chat_send_player(player_name, fishing_setting.func.S("You caught "..desc), false)
  86. end
  87. fishing_setting.func.add_to_trophies(clicker, self.prize[2], desc)
  88. local wear_value = fishing_setting.func.wear_value(self.prize[3])
  89. if inv:room_for_item("main", {name=name, count=1, wear=wear_value, metadata=""}) then
  90. inv:add_item("main", {name=name, count=1, wear=wear_value, metadata=""})
  91. else
  92. minetest.spawn_item(clicker:getpos(), {name=name, count=1, wear=wear_value, metadata=""})
  93. end
  94. end
  95. else
  96. if not fishing_setting.is_creative_mode then
  97. if inv:room_for_item("main", {name=self.bait, count=1, wear=0, metadata=""}) then
  98. inv:add_item("main", {name=self.bait, count=1, wear=0, metadata=""})
  99. end
  100. end
  101. end
  102. -- weither player has fishing pole or not
  103. minetest.sound_play("fishing_bobber1", { pos = self.object:getpos(), gain = 0.5, })
  104. self.object:remove()
  105. elseif item_name == "fishing:baitball_shark" then
  106. if not fishing_setting.is_creative_mode then
  107. inv:remove_item("main", "fishing:baitball_shark")
  108. end
  109. self.baitball = 20
  110. --addparticle
  111. minetest.add_particlespawner(30, 0.5, -- for how long (?) -- Particles on splash
  112. {x=pos.x,y=pos.y-0.0325,z=pos.z}, {x=pos.x,y=pos.y,z=pos.z}, -- position min, pos max
  113. {x=-2,y=-0.0325,z=-2}, {x=2,y=3,z=2}, -- velocity min, vel max
  114. {x=0,y=-3.8,z=0}, {x=0,y=-9.8,z=0},
  115. 0.3, 1.2,
  116. 0.25, 0.40, -- min size, max size
  117. false, "fishing_particle_baitball_shark.png")
  118. -- add sound
  119. minetest.sound_play("fishing_baitball", {pos = self.object:getpos(), gain = 0.2, })
  120. end
  121. end,
  122. -- AS SOON AS THE BOBBER IS PLACED IT WILL ACT LIKE
  123. on_step = function(self, dtime)
  124. local pos = self.object:getpos()
  125. --remove if no owner, no player, owner no in bobber_view_range
  126. if self.owner == nil then self.object:remove(); return end
  127. --remove if not node water
  128. local node = minetest.get_node_or_nil({x=pos.x, y=pos.y-0.5, z=pos.z})
  129. if not node or string.find(node.name, "water_source") == nil then
  130. if fishing_setting.settings["message"] == true then
  131. minetest.chat_send_player(self.owner, fishing_setting.func.S("Haha, Fishing is prohibited outside water!"))
  132. end
  133. self.object:remove()
  134. return
  135. end
  136. local player = minetest.get_player_by_name(self.owner)
  137. if not player then self.object:remove(); return end
  138. local p = player:getpos()
  139. local dist = ((p.x-pos.x)^2 + (p.y-pos.y)^2 + (p.z-pos.z)^2)^0.5
  140. if dist > fishing_setting.settings["bobber_view_range"] then
  141. minetest.sound_play("fishing_bobber1", {pos = self.object:getpos(),gain = 0.5,})
  142. self.object:remove()
  143. return
  144. end
  145. --rotate bobber
  146. if math.random(1, 4) == 1 then
  147. self.object:setyaw(self.object:getyaw()+((math.random(0,360)-180)/2880*math.pi))
  148. end
  149. self.timer = self.timer + 1
  150. if self.timer < self.randomtime then
  151. -- if fish or others items, move bobber to simulate fish on the line
  152. if self.prize ~= "" and math.random(1,3) == 1 then
  153. if self.old_pos2 == true then
  154. pos.y = pos.y-0.050
  155. self.object:moveto(pos, false)
  156. self.old_pos2 = false
  157. else
  158. pos.y = pos.y+0.050
  159. self.object:moveto(pos, false)
  160. self.old_pos2 = true
  161. end
  162. end
  163. return
  164. end
  165. --change item on line
  166. self.timer = 0
  167. if self.prize ~= "" and fishing_setting.have_true_fish and fishing_setting.prizes["true_fish"]["big"][self.prize[1]..":"..self.prize[2]] then
  168. minetest.add_entity({x=pos.x, y=pos.y-1, z=pos.z}, self.prize[1]..":"..self.prize[2])
  169. end
  170. self.prize = ""
  171. self.object:moveto(self.old_pos, false)
  172. --Once the fish are not hungry :), baitball increase hungry + 20%
  173. if math.random(1, 100) > fishing_setting.baits[self.bait]["hungry"] + self.baitball then
  174. --Fish not hungry !(
  175. self.randomtime = math.random(20,60)*10
  176. return
  177. end
  178. self.randomtime = math.random(1,5)*10
  179. local chance = math.random(1, 100)
  180. --if 1 you catch a treasure
  181. if fishing_setting.settings["treasure_enable"] and chance == 1 then
  182. if math.random(1, 100) <= fishing_setting.settings["treasure_chance"] then
  183. self.prize = fishing_setting.prizes["treasure"][math.random(1,#fishing_setting.prizes["treasure"])]
  184. end
  185. elseif chance <= fishing_setting.settings["fish_chance"] then
  186. if self.water_type and self.water_type == "sea" then
  187. self.prize = fishing_setting.prizes["sea"]["big"][math.random(1,#fishing_setting.prizes["sea"]["big"])]
  188. else
  189. self.prize = fishing_setting.prizes["rivers"]["big"][math.random(1,#fishing_setting.prizes["rivers"]["big"])]
  190. end
  191. -- to mobs_fish modpack
  192. if fishing_setting.have_true_fish then
  193. local objs = minetest.get_objects_inside_radius({x=pos.x, y=pos.y-2, z=pos.z}, 3)
  194. for _, obj in pairs(objs) do
  195. if obj:get_luaentity() ~= nil then
  196. local name = obj:get_luaentity().name
  197. if fishing_setting.prizes["true_fish"]["big"][name] then
  198. self.prize = fishing_setting.prizes["true_fish"]["big"][name]
  199. obj:remove()
  200. self.randomtime = math.random(3,7)*10
  201. break
  202. end
  203. end
  204. end
  205. end
  206. elseif math.random(1, 100) <= 10 then
  207. self.prize = fishing_setting.func.get_loot()
  208. end
  209. if self.prize ~= "" then
  210. pos.y = self.old_pos.y-0.140
  211. self.object:moveto(pos, false)
  212. minetest.sound_play("fishing_bobber1", {pos=pos,gain = 0.5,})
  213. end
  214. end,
  215. }
  216. minetest.register_entity("fishing:bobber_shark_entity", FISHING_BOBBER_SHARK_ENTITY)