3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 #include "jthread/jmutexautolock.h"
24 #include "util/numeric.h"
26 #include "constants.h"
29 #include "content_sao.h"
32 #include "porting.h" // strlcpy
35 Player::Player(IGameDef *gamedef, const char *name):
36 touching_ground(false),
38 in_liquid_stable(false),
41 swimming_vertical(false),
42 camera_barely_in_ceiling(false),
43 inventory(gamedef->idef()),
46 hurt_tilt_strength(0),
48 peer_id(PEER_ID_INEXISTENT),
52 m_breath(PLAYER_MAX_BREATH),
57 m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30),
60 strlcpy(m_name, name, PLAYERNAME_SIZE);
63 inventory.addList("main", PLAYER_INVENTORY_SIZE);
64 InventoryList *craft = inventory.addList("craft", 9);
66 inventory.addList("craftpreview", 1);
67 inventory.addList("craftresult", 1);
68 inventory.setModified(false);
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;]"
76 "list[current_player;craftpreview;7,1;1,1;]";
78 // Initialize movement settings at default values, so movement can work if the server fails to send them
79 movement_acceleration_default = 3 * BS;
80 movement_acceleration_air = 2 * BS;
81 movement_acceleration_fast = 10 * BS;
82 movement_speed_walk = 4 * BS;
83 movement_speed_crouch = 1.35 * BS;
84 movement_speed_fast = 20 * BS;
85 movement_speed_climb = 2 * BS;
86 movement_speed_jump = 6.5 * BS;
87 movement_liquid_fluidity = 1 * BS;
88 movement_liquid_fluidity_smooth = 0.5 * BS;
89 movement_liquid_sink = 10 * BS;
90 movement_gravity = 9.81 * BS;
91 local_animation_speed = 0.0;
93 // Movement overrides are multipliers and must be 1 by default
94 physics_override_speed = 1;
95 physics_override_jump = 1;
96 physics_override_gravity = 1;
97 physics_override_sneak = true;
98 physics_override_sneak_glitch = true;
100 hud_flags = HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
101 HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE |
102 HUD_FLAG_BREATHBAR_VISIBLE;
104 hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
112 // Horizontal acceleration (X and Z), Y direction is ignored
113 void Player::accelerateHorizontal(v3f target_speed, f32 max_increase)
115 if(max_increase == 0)
118 v3f d_wanted = target_speed - m_speed;
120 f32 dl = d_wanted.getLength();
121 if(dl > max_increase)
124 v3f d = d_wanted.normalize() * dl;
131 // Vertical acceleration (Y), X and Z directions are ignored
132 void Player::accelerateVertical(v3f target_speed, f32 max_increase)
134 if(max_increase == 0)
137 f32 d_wanted = target_speed.Y - m_speed.Y;
138 if(d_wanted > max_increase)
139 d_wanted = max_increase;
140 else if(d_wanted < -max_increase)
141 d_wanted = -max_increase;
143 m_speed.Y += d_wanted;
147 v3s16 Player::getLightPosition() const
149 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
152 void Player::serialize(std::ostream &os)
154 // Utilize a Settings object for storing values
156 args.setS32("version", 1);
157 args.set("name", m_name);
158 //args.set("password", m_password);
159 args.setFloat("pitch", m_pitch);
160 args.setFloat("yaw", m_yaw);
161 args.setV3F("position", m_position);
162 args.setS32("hp", hp);
163 args.setS32("breath", m_breath);
167 os<<"PlayerArgsEnd\n";
169 inventory.serialize(os);
172 void Player::deSerialize(std::istream &is, std::string playername)
176 if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
177 throw SerializationError("PlayerArgsEnd of player " +
178 playername + " not found!");
182 //args.getS32("version"); // Version field value not used
183 std::string name = args.get("name");
184 strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE);
185 setPitch(args.getFloat("pitch"));
186 setYaw(args.getFloat("yaw"));
187 setPosition(args.getV3F("position"));
189 hp = args.getS32("hp");
190 }catch(SettingNotFoundException &e) {
194 m_breath = args.getS32("breath");
195 }catch(SettingNotFoundException &e) {
196 m_breath = PLAYER_MAX_BREATH;
199 inventory.deSerialize(is);
201 if(inventory.getList("craftpreview") == NULL) {
202 // Convert players without craftpreview
203 inventory.addList("craftpreview", 1);
205 bool craftresult_is_preview = true;
206 if(args.exists("craftresult_is_preview"))
207 craftresult_is_preview = args.getBool("craftresult_is_preview");
208 if(craftresult_is_preview)
211 inventory.getList("craftresult")->changeItem(0, ItemStack());
216 u32 Player::addHud(HudElement *toadd)
218 JMutexAutoLock lock(m_mutex);
220 u32 id = getFreeHudID();
225 hud.push_back(toadd);
230 HudElement* Player::getHud(u32 id)
232 JMutexAutoLock lock(m_mutex);
240 HudElement* Player::removeHud(u32 id)
242 JMutexAutoLock lock(m_mutex);
244 HudElement* retval = NULL;
245 if (id < hud.size()) {
252 void Player::clearHud()
254 JMutexAutoLock lock(m_mutex);
256 while(!hud.empty()) {
263 void RemotePlayer::save(std::string savedir)
266 * We have to open all possible player files in the players directory
267 * and check their player names because some file systems are not
268 * case-sensitive and player names are case-sensitive.
271 // A player to deserialize files into to check their names
272 RemotePlayer testplayer(m_gamedef, "");
274 savedir += DIR_DELIM;
275 std::string path = savedir + m_name;
276 for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
277 if (!fs::PathExists(path)) {
278 // Open file and serialize
279 std::ostringstream ss(std::ios_base::binary);
281 if (!fs::safeWriteToFile(path, ss.str())) {
282 infostream << "Failed to write " << path << std::endl;
287 // Open file and deserialize
288 std::ifstream is(path.c_str(), std::ios_base::binary);
290 infostream << "Failed to open " << path << std::endl;
293 testplayer.deSerialize(is, path);
295 if (strcmp(testplayer.getName(), m_name) == 0) {
296 // Open file and serialize
297 std::ostringstream ss(std::ios_base::binary);
299 if (!fs::safeWriteToFile(path, ss.str())) {
300 infostream << "Failed to write " << path << std::endl;
305 path = savedir + m_name + itos(i);
308 infostream << "Didn't find free file for player " << m_name << std::endl;
315 void RemotePlayer::setPosition(const v3f &position)
317 Player::setPosition(position);
319 m_sao->setBasePosition(position);