serverobject.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 SERVEROBJECT_HEADER
  17. #define SERVEROBJECT_HEADER
  18. #include "irrlichttypes_bloated.h"
  19. #include "activeobject.h"
  20. #include "inventorymanager.h"
  21. #include "itemgroup.h"
  22. #include "util/container.h"
  23. /*
  24. Some planning
  25. -------------
  26. * Server environment adds an active object, which gets the id 1
  27. * The active object list is scanned for each client once in a while,
  28. and it finds out what objects have been added that are not known
  29. by the client yet. This scan is initiated by the Server class and
  30. the result ends up directly to the server.
  31. * A network packet is created with the info and sent to the client.
  32. * Environment converts objects to static data and static data to
  33. objects, based on how close players are to them.
  34. */
  35. class ServerEnvironment;
  36. struct ItemStack;
  37. class Player;
  38. struct ToolCapabilities;
  39. struct ObjectProperties;
  40. class ServerActiveObject : public ActiveObject
  41. {
  42. public:
  43. /*
  44. NOTE: m_env can be NULL, but step() isn't called if it is.
  45. Prototypes are used that way.
  46. */
  47. ServerActiveObject(ServerEnvironment *env, v3f pos);
  48. virtual ~ServerActiveObject();
  49. virtual u8 getSendType() const
  50. { return getType(); }
  51. // Called after id has been set and has been inserted in environment
  52. virtual void addedToEnvironment(u32 dtime_s){};
  53. // Called before removing from environment
  54. virtual void removingFromEnvironment(){};
  55. // Returns true if object's deletion is the job of the
  56. // environment
  57. virtual bool environmentDeletes() const
  58. { return true; }
  59. virtual bool unlimitedTransferDistance() const
  60. { return false; }
  61. // Create a certain type of ServerActiveObject
  62. static ServerActiveObject* create(u8 type,
  63. ServerEnvironment *env, u16 id, v3f pos,
  64. const std::string &data);
  65. /*
  66. Some simple getters/setters
  67. */
  68. v3f getBasePosition(){ return m_base_position; }
  69. void setBasePosition(v3f pos){ m_base_position = pos; }
  70. ServerEnvironment* getEnv(){ return m_env; }
  71. /*
  72. Some more dynamic interface
  73. */
  74. virtual void setPos(v3f pos)
  75. { setBasePosition(pos); }
  76. // continuous: if true, object does not stop immediately at pos
  77. virtual void moveTo(v3f pos, bool continuous)
  78. { setBasePosition(pos); }
  79. // If object has moved less than this and data has not changed,
  80. // saving to disk may be omitted
  81. virtual float getMinimumSavedMovement();
  82. virtual bool isPeaceful(){return true;}
  83. virtual std::string getDescription(){return "SAO";}
  84. /*
  85. Step object in time.
  86. Messages added to messages are sent to client over network.
  87. send_recommended:
  88. True at around 5-10 times a second, same for all objects.
  89. This is used to let objects send most of the data at the
  90. same time so that the data can be combined in a single
  91. packet.
  92. */
  93. virtual void step(float dtime, bool send_recommended){}
  94. /*
  95. The return value of this is passed to the client-side object
  96. when it is created
  97. */
  98. virtual std::string getClientInitializationData(u16 protocol_version){return "";}
  99. /*
  100. The return value of this is passed to the server-side object
  101. when it is created (converted from static to active - actually
  102. the data is the static form)
  103. */
  104. virtual std::string getStaticData()
  105. {
  106. assert(isStaticAllowed());
  107. return "";
  108. }
  109. /*
  110. Return false in here to never save and instead remove object
  111. on unload. getStaticData() will not be called in that case.
  112. */
  113. virtual bool isStaticAllowed() const
  114. {return true;}
  115. // Returns tool wear
  116. virtual int punch(v3f dir,
  117. const ToolCapabilities *toolcap=NULL,
  118. ServerActiveObject *puncher=NULL,
  119. float time_from_last_punch=1000000)
  120. { return 0; }
  121. virtual void rightClick(ServerActiveObject *clicker)
  122. {}
  123. virtual void setHP(s16 hp)
  124. {}
  125. virtual s16 getHP() const
  126. { return 0; }
  127. virtual void setArmorGroups(const ItemGroupList &armor_groups)
  128. {}
  129. virtual void setPhysicsOverride(float physics_override_speed, float physics_override_jump, float physics_override_gravity)
  130. {}
  131. virtual void setAnimation(v2f frames, float frame_speed, float frame_blend)
  132. {}
  133. virtual void setBonePosition(std::string bone, v3f position, v3f rotation)
  134. {}
  135. virtual void setAttachment(int parent_id, std::string bone, v3f position, v3f rotation)
  136. {}
  137. virtual ObjectProperties* accessObjectProperties()
  138. { return NULL; }
  139. virtual void notifyObjectPropertiesModified()
  140. {}
  141. // Inventory and wielded item
  142. virtual Inventory* getInventory()
  143. { return NULL; }
  144. virtual const Inventory* getInventory() const
  145. { return NULL; }
  146. virtual InventoryLocation getInventoryLocation() const
  147. { return InventoryLocation(); }
  148. virtual void setInventoryModified()
  149. {}
  150. virtual std::string getWieldList() const
  151. { return ""; }
  152. virtual int getWieldIndex() const
  153. { return 0; }
  154. virtual ItemStack getWieldedItem() const;
  155. virtual bool setWieldedItem(const ItemStack &item);
  156. /*
  157. Number of players which know about this object. Object won't be
  158. deleted until this is 0 to keep the id preserved for the right
  159. object.
  160. */
  161. u16 m_known_by_count;
  162. /*
  163. - Whether this object is to be removed when nobody knows about
  164. it anymore.
  165. - Removal is delayed to preserve the id for the time during which
  166. it could be confused to some other object by some client.
  167. - This is set to true by the step() method when the object wants
  168. to be deleted.
  169. - This can be set to true by anything else too.
  170. */
  171. bool m_removed;
  172. /*
  173. This is set to true when an object should be removed from the active
  174. object list but couldn't be removed because the id has to be
  175. reserved for some client.
  176. The environment checks this periodically. If this is true and also
  177. m_known_by_count is true, object is deleted from the active object
  178. list.
  179. */
  180. bool m_pending_deactivation;
  181. /*
  182. Whether the object's static data has been stored to a block
  183. */
  184. bool m_static_exists;
  185. /*
  186. The block from which the object was loaded from, and in which
  187. a copy of the static data resides.
  188. */
  189. v3s16 m_static_block;
  190. /*
  191. Queue of messages to be sent to the client
  192. */
  193. Queue<ActiveObjectMessage> m_messages_out;
  194. protected:
  195. // Used for creating objects based on type
  196. typedef ServerActiveObject* (*Factory)
  197. (ServerEnvironment *env, v3f pos,
  198. const std::string &data);
  199. static void registerType(u16 type, Factory f);
  200. ServerEnvironment *m_env;
  201. v3f m_base_position;
  202. private:
  203. // Used for creating objects based on type
  204. static std::map<u16, Factory> m_types;
  205. };
  206. #endif