bobber.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. -----------------------------------------------------------------------------------------------
  2. -- Fishing - Mossmanikin's version - Bobber 0.1.0
  3. -- License (code & textures): WTFPL
  4. -- Contains code from: fishing (original), mobs, throwing
  5. -- Supports: animal_clownfish, animal_fish_blue_white, animal_rat, mobs
  6. -----------------------------------------------------------------------------------------------
  7. -- 0.0625 (= 1 pixel on 16x16 texture)
  8. -- 0.125
  9. -- 0.1875
  10. -- 0.25
  11. -- 0.3125
  12. -- 0.375
  13. -- 0.4375
  14. -- 0.5 (= 8 pixels on 16x16 texture)
  15. -- 0.5625 (= 9 pixels on 16x16 texture)
  16. -- 0.625
  17. -- 0.6875
  18. -- 0.75
  19. -- 0.8125
  20. -- 0.875
  21. -- 0.9375
  22. -- 1.0 (= 16 pixels on 16x16 texture)
  23. minetest.register_node("fishing:bobber_box", {
  24. drawtype = "nodebox",
  25. node_box = {
  26. type = "fixed",
  27. fixed = {
  28. -- { left , bottom , front , right , top , back }
  29. {-0.0625, -0.6875, -0.0625, 0.0625, -0.5625, 0.0625},
  30. }
  31. },
  32. tiles = {"fishing_bobber.png"},
  33. groups = {not_in_creative_inventory=1},
  34. })
  35. minetest.register_node("fishing:bobber_box_ready", {
  36. drawtype = "nodebox",
  37. node_box = {
  38. type = "fixed",
  39. fixed = {
  40. -- { left , bottom , front , right , top , back }
  41. {-0.0625, -0.6875, -0.0625, 0.0625, -0.5625, 0.0625},
  42. }
  43. },
  44. tiles = {"fishing_bobber_ready.png"},
  45. groups = {not_in_creative_inventory=1},
  46. })
  47. local FISHING_BOBBER_ENTITY={
  48. hp_max = 605,
  49. water_damage = 1,
  50. physical = true,
  51. timer = 0,
  52. env_damage_timer = 0,
  53. visual = "wielditem",
  54. visual_size = {x=0.5, y=0.5, z=0.5},
  55. textures = {"fishing:bobber_box"},
  56. -- { left , bottom , front , right , top , back }
  57. collisionbox = {-0.125 , -0.5625, -0.125 , 0.125 , -0.3125, 0.125 },
  58. view_range = 7,
  59. -- DESTROY BOBBER WHEN PUNCHING IT
  60. on_punch = function (self, puncher, time_from_last_punch, tool_capabilities, dir)
  61. local player = puncher:get_player_name()
  62. if MESSAGES == true then
  63. --minetest.chat_send_all("Your fish escaped.")
  64. minetest.chat_send_player(player, "Your fish escaped.", false)
  65. end
  66. minetest.sound_play("fishing_bobber1", {
  67. pos = self.object:getpos(),
  68. gain = 0.5,
  69. })
  70. self.object:remove()
  71. end,
  72. -- WHEN RIGHTCLICKING THE BOBBER THE FOLLOWING HAPPENS (CLICK AT THE RIGHT TIME WHILE HOLDING A FISHING POLE)
  73. on_rightclick = function (self, clicker)
  74. local item = clicker:get_wielded_item()
  75. local player = clicker:get_player_name()
  76. if item:get_name() == "fishing:pole" then
  77. local inv = clicker:get_inventory()
  78. local room_fish = inv:room_for_item("main", {name="fishing:fish_raw", count=1, wear=0, metadata=""})
  79. if self.object:get_hp() <= 300 then
  80. if math.random(1, 100) < FISH_CHANCE then
  81. local chance = math.random(1, 84)
  82. if chance <= 60 then
  83. if room_fish then
  84. inv:add_item("main", {name="fishing:fish_raw", count=1, wear=0, metadata=""})
  85. if MESSAGES == true then
  86. --minetest.chat_send_all("You caught a Fish.")
  87. minetest.chat_send_player(player, "You caught a Fish.", false)
  88. end
  89. end
  90. elseif chance <= 70 then
  91. if minetest.get_modpath("animal_clownfish") ~= nil then
  92. if inv:room_for_item("main", {name="animal_clownfish:clownfish", count=1, wear=0, metadata=""}) then
  93. inv:add_item("main", {name="animal_clownfish:clownfish", count=1, wear=0, metadata=""})
  94. if MESSAGES == true then
  95. --minetest.chat_send_all("You caught a Clownfish.")
  96. minetest.chat_send_player(player, "You caught a Clownfish.", false)
  97. end
  98. end
  99. else
  100. if room_fish then
  101. inv:add_item("main", {name="fishing:fish_raw", count=1, wear=0, metadata=""})
  102. if MESSAGES == true then
  103. --minetest.chat_send_all("You caught a Fish.")
  104. minetest.chat_send_player(player, "You caught a Fish.", false)
  105. end
  106. end
  107. end
  108. elseif chance <= 80 then
  109. if minetest.get_modpath("animal_fish_blue_white") ~= nil then
  110. if inv:room_for_item("main", {name="animal_fish_blue_white:fish_blue_white", count=1, wear=0, metadata=""}) then
  111. inv:add_item("main", {name="animal_fish_blue_white:fish_blue_white", count=1, wear=0, metadata=""})
  112. if MESSAGES == true then
  113. --minetest.chat_send_all("You caught a Blue white fish.")
  114. minetest.chat_send_player(player, "You caught a Blue white fish.", false)
  115. end
  116. end
  117. else
  118. if room_fish then
  119. inv:add_item("main", {name="fishing:fish_raw", count=1, wear=0, metadata=""})
  120. if MESSAGES == true then
  121. --minetest.chat_send_all("You caught a Fish.")
  122. minetest.chat_send_player(player, "You caught a Fish.", false)
  123. end
  124. end
  125. end
  126. elseif chance == 81 then
  127. if inv:room_for_item("main", {name="default:stick", count=1, wear=0, metadata=""}) then
  128. inv:add_item("main", {name="default:stick", count=1, wear=0, metadata=""})
  129. if MESSAGES == true then
  130. --minetest.chat_send_all("You caught a Stick.")
  131. minetest.chat_send_player(player, "You caught a Stick.", false)
  132. end
  133. end
  134. elseif chance == 82 then
  135. if minetest.get_modpath("mobs") ~= nil then
  136. if inv:room_for_item("main", {name="mobs:rat", count=1, wear=0, metadata=""}) then
  137. inv:add_item("main", {name="mobs:rat", count=1, wear=0, metadata=""})
  138. if MESSAGES == true then
  139. --minetest.chat_send_all("You caught a Rat.")
  140. minetest.chat_send_player(player, "You caught a Rat.", false)
  141. end
  142. end
  143. elseif minetest.get_modpath("animal_rat") ~= nil then
  144. if inv:room_for_item("main", {name="animal_rat:rat", count=1, wear=0, metadata=""}) then
  145. inv:add_item("main", {name="animal_rat:rat", count=1, wear=0, metadata=""})
  146. if MESSAGES == true then
  147. --minetest.chat_send_all("You caught a Rat.")
  148. minetest.chat_send_player(player, "You caught a Rat.", false)
  149. end
  150. end
  151. else
  152. if inv:room_for_item("main", {name="rat", count=1, wear=0, metadata=""}) then
  153. inv:add_item("main", {name="rat", count=1, wear=0, metadata=""})
  154. if MESSAGES == true then
  155. --minetest.chat_send_all("You caught a Rat.")
  156. minetest.chat_send_player(player, "You caught a Rat.", false)
  157. end
  158. end
  159. end
  160. end
  161. elseif chance == 83 then
  162. if minetest.get_modpath("flowers_plus") ~= nil then
  163. if inv:room_for_item("main", {name="flowers:seaweed", count=1, wear=0, metadata=""}) then
  164. inv:add_item("main", {name="flowers:seaweed", count=1, wear=0, metadata=""})
  165. if MESSAGES == true then
  166. --minetest.chat_send_all("You caught a Clownfish.")
  167. minetest.chat_send_player(player, "You caught some Seaweed.", false)
  168. end
  169. end
  170. else
  171. if room_fish then
  172. inv:add_item("main", {name="fishing:fish_raw", count=1, wear=0, metadata=""})
  173. if MESSAGES == true then
  174. --minetest.chat_send_all("You caught a Fish.")
  175. minetest.chat_send_player(player, "You caught a Fish.", false)
  176. end
  177. end
  178. end
  179. elseif chance == 84 then
  180. if minetest.get_modpath("seaplants") ~= nil then
  181. if inv:room_for_item("main", {name="seaplants:leavysnackgreen", count=1, wear=0, metadata=""}) then
  182. inv:add_item("main", {name="seaplants:leavysnackgreen", count=1, wear=0, metadata=""})
  183. if MESSAGES == true then
  184. --minetest.chat_send_all("You caught a Clownfish.")
  185. minetest.chat_send_player(player, "You caught a Leavy Snack.", false)
  186. end
  187. end
  188. else
  189. if room_fish then
  190. inv:add_item("main", {name="fishing:fish_raw", count=1, wear=0, metadata=""})
  191. if MESSAGES == true then
  192. --minetest.chat_send_all("You caught a Fish.")
  193. minetest.chat_send_player(player, "You caught a Fish.", false)
  194. end
  195. end
  196. end
  197. else
  198. if MESSAGES == true then
  199. --minetest.chat_send_all("Your fish escaped.")
  200. minetest.chat_send_player(player, "Your fish escaped.", false)
  201. end
  202. end
  203. else
  204. if MESSAGES == true then
  205. --minetest.chat_send_all("Your fish escaped.")
  206. minetest.chat_send_player(player, "Your fish escaped.", false)
  207. end
  208. end
  209. else
  210. if MESSAGES == true then
  211. --minetest.chat_send_all("Your fish escaped.")
  212. minetest.chat_send_player(player, "Your fish escaped.", false)
  213. end
  214. end
  215. minetest.sound_play("fishing_bobber1", {
  216. pos = self.object:getpos(),
  217. gain = 0.5,
  218. })
  219. self.object:remove()
  220. end,
  221. -- AS SOON AS THE BOBBER IS PLACED IT WILL ACT LIKE
  222. on_step = function(self, dtime)
  223. local pos = self.object:getpos()
  224. if BOBBER_CHECK_RADIUS > 0 then
  225. local objs = minetest.env:get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, BOBBER_CHECK_RADIUS)
  226. for k, obj in pairs(objs) do
  227. if obj:get_luaentity() ~= nil then
  228. if obj:get_luaentity().name == "fishing:bobber_entity" then
  229. if obj:get_luaentity() ~= self then
  230. self.object:remove()
  231. end
  232. end
  233. end
  234. end
  235. end
  236. if math.random(1, 4) == 1 then
  237. self.object:setyaw(self.object:getyaw()+((math.random(0,360)-180)/180*math.pi))
  238. end
  239. for _,player in pairs(minetest.get_connected_players()) do
  240. local s = self.object:getpos()
  241. local p = player:getpos()
  242. local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
  243. if dist > self.view_range then
  244. minetest.sound_play("fishing_bobber1", {
  245. pos = self.object:getpos(),
  246. gain = 0.5,
  247. })
  248. self.object:remove()
  249. end
  250. end
  251. local do_env_damage = function(self)
  252. self.object:set_hp(self.object:get_hp()-self.water_damage)
  253. --local pos = self.object:getpos()
  254. if self.object:get_hp() == 600 then
  255. self.object:moveto({x=pos.x,y=pos.y-0.03125,z=pos.z})
  256. elseif self.object:get_hp() == 595 then
  257. self.object:moveto({x=pos.x,y=pos.y+0.03125,z=pos.z})
  258. elseif self.object:get_hp() == 590 then
  259. self.object:moveto({x=pos.x,y=pos.y+0.03125,z=pos.z})
  260. elseif self.object:get_hp() == 585 then
  261. self.object:moveto({x=pos.x,y=pos.y-0.03125,z=pos.z})
  262. self.object:set_hp(self.object:get_hp()-(math.random(1, 200)))
  263. elseif self.object:get_hp() == 300 then
  264. minetest.sound_play("fishing_bobber1", {
  265. pos = self.object:getpos(),
  266. gain = 0.5,
  267. })
  268. if BOBBER_COLOR_CHANGE == true then
  269. self.object:set_properties({
  270. textures = {"fishing:bobber_box_ready"},
  271. })
  272. end
  273. self.object:moveto({x=pos.x,y=pos.y-0.0625,z=pos.z})
  274. elseif self.object:get_hp() == 295 then
  275. self.object:moveto({x=pos.x,y=pos.y+0.0625,z=pos.z})
  276. elseif self.object:get_hp() == 290 then
  277. self.object:moveto({x=pos.x,y=pos.y+0.0625,z=pos.z})
  278. elseif self.object:get_hp() == 285 then
  279. self.object:moveto({x=pos.x,y=pos.y-0.0625,z=pos.z})
  280. elseif self.object:get_hp() < 284 then
  281. self.object:moveto({x=pos.x+(0.001*(math.random(-8, 8))),y=pos.y,z=pos.z+(0.001*(math.random(-8, 8)))})
  282. elseif self.object:get_hp() == 0 then
  283. minetest.sound_play("fishing_bobber1", {
  284. pos = self.object:getpos(),
  285. gain = 0.5,
  286. })
  287. self.object:remove()
  288. end
  289. end
  290. do_env_damage(self)
  291. end,
  292. }
  293. minetest.register_entity("fishing:bobber_entity", FISHING_BOBBER_ENTITY)