Fix bone-attached entities (#10015)
[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 "network/networkprotocol.h"
26 #include "util/basic_macros.h"
27 #include <list>
28 #include <mutex>
29
30 #define PLAYERNAME_SIZE 20
31
32 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
33 #define PLAYERNAME_ALLOWED_CHARS_USER_EXPL "'a' to 'z', 'A' to 'Z', '0' to '9', '-', '_'"
34
35 struct PlayerFovSpec
36 {
37         f32 fov;
38
39         // Whether to multiply the client's FOV or to override it
40         bool is_multiplier;
41
42         // The time to be take to trasition to the new FOV value.
43         // Transition is instantaneous if omitted. Omitted by default.
44         f32 transition_time;
45 };
46
47 struct PlayerControl
48 {
49         PlayerControl() = default;
50
51         PlayerControl(
52                 bool a_up,
53                 bool a_down,
54                 bool a_left,
55                 bool a_right,
56                 bool a_jump,
57                 bool a_aux1,
58                 bool a_sneak,
59                 bool a_zoom,
60                 bool a_LMB,
61                 bool a_RMB,
62                 float a_pitch,
63                 float a_yaw,
64                 float a_sidew_move_joystick_axis,
65                 float a_forw_move_joystick_axis
66         )
67         {
68                 up = a_up;
69                 down = a_down;
70                 left = a_left;
71                 right = a_right;
72                 jump = a_jump;
73                 aux1 = a_aux1;
74                 sneak = a_sneak;
75                 zoom = a_zoom;
76                 LMB = a_LMB;
77                 RMB = a_RMB;
78                 pitch = a_pitch;
79                 yaw = a_yaw;
80                 sidew_move_joystick_axis = a_sidew_move_joystick_axis;
81                 forw_move_joystick_axis = a_forw_move_joystick_axis;
82         }
83         bool up = false;
84         bool down = false;
85         bool left = false;
86         bool right = false;
87         bool jump = false;
88         bool aux1 = false;
89         bool sneak = false;
90         bool zoom = false;
91         bool LMB = false;
92         bool RMB = false;
93         float pitch = 0.0f;
94         float yaw = 0.0f;
95         float sidew_move_joystick_axis = 0.0f;
96         float forw_move_joystick_axis = 0.0f;
97 };
98
99 struct PlayerSettings
100 {
101         bool free_move = false;
102         bool pitch_move = false;
103         bool fast_move = false;
104         bool continuous_forward = false;
105         bool always_fly_fast = false;
106         bool aux1_descends = false;
107         bool noclip = false;
108         bool autojump = false;
109
110         const std::string setting_names[8] = {
111                 "free_move", "pitch_move", "fast_move", "continuous_forward", "always_fly_fast",
112                 "aux1_descends", "noclip", "autojump"
113         };
114         void readGlobalSettings();
115 };
116
117 class Map;
118 struct CollisionInfo;
119 struct HudElement;
120 class Environment;
121
122 class Player
123 {
124 public:
125
126         Player(const char *name, IItemDefManager *idef);
127         virtual ~Player() = 0;
128
129         DISABLE_CLASS_COPY(Player);
130
131         virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
132         {}
133         virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
134                         std::vector<CollisionInfo> *collision_info)
135         {}
136
137         const v3f &getSpeed() const
138         {
139                 return m_speed;
140         }
141
142         void setSpeed(const v3f &speed)
143         {
144                 m_speed = speed;
145         }
146
147         const char *getName() const { return m_name; }
148
149         u32 getFreeHudID()
150         {
151                 size_t size = hud.size();
152                 for (size_t i = 0; i != size; i++) {
153                         if (!hud[i])
154                                 return i;
155                 }
156                 return size;
157         }
158
159         v3f eye_offset_first;
160         v3f eye_offset_third;
161
162         Inventory inventory;
163
164         f32 movement_acceleration_default;
165         f32 movement_acceleration_air;
166         f32 movement_acceleration_fast;
167         f32 movement_speed_walk;
168         f32 movement_speed_crouch;
169         f32 movement_speed_fast;
170         f32 movement_speed_climb;
171         f32 movement_speed_jump;
172         f32 movement_liquid_fluidity;
173         f32 movement_liquid_fluidity_smooth;
174         f32 movement_liquid_sink;
175         f32 movement_gravity;
176
177         v2s32 local_animations[4];
178         float local_animation_speed;
179
180         std::string inventory_formspec;
181         std::string formspec_prepend;
182
183         PlayerControl control;
184         const PlayerControl& getPlayerControl() { return control; }
185         PlayerSettings &getPlayerSettings() { return m_player_settings; }
186         static void settingsChangedCallback(const std::string &name, void *data);
187
188         // Returns non-empty `selected` ItemStack. `hand` is a fallback, if specified
189         ItemStack &getWieldedItem(ItemStack *selected, ItemStack *hand) const;
190         void setWieldIndex(u16 index);
191         u16 getWieldIndex() const { return m_wield_index; }
192
193         void setFov(const PlayerFovSpec &spec)
194         {
195                 m_fov_override_spec = spec;
196         }
197
198         const PlayerFovSpec &getFov() const
199         {
200                 return m_fov_override_spec;
201         }
202
203         u32 keyPressed = 0;
204
205         HudElement* getHud(u32 id);
206         u32         addHud(HudElement* hud);
207         HudElement* removeHud(u32 id);
208         void        clearHud();
209
210         u32 hud_flags;
211         s32 hud_hotbar_itemcount;
212
213 protected:
214         char m_name[PLAYERNAME_SIZE];
215         v3f m_speed;
216         u16 m_wield_index = 0;
217         PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f };
218
219         std::vector<HudElement *> hud;
220 private:
221         // Protect some critical areas
222         // hud for example can be modified by EmergeThread
223         // and ServerThread
224         std::mutex m_mutex;
225         PlayerSettings m_player_settings;
226 };