]> git.lizzy.rs Git - minetest.git/blob - src/player.h
Dual wielding
[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 "util/pointedthing.h"
28 #include <list>
29 #include <mutex>
30
31 #define PLAYERNAME_SIZE 20
32
33 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
34 #define PLAYERNAME_ALLOWED_CHARS_USER_EXPL "'a' to 'z', 'A' to 'Z', '0' to '9', '-', '_'"
35
36 struct PlayerFovSpec
37 {
38         f32 fov;
39
40         // Whether to multiply the client's FOV or to override it
41         bool is_multiplier;
42
43         // The time to be take to trasition to the new FOV value.
44         // Transition is instantaneous if omitted. Omitted by default.
45         f32 transition_time;
46 };
47
48 struct PlayerControl
49 {
50         PlayerControl() = default;
51
52         PlayerControl(
53                 bool a_up, bool a_down, bool a_left, bool a_right,
54                 bool a_jump, bool a_aux1, bool a_sneak,
55                 bool a_zoom,
56                 bool a_dig, bool a_place,
57                 float a_pitch, float a_yaw,
58                 float a_movement_speed, float a_movement_direction
59         )
60         {
61                 // Encode direction keys into a single value so nobody uses it accidentally
62                 // as movement_{speed,direction} is supposed to be the source of truth.
63                 direction_keys = (a_up&1) | ((a_down&1) << 1) |
64                         ((a_left&1) << 2) | ((a_right&1) << 3);
65                 jump = a_jump;
66                 aux1 = a_aux1;
67                 sneak = a_sneak;
68                 zoom = a_zoom;
69                 dig = a_dig;
70                 place = a_place;
71                 pitch = a_pitch;
72                 yaw = a_yaw;
73                 movement_speed = a_movement_speed;
74                 movement_direction = a_movement_direction;
75         }
76
77 #ifndef SERVER
78         // For client use
79         u32 getKeysPressed() const;
80         inline bool isMoving() const { return movement_speed > 0.001f; }
81 #endif
82
83         // For server use
84         void unpackKeysPressed(u32 keypress_bits);
85
86         u8 direction_keys = 0;
87         bool jump = false;
88         bool aux1 = false;
89         bool sneak = false;
90         bool zoom = false;
91         bool dig = false;
92         bool place = false;
93         // Note: These four are NOT available on the server
94         float pitch = 0.0f;
95         float yaw = 0.0f;
96         float movement_speed = 0.0f;
97         float movement_direction = 0.0f;
98 };
99
100 struct PlayerPhysicsOverride
101 {
102         float speed = 1.f;
103         float jump = 1.f;
104         float gravity = 1.f;
105
106         bool sneak = true;
107         bool sneak_glitch = false;
108         // "Temporary" option for old move code
109         bool new_move = true;
110 };
111
112 struct PlayerSettings
113 {
114         bool free_move = false;
115         bool pitch_move = false;
116         bool fast_move = false;
117         bool continuous_forward = false;
118         bool always_fly_fast = false;
119         bool aux1_descends = false;
120         bool noclip = false;
121         bool autojump = false;
122
123         const std::string setting_names[8] = {
124                 "free_move", "pitch_move", "fast_move", "continuous_forward", "always_fly_fast",
125                 "aux1_descends", "noclip", "autojump"
126         };
127         void readGlobalSettings();
128 };
129
130 class Map;
131 struct CollisionInfo;
132 struct HudElement;
133 class Environment;
134
135 class Player
136 {
137 public:
138
139         Player(const char *name, IItemDefManager *idef);
140         virtual ~Player() = 0;
141
142         DISABLE_CLASS_COPY(Player);
143
144         virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
145         {}
146         virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
147                         std::vector<CollisionInfo> *collision_info)
148         {}
149
150         v3f getSpeed() const
151         {
152                 return m_speed;
153         }
154
155         void setSpeed(v3f speed)
156         {
157                 m_speed = speed;
158         }
159
160         const char *getName() const { return m_name; }
161
162         u32 getFreeHudID()
163         {
164                 size_t size = hud.size();
165                 for (size_t i = 0; i != size; i++) {
166                         if (!hud[i])
167                                 return i;
168                 }
169                 return size;
170         }
171
172         v3f eye_offset_first;
173         v3f eye_offset_third;
174
175         Inventory inventory;
176
177         f32 movement_acceleration_default;
178         f32 movement_acceleration_air;
179         f32 movement_acceleration_fast;
180         f32 movement_speed_walk;
181         f32 movement_speed_crouch;
182         f32 movement_speed_fast;
183         f32 movement_speed_climb;
184         f32 movement_speed_jump;
185         f32 movement_liquid_fluidity;
186         f32 movement_liquid_fluidity_smooth;
187         f32 movement_liquid_sink;
188         f32 movement_gravity;
189
190         v2s32 local_animations[4];
191         float local_animation_speed;
192
193         std::string inventory_formspec;
194         std::string formspec_prepend;
195
196         PlayerControl control;
197         const PlayerControl& getPlayerControl() { return control; }
198         PlayerPhysicsOverride physics_override;
199         PlayerSettings &getPlayerSettings() { return m_player_settings; }
200         static void settingsChangedCallback(const std::string &name, void *data);
201
202         // Returns non-empty `selected` ItemStack. `hand` is a fallback, if specified
203         ItemStack &getWieldedItem(ItemStack *selected, ItemStack *hand) const;
204
205         // item currently in secondary hand is returned in `offhand`
206         // item to use for place / secondary_use (either main or offhand) is (optionally) returned in `place`
207         // return value: whether to use main or offhand for placing
208         bool getOffhandWieldedItem(ItemStack *offhand, ItemStack *place,
209                         IItemDefManager *idef, const PointedThing &pointed) const;
210
211         void setWieldIndex(u16 index);
212         u16 getWieldIndex() const { return m_wield_index; }
213
214         void setFov(const PlayerFovSpec &spec)
215         {
216                 m_fov_override_spec = spec;
217         }
218
219         const PlayerFovSpec &getFov() const
220         {
221                 return m_fov_override_spec;
222         }
223
224         HudElement* getHud(u32 id);
225         u32         addHud(HudElement* hud);
226         HudElement* removeHud(u32 id);
227         void        clearHud();
228
229         u32 hud_flags;
230         s32 hud_hotbar_itemcount;
231
232 protected:
233         char m_name[PLAYERNAME_SIZE];
234         v3f m_speed;
235         u16 m_wield_index = 0;
236         PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f };
237
238         std::vector<HudElement *> hud;
239 private:
240         // Protect some critical areas
241         // hud for example can be modified by EmergeThread
242         // and ServerThread
243         std::mutex m_mutex;
244         PlayerSettings m_player_settings;
245 };