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