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.
22 #include "constants.h"
25 #include "content_sao.h"
26 #include "util/numeric.h"
28 Player::Player(IGameDef *gamedef):
29 touching_ground(false),
31 in_liquid_stable(false),
34 swimming_vertical(false),
35 camera_barely_in_ceiling(false),
37 inventory(gamedef->idef()),
40 hurt_tilt_strength(0),
41 peer_id(PEER_ID_INEXISTENT),
50 m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30),
54 m_last_hp(PLAYER_MAX_HP),
55 m_last_inventory(gamedef->idef())
57 updateName("<not set>");
59 inventory.addList("main", PLAYER_INVENTORY_SIZE);
60 InventoryList *craft = inventory.addList("craft", 9);
62 inventory.addList("craftpreview", 1);
63 inventory.addList("craftresult", 1);
64 m_last_inventory = inventory;
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;]";
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;
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;
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;
98 hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
105 // Horizontal acceleration (X and Z), Y direction is ignored
106 void Player::accelerateHorizontal(v3f target_speed, f32 max_increase)
108 if(max_increase == 0)
111 v3f d_wanted = target_speed - m_speed;
113 f32 dl = d_wanted.getLength();
114 if(dl > max_increase)
117 v3f d = d_wanted.normalize() * dl;
123 if(m_speed.X < target_speed.X - max_increase)
124 m_speed.X += max_increase;
125 else if(m_speed.X > target_speed.X + max_increase)
126 m_speed.X -= max_increase;
127 else if(m_speed.X < target_speed.X)
128 m_speed.X = target_speed.X;
129 else if(m_speed.X > target_speed.X)
130 m_speed.X = target_speed.X;
132 if(m_speed.Z < target_speed.Z - max_increase)
133 m_speed.Z += max_increase;
134 else if(m_speed.Z > target_speed.Z + max_increase)
135 m_speed.Z -= max_increase;
136 else if(m_speed.Z < target_speed.Z)
137 m_speed.Z = target_speed.Z;
138 else if(m_speed.Z > target_speed.Z)
139 m_speed.Z = target_speed.Z;
143 // Vertical acceleration (Y), X and Z directions are ignored
144 void Player::accelerateVertical(v3f target_speed, f32 max_increase)
146 if(max_increase == 0)
149 f32 d_wanted = target_speed.Y - m_speed.Y;
150 if(d_wanted > max_increase)
151 d_wanted = max_increase;
152 else if(d_wanted < -max_increase)
153 d_wanted = -max_increase;
155 m_speed.Y += d_wanted;
158 if(m_speed.Y < target_speed.Y - max_increase)
159 m_speed.Y += max_increase;
160 else if(m_speed.Y > target_speed.Y + max_increase)
161 m_speed.Y -= max_increase;
162 else if(m_speed.Y < target_speed.Y)
163 m_speed.Y = target_speed.Y;
164 else if(m_speed.Y > target_speed.Y)
165 m_speed.Y = target_speed.Y;
169 v3s16 Player::getLightPosition() const
171 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
174 void Player::serialize(std::ostream &os)
176 // Utilize a Settings object for storing values
178 args.setS32("version", 1);
179 args.set("name", m_name);
180 //args.set("password", m_password);
181 args.setFloat("pitch", m_pitch);
182 args.setFloat("yaw", m_yaw);
183 args.setV3F("position", m_position);
184 args.setS32("hp", hp);
185 args.setS32("breath", m_breath);
189 os<<"PlayerArgsEnd\n";
191 inventory.serialize(os);
194 void Player::deSerialize(std::istream &is, std::string playername)
201 throw SerializationError
202 (("Player::deSerialize(): PlayerArgsEnd of player \"" + playername + "\" not found").c_str());
204 std::getline(is, line);
205 std::string trimmedline = trim(line);
206 if(trimmedline == "PlayerArgsEnd")
208 args.parseConfigLine(line);
211 //args.getS32("version"); // Version field value not used
212 std::string name = args.get("name");
213 updateName(name.c_str());
214 setPitch(args.getFloat("pitch"));
215 setYaw(args.getFloat("yaw"));
216 setPosition(args.getV3F("position"));
218 hp = args.getS32("hp");
219 }catch(SettingNotFoundException &e){
223 m_breath = args.getS32("breath");
224 }catch(SettingNotFoundException &e){
228 inventory.deSerialize(is);
230 if(inventory.getList("craftpreview") == NULL)
232 // Convert players without craftpreview
233 inventory.addList("craftpreview", 1);
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)
241 inventory.getList("craftresult")->changeItem(0, ItemStack());
257 void RemotePlayer::setPosition(const v3f &position)
259 Player::setPosition(position);
261 m_sao->setBasePosition(position);