Clean up threading
[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 "threading/mutex.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                 float eye_height = camera_barely_in_ceiling ? 1.5f : 1.625f;
134                 return v3f(0, BS * eye_height, 0);
135         }
136
137         v3f getEyePosition()
138         {
139                 return m_position + getEyeOffset();
140         }
141
142         virtual void setPosition(const v3f &position)
143         {
144                 if (position != m_position)
145                         m_dirty = true;
146                 m_position = position;
147         }
148
149         void setPitch(f32 pitch)
150         {
151                 if (pitch != m_pitch)
152                         m_dirty = true;
153                 m_pitch = pitch;
154         }
155
156         virtual void setYaw(f32 yaw)
157         {
158                 if (yaw != m_yaw)
159                         m_dirty = true;
160                 m_yaw = yaw;
161         }
162
163         f32 getPitch()
164         {
165                 return m_pitch;
166         }
167
168         f32 getYaw()
169         {
170                 return m_yaw;
171         }
172
173         u16 getBreath()
174         {
175                 return m_breath;
176         }
177
178         virtual void setBreath(u16 breath)
179         {
180                 if (breath != m_breath)
181                         m_dirty = true;
182                 m_breath = breath;
183         }
184
185         f32 getRadPitch()
186         {
187                 return -1.0 * m_pitch * core::DEGTORAD;
188         }
189
190         f32 getRadYaw()
191         {
192                 return (m_yaw + 90.) * core::DEGTORAD;
193         }
194
195         const char *getName() const
196         {
197                 return m_name;
198         }
199
200         core::aabbox3d<f32> getCollisionbox()
201         {
202                 return m_collisionbox;
203         }
204
205         u32 getFreeHudID() {
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         void setHotbarItemcount(s32 hotbar_itemcount)
215         {
216                 hud_hotbar_itemcount = hotbar_itemcount;
217         }
218
219         s32 getHotbarItemcount()
220         {
221                 return hud_hotbar_itemcount;
222         }
223
224         void setHotbarImage(const std::string &name)
225         {
226                 hud_hotbar_image = name;
227         }
228
229         std::string getHotbarImage()
230         {
231                 return hud_hotbar_image;
232         }
233
234         void setHotbarSelectedImage(const std::string &name)
235         {
236                 hud_hotbar_selected_image = name;
237         }
238
239         std::string getHotbarSelectedImage() {
240                 return hud_hotbar_selected_image;
241         }
242
243         void setSky(const video::SColor &bgcolor, const std::string &type,
244                 const std::vector<std::string> &params)
245         {
246                 m_sky_bgcolor = bgcolor;
247                 m_sky_type = type;
248                 m_sky_params = params;
249         }
250
251         void getSky(video::SColor *bgcolor, std::string *type,
252                 std::vector<std::string> *params)
253         {
254                 *bgcolor = m_sky_bgcolor;
255                 *type = m_sky_type;
256                 *params = m_sky_params;
257         }
258
259         void overrideDayNightRatio(bool do_override, float ratio)
260         {
261                 m_day_night_ratio_do_override = do_override;
262                 m_day_night_ratio = ratio;
263         }
264
265         void getDayNightRatio(bool *do_override, float *ratio)
266         {
267                 *do_override = m_day_night_ratio_do_override;
268                 *ratio = m_day_night_ratio;
269         }
270
271         void setLocalAnimations(v2s32 frames[4], float frame_speed)
272         {
273                 for (int i = 0; i < 4; i++)
274                         local_animations[i] = frames[i];
275                 local_animation_speed = frame_speed;
276         }
277
278         void getLocalAnimations(v2s32 *frames, float *frame_speed)
279         {
280                 for (int i = 0; i < 4; i++)
281                         frames[i] = local_animations[i];
282                 *frame_speed = local_animation_speed;
283         }
284
285         virtual bool isLocal() const
286         {
287                 return false;
288         }
289
290         virtual PlayerSAO *getPlayerSAO()
291         {
292                 return NULL;
293         }
294
295         virtual void setPlayerSAO(PlayerSAO *sao)
296         {
297                 FATAL_ERROR("FIXME");
298         }
299
300         /*
301                 serialize() writes a bunch of text that can contain
302                 any characters except a '\0', and such an ending that
303                 deSerialize stops reading exactly at the right point.
304         */
305         void serialize(std::ostream &os);
306         void deSerialize(std::istream &is, std::string playername);
307
308         bool checkModified() const
309         {
310                 return m_dirty || inventory.checkModified();
311         }
312
313         void setModified(const bool x)
314         {
315                 m_dirty = x;
316                 if (x == false)
317                         inventory.setModified(x);
318         }
319
320         // Use a function, if isDead can be defined by other conditions
321         bool isDead() { return hp == 0; }
322
323         bool touching_ground;
324         // This oscillates so that the player jumps a bit above the surface
325         bool in_liquid;
326         // This is more stable and defines the maximum speed of the player
327         bool in_liquid_stable;
328         // Gets the viscosity of water to calculate friction
329         u8 liquid_viscosity;
330         bool is_climbing;
331         bool swimming_vertical;
332         bool camera_barely_in_ceiling;
333         v3f eye_offset_first;
334         v3f eye_offset_third;
335
336         Inventory inventory;
337
338         f32 movement_acceleration_default;
339         f32 movement_acceleration_air;
340         f32 movement_acceleration_fast;
341         f32 movement_speed_walk;
342         f32 movement_speed_crouch;
343         f32 movement_speed_fast;
344         f32 movement_speed_climb;
345         f32 movement_speed_jump;
346         f32 movement_liquid_fluidity;
347         f32 movement_liquid_fluidity_smooth;
348         f32 movement_liquid_sink;
349         f32 movement_gravity;
350
351         float physics_override_speed;
352         float physics_override_jump;
353         float physics_override_gravity;
354         bool physics_override_sneak;
355         bool physics_override_sneak_glitch;
356
357         v2s32 local_animations[4];
358         float local_animation_speed;
359
360         u16 hp;
361
362         float hurt_tilt_timer;
363         float hurt_tilt_strength;
364
365         u16 protocol_version;
366         u16 peer_id;
367
368         std::string inventory_formspec;
369
370         PlayerControl control;
371         PlayerControl getPlayerControl()
372         {
373                 return control;
374         }
375
376         u32 keyPressed;
377
378
379         HudElement* getHud(u32 id);
380         u32         addHud(HudElement* hud);
381         HudElement* removeHud(u32 id);
382         void        clearHud();
383         u32         maxHudId() {
384                 return hud.size();
385         }
386
387         u32 hud_flags;
388         s32 hud_hotbar_itemcount;
389         std::string hud_hotbar_image;
390         std::string hud_hotbar_selected_image;
391 protected:
392         IGameDef *m_gamedef;
393
394         char m_name[PLAYERNAME_SIZE];
395         u16 m_breath;
396         f32 m_pitch;
397         f32 m_yaw;
398         v3f m_speed;
399         v3f m_position;
400         core::aabbox3d<f32> m_collisionbox;
401
402         bool m_dirty;
403
404         std::vector<HudElement *> hud;
405
406         std::string m_sky_type;
407         video::SColor m_sky_bgcolor;
408         std::vector<std::string> m_sky_params;
409
410         bool m_day_night_ratio_do_override;
411         float m_day_night_ratio;
412 private:
413         // Protect some critical areas
414         // hud for example can be modified by EmergeThread
415         // and ServerThread
416         Mutex m_mutex;
417 };
418
419
420 /*
421         Player on the server
422 */
423 class RemotePlayer : public Player
424 {
425 public:
426         RemotePlayer(IGameDef *gamedef, const char *name):
427                 Player(gamedef, name),
428                 m_sao(NULL)
429         {}
430         virtual ~RemotePlayer() {}
431
432         void save(std::string savedir);
433
434         PlayerSAO *getPlayerSAO()
435         { return m_sao; }
436         void setPlayerSAO(PlayerSAO *sao)
437         { m_sao = sao; }
438         void setPosition(const v3f &position);
439
440 private:
441         PlayerSAO *m_sao;
442 };
443
444 #endif
445