3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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"
24 #include "connection.h" // PEER_ID_INEXISTENT
26 #include "content_sao.h"
28 Player::Player(IGameDef *gamedef):
29 touching_ground(false),
31 in_water_stable(false),
34 camera_barely_in_ceiling(false),
35 inventory(gamedef->idef()),
37 peer_id(PEER_ID_INEXISTENT),
45 updateName("<not set>");
47 inventory.addList("main", PLAYER_INVENTORY_SIZE);
48 inventory.addList("craft", 9);
49 inventory.addList("craftpreview", 1);
50 inventory.addList("craftresult", 1);
57 // Y direction is ignored
58 void Player::accelerate(v3f target_speed, f32 max_increase)
60 v3f d_wanted = target_speed - m_speed;
62 f32 dl_wanted = d_wanted.getLength();
67 v3f d = d_wanted.normalize() * dl;
74 if(m_speed.X < target_speed.X - max_increase)
75 m_speed.X += max_increase;
76 else if(m_speed.X > target_speed.X + max_increase)
77 m_speed.X -= max_increase;
78 else if(m_speed.X < target_speed.X)
79 m_speed.X = target_speed.X;
80 else if(m_speed.X > target_speed.X)
81 m_speed.X = target_speed.X;
83 if(m_speed.Z < target_speed.Z - max_increase)
84 m_speed.Z += max_increase;
85 else if(m_speed.Z > target_speed.Z + max_increase)
86 m_speed.Z -= max_increase;
87 else if(m_speed.Z < target_speed.Z)
88 m_speed.Z = target_speed.Z;
89 else if(m_speed.Z > target_speed.Z)
90 m_speed.Z = target_speed.Z;
94 v3s16 Player::getLightPosition() const
96 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
99 void Player::serialize(std::ostream &os)
101 // Utilize a Settings object for storing values
103 args.setS32("version", 1);
104 args.set("name", m_name);
105 //args.set("password", m_password);
106 args.setFloat("pitch", m_pitch);
107 args.setFloat("yaw", m_yaw);
108 args.setV3F("position", m_position);
109 args.setS32("hp", hp);
113 os<<"PlayerArgsEnd\n";
115 inventory.serialize(os);
118 void Player::deSerialize(std::istream &is)
125 throw SerializationError
126 ("Player::deSerialize(): PlayerArgsEnd not found");
128 std::getline(is, line);
129 std::string trimmedline = trim(line);
130 if(trimmedline == "PlayerArgsEnd")
132 args.parseConfigLine(line);
135 //args.getS32("version"); // Version field value not used
136 std::string name = args.get("name");
137 updateName(name.c_str());
138 setPitch(args.getFloat("pitch"));
139 setYaw(args.getFloat("yaw"));
140 setPosition(args.getV3F("position"));
142 hp = args.getS32("hp");
143 }catch(SettingNotFoundException &e){
147 inventory.deSerialize(is);
149 if(inventory.getList("craftpreview") == NULL)
151 // Convert players without craftpreview
152 inventory.addList("craftpreview", 1);
154 bool craftresult_is_preview = true;
155 if(args.exists("craftresult_is_preview"))
156 craftresult_is_preview = args.getBool("craftresult_is_preview");
157 if(craftresult_is_preview)
160 inventory.getList("craftresult")->changeItem(0, ItemStack());
169 void RemotePlayer::setPosition(const v3f &position)
171 Player::setPosition(position);
173 m_sao->setBasePosition(position);