content_sao.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. virtual void setYaw(const float yaw) { m_yaw = yaw; }
  29. float getYaw() const { return m_yaw; };
  30. f32 getRadYaw() const { return m_yaw * core::DEGTORAD; }
  31. // Deprecated
  32. f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; }
  33. s16 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. bool isAttached() const;
  37. void setArmorGroups(const ItemGroupList &armor_groups);
  38. const ItemGroupList &getArmorGroups();
  39. void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
  40. void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
  41. void setAnimationSpeed(float frame_speed);
  42. void setBonePosition(const std::string &bone, v3f position, v3f rotation);
  43. void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
  44. void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
  45. void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
  46. void addAttachmentChild(int child_id);
  47. void removeAttachmentChild(int child_id);
  48. const std::unordered_set<int> &getAttachmentChildIds();
  49. ObjectProperties* accessObjectProperties();
  50. void notifyObjectPropertiesModified();
  51. protected:
  52. s16 m_hp = -1;
  53. float m_yaw = 0.0f;
  54. bool m_properties_sent = true;
  55. struct ObjectProperties m_prop;
  56. ItemGroupList m_armor_groups;
  57. bool m_armor_groups_sent = false;
  58. v2f m_animation_range;
  59. float m_animation_speed = 0.0f;
  60. float m_animation_blend = 0.0f;
  61. bool m_animation_loop = true;
  62. bool m_animation_sent = false;
  63. bool m_animation_speed_sent = false;
  64. // Stores position and rotation for each bone name
  65. std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
  66. bool m_bone_position_sent = false;
  67. int m_attachment_parent_id = 0;
  68. std::unordered_set<int> m_attachment_child_ids;
  69. std::string m_attachment_bone = "";
  70. v3f m_attachment_position;
  71. v3f m_attachment_rotation;
  72. bool m_attachment_sent = false;
  73. };
  74. /*
  75. LuaEntitySAO needs some internals exposed.
  76. */
  77. class LuaEntitySAO : public UnitSAO
  78. {
  79. public:
  80. LuaEntitySAO(ServerEnvironment *env, v3f pos,
  81. const std::string &name, const std::string &state);
  82. ~LuaEntitySAO();
  83. ActiveObjectType getType() const
  84. { return ACTIVEOBJECT_TYPE_LUAENTITY; }
  85. ActiveObjectType getSendType() const
  86. { return ACTIVEOBJECT_TYPE_GENERIC; }
  87. virtual void addedToEnvironment(u32 dtime_s);
  88. static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
  89. const std::string &data);
  90. void step(float dtime, bool send_recommended);
  91. std::string getClientInitializationData(u16 protocol_version);
  92. void getStaticData(std::string *result) const;
  93. int punch(v3f dir,
  94. const ToolCapabilities *toolcap=NULL,
  95. ServerActiveObject *puncher=NULL,
  96. float time_from_last_punch=1000000);
  97. void rightClick(ServerActiveObject *clicker);
  98. void setPos(const v3f &pos);
  99. void moveTo(v3f pos, bool continuous);
  100. float getMinimumSavedMovement();
  101. std::string getDescription();
  102. void setHP(s16 hp);
  103. s16 getHP() const;
  104. /* LuaEntitySAO-specific */
  105. void setVelocity(v3f velocity);
  106. v3f getVelocity();
  107. void setAcceleration(v3f acceleration);
  108. v3f getAcceleration();
  109. void setTextureMod(const std::string &mod);
  110. std::string getTextureMod() const;
  111. void setSprite(v2s16 p, int num_frames, float framelength,
  112. bool select_horiz_by_yawpitch);
  113. std::string getName();
  114. bool getCollisionBox(aabb3f *toset) const;
  115. bool getSelectionBox(aabb3f *toset) const;
  116. bool collideWithObjects() const;
  117. private:
  118. std::string getPropertyPacket();
  119. void sendPosition(bool do_interpolate, bool is_movement_end);
  120. std::string m_init_name;
  121. std::string m_init_state;
  122. bool m_registered = false;
  123. v3f m_velocity;
  124. v3f m_acceleration;
  125. float m_last_sent_yaw = 0.0f;
  126. v3f m_last_sent_position;
  127. v3f m_last_sent_velocity;
  128. float m_last_sent_position_timer = 0.0f;
  129. float m_last_sent_move_precision = 0.0f;
  130. std::string m_current_texture_modifier = "";
  131. };
  132. /*
  133. PlayerSAO needs some internals exposed.
  134. */
  135. class LagPool
  136. {
  137. float m_pool = 15.0f;
  138. float m_max = 15.0f;
  139. public:
  140. LagPool() = default;
  141. void setMax(float new_max)
  142. {
  143. m_max = new_max;
  144. if(m_pool > new_max)
  145. m_pool = new_max;
  146. }
  147. void add(float dtime)
  148. {
  149. m_pool -= dtime;
  150. if(m_pool < 0)
  151. m_pool = 0;
  152. }
  153. void empty()
  154. {
  155. m_pool = m_max;
  156. }
  157. bool grab(float dtime)
  158. {
  159. if(dtime <= 0)
  160. return true;
  161. if(m_pool + dtime > m_max)
  162. return false;
  163. m_pool += dtime;
  164. return true;
  165. }
  166. };
  167. typedef std::unordered_map<std::string, std::string> PlayerAttributes;
  168. class RemotePlayer;
  169. class PlayerSAO : public UnitSAO
  170. {
  171. public:
  172. PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t peer_id_,
  173. bool is_singleplayer);
  174. ~PlayerSAO();
  175. ActiveObjectType getType() const
  176. { return ACTIVEOBJECT_TYPE_PLAYER; }
  177. ActiveObjectType getSendType() const
  178. { return ACTIVEOBJECT_TYPE_GENERIC; }
  179. std::string getDescription();
  180. /*
  181. Active object <-> environment interface
  182. */
  183. void addedToEnvironment(u32 dtime_s);
  184. void removingFromEnvironment();
  185. bool isStaticAllowed() const { return false; }
  186. std::string getClientInitializationData(u16 protocol_version);
  187. void getStaticData(std::string *result) const;
  188. void step(float dtime, bool send_recommended);
  189. void setBasePosition(const v3f &position);
  190. void setPos(const v3f &pos);
  191. void moveTo(v3f pos, bool continuous);
  192. void setYaw(const float yaw);
  193. // Data should not be sent at player initialization
  194. void setYawAndSend(const float yaw);
  195. void setPitch(const float pitch);
  196. // Data should not be sent at player initialization
  197. void setPitchAndSend(const float pitch);
  198. f32 getPitch() const { return m_pitch; }
  199. f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
  200. // Deprecated
  201. f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
  202. void setFov(const float pitch);
  203. f32 getFov() const { return m_fov; }
  204. void setWantedRange(const s16 range);
  205. s16 getWantedRange() const { return m_wanted_range; }
  206. /*
  207. Interaction interface
  208. */
  209. int punch(v3f dir,
  210. const ToolCapabilities *toolcap,
  211. ServerActiveObject *puncher,
  212. float time_from_last_punch);
  213. void rightClick(ServerActiveObject *clicker) {}
  214. void setHP(s16 hp);
  215. void setHPRaw(s16 hp) { m_hp = hp; }
  216. s16 readDamage();
  217. u16 getBreath() const { return m_breath; }
  218. void setBreath(const u16 breath, bool send = true);
  219. /*
  220. Inventory interface
  221. */
  222. Inventory* getInventory();
  223. const Inventory* getInventory() const;
  224. InventoryLocation getInventoryLocation() const;
  225. std::string getWieldList() const;
  226. ItemStack getWieldedItem() const;
  227. ItemStack getWieldedItemOrHand() const;
  228. bool setWieldedItem(const ItemStack &item);
  229. int getWieldIndex() const;
  230. void setWieldIndex(int i);
  231. /*
  232. Modding interface
  233. */
  234. inline void setExtendedAttribute(const std::string &attr, const std::string &value)
  235. {
  236. m_extra_attributes[attr] = value;
  237. m_extended_attributes_modified = true;
  238. }
  239. inline bool getExtendedAttribute(const std::string &attr, std::string *value)
  240. {
  241. if (m_extra_attributes.find(attr) == m_extra_attributes.end())
  242. return false;
  243. *value = m_extra_attributes[attr];
  244. return true;
  245. }
  246. inline void removeExtendedAttribute(const std::string &attr)
  247. {
  248. PlayerAttributes::iterator it = m_extra_attributes.find(attr);
  249. if (it == m_extra_attributes.end())
  250. return;
  251. m_extra_attributes.erase(it);
  252. m_extended_attributes_modified = true;
  253. }
  254. inline const PlayerAttributes &getExtendedAttributes()
  255. {
  256. return m_extra_attributes;
  257. }
  258. inline bool extendedAttributesModified() const
  259. {
  260. return m_extended_attributes_modified;
  261. }
  262. inline void setExtendedAttributeModified(bool v)
  263. {
  264. m_extended_attributes_modified = v;
  265. }
  266. /*
  267. PlayerSAO-specific
  268. */
  269. void disconnected();
  270. RemotePlayer *getPlayer() { return m_player; }
  271. session_t getPeerID() const { return m_peer_id; }
  272. // Cheat prevention
  273. v3f getLastGoodPosition() const
  274. {
  275. return m_last_good_position;
  276. }
  277. float resetTimeFromLastPunch()
  278. {
  279. float r = m_time_from_last_punch;
  280. m_time_from_last_punch = 0.0;
  281. return r;
  282. }
  283. void noCheatDigStart(const v3s16 &p)
  284. {
  285. m_nocheat_dig_pos = p;
  286. m_nocheat_dig_time = 0;
  287. }
  288. v3s16 getNoCheatDigPos()
  289. {
  290. return m_nocheat_dig_pos;
  291. }
  292. float getNoCheatDigTime()
  293. {
  294. return m_nocheat_dig_time;
  295. }
  296. void noCheatDigEnd()
  297. {
  298. m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
  299. }
  300. LagPool& getDigPool()
  301. {
  302. return m_dig_pool;
  303. }
  304. // Returns true if cheated
  305. bool checkMovementCheat();
  306. // Other
  307. void updatePrivileges(const std::set<std::string> &privs,
  308. bool is_singleplayer)
  309. {
  310. m_privs = privs;
  311. m_is_singleplayer = is_singleplayer;
  312. }
  313. bool getCollisionBox(aabb3f *toset) const;
  314. bool getSelectionBox(aabb3f *toset) const;
  315. bool collideWithObjects() const { return true; }
  316. void finalize(RemotePlayer *player, const std::set<std::string> &privs);
  317. v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
  318. v3f getEyeOffset() const;
  319. private:
  320. std::string getPropertyPacket();
  321. void unlinkPlayerSessionAndSave();
  322. RemotePlayer *m_player = nullptr;
  323. session_t m_peer_id = 0;
  324. Inventory *m_inventory = nullptr;
  325. s16 m_damage = 0;
  326. // Cheat prevention
  327. LagPool m_dig_pool;
  328. LagPool m_move_pool;
  329. v3f m_last_good_position;
  330. float m_time_from_last_teleport = 0.0f;
  331. float m_time_from_last_punch = 0.0f;
  332. v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
  333. float m_nocheat_dig_time = 0.0f;
  334. // Timers
  335. IntervalLimiter m_breathing_interval;
  336. IntervalLimiter m_drowning_interval;
  337. IntervalLimiter m_node_hurt_interval;
  338. int m_wield_index = 0;
  339. bool m_position_not_sent = false;
  340. // Cached privileges for enforcement
  341. std::set<std::string> m_privs;
  342. bool m_is_singleplayer;
  343. u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
  344. f32 m_pitch = 0.0f;
  345. f32 m_fov = 0.0f;
  346. s16 m_wanted_range = 0.0f;
  347. PlayerAttributes m_extra_attributes;
  348. bool m_extended_attributes_modified = false;
  349. public:
  350. float m_physics_override_speed = 1.0f;
  351. float m_physics_override_jump = 1.0f;
  352. float m_physics_override_gravity = 1.0f;
  353. bool m_physics_override_sneak = true;
  354. bool m_physics_override_sneak_glitch = false;
  355. bool m_physics_override_new_move = true;
  356. bool m_physics_override_sent = false;
  357. };