Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
player_sao.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// Copyright (C) 2013-2020 Minetest core developers & community
5
6#pragma once
7
8#include "constants.h"
9#include "metadata.h"
11#include "unit_sao.h"
12#include "util/numeric.h"
13
14/*
15 PlayerSAO needs some internals exposed.
16*/
17
19{
20 float m_pool = 15.0f;
21 float m_max = 15.0f;
22
23public:
24 LagPool() = default;
25
26 void setMax(float new_max)
27 {
28 m_max = new_max;
29 if (m_pool > new_max)
30 m_pool = new_max;
31 }
32
33 void add(float dtime)
34 {
35 m_pool -= dtime;
36 if (m_pool < 0)
37 m_pool = 0;
38 }
39
40 void empty() { m_pool = m_max; }
41
42 bool grab(float dtime)
43 {
44 if (dtime <= 0)
45 return true;
46 if (m_pool + dtime > m_max)
47 return false;
48 m_pool += dtime;
49 return true;
50 }
51};
52
53class RemotePlayer;
54
55class PlayerSAO : public UnitSAO
56{
57public:
58 PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t peer_id_,
59 bool is_singleplayer);
60
63 std::string getDescription() override;
64
65 /*
66 Active object <-> environment interface
67 */
68
69 void addedToEnvironment(u32 dtime_s) override;
70 void removingFromEnvironment() override;
71 bool isStaticAllowed() const override { return false; }
72 bool shouldUnload() const override { return false; }
73 std::string getClientInitializationData(u16 protocol_version) override;
74 void getStaticData(std::string *result) const override;
75 void step(float dtime, bool send_recommended) override;
76 void setBasePosition(v3f position);
77 void setPos(const v3f &pos) override;
78 void addPos(const v3f &added_pos) override;
79 void moveTo(v3f pos, bool continuous) override;
80 void setPlayerYaw(const float yaw);
81 // Data should not be sent at player initialization
82 void setPlayerYawAndSend(const float yaw);
83 void setLookPitch(const float pitch);
84 // Data should not be sent at player initialization
85 void setLookPitchAndSend(const float pitch);
86 f32 getLookPitch() const { return m_pitch; }
87 f32 getRadLookPitch() const { return m_pitch * core::DEGTORAD; }
88 // Deprecated
89 f32 getRadLookPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
90 void setFov(const float pitch);
91 f32 getFov() const { return m_fov; }
92 void setWantedRange(const s16 range);
93 s16 getWantedRange() const { return m_wanted_range; }
94 void setCameraInverted(bool camera_inverted) { m_camera_inverted = camera_inverted; }
95 bool getCameraInverted() const { return m_camera_inverted; }
96
97 /*
98 Interaction interface
99 */
100
101 u32 punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher,
102 float time_from_last_punch, u16 initial_wear = 0) override;
103 void rightClick(ServerActiveObject *clicker) override;
104 void setHP(s32 hp, const PlayerHPChangeReason &reason) override
105 {
106 return setHP(hp, reason, false);
107 }
108 void setHP(s32 hp, const PlayerHPChangeReason &reason, bool from_client);
109 void setHPRaw(u16 hp) { m_hp = hp; }
110 u16 getBreath() const { return m_breath; }
111 void setBreath(const u16 breath, bool send = true);
112 void respawn();
113
114 /*
115 Inventory interface
116 */
117 Inventory *getInventory() const override;
119 void setInventoryModified() override {}
120 std::string getWieldList() const override { return "main"; }
121 u16 getWieldIndex() const override;
122 ItemStack getWieldedItem(ItemStack *selected, ItemStack *hand = nullptr) const override;
123 bool setWieldedItem(const ItemStack &item) override;
124
125 /*
126 PlayerSAO-specific
127 */
128
129 void disconnected();
130
131 void setPlayer(RemotePlayer *player) { m_player = player; }
133 session_t getPeerID() const;
134
135 // Cheat prevention
136
139 {
140 float r = m_time_from_last_punch;
142 return r;
143 }
145 {
148 }
151 void noCheatDigEnd() { m_nocheat_dig_pos = v3s16(32767, 32767, 32767); }
153 void setMaxSpeedOverride(const v3f &vel);
154 // Returns true if cheated
155 bool checkMovementCheat();
156
157 // Other
158
159 void updatePrivileges(const std::set<std::string> &privs)
160 {
161 m_privs = privs;
162 }
163
164 inline void setNewPlayer() { m_is_new_player = true; }
165 inline bool isNewPlayer() { return m_is_new_player; }
166
167 bool getCollisionBox(aabb3f *toset) const override;
168 bool getSelectionBox(aabb3f *toset) const override;
169 bool collideWithObjects() const override { return true; }
170
171 void finalize(RemotePlayer *player, const std::set<std::string> &privs);
172
174 v3f getEyeOffset() const;
175 float getZoomFOV() const;
176
177 inline SimpleMetadata &getMeta() { return m_meta; }
178
179private:
180 std::string getPropertyPacket();
182 std::string generateUpdatePhysicsOverrideCommand() const;
183
186
187 // Cheat prevention
193 v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
194 float m_nocheat_dig_time = 0.0f;
196 v3f m_max_speed_override = v3f(0.0f, 0.0f, 0.0f);
197
198 // Timers
202
204
205 // Cached privileges for enforcement
206 std::set<std::string> m_privs;
208 bool m_is_new_player = false;
209
211 f32 m_pitch = 0.0f;
212 f32 m_fov = 0.0f;
213 s16 m_wanted_range = 0.0f;
214
215 bool m_camera_inverted = false; // this is not store in the player db
216
218
219public:
220 struct {
221 bool breathing : 1;
222 bool drowning : 1;
223 bool node_damage : 1;
224 } m_flags = {true, true, true};
225
227};
228
230{
231 enum Type : u8
232 {
234 SET_HP_MAX, // internal type to allow distinguishing hp reset and damage (for effects)
239 RESPAWN
240 };
241
243 bool from_mod = false;
245
246 // For PLAYER_PUNCH
247 ServerActiveObject *object = nullptr;
248 // For NODE_DAMAGE
249 std::string node;
251
252 inline bool hasLuaReference() const { return lua_reference >= 0; }
253
254 bool setTypeFromString(const std::string &typestr)
255 {
256 if (typestr == "set_hp")
257 type = SET_HP;
258 else if (typestr == "punch")
260 else if (typestr == "fall")
261 type = FALL;
262 else if (typestr == "node_damage")
264 else if (typestr == "drown")
265 type = DROWNING;
266 else if (typestr == "respawn")
267 type = RESPAWN;
268 else
269 return false;
270
271 return true;
272 }
273
274 std::string getTypeAsString() const
275 {
276 switch (type) {
279 return "set_hp";
281 return "punch";
283 return "fall";
285 return "node_damage";
287 return "drown";
289 return "respawn";
290 default:
291 return "?";
292 }
293 }
294
296
301
303};
ActiveObjectType
Definition activeobject.h:14
@ ACTIVEOBJECT_TYPE_PLAYER
Definition activeobject.h:26
@ ACTIVEOBJECT_TYPE_GENERIC
Definition activeobject.h:28
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:191
Definition numeric.h:347
Definition inventory.h:314
Definition player_sao.h:19
LagPool()=default
float m_max
Definition player_sao.h:21
bool grab(float dtime)
Definition player_sao.h:42
void add(float dtime)
Definition player_sao.h:33
void empty()
Definition player_sao.h:40
float m_pool
Definition player_sao.h:20
void setMax(float new_max)
Definition player_sao.h:26
Definition player_sao.h:56
void setPlayerYawAndSend(const float yaw)
Definition player_sao.cpp:428
std::string generateUpdatePhysicsOverrideCommand() const
Definition player_sao.cpp:301
std::string getClientInitializationData(u16 protocol_version) override
Definition player_sao.cpp:95
void rightClick(ServerActiveObject *clicker) override
Definition player_sao.cpp:500
bool drowning
Definition player_sao.h:222
void setBasePosition(v3f position)
Definition player_sao.cpp:334
void disconnected()
Definition player_sao.cpp:594
s16 getWantedRange() const
Definition player_sao.h:93
bool m_is_new_player
Definition player_sao.h:208
float getZoomFOV() const
Definition player_sao.cpp:765
v3s16 m_nocheat_dig_pos
Definition player_sao.h:193
u16 getWieldIndex() const override
Definition player_sao.cpp:574
std::string getWieldList() const override
Definition player_sao.h:120
void noCheatDigEnd()
Definition player_sao.h:151
void unlinkPlayerSessionAndSave()
Definition player_sao.cpp:606
bool m_position_not_sent
Definition player_sao.h:203
float m_time_from_last_teleport
Definition player_sao.h:191
void setPos(const v3f &pos) override
Definition player_sao.cpp:348
void moveTo(v3f pos, bool continuous) override
Definition player_sao.cpp:389
IntervalLimiter m_drowning_interval
Definition player_sao.h:200
s16 m_wanted_range
Definition player_sao.h:213
void noCheatDigStart(const v3s16 &p)
Definition player_sao.h:144
void setBreath(const u16 breath, bool send=true)
Definition player_sao.cpp:536
std::set< std::string > m_privs
Definition player_sao.h:206
const bool m_is_singleplayer
Definition player_sao.h:207
void setInventoryModified() override
Definition player_sao.h:119
LagPool & getDigPool()
Definition player_sao.h:152
void step(float dtime, bool send_recommended) override
Definition player_sao.cpp:142
bool m_camera_inverted
Definition player_sao.h:215
InventoryLocation getInventoryLocation() const override
Definition player_sao.cpp:567
SimpleMetadata m_meta
Definition player_sao.h:217
bool m_physics_override_sent
Definition player_sao.h:226
IntervalLimiter m_node_hurt_interval
Definition player_sao.h:201
v3f getLastGoodPosition() const
Definition player_sao.h:137
IntervalLimiter m_breathing_interval
Definition player_sao.h:199
void setLookPitchAndSend(const float pitch)
Definition player_sao.cpp:442
LagPool m_dig_pool
Definition player_sao.h:188
bool node_damage
Definition player_sao.h:223
void setPlayerYaw(const float yaw)
Definition player_sao.cpp:402
bool getCameraInverted() const
Definition player_sao.h:95
v3f m_last_good_position
Definition player_sao.h:190
float m_nocheat_dig_time
Definition player_sao.h:194
u16 m_breath
Definition player_sao.h:210
u16 getBreath() const
Definition player_sao.h:110
void setNewPlayer()
Definition player_sao.h:164
f32 getFov() const
Definition player_sao.h:91
float m_time_from_last_punch
Definition player_sao.h:192
PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t peer_id_, bool is_singleplayer)
Definition player_sao.cpp:13
u32 punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher, float time_from_last_punch, u16 initial_wear=0) override
Definition player_sao.cpp:448
void finalize(RemotePlayer *player, const std::set< std::string > &privs)
Definition player_sao.cpp:53
bool checkMovementCheat()
Definition player_sao.cpp:632
bool isNewPlayer()
Definition player_sao.h:165
v3f getEyeOffset() const
Definition player_sao.cpp:60
f32 getLookPitch() const
Definition player_sao.h:86
void setCameraInverted(bool camera_inverted)
Definition player_sao.h:94
std::string getPropertyPacket()
Definition player_sao.cpp:613
f32 getRadLookPitch() const
Definition player_sao.h:87
void setHP(s32 hp, const PlayerHPChangeReason &reason) override
Definition player_sao.h:104
void setMaxSpeedOverride(const v3f &vel)
Definition player_sao.cpp:619
void setFov(const float pitch)
Definition player_sao.cpp:412
void respawn()
Definition player_sao.cpp:547
f32 m_pitch
Definition player_sao.h:211
Inventory * getInventory() const override
Definition player_sao.cpp:562
RemotePlayer * m_player
Definition player_sao.h:184
float m_max_speed_override_time
Definition player_sao.h:195
session_t getPeerID() const
Definition player_sao.cpp:600
std::string getDescription() override
Definition player_sao.cpp:65
ItemStack getWieldedItem(ItemStack *selected, ItemStack *hand=nullptr) const override
Definition player_sao.cpp:579
bool getSelectionBox(aabb3f *toset) const override
Definition player_sao.cpp:753
void getStaticData(std::string *result) const override
Definition player_sao.cpp:137
bool setWieldedItem(const ItemStack &item) override
Definition player_sao.cpp:584
float resetTimeFromLastPunch()
Definition player_sao.h:138
SimpleMetadata & getMeta()
Definition player_sao.h:177
void setWantedRange(const s16 range)
Definition player_sao.cpp:420
void setHPRaw(u16 hp)
Definition player_sao.h:109
f32 getRadLookPitchDep() const
Definition player_sao.h:89
ActiveObjectType getSendType() const override
Definition player_sao.h:62
float getNoCheatDigTime()
Definition player_sao.h:150
v3s16 getNoCheatDigPos()
Definition player_sao.h:149
v3f getEyePosition() const
Definition player_sao.h:173
RemotePlayer * getPlayer()
Definition player_sao.h:132
bool getCollisionBox(aabb3f *toset) const override
Definition player_sao.cpp:742
bool isStaticAllowed() const override
Definition player_sao.h:71
v3f m_max_speed_override
Definition player_sao.h:196
void setLookPitch(const float pitch)
Definition player_sao.cpp:434
void setPlayer(RemotePlayer *player)
Definition player_sao.h:131
bool shouldUnload() const override
Definition player_sao.h:72
void addPos(const v3f &added_pos) override
Definition player_sao.cpp:365
bool collideWithObjects() const override
Definition player_sao.h:169
f32 m_fov
Definition player_sao.h:212
ActiveObjectType getType() const override
Definition player_sao.h:61
void removingFromEnvironment() override
Definition player_sao.cpp:82
void updatePrivileges(const std::set< std::string > &privs)
Definition player_sao.h:159
void addedToEnvironment(u32 dtime_s) override
Definition player_sao.cpp:71
session_t m_peer_id_initial
only used to initialize RemotePlayer
Definition player_sao.h:185
LagPool m_move_pool
Definition player_sao.h:189
bool breathing
Definition player_sao.h:221
struct PlayerSAO::@29 m_flags
Definition remoteplayer.h:26
Definition serveractiveobject.h:41
v3f m_base_position
Definition serveractiveobject.h:248
Definition serverenvironment.h:219
Definition metadata.h:63
Definition unit_sao.h:14
u16 m_hp
Definition unit_sao.h:96
#define PLAYER_MAX_BREATH_DEFAULT
Definition constants.h:83
core::aabbox3d< f32 > aabb3f
Definition irr_aabb3d.h:11
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
core::vector3df v3f
Definition irr_v3d.h:11
u16 session_t
Definition networkprotocol.h:22
Definition inventorymanager.h:18
Definition inventory.h:19
Definition player_sao.h:230
PlayerHPChangeReason(Type type, ServerActiveObject *object)
Definition player_sao.h:297
ServerActiveObject * object
Definition player_sao.h:247
Type
Definition player_sao.h:232
@ RESPAWN
Definition player_sao.h:239
@ DROWNING
Definition player_sao.h:238
@ FALL
Definition player_sao.h:236
@ SET_HP
Definition player_sao.h:233
@ SET_HP_MAX
Definition player_sao.h:234
@ PLAYER_PUNCH
Definition player_sao.h:235
@ NODE_DAMAGE
Definition player_sao.h:237
bool from_mod
Definition player_sao.h:243
PlayerHPChangeReason(Type type, std::string node, v3s16 node_pos)
Definition player_sao.h:302
std::string getTypeAsString() const
Definition player_sao.h:274
bool setTypeFromString(const std::string &typestr)
Definition player_sao.h:254
std::string node
Definition player_sao.h:249
int lua_reference
Definition player_sao.h:244
PlayerHPChangeReason(Type type)
Definition player_sao.h:295
bool hasLuaReference() const
Definition player_sao.h:252
Type type
Definition player_sao.h:242
v3s16 node_pos
Definition player_sao.h:250
Definition tool.h:53
static std::string p(std::string path)
Definition test_filesys.cpp:55