b317cda4fbeaf3035c102365bb2fe3f2f6b3c04b
[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 #define PLAYERNAME_ALLOWED_CHARS_USER_EXPL "'a' to 'z', 'A' to 'Z', '0' to '9', '-', '_'"
33
34 struct PlayerControl
35 {
36         PlayerControl()
37         {
38                 up = false;
39                 down = false;
40                 left = false;
41                 right = false;
42                 jump = false;
43                 aux1 = false;
44                 sneak = false;
45                 LMB = false;
46                 RMB = false;
47                 pitch = 0;
48                 yaw = 0;
49         }
50         PlayerControl(
51                 bool a_up,
52                 bool a_down,
53                 bool a_left,
54                 bool a_right,
55                 bool a_jump,
56                 bool a_aux1,
57                 bool a_sneak,
58                 bool a_LMB,
59                 bool a_RMB,
60                 float a_pitch,
61                 float a_yaw
62         )
63         {
64                 up = a_up;
65                 down = a_down;
66                 left = a_left;
67                 right = a_right;
68                 jump = a_jump;
69                 aux1 = a_aux1;
70                 sneak = a_sneak;
71                 LMB = a_LMB;
72                 RMB = a_RMB;
73                 pitch = a_pitch;
74                 yaw = a_yaw;
75         }
76         bool up;
77         bool down;
78         bool left;
79         bool right;
80         bool jump;
81         bool aux1;
82         bool sneak;
83         bool LMB;
84         bool RMB;
85         float pitch;
86         float yaw;
87 };
88
89 class Map;
90 class IGameDef;
91 struct CollisionInfo;
92 class PlayerSAO;
93 struct HudElement;
94 class Environment;
95
96 // IMPORTANT:
97 // Do *not* perform an assignment or copy operation on a Player or
98 // RemotePlayer object!  This will copy the lock held for HUD synchronization
99 class Player
100 {
101 public:
102
103         Player(IGameDef *gamedef, const char *name);
104         virtual ~Player() = 0;
105
106         virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
107         {}
108         virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
109                         std::vector<CollisionInfo> *collision_info)
110         {}
111
112         v3f getSpeed()
113         {
114                 return m_speed;
115         }
116
117         void setSpeed(v3f speed)
118         {
119                 m_speed = speed;
120         }
121
122         v3f getPosition()
123         {
124                 return m_position;
125         }
126
127         v3s16 getLightPosition() const;
128
129         v3f getEyeOffset()
130         {
131                 float eye_height = camera_barely_in_ceiling ? 1.5f : 1.625f;
132                 return v3f(0, BS * eye_height, 0);
133         }
134
135         v3f getEyePosition()
136         {
137                 return m_position + getEyeOffset();
138         }
139
140         virtual void setPosition(const v3f &position)
141         {
142                 if (position != m_position)
143                         m_dirty = true;
144                 m_position = position;
145         }
146
147         void setPitch(f32 pitch)
148         {
149                 if (pitch != m_pitch)
150                         m_dirty = true;
151                 m_pitch = pitch;
152         }
153
154         virtual void setYaw(f32 yaw)
155         {
156                 if (yaw != m_yaw)
157                         m_dirty = true;
158                 m_yaw = yaw;
159         }
160
161         f32 getPitch()
162         {
163                 return m_pitch;
164         }
165
166         f32 getYaw()
167         {
168                 return m_yaw;
169         }
170
171         u16 getBreath()
172         {
173                 return m_breath;
174         }
175
176         virtual void setBreath(u16 breath)
177         {
178                 if (breath != m_breath)
179                         m_dirty = true;
180                 m_breath = breath;
181         }
182
183         f32 getRadPitch()
184         {
185                 return -1.0 * m_pitch * core::DEGTORAD;
186         }
187
188         f32 getRadYaw()
189         {
190                 return (m_yaw + 90.) * core::DEGTORAD;
191         }
192
193         const char *getName() const
194         {
195                 return m_name;
196         }
197
198         aabb3f getCollisionbox()
199         {
200                 return m_collisionbox;
201         }
202
203         u32 getFreeHudID() {
204                 size_t size = hud.size();
205                 for (size_t i = 0; i != size; i++) {
206                         if (!hud[i])
207                                 return i;
208                 }
209                 return size;
210         }
211
212         void setHotbarItemcount(s32 hotbar_itemcount)
213         {
214                 hud_hotbar_itemcount = hotbar_itemcount;
215         }
216
217         s32 getHotbarItemcount()
218         {
219                 return hud_hotbar_itemcount;
220         }
221
222         void setHotbarImage(const std::string &name)
223         {
224                 hud_hotbar_image = name;
225         }
226
227         std::string getHotbarImage()
228         {
229                 return hud_hotbar_image;
230         }
231
232         void setHotbarSelectedImage(const std::string &name)
233         {
234                 hud_hotbar_selected_image = name;
235         }
236
237         std::string getHotbarSelectedImage() {
238                 return hud_hotbar_selected_image;
239         }
240
241         void setSky(const video::SColor &bgcolor, const std::string &type,
242                 const std::vector<std::string> &params)
243         {
244                 m_sky_bgcolor = bgcolor;
245                 m_sky_type = type;
246                 m_sky_params = params;
247         }
248
249         void getSky(video::SColor *bgcolor, std::string *type,
250                 std::vector<std::string> *params)
251         {
252                 *bgcolor = m_sky_bgcolor;
253                 *type = m_sky_type;
254                 *params = m_sky_params;
255         }
256
257         void overrideDayNightRatio(bool do_override, float ratio)
258         {
259                 m_day_night_ratio_do_override = do_override;
260                 m_day_night_ratio = ratio;
261         }
262
263         void getDayNightRatio(bool *do_override, float *ratio)
264         {
265                 *do_override = m_day_night_ratio_do_override;
266                 *ratio = m_day_night_ratio;
267         }
268
269         void setLocalAnimations(v2s32 frames[4], float frame_speed)
270         {
271                 for (int i = 0; i < 4; i++)
272                         local_animations[i] = frames[i];
273                 local_animation_speed = frame_speed;
274         }
275
276         void getLocalAnimations(v2s32 *frames, float *frame_speed)
277         {
278                 for (int i = 0; i < 4; i++)
279                         frames[i] = local_animations[i];
280                 *frame_speed = local_animation_speed;
281         }
282
283         virtual bool isLocal() const
284         {
285                 return false;
286         }
287
288         virtual PlayerSAO *getPlayerSAO()
289         {
290                 return NULL;
291         }
292
293         virtual void setPlayerSAO(PlayerSAO *sao)
294         {
295                 FATAL_ERROR("FIXME");
296         }
297
298         /*
299                 serialize() writes a bunch of text that can contain
300                 any characters except a '\0', and such an ending that
301                 deSerialize stops reading exactly at the right point.
302         */
303         void serialize(std::ostream &os);
304         void deSerialize(std::istream &is, std::string playername);
305
306         bool checkModified() const
307         {
308                 return m_dirty || inventory.checkModified();
309         }
310
311         void setModified(const bool x)
312         {
313                 m_dirty = x;
314                 if (x == false)
315                         inventory.setModified(x);
316         }
317
318         // Use a function, if isDead can be defined by other conditions
319         bool isDead() { return hp == 0; }
320
321         bool got_teleported;
322         bool touching_ground;
323         // This oscillates so that the player jumps a bit above the surface
324         bool in_liquid;
325         // This is more stable and defines the maximum speed of the player
326         bool in_liquid_stable;
327         // Gets the viscosity of water to calculate friction
328         u8 liquid_viscosity;
329         bool is_climbing;
330         bool swimming_vertical;
331         bool camera_barely_in_ceiling;
332         v3f eye_offset_first;
333         v3f eye_offset_third;
334
335         Inventory inventory;
336
337         f32 movement_acceleration_default;
338         f32 movement_acceleration_air;
339         f32 movement_acceleration_fast;
340         f32 movement_speed_walk;
341         f32 movement_speed_crouch;
342         f32 movement_speed_fast;
343         f32 movement_speed_climb;
344         f32 movement_speed_jump;
345         f32 movement_liquid_fluidity;
346         f32 movement_liquid_fluidity_smooth;
347         f32 movement_liquid_sink;
348         f32 movement_gravity;
349
350         float physics_override_speed;
351         float physics_override_jump;
352         float physics_override_gravity;
353         bool physics_override_sneak;
354         bool physics_override_sneak_glitch;
355
356         v2s32 local_animations[4];
357         float local_animation_speed;
358
359         u16 hp;
360
361         float hurt_tilt_timer;
362         float hurt_tilt_strength;
363
364         u16 protocol_version;
365         u16 peer_id;
366
367         std::string inventory_formspec;
368
369         PlayerControl control;
370         PlayerControl getPlayerControl()
371         {
372                 return control;
373         }
374
375         u32 keyPressed;
376
377
378         HudElement* getHud(u32 id);
379         u32         addHud(HudElement* hud);
380         HudElement* removeHud(u32 id);
381         void        clearHud();
382         u32         maxHudId() {
383                 return hud.size();
384         }
385
386         u32 hud_flags;
387         s32 hud_hotbar_itemcount;
388         std::string hud_hotbar_image;
389         std::string hud_hotbar_selected_image;
390 protected:
391         IGameDef *m_gamedef;
392
393         char m_name[PLAYERNAME_SIZE];
394         u16 m_breath;
395         f32 m_pitch;
396         f32 m_yaw;
397         v3f m_speed;
398         v3f m_position;
399         aabb3f m_collisionbox;
400
401         bool m_dirty;
402
403         std::vector<HudElement *> hud;
404
405         std::string m_sky_type;
406         video::SColor m_sky_bgcolor;
407         std::vector<std::string> m_sky_params;
408
409         bool m_day_night_ratio_do_override;
410         float m_day_night_ratio;
411 private:
412         // Protect some critical areas
413         // hud for example can be modified by EmergeThread
414         // and ServerThread
415         Mutex m_mutex;
416 };
417
418
419 /*
420         Player on the server
421 */
422 class RemotePlayer : public Player
423 {
424 public:
425         RemotePlayer(IGameDef *gamedef, const char *name);
426         virtual ~RemotePlayer() {}
427
428         void save(std::string savedir);
429
430         PlayerSAO *getPlayerSAO()
431         { return m_sao; }
432         void setPlayerSAO(PlayerSAO *sao)
433         { m_sao = sao; }
434         void setPosition(const v3f &position);
435
436 private:
437         PlayerSAO *m_sao;
438 };
439
440 #endif
441