e86b642062277e6ec10ca64702977d5476755df1
[oweals/minetest.git] / src / player.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 "hud.h"
22 #include "constants.h"
23 #include "gamedef.h"
24 #include "connection.h" // PEER_ID_INEXISTENT
25 #include "settings.h"
26 #include "content_sao.h"
27 #include "util/numeric.h"
28
29 Player::Player(IGameDef *gamedef):
30         touching_ground(false),
31         in_liquid(false),
32         in_liquid_stable(false),
33         liquid_viscosity(0),
34         is_climbing(false),
35         swimming_vertical(false),
36         camera_barely_in_ceiling(false),
37         inventory(gamedef->idef()),
38         hp(PLAYER_MAX_HP),
39         peer_id(PEER_ID_INEXISTENT),
40 // protected
41         m_gamedef(gamedef),
42         m_pitch(0),
43         m_yaw(0),
44         m_speed(0,0,0),
45         m_position(0,0,0)
46 {
47         updateName("<not set>");
48         inventory.clear();
49         inventory.addList("main", PLAYER_INVENTORY_SIZE);
50         InventoryList *craft = inventory.addList("craft", 9);
51         craft->setWidth(3);
52         inventory.addList("craftpreview", 1);
53         inventory.addList("craftresult", 1);
54
55         // Can be redefined via Lua
56         inventory_formspec =  "size[8,7.5]"
57                 //"image[1,0.6;1,2;player.png]"
58                 "list[current_player;main;0,3.5;8,4;]"
59                 "list[current_player;craft;3,0;3,3;]"
60                 "list[current_player;craftpreview;7,1;1,1;]";
61
62         // Initialize movement settings at default values, so movement can work if the server fails to send them
63         movement_acceleration_default = 3 * BS;
64         movement_acceleration_air = 2 * BS;
65         movement_acceleration_fast = 10 * BS;
66         movement_speed_walk = 4 * BS;
67         movement_speed_crouch = 1.35 * BS;
68         movement_speed_fast = 20 * BS;
69         movement_speed_climb = 2 * BS;
70         movement_speed_jump = 6.5 * BS;
71         movement_liquid_fluidity = 1 * BS;
72         movement_liquid_fluidity_smooth = 0.5 * BS;
73         movement_liquid_sink = 10 * BS;
74         movement_gravity = 9.81 * BS;
75
76         // Movement overrides are multipliers and must be 1 by default
77         physics_override_speed = 1;
78         physics_override_jump = 1;
79         physics_override_gravity = 1;
80
81         hud_flags = HUD_DRAW_HOTBAR
82                         | HUD_DRAW_HEALTHBAR
83                         | HUD_DRAW_CROSSHAIR
84                         | HUD_DRAW_WIELDITEM;
85 }
86
87 Player::~Player()
88 {
89 }
90
91 // Horizontal acceleration (X and Z), Y direction is ignored
92 void Player::accelerateHorizontal(v3f target_speed, f32 max_increase)
93 {
94         if(max_increase == 0)
95                 return;
96
97         v3f d_wanted = target_speed - m_speed;
98         d_wanted.Y = 0;
99         f32 dl = d_wanted.getLength();
100         if(dl > max_increase)
101                 dl = max_increase;
102         
103         v3f d = d_wanted.normalize() * dl;
104
105         m_speed.X += d.X;
106         m_speed.Z += d.Z;
107
108 #if 0 // old code
109         if(m_speed.X < target_speed.X - max_increase)
110                 m_speed.X += max_increase;
111         else if(m_speed.X > target_speed.X + max_increase)
112                 m_speed.X -= max_increase;
113         else if(m_speed.X < target_speed.X)
114                 m_speed.X = target_speed.X;
115         else if(m_speed.X > target_speed.X)
116                 m_speed.X = target_speed.X;
117
118         if(m_speed.Z < target_speed.Z - max_increase)
119                 m_speed.Z += max_increase;
120         else if(m_speed.Z > target_speed.Z + max_increase)
121                 m_speed.Z -= max_increase;
122         else if(m_speed.Z < target_speed.Z)
123                 m_speed.Z = target_speed.Z;
124         else if(m_speed.Z > target_speed.Z)
125                 m_speed.Z = target_speed.Z;
126 #endif
127 }
128
129 // Vertical acceleration (Y), X and Z directions are ignored
130 void Player::accelerateVertical(v3f target_speed, f32 max_increase)
131 {
132         if(max_increase == 0)
133                 return;
134
135         f32 d_wanted = target_speed.Y - m_speed.Y;
136         if(d_wanted > max_increase)
137                 d_wanted = max_increase;
138         else if(d_wanted < -max_increase)
139                 d_wanted = -max_increase;
140
141         m_speed.Y += d_wanted;
142
143 #if 0 // old code
144         if(m_speed.Y < target_speed.Y - max_increase)
145                 m_speed.Y += max_increase;
146         else if(m_speed.Y > target_speed.Y + max_increase)
147                 m_speed.Y -= max_increase;
148         else if(m_speed.Y < target_speed.Y)
149                 m_speed.Y = target_speed.Y;
150         else if(m_speed.Y > target_speed.Y)
151                 m_speed.Y = target_speed.Y;
152 #endif
153 }
154
155 v3s16 Player::getLightPosition() const
156 {
157         return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
158 }
159
160 void Player::serialize(std::ostream &os)
161 {
162         // Utilize a Settings object for storing values
163         Settings args;
164         args.setS32("version", 1);
165         args.set("name", m_name);
166         //args.set("password", m_password);
167         args.setFloat("pitch", m_pitch);
168         args.setFloat("yaw", m_yaw);
169         args.setV3F("position", m_position);
170         args.setS32("hp", hp);
171
172         args.writeLines(os);
173
174         os<<"PlayerArgsEnd\n";
175         
176         inventory.serialize(os);
177 }
178
179 void Player::deSerialize(std::istream &is)
180 {
181         Settings args;
182         
183         for(;;)
184         {
185                 if(is.eof())
186                         throw SerializationError
187                                         ("Player::deSerialize(): PlayerArgsEnd not found");
188                 std::string line;
189                 std::getline(is, line);
190                 std::string trimmedline = trim(line);
191                 if(trimmedline == "PlayerArgsEnd")
192                         break;
193                 args.parseConfigLine(line);
194         }
195
196         //args.getS32("version"); // Version field value not used
197         std::string name = args.get("name");
198         updateName(name.c_str());
199         setPitch(args.getFloat("pitch"));
200         setYaw(args.getFloat("yaw"));
201         setPosition(args.getV3F("position"));
202         try{
203                 hp = args.getS32("hp");
204         }catch(SettingNotFoundException &e){
205                 hp = 20;
206         }
207
208         inventory.deSerialize(is);
209
210         if(inventory.getList("craftpreview") == NULL)
211         {
212                 // Convert players without craftpreview
213                 inventory.addList("craftpreview", 1);
214
215                 bool craftresult_is_preview = true;
216                 if(args.exists("craftresult_is_preview"))
217                         craftresult_is_preview = args.getBool("craftresult_is_preview");
218                 if(craftresult_is_preview)
219                 {
220                         // Clear craftresult
221                         inventory.getList("craftresult")->changeItem(0, ItemStack());
222                 }
223         }
224 }
225
226 /*
227         RemotePlayer
228 */
229
230
231
232
233
234 void RemotePlayer::setPosition(const v3f &position)
235 {
236         Player::setPosition(position);
237         if(m_sao)
238                 m_sao->setBasePosition(position);
239 }