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 InventoryList *craft = inventory.addList("craft", 9);
50 inventory.addList("craftpreview", 1);
51 inventory.addList("craftresult", 1);
53 // Can be redefined via Lua
54 inventory_formspec = "size[8,7.5]"
55 //"image[1,0.6;1,2;player.png]"
56 "list[current_player;main;0,3.5;8,4;]"
57 "list[current_player;craft;3,0;3,3;]"
58 "list[current_player;craftpreview;7,1;1,1;]";
65 // Y direction is ignored
66 void Player::accelerate(v3f target_speed, f32 max_increase)
68 v3f d_wanted = target_speed - m_speed;
70 f32 dl_wanted = d_wanted.getLength();
75 v3f d = d_wanted.normalize() * dl;
82 if(m_speed.X < target_speed.X - max_increase)
83 m_speed.X += max_increase;
84 else if(m_speed.X > target_speed.X + max_increase)
85 m_speed.X -= max_increase;
86 else if(m_speed.X < target_speed.X)
87 m_speed.X = target_speed.X;
88 else if(m_speed.X > target_speed.X)
89 m_speed.X = target_speed.X;
91 if(m_speed.Z < target_speed.Z - max_increase)
92 m_speed.Z += max_increase;
93 else if(m_speed.Z > target_speed.Z + max_increase)
94 m_speed.Z -= max_increase;
95 else if(m_speed.Z < target_speed.Z)
96 m_speed.Z = target_speed.Z;
97 else if(m_speed.Z > target_speed.Z)
98 m_speed.Z = target_speed.Z;
102 v3s16 Player::getLightPosition() const
104 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
107 void Player::serialize(std::ostream &os)
109 // Utilize a Settings object for storing values
111 args.setS32("version", 1);
112 args.set("name", m_name);
113 //args.set("password", m_password);
114 args.setFloat("pitch", m_pitch);
115 args.setFloat("yaw", m_yaw);
116 args.setV3F("position", m_position);
117 args.setS32("hp", hp);
121 os<<"PlayerArgsEnd\n";
123 inventory.serialize(os);
126 void Player::deSerialize(std::istream &is)
133 throw SerializationError
134 ("Player::deSerialize(): PlayerArgsEnd not found");
136 std::getline(is, line);
137 std::string trimmedline = trim(line);
138 if(trimmedline == "PlayerArgsEnd")
140 args.parseConfigLine(line);
143 //args.getS32("version"); // Version field value not used
144 std::string name = args.get("name");
145 updateName(name.c_str());
146 setPitch(args.getFloat("pitch"));
147 setYaw(args.getFloat("yaw"));
148 setPosition(args.getV3F("position"));
150 hp = args.getS32("hp");
151 }catch(SettingNotFoundException &e){
155 inventory.deSerialize(is);
157 if(inventory.getList("craftpreview") == NULL)
159 // Convert players without craftpreview
160 inventory.addList("craftpreview", 1);
162 bool craftresult_is_preview = true;
163 if(args.exists("craftresult_is_preview"))
164 craftresult_is_preview = args.getBool("craftresult_is_preview");
165 if(craftresult_is_preview)
168 inventory.getList("craftresult")->changeItem(0, ItemStack());
181 void RemotePlayer::setPosition(const v3f &position)
183 Player::setPosition(position);
185 m_sao->setBasePosition(position);