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