]> git.lizzy.rs Git - dragonfireclient.git/blob - src/player.cpp
Fix memory leaks in GenericCAO, ShaderSource and Player classes
[dragonfireclient.git] / src / player.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "player.h"
21 #include "hud.h"
22 #include "constants.h"
23 #include "gamedef.h"
24 #include "settings.h"
25 #include "content_sao.h"
26 #include "util/numeric.h"
27
28 Player::Player(IGameDef *gamedef):
29         touching_ground(false),
30         in_liquid(false),
31         in_liquid_stable(false),
32         liquid_viscosity(0),
33         is_climbing(false),
34         swimming_vertical(false),
35         camera_barely_in_ceiling(false),
36         light(0),
37         inventory(gamedef->idef()),
38         hp(PLAYER_MAX_HP),
39         hurt_tilt_timer(0),
40         hurt_tilt_strength(0),
41         peer_id(PEER_ID_INEXISTENT),
42         keyPressed(0),
43 // protected
44         m_gamedef(gamedef),
45         m_breath(-1),
46         m_pitch(0),
47         m_yaw(0),
48         m_speed(0,0,0),
49         m_position(0,0,0),
50         m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30),
51         m_last_pitch(0),
52         m_last_yaw(0),
53         m_last_pos(0,0,0),
54         m_last_hp(PLAYER_MAX_HP),
55         m_last_inventory(gamedef->idef())
56 {
57         updateName("<not set>");
58         inventory.clear();
59         inventory.addList("main", PLAYER_INVENTORY_SIZE);
60         InventoryList *craft = inventory.addList("craft", 9);
61         craft->setWidth(3);
62         inventory.addList("craftpreview", 1);
63         inventory.addList("craftresult", 1);
64         m_last_inventory = inventory;
65
66         // Can be redefined via Lua
67         inventory_formspec = "size[8,7.5]"
68                 //"image[1,0.6;1,2;player.png]"
69                 "list[current_player;main;0,3.5;8,4;]"
70                 "list[current_player;craft;3,0;3,3;]"
71                 "list[current_player;craftpreview;7,1;1,1;]";
72
73         // Initialize movement settings at default values, so movement can work if the server fails to send them
74         movement_acceleration_default   = 3    * BS;
75         movement_acceleration_air       = 2    * BS;
76         movement_acceleration_fast      = 10   * BS;
77         movement_speed_walk             = 4    * BS;
78         movement_speed_crouch           = 1.35 * BS;
79         movement_speed_fast             = 20   * BS;
80         movement_speed_climb            = 2    * BS;
81         movement_speed_jump             = 6.5  * BS;
82         movement_liquid_fluidity        = 1    * BS;
83         movement_liquid_fluidity_smooth = 0.5  * BS;
84         movement_liquid_sink            = 10   * BS;
85         movement_gravity                = 9.81 * BS;
86
87         // Movement overrides are multipliers and must be 1 by default
88         physics_override_speed        = 1;
89         physics_override_jump         = 1;
90         physics_override_gravity      = 1;
91         physics_override_sneak        = true;
92         physics_override_sneak_glitch = true;
93
94         hud_flags = HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
95                          HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE |
96                          HUD_FLAG_BREATHBAR_VISIBLE;
97
98         hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
99 }
100
101 Player::~Player()
102 {
103         clearHud();
104 }
105
106 // Horizontal acceleration (X and Z), Y direction is ignored
107 void Player::accelerateHorizontal(v3f target_speed, f32 max_increase)
108 {
109         if(max_increase == 0)
110                 return;
111
112         v3f d_wanted = target_speed - m_speed;
113         d_wanted.Y = 0;
114         f32 dl = d_wanted.getLength();
115         if(dl > max_increase)
116                 dl = max_increase;
117         
118         v3f d = d_wanted.normalize() * dl;
119
120         m_speed.X += d.X;
121         m_speed.Z += d.Z;
122
123 #if 0 // old code
124         if(m_speed.X < target_speed.X - max_increase)
125                 m_speed.X += max_increase;
126         else if(m_speed.X > target_speed.X + max_increase)
127                 m_speed.X -= max_increase;
128         else if(m_speed.X < target_speed.X)
129                 m_speed.X = target_speed.X;
130         else if(m_speed.X > target_speed.X)
131                 m_speed.X = target_speed.X;
132
133         if(m_speed.Z < target_speed.Z - max_increase)
134                 m_speed.Z += max_increase;
135         else if(m_speed.Z > target_speed.Z + max_increase)
136                 m_speed.Z -= max_increase;
137         else if(m_speed.Z < target_speed.Z)
138                 m_speed.Z = target_speed.Z;
139         else if(m_speed.Z > target_speed.Z)
140                 m_speed.Z = target_speed.Z;
141 #endif
142 }
143
144 // Vertical acceleration (Y), X and Z directions are ignored
145 void Player::accelerateVertical(v3f target_speed, f32 max_increase)
146 {
147         if(max_increase == 0)
148                 return;
149
150         f32 d_wanted = target_speed.Y - m_speed.Y;
151         if(d_wanted > max_increase)
152                 d_wanted = max_increase;
153         else if(d_wanted < -max_increase)
154                 d_wanted = -max_increase;
155
156         m_speed.Y += d_wanted;
157
158 #if 0 // old code
159         if(m_speed.Y < target_speed.Y - max_increase)
160                 m_speed.Y += max_increase;
161         else if(m_speed.Y > target_speed.Y + max_increase)
162                 m_speed.Y -= max_increase;
163         else if(m_speed.Y < target_speed.Y)
164                 m_speed.Y = target_speed.Y;
165         else if(m_speed.Y > target_speed.Y)
166                 m_speed.Y = target_speed.Y;
167 #endif
168 }
169
170 v3s16 Player::getLightPosition() const
171 {
172         return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
173 }
174
175 void Player::serialize(std::ostream &os)
176 {
177         // Utilize a Settings object for storing values
178         Settings args;
179         args.setS32("version", 1);
180         args.set("name", m_name);
181         //args.set("password", m_password);
182         args.setFloat("pitch", m_pitch);
183         args.setFloat("yaw", m_yaw);
184         args.setV3F("position", m_position);
185         args.setS32("hp", hp);
186         args.setS32("breath", m_breath);
187
188         args.writeLines(os);
189
190         os<<"PlayerArgsEnd\n";
191
192         inventory.serialize(os);
193 }
194
195 void Player::deSerialize(std::istream &is, std::string playername)
196 {
197         Settings args;
198         
199         for(;;)
200         {
201                 if(is.eof())
202                         throw SerializationError
203                                         (("Player::deSerialize(): PlayerArgsEnd of player \"" + playername + "\" not found").c_str());
204                 std::string line;
205                 std::getline(is, line);
206                 std::string trimmedline = trim(line);
207                 if(trimmedline == "PlayerArgsEnd")
208                         break;
209                 args.parseConfigLine(line);
210         }
211
212         //args.getS32("version"); // Version field value not used
213         std::string name = args.get("name");
214         updateName(name.c_str());
215         setPitch(args.getFloat("pitch"));
216         setYaw(args.getFloat("yaw"));
217         setPosition(args.getV3F("position"));
218         try{
219                 hp = args.getS32("hp");
220         }catch(SettingNotFoundException &e) {
221                 hp = 20;
222         }
223         try{
224                 m_breath = args.getS32("breath");
225         }catch(SettingNotFoundException &e) {
226                 m_breath = 11;
227         }
228
229         inventory.deSerialize(is);
230
231         if(inventory.getList("craftpreview") == NULL) {
232                 // Convert players without craftpreview
233                 inventory.addList("craftpreview", 1);
234
235                 bool craftresult_is_preview = true;
236                 if(args.exists("craftresult_is_preview"))
237                         craftresult_is_preview = args.getBool("craftresult_is_preview");
238                 if(craftresult_is_preview)
239                 {
240                         // Clear craftresult
241                         inventory.getList("craftresult")->changeItem(0, ItemStack());
242                 }
243         }
244
245         // Set m_last_*
246         checkModified();
247 }
248
249 u32 Player::addHud(HudElement *toadd)
250 {
251         u32 id = getFreeHudID();
252
253         if (id < hud.size())
254                 hud[id] = toadd;
255         else
256                 hud.push_back(toadd);
257
258         return id;
259 }
260
261 HudElement* Player::getHud(u32 id)
262 {
263         if (id < hud.size())
264                 return hud[id];
265
266         return NULL;
267 }
268
269 HudElement* Player::removeHud(u32 id)
270 {
271         HudElement* retval = NULL;
272         if (id < hud.size()) {
273                 retval = hud[id];
274                 hud[id] = NULL;
275         }
276         return retval;
277 }
278
279 void Player::clearHud()
280 {
281         while(!hud.empty()) {
282                 delete hud.back();
283                 hud.pop_back();
284         }
285 }
286
287
288 void RemotePlayer::save(std::string savedir)
289 {
290         /*
291          * We have to open all possible player files in the players directory
292          * and check their player names because some file systems are not
293          * case-sensitive and player names are case-sensitive.
294          */
295
296         // A player to deserialize files into to check their names
297         RemotePlayer testplayer(m_gamedef);
298
299         savedir += DIR_DELIM;
300         std::string path = savedir + m_name;
301         for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
302                 if (!fs::PathExists(path)) {
303                         // Open file and serialize
304                         std::ostringstream ss(std::ios_base::binary);
305                         serialize(ss);
306                         if (!fs::safeWriteToFile(path, ss.str())) {
307                                 infostream << "Failed to write " << path << std::endl;
308                         }
309                         return;
310                 }
311                 // Open file and deserialize
312                 std::ifstream is(path.c_str(), std::ios_base::binary);
313                 if (!is.good()) {
314                         infostream << "Failed to open " << path << std::endl;
315                         return;
316                 }
317                 testplayer.deSerialize(is, path);
318                 is.close();
319                 if (strcmp(testplayer.getName(), m_name) == 0) {
320                         // Open file and serialize
321                         std::ostringstream ss(std::ios_base::binary);
322                         serialize(ss);
323                         if (!fs::safeWriteToFile(path, ss.str())) {
324                                 infostream << "Failed to write " << path << std::endl;
325                         }
326                         return;
327                 }
328                 path = savedir + m_name + itos(i);
329         }
330
331         infostream << "Didn't find free file for player " << m_name << std::endl;
332         return;
333 }
334
335 /*
336         RemotePlayer
337 */
338 void RemotePlayer::setPosition(const v3f &position)
339 {
340         Player::setPosition(position);
341         if(m_sao)
342                 m_sao->setBasePosition(position);
343 }
344