1 -- Minetest 0.4 mod: player
2 -- See README.txt for licensing and other information.
6 -- Player animation blending
7 -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
8 local animation_blend = 0
10 player_api.registered_models = { }
13 local models = player_api.registered_models
15 function player_api.register_model(name, def)
19 -- Player stats and animations
20 local player_model = {}
21 local player_textures = {}
22 local wielded_item = {}
23 local player_anim = {}
24 local player_sneak = {}
25 player_api.player_attached = {}
28 --modify the player's wielded item
29 function player_api.set_wielded_item(player)
30 local name = player:get_player_name()
33 --local model = models[model_name]
34 if not wielded_item[name] or not wielded_item[name]:get_luaentity() then
35 wielded_item[name] = nil
36 --we give the player an item to hold
37 local itemstring = player:get_wielded_item():get_name()
39 local wield_item = minetest.add_entity(player:get_pos(),"player_api:item")
42 wield_item:get_luaentity():set_item(itemstring)
44 wield_item:get_luaentity().wielder = player:get_player_name()
46 wield_item:set_attach(player, "Right_Hand", vector.new(0,0,0), vector.new(0,0,0))
48 wielded_item[name] = wield_item
54 local itemstring = wielded_item[name]:get_luaentity().itemstring
55 local player_wield_item = player:get_wielded_item():get_name()
57 if itemstring ~= player_wield_item then
58 wielded_item[name]:get_luaentity().itemstring = player_wield_item
59 wielded_item[name]:get_luaentity():set_item(player_wield_item)
65 function player_api.get_animation(player)
66 local name = player:get_player_name()
68 model = player_model[name],
69 textures = player_textures[name],
70 animation = player_anim[name],
74 -- Called when a player's appearance needs to be updated
75 function player_api.set_model(player, model_name)
76 local name = player:get_player_name()
77 local model = models[model_name]
78 if player_model[name] == model_name then
81 player:set_properties({
83 textures = player_textures[name] or model.textures,
85 visual_size = model.visual_size or {x = 1, y = 1},
86 collisionbox = model.collisionbox or {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3},
87 stepheight = model.stepheight or 0.6,
88 eye_height = model.eye_height or 1.47,
90 player_api.set_animation(player, "stand")
91 player_model[name] = model_name
94 function player_api.set_textures(player, textures)
95 local name = player:get_player_name()
96 local model = models[player_model[name]]
97 local model_textures = model and model.textures or nil
98 player_textures[name] = textures or model_textures
99 player:set_properties({textures = textures or model_textures,})
102 function player_api.set_animation(player, anim_name, speed)
103 local name = player:get_player_name()
104 if player_anim[name] == anim_name then
107 local model = player_model[name] and models[player_model[name]]
108 if not (model and model.animations[anim_name]) then
111 local anim = model.animations[anim_name]
114 --calculate local animation
115 --update player's frame speed
117 local local_player_animation = player:get_local_animation()
118 local idle_animation = {x = 0, y = 79}
119 local walk_animation = {x = 168, y = 187}
120 local dig_animation = {x = 189, y = 198}
121 local walk_and_dig = {x = 200, y = 219}
122 local sneak_speed = nil
124 if anim_name == "sneak" or anim_name == "sneak_mine_stand" or anim_name == "sneak_walk" or anim_name == "sneak_mine_walk" then
125 idle_animation = model.animations.sneak
126 walk_animation = model.animations.sneak_walk
127 dig_animation = model.animations.sneak_mine_stand
128 walk_and_dig = model.animations.sneak_mine_walk
133 --update the external animation speed that other players see
134 local sneaker_speed = speed
136 sneaker_speed = sneak_speed
139 player:set_local_animation(
140 idle_animation,--idle
141 walk_animation,--walk
143 walk_and_dig,--walk and dig
146 player:set_nametag_attributes(
154 player_api.set_model(player, "character.b3d")
155 player_anim[name] = anim_name
156 player:set_animation(anim, sneaker_speed, animation_blend)
159 minetest.register_on_leaveplayer(function(player)
160 local name = player:get_player_name()
161 player_model[name] = nil
162 player_anim[name] = nil
163 player_textures[name] = nil
166 -- Localize for better performance.
167 local player_set_animation = player_api.set_animation
168 local player_attached = player_api.player_attached
170 -- Prevent knockback for attached players
171 local old_calculate_knockback = minetest.calculate_knockback
172 function minetest.calculate_knockback(player, ...)
173 if player_attached[player:get_player_name()] then
176 return old_calculate_knockback(player, ...)
179 -- Check each player and apply animations
180 minetest.register_globalstep(function()
181 for _, player in pairs(minetest.get_connected_players()) do
182 --update the player wielded item model
183 player_api.set_wielded_item(player)
185 local name = player:get_player_name()
186 local model_name = player_model[name]
187 local model = model_name and models[model_name]
188 if model and not player_attached[name] then
189 local controls = player:get_player_control()
190 local animation_speed_mod = model.animation_speed or 30
192 -- Determine if the player is sneaking, and reduce animation speed if so
193 --if controls.sneak then
194 -- animation_speed_mod = 0
197 local meta = player:get_meta()
198 local movement_state = meta:get_string("player.player_movement_state")
200 -- Apply animations based on what the player is doing
201 if player:get_hp() == 0 then
202 player_set_animation(player, "lay")
203 elseif movement_state == "0" then
205 if controls.up or controls.down or controls.left or controls.right then
206 if controls.LMB or controls.RMB then
207 player_set_animation(player, "walk_mine", animation_speed_mod)
209 player_set_animation(player, "walk", animation_speed_mod)
212 if controls.LMB or controls.RMB then
213 player_set_animation(player, "mine", animation_speed_mod)
215 player_set_animation(player, "stand", animation_speed_mod)
218 elseif movement_state == "1" then
220 if controls.up or controls.down or controls.left or controls.right then
221 if controls.LMB or controls.RMB then
222 player_set_animation(player, "walk_mine", animation_speed_mod*1.5)
224 player_set_animation(player, "walk", animation_speed_mod*1.5)
227 if controls.LMB or controls.RMB then
228 player_set_animation(player, "mine", animation_speed_mod*1.5)
230 player_set_animation(player, "stand", animation_speed_mod*1.5)
233 elseif movement_state == "2" then
235 if controls.up or controls.down or controls.left or controls.right then
236 if controls.LMB or controls.RMB then
237 player_set_animation(player, "walk_mine", animation_speed_mod*1.75)
239 player_set_animation(player, "walk", animation_speed_mod*1.75)
242 if controls.LMB or controls.RMB then
243 player_set_animation(player, "mine", animation_speed_mod*1.75)
245 player_set_animation(player, "stand", animation_speed_mod*1.75)
248 elseif movement_state == "3" then
250 if controls.up or controls.down or controls.left or controls.right then
251 if controls.LMB or controls.RMB then
252 player_set_animation(player, "sneak_mine_walk", 30)
254 player_set_animation(player, "sneak_walk", 30)
257 if controls.LMB or controls.RMB then
258 player_set_animation(player, "sneak_mine_stand", 30)
260 player_set_animation(player, "sneak", 30)
264 elseif controls.LMB or controls.RMB then
265 player_set_animation(player, "mine", animation_speed_mod)
267 player_set_animation(player, "sneak", animation_speed_mod)