3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013-2020 Minetest core developers & community
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "constants.h"
24 #include "network/networkprotocol.h"
26 #include "util/numeric.h"
29 PlayerSAO needs some internals exposed.
40 void setMax(float new_max)
54 void empty() { m_pool = m_max; }
56 bool grab(float dtime)
60 if (m_pool + dtime > m_max)
69 class PlayerSAO : public UnitSAO
72 PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t peer_id_,
73 bool is_singleplayer);
75 ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_PLAYER; }
76 ActiveObjectType getSendType() const { return ACTIVEOBJECT_TYPE_GENERIC; }
77 std::string getDescription();
80 Active object <-> environment interface
83 void addedToEnvironment(u32 dtime_s);
84 void removingFromEnvironment();
85 bool isStaticAllowed() const { return false; }
86 bool shouldUnload() const { return false; }
87 std::string getClientInitializationData(u16 protocol_version);
88 void getStaticData(std::string *result) const;
89 void step(float dtime, bool send_recommended);
90 void setBasePosition(const v3f &position);
91 void setPos(const v3f &pos);
92 void moveTo(v3f pos, bool continuous);
93 void setPlayerYaw(const float yaw);
94 // Data should not be sent at player initialization
95 void setPlayerYawAndSend(const float yaw);
96 void setLookPitch(const float pitch);
97 // Data should not be sent at player initialization
98 void setLookPitchAndSend(const float pitch);
99 f32 getLookPitch() const { return m_pitch; }
100 f32 getRadLookPitch() const { return m_pitch * core::DEGTORAD; }
102 f32 getRadLookPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
103 void setFov(const float pitch);
104 f32 getFov() const { return m_fov; }
105 void setWantedRange(const s16 range);
106 s16 getWantedRange() const { return m_wanted_range; }
109 Interaction interface
112 u16 punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher,
113 float time_from_last_punch);
114 void rightClick(ServerActiveObject *clicker);
115 void setHP(s32 hp, const PlayerHPChangeReason &reason) override
117 return setHP(hp, reason, true);
119 void setHP(s32 hp, const PlayerHPChangeReason &reason, bool send);
120 void setHPRaw(u16 hp) { m_hp = hp; }
121 u16 getBreath() const { return m_breath; }
122 void setBreath(const u16 breath, bool send = true);
127 Inventory *getInventory() const;
128 InventoryLocation getInventoryLocation() const;
129 void setInventoryModified() {}
130 std::string getWieldList() const { return "main"; }
131 u16 getWieldIndex() const;
132 ItemStack getWieldedItem(ItemStack *selected, ItemStack *hand = nullptr) const;
133 bool setWieldedItem(const ItemStack &item);
141 RemotePlayer *getPlayer() { return m_player; }
142 session_t getPeerID() const { return m_peer_id; }
146 v3f getLastGoodPosition() const { return m_last_good_position; }
147 float resetTimeFromLastPunch()
149 float r = m_time_from_last_punch;
150 m_time_from_last_punch = 0.0;
153 void noCheatDigStart(const v3s16 &p)
155 m_nocheat_dig_pos = p;
156 m_nocheat_dig_time = 0;
158 v3s16 getNoCheatDigPos() { return m_nocheat_dig_pos; }
159 float getNoCheatDigTime() { return m_nocheat_dig_time; }
160 void noCheatDigEnd() { m_nocheat_dig_pos = v3s16(32767, 32767, 32767); }
161 LagPool &getDigPool() { return m_dig_pool; }
162 void setMaxSpeedOverride(const v3f &vel);
163 // Returns true if cheated
164 bool checkMovementCheat();
168 void updatePrivileges(const std::set<std::string> &privs, bool is_singleplayer)
171 m_is_singleplayer = is_singleplayer;
174 bool getCollisionBox(aabb3f *toset) const;
175 bool getSelectionBox(aabb3f *toset) const;
176 bool collideWithObjects() const { return true; }
178 void finalize(RemotePlayer *player, const std::set<std::string> &privs);
180 v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
181 v3f getEyeOffset() const;
182 float getZoomFOV() const;
184 inline Metadata &getMeta() { return m_meta; }
187 std::string getPropertyPacket();
188 void unlinkPlayerSessionAndSave();
189 std::string generateUpdatePhysicsOverrideCommand() const;
191 RemotePlayer *m_player = nullptr;
192 session_t m_peer_id = 0;
197 v3f m_last_good_position;
198 float m_time_from_last_teleport = 0.0f;
199 float m_time_from_last_punch = 0.0f;
200 v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
201 float m_nocheat_dig_time = 0.0f;
202 float m_max_speed_override_time = 0.0f;
203 v3f m_max_speed_override = v3f(0.0f, 0.0f, 0.0f);
206 IntervalLimiter m_breathing_interval;
207 IntervalLimiter m_drowning_interval;
208 IntervalLimiter m_node_hurt_interval;
210 bool m_position_not_sent = false;
212 // Cached privileges for enforcement
213 std::set<std::string> m_privs;
214 bool m_is_singleplayer;
216 u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
219 s16 m_wanted_range = 0.0f;
224 float m_physics_override_speed = 1.0f;
225 float m_physics_override_jump = 1.0f;
226 float m_physics_override_gravity = 1.0f;
227 bool m_physics_override_sneak = true;
228 bool m_physics_override_sneak_glitch = false;
229 bool m_physics_override_new_move = true;
230 bool m_physics_override_sent = false;
233 struct PlayerHPChangeReason
246 bool from_mod = false;
247 int lua_reference = -1;
250 ServerActiveObject *object = nullptr;
254 inline bool hasLuaReference() const { return lua_reference >= 0; }
256 bool setTypeFromString(const std::string &typestr)
258 if (typestr == "set_hp")
260 else if (typestr == "punch")
262 else if (typestr == "fall")
264 else if (typestr == "node_damage")
266 else if (typestr == "drown")
268 else if (typestr == "respawn")
276 std::string getTypeAsString() const
279 case PlayerHPChangeReason::SET_HP:
281 case PlayerHPChangeReason::PLAYER_PUNCH:
283 case PlayerHPChangeReason::FALL:
285 case PlayerHPChangeReason::NODE_DAMAGE:
286 return "node_damage";
287 case PlayerHPChangeReason::DROWNING:
289 case PlayerHPChangeReason::RESPAWN:
296 PlayerHPChangeReason(Type type) : type(type) {}
298 PlayerHPChangeReason(Type type, ServerActiveObject *object) :
299 type(type), object(object)
303 PlayerHPChangeReason(Type type, std::string node) : type(type), node(node) {}