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 "util/numeric.h"
25 #include "constants.h"
28 #include "content_sao.h"
32 Player::Player(IGameDef *gamedef):
33 touching_ground(false),
35 in_liquid_stable(false),
38 swimming_vertical(false),
39 camera_barely_in_ceiling(false),
41 inventory(gamedef->idef()),
44 hurt_tilt_strength(0),
45 peer_id(PEER_ID_INEXISTENT),
54 m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30),
58 m_last_hp(PLAYER_MAX_HP),
59 m_last_inventory(gamedef->idef())
61 updateName("<not set>");
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 m_last_inventory = inventory;
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 "list[current_player;craftpreview;7,1;1,1;]";
77 // Initialize movement settings at default values, so movement can work if the server fails to send them
78 movement_acceleration_default = 3 * BS;
79 movement_acceleration_air = 2 * BS;
80 movement_acceleration_fast = 10 * BS;
81 movement_speed_walk = 4 * BS;
82 movement_speed_crouch = 1.35 * BS;
83 movement_speed_fast = 20 * BS;
84 movement_speed_climb = 2 * BS;
85 movement_speed_jump = 6.5 * BS;
86 movement_liquid_fluidity = 1 * BS;
87 movement_liquid_fluidity_smooth = 0.5 * BS;
88 movement_liquid_sink = 10 * BS;
89 movement_gravity = 9.81 * BS;
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;
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;
102 hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
110 // Horizontal acceleration (X and Z), Y direction is ignored
111 void Player::accelerateHorizontal(v3f target_speed, f32 max_increase)
113 if(max_increase == 0)
116 v3f d_wanted = target_speed - m_speed;
118 f32 dl = d_wanted.getLength();
119 if(dl > max_increase)
122 v3f d = d_wanted.normalize() * dl;
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;
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;
148 // Vertical acceleration (Y), X and Z directions are ignored
149 void Player::accelerateVertical(v3f target_speed, f32 max_increase)
151 if(max_increase == 0)
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;
160 m_speed.Y += d_wanted;
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;
174 v3s16 Player::getLightPosition() const
176 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
179 void Player::serialize(std::ostream &os)
181 // Utilize a Settings object for storing values
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);
194 os<<"PlayerArgsEnd\n";
196 inventory.serialize(os);
199 void Player::deSerialize(std::istream &is, std::string playername)
203 if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
204 throw SerializationError("PlayerArgsEnd of player " +
205 playername + " not found!");
208 //args.getS32("version"); // Version field value not used
209 std::string name = args.get("name");
210 updateName(name.c_str());
211 setPitch(args.getFloat("pitch"));
212 setYaw(args.getFloat("yaw"));
213 setPosition(args.getV3F("position"));
215 hp = args.getS32("hp");
216 }catch(SettingNotFoundException &e) {
220 m_breath = args.getS32("breath");
221 }catch(SettingNotFoundException &e) {
225 inventory.deSerialize(is);
227 if(inventory.getList("craftpreview") == NULL) {
228 // Convert players without craftpreview
229 inventory.addList("craftpreview", 1);
231 bool craftresult_is_preview = true;
232 if(args.exists("craftresult_is_preview"))
233 craftresult_is_preview = args.getBool("craftresult_is_preview");
234 if(craftresult_is_preview)
237 inventory.getList("craftresult")->changeItem(0, ItemStack());
245 u32 Player::addHud(HudElement *toadd)
247 u32 id = getFreeHudID();
252 hud.push_back(toadd);
257 HudElement* Player::getHud(u32 id)
265 HudElement* Player::removeHud(u32 id)
267 HudElement* retval = NULL;
268 if (id < hud.size()) {
275 void Player::clearHud()
277 while(!hud.empty()) {
284 void RemotePlayer::save(std::string savedir)
287 * We have to open all possible player files in the players directory
288 * and check their player names because some file systems are not
289 * case-sensitive and player names are case-sensitive.
292 // A player to deserialize files into to check their names
293 RemotePlayer testplayer(m_gamedef);
295 savedir += DIR_DELIM;
296 std::string path = savedir + m_name;
297 for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
298 if (!fs::PathExists(path)) {
299 // Open file and serialize
300 std::ostringstream ss(std::ios_base::binary);
302 if (!fs::safeWriteToFile(path, ss.str())) {
303 infostream << "Failed to write " << path << std::endl;
307 // Open file and deserialize
308 std::ifstream is(path.c_str(), std::ios_base::binary);
310 infostream << "Failed to open " << path << std::endl;
313 testplayer.deSerialize(is, path);
315 if (strcmp(testplayer.getName(), m_name) == 0) {
316 // Open file and serialize
317 std::ostringstream ss(std::ios_base::binary);
319 if (!fs::safeWriteToFile(path, ss.str())) {
320 infostream << "Failed to write " << path << std::endl;
324 path = savedir + m_name + itos(i);
327 infostream << "Didn't find free file for player " << m_name << std::endl;
334 void RemotePlayer::setPosition(const v3f &position)
336 Player::setPosition(position);
338 m_sao->setBasePosition(position);