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