init.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. local ANIMATION_SPEED = tonumber(minetest.settings:get("playeranim.animation_speed")) or 2.4
  2. local ANIMATION_SPEED_SNEAK = tonumber(minetest.settings:get("playeranim.animation_speed_sneak")) or 0.8
  3. local BODY_ROTATION_DELAY = math.max(math.floor(tonumber(minetest.settings:get("playeranim.body_rotation_delay")) or 7), 1)
  4. local BODY_X_ROTATION_SNEAK = tonumber(minetest.settings:get("playeranim.body_x_rotation_sneak")) or 6.0
  5. local BONE_POSITION, BONE_ROTATION = (function()
  6. local modname = minetest.get_current_modname()
  7. local modpath = minetest.get_modpath(modname)
  8. return dofile(modpath .. "/model.lua")
  9. end)()
  10. local get_animation = player_api and player_api.get_animation or default.player_get_animation
  11. if not get_animation then
  12. error("player_api.get_animation or default.player_get_animation is not found")
  13. end
  14. local function get_animation_speed(player)
  15. if player:get_player_control().sneak then
  16. return ANIMATION_SPEED_SNEAK
  17. end
  18. return ANIMATION_SPEED
  19. end
  20. local math_deg = math.deg
  21. local function get_pitch_deg(player)
  22. return math_deg(player:get_look_vertical())
  23. end
  24. local players_animation_data = setmetatable({}, {
  25. __index = {
  26. init_player = function(self, player)
  27. self[player] = {
  28. time = 0,
  29. yaw_history = {},
  30. bone_rotations = {},
  31. bone_positions = {},
  32. previous_animation = 0,
  33. }
  34. end,
  35. -- time
  36. get_time = function(self, player)
  37. return self[player].time
  38. end,
  39. increment_time = function(self, player, dtime)
  40. self[player].time = self:get_time(player) + dtime
  41. end,
  42. reset_time = function(self, player)
  43. self[player].time = 0
  44. end,
  45. -- yaw_history
  46. get_yaw_history = function(self, player)
  47. return self[player].yaw_history -- Return mutable reference
  48. end,
  49. add_yaw_to_history = function(self, player)
  50. local yaw = player:get_look_horizontal()
  51. local history = self:get_yaw_history(player)
  52. history[#history + 1] = yaw
  53. end,
  54. clear_yaw_history = function(self, player)
  55. if #self[player].yaw_history > 0 then
  56. self[player].yaw_history = {}
  57. end
  58. end,
  59. -- bone_rotations
  60. get_bone_rotation = function(self, player, bone)
  61. return self[player].bone_rotations[bone]
  62. end,
  63. set_bone_rotation = function(self, player, bone, rotation)
  64. self[player].bone_rotations[bone] = rotation
  65. end,
  66. -- bone_positions
  67. get_bone_position = function(self, player, bone)
  68. return self[player].bone_positions[bone]
  69. end,
  70. set_bone_position = function(self, player, bone, position)
  71. self[player].bone_positions[bone] = position
  72. end,
  73. -- previous_animation
  74. get_previous_animation = function(self, player)
  75. return self[player].previous_animation
  76. end,
  77. set_previous_animation = function(self, player, animation)
  78. self[player].previous_animation = animation
  79. end,
  80. }
  81. })
  82. minetest.register_on_joinplayer(function(player)
  83. players_animation_data:init_player(player)
  84. end)
  85. local vector_add, vector_equals = vector.add, vector.equals
  86. local function rotate_bone(player, bone, rotation, position_optional)
  87. local previous_rotation = players_animation_data:get_bone_rotation(player, bone)
  88. local rotation = vector_add(rotation, BONE_ROTATION[bone])
  89. local previous_position = players_animation_data:get_bone_position(player, bone)
  90. local position = BONE_POSITION[bone]
  91. if position_optional then
  92. position = vector_add(position, position_optional)
  93. end
  94. if not previous_rotation
  95. or not previous_position
  96. or not vector_equals(rotation, previous_rotation)
  97. or not vector_equals(position, previous_position) then
  98. player:set_bone_position(bone, position, rotation)
  99. players_animation_data:set_bone_rotation(player, bone, rotation)
  100. players_animation_data:set_bone_position(player, bone, position)
  101. end
  102. end
  103. -- Animation alias
  104. local STAND = 1
  105. local WALK = 2
  106. local MINE = 3
  107. local WALK_MINE = 4
  108. local SIT = 5
  109. local LAY = 6
  110. -- Bone alias
  111. local BODY = "Body"
  112. local HEAD = "Head"
  113. local CAPE = "Cape"
  114. local LARM = "Arm_Left"
  115. local RARM = "Arm_Right"
  116. local LLEG = "Leg_Left"
  117. local RLEG = "Leg_Right"
  118. local math_sin, math_cos, math_pi = math.sin, math.cos, math.pi
  119. local ANIMATIONS = {
  120. [STAND] = function(player, _time)
  121. rotate_bone(player, BODY, {x = 0, y = 0, z = 0})
  122. rotate_bone(player, CAPE, {x = 0, y = 0, z = 0})
  123. rotate_bone(player, LARM, {x = 0, y = 0, z = 0})
  124. rotate_bone(player, RARM, {x = 0, y = 0, z = 0})
  125. rotate_bone(player, LLEG, {x = 0, y = 0, z = 0})
  126. rotate_bone(player, RLEG, {x = 0, y = 0, z = 0})
  127. end,
  128. [LAY] = function(player, _time)
  129. rotate_bone(player, HEAD, {x = 0, y = 0, z = 0})
  130. rotate_bone(player, CAPE, {x = 0, y = 0, z = 0})
  131. rotate_bone(player, LARM, {x = 0, y = 0, z = 0})
  132. rotate_bone(player, RARM, {x = 0, y = 0, z = 0})
  133. rotate_bone(player, LLEG, {x = 0, y = 0, z = 0})
  134. rotate_bone(player, RLEG, {x = 0, y = 0, z = 0})
  135. rotate_bone(player, BODY, BONE_ROTATION.body_lay, BONE_POSITION.body_lay)
  136. end,
  137. [SIT] = function(player, _time)
  138. rotate_bone(player, LARM, {x = 0, y = 0, z = 0})
  139. rotate_bone(player, RARM, {x = 0, y = 0, z = 0})
  140. rotate_bone(player, LLEG, {x = 90, y = 0, z = 0})
  141. rotate_bone(player, RLEG, {x = 90, y = 0, z = 0})
  142. rotate_bone(player, BODY, BONE_ROTATION.body_sit, BONE_POSITION.body_sit)
  143. end,
  144. [WALK] = function(player, time)
  145. local speed = get_animation_speed(player)
  146. local sin = math_sin(time * speed * math_pi)
  147. rotate_bone(player, CAPE, {x = -35 * sin - 35, y = 0, z = 0})
  148. rotate_bone(player, LARM, {x = -55 * sin, y = 0, z = 0})
  149. rotate_bone(player, RARM, {x = 55 * sin, y = 0, z = 0})
  150. rotate_bone(player, LLEG, {x = 55 * sin, y = 0, z = 0})
  151. rotate_bone(player, RLEG, {x = -55 * sin, y = 0, z = 0})
  152. end,
  153. [MINE] = function(player, time)
  154. local speed = get_animation_speed(player)
  155. local cape_sin = math_sin(time * speed * math_pi)
  156. local rarm_sin = math_sin(2 * time * speed * math_pi)
  157. local rarm_cos = -math_cos(2 * time * speed * math_pi)
  158. local pitch = 90 - get_pitch_deg(player)
  159. rotate_bone(player, CAPE, {x = -5 * cape_sin - 5, y = 0, z = 0})
  160. rotate_bone(player, LARM, {x = 0, y = 0, z = 0})
  161. rotate_bone(player, RARM, {x = 10 * rarm_sin + pitch, y = 10 * rarm_cos, z = 0})
  162. rotate_bone(player, LLEG, {x = 0, y = 0, z = 0})
  163. rotate_bone(player, RLEG, {x = 0, y = 0, z = 0})
  164. end,
  165. [WALK_MINE] = function(player, time)
  166. local speed = get_animation_speed(player)
  167. local sin = math_sin(time * speed * math_pi)
  168. local rarm_sin = math_sin(2 * time * speed * math_pi)
  169. local rarm_cos = -math_cos(2 * time * speed * math_pi)
  170. local pitch = 90 - get_pitch_deg(player)
  171. rotate_bone(player, CAPE, {x = -35 * sin - 35, y = 0, z = 0})
  172. rotate_bone(player, LARM, {x = -55 * sin, y = 0, z = 0})
  173. rotate_bone(player, RARM, {x = 10 * rarm_sin + pitch, y = 10 * rarm_cos, z = 0})
  174. rotate_bone(player, LLEG, {x = 55 * sin, y = 0, z = 0})
  175. rotate_bone(player, RLEG, {x = -55 * sin, y = 0, z = 0})
  176. end,
  177. }
  178. local function set_animation(player, animation, force_animate)
  179. local animation_changed
  180. = (players_animation_data:get_previous_animation(player) ~= animation)
  181. if force_animate or animation_changed then
  182. players_animation_data:set_previous_animation(player, animation)
  183. ANIMATIONS[animation](player, players_animation_data:get_time(player))
  184. end
  185. end
  186. local function rotate_head(player)
  187. local head_x_rotation = -get_pitch_deg(player)
  188. rotate_bone(player, HEAD, {x = head_x_rotation, y = 0, z = 0})
  189. end
  190. local table_remove, math_deg = table.remove, math.deg
  191. local function rotate_body_and_head(player)
  192. local body_x_rotation = (function()
  193. local sneak = player:get_player_control().sneak
  194. return sneak and BODY_X_ROTATION_SNEAK or 0
  195. end)()
  196. local body_y_rotation = (function()
  197. local yaw_history = players_animation_data:get_yaw_history(player)
  198. if #yaw_history > BODY_ROTATION_DELAY then
  199. local body_yaw = table_remove(yaw_history, 1)
  200. local player_yaw = player:get_look_horizontal()
  201. return math_deg(player_yaw - body_yaw)
  202. end
  203. return 0
  204. end)()
  205. rotate_bone(player, BODY, {x = body_x_rotation, y = body_y_rotation, z = 0})
  206. local head_x_rotation = -get_pitch_deg(player)
  207. rotate_bone(player, HEAD, {x = head_x_rotation, y = -body_y_rotation, z = 0})
  208. end
  209. local function animate_player(player, dtime)
  210. local animation = get_animation(player).animation
  211. -- Yaw history
  212. if animation == "lay" or animation == "sit" then
  213. players_animation_data:clear_yaw_history(player)
  214. else
  215. players_animation_data:add_yaw_to_history(player)
  216. end
  217. -- Increment animation time
  218. if animation == "walk"
  219. or animation == "mine"
  220. or animation == "walk_mine" then
  221. players_animation_data:increment_time(player, dtime)
  222. else
  223. players_animation_data:reset_time(player)
  224. end
  225. -- Set animation
  226. if animation == "stand" then
  227. set_animation(player, STAND)
  228. elseif animation == "lay" then
  229. set_animation(player, LAY)
  230. elseif animation == "sit" then
  231. set_animation(player, SIT)
  232. elseif animation == "walk" then
  233. set_animation(player, WALK, true)
  234. elseif animation == "mine" then
  235. set_animation(player, MINE, true)
  236. elseif animation == "walk_mine" then
  237. set_animation(player, WALK_MINE, true)
  238. end
  239. -- Rotate body and head
  240. if animation == "lay" then
  241. -- Do nothing
  242. elseif animation == "sit" then
  243. rotate_head(player)
  244. else
  245. rotate_body_and_head(player)
  246. end
  247. end
  248. local minetest_get_connected_players = minetest.get_connected_players
  249. minetest.register_globalstep(function(dtime)
  250. for _, player in ipairs(minetest_get_connected_players()) do
  251. animate_player(player, dtime)
  252. end
  253. end)