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