Fix usage of destroyed mutex
[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 "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 // IMPORTANT:
96 // Do *not* perform an assignment or copy operation on a Player or
97 // RemotePlayer object!  This will copy the lock held for HUD synchronization
98 class Player
99 {
100 public:
101
102         Player(IGameDef *gamedef, const char *name);
103         virtual ~Player() = 0;
104
105         virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
106         {}
107         virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
108                         std::vector<CollisionInfo> *collision_info)
109         {}
110
111         v3f getSpeed()
112         {
113                 return m_speed;
114         }
115
116         void setSpeed(v3f speed)
117         {
118                 m_speed = speed;
119         }
120
121         void accelerateHorizontal(v3f target_speed, f32 max_increase);
122         void accelerateVertical(v3f target_speed, f32 max_increase);
123
124         v3f getPosition()
125         {
126                 return m_position;
127         }
128
129         v3s16 getLightPosition() const;
130
131         v3f getEyeOffset()
132         {
133                 // This is at the height of the eyes of the current figure
134                 // return v3f(0, BS*1.5, 0);
135                 // This is more like in minecraft
136                 if(camera_barely_in_ceiling)
137                         return v3f(0,BS*1.5,0);
138                 else
139                         return v3f(0,BS*1.625,0);
140         }
141
142         v3f getEyePosition()
143         {
144                 return m_position + getEyeOffset();
145         }
146
147         virtual void setPosition(const v3f &position)
148         {
149                 if (position != m_position)
150                         m_dirty = true;
151                 m_position = position;
152         }
153
154         void setPitch(f32 pitch)
155         {
156                 if (pitch != m_pitch)
157                         m_dirty = true;
158                 m_pitch = pitch;
159         }
160
161         virtual void setYaw(f32 yaw)
162         {
163                 if (yaw != m_yaw)
164                         m_dirty = true;
165                 m_yaw = yaw;
166         }
167
168         f32 getPitch()
169         {
170                 return m_pitch;
171         }
172
173         f32 getYaw()
174         {
175                 return m_yaw;
176         }
177
178         u16 getBreath()
179         {
180                 return m_breath;
181         }
182
183         virtual void setBreath(u16 breath)
184         {
185                 if (breath != m_breath)
186                         m_dirty = true;
187                 m_breath = breath;
188         }
189
190         f32 getRadPitch()
191         {
192                 return -1.0 * m_pitch * core::DEGTORAD;
193         }
194
195         f32 getRadYaw()
196         {
197                 return (m_yaw + 90.) * core::DEGTORAD;
198         }
199
200         const char * getName() const
201         {
202                 return m_name;
203         }
204
205         core::aabbox3d<f32> getCollisionbox() {
206                 return m_collisionbox;
207         }
208
209         u32 getFreeHudID() {
210                 size_t size = hud.size();
211                 for (size_t i = 0; i != size; i++) {
212                         if (!hud[i])
213                                 return i;
214                 }
215                 return size;
216         }
217
218         virtual bool isLocal() const
219         { return false; }
220         virtual PlayerSAO *getPlayerSAO()
221         { return NULL; }
222         virtual void setPlayerSAO(PlayerSAO *sao)
223         { FATAL_ERROR("FIXME"); }
224
225         /*
226                 serialize() writes a bunch of text that can contain
227                 any characters except a '\0', and such an ending that
228                 deSerialize stops reading exactly at the right point.
229         */
230         void serialize(std::ostream &os);
231         void deSerialize(std::istream &is, std::string playername);
232
233         bool checkModified() const
234         {
235                 return m_dirty || inventory.checkModified();
236         }
237
238         void setModified(const bool x)
239         {
240                 m_dirty = x;
241                 if (x == false)
242                         inventory.setModified(x);
243         }
244
245         // Use a function, if isDead can be defined by other conditions
246         bool isDead() { return hp == 0; }
247
248         bool touching_ground;
249         // This oscillates so that the player jumps a bit above the surface
250         bool in_liquid;
251         // This is more stable and defines the maximum speed of the player
252         bool in_liquid_stable;
253         // Gets the viscosity of water to calculate friction
254         u8 liquid_viscosity;
255         bool is_climbing;
256         bool swimming_vertical;
257         bool camera_barely_in_ceiling;
258
259         Inventory inventory;
260
261         f32 movement_acceleration_default;
262         f32 movement_acceleration_air;
263         f32 movement_acceleration_fast;
264         f32 movement_speed_walk;
265         f32 movement_speed_crouch;
266         f32 movement_speed_fast;
267         f32 movement_speed_climb;
268         f32 movement_speed_jump;
269         f32 movement_liquid_fluidity;
270         f32 movement_liquid_fluidity_smooth;
271         f32 movement_liquid_sink;
272         f32 movement_gravity;
273
274         float physics_override_speed;
275         float physics_override_jump;
276         float physics_override_gravity;
277         bool physics_override_sneak;
278         bool physics_override_sneak_glitch;
279
280         v2s32 local_animations[4];
281         float local_animation_speed;
282
283         u16 hp;
284
285         float hurt_tilt_timer;
286         float hurt_tilt_strength;
287
288         u16 peer_id;
289
290         std::string inventory_formspec;
291
292         PlayerControl control;
293         PlayerControl getPlayerControl()
294         {
295                 return control;
296         }
297
298         u32 keyPressed;
299
300
301         HudElement* getHud(u32 id);
302         u32         addHud(HudElement* hud);
303         HudElement* removeHud(u32 id);
304         void        clearHud();
305         u32         maxHudId() {
306                 return hud.size();
307         }
308
309         u32 hud_flags;
310         s32 hud_hotbar_itemcount;
311 protected:
312         IGameDef *m_gamedef;
313
314         char m_name[PLAYERNAME_SIZE];
315         u16 m_breath;
316         f32 m_pitch;
317         f32 m_yaw;
318         v3f m_speed;
319         v3f m_position;
320         core::aabbox3d<f32> m_collisionbox;
321
322         bool m_dirty;
323
324         std::vector<HudElement *> hud;
325 private:
326         // Protect some critical areas
327         // hud for example can be modified by EmergeThread
328         // and ServerThread
329         JMutex m_mutex;
330 };
331
332
333 /*
334         Player on the server
335 */
336 class RemotePlayer : public Player
337 {
338 public:
339         RemotePlayer(IGameDef *gamedef, const char *name):
340                 Player(gamedef, name),
341                 m_sao(NULL)
342         {}
343         virtual ~RemotePlayer() {}
344
345         void save(std::string savedir);
346
347         PlayerSAO *getPlayerSAO()
348         { return m_sao; }
349         void setPlayerSAO(PlayerSAO *sao)
350         { m_sao = sao; }
351         void setPosition(const v3f &position);
352
353 private:
354         PlayerSAO *m_sao;
355 };
356
357 #endif
358