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
10 #include "network/networkprotocol.h"
12 #include "util/numeric.h"
15 PlayerSAO needs some internals exposed.
26 void setMax(float new_max
)
40 void empty() { m_pool
= m_max
; }
42 bool grab(float dtime
)
46 if (m_pool
+ dtime
> m_max
)
55 class PlayerSAO
: public UnitSAO
58 PlayerSAO(ServerEnvironment
*env_
, RemotePlayer
*player_
, session_t peer_id_
,
59 bool is_singleplayer
);
61 ActiveObjectType
getType() const override
{ return ACTIVEOBJECT_TYPE_PLAYER
; }
62 ActiveObjectType
getSendType() const override
{ return ACTIVEOBJECT_TYPE_GENERIC
; }
63 std::string
getDescription() override
;
66 Active object <-> environment interface
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
; }
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
; }
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
106 return setHP(hp
, reason
, false);
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);
117 Inventory
*getInventory() const override
;
118 InventoryLocation
getInventoryLocation() 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
;
131 void setPlayer(RemotePlayer
*player
) { m_player
= player
; }
132 RemotePlayer
*getPlayer() { return m_player
; }
133 session_t
getPeerID() const;
137 v3f
getLastGoodPosition() const { return m_last_good_position
; }
138 float resetTimeFromLastPunch()
140 float r
= m_time_from_last_punch
;
141 m_time_from_last_punch
= 0.0;
144 void noCheatDigStart(const v3s16
&p
)
146 m_nocheat_dig_pos
= p
;
147 m_nocheat_dig_time
= 0;
149 v3s16
getNoCheatDigPos() { return m_nocheat_dig_pos
; }
150 float getNoCheatDigTime() { return m_nocheat_dig_time
; }
151 void noCheatDigEnd() { m_nocheat_dig_pos
= v3s16(32767, 32767, 32767); }
152 LagPool
&getDigPool() { return m_dig_pool
; }
153 void setMaxSpeedOverride(const v3f
&vel
);
154 // Returns true if cheated
155 bool checkMovementCheat();
159 void updatePrivileges(const std::set
<std::string
> &privs
)
164 inline void setNewPlayer() { m_is_new_player
= true; }
165 inline bool isNewPlayer() { return m_is_new_player
; }
167 bool getCollisionBox(aabb3f
*toset
) const override
;
168 bool getSelectionBox(aabb3f
*toset
) const override
;
169 bool collideWithObjects() const override
{ return true; }
171 void finalize(RemotePlayer
*player
, const std::set
<std::string
> &privs
);
173 v3f
getEyePosition() const { return m_base_position
+ getEyeOffset(); }
174 v3f
getEyeOffset() const;
175 float getZoomFOV() const;
177 inline SimpleMetadata
&getMeta() { return m_meta
; }
180 std::string
getPropertyPacket();
181 void unlinkPlayerSessionAndSave();
182 std::string
generateUpdatePhysicsOverrideCommand() const;
184 RemotePlayer
*m_player
= nullptr;
185 session_t m_peer_id_initial
= 0; ///< only used to initialize RemotePlayer
190 v3f m_last_good_position
;
191 float m_time_from_last_teleport
= 0.0f
;
192 float m_time_from_last_punch
= 0.0f
;
193 v3s16 m_nocheat_dig_pos
= v3s16(32767, 32767, 32767);
194 float m_nocheat_dig_time
= 0.0f
;
195 float m_max_speed_override_time
= 0.0f
;
196 v3f m_max_speed_override
= v3f(0.0f
, 0.0f
, 0.0f
);
199 IntervalLimiter m_breathing_interval
;
200 IntervalLimiter m_drowning_interval
;
201 IntervalLimiter m_node_hurt_interval
;
203 bool m_position_not_sent
= false;
205 // Cached privileges for enforcement
206 std::set
<std::string
> m_privs
;
207 const bool m_is_singleplayer
;
208 bool m_is_new_player
= false;
210 u16 m_breath
= PLAYER_MAX_BREATH_DEFAULT
;
213 s16 m_wanted_range
= 0.0f
;
215 bool m_camera_inverted
= false; // this is not store in the player db
217 SimpleMetadata m_meta
;
223 bool node_damage
: 1;
224 } m_flags
= {true, true, true};
226 bool m_physics_override_sent
= false;
229 struct PlayerHPChangeReason
234 SET_HP_MAX
, // internal type to allow distinguishing hp reset and damage (for effects)
243 bool from_mod
= false;
244 int lua_reference
= -1;
247 ServerActiveObject
*object
= nullptr;
252 inline bool hasLuaReference() const { return lua_reference
>= 0; }
254 bool setTypeFromString(const std::string
&typestr
)
256 if (typestr
== "set_hp")
258 else if (typestr
== "punch")
260 else if (typestr
== "fall")
262 else if (typestr
== "node_damage")
264 else if (typestr
== "drown")
266 else if (typestr
== "respawn")
274 std::string
getTypeAsString() const
277 case PlayerHPChangeReason::SET_HP
:
278 case PlayerHPChangeReason::SET_HP_MAX
:
280 case PlayerHPChangeReason::PLAYER_PUNCH
:
282 case PlayerHPChangeReason::FALL
:
284 case PlayerHPChangeReason::NODE_DAMAGE
:
285 return "node_damage";
286 case PlayerHPChangeReason::DROWNING
:
288 case PlayerHPChangeReason::RESPAWN
:
295 PlayerHPChangeReason(Type type
) : type(type
) {}
297 PlayerHPChangeReason(Type type
, ServerActiveObject
*object
) :
298 type(type
), object(object
)
302 PlayerHPChangeReason(Type type
, std::string node
, v3s16 node_pos
) : type(type
), node(node
), node_pos(node_pos
) {}