For usages of assert() that are meant to persist in Release builds (when NDEBUG is...
[oweals/minetest.git] / src / player.h
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 #ifndef PLAYER_HEADER
21 #define PLAYER_HEADER
22
23 #include "irrlichttypes_bloated.h"
24 #include "inventory.h"
25 #include "constants.h" // BS
26 #include <list>
27
28 #define PLAYERNAME_SIZE 20
29
30 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
31
32 struct PlayerControl
33 {
34         PlayerControl()
35         {
36                 up = false;
37                 down = false;
38                 left = false;
39                 right = false;
40                 jump = false;
41                 aux1 = false;
42                 sneak = false;
43                 LMB = false;
44                 RMB = false;
45                 pitch = 0;
46                 yaw = 0;
47         }
48         PlayerControl(
49                 bool a_up,
50                 bool a_down,
51                 bool a_left,
52                 bool a_right,
53                 bool a_jump,
54                 bool a_aux1,
55                 bool a_sneak,
56                 bool a_LMB,
57                 bool a_RMB,
58                 float a_pitch,
59                 float a_yaw
60         )
61         {
62                 up = a_up;
63                 down = a_down;
64                 left = a_left;
65                 right = a_right;
66                 jump = a_jump;
67                 aux1 = a_aux1;
68                 sneak = a_sneak;
69                 LMB = a_LMB;
70                 RMB = a_RMB;
71                 pitch = a_pitch;
72                 yaw = a_yaw;
73         }
74         bool up;
75         bool down;
76         bool left;
77         bool right;
78         bool jump;
79         bool aux1;
80         bool sneak;
81         bool LMB;
82         bool RMB;
83         float pitch;
84         float yaw;
85 };
86
87 class Map;
88 class IGameDef;
89 struct CollisionInfo;
90 class PlayerSAO;
91 struct HudElement;
92 class Environment;
93
94 class Player
95 {
96 public:
97
98         Player(IGameDef *gamedef, const char *name);
99         virtual ~Player() = 0;
100
101         virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
102         {}
103         virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
104                         std::list<CollisionInfo> *collision_info)
105         {}
106
107         v3f getSpeed()
108         {
109                 return m_speed;
110         }
111
112         void setSpeed(v3f speed)
113         {
114                 m_speed = speed;
115         }
116         
117         void accelerateHorizontal(v3f target_speed, f32 max_increase);
118         void accelerateVertical(v3f target_speed, f32 max_increase);
119
120         v3f getPosition()
121         {
122                 return m_position;
123         }
124
125         v3s16 getLightPosition() const;
126
127         v3f getEyeOffset()
128         {
129                 // This is at the height of the eyes of the current figure
130                 // return v3f(0, BS*1.5, 0);
131                 // This is more like in minecraft
132                 if(camera_barely_in_ceiling)
133                         return v3f(0,BS*1.5,0);
134                 else
135                         return v3f(0,BS*1.625,0);
136         }
137
138         v3f getEyePosition()
139         {
140                 return m_position + getEyeOffset();
141         }
142
143         virtual void setPosition(const v3f &position)
144         {
145                 if (position != m_position)
146                         m_dirty = true;
147                 m_position = position;
148         }
149
150         void setPitch(f32 pitch)
151         {
152                 if (pitch != m_pitch)
153                         m_dirty = true;
154                 m_pitch = pitch;
155         }
156
157         virtual void setYaw(f32 yaw)
158         {
159                 if (yaw != m_yaw)
160                         m_dirty = true;
161                 m_yaw = yaw;
162         }
163
164         f32 getPitch()
165         {
166                 return m_pitch;
167         }
168
169         f32 getYaw()
170         {
171                 return m_yaw;
172         }
173
174         u16 getBreath()
175         {
176                 return m_breath;
177         }
178
179         virtual void setBreath(u16 breath)
180         {
181                 if (breath != m_breath)
182                         m_dirty = true;
183                 m_breath = breath;
184         }
185
186         f32 getRadPitch()
187         {
188                 return -1.0 * m_pitch * core::DEGTORAD;
189         }
190
191         f32 getRadYaw()
192         {
193                 return (m_yaw + 90.) * core::DEGTORAD;
194         }
195
196         const char * getName() const
197         {
198                 return m_name;
199         }
200
201         core::aabbox3d<f32> getCollisionbox() {
202                 return m_collisionbox;
203         }
204
205         u32 getFreeHudID() const {
206                 size_t size = hud.size();
207                 for (size_t i = 0; i != size; i++) {
208                         if (!hud[i])
209                                 return i;
210                 }
211                 return size;
212         }
213
214         virtual bool isLocal() const
215         { return false; }
216         virtual PlayerSAO *getPlayerSAO()
217         { return NULL; }
218         virtual void setPlayerSAO(PlayerSAO *sao)
219         { FATAL_ERROR("FIXME"); }
220
221         /*
222                 serialize() writes a bunch of text that can contain
223                 any characters except a '\0', and such an ending that
224                 deSerialize stops reading exactly at the right point.
225         */
226         void serialize(std::ostream &os);
227         void deSerialize(std::istream &is, std::string playername);
228
229         bool checkModified() const
230         {
231                 return m_dirty || inventory.checkModified();
232         }
233
234         void setModified(const bool x)
235         {
236                 m_dirty = x;
237                 if (x == false)
238                         inventory.setModified(x);
239         }
240
241         bool touching_ground;
242         // This oscillates so that the player jumps a bit above the surface
243         bool in_liquid;
244         // This is more stable and defines the maximum speed of the player
245         bool in_liquid_stable;
246         // Gets the viscosity of water to calculate friction
247         u8 liquid_viscosity;
248         bool is_climbing;
249         bool swimming_vertical;
250         bool camera_barely_in_ceiling;
251         
252         Inventory inventory;
253
254         f32 movement_acceleration_default;
255         f32 movement_acceleration_air;
256         f32 movement_acceleration_fast;
257         f32 movement_speed_walk;
258         f32 movement_speed_crouch;
259         f32 movement_speed_fast;
260         f32 movement_speed_climb;
261         f32 movement_speed_jump;
262         f32 movement_liquid_fluidity;
263         f32 movement_liquid_fluidity_smooth;
264         f32 movement_liquid_sink;
265         f32 movement_gravity;
266
267         float physics_override_speed;
268         float physics_override_jump;
269         float physics_override_gravity;
270         bool physics_override_sneak;
271         bool physics_override_sneak_glitch;
272
273         v2s32 local_animations[4];
274         float local_animation_speed;
275
276         u16 hp;
277
278         float hurt_tilt_timer;
279         float hurt_tilt_strength;
280
281         u16 peer_id;
282
283         std::string inventory_formspec;
284         
285         PlayerControl control;
286         PlayerControl getPlayerControl()
287         {
288                 return control;
289         }
290         
291         u32 keyPressed;
292         
293
294         HudElement* getHud(u32 id);
295         u32         addHud(HudElement* hud);
296         HudElement* removeHud(u32 id);
297         void        clearHud();
298         u32         maxHudId() {
299                 return hud.size();
300         }
301
302         u32 hud_flags;
303         s32 hud_hotbar_itemcount;
304 protected:
305         IGameDef *m_gamedef;
306
307         char m_name[PLAYERNAME_SIZE];
308         u16 m_breath;
309         f32 m_pitch;
310         f32 m_yaw;
311         v3f m_speed;
312         v3f m_position;
313         core::aabbox3d<f32> m_collisionbox;
314
315         bool m_dirty;
316
317         std::vector<HudElement *> hud;
318 };
319
320
321 /*
322         Player on the server
323 */
324 class RemotePlayer : public Player
325 {
326 public:
327         RemotePlayer(IGameDef *gamedef, const char *name):
328                 Player(gamedef, name),
329                 m_sao(NULL)
330         {}
331         virtual ~RemotePlayer() {}
332
333         void save(std::string savedir);
334
335         PlayerSAO *getPlayerSAO()
336         { return m_sao; }
337         void setPlayerSAO(PlayerSAO *sao)
338         { m_sao = sao; }
339         void setPosition(const v3f &position);
340         
341 private:
342         PlayerSAO *m_sao;
343 };
344
345 #endif
346