api.lua 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. player_api = {}
  2. -- Player animation blending
  3. -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
  4. local animation_blend = 0
  5. player_api.registered_models = {}
  6. -- Local for speed.
  7. local models = player_api.registered_models
  8. local function collisionbox_equals(collisionbox, other_collisionbox)
  9. if collisionbox == other_collisionbox then
  10. return true
  11. end
  12. for index = 1, 6 do
  13. if collisionbox[index] ~= other_collisionbox[index] then
  14. return false
  15. end
  16. end
  17. return true
  18. end
  19. function player_api.register_model(name, def)
  20. models[name] = def
  21. def.visual_size = def.visual_size or {x = 1, y = 1}
  22. def.collisionbox = def.collisionbox or {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3}
  23. def.stepheight = def.stepheight or 0.6
  24. def.eye_height = def.eye_height or 1.47
  25. -- Sort animations into property classes:
  26. -- Animations with same properties have the same _equals value
  27. for animation_name, animation in pairs(def.animations) do
  28. animation.eye_height = animation.eye_height or def.eye_height
  29. animation.collisionbox = animation.collisionbox or def.collisionbox
  30. animation.override_local = animation.override_local or false
  31. for _, other_animation in pairs(def.animations) do
  32. if other_animation._equals then
  33. if collisionbox_equals(animation.collisionbox, other_animation.collisionbox)
  34. and animation.eye_height == other_animation.eye_height then
  35. animation._equals = other_animation._equals
  36. break
  37. end
  38. end
  39. end
  40. animation._equals = animation._equals or animation_name
  41. end
  42. end
  43. -- Player stats and animations
  44. -- model, textures, animation
  45. local players = {}
  46. player_api.player_attached = {}
  47. local function get_player_data(player)
  48. return assert(players[player:get_player_name()])
  49. end
  50. function player_api.get_animation(player)
  51. return get_player_data(player)
  52. end
  53. -- Called when a player's appearance needs to be updated
  54. function player_api.set_model(player, model_name)
  55. local player_data = get_player_data(player)
  56. if player_data.model == model_name then
  57. return
  58. end
  59. -- Update data
  60. player_data.model = model_name
  61. -- Clear animation data as the model has changed
  62. -- (required for setting the `stand` animation not to be a no-op)
  63. player_data.animation, player_data.animation_speed, player_data.animation_loop = nil, nil, nil
  64. local model = models[model_name]
  65. if model then
  66. player:set_properties({
  67. mesh = model_name,
  68. textures = player_data.textures or model.textures,
  69. visual = "mesh",
  70. visual_size = model.visual_size,
  71. stepheight = model.stepheight
  72. })
  73. -- sets local_animation, collisionbox & eye_height
  74. player_api.set_animation(player, "stand")
  75. else
  76. player:set_properties({
  77. textures = {"player.png", "player_back.png"},
  78. visual = "upright_sprite",
  79. visual_size = {x = 1, y = 2},
  80. collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.75, 0.3},
  81. stepheight = 0.6,
  82. eye_height = 1.625,
  83. })
  84. end
  85. end
  86. function player_api.get_textures(player)
  87. local player_data = get_player_data(player)
  88. local model = models[player_data.model]
  89. return assert(player_data.textures or (model and model.textures))
  90. end
  91. function player_api.set_textures(player, textures)
  92. local player_data = get_player_data(player)
  93. local model = models[player_data.model]
  94. local new_textures = assert(textures or (model and model.textures))
  95. player_data.textures = new_textures
  96. player:set_properties({textures = new_textures})
  97. end
  98. function player_api.set_texture(player, index, texture)
  99. local textures = table.copy(player_api.get_textures(player))
  100. textures[index] = texture
  101. player_api.set_textures(player, textures)
  102. end
  103. function player_api.set_animation(player, anim_name, speed, loop)
  104. local player_data = get_player_data(player)
  105. local model = models[player_data.model]
  106. if not (model and model.animations[anim_name]) then
  107. return
  108. end
  109. speed = speed or model.animation_speed
  110. if loop == nil then
  111. loop = true
  112. end
  113. if player_data.animation == anim_name
  114. and player_data.animation_speed == speed
  115. and player_data.animation_loop == loop
  116. then
  117. return
  118. end
  119. local previous_anim = model.animations[player_data.animation] or {}
  120. local anim = model.animations[anim_name]
  121. player_data.animation = anim_name
  122. player_data.animation_speed = speed
  123. player_data.animation_loop = loop
  124. -- If necessary change the local animation (only seen by the client of *that* player)
  125. -- `override_local` <=> suspend local animations while this one is active
  126. -- (this is basically a hack, proper engine feature needed...)
  127. if anim.override_local ~= previous_anim.override_local then
  128. if anim.override_local then
  129. local none = {x=0, y=0}
  130. player:set_local_animation(none, none, none, none, 1)
  131. else
  132. local a = model.animations -- (not specific to the animation being set)
  133. player:set_local_animation(
  134. a.stand, a.walk, a.mine, a.walk_mine,
  135. model.animation_speed or 30
  136. )
  137. end
  138. end
  139. -- Set the animation seen by everyone else
  140. player:set_animation(anim, speed, animation_blend, loop)
  141. -- Update related properties if they changed
  142. if anim._equals ~= previous_anim._equals then
  143. player:set_properties({
  144. collisionbox = anim.collisionbox,
  145. eye_height = anim.eye_height
  146. })
  147. end
  148. end
  149. minetest.register_on_joinplayer(function(player)
  150. local name = player:get_player_name()
  151. players[name] = {}
  152. player_api.player_attached[name] = false
  153. end)
  154. minetest.register_on_leaveplayer(function(player)
  155. local name = player:get_player_name()
  156. players[name] = nil
  157. player_api.player_attached[name] = nil
  158. end)
  159. -- Localize for better performance.
  160. local player_set_animation = player_api.set_animation
  161. local player_attached = player_api.player_attached
  162. -- Prevent knockback for attached players
  163. local old_calculate_knockback = minetest.calculate_knockback
  164. function minetest.calculate_knockback(player, ...)
  165. if player_attached[player:get_player_name()] then
  166. return 0
  167. end
  168. return old_calculate_knockback(player, ...)
  169. end
  170. -- Check each player and apply animations
  171. function player_api.globalstep()
  172. for _, player in ipairs(minetest.get_connected_players()) do
  173. local name = player:get_player_name()
  174. local player_data = players[name]
  175. local model = player_data and models[player_data.model]
  176. if model and not player_attached[name] then
  177. local controls = player:get_player_control()
  178. local animation_speed_mod = model.animation_speed or 30
  179. -- Determine if the player is sneaking, and reduce animation speed if so
  180. if controls.sneak then
  181. animation_speed_mod = animation_speed_mod / 2
  182. end
  183. -- Apply animations based on what the player is doing
  184. if player:get_hp() == 0 then
  185. player_set_animation(player, "lay")
  186. elseif controls.up or controls.down or controls.left or controls.right then
  187. if controls.LMB or controls.RMB then
  188. player_set_animation(player, "walk_mine", animation_speed_mod)
  189. else
  190. player_set_animation(player, "walk", animation_speed_mod)
  191. end
  192. elseif controls.LMB or controls.RMB then
  193. player_set_animation(player, "mine", animation_speed_mod)
  194. else
  195. player_set_animation(player, "stand", animation_speed_mod)
  196. end
  197. end
  198. end
  199. end
  200. -- Mods can modify the globalstep by overriding player_api.globalstep
  201. minetest.register_globalstep(function(...)
  202. player_api.globalstep(...)
  203. end)
  204. for _, api_function in pairs({"get_animation", "set_animation", "set_model", "set_textures"}) do
  205. local original_function = player_api[api_function]
  206. player_api[api_function] = function(player, ...)
  207. if not players[player:get_player_name()] then
  208. -- HACK for keeping backwards compatibility
  209. minetest.log("warning", api_function .. " called on offline player")
  210. return
  211. end
  212. return original_function(player, ...)
  213. end
  214. end