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 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_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);
52 // Can be redefined via Lua
53 inventory_formspec = "invsize[8,7.5;]"
54 //"image[1,0.6;1,2;player.png]"
55 "list[current_player;main;0,3.5;8,4;]"
56 "list[current_player;craft;3,0;3,3;]"
57 "list[current_player;craftpreview;7,1;1,1;]";
64 // Y direction is ignored
65 void Player::accelerate(v3f target_speed, f32 max_increase)
67 v3f d_wanted = target_speed - m_speed;
69 f32 dl_wanted = d_wanted.getLength();
74 v3f d = d_wanted.normalize() * dl;
81 if(m_speed.X < target_speed.X - max_increase)
82 m_speed.X += max_increase;
83 else if(m_speed.X > target_speed.X + max_increase)
84 m_speed.X -= max_increase;
85 else if(m_speed.X < target_speed.X)
86 m_speed.X = target_speed.X;
87 else if(m_speed.X > target_speed.X)
88 m_speed.X = target_speed.X;
90 if(m_speed.Z < target_speed.Z - max_increase)
91 m_speed.Z += max_increase;
92 else if(m_speed.Z > target_speed.Z + max_increase)
93 m_speed.Z -= max_increase;
94 else if(m_speed.Z < target_speed.Z)
95 m_speed.Z = target_speed.Z;
96 else if(m_speed.Z > target_speed.Z)
97 m_speed.Z = target_speed.Z;
101 v3s16 Player::getLightPosition() const
103 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
106 void Player::serialize(std::ostream &os)
108 // Utilize a Settings object for storing values
110 args.setS32("version", 1);
111 args.set("name", m_name);
112 //args.set("password", m_password);
113 args.setFloat("pitch", m_pitch);
114 args.setFloat("yaw", m_yaw);
115 args.setV3F("position", m_position);
116 args.setS32("hp", hp);
120 os<<"PlayerArgsEnd\n";
122 inventory.serialize(os);
125 void Player::deSerialize(std::istream &is)
132 throw SerializationError
133 ("Player::deSerialize(): PlayerArgsEnd not found");
135 std::getline(is, line);
136 std::string trimmedline = trim(line);
137 if(trimmedline == "PlayerArgsEnd")
139 args.parseConfigLine(line);
142 //args.getS32("version"); // Version field value not used
143 std::string name = args.get("name");
144 updateName(name.c_str());
145 setPitch(args.getFloat("pitch"));
146 setYaw(args.getFloat("yaw"));
147 setPosition(args.getV3F("position"));
149 hp = args.getS32("hp");
150 }catch(SettingNotFoundException &e){
154 inventory.deSerialize(is);
156 if(inventory.getList("craftpreview") == NULL)
158 // Convert players without craftpreview
159 inventory.addList("craftpreview", 1);
161 bool craftresult_is_preview = true;
162 if(args.exists("craftresult_is_preview"))
163 craftresult_is_preview = args.getBool("craftresult_is_preview");
164 if(craftresult_is_preview)
167 inventory.getList("craftresult")->changeItem(0, ItemStack());
180 void RemotePlayer::setPosition(const v3f &position)
182 Player::setPosition(position);
184 m_sao->setBasePosition(position);