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"
24 #include "connection.h" // PEER_ID_INEXISTENT
26 #include "content_sao.h"
27 #include "util/numeric.h"
29 Player::Player(IGameDef *gamedef):
30 touching_ground(false),
32 in_liquid_stable(false),
35 swimming_vertical(false),
36 camera_barely_in_ceiling(false),
37 inventory(gamedef->idef()),
39 peer_id(PEER_ID_INEXISTENT),
47 m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.55,BS*0.30),
51 m_last_hp(PLAYER_MAX_HP),
52 m_last_inventory(gamedef->idef())
54 updateName("<not set>");
56 inventory.addList("main", PLAYER_INVENTORY_SIZE);
57 InventoryList *craft = inventory.addList("craft", 9);
59 inventory.addList("craftpreview", 1);
60 inventory.addList("craftresult", 1);
61 m_last_inventory = inventory;
63 // Can be redefined via Lua
64 inventory_formspec = "size[8,7.5]"
65 //"image[1,0.6;1,2;player.png]"
66 "list[current_player;main;0,3.5;8,4;]"
67 "list[current_player;craft;3,0;3,3;]"
68 "list[current_player;craftpreview;7,1;1,1;]";
70 // Initialize movement settings at default values, so movement can work if the server fails to send them
71 movement_acceleration_default = 3 * BS;
72 movement_acceleration_air = 2 * BS;
73 movement_acceleration_fast = 10 * BS;
74 movement_speed_walk = 4 * BS;
75 movement_speed_crouch = 1.35 * BS;
76 movement_speed_fast = 20 * BS;
77 movement_speed_climb = 2 * BS;
78 movement_speed_jump = 6.5 * BS;
79 movement_liquid_fluidity = 1 * BS;
80 movement_liquid_fluidity_smooth = 0.5 * BS;
81 movement_liquid_sink = 10 * BS;
82 movement_gravity = 9.81 * BS;
84 // Movement overrides are multipliers and must be 1 by default
85 physics_override_speed = 1;
86 physics_override_jump = 1;
87 physics_override_gravity = 1;
89 hud_flags = HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
90 HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE |
91 HUD_FLAG_BREATHBAR_VISIBLE;
93 hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
100 // Horizontal acceleration (X and Z), Y direction is ignored
101 void Player::accelerateHorizontal(v3f target_speed, f32 max_increase)
103 if(max_increase == 0)
106 v3f d_wanted = target_speed - m_speed;
108 f32 dl = d_wanted.getLength();
109 if(dl > max_increase)
112 v3f d = d_wanted.normalize() * dl;
118 if(m_speed.X < target_speed.X - max_increase)
119 m_speed.X += max_increase;
120 else if(m_speed.X > target_speed.X + max_increase)
121 m_speed.X -= max_increase;
122 else if(m_speed.X < target_speed.X)
123 m_speed.X = target_speed.X;
124 else if(m_speed.X > target_speed.X)
125 m_speed.X = target_speed.X;
127 if(m_speed.Z < target_speed.Z - max_increase)
128 m_speed.Z += max_increase;
129 else if(m_speed.Z > target_speed.Z + max_increase)
130 m_speed.Z -= max_increase;
131 else if(m_speed.Z < target_speed.Z)
132 m_speed.Z = target_speed.Z;
133 else if(m_speed.Z > target_speed.Z)
134 m_speed.Z = target_speed.Z;
138 // Vertical acceleration (Y), X and Z directions are ignored
139 void Player::accelerateVertical(v3f target_speed, f32 max_increase)
141 if(max_increase == 0)
144 f32 d_wanted = target_speed.Y - m_speed.Y;
145 if(d_wanted > max_increase)
146 d_wanted = max_increase;
147 else if(d_wanted < -max_increase)
148 d_wanted = -max_increase;
150 m_speed.Y += d_wanted;
153 if(m_speed.Y < target_speed.Y - max_increase)
154 m_speed.Y += max_increase;
155 else if(m_speed.Y > target_speed.Y + max_increase)
156 m_speed.Y -= max_increase;
157 else if(m_speed.Y < target_speed.Y)
158 m_speed.Y = target_speed.Y;
159 else if(m_speed.Y > target_speed.Y)
160 m_speed.Y = target_speed.Y;
164 v3s16 Player::getLightPosition() const
166 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
169 void Player::serialize(std::ostream &os)
171 // Utilize a Settings object for storing values
173 args.setS32("version", 1);
174 args.set("name", m_name);
175 //args.set("password", m_password);
176 args.setFloat("pitch", m_pitch);
177 args.setFloat("yaw", m_yaw);
178 args.setV3F("position", m_position);
179 args.setS32("hp", hp);
180 args.setS32("breath", m_breath);
184 os<<"PlayerArgsEnd\n";
186 inventory.serialize(os);
189 void Player::deSerialize(std::istream &is, std::string playername)
196 throw SerializationError
197 (("Player::deSerialize(): PlayerArgsEnd of player \"" + playername + "\" not found").c_str());
199 std::getline(is, line);
200 std::string trimmedline = trim(line);
201 if(trimmedline == "PlayerArgsEnd")
203 args.parseConfigLine(line);
206 //args.getS32("version"); // Version field value not used
207 std::string name = args.get("name");
208 updateName(name.c_str());
209 setPitch(args.getFloat("pitch"));
210 setYaw(args.getFloat("yaw"));
211 setPosition(args.getV3F("position"));
213 hp = args.getS32("hp");
214 }catch(SettingNotFoundException &e){
218 m_breath = args.getS32("breath");
219 }catch(SettingNotFoundException &e){
223 inventory.deSerialize(is);
225 if(inventory.getList("craftpreview") == NULL)
227 // Convert players without craftpreview
228 inventory.addList("craftpreview", 1);
230 bool craftresult_is_preview = true;
231 if(args.exists("craftresult_is_preview"))
232 craftresult_is_preview = args.getBool("craftresult_is_preview");
233 if(craftresult_is_preview)
236 inventory.getList("craftresult")->changeItem(0, ItemStack());
252 void RemotePlayer::setPosition(const v3f &position)
254 Player::setPosition(position);
256 m_sao->setBasePosition(position);