content_sao.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "network/networkprotocol.h"
  18. #include "util/numeric.h"
  19. #include "serverobject.h"
  20. #include "itemgroup.h"
  21. #include "object_properties.h"
  22. #include "constants.h"
  23. class UnitSAO: public ServerActiveObject
  24. {
  25. public:
  26. UnitSAO(ServerEnvironment *env, v3f pos);
  27. virtual ~UnitSAO() = default;
  28. void setRotation(v3f rotation) { m_rotation = rotation; }
  29. const v3f &getRotation() const { return m_rotation; }
  30. v3f getRadRotation() { return m_rotation * core::DEGTORAD; }
  31. // Deprecated
  32. f32 getRadYawDep() const { return (m_rotation.Y + 90.) * core::DEGTORAD; }
  33. u16 getHP() const { return m_hp; }
  34. // Use a function, if isDead can be defined by other conditions
  35. bool isDead() const { return m_hp == 0; }
  36. inline bool isAttached() const
  37. { return getParent(); }
  38. void setArmorGroups(const ItemGroupList &armor_groups);
  39. const ItemGroupList &getArmorGroups();
  40. void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
  41. void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
  42. void setAnimationSpeed(float frame_speed);
  43. void setBonePosition(const std::string &bone, v3f position, v3f rotation);
  44. void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
  45. void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
  46. void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
  47. void clearChildAttachments();
  48. void clearParentAttachment();
  49. void addAttachmentChild(int child_id);
  50. void removeAttachmentChild(int child_id);
  51. const std::unordered_set<int> &getAttachmentChildIds();
  52. ServerActiveObject *getParent() const;
  53. ObjectProperties* accessObjectProperties();
  54. void notifyObjectPropertiesModified();
  55. protected:
  56. u16 m_hp = 1;
  57. v3f m_rotation;
  58. bool m_properties_sent = true;
  59. ObjectProperties m_prop;
  60. ItemGroupList m_armor_groups;
  61. bool m_armor_groups_sent = false;
  62. v2f m_animation_range;
  63. float m_animation_speed = 0.0f;
  64. float m_animation_blend = 0.0f;
  65. bool m_animation_loop = true;
  66. bool m_animation_sent = false;
  67. bool m_animation_speed_sent = false;
  68. // Stores position and rotation for each bone name
  69. std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
  70. bool m_bone_position_sent = false;
  71. int m_attachment_parent_id = 0;
  72. std::unordered_set<int> m_attachment_child_ids;
  73. std::string m_attachment_bone = "";
  74. v3f m_attachment_position;
  75. v3f m_attachment_rotation;
  76. bool m_attachment_sent = false;
  77. private:
  78. void onAttach(int parent_id);
  79. void onDetach(int parent_id);
  80. };
  81. /*
  82. LuaEntitySAO needs some internals exposed.
  83. */
  84. class LuaEntitySAO : public UnitSAO
  85. {
  86. public:
  87. LuaEntitySAO(ServerEnvironment *env, v3f pos,
  88. const std::string &name, const std::string &state);
  89. ~LuaEntitySAO();
  90. ActiveObjectType getType() const
  91. { return ACTIVEOBJECT_TYPE_LUAENTITY; }
  92. ActiveObjectType getSendType() const
  93. { return ACTIVEOBJECT_TYPE_GENERIC; }
  94. virtual void addedToEnvironment(u32 dtime_s);
  95. static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
  96. const std::string &data);
  97. void step(float dtime, bool send_recommended);
  98. std::string getClientInitializationData(u16 protocol_version);
  99. bool isStaticAllowed() const
  100. { return m_prop.static_save; }
  101. void getStaticData(std::string *result) const;
  102. int punch(v3f dir,
  103. const ToolCapabilities *toolcap=NULL,
  104. ServerActiveObject *puncher=NULL,
  105. float time_from_last_punch=1000000);
  106. void rightClick(ServerActiveObject *clicker);
  107. void setPos(const v3f &pos);
  108. void moveTo(v3f pos, bool continuous);
  109. float getMinimumSavedMovement();
  110. std::string getDescription();
  111. void setHP(s32 hp, const PlayerHPChangeReason &reason);
  112. u16 getHP() const;
  113. /* LuaEntitySAO-specific */
  114. void setVelocity(v3f velocity);
  115. void addVelocity(v3f velocity)
  116. {
  117. m_velocity += velocity;
  118. }
  119. v3f getVelocity();
  120. void setAcceleration(v3f acceleration);
  121. v3f getAcceleration();
  122. void setTextureMod(const std::string &mod);
  123. std::string getTextureMod() const;
  124. void setSprite(v2s16 p, int num_frames, float framelength,
  125. bool select_horiz_by_yawpitch);
  126. std::string getName();
  127. bool getCollisionBox(aabb3f *toset) const;
  128. bool getSelectionBox(aabb3f *toset) const;
  129. bool collideWithObjects() const;
  130. private:
  131. std::string getPropertyPacket();
  132. void sendPosition(bool do_interpolate, bool is_movement_end);
  133. std::string m_init_name;
  134. std::string m_init_state;
  135. bool m_registered = false;
  136. v3f m_velocity;
  137. v3f m_acceleration;
  138. v3f m_last_sent_position;
  139. v3f m_last_sent_velocity;
  140. v3f m_last_sent_rotation;
  141. float m_last_sent_position_timer = 0.0f;
  142. float m_last_sent_move_precision = 0.0f;
  143. std::string m_current_texture_modifier = "";
  144. };
  145. /*
  146. PlayerSAO needs some internals exposed.
  147. */
  148. class LagPool
  149. {
  150. float m_pool = 15.0f;
  151. float m_max = 15.0f;
  152. public:
  153. LagPool() = default;
  154. void setMax(float new_max)
  155. {
  156. m_max = new_max;
  157. if(m_pool > new_max)
  158. m_pool = new_max;
  159. }
  160. void add(float dtime)
  161. {
  162. m_pool -= dtime;
  163. if(m_pool < 0)
  164. m_pool = 0;
  165. }
  166. void empty()
  167. {
  168. m_pool = m_max;
  169. }
  170. bool grab(float dtime)
  171. {
  172. if(dtime <= 0)
  173. return true;
  174. if(m_pool + dtime > m_max)
  175. return false;
  176. m_pool += dtime;
  177. return true;
  178. }
  179. };
  180. class RemotePlayer;
  181. class PlayerSAO : public UnitSAO
  182. {
  183. public:
  184. PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t peer_id_,
  185. bool is_singleplayer);
  186. ~PlayerSAO();
  187. ActiveObjectType getType() const
  188. { return ACTIVEOBJECT_TYPE_PLAYER; }
  189. ActiveObjectType getSendType() const
  190. { return ACTIVEOBJECT_TYPE_GENERIC; }
  191. std::string getDescription();
  192. /*
  193. Active object <-> environment interface
  194. */
  195. void addedToEnvironment(u32 dtime_s);
  196. void removingFromEnvironment();
  197. bool isStaticAllowed() const { return false; }
  198. std::string getClientInitializationData(u16 protocol_version);
  199. void getStaticData(std::string *result) const;
  200. void step(float dtime, bool send_recommended);
  201. void setBasePosition(const v3f &position);
  202. void setPos(const v3f &pos);
  203. void moveTo(v3f pos, bool continuous);
  204. void setPlayerYaw(const float yaw);
  205. // Data should not be sent at player initialization
  206. void setPlayerYawAndSend(const float yaw);
  207. void setLookPitch(const float pitch);
  208. // Data should not be sent at player initialization
  209. void setLookPitchAndSend(const float pitch);
  210. f32 getLookPitch() const { return m_pitch; }
  211. f32 getRadLookPitch() const { return m_pitch * core::DEGTORAD; }
  212. // Deprecated
  213. f32 getRadLookPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
  214. void setFov(const float pitch);
  215. f32 getFov() const { return m_fov; }
  216. void setWantedRange(const s16 range);
  217. s16 getWantedRange() const { return m_wanted_range; }
  218. /*
  219. Interaction interface
  220. */
  221. int punch(v3f dir,
  222. const ToolCapabilities *toolcap,
  223. ServerActiveObject *puncher,
  224. float time_from_last_punch);
  225. void rightClick(ServerActiveObject *clicker) {}
  226. void setHP(s32 hp, const PlayerHPChangeReason &reason);
  227. void setHPRaw(u16 hp) { m_hp = hp; }
  228. s16 readDamage();
  229. u16 getBreath() const { return m_breath; }
  230. void setBreath(const u16 breath, bool send = true);
  231. /*
  232. Inventory interface
  233. */
  234. Inventory* getInventory();
  235. const Inventory* getInventory() const;
  236. InventoryLocation getInventoryLocation() const;
  237. std::string getWieldList() const;
  238. ItemStack getWieldedItem() const;
  239. ItemStack getWieldedItemOrHand() const;
  240. bool setWieldedItem(const ItemStack &item);
  241. int getWieldIndex() const;
  242. void setWieldIndex(int i);
  243. /*
  244. PlayerSAO-specific
  245. */
  246. void disconnected();
  247. RemotePlayer *getPlayer() { return m_player; }
  248. session_t getPeerID() const { return m_peer_id; }
  249. // Cheat prevention
  250. v3f getLastGoodPosition() const
  251. {
  252. return m_last_good_position;
  253. }
  254. float resetTimeFromLastPunch()
  255. {
  256. float r = m_time_from_last_punch;
  257. m_time_from_last_punch = 0.0;
  258. return r;
  259. }
  260. void noCheatDigStart(const v3s16 &p)
  261. {
  262. m_nocheat_dig_pos = p;
  263. m_nocheat_dig_time = 0;
  264. }
  265. v3s16 getNoCheatDigPos()
  266. {
  267. return m_nocheat_dig_pos;
  268. }
  269. float getNoCheatDigTime()
  270. {
  271. return m_nocheat_dig_time;
  272. }
  273. void noCheatDigEnd()
  274. {
  275. m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
  276. }
  277. LagPool& getDigPool()
  278. {
  279. return m_dig_pool;
  280. }
  281. // Returns true if cheated
  282. bool checkMovementCheat();
  283. // Other
  284. void updatePrivileges(const std::set<std::string> &privs,
  285. bool is_singleplayer)
  286. {
  287. m_privs = privs;
  288. m_is_singleplayer = is_singleplayer;
  289. }
  290. bool getCollisionBox(aabb3f *toset) const;
  291. bool getSelectionBox(aabb3f *toset) const;
  292. bool collideWithObjects() const { return true; }
  293. void finalize(RemotePlayer *player, const std::set<std::string> &privs);
  294. v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
  295. v3f getEyeOffset() const;
  296. float getZoomFOV() const;
  297. inline Metadata &getMeta() { return m_meta; }
  298. private:
  299. std::string getPropertyPacket();
  300. void unlinkPlayerSessionAndSave();
  301. RemotePlayer *m_player = nullptr;
  302. session_t m_peer_id = 0;
  303. Inventory *m_inventory = nullptr;
  304. // Cheat prevention
  305. LagPool m_dig_pool;
  306. LagPool m_move_pool;
  307. v3f m_last_good_position;
  308. float m_time_from_last_teleport = 0.0f;
  309. float m_time_from_last_punch = 0.0f;
  310. v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
  311. float m_nocheat_dig_time = 0.0f;
  312. // Timers
  313. IntervalLimiter m_breathing_interval;
  314. IntervalLimiter m_drowning_interval;
  315. IntervalLimiter m_node_hurt_interval;
  316. int m_wield_index = 0;
  317. bool m_position_not_sent = false;
  318. // Cached privileges for enforcement
  319. std::set<std::string> m_privs;
  320. bool m_is_singleplayer;
  321. u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
  322. f32 m_pitch = 0.0f;
  323. f32 m_fov = 0.0f;
  324. s16 m_wanted_range = 0.0f;
  325. Metadata m_meta;
  326. public:
  327. float m_physics_override_speed = 1.0f;
  328. float m_physics_override_jump = 1.0f;
  329. float m_physics_override_gravity = 1.0f;
  330. bool m_physics_override_sneak = true;
  331. bool m_physics_override_sneak_glitch = false;
  332. bool m_physics_override_new_move = true;
  333. bool m_physics_override_sent = false;
  334. };
  335. struct PlayerHPChangeReason {
  336. enum Type : u8 {
  337. SET_HP,
  338. PLAYER_PUNCH,
  339. FALL,
  340. NODE_DAMAGE,
  341. DROWNING,
  342. RESPAWN
  343. };
  344. Type type = SET_HP;
  345. ServerActiveObject *object;
  346. bool from_mod = false;
  347. int lua_reference = -1;
  348. inline bool hasLuaReference() const
  349. {
  350. return lua_reference >= 0;
  351. }
  352. bool setTypeFromString(const std::string &typestr)
  353. {
  354. if (typestr == "set_hp")
  355. type = SET_HP;
  356. else if (typestr == "punch")
  357. type = PLAYER_PUNCH;
  358. else if (typestr == "fall")
  359. type = FALL;
  360. else if (typestr == "node_damage")
  361. type = NODE_DAMAGE;
  362. else if (typestr == "drown")
  363. type = DROWNING;
  364. else if (typestr == "respawn")
  365. type = RESPAWN;
  366. else
  367. return false;
  368. return true;
  369. }
  370. std::string getTypeAsString() const
  371. {
  372. switch (type) {
  373. case PlayerHPChangeReason::SET_HP:
  374. return "set_hp";
  375. case PlayerHPChangeReason::PLAYER_PUNCH:
  376. return "punch";
  377. case PlayerHPChangeReason::FALL:
  378. return "fall";
  379. case PlayerHPChangeReason::NODE_DAMAGE:
  380. return "node_damage";
  381. case PlayerHPChangeReason::DROWNING:
  382. return "drown";
  383. case PlayerHPChangeReason::RESPAWN:
  384. return "respawn";
  385. default:
  386. return "?";
  387. }
  388. }
  389. PlayerHPChangeReason(Type type, ServerActiveObject *object=NULL):
  390. type(type), object(object)
  391. {}
  392. };