worm.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. -----------------------------------------------------------------------------------------------
  2. -- Fishing - Mossmanikin's version - Worm 0.0.2
  3. -----------------------------------------------------------------------------------------------
  4. -- License (code & textures): WTFPL
  5. -- Contains code from: fishing (original), mobs
  6. -- Looked at code from: my_mobs
  7. -- Dependencies: default
  8. -----------------------------------------------------------------------------------------------
  9. -----------------------------------------------------------------------------------------------
  10. -- WORM ITEM
  11. -----------------------------------------------------------------------------------------------
  12. minetest.register_craftitem("fishing:bait_worm", {
  13. description = "Worm",
  14. groups = { fishing_bait=1 },
  15. inventory_image = "fishing_worm.png",
  16. on_use = minetest.item_eat(1),
  17. on_place = function(itemstack, placer, pointed_thing)
  18. local pt = pointed_thing
  19. minetest.env:add_entity({x=pt.under.x, y=pt.under.y+0.6, z=pt.under.z}, "fishing:bait_worm_entity")
  20. itemstack:take_item()
  21. return itemstack
  22. end,
  23. on_drop = function(itemstack, dropper, pos)
  24. minetest.env:add_entity({x = pos.x, y = pos.y, z = pos.z}, "fishing:bait_worm_entity")
  25. itemstack:take_item()
  26. return itemstack
  27. end,
  28. })
  29. -----------------------------------------------------------------------------------------------
  30. -- WORM MOB
  31. -----------------------------------------------------------------------------------------------
  32. minetest.register_entity("fishing:bait_worm_entity", {
  33. hp_max = 300,
  34. damage_over_time = 1,
  35. collisionbox = {-3/16, -3/16, -3/16, 3/16, 3/16, 3/16},
  36. visual = "sprite",
  37. visual_size = {x=1/2, y=1/2},
  38. textures = { "fishing_worm.png", "fishing_worm.png"},
  39. view_range = 32,
  40. -- Don't punch this poor creature...
  41. on_punch = function(self, puncher)
  42. self.object:remove()
  43. end,
  44. -- ...softly take it into your hand.
  45. on_rightclick = function(self, clicker)
  46. if clicker:is_player() and clicker:get_inventory() then
  47. clicker:get_inventory():add_item("main", "fishing:bait_worm")
  48. self.object:remove()
  49. end
  50. end,
  51. -- AI :D
  52. on_step = function(self, dtime)
  53. local pos = self.object:getpos()
  54. local n = minetest.env:get_node({x=pos.x,y=pos.y-0.3,z=pos.z})
  55. -- despawn when no player in range
  56. for _,player in pairs(minetest.get_connected_players()) do
  57. local s = self.object:getpos()
  58. local p = player:getpos()
  59. local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
  60. if dist > self.view_range then
  61. self.object:remove()
  62. end
  63. end
  64. -- move in world
  65. local look_whats_up = function(self)
  66. self.object:set_hp(self.object:get_hp()-self.damage_over_time) -- creature is getting older
  67. if n.name == "air" then -- fall when in air
  68. self.object:moveto({x=pos.x,y=pos.y-0.5,z=pos.z})
  69. self.object:set_hp(self.object:get_hp()-75)
  70. --if n.name == "snappy" then -- fall when leaves or similar
  71. elseif minetest.get_item_group(n.name, "snappy") ~= 0 then
  72. self.object:moveto({x=pos.x+(0.001*(math.random(-32, 32))),y=pos.y-(0.001*(math.random(0, 64))),z=pos.z+(0.001*(math.random(-32, 32)))})
  73. elseif string.find(n.name, "default:water") then -- sink when in water
  74. self.object:moveto({x=pos.x,y=pos.y-0.25,z=pos.z})
  75. self.object:set_hp(self.object:get_hp()-37)
  76. elseif minetest.get_item_group(n.name, "soil") ~= 0 then
  77. if minetest.get_item_group(minetest.env:get_node({x=pos.x,y=pos.y-0.1,z=pos.z}).name, "soil") == 0 and self.object:get_hp() > 200 then
  78. self.object:set_hp(199)
  79. elseif self.object:get_hp() > 200 then -- leave dirt to see whats going on
  80. self.object:moveto({x=pos.x+(0.001*(math.random(-2, 2))),y=pos.y+0.003,z=pos.z+(0.001*(math.random(-2, 2)))})
  81. elseif self.object:get_hp() < 199 then -- no rain here, let's get outa here
  82. self.object:moveto({x=pos.x+(0.001*(math.random(-2, 2))),y=pos.y-0.001,z=pos.z+(0.001*(math.random(-2, 2)))})
  83. elseif self.object:get_hp() == 0 then
  84. self.object:remove()
  85. end
  86. else -- check if there's dirt anywhere (not finished)
  87. local check_group = minetest.get_item_group
  88. local goal_01 = check_group(minetest.env:get_node({x = pos.x + 1, y = pos.y-0.4, z = pos.z }).name, "soil")
  89. local goal_02 = check_group(minetest.env:get_node({x = pos.x, y = pos.y-0.4, z = pos.z + 1}).name, "soil")
  90. local goal_03 = check_group(minetest.env:get_node({x = pos.x - 1, y = pos.y-0.4, z = pos.z }).name, "soil")
  91. local goal_04 = check_group(minetest.env:get_node({x = pos.x, y = pos.y-0.4, z = pos.z - 1}).name, "soil")
  92. local goal_1a = check_group(minetest.env:get_node({x = pos.x + 1, y = pos.y+0.6, z = pos.z }).name, "soil")
  93. local goal_2a = check_group(minetest.env:get_node({x = pos.x, y = pos.y+0.6, z = pos.z + 1}).name, "soil")
  94. local goal_3a = check_group(minetest.env:get_node({x = pos.x - 1, y = pos.y+0.6, z = pos.z }).name, "soil")
  95. local goal_4a = check_group(minetest.env:get_node({x = pos.x, y = pos.y+0.6, z = pos.z - 1}).name, "soil")
  96. -- if there's dirt nearby, go there
  97. if goal_01 ~= 0 or goal_1a ~= 0 then
  98. self.object:moveto({x=pos.x+0.002,y=pos.y,z=pos.z+(0.001*(math.random(-2, 2)))})
  99. elseif goal_02 ~= 0 or goal_2a ~= 0 then
  100. self.object:moveto({x=pos.x+(0.001*(math.random(-2, 2))),y=pos.y,z=pos.z+0.002})
  101. elseif goal_03 ~= 0 or goal_3a ~= 0 then
  102. self.object:moveto({x=pos.x-0.002,y=pos.y,z=pos.z+(0.001*(math.random(-2, 2)))})
  103. elseif goal_04 ~= 0 or goal_4a ~= 0 then
  104. self.object:moveto({x=pos.x+(0.001*(math.random(-2, 2))),y=pos.y,z=pos.z-0.002})
  105. else -- I'm lost, no dirt
  106. self.object:moveto({x=pos.x+(0.001*(math.random(-8, 8))),y=pos.y,z=pos.z+(0.001*(math.random(-8, 8)))})
  107. end
  108. end
  109. end
  110. look_whats_up(self)
  111. end,
  112. })