Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
player.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
8#include "inventory.h"
9#include "constants.h"
10#include "util/basic_macros.h"
11#include "util/string.h"
12#include <mutex>
13#include <functional>
14#include <string>
15
16#define PLAYERNAME_SIZE 20
17
18#define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
19#define PLAYERNAME_ALLOWED_CHARS_USER_EXPL "'a' to 'z', 'A' to 'Z', '0' to '9', '-', '_'"
20
21bool is_valid_player_name(std::string_view name);
22
24{
25 f32 fov;
26
27 // Whether to multiply the client's FOV or to override it
29
30 // The time to be take to trasition to the new FOV value.
31 // Transition is instantaneous if omitted. Omitted by default.
33
34 inline bool operator==(const PlayerFovSpec &other) const {
35 // transition_time is compared here since that could be relevant
36 // when aborting a running transition.
37 return fov == other.fov && is_multiplier == other.is_multiplier &&
39 }
40 inline bool operator!=(const PlayerFovSpec &other) const {
41 return !(*this == other);
42 }
43};
44
46{
47 PlayerControl() = default;
48
50 bool a_up, bool a_down, bool a_left, bool a_right,
51 bool a_jump, bool a_aux1, bool a_sneak,
52 bool a_zoom,
53 bool a_dig, bool a_place,
54 float a_pitch, float a_yaw,
55 float a_movement_speed, float a_movement_direction
56 )
57 {
58 // Encode direction keys into a single value so nobody uses it accidentally
59 // as movement_{speed,direction} is supposed to be the source of truth.
60 direction_keys = (a_up&1) | ((a_down&1) << 1) |
61 ((a_left&1) << 2) | ((a_right&1) << 3);
62 jump = a_jump;
63 aux1 = a_aux1;
64 sneak = a_sneak;
65 zoom = a_zoom;
66 dig = a_dig;
67 place = a_place;
68 pitch = a_pitch;
69 yaw = a_yaw;
70 movement_speed = a_movement_speed;
71 movement_direction = a_movement_direction;
72 }
73
74 // Sets movement_speed and movement_direction according to direction_keys
75 // if direction_keys != 0, otherwise leaves them unchanged to preserve
76 // joystick input.
78
79 // For client use
80 u32 getKeysPressed() const;
81 inline bool isMoving() const { return movement_speed > 0.001f; }
82
83 // For server use
84 void unpackKeysPressed(u32 keypress_bits);
85 v2f getMovement() const;
86
88 bool jump = false;
89 bool aux1 = false;
90 bool sneak = false;
91 bool zoom = false;
92 bool dig = false;
93 bool place = false;
94 // Note: These two are NOT available on the server
95 float pitch = 0.0f;
96 float yaw = 0.0f;
97 float movement_speed = 0.0f;
98 float movement_direction = 0.0f;
99};
100
102{
103 float speed = 1.f;
104 float jump = 1.f;
105 float gravity = 1.f;
106
107 bool sneak = true;
108 bool sneak_glitch = false;
109 // "Temporary" option for old move code
110 bool new_move = true;
111
112 float speed_climb = 1.f;
113 float speed_crouch = 1.f;
114 float liquid_fluidity = 1.f;
116 float liquid_sink = 1.f;
118 float acceleration_air = 1.f;
119 float speed_fast = 1.f;
120 float acceleration_fast = 1.f;
121 float speed_walk = 1.f;
122
123 bool operator==(const PlayerPhysicsOverride &other) const;
124 bool operator!=(const PlayerPhysicsOverride &other) const {
125 return !(*this == other);
126 }
127};
128
129class Map;
130struct HudElement;
131class Environment;
132
134{
135public:
136
137 Player(const std::string &name, IItemDefManager *idef);
138 virtual ~Player() = 0;
139
141
142 // in BS-space
143 inline void setSpeed(v3f speed)
144 {
145 m_speed = speed;
146 }
147
148 // in BS-space
149 v3f getSpeed() const { return m_speed; }
150
151 const std::string& getName() const { return m_name; }
152
154 {
155 size_t size = hud.size();
156 for (size_t i = 0; i != size; i++) {
157 if (!hud[i])
158 return i;
159 }
160 return size;
161 }
162
166
168
181
184
186 std::string formspec_prepend;
187
190
192
193 // Returns non-empty `selected` ItemStack. `hand` is a fallback, if specified
194 ItemStack &getWieldedItem(ItemStack *selected, ItemStack *hand) const;
195 void setWieldIndex(u16 index);
196 u16 getWieldIndex();
197
198 bool setFov(const PlayerFovSpec &spec)
199 {
200 if (m_fov_override_spec == spec)
201 return false;
202 m_fov_override_spec = spec;
203 return true;
204 }
205
206 const PlayerFovSpec &getFov() const
207 {
208 return m_fov_override_spec;
209 }
210
211 HudElement* getHud(u32 id);
212 void hudApply(std::function<void(const std::vector<HudElement*>&)> f);
213 u32 addHud(HudElement* hud);
214 HudElement* removeHud(u32 id);
215 void clearHud();
216
219
220 // Get actual usable number of hotbar items (clamped to size of "main" list)
222
223protected:
224 std::string m_name;
225 v3f m_speed; // velocity; in BS-space
227 PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f };
228
229 std::vector<HudElement *> hud;
230
231private:
232 // Protect some critical areas
233 // hud for example can be modified by EmergeThread
234 // and ServerThread
235 // FIXME: ^ this sounds like nonsense. should be checked.
236 std::mutex m_mutex;
237};
Definition environment.h:34
Definition itemdef.h:129
Definition inventory.h:314
Definition map.h:101
Definition player.h:134
PlayerControl control
Definition player.h:188
u16 getWieldIndex()
Definition player.cpp:82
void hudApply(std::function< void(const std::vector< HudElement * > &)> f)
Definition player.cpp:128
f32 movement_liquid_fluidity
Definition player.h:177
u16 m_wield_index
Definition player.h:226
Player(const std::string &name, IItemDefManager *idef)
Definition player.cpp:24
std::vector< HudElement * > hud
Definition player.h:229
virtual ~Player()=0
Definition player.cpp:71
u32 addHud(HudElement *hud)
Definition player.cpp:104
DISABLE_CLASS_COPY(Player)
std::string inventory_formspec
Definition player.h:185
f32 movement_acceleration_air
Definition player.h:170
f32 movement_speed_crouch
Definition player.h:173
const std::string & getName() const
Definition player.h:151
f32 movement_acceleration_fast
Definition player.h:171
v2f local_animations[4]
Definition player.h:182
ItemStack & getWieldedItem(ItemStack *selected, ItemStack *hand) const
Definition player.cpp:87
std::mutex m_mutex
Definition player.h:236
v3f m_speed
Definition player.h:225
void setWieldIndex(u16 index)
Definition player.cpp:76
void clearHud()
Definition player.cpp:146
u32 getFreeHudID()
Definition player.h:153
s32 hud_hotbar_itemcount
Definition player.h:218
f32 movement_speed_jump
Definition player.h:176
HudElement * getHud(u32 id)
Definition player.cpp:118
bool setFov(const PlayerFovSpec &spec)
Definition player.h:198
u16 getMaxHotbarItemcount()
Definition player.cpp:156
PlayerFovSpec m_fov_override_spec
Definition player.h:227
std::string formspec_prepend
Definition player.h:186
v3f eye_offset_third
Definition player.h:164
v3f eye_offset_first
Definition player.h:163
u32 hud_flags
Definition player.h:217
const PlayerFovSpec & getFov() const
Definition player.h:206
Inventory inventory
Definition player.h:167
f32 movement_speed_climb
Definition player.h:175
HudElement * removeHud(u32 id)
Definition player.cpp:134
PlayerPhysicsOverride physics_override
Definition player.h:191
const PlayerControl & getPlayerControl()
Definition player.h:189
float local_animation_speed
Definition player.h:183
v3f eye_offset_third_front
Definition player.h:165
std::string m_name
Definition player.h:224
f32 movement_speed_fast
Definition player.h:174
f32 movement_acceleration_default
Definition player.h:169
v3f getSpeed() const
Definition player.h:149
f32 movement_liquid_fluidity_smooth
Definition player.h:178
f32 movement_gravity
Definition player.h:180
void setSpeed(v3f speed)
Definition player.h:143
f32 movement_liquid_sink
Definition player.h:179
f32 movement_speed_walk
Definition player.h:172
core::vector2d< f32 > v2f
Definition irr_v2d.h:11
core::vector3df v3f
Definition irr_v3d.h:11
bool is_valid_player_name(std::string_view name)
Definition player.cpp:19
Definition hud.h:84
Definition inventory.h:19
Definition player.h:46
PlayerControl()=default
bool place
Definition player.h:93
void unpackKeysPressed(u32 keypress_bits)
Definition player.cpp:242
void setMovementFromKeys()
Definition player.cpp:162
float yaw
Definition player.h:96
bool dig
Definition player.h:92
bool isMoving() const
Definition player.h:81
float movement_direction
Definition player.h:98
float pitch
Definition player.h:95
bool zoom
Definition player.h:91
bool aux1
Definition player.h:89
u8 direction_keys
Definition player.h:87
bool sneak
Definition player.h:90
float movement_speed
Definition player.h:97
PlayerControl(bool a_up, bool a_down, bool a_left, bool a_right, bool a_jump, bool a_aux1, bool a_sneak, bool a_zoom, bool a_dig, bool a_place, float a_pitch, float a_yaw, float a_movement_speed, float a_movement_direction)
Definition player.h:49
v2f getMovement() const
Definition player.cpp:253
u32 getKeysPressed() const
Definition player.cpp:198
bool jump
Definition player.h:88
Definition player.h:24
bool is_multiplier
Definition player.h:28
bool operator==(const PlayerFovSpec &other) const
Definition player.h:34
f32 transition_time
Definition player.h:32
f32 fov
Definition player.h:25
bool operator!=(const PlayerFovSpec &other) const
Definition player.h:40
Definition player.h:102
float speed_fast
Definition player.h:119
float speed_walk
Definition player.h:121
float liquid_sink
Definition player.h:116
bool new_move
Definition player.h:110
float liquid_fluidity
Definition player.h:114
bool operator!=(const PlayerPhysicsOverride &other) const
Definition player.h:124
bool operator==(const PlayerPhysicsOverride &other) const
Definition player.cpp:269
float acceleration_air
Definition player.h:118
float jump
Definition player.h:104
bool sneak
Definition player.h:107
float acceleration_default
Definition player.h:117
bool sneak_glitch
Definition player.h:108
float liquid_fluidity_smooth
Definition player.h:115
float speed_crouch
Definition player.h:113
float speed
Definition player.h:103
float speed_climb
Definition player.h:112
float acceleration_fast
Definition player.h:120
float gravity
Definition player.h:105