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