api.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 = 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)
  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 player_data.animation == anim_name and player_data.animation_speed == speed then
  111. return
  112. end
  113. local previous_anim = model.animations[player_data.animation] or {}
  114. local anim = model.animations[anim_name]
  115. player_data.animation = anim_name
  116. player_data.animation_speed = speed
  117. -- If necessary change the local animation (only seen by the client of *that* player)
  118. -- `override_local` <=> suspend local animations while this one is active
  119. -- (this is basically a hack, proper engine feature needed...)
  120. if anim.override_local ~= previous_anim.override_local then
  121. if anim.override_local then
  122. local none = {x=0, y=0}
  123. player:set_local_animation(none, none, none, none, 1)
  124. else
  125. local a = model.animations -- (not specific to the animation being set)
  126. player:set_local_animation(
  127. a.stand, a.walk, a.mine, a.walk_mine,
  128. model.animation_speed or 30
  129. )
  130. end
  131. end
  132. -- Set the animation seen by everyone else
  133. player:set_animation(anim, speed, animation_blend)
  134. -- Update related properties if they changed
  135. if anim._equals ~= previous_anim._equals then
  136. player:set_properties({
  137. collisionbox = anim.collisionbox,
  138. eye_height = anim.eye_height
  139. })
  140. end
  141. end
  142. minetest.register_on_joinplayer(function(player)
  143. local name = player:get_player_name()
  144. players[name] = {}
  145. player_api.player_attached[name] = false
  146. end)
  147. minetest.register_on_leaveplayer(function(player)
  148. local name = player:get_player_name()
  149. players[name] = nil
  150. player_api.player_attached[name] = nil
  151. end)
  152. -- Localize for better performance.
  153. local player_set_animation = player_api.set_animation
  154. local player_attached = player_api.player_attached
  155. -- Prevent knockback for attached players
  156. local old_calculate_knockback = minetest.calculate_knockback
  157. function minetest.calculate_knockback(player, ...)
  158. if player_attached[player:get_player_name()] then
  159. return 0
  160. end
  161. return old_calculate_knockback(player, ...)
  162. end
  163. -- Check each player and apply animations
  164. function player_api.globalstep()
  165. for _, player in ipairs(minetest.get_connected_players()) do
  166. local name = player:get_player_name()
  167. local player_data = players[name]
  168. local model = player_data and models[player_data.model]
  169. if model and not player_attached[name] then
  170. local controls = player:get_player_control()
  171. local animation_speed_mod = model.animation_speed or 30
  172. -- Determine if the player is sneaking, and reduce animation speed if so
  173. if controls.sneak then
  174. animation_speed_mod = animation_speed_mod / 2
  175. end
  176. -- Apply animations based on what the player is doing
  177. if player:get_hp() == 0 then
  178. player_set_animation(player, "lay")
  179. elseif controls.up or controls.down or controls.left or controls.right then
  180. if controls.LMB or controls.RMB then
  181. player_set_animation(player, "walk_mine", animation_speed_mod)
  182. else
  183. player_set_animation(player, "walk", animation_speed_mod)
  184. end
  185. elseif controls.LMB or controls.RMB then
  186. player_set_animation(player, "mine", animation_speed_mod)
  187. else
  188. player_set_animation(player, "stand", animation_speed_mod)
  189. end
  190. end
  191. end
  192. end
  193. -- Mods can modify the globalstep by overriding player_api.globalstep
  194. minetest.register_globalstep(function(...)
  195. player_api.globalstep(...)
  196. end)
  197. for _, api_function in pairs({"get_animation", "set_animation", "set_model", "set_textures"}) do
  198. local original_function = player_api[api_function]
  199. player_api[api_function] = function(player, ...)
  200. if not players[player:get_player_name()] then
  201. -- HACK for keeping backwards compatibility
  202. minetest.log("warning", api_function .. " called on offline player")
  203. return
  204. end
  205. return original_function(player, ...)
  206. end
  207. end