mount.lua 9.6 KB

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