content_sao.h 11 KB

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