mount.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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:getyaw() - rot_view)
  106. player:set_look_horizontal(entity.object:getyaw() - 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:getpos()
  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:setpos(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:getvelocity()
  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:setyaw(entity.driver:get_look_yaw() - entity.rotate)
  141. entity.object:setyaw(entity.driver:get_look_horizontal() - 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:setvelocity({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:getpos()
  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:getpos()
  237. pos.y = math.floor(pos.y) + 0.5
  238. entity.object:setpos(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:getyaw() - rot_view, velo.y)
  247. new_acce.y = new_acce.y + acce_y
  248. entity.object:setvelocity(new_velo)
  249. entity.object:setacceleration(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:getvelocity()
  267. local dir = entity.driver:get_look_dir()
  268. -- local yaw = entity.driver:get_look_yaw()
  269. local yaw = entity.driver:get_look_horizontal() + 1.57 -- offset fix between old and new commands
  270. local rot_steer, rot_view = math.pi / 2, 0
  271. if entity.player_rotation.y == 90 then
  272. rot_steer, rot_view = 0, math.pi / 2
  273. end
  274. if ctrl.up then
  275. entity.object:setvelocity({
  276. x = dir.x * speed,
  277. y = dir.y * speed + 2,
  278. z = dir.z * speed
  279. })
  280. elseif ctrl.down then
  281. entity.object:setvelocity({
  282. x = -dir.x * speed,
  283. y = dir.y * speed + 2,
  284. z = -dir.z * speed
  285. })
  286. elseif not ctrl.down or ctrl.up or ctrl.jump then
  287. entity.object:setvelocity({x = 0, y = -2, z = 0})
  288. end
  289. entity.object:setyaw(yaw + math.pi + math.pi / 2 - entity.rotate)
  290. -- firing arrows
  291. if ctrl.LMB and ctrl.sneak and shoots then
  292. local pos = entity.object:getpos()
  293. local obj = minetest.add_entity({
  294. x = pos.x + 0 + dir.x * 2.5,
  295. y = pos.y + 1.5 + dir.y,
  296. z = pos.z + 0 + dir.z * 2.5}, arrow)
  297. local ent = obj:get_luaentity()
  298. if ent then
  299. ent.switch = 1 -- for mob specific arrows
  300. ent.owner_id = tostring(entity.object) -- so arrows dont hurt entity you are riding
  301. local vec = {x = dir.x * 6, y = dir.y * 6, z = dir.z * 6}
  302. -- local yaw = entity.driver:get_look_yaw()
  303. local yaw = entity.driver:get_look_horizontal()
  304. obj:setyaw(yaw + math.pi / 2)
  305. obj:setvelocity(vec)
  306. else
  307. obj:remove()
  308. end
  309. end
  310. -- change animation if stopped
  311. if velo.x == 0 and velo.y == 0 and velo.z == 0 then
  312. mobs:set_animation(entity, stand_anim)
  313. else
  314. -- moving animation
  315. mobs:set_animation(entity, moving_anim)
  316. end
  317. end