content_sao.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #ifndef CONTENT_SAO_HEADER
  17. #define CONTENT_SAO_HEADER
  18. #include "serverobject.h"
  19. #include "itemgroup.h"
  20. #include "object_properties.h"
  21. #include "constants.h"
  22. class UnitSAO: public ServerActiveObject
  23. {
  24. public:
  25. UnitSAO(ServerEnvironment *env, v3f pos):
  26. ServerActiveObject(env, pos),
  27. m_hp(-1), m_yaw(0) {}
  28. virtual ~UnitSAO() {}
  29. virtual void setYaw(const float yaw) { m_yaw = yaw; }
  30. float getYaw() const { return m_yaw; };
  31. f32 getRadYaw() const { return m_yaw * core::DEGTORAD; }
  32. // Deprecated
  33. f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; }
  34. s16 getHP() const { return m_hp; }
  35. // Use a function, if isDead can be defined by other conditions
  36. bool isDead() const { return m_hp == 0; }
  37. protected:
  38. s16 m_hp;
  39. float m_yaw;
  40. };
  41. /*
  42. LuaEntitySAO needs some internals exposed.
  43. */
  44. class LuaEntitySAO : public UnitSAO
  45. {
  46. public:
  47. LuaEntitySAO(ServerEnvironment *env, v3f pos,
  48. const std::string &name, const std::string &state);
  49. ~LuaEntitySAO();
  50. ActiveObjectType getType() const
  51. { return ACTIVEOBJECT_TYPE_LUAENTITY; }
  52. ActiveObjectType getSendType() const
  53. { return ACTIVEOBJECT_TYPE_GENERIC; }
  54. virtual void addedToEnvironment(u32 dtime_s);
  55. static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
  56. const std::string &data);
  57. bool isAttached();
  58. void step(float dtime, bool send_recommended);
  59. std::string getClientInitializationData(u16 protocol_version);
  60. std::string getStaticData();
  61. int punch(v3f dir,
  62. const ToolCapabilities *toolcap=NULL,
  63. ServerActiveObject *puncher=NULL,
  64. float time_from_last_punch=1000000);
  65. void rightClick(ServerActiveObject *clicker);
  66. void setPos(v3f pos);
  67. void moveTo(v3f pos, bool continuous);
  68. float getMinimumSavedMovement();
  69. std::string getDescription();
  70. void setHP(s16 hp);
  71. s16 getHP() const;
  72. void setArmorGroups(const ItemGroupList &armor_groups);
  73. ItemGroupList getArmorGroups();
  74. void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
  75. void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
  76. void setBonePosition(const std::string &bone, v3f position, v3f rotation);
  77. void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
  78. void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
  79. void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
  80. void addAttachmentChild(int child_id);
  81. void removeAttachmentChild(int child_id);
  82. UNORDERED_SET<int> getAttachmentChildIds();
  83. ObjectProperties* accessObjectProperties();
  84. void notifyObjectPropertiesModified();
  85. /* LuaEntitySAO-specific */
  86. void setVelocity(v3f velocity);
  87. v3f getVelocity();
  88. void setAcceleration(v3f acceleration);
  89. v3f getAcceleration();
  90. void setTextureMod(const std::string &mod);
  91. void setSprite(v2s16 p, int num_frames, float framelength,
  92. bool select_horiz_by_yawpitch);
  93. std::string getName();
  94. bool getCollisionBox(aabb3f *toset);
  95. bool collideWithObjects();
  96. private:
  97. std::string getPropertyPacket();
  98. void sendPosition(bool do_interpolate, bool is_movement_end);
  99. std::string m_init_name;
  100. std::string m_init_state;
  101. bool m_registered;
  102. struct ObjectProperties m_prop;
  103. v3f m_velocity;
  104. v3f m_acceleration;
  105. ItemGroupList m_armor_groups;
  106. bool m_properties_sent;
  107. float m_last_sent_yaw;
  108. v3f m_last_sent_position;
  109. v3f m_last_sent_velocity;
  110. float m_last_sent_position_timer;
  111. float m_last_sent_move_precision;
  112. bool m_armor_groups_sent;
  113. v2f m_animation_range;
  114. float m_animation_speed;
  115. float m_animation_blend;
  116. bool m_animation_loop;
  117. bool m_animation_sent;
  118. UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
  119. bool m_bone_position_sent;
  120. int m_attachment_parent_id;
  121. UNORDERED_SET<int> m_attachment_child_ids;
  122. std::string m_attachment_bone;
  123. v3f m_attachment_position;
  124. v3f m_attachment_rotation;
  125. bool m_attachment_sent;
  126. };
  127. /*
  128. PlayerSAO needs some internals exposed.
  129. */
  130. class LagPool
  131. {
  132. float m_pool;
  133. float m_max;
  134. public:
  135. LagPool(): m_pool(15), m_max(15)
  136. {}
  137. void setMax(float new_max)
  138. {
  139. m_max = new_max;
  140. if(m_pool > new_max)
  141. m_pool = new_max;
  142. }
  143. void add(float dtime)
  144. {
  145. m_pool -= dtime;
  146. if(m_pool < 0)
  147. m_pool = 0;
  148. }
  149. bool grab(float dtime)
  150. {
  151. if(dtime <= 0)
  152. return true;
  153. if(m_pool + dtime > m_max)
  154. return false;
  155. m_pool += dtime;
  156. return true;
  157. }
  158. };
  159. class RemotePlayer;
  160. class PlayerSAO : public UnitSAO
  161. {
  162. public:
  163. PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer);
  164. ~PlayerSAO();
  165. ActiveObjectType getType() const
  166. { return ACTIVEOBJECT_TYPE_PLAYER; }
  167. ActiveObjectType getSendType() const
  168. { return ACTIVEOBJECT_TYPE_GENERIC; }
  169. std::string getDescription();
  170. /*
  171. Active object <-> environment interface
  172. */
  173. void addedToEnvironment(u32 dtime_s);
  174. void removingFromEnvironment();
  175. bool isStaticAllowed() const;
  176. std::string getClientInitializationData(u16 protocol_version);
  177. std::string getStaticData();
  178. bool isAttached();
  179. void step(float dtime, bool send_recommended);
  180. void setBasePosition(const v3f &position);
  181. void setPos(const v3f &pos);
  182. void moveTo(v3f pos, bool continuous);
  183. void setYaw(const float yaw, bool send_data = true);
  184. void setPitch(const float pitch, bool send_data = true);
  185. f32 getPitch() const { return m_pitch; }
  186. f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
  187. // Deprecated
  188. f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
  189. /*
  190. Interaction interface
  191. */
  192. int punch(v3f dir,
  193. const ToolCapabilities *toolcap,
  194. ServerActiveObject *puncher,
  195. float time_from_last_punch);
  196. void rightClick(ServerActiveObject *clicker);
  197. void setHP(s16 hp, bool direct = false);
  198. s16 readDamage();
  199. u16 getBreath() const { return m_breath; }
  200. void setBreath(const u16 breath);
  201. void setArmorGroups(const ItemGroupList &armor_groups);
  202. ItemGroupList getArmorGroups();
  203. void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
  204. void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
  205. void setBonePosition(const std::string &bone, v3f position, v3f rotation);
  206. void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
  207. void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
  208. void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
  209. void addAttachmentChild(int child_id);
  210. void removeAttachmentChild(int child_id);
  211. UNORDERED_SET<int> getAttachmentChildIds();
  212. ObjectProperties* accessObjectProperties();
  213. void notifyObjectPropertiesModified();
  214. /*
  215. Inventory interface
  216. */
  217. Inventory* getInventory();
  218. const Inventory* getInventory() const;
  219. InventoryLocation getInventoryLocation() const;
  220. std::string getWieldList() const;
  221. int getWieldIndex() const;
  222. void setWieldIndex(int i);
  223. /*
  224. PlayerSAO-specific
  225. */
  226. void disconnected();
  227. RemotePlayer *getPlayer() { return m_player; }
  228. u16 getPeerID() const { return m_peer_id; }
  229. // Cheat prevention
  230. v3f getLastGoodPosition() const
  231. {
  232. return m_last_good_position;
  233. }
  234. float resetTimeFromLastPunch()
  235. {
  236. float r = m_time_from_last_punch;
  237. m_time_from_last_punch = 0.0;
  238. return r;
  239. }
  240. void noCheatDigStart(v3s16 p)
  241. {
  242. m_nocheat_dig_pos = p;
  243. m_nocheat_dig_time = 0;
  244. }
  245. v3s16 getNoCheatDigPos()
  246. {
  247. return m_nocheat_dig_pos;
  248. }
  249. float getNoCheatDigTime()
  250. {
  251. return m_nocheat_dig_time;
  252. }
  253. void noCheatDigEnd()
  254. {
  255. m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
  256. }
  257. LagPool& getDigPool()
  258. {
  259. return m_dig_pool;
  260. }
  261. // Returns true if cheated
  262. bool checkMovementCheat();
  263. // Other
  264. void updatePrivileges(const std::set<std::string> &privs,
  265. bool is_singleplayer)
  266. {
  267. m_privs = privs;
  268. m_is_singleplayer = is_singleplayer;
  269. }
  270. bool getCollisionBox(aabb3f *toset);
  271. bool collideWithObjects();
  272. void initialize(RemotePlayer *player, const std::set<std::string> &privs);
  273. v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
  274. v3f getEyeOffset() const { return v3f(0, BS * 1.625f, 0); }
  275. private:
  276. std::string getPropertyPacket();
  277. RemotePlayer *m_player;
  278. u16 m_peer_id;
  279. Inventory *m_inventory;
  280. s16 m_damage;
  281. // Cheat prevention
  282. LagPool m_dig_pool;
  283. LagPool m_move_pool;
  284. v3f m_last_good_position;
  285. float m_time_from_last_punch;
  286. v3s16 m_nocheat_dig_pos;
  287. float m_nocheat_dig_time;
  288. int m_wield_index;
  289. bool m_position_not_sent;
  290. ItemGroupList m_armor_groups;
  291. bool m_armor_groups_sent;
  292. bool m_properties_sent;
  293. struct ObjectProperties m_prop;
  294. // Cached privileges for enforcement
  295. std::set<std::string> m_privs;
  296. bool m_is_singleplayer;
  297. v2f m_animation_range;
  298. float m_animation_speed;
  299. float m_animation_blend;
  300. bool m_animation_loop;
  301. bool m_animation_sent;
  302. // Stores position and rotation for each bone name
  303. UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
  304. bool m_bone_position_sent;
  305. int m_attachment_parent_id;
  306. UNORDERED_SET<int> m_attachment_child_ids;
  307. std::string m_attachment_bone;
  308. v3f m_attachment_position;
  309. v3f m_attachment_rotation;
  310. bool m_attachment_sent;
  311. u16 m_breath;
  312. f32 m_pitch;
  313. public:
  314. float m_physics_override_speed;
  315. float m_physics_override_jump;
  316. float m_physics_override_gravity;
  317. bool m_physics_override_sneak;
  318. bool m_physics_override_sneak_glitch;
  319. bool m_physics_override_sent;
  320. };
  321. #endif