2
0

api.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. player_data.model = model_name
  60. local model = models[model_name]
  61. if model then
  62. player:set_properties({
  63. mesh = model_name,
  64. textures = player_data.textures or model.textures,
  65. visual = "mesh",
  66. visual_size = model.visual_size,
  67. stepheight = model.stepheight
  68. })
  69. -- sets local_animation, collisionbox & eye_height
  70. player_api.set_animation(player, "stand")
  71. else
  72. player:set_properties({
  73. textures = {"player.png", "player_back.png"},
  74. visual = "upright_sprite",
  75. visual_size = {x = 1, y = 2},
  76. collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.75, 0.3},
  77. stepheight = 0.6,
  78. eye_height = 1.625,
  79. })
  80. end
  81. end
  82. function player_api.get_textures(player)
  83. local player_data = get_player_data(player)
  84. local model = models[player_data.model]
  85. return assert(player_data.textures or (model and model.textures))
  86. end
  87. function player_api.set_textures(player, textures)
  88. local player_data = get_player_data(player)
  89. local model = models[player_data.model]
  90. local new_textures = assert(textures or (model and model.textures))
  91. player_data.textures = new_textures
  92. player:set_properties({textures = new_textures})
  93. end
  94. function player_api.set_texture(player, index, texture)
  95. local textures = table.copy(player_api.get_textures(player))
  96. textures[index] = texture
  97. player_api.set_textures(player, textures)
  98. end
  99. function player_api.set_animation(player, anim_name, speed)
  100. local player_data = get_player_data(player)
  101. local model = models[player_data.model]
  102. if not (model and model.animations[anim_name]) then
  103. return
  104. end
  105. speed = speed or model.animation_speed
  106. if player_data.animation == anim_name and player_data.animation_speed == speed then
  107. return
  108. end
  109. local previous_anim = model.animations[player_data.animation] or {}
  110. local anim = model.animations[anim_name]
  111. player_data.animation = anim_name
  112. player_data.animation_speed = speed
  113. -- If necessary change the local animation (only seen by the client of *that* player)
  114. -- `override_local` <=> suspend local animations while this one is active
  115. -- (this is basically a hack, proper engine feature needed...)
  116. if anim.override_local ~= previous_anim.override_local then
  117. if anim.override_local then
  118. local none = {x=0, y=0}
  119. player:set_local_animation(none, none, none, none, 1)
  120. else
  121. local a = model.animations -- (not specific to the animation being set)
  122. player:set_local_animation(
  123. a.stand, a.walk, a.mine, a.walk_mine,
  124. model.animation_speed or 30
  125. )
  126. end
  127. end
  128. -- Set the animation seen by everyone else
  129. player:set_animation(anim, speed, animation_blend)
  130. -- Update related properties if they changed
  131. if anim._equals ~= previous_anim._equals then
  132. player:set_properties({
  133. collisionbox = anim.collisionbox,
  134. eye_height = anim.eye_height
  135. })
  136. end
  137. end
  138. minetest.register_on_joinplayer(function(player)
  139. local name = player:get_player_name()
  140. players[name] = {}
  141. player_api.player_attached[name] = false
  142. end)
  143. minetest.register_on_leaveplayer(function(player)
  144. local name = player:get_player_name()
  145. players[name] = nil
  146. player_api.player_attached[name] = nil
  147. end)
  148. -- Localize for better performance.
  149. local player_set_animation = player_api.set_animation
  150. local player_attached = player_api.player_attached
  151. -- Prevent knockback for attached players
  152. local old_calculate_knockback = minetest.calculate_knockback
  153. function minetest.calculate_knockback(player, ...)
  154. if player_attached[player:get_player_name()] then
  155. return 0
  156. end
  157. return old_calculate_knockback(player, ...)
  158. end
  159. -- Check each player and apply animations
  160. function player_api.globalstep()
  161. for _, player in ipairs(minetest.get_connected_players()) do
  162. local name = player:get_player_name()
  163. local player_data = players[name]
  164. local model = player_data and models[player_data.model]
  165. if model and not player_attached[name] then
  166. local controls = player:get_player_control()
  167. local animation_speed_mod = model.animation_speed or 30
  168. -- Determine if the player is sneaking, and reduce animation speed if so
  169. if controls.sneak then
  170. animation_speed_mod = animation_speed_mod / 2
  171. end
  172. -- Apply animations based on what the player is doing
  173. if player:get_hp() == 0 then
  174. player_set_animation(player, "lay")
  175. elseif controls.up or controls.down or controls.left or controls.right then
  176. if controls.LMB or controls.RMB then
  177. player_set_animation(player, "walk_mine", animation_speed_mod)
  178. else
  179. player_set_animation(player, "walk", animation_speed_mod)
  180. end
  181. elseif controls.LMB or controls.RMB then
  182. player_set_animation(player, "mine", animation_speed_mod)
  183. else
  184. player_set_animation(player, "stand", animation_speed_mod)
  185. end
  186. end
  187. end
  188. end
  189. -- Mods can modify the globalstep by overriding player_api.globalstep
  190. minetest.register_globalstep(function(...)
  191. player_api.globalstep(...)
  192. end)
  193. for _, api_function in pairs({"get_animation", "set_animation", "set_model", "set_textures"}) do
  194. local original_function = player_api[api_function]
  195. player_api[api_function] = function(player, ...)
  196. if not players[player:get_player_name()] then
  197. -- HACK for keeping backwards compatibility
  198. minetest.log("warning", api_function .. " called on offline player")
  199. return
  200. end
  201. return original_function(player, ...)
  202. end
  203. end