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.
21 #include "constants.h"
23 #include "connection.h" // PEER_ID_INEXISTENT
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),
36 inventory(gamedef->idef()),
38 peer_id(PEER_ID_INEXISTENT),
46 updateName("<not set>");
48 inventory.addList("main", PLAYER_INVENTORY_SIZE);
49 InventoryList *craft = inventory.addList("craft", 9);
51 inventory.addList("craftpreview", 1);
52 inventory.addList("craftresult", 1);
54 // Can be redefined via Lua
55 inventory_formspec = "size[8,7.5]"
56 //"image[1,0.6;1,2;player.png]"
57 "list[current_player;main;0,3.5;8,4;]"
58 "list[current_player;craft;3,0;3,3;]"
59 "list[current_player;craftpreview;7,1;1,1;]";
61 // Initialize movement settings at default values, so movement can work if the server fails to send them
62 movement_acceleration_default = 3 * BS;
63 movement_acceleration_air = 2 * BS;
64 movement_acceleration_fast = 10 * BS;
65 movement_speed_walk = 4 * BS;
66 movement_speed_crouch = 1.35 * BS;
67 movement_speed_fast = 20 * BS;
68 movement_speed_climb = 2 * BS;
69 movement_speed_jump = 6.5 * BS;
70 movement_liquid_fluidity = 1 * BS;
71 movement_liquid_fluidity_smooth = 0.5 * BS;
72 movement_liquid_sink = 10 * BS;
73 movement_gravity = 9.81 * BS;
75 // Movement overrides are multipliers and must be 1 by default
76 physics_override_speed = 1;
77 physics_override_jump = 1;
78 physics_override_gravity = 1;
85 // Horizontal acceleration (X and Z), Y direction is ignored
86 void Player::accelerateHorizontal(v3f target_speed, f32 max_increase)
91 v3f d_wanted = target_speed - m_speed;
93 f32 dl = d_wanted.getLength();
97 v3f d = d_wanted.normalize() * dl;
103 if(m_speed.X < target_speed.X - max_increase)
104 m_speed.X += max_increase;
105 else if(m_speed.X > target_speed.X + max_increase)
106 m_speed.X -= max_increase;
107 else if(m_speed.X < target_speed.X)
108 m_speed.X = target_speed.X;
109 else if(m_speed.X > target_speed.X)
110 m_speed.X = target_speed.X;
112 if(m_speed.Z < target_speed.Z - max_increase)
113 m_speed.Z += max_increase;
114 else if(m_speed.Z > target_speed.Z + max_increase)
115 m_speed.Z -= max_increase;
116 else if(m_speed.Z < target_speed.Z)
117 m_speed.Z = target_speed.Z;
118 else if(m_speed.Z > target_speed.Z)
119 m_speed.Z = target_speed.Z;
123 // Vertical acceleration (Y), X and Z directions are ignored
124 void Player::accelerateVertical(v3f target_speed, f32 max_increase)
126 if(max_increase == 0)
129 f32 d_wanted = target_speed.Y - m_speed.Y;
130 if(d_wanted > max_increase)
131 d_wanted = max_increase;
132 else if(d_wanted < -max_increase)
133 d_wanted = -max_increase;
135 m_speed.Y += d_wanted;
138 if(m_speed.Y < target_speed.Y - max_increase)
139 m_speed.Y += max_increase;
140 else if(m_speed.Y > target_speed.Y + max_increase)
141 m_speed.Y -= max_increase;
142 else if(m_speed.Y < target_speed.Y)
143 m_speed.Y = target_speed.Y;
144 else if(m_speed.Y > target_speed.Y)
145 m_speed.Y = target_speed.Y;
149 v3s16 Player::getLightPosition() const
151 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
154 void Player::serialize(std::ostream &os)
156 // Utilize a Settings object for storing values
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);
168 os<<"PlayerArgsEnd\n";
170 inventory.serialize(os);
173 void Player::deSerialize(std::istream &is)
180 throw SerializationError
181 ("Player::deSerialize(): PlayerArgsEnd not found");
183 std::getline(is, line);
184 std::string trimmedline = trim(line);
185 if(trimmedline == "PlayerArgsEnd")
187 args.parseConfigLine(line);
190 //args.getS32("version"); // Version field value not used
191 std::string name = args.get("name");
192 updateName(name.c_str());
193 setPitch(args.getFloat("pitch"));
194 setYaw(args.getFloat("yaw"));
195 setPosition(args.getV3F("position"));
197 hp = args.getS32("hp");
198 }catch(SettingNotFoundException &e){
202 inventory.deSerialize(is);
204 if(inventory.getList("craftpreview") == NULL)
206 // Convert players without craftpreview
207 inventory.addList("craftpreview", 1);
209 bool craftresult_is_preview = true;
210 if(args.exists("craftresult_is_preview"))
211 craftresult_is_preview = args.getBool("craftresult_is_preview");
212 if(craftresult_is_preview)
215 inventory.getList("craftresult")->changeItem(0, ItemStack());
228 void RemotePlayer::setPosition(const v3f &position)
230 Player::setPosition(position);
232 m_sao->setBasePosition(position);