mount.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. entity.object:set_yaw(entity.driver:get_look_horizontal() - entity.rotate)
  141. if can_fly then
  142. -- fly up
  143. if ctrl.jump then
  144. velo.y = velo.y + 1
  145. if velo.y > entity.accel then velo.y = entity.accel end
  146. elseif velo.y > 0 then
  147. velo.y = velo.y - 0.1
  148. if velo.y < 0 then velo.y = 0 end
  149. end
  150. -- fly down
  151. if ctrl.sneak then
  152. velo.y = velo.y - 1
  153. if velo.y < -entity.accel then velo.y = -entity.accel end
  154. elseif velo.y < 0 then
  155. velo.y = velo.y + 0.1
  156. if velo.y > 0 then velo.y = 0 end
  157. end
  158. else
  159. -- jump
  160. if ctrl.jump then
  161. if velo.y == 0 then
  162. velo.y = velo.y + entity.jump_height
  163. acce_y = acce_y + (acce_y * 3) + 1
  164. end
  165. end
  166. end
  167. end
  168. -- if not moving then set animation and return
  169. if entity.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
  170. if stand_anim then
  171. mobs:set_animation(entity, stand_anim)
  172. end
  173. return
  174. end
  175. -- set moving animation
  176. if moving_anim then
  177. mobs:set_animation(entity, moving_anim)
  178. end
  179. -- Stop!
  180. local s = get_sign(entity.v)
  181. entity.v = entity.v - 0.02 * s
  182. if s ~= get_sign(entity.v) then
  183. entity.object:set_velocity({x = 0, y = 0, z = 0})
  184. entity.v = 0
  185. return
  186. end
  187. -- enforce speed limit forward and reverse
  188. local max_spd = entity.max_speed_reverse
  189. if get_sign(entity.v) >= 0 then
  190. max_spd = entity.max_speed_forward
  191. end
  192. if math.abs(entity.v) > max_spd then
  193. entity.v = entity.v - get_sign(entity.v)
  194. end
  195. -- Set position, velocity and acceleration
  196. local p = entity.object:get_pos()
  197. local new_velo = {x = 0, y = 0, z = 0}
  198. local new_acce = {x = 0, y = -9.8, z = 0}
  199. p.y = p.y - 0.5
  200. local ni = node_is(p)
  201. local v = entity.v
  202. if ni == "air" then
  203. if can_fly == true then
  204. new_acce.y = 0
  205. end
  206. elseif ni == "liquid" or ni == "lava" then
  207. if ni == "lava" and entity.lava_damage ~= 0 then
  208. entity.lava_counter = (entity.lava_counter or 0) + dtime
  209. if entity.lava_counter > 1 then
  210. minetest.sound_play("default_punch", {
  211. object = entity.object,
  212. max_hear_distance = 5
  213. })
  214. entity.object:punch(entity.object, 1.0, {
  215. full_punch_interval = 1.0,
  216. damage_groups = {fleshy = entity.lava_damage}
  217. }, nil)
  218. entity.lava_counter = 0
  219. end
  220. end
  221. if entity.terrain_type == 2
  222. or entity.terrain_type == 3 then
  223. new_acce.y = 0
  224. p.y = p.y + 1
  225. if node_is(p) == "liquid" then
  226. if velo.y >= 5 then
  227. velo.y = 5
  228. elseif velo.y < 0 then
  229. new_acce.y = 20
  230. else
  231. new_acce.y = 5
  232. end
  233. else
  234. if math.abs(velo.y) < 1 then
  235. local pos = entity.object:get_pos()
  236. pos.y = math.floor(pos.y) + 0.5
  237. entity.object:set_pos(pos)
  238. velo.y = 0
  239. end
  240. end
  241. else
  242. v = v * 0.25
  243. end
  244. end
  245. new_velo = get_velocity(v, entity.object:get_yaw() - rot_view, velo.y)
  246. new_acce.y = new_acce.y + acce_y
  247. entity.object:set_velocity(new_velo)
  248. entity.object:set_acceleration(new_acce)
  249. -- CRASH!
  250. if enable_crash then
  251. local intensity = entity.v2 - v
  252. if intensity >= crash_threshold then
  253. --print("----------- crash", intensity)
  254. entity.object:punch(entity.object, 1.0, {
  255. full_punch_interval = 1.0,
  256. damage_groups = {fleshy = intensity}
  257. }, nil)
  258. end
  259. end
  260. entity.v2 = v
  261. end
  262. -- directional flying routine by D00Med (edited by TenPlus1)
  263. function mobs.fly(entity, dtime, speed, shoots, arrow, moving_anim, stand_anim)
  264. local ctrl = entity.driver:get_player_control()
  265. local velo = entity.object:get_velocity()
  266. local dir = entity.driver:get_look_dir()
  267. local yaw = entity.driver:get_look_horizontal() + 1.57 -- offset fix between old and new commands
  268. local rot_steer, rot_view = math.pi / 2, 0
  269. if entity.player_rotation.y == 90 then
  270. rot_steer, rot_view = 0, math.pi / 2
  271. end
  272. if ctrl.up then
  273. entity.object:set_velocity({
  274. x = dir.x * speed,
  275. y = dir.y * speed + 2,
  276. z = dir.z * speed
  277. })
  278. elseif ctrl.down then
  279. entity.object:set_velocity({
  280. x = -dir.x * speed,
  281. y = dir.y * speed + 2,
  282. z = -dir.z * speed
  283. })
  284. elseif not ctrl.down or ctrl.up or ctrl.jump then
  285. entity.object:set_velocity({x = 0, y = -2, z = 0})
  286. end
  287. entity.object:set_yaw(yaw + math.pi + math.pi / 2 - entity.rotate)
  288. -- firing arrows
  289. if ctrl.LMB and ctrl.sneak and shoots then
  290. local pos = entity.object:get_pos()
  291. local obj = minetest.add_entity({
  292. x = pos.x + 0 + dir.x * 2.5,
  293. y = pos.y + 1.5 + dir.y,
  294. z = pos.z + 0 + dir.z * 2.5}, arrow)
  295. local ent = obj:get_luaentity()
  296. if ent then
  297. ent.switch = 1 -- for mob specific arrows
  298. ent.owner_id = tostring(entity.object) -- so arrows dont hurt entity you are riding
  299. local vec = {x = dir.x * 6, y = dir.y * 6, z = dir.z * 6}
  300. local yaw = entity.driver:get_look_horizontal()
  301. obj:set_yaw(yaw + math.pi / 2)
  302. obj:set_velocity(vec)
  303. else
  304. obj:remove()
  305. end
  306. end
  307. -- change animation if stopped
  308. if velo.x == 0 and velo.y == 0 and velo.z == 0 then
  309. mobs:set_animation(entity, stand_anim)
  310. else
  311. -- moving animation
  312. mobs:set_animation(entity, moving_anim)
  313. end
  314. end