Unify wield item handling (#8677)
[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 PlayerControl
36 {
37         PlayerControl() = default;
38
39         PlayerControl(
40                 bool a_up,
41                 bool a_down,
42                 bool a_left,
43                 bool a_right,
44                 bool a_jump,
45                 bool a_aux1,
46                 bool a_sneak,
47                 bool a_zoom,
48                 bool a_LMB,
49                 bool a_RMB,
50                 float a_pitch,
51                 float a_yaw,
52                 float a_sidew_move_joystick_axis,
53                 float a_forw_move_joystick_axis
54         )
55         {
56                 up = a_up;
57                 down = a_down;
58                 left = a_left;
59                 right = a_right;
60                 jump = a_jump;
61                 aux1 = a_aux1;
62                 sneak = a_sneak;
63                 zoom = a_zoom;
64                 LMB = a_LMB;
65                 RMB = a_RMB;
66                 pitch = a_pitch;
67                 yaw = a_yaw;
68                 sidew_move_joystick_axis = a_sidew_move_joystick_axis;
69                 forw_move_joystick_axis = a_forw_move_joystick_axis;
70         }
71         bool up = false;
72         bool down = false;
73         bool left = false;
74         bool right = false;
75         bool jump = false;
76         bool aux1 = false;
77         bool sneak = false;
78         bool zoom = false;
79         bool LMB = false;
80         bool RMB = false;
81         float pitch = 0.0f;
82         float yaw = 0.0f;
83         float sidew_move_joystick_axis = 0.0f;
84         float forw_move_joystick_axis = 0.0f;
85 };
86
87 struct PlayerSettings
88 {
89         bool free_move = false;
90         bool pitch_move = false;
91         bool fast_move = false;
92         bool continuous_forward = false;
93         bool always_fly_fast = false;
94         bool aux1_descends = false;
95         bool noclip = false;
96         bool autojump = false;
97
98         const std::string setting_names[8] = {
99                 "free_move", "pitch_move", "fast_move", "continuous_forward", "always_fly_fast",
100                 "aux1_descends", "noclip", "autojump"
101         };
102         void readGlobalSettings();
103 };
104
105 class Map;
106 struct CollisionInfo;
107 struct HudElement;
108 class Environment;
109
110 class Player
111 {
112 public:
113
114         Player(const char *name, IItemDefManager *idef);
115         virtual ~Player() = 0;
116
117         DISABLE_CLASS_COPY(Player);
118
119         virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
120         {}
121         virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
122                         std::vector<CollisionInfo> *collision_info)
123         {}
124
125         const v3f &getSpeed() const
126         {
127                 return m_speed;
128         }
129
130         void setSpeed(const v3f &speed)
131         {
132                 m_speed = speed;
133         }
134
135         const char *getName() const { return m_name; }
136
137         u32 getFreeHudID()
138         {
139                 size_t size = hud.size();
140                 for (size_t i = 0; i != size; i++) {
141                         if (!hud[i])
142                                 return i;
143                 }
144                 return size;
145         }
146
147         v3f eye_offset_first;
148         v3f eye_offset_third;
149
150         Inventory inventory;
151
152         f32 movement_acceleration_default;
153         f32 movement_acceleration_air;
154         f32 movement_acceleration_fast;
155         f32 movement_speed_walk;
156         f32 movement_speed_crouch;
157         f32 movement_speed_fast;
158         f32 movement_speed_climb;
159         f32 movement_speed_jump;
160         f32 movement_liquid_fluidity;
161         f32 movement_liquid_fluidity_smooth;
162         f32 movement_liquid_sink;
163         f32 movement_gravity;
164
165         v2s32 local_animations[4];
166         float local_animation_speed;
167
168         std::string inventory_formspec;
169         std::string formspec_prepend;
170
171         PlayerControl control;
172         const PlayerControl& getPlayerControl() { return control; }
173         PlayerSettings &getPlayerSettings() { return m_player_settings; }
174         static void settingsChangedCallback(const std::string &name, void *data);
175
176         // Returns non-empty `selected` ItemStack. `hand` is a fallback, if specified
177         ItemStack &getWieldedItem(ItemStack *selected, ItemStack *hand) const;
178         void setWieldIndex(u16 index);
179         u16 getWieldIndex() const { return m_wield_index; }
180
181         u32 keyPressed = 0;
182
183         HudElement* getHud(u32 id);
184         u32         addHud(HudElement* hud);
185         HudElement* removeHud(u32 id);
186         void        clearHud();
187
188         u32 hud_flags;
189         s32 hud_hotbar_itemcount;
190 protected:
191         char m_name[PLAYERNAME_SIZE];
192         v3f m_speed;
193         u16 m_wield_index = 0;
194
195         std::vector<HudElement *> hud;
196 private:
197         // Protect some critical areas
198         // hud for example can be modified by EmergeThread
199         // and ServerThread
200         std::mutex m_mutex;
201         PlayerSettings m_player_settings;
202 };