pet.lua 5.5 KB

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