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, bool is_singleplayer)
160 {
161 m_privs = privs;
162 m_is_singleplayer = is_singleplayer;
163 }
164
165 bool getCollisionBox(aabb3f *toset) const override;
166 bool getSelectionBox(aabb3f *toset) const override;
167 bool collideWithObjects() const override { return true; }
168
169 void finalize(RemotePlayer *player, const std::set<std::string> &privs);
170
172 v3f getEyeOffset() const;
173 float getZoomFOV() const;
174
175 inline SimpleMetadata &getMeta() { return m_meta; }
176
177private:
178 std::string getPropertyPacket();
180 std::string generateUpdatePhysicsOverrideCommand() const;
181
184
185 // Cheat prevention
191 v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
192 float m_nocheat_dig_time = 0.0f;
194 v3f m_max_speed_override = v3f(0.0f, 0.0f, 0.0f);
195
196 // Timers
200
202
203 // Cached privileges for enforcement
204 std::set<std::string> m_privs;
206
208 f32 m_pitch = 0.0f;
209 f32 m_fov = 0.0f;
210 s16 m_wanted_range = 0.0f;
211
212 bool m_camera_inverted = false; // this is not store in the player db
213
215
216public:
217 struct {
218 bool breathing : 1;
219 bool drowning : 1;
220 bool node_damage : 1;
221 } m_flags = {true, true, true};
222
224};
225
227{
228 enum Type : u8
229 {
231 SET_HP_MAX, // internal type to allow distinguishing hp reset and damage (for effects)
236 RESPAWN
237 };
238
240 bool from_mod = false;
242
243 // For PLAYER_PUNCH
244 ServerActiveObject *object = nullptr;
245 // For NODE_DAMAGE
246 std::string node;
248
249 inline bool hasLuaReference() const { return lua_reference >= 0; }
250
251 bool setTypeFromString(const std::string &typestr)
252 {
253 if (typestr == "set_hp")
254 type = SET_HP;
255 else if (typestr == "punch")
257 else if (typestr == "fall")
258 type = FALL;
259 else if (typestr == "node_damage")
261 else if (typestr == "drown")
262 type = DROWNING;
263 else if (typestr == "respawn")
264 type = RESPAWN;
265 else
266 return false;
267
268 return true;
269 }
270
271 std::string getTypeAsString() const
272 {
273 switch (type) {
276 return "set_hp";
278 return "punch";
280 return "fall";
282 return "node_damage";
284 return "drown";
286 return "respawn";
287 default:
288 return "?";
289 }
290 }
291
293
298
300};
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:219
void setBasePosition(v3f position)
Definition player_sao.cpp:334
void disconnected()
Definition player_sao.cpp:594
s16 getWantedRange() const
Definition player_sao.h:93
float getZoomFOV() const
Definition player_sao.cpp:765
v3s16 m_nocheat_dig_pos
Definition player_sao.h:191
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:201
float m_time_from_last_teleport
Definition player_sao.h:189
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:198
s16 m_wanted_range
Definition player_sao.h:210
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:204
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:212
InventoryLocation getInventoryLocation() const override
Definition player_sao.cpp:567
SimpleMetadata m_meta
Definition player_sao.h:214
bool m_physics_override_sent
Definition player_sao.h:223
IntervalLimiter m_node_hurt_interval
Definition player_sao.h:199
v3f getLastGoodPosition() const
Definition player_sao.h:137
IntervalLimiter m_breathing_interval
Definition player_sao.h:197
void setLookPitchAndSend(const float pitch)
Definition player_sao.cpp:442
LagPool m_dig_pool
Definition player_sao.h:186
bool node_damage
Definition player_sao.h:220
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:188
float m_nocheat_dig_time
Definition player_sao.h:192
u16 m_breath
Definition player_sao.h:207
u16 getBreath() const
Definition player_sao.h:110
f32 getFov() const
Definition player_sao.h:91
float m_time_from_last_punch
Definition player_sao.h:190
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
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:208
Inventory * getInventory() const override
Definition player_sao.cpp:562
RemotePlayer * m_player
Definition player_sao.h:182
float m_max_speed_override_time
Definition player_sao.h:193
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:175
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:171
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:194
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:167
f32 m_fov
Definition player_sao.h:209
ActiveObjectType getType() const override
Definition player_sao.h:61
void removingFromEnvironment() override
Definition player_sao.cpp:82
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:183
LagPool m_move_pool
Definition player_sao.h:187
void updatePrivileges(const std::set< std::string > &privs, bool is_singleplayer)
Definition player_sao.h:159
bool m_is_singleplayer
Definition player_sao.h:205
bool breathing
Definition player_sao.h:218
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:227
PlayerHPChangeReason(Type type, ServerActiveObject *object)
Definition player_sao.h:294
ServerActiveObject * object
Definition player_sao.h:244
Type
Definition player_sao.h:229
@ RESPAWN
Definition player_sao.h:236
@ DROWNING
Definition player_sao.h:235
@ FALL
Definition player_sao.h:233
@ SET_HP
Definition player_sao.h:230
@ SET_HP_MAX
Definition player_sao.h:231
@ PLAYER_PUNCH
Definition player_sao.h:232
@ NODE_DAMAGE
Definition player_sao.h:234
bool from_mod
Definition player_sao.h:240
PlayerHPChangeReason(Type type, std::string node, v3s16 node_pos)
Definition player_sao.h:299
std::string getTypeAsString() const
Definition player_sao.h:271
bool setTypeFromString(const std::string &typestr)
Definition player_sao.h:251
std::string node
Definition player_sao.h:246
int lua_reference
Definition player_sao.h:241
PlayerHPChangeReason(Type type)
Definition player_sao.h:292
bool hasLuaReference() const
Definition player_sao.h:249
Type type
Definition player_sao.h:239
v3s16 node_pos
Definition player_sao.h:247
Definition tool.h:46
static std::string p(std::string path)
Definition test_filesys.cpp:53