Luanti 5.10.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 CollisionInfo;
131struct HudElement;
132class Environment;
133
135{
136public:
137
138 Player(const std::string &name, IItemDefManager *idef);
139 virtual ~Player() = 0;
140
142
143 virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
144 {}
145 virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
146 std::vector<CollisionInfo> *collision_info)
147 {}
148
149 // in BS-space
150 inline void setSpeed(v3f speed)
151 {
152 m_speed = speed;
153 }
154
155 // in BS-space
156 v3f getSpeed() const { return m_speed; }
157
158 const std::string& getName() const { return m_name; }
159
161 {
162 size_t size = hud.size();
163 for (size_t i = 0; i != size; i++) {
164 if (!hud[i])
165 return i;
166 }
167 return size;
168 }
169
173
175
188
191
193 std::string formspec_prepend;
194
197
199
200 // Returns non-empty `selected` ItemStack. `hand` is a fallback, if specified
201 ItemStack &getWieldedItem(ItemStack *selected, ItemStack *hand) const;
202 void setWieldIndex(u16 index);
203 u16 getWieldIndex();
204
205 bool setFov(const PlayerFovSpec &spec)
206 {
207 if (m_fov_override_spec == spec)
208 return false;
209 m_fov_override_spec = spec;
210 return true;
211 }
212
213 const PlayerFovSpec &getFov() const
214 {
215 return m_fov_override_spec;
216 }
217
218 HudElement* getHud(u32 id);
219 void hudApply(std::function<void(const std::vector<HudElement*>&)> f);
220 u32 addHud(HudElement* hud);
221 HudElement* removeHud(u32 id);
222 void clearHud();
223
226
227 // Get actual usable number of hotbar items (clamped to size of "main" list)
229
230protected:
231 std::string m_name;
232 v3f m_speed; // velocity; in BS-space
234 PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f };
235
236 std::vector<HudElement *> hud;
237
238private:
239 // Protect some critical areas
240 // hud for example can be modified by EmergeThread
241 // and ServerThread
242 // FIXME: ^ this sounds like nonsense. should be checked.
243 std::mutex m_mutex;
244};
Definition environment.h:34
Definition itemdef.h:129
Definition inventory.h:314
Definition map.h:101
Definition player.h:135
PlayerControl control
Definition player.h:195
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:184
u16 m_wield_index
Definition player.h:233
Player(const std::string &name, IItemDefManager *idef)
Definition player.cpp:24
std::vector< HudElement * > hud
Definition player.h:236
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:192
f32 movement_acceleration_air
Definition player.h:177
f32 movement_speed_crouch
Definition player.h:180
const std::string & getName() const
Definition player.h:158
f32 movement_acceleration_fast
Definition player.h:178
v2f local_animations[4]
Definition player.h:189
virtual void move(f32 dtime, Environment *env, f32 pos_max_d, std::vector< CollisionInfo > *collision_info)
Definition player.h:145
ItemStack & getWieldedItem(ItemStack *selected, ItemStack *hand) const
Definition player.cpp:87
std::mutex m_mutex
Definition player.h:243
v3f m_speed
Definition player.h:232
void setWieldIndex(u16 index)
Definition player.cpp:76
void clearHud()
Definition player.cpp:146
u32 getFreeHudID()
Definition player.h:160
virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
Definition player.h:143
s32 hud_hotbar_itemcount
Definition player.h:225
f32 movement_speed_jump
Definition player.h:183
HudElement * getHud(u32 id)
Definition player.cpp:118
bool setFov(const PlayerFovSpec &spec)
Definition player.h:205
u16 getMaxHotbarItemcount()
Definition player.cpp:156
PlayerFovSpec m_fov_override_spec
Definition player.h:234
std::string formspec_prepend
Definition player.h:193
v3f eye_offset_third
Definition player.h:171
v3f eye_offset_first
Definition player.h:170
u32 hud_flags
Definition player.h:224
const PlayerFovSpec & getFov() const
Definition player.h:213
Inventory inventory
Definition player.h:174
f32 movement_speed_climb
Definition player.h:182
HudElement * removeHud(u32 id)
Definition player.cpp:134
PlayerPhysicsOverride physics_override
Definition player.h:198
const PlayerControl & getPlayerControl()
Definition player.h:196
float local_animation_speed
Definition player.h:190
v3f eye_offset_third_front
Definition player.h:172
std::string m_name
Definition player.h:231
f32 movement_speed_fast
Definition player.h:181
f32 movement_acceleration_default
Definition player.h:176
v3f getSpeed() const
Definition player.h:156
f32 movement_liquid_fluidity_smooth
Definition player.h:185
f32 movement_gravity
Definition player.h:187
void setSpeed(v3f speed)
Definition player.h:150
f32 movement_liquid_sink
Definition player.h:186
f32 movement_speed_walk
Definition player.h:179
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 collision.h:30
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