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