pet.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. local get_distance = function(a, b)
  2. local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z
  3. return (x * x + y * y + z * z)
  4. end
  5. mobs:register_mob('scorpion:pet', {
  6. type = 'npc',
  7. passive = false,
  8. attack_type = 'dogfight',
  9. owner_loyal = true,
  10. pathfinding = true,
  11. damage = 10,
  12. hp_min = 30, hp_max = 75, armor = 20,
  13. collisionbox = {-0.6, -0.35, -0.5, 0.6, 0.3, 0.5},
  14. visual = 'mesh',
  15. mesh = 'scorpion.b3d',
  16. drawtype = 'front',
  17. textures = {
  18. {'scorpion_red.png^scorpion_pet.png'},
  19. {'scorpion_green.png^scorpion_pet.png'},
  20. {'scorpion_tan.png^scorpion_pet.png'},
  21. },
  22. follow = {'mobs:meat_raw', 'mobs:chicken_raw'},
  23. blood_texture = 'mobs_blood.png',
  24. visual_size = {x=8,y=8},
  25. makes_footstep_sound = true,
  26. sounds = {
  27. war_cry = 'scorpion_squeak',
  28. },
  29. walk_velocity = 3,
  30. run_velocity = 6,
  31. jump = true,
  32. stepheight = 1.7,
  33. reach = 4,
  34. view_range = 15,
  35. fear_height = 2,
  36. owner = '',
  37. order = 'follow',
  38. drops = {
  39. {name = 'mobs:meat_raw', chance = 1, min = 2, max = 8},
  40. {name = 'scorpion:shell', chance = 1, min = 5, max = 20},
  41. },
  42. water_damage = 1,
  43. lava_damage = 6,
  44. light_damage = 0,
  45. animation = {
  46. speed_normal = 45, speed_run = 90,
  47. stand_start = 0, stand_end = 60,
  48. walk_start = 150, walk_end = 210,
  49. run_start = 150, run_end = 210,
  50. punch_start = 220, punch_end = 260,
  51. punch2_start = 265, punch2_end = 305,
  52. punch3_start = 70, punch3_end = 140,
  53. },
  54. on_spawn = function(self)
  55. local pos = self.object:get_pos()
  56. local objs = minetest.get_objects_inside_radius(pos, 4)
  57. for _, obj in pairs(objs) do
  58. if obj:is_player() then
  59. local owner = obj:get_player_name()
  60. self.owner = owner
  61. minetest.chat_send_player(owner, "Aww, a baby scorpion thinks you're its parent!")
  62. --This should only send to the player that is chosen to be the owner.
  63. end
  64. end
  65. return true
  66. end,
  67. on_rightclick = function(self, clicker)
  68. if mobs:protect(self, clicker) then return end
  69. local name = clicker:get_player_name()
  70. local inv = clicker:get_inventory()
  71. local tool = clicker:get_wielded_item()
  72. local item = tool:get_name()
  73. if mobs:feed_tame(self, clicker, 10, false, true) then
  74. return
  75. end
  76. if self.owner and self.owner == name then
  77. if item ~= 'scorpion:saddle' then
  78. if self.order == 'follow' then
  79. self.order = 'stand'
  80. minetest.chat_send_player(name, ('Scorpion holding ground.'))
  81. else
  82. self.order = 'follow'
  83. minetest.chat_send_player(name, ('Scorpion following you.'))
  84. end
  85. mobs:capture_mob(self, clicker, 20, 0, 100, false, nil)
  86. end
  87. -- detatch player
  88. if self.driver and clicker == self.driver then
  89. mobs.detach(clicker, {x = 1, y = 0, z = 1})
  90. -- add saddle back to inventory
  91. if inv:room_for_item("main", "scorpion:saddle") then
  92. inv:add_item("main", "scorpion:saddle")
  93. else
  94. minetest.add_item(clicker:get_pos(), "scorpion:saddle")
  95. end
  96. self.saddle = nil
  97. -- attach player
  98. elseif (not self.driver and not self.child and item == "scorpion:saddle")
  99. or self.saddle then
  100. self.object:set_properties({stepheight = 1.1})
  101. mobs.attach(self, clicker)
  102. if not self.saddle then
  103. inv:remove_item("main", "scorpion:saddle")
  104. end
  105. self.saddle = true
  106. end
  107. end
  108. end,
  109. on_grown = function(self)
  110. local owner = self.owner
  111. minetest.chat_send_player(owner, ('Your pet scorpion grew up!'))
  112. end,
  113. do_custom = function(self, dtime) --Thanks to IhrFussel for many ideas used in this code block.
  114. local pet_pos = self.object:get_pos()
  115. local owner = minetest.get_player_by_name(self.owner) or false
  116. if owner and self.order == 'follow' then --and self.state == "walk
  117. self.dtime = (self.dtime or 0) + dtime
  118. if self.dtime > 5 then
  119. local distance = get_distance(owner:get_pos(), pet_pos)
  120. if distance > 1000 then --should teleport to player
  121. self.object:set_pos(owner:get_pos())
  122. end
  123. self.dtime = 0
  124. end
  125. end
  126. -- set needed values if not already present
  127. if not self.v2 then
  128. self.v2 = 0
  129. self.max_speed_forward = 6
  130. self.max_speed_reverse = 2
  131. self.accel = 6
  132. self.terrain_type = 3
  133. self.driver_attach_at = {x = 0, y = y_off, z = -0.25}
  134. self.driver_eye_offset = {x = 0, y = 2, z = 0}
  135. self.driver_scale = {x = .125, y = .125}
  136. end
  137. -- if driver present allow control of horse
  138. if self.driver then
  139. mobs.drive(self, "walk", "stand", false, dtime)
  140. return false -- skip rest of mob functions
  141. end
  142. return true
  143. end,
  144. on_die = function(self, pos)
  145. if self.driver then
  146. minetest.add_item(pos, "scorpion:saddle")
  147. mobs.detach(self.driver, {x = 1, y = 0, z = 1})
  148. self.saddle = nil
  149. end
  150. end,
  151. })
  152. mobs:register_egg('scorpion:pet', 'Pet Scorpion', 'default_grass.png', 1)
  153. minetest.register_craftitem('scorpion:saddle', {
  154. description = 'Scorpion saddle',
  155. inventory_image = 'scorpion_saddle.png'
  156. })