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