environment.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. Minetest-c55
  3. Copyright (C) 2010 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 General Public License as published by
  6. the Free Software Foundation; either version 2 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 General Public License for more details.
  12. You should have received a copy of the GNU 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 ENVIRONMENT_HEADER
  17. #define ENVIRONMENT_HEADER
  18. /*
  19. This class is the game's environment.
  20. It contains:
  21. - The map
  22. - Players
  23. - Other objects
  24. - The current time in the game (actually it only contains the brightness)
  25. - etc.
  26. */
  27. #include <list>
  28. #include "common_irrlicht.h"
  29. #include "player.h"
  30. #include "map.h"
  31. #include <ostream>
  32. #include "utility.h"
  33. class Environment
  34. {
  35. public:
  36. // Environment will delete the map passed to the constructor
  37. Environment();
  38. virtual ~Environment();
  39. /*
  40. Step everything in environment.
  41. - Move players
  42. - Step mobs
  43. - Run timers of map
  44. */
  45. virtual void step(f32 dtime) = 0;
  46. virtual Map & getMap() = 0;
  47. virtual void addPlayer(Player *player);
  48. void removePlayer(u16 peer_id);
  49. Player * getPlayer(u16 peer_id);
  50. Player * getPlayer(const char *name);
  51. Player * getRandomConnectedPlayer();
  52. Player * getNearestConnectedPlayer(v3f pos);
  53. core::list<Player*> getPlayers();
  54. core::list<Player*> getPlayers(bool ignore_disconnected);
  55. void printPlayers(std::ostream &o);
  56. void setDayNightRatio(u32 r);
  57. u32 getDayNightRatio();
  58. protected:
  59. // peer_ids in here should be unique, except that there may be many 0s
  60. core::list<Player*> m_players;
  61. // Brightness
  62. u32 m_daynight_ratio;
  63. };
  64. /*
  65. The server-side environment.
  66. This is not thread-safe. Server uses an environment mutex.
  67. */
  68. #include "serverobject.h"
  69. class Server;
  70. class ServerEnvironment : public Environment
  71. {
  72. public:
  73. ServerEnvironment(ServerMap *map, Server *server);
  74. ~ServerEnvironment();
  75. Map & getMap()
  76. {
  77. return *m_map;
  78. }
  79. ServerMap & getServerMap()
  80. {
  81. return *m_map;
  82. }
  83. Server * getServer()
  84. {
  85. return m_server;
  86. }
  87. void step(f32 dtime);
  88. void serializePlayers(const std::string &savedir);
  89. void deSerializePlayers(const std::string &savedir);
  90. /*
  91. ActiveObjects
  92. */
  93. ServerActiveObject* getActiveObject(u16 id);
  94. /*
  95. Adds an active object to the environment.
  96. Environment handles deletion of object.
  97. Object may be deleted by environment immediately.
  98. If id of object is 0, assigns a free id to it.
  99. Returns the id of the object.
  100. Returns 0 if not added and thus deleted.
  101. */
  102. u16 addActiveObject(ServerActiveObject *object);
  103. /*
  104. Finds out what new objects have been added to
  105. inside a radius around a position
  106. */
  107. void getAddedActiveObjects(v3s16 pos, s16 radius,
  108. core::map<u16, bool> &current_objects,
  109. core::map<u16, bool> &added_objects);
  110. /*
  111. Finds out what new objects have been removed from
  112. inside a radius around a position
  113. */
  114. void getRemovedActiveObjects(v3s16 pos, s16 radius,
  115. core::map<u16, bool> &current_objects,
  116. core::map<u16, bool> &removed_objects);
  117. /*
  118. Gets the next message emitted by some active object.
  119. Returns a message with id=0 if no messages are available.
  120. */
  121. ActiveObjectMessage getActiveObjectMessage();
  122. private:
  123. ServerMap *m_map;
  124. Server *m_server;
  125. core::map<u16, ServerActiveObject*> m_active_objects;
  126. Queue<ActiveObjectMessage> m_active_object_messages;
  127. float m_random_spawn_timer;
  128. float m_send_recommended_timer;
  129. IntervalLimiter m_object_management_interval;
  130. };
  131. #ifndef SERVER
  132. #include "clientobject.h"
  133. /*
  134. The client-side environment.
  135. This is not thread-safe.
  136. Must be called from main (irrlicht) thread (uses the SceneManager)
  137. Client uses an environment mutex.
  138. */
  139. enum ClientEnvEventType
  140. {
  141. CEE_NONE,
  142. CEE_PLAYER_DAMAGE
  143. };
  144. struct ClientEnvEvent
  145. {
  146. ClientEnvEventType type;
  147. union {
  148. struct{
  149. } none;
  150. struct{
  151. u8 amount;
  152. } player_damage;
  153. };
  154. };
  155. class ClientEnvironment : public Environment
  156. {
  157. public:
  158. ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
  159. ~ClientEnvironment();
  160. Map & getMap()
  161. {
  162. return *m_map;
  163. }
  164. ClientMap & getClientMap()
  165. {
  166. return *m_map;
  167. }
  168. void step(f32 dtime);
  169. virtual void addPlayer(Player *player);
  170. LocalPlayer * getLocalPlayer();
  171. void updateMeshes(v3s16 blockpos);
  172. void expireMeshes(bool only_daynight_diffed);
  173. /*
  174. ActiveObjects
  175. */
  176. ClientActiveObject* getActiveObject(u16 id);
  177. /*
  178. Adds an active object to the environment.
  179. Environment handles deletion of object.
  180. Object may be deleted by environment immediately.
  181. If id of object is 0, assigns a free id to it.
  182. Returns the id of the object.
  183. Returns 0 if not added and thus deleted.
  184. */
  185. u16 addActiveObject(ClientActiveObject *object);
  186. void addActiveObject(u16 id, u8 type, const std::string &init_data);
  187. void removeActiveObject(u16 id);
  188. void processActiveObjectMessage(u16 id, const std::string &data);
  189. /*
  190. Callbacks for activeobjects
  191. */
  192. void damageLocalPlayer(u8 damage);
  193. /*
  194. Client likes to call these
  195. */
  196. // Get all nearby objects
  197. void getActiveObjects(v3f origin, f32 max_d,
  198. core::array<DistanceSortedActiveObject> &dest);
  199. // Get event from queue. CEE_NONE is returned if queue is empty.
  200. ClientEnvEvent getClientEvent();
  201. private:
  202. ClientMap *m_map;
  203. scene::ISceneManager *m_smgr;
  204. core::map<u16, ClientActiveObject*> m_active_objects;
  205. Queue<ClientEnvEvent> m_client_event_queue;
  206. };
  207. #endif
  208. #endif