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"
27 #include "util/pointedthing.h"
30 PlayerSAO needs some internals exposed.
41 void setMax(float new_max)
55 void empty() { m_pool = m_max; }
57 bool grab(float dtime)
61 if (m_pool + dtime > m_max)
70 class PlayerSAO : public UnitSAO
73 PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t peer_id_,
74 bool is_singleplayer);
76 ActiveObjectType getType() const override { return ACTIVEOBJECT_TYPE_PLAYER; }
77 ActiveObjectType getSendType() const override { return ACTIVEOBJECT_TYPE_GENERIC; }
78 std::string getDescription() override;
81 Active object <-> environment interface
84 void addedToEnvironment(u32 dtime_s) override;
85 void removingFromEnvironment() override;
86 bool isStaticAllowed() const override { return false; }
87 bool shouldUnload() const override { return false; }
88 std::string getClientInitializationData(u16 protocol_version) override;
89 void getStaticData(std::string *result) const override;
90 void step(float dtime, bool send_recommended) override;
91 void setBasePosition(v3f position);
92 void setPos(const v3f &pos) override;
93 void moveTo(v3f pos, bool continuous) override;
94 void setPlayerYaw(const float yaw);
95 // Data should not be sent at player initialization
96 void setPlayerYawAndSend(const float yaw);
97 void setLookPitch(const float pitch);
98 // Data should not be sent at player initialization
99 void setLookPitchAndSend(const float pitch);
100 f32 getLookPitch() const { return m_pitch; }
101 f32 getRadLookPitch() const { return m_pitch * core::DEGTORAD; }
103 f32 getRadLookPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
104 void setFov(const float pitch);
105 f32 getFov() const { return m_fov; }
106 void setWantedRange(const s16 range);
107 s16 getWantedRange() const { return m_wanted_range; }
110 Interaction interface
113 u32 punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher,
114 float time_from_last_punch, u16 initial_wear = 0) override;
115 void rightClick(ServerActiveObject *clicker) override;
116 void setHP(s32 hp, const PlayerHPChangeReason &reason) override
118 return setHP(hp, reason, false);
120 void setHP(s32 hp, const PlayerHPChangeReason &reason, bool from_client);
121 void setHPRaw(u16 hp) { m_hp = hp; }
122 u16 getBreath() const { return m_breath; }
123 void setBreath(const u16 breath, bool send = true);
128 Inventory *getInventory() const override;
129 InventoryLocation getInventoryLocation() const override;
130 void setInventoryModified() override {}
131 std::string getWieldList() const override { return "main"; }
132 u16 getWieldIndex() const override;
133 ItemStack getWieldedItem(ItemStack *selected, ItemStack *hand = nullptr) const override;
134 bool getOffhandWieldedItem(ItemStack *offhand, ItemStack *place, IItemDefManager *itemdef_manager, PointedThing pointed) const;
135 bool setWieldedItem(const ItemStack &item) override;
136 bool setOffhandWieldedItem(const ItemStack &item);
144 RemotePlayer *getPlayer() { return m_player; }
145 session_t getPeerID() const { return m_peer_id; }
149 v3f getLastGoodPosition() const { return m_last_good_position; }
150 float resetTimeFromLastPunch()
152 float r = m_time_from_last_punch;
153 m_time_from_last_punch = 0.0;
156 void noCheatDigStart(const v3s16 &p)
158 m_nocheat_dig_pos = p;
159 m_nocheat_dig_time = 0;
161 v3s16 getNoCheatDigPos() { return m_nocheat_dig_pos; }
162 float getNoCheatDigTime() { return m_nocheat_dig_time; }
163 void noCheatDigEnd() { m_nocheat_dig_pos = v3s16(32767, 32767, 32767); }
164 LagPool &getDigPool() { return m_dig_pool; }
165 void setMaxSpeedOverride(const v3f &vel);
166 // Returns true if cheated
167 bool checkMovementCheat();
171 void updatePrivileges(const std::set<std::string> &privs, bool is_singleplayer)
174 m_is_singleplayer = is_singleplayer;
177 bool getCollisionBox(aabb3f *toset) const override;
178 bool getSelectionBox(aabb3f *toset) const override;
179 bool collideWithObjects() const override { return true; }
181 void finalize(RemotePlayer *player, const std::set<std::string> &privs);
183 v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
184 v3f getEyeOffset() const;
185 float getZoomFOV() const;
187 inline SimpleMetadata &getMeta() { return m_meta; }
190 std::string getPropertyPacket();
191 void unlinkPlayerSessionAndSave();
192 std::string generateUpdatePhysicsOverrideCommand() const;
194 RemotePlayer *m_player = nullptr;
195 session_t m_peer_id = 0;
200 v3f m_last_good_position;
201 float m_time_from_last_teleport = 0.0f;
202 float m_time_from_last_punch = 0.0f;
203 v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
204 float m_nocheat_dig_time = 0.0f;
205 float m_max_speed_override_time = 0.0f;
206 v3f m_max_speed_override = v3f(0.0f, 0.0f, 0.0f);
209 IntervalLimiter m_breathing_interval;
210 IntervalLimiter m_drowning_interval;
211 IntervalLimiter m_node_hurt_interval;
213 bool m_position_not_sent = false;
215 // Cached privileges for enforcement
216 std::set<std::string> m_privs;
217 bool m_is_singleplayer;
219 u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
222 s16 m_wanted_range = 0.0f;
224 SimpleMetadata m_meta;
227 bool m_physics_override_sent = false;
230 struct PlayerHPChangeReason
235 SET_HP_MAX, // internal type to allow distinguishing hp reset and damage (for effects)
244 bool from_mod = false;
245 int lua_reference = -1;
248 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) : type(type), node(node) {}