Custom boxy nodes (stairs, slabs) and collision changes
[oweals/minetest.git] / src / player.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "player.h"
21 #include "constants.h"
22 #include "gamedef.h"
23 #include "connection.h" // PEER_ID_INEXISTENT
24 #include "settings.h"
25 #include "content_sao.h"
26 #include "util/numeric.h"
27
28 Player::Player(IGameDef *gamedef):
29         touching_ground(false),
30         in_water(false),
31         in_water_stable(false),
32         is_climbing(false),
33         swimming_up(false),
34         camera_barely_in_ceiling(false),
35         inventory(gamedef->idef()),
36         hp(PLAYER_MAX_HP),
37         peer_id(PEER_ID_INEXISTENT),
38 // protected
39         m_gamedef(gamedef),
40         m_pitch(0),
41         m_yaw(0),
42         m_speed(0,0,0),
43         m_position(0,0,0)
44 {
45         updateName("<not set>");
46         inventory.clear();
47         inventory.addList("main", PLAYER_INVENTORY_SIZE);
48         inventory.addList("craft", 9);
49         inventory.addList("craftpreview", 1);
50         inventory.addList("craftresult", 1);
51 }
52
53 Player::~Player()
54 {
55 }
56
57 // Y direction is ignored
58 void Player::accelerate(v3f target_speed, f32 max_increase)
59 {
60         v3f d_wanted = target_speed - m_speed;
61         d_wanted.Y = 0;
62         f32 dl_wanted = d_wanted.getLength();
63         f32 dl = dl_wanted;
64         if(dl > max_increase)
65                 dl = max_increase;
66         
67         v3f d = d_wanted.normalize() * dl;
68
69         m_speed.X += d.X;
70         m_speed.Z += d.Z;
71         //m_speed += d;
72
73 #if 0 // old code
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;
82
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;
91 #endif
92 }
93
94 v3s16 Player::getLightPosition() const
95 {
96         return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
97 }
98
99 void Player::serialize(std::ostream &os)
100 {
101         // Utilize a Settings object for storing values
102         Settings args;
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);
110
111         args.writeLines(os);
112
113         os<<"PlayerArgsEnd\n";
114         
115         inventory.serialize(os);
116 }
117
118 void Player::deSerialize(std::istream &is)
119 {
120         Settings args;
121         
122         for(;;)
123         {
124                 if(is.eof())
125                         throw SerializationError
126                                         ("Player::deSerialize(): PlayerArgsEnd not found");
127                 std::string line;
128                 std::getline(is, line);
129                 std::string trimmedline = trim(line);
130                 if(trimmedline == "PlayerArgsEnd")
131                         break;
132                 args.parseConfigLine(line);
133         }
134
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"));
141         try{
142                 hp = args.getS32("hp");
143         }catch(SettingNotFoundException &e){
144                 hp = 20;
145         }
146
147         inventory.deSerialize(is);
148
149         if(inventory.getList("craftpreview") == NULL)
150         {
151                 // Convert players without craftpreview
152                 inventory.addList("craftpreview", 1);
153
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)
158                 {
159                         // Clear craftresult
160                         inventory.getList("craftresult")->changeItem(0, ItemStack());
161                 }
162         }
163 }
164
165 /*
166         RemotePlayer
167 */
168
169
170
171
172
173 void RemotePlayer::setPosition(const v3f &position)
174 {
175         Player::setPosition(position);
176         if(m_sao)
177                 m_sao->setBasePosition(position);
178 }