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