]> git.lizzy.rs Git - dragonfireclient.git/blob - src/player.h
Merge branch 'master' of https://github.com/minetest/minetest
[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 #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, bool a_down, bool a_left, bool a_right,
53                 bool a_jump, bool a_aux1, bool a_sneak,
54                 bool a_zoom,
55                 bool a_dig, bool a_place,
56                 float a_pitch, float a_yaw,
57                 float a_movement_speed, float a_movement_direction
58         )
59         {
60                 // Encode direction keys into a single value so nobody uses it accidentally
61                 // as movement_{speed,direction} is supposed to be the source of truth.
62                 direction_keys = (a_up&1) | ((a_down&1) << 1) |
63                         ((a_left&1) << 2) | ((a_right&1) << 3);
64                 jump = a_jump;
65                 aux1 = a_aux1;
66                 sneak = a_sneak;
67                 zoom = a_zoom;
68                 dig = a_dig;
69                 place = a_place;
70                 pitch = a_pitch;
71                 yaw = a_yaw;
72                 movement_speed = a_movement_speed;
73                 movement_direction = a_movement_direction;
74         }
75
76 #ifndef SERVER
77         // For client use
78         u32 getKeysPressed() const;
79         inline bool isMoving() const { return movement_speed > 0.001f; }
80 #endif
81
82         // For server use
83         void unpackKeysPressed(u32 keypress_bits);
84
85         u8 direction_keys = 0;
86         bool jump = false;
87         bool aux1 = false;
88         bool sneak = false;
89         bool zoom = false;
90         bool dig = false;
91         bool place = false;
92         // Note: These four are NOT available on the server
93         float pitch = 0.0f;
94         float yaw = 0.0f;
95         float movement_speed = 0.0f;
96         float movement_direction = 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 freecam = false;
105         bool continuous_forward = false;
106         bool always_fly_fast = false;
107         bool aux1_descends = false;
108         bool noclip = false;
109         bool autojump = false;
110
111         const std::string setting_names[9] = {
112                 "free_move", "pitch_move", "fast_move", "freecam", "continuous_forward", "always_fly_fast",
113                 "aux1_descends", "noclip", "autojump"
114         };
115         void readGlobalSettings();
116 };
117
118 class Map;
119 struct CollisionInfo;
120 struct HudElement;
121 class Environment;
122
123 class Player
124 {
125 public:
126
127         Player(const char *name, IItemDefManager *idef);
128         virtual ~Player() = 0;
129
130         DISABLE_CLASS_COPY(Player);
131
132         virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
133         {}
134         virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
135                         std::vector<CollisionInfo> *collision_info)
136         {}
137
138         const v3f &getSpeed() const
139         {
140                 return m_speed;
141         }
142
143         void setSpeed(const v3f &speed)
144         {
145                 m_speed = speed;
146         }
147
148         const char *getName() const { return m_name; }
149
150         u32 getFreeHudID()
151         {
152                 size_t size = hud.size();
153                 for (size_t i = 0; i != size; i++) {
154                         if (!hud[i])
155                                 return i;
156                 }
157                 return size;
158         }
159
160         v3f eye_offset_first;
161         v3f eye_offset_third;
162
163         Inventory inventory;
164
165         f32 movement_acceleration_default;
166         f32 movement_acceleration_air;
167         f32 movement_acceleration_fast;
168         f32 movement_speed_walk;
169         f32 movement_speed_crouch;
170         f32 movement_speed_fast;
171         f32 movement_speed_climb;
172         f32 movement_speed_jump;
173         f32 movement_liquid_fluidity;
174         f32 movement_liquid_fluidity_smooth;
175         f32 movement_liquid_sink;
176         f32 movement_gravity;
177
178         v2s32 local_animations[4];
179         float local_animation_speed;
180
181         std::string inventory_formspec;
182         std::string formspec_prepend;
183
184         PlayerControl control;
185         const PlayerControl& getPlayerControl() { return control; }
186         PlayerSettings &getPlayerSettings() { return m_player_settings; }
187         static void settingsChangedCallback(const std::string &name, void *data);
188
189         // Returns non-empty `selected` ItemStack. `hand` is a fallback, if specified
190         ItemStack &getWieldedItem(ItemStack *selected, ItemStack *hand) const;
191         void setWieldIndex(u16 index);
192         u16 getWieldIndex() const { return m_wield_index; }
193
194         void setFov(const PlayerFovSpec &spec)
195         {
196                 m_fov_override_spec = spec;
197         }
198
199         const PlayerFovSpec &getFov() const
200         {
201                 return m_fov_override_spec;
202         }
203
204         HudElement* getHud(u32 id);
205         u32         addHud(HudElement* hud);
206         HudElement* removeHud(u32 id);
207         void        clearHud();
208
209         u32 hud_flags;
210         s32 hud_hotbar_itemcount;
211
212 protected:
213         char m_name[PLAYERNAME_SIZE];
214         v3f m_speed;
215         u16 m_wield_index = 0;
216         PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f };
217
218         std::vector<HudElement *> hud;
219 private:
220         // Protect some critical areas
221         // hud for example can be modified by EmergeThread
222         // and ServerThread
223         std::mutex m_mutex;
224         PlayerSettings m_player_settings;
225 };