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