Disable treespawning on mapgen
[minetest_hades.git] / mods / default / player.lua
blobb0278c10fd95714c66688ff8385ef9abddde8770
1 -- Minetest 0.4 mod: player
2 -- See README.txt for licensing and other information.
4 -- Player animation blending
5 -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
6 local animation_blend = 0
8 default.registered_player_models = { }
10 -- Local for speed.
11 local models = default.registered_player_models
13 function default.player_register_model(name, def)
14 models[name] = def
15 end
17 -- Default player appearance
18 default.player_register_model("character.b3d", {
19 animation_speed = 30,
20 textures = {"character.png", },
21 animations = {
22 -- Standard animations.
23 stand = { x= 0, y= 79, },
24 lay = { x=162, y=166, },
25 walk = { x=168, y=187, },
26 mine = { x=189, y=198, },
27 walk_mine = { x=200, y=219, },
28 -- Extra animations (not currently used by the game).
29 sit = { x= 81, y=160, },
33 -- Player stats and animations
34 local player_model = {}
35 local player_textures = {}
36 local player_anim = {}
37 local player_sneak = {}
38 default.player_attached = {}
40 function default.player_get_animation(player)
41 local name = player:get_player_name()
42 return {
43 model = player_model[name],
44 textures = player_textures[name],
45 animation = player_anim[name],
47 end
49 -- Called when a player's appearance needs to be updated
50 function default.player_set_model(player, model_name)
51 local name = player:get_player_name()
52 local model = models[model_name]
53 if model then
54 if player_model[name] == model_name then
55 return
56 end
57 player:set_properties({
58 mesh = model_name,
59 textures = player_textures[name] or model.textures,
60 visual = "mesh",
61 visual_size = model.visual_size or {x=1, y=1},
63 default.player_set_animation(player, "stand")
64 else
65 player:set_properties({
66 textures = { "player.png", "player_back.png", },
67 visual = "upright_sprite",
69 end
70 player_model[name] = model_name
71 end
73 function default.player_set_textures(player, textures)
74 local name = player:get_player_name()
75 player_textures[name] = textures
76 player:set_properties({textures = textures,})
77 end
79 function default.player_set_animation(player, anim_name, speed)
80 local name = player:get_player_name()
81 if player_anim[name] == anim_name then
82 return
83 end
84 local model = player_model[name] and models[player_model[name]]
85 if not (model and model.animations[anim_name]) then
86 return
87 end
88 local anim = model.animations[anim_name]
89 player_anim[name] = anim_name
90 player:set_animation(anim, speed or model.animation_speed, animation_blend)
91 end
93 -- Update appearance when the player joins
94 minetest.register_on_joinplayer(function(player)
95 default.player_attached[player:get_player_name()] = false
96 default.player_set_model(player, "character.b3d")
97 player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
99 -- set GUI
100 if not minetest.setting_getbool("creative_mode") then
101 player:set_inventory_formspec(default.gui_survival_form)
103 player:hud_set_hotbar_image("gui_hotbar.png")
104 player:hud_set_hotbar_selected_image("gui_hotbar_selected.png")
105 end)
107 minetest.register_on_leaveplayer(function(player)
108 local name = player:get_player_name()
109 player_model[name] = nil
110 player_anim[name] = nil
111 player_textures[name] = nil
112 end)
114 -- Localize for better performance.
115 local player_set_animation = default.player_set_animation
116 local player_attached = default.player_attached
118 -- Check each player and apply animations
119 minetest.register_globalstep(function(dtime)
120 for _, player in pairs(minetest.get_connected_players()) do
121 local name = player:get_player_name()
122 local model_name = player_model[name]
123 local model = model_name and models[model_name]
125 --[[ Skybox (original by paramat's moonrealm)
127 local YMIN = -31000
128 local YMAX = 31000
129 local pos = player:getpos()
130 if pos.y > YMIN and pos.y < YMAX then
131 local skytextures = {
132 "hadessky_posy.png",
133 "hadessky_negy.png",
134 "hadessky_posz.png",
135 "hadessky_negz.png",
136 "hadessky_negx.png",
137 "hadessky_posx.png",
139 player:set_sky({r=0, g=0, b=0, a=0}, "skybox", skytextures)
140 player:override_day_night_ratio(5555)
141 else
142 player:set_sky({}, "regular", {})
144 -- end Skybox
145 --]]
148 if model and not player_attached[name] then
149 local controls = player:get_player_control()
150 local walking = false
151 local animation_speed_mod = model.animation_speed or 30
153 -- Determine if the player is walking
154 if controls.up or controls.down or controls.left or controls.right then
155 walking = true
158 -- Determine if the player is sneaking, and reduce animation speed if so
159 if controls.sneak then
160 animation_speed_mod = animation_speed_mod / 2
163 -- Apply animations based on what the player is doing
164 if player:get_hp() == 0 then
165 player_set_animation(player, "lay")
166 elseif walking then
167 if player_sneak[name] ~= controls.sneak then
168 player_anim[name] = nil
169 player_sneak[name] = controls.sneak
171 if controls.LMB then
172 player_set_animation(player, "walk_mine", animation_speed_mod)
173 else
174 player_set_animation(player, "walk", animation_speed_mod)
176 elseif controls.LMB then
177 player_set_animation(player, "mine")
178 else
179 player_set_animation(player, "stand", animation_speed_mod)
183 end)