Code modernization: src/p*, src/q*, src/r*, src/s* (partial) (#6282)
[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 #pragma once
21
22 #include "irrlichttypes_bloated.h"
23 #include "inventory.h"
24 #include "constants.h"
25 #include "util/basic_macros.h"
26 #include <list>
27 #include <mutex>
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() = default;
37
38         PlayerControl(
39                 bool a_up,
40                 bool a_down,
41                 bool a_left,
42                 bool a_right,
43                 bool a_jump,
44                 bool a_aux1,
45                 bool a_sneak,
46                 bool a_zoom,
47                 bool a_LMB,
48                 bool a_RMB,
49                 float a_pitch,
50                 float a_yaw,
51                 float a_sidew_move_joystick_axis,
52                 float a_forw_move_joystick_axis
53         )
54         {
55                 up = a_up;
56                 down = a_down;
57                 left = a_left;
58                 right = a_right;
59                 jump = a_jump;
60                 aux1 = a_aux1;
61                 sneak = a_sneak;
62                 zoom = a_zoom;
63                 LMB = a_LMB;
64                 RMB = a_RMB;
65                 pitch = a_pitch;
66                 yaw = a_yaw;
67                 sidew_move_joystick_axis = a_sidew_move_joystick_axis;
68                 forw_move_joystick_axis = a_forw_move_joystick_axis;
69         }
70         bool up = false;
71         bool down = false;
72         bool left = false;
73         bool right = false;
74         bool jump = false;
75         bool aux1 = false;
76         bool sneak = false;
77         bool zoom = false;
78         bool LMB = false;
79         bool RMB = false;
80         float pitch = 0.0f;
81         float yaw = 0.0f;
82         float sidew_move_joystick_axis = 0.0f;
83         float forw_move_joystick_axis = 0.0f;
84 };
85
86 class Map;
87 struct CollisionInfo;
88 struct HudElement;
89 class Environment;
90
91 class Player
92 {
93 public:
94
95         Player(const char *name, IItemDefManager *idef);
96         virtual ~Player() = 0;
97
98         DISABLE_CLASS_COPY(Player);
99
100         virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
101         {}
102         virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
103                         std::vector<CollisionInfo> *collision_info)
104         {}
105
106         const v3f &getSpeed() const
107         {
108                 return m_speed;
109         }
110
111         void setSpeed(const v3f &speed)
112         {
113                 m_speed = speed;
114         }
115
116         const char *getName() const { return m_name; }
117
118         u32 getFreeHudID()
119         {
120                 size_t size = hud.size();
121                 for (size_t i = 0; i != size; i++) {
122                         if (!hud[i])
123                                 return i;
124                 }
125                 return size;
126         }
127
128         v3f eye_offset_first;
129         v3f eye_offset_third;
130
131         Inventory inventory;
132
133         f32 movement_acceleration_default;
134         f32 movement_acceleration_air;
135         f32 movement_acceleration_fast;
136         f32 movement_speed_walk;
137         f32 movement_speed_crouch;
138         f32 movement_speed_fast;
139         f32 movement_speed_climb;
140         f32 movement_speed_jump;
141         f32 movement_liquid_fluidity;
142         f32 movement_liquid_fluidity_smooth;
143         f32 movement_liquid_sink;
144         f32 movement_gravity;
145
146         v2s32 local_animations[4];
147         float local_animation_speed;
148
149         u16 peer_id = PEER_ID_INEXISTENT;
150
151         std::string inventory_formspec;
152
153         PlayerControl control;
154         const PlayerControl& getPlayerControl() { return control; }
155
156         u32 keyPressed = 0;
157
158         HudElement* getHud(u32 id);
159         u32         addHud(HudElement* hud);
160         HudElement* removeHud(u32 id);
161         void        clearHud();
162
163         u32 hud_flags;
164         s32 hud_hotbar_itemcount;
165 protected:
166         char m_name[PLAYERNAME_SIZE];
167         v3f m_speed;
168
169         std::vector<HudElement *> hud;
170 private:
171         // Protect some critical areas
172         // hud for example can be modified by EmergeThread
173         // and ServerThread
174         std::mutex m_mutex;
175 };