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