Mob API for mods to add monsters, animals and more. https://content.minetest.net/packages/TenPlus1/mobs/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

449 lines
9.1KB

  1. -- lib_mount by Blert2112 (edited by TenPlus1)
  2. local enable_crash = false
  3. local crash_threshold = 6.5 -- ignored if enable_crash=false
  4. ------------------------------------------------------------------------------
  5. --
  6. -- Helper functions
  7. --
  8. local node_ok = function(pos, fallback)
  9. fallback = fallback or mobs.fallback_node
  10. local node = minetest.get_node_or_nil(pos)
  11. if node and minetest.registered_nodes[node.name] then
  12. return node
  13. end
  14. return {name = fallback}
  15. end
  16. local function node_is(pos)
  17. local node = node_ok(pos)
  18. if node.name == "air" then
  19. return "air"
  20. end
  21. if minetest.get_item_group(node.name, "lava") ~= 0 then
  22. return "lava"
  23. end
  24. if minetest.get_item_group(node.name, "liquid") ~= 0 then
  25. return "liquid"
  26. end
  27. if minetest.registered_nodes[node.name].walkable == true then
  28. return "walkable"
  29. end
  30. return "other"
  31. end
  32. local function get_sign(i)
  33. i = i or 0
  34. if i == 0 then
  35. return 0
  36. else
  37. return i / math.abs(i)
  38. end
  39. end
  40. local function get_velocity(v, yaw, y)
  41. local x = -math.sin(yaw) * v
  42. local z = math.cos(yaw) * v
  43. return {x = x, y = y, z = z}
  44. end
  45. local function get_v(v)
  46. return math.sqrt(v.x * v.x + v.z * v.z)
  47. end
  48. local function force_detach(player)
  49. local attached_to = player:get_attach()
  50. if not attached_to then
  51. return
  52. end
  53. local entity = attached_to:get_luaentity()
  54. if entity.driver
  55. and entity.driver == player then
  56. entity.driver = nil
  57. end
  58. player:set_detach()
  59. default.player_attached[player:get_player_name()] = false
  60. player:set_eye_offset({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
  61. default.player_set_animation(player, "stand" , 30)
  62. player:set_properties({visual_size = {x = 1, y = 1} })
  63. end
  64. -------------------------------------------------------------------------------
  65. minetest.register_on_leaveplayer(function(player)
  66. force_detach(player)
  67. end)
  68. minetest.register_on_shutdown(function()
  69. local players = minetest.get_connected_players()
  70. for i = 1, #players do
  71. force_detach(players[i])
  72. end
  73. end)
  74. minetest.register_on_dieplayer(function(player)
  75. force_detach(player)
  76. return true
  77. end)
  78. -------------------------------------------------------------------------------
  79. function mobs.attach(entity, player)
  80. local attach_at, eye_offset = {}, {}
  81. entity.player_rotation = entity.player_rotation or {x = 0, y = 0, z = 0}
  82. entity.driver_attach_at = entity.driver_attach_at or {x = 0, y = 0, z = 0}
  83. entity.driver_eye_offset = entity.driver_eye_offset or {x = 0, y = 0, z = 0}
  84. entity.driver_scale = entity.driver_scale or {x = 1, y = 1}
  85. local rot_view = 0
  86. if entity.player_rotation.y == 90 then
  87. rot_view = math.pi/2
  88. end
  89. attach_at = entity.driver_attach_at
  90. eye_offset = entity.driver_eye_offset
  91. entity.driver = player
  92. force_detach(player)
  93. player:set_attach(entity.object, "", attach_at, entity.player_rotation)
  94. default.player_attached[player:get_player_name()] = true
  95. player:set_eye_offset(eye_offset, {x = 0, y = 0, z = 0})
  96. player:set_properties({
  97. visual_size = {
  98. x = entity.driver_scale.x,
  99. y = entity.driver_scale.y
  100. }
  101. })
  102. minetest.after(0.2, function()
  103. default.player_set_animation(player, "sit" , 30)
  104. end)
  105. --player:set_look_yaw(entity.object:get_yaw() - rot_view)
  106. player:set_look_horizontal(entity.object:get_yaw() - rot_view)
  107. end
  108. function mobs.detach(player, offset)
  109. force_detach(player)
  110. default.player_set_animation(player, "stand" , 30)
  111. local pos = player:get_pos()
  112. pos = {x = pos.x + offset.x, y = pos.y + 0.2 + offset.y, z = pos.z + offset.z}
  113. minetest.after(0.1, function()
  114. player:set_pos(pos)
  115. end)
  116. end
  117. function mobs.drive(entity, moving_anim, stand_anim, can_fly, dtime)
  118. local rot_steer, rot_view = math.pi/2, 0
  119. if entity.player_rotation.y == 90 then
  120. rot_steer, rot_view = 0, math.pi/2
  121. end
  122. local acce_y = 0
  123. local velo = entity.object:get_velocity()
  124. entity.v = get_v(velo) * get_sign(entity.v)
  125. -- process controls
  126. if entity.driver then
  127. --print ("---velo", get_v(velo))
  128. local ctrl = entity.driver:get_player_control()
  129. -- move forwards
  130. if ctrl.up then
  131. entity.v = entity.v + entity.accel / 10
  132. -- move backwards
  133. elseif ctrl.down then
  134. if entity.max_speed_reverse == 0 and entity.v == 0 then
  135. return
  136. end
  137. entity.v = entity.v - entity.accel / 10
  138. end
  139. -- fix mob rotation
  140. local horz = entity.driver:get_look_horizontal() or 0
  141. entity.object:set_yaw(horz - entity.rotate)
  142. if can_fly then
  143. -- fly up
  144. if ctrl.jump then
  145. velo.y = velo.y + 1
  146. if velo.y > entity.accel then velo.y = entity.accel end
  147. elseif velo.y > 0 then
  148. velo.y = velo.y - 0.1
  149. if velo.y < 0 then velo.y = 0 end
  150. end
  151. -- fly down
  152. if ctrl.sneak then
  153. velo.y = velo.y - 1
  154. if velo.y < -entity.accel then velo.y = -entity.accel end
  155. elseif velo.y < 0 then
  156. velo.y = velo.y + 0.1
  157. if velo.y > 0 then velo.y = 0 end
  158. end
  159. else
  160. -- jump
  161. if ctrl.jump then
  162. if velo.y == 0 then
  163. velo.y = velo.y + entity.jump_height
  164. acce_y = acce_y + (acce_y * 3) + 1
  165. end
  166. end
  167. end
  168. end
  169. -- if not moving then set animation and return
  170. if entity.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
  171. if stand_anim then
  172. mobs:set_animation(entity, stand_anim)
  173. end
  174. return
  175. end
  176. -- set moving animation
  177. if moving_anim then
  178. mobs:set_animation(entity, moving_anim)
  179. end
  180. -- Stop!
  181. local s = get_sign(entity.v)
  182. entity.v = entity.v - 0.02 * s
  183. if s ~= get_sign(entity.v) then
  184. entity.object:set_velocity({x = 0, y = 0, z = 0})
  185. entity.v = 0
  186. return
  187. end
  188. -- enforce speed limit forward and reverse
  189. local max_spd = entity.max_speed_reverse
  190. if get_sign(entity.v) >= 0 then
  191. max_spd = entity.max_speed_forward
  192. end
  193. if math.abs(entity.v) > max_spd then
  194. entity.v = entity.v - get_sign(entity.v)
  195. end
  196. -- Set position, velocity and acceleration
  197. local p = entity.object:get_pos()
  198. local new_velo = {x = 0, y = 0, z = 0}
  199. local new_acce = {x = 0, y = -9.8, z = 0}
  200. p.y = p.y - 0.5
  201. local ni = node_is(p)
  202. local v = entity.v
  203. if ni == "air" then
  204. if can_fly == true then
  205. new_acce.y = 0
  206. end
  207. elseif ni == "liquid" or ni == "lava" then
  208. if ni == "lava" and entity.lava_damage ~= 0 then
  209. entity.lava_counter = (entity.lava_counter or 0) + dtime
  210. if entity.lava_counter > 1 then
  211. minetest.sound_play("default_punch", {
  212. object = entity.object,
  213. max_hear_distance = 5
  214. })
  215. entity.object:punch(entity.object, 1.0, {
  216. full_punch_interval = 1.0,
  217. damage_groups = {fleshy = entity.lava_damage}
  218. }, nil)
  219. entity.lava_counter = 0
  220. end
  221. end
  222. if entity.terrain_type == 2
  223. or entity.terrain_type == 3 then
  224. new_acce.y = 0
  225. p.y = p.y + 1
  226. if node_is(p) == "liquid" then
  227. if velo.y >= 5 then
  228. velo.y = 5
  229. elseif velo.y < 0 then
  230. new_acce.y = 20
  231. else
  232. new_acce.y = 5
  233. end
  234. else
  235. if math.abs(velo.y) < 1 then
  236. local pos = entity.object:get_pos()
  237. pos.y = math.floor(pos.y) + 0.5
  238. entity.object:set_pos(pos)
  239. velo.y = 0
  240. end
  241. end
  242. else
  243. v = v * 0.25
  244. end
  245. end
  246. new_velo = get_velocity(v, entity.object:get_yaw() - rot_view, velo.y)
  247. new_acce.y = new_acce.y + acce_y
  248. entity.object:set_velocity(new_velo)
  249. entity.object:set_acceleration(new_acce)
  250. -- CRASH!
  251. if enable_crash then
  252. local intensity = entity.v2 - v
  253. if intensity >= crash_threshold then
  254. --print("----------- crash", intensity)
  255. entity.object:punch(entity.object, 1.0, {
  256. full_punch_interval = 1.0,
  257. damage_groups = {fleshy = intensity}
  258. }, nil)
  259. end
  260. end
  261. entity.v2 = v
  262. end
  263. -- directional flying routine by D00Med (edited by TenPlus1)
  264. function mobs.fly(entity, dtime, speed, shoots, arrow, moving_anim, stand_anim)
  265. local ctrl = entity.driver:get_player_control()
  266. local velo = entity.object:get_velocity()
  267. local dir = entity.driver:get_look_dir()
  268. local yaw = entity.driver:get_look_horizontal() + 1.57 -- offset fix between old and new commands
  269. local rot_steer, rot_view = math.pi / 2, 0
  270. if entity.player_rotation.y == 90 then
  271. rot_steer, rot_view = 0, math.pi / 2
  272. end
  273. if ctrl.up then
  274. entity.object:set_velocity({
  275. x = dir.x * speed,
  276. y = dir.y * speed + 2,
  277. z = dir.z * speed
  278. })
  279. elseif ctrl.down then
  280. entity.object:set_velocity({
  281. x = -dir.x * speed,
  282. y = dir.y * speed + 2,
  283. z = -dir.z * speed
  284. })
  285. elseif not ctrl.down or ctrl.up or ctrl.jump then
  286. entity.object:set_velocity({x = 0, y = -2, z = 0})
  287. end
  288. entity.object:set_yaw(yaw + math.pi + math.pi / 2 - entity.rotate)
  289. -- firing arrows
  290. if ctrl.LMB and ctrl.sneak and shoots then
  291. local pos = entity.object:get_pos()
  292. local obj = minetest.add_entity({
  293. x = pos.x + 0 + dir.x * 2.5,
  294. y = pos.y + 1.5 + dir.y,
  295. z = pos.z + 0 + dir.z * 2.5}, arrow)
  296. local ent = obj:get_luaentity()
  297. if ent then
  298. ent.switch = 1 -- for mob specific arrows
  299. ent.owner_id = tostring(entity.object) -- so arrows dont hurt entity you are riding
  300. local vec = {x = dir.x * 6, y = dir.y * 6, z = dir.z * 6}
  301. local yaw = entity.driver:get_look_horizontal()
  302. obj:set_yaw(yaw + math.pi / 2)
  303. obj:set_velocity(vec)
  304. else
  305. obj:remove()
  306. end
  307. end
  308. -- change animation if stopped
  309. if velo.x == 0 and velo.y == 0 and velo.z == 0 then
  310. mobs:set_animation(entity, stand_anim)
  311. else
  312. -- moving animation
  313. mobs:set_animation(entity, moving_anim)
  314. end
  315. end