3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef ENVIRONMENT_HEADER
21 #define ENVIRONMENT_HEADER
24 This class is the game's environment.
29 - The current time in the game (actually it only contains the brightness)
34 #include "common_irrlicht.h"
43 // Environment will delete the map passed to the constructor
45 virtual ~Environment();
48 Step everything in environment.
53 virtual void step(f32 dtime) = 0;
55 virtual Map & getMap() = 0;
57 virtual void addPlayer(Player *player);
58 void removePlayer(u16 peer_id);
59 Player * getPlayer(u16 peer_id);
60 Player * getPlayer(const char *name);
61 Player * getRandomConnectedPlayer();
62 Player * getNearestConnectedPlayer(v3f pos);
63 core::list<Player*> getPlayers();
64 core::list<Player*> getPlayers(bool ignore_disconnected);
65 void printPlayers(std::ostream &o);
67 void setDayNightRatio(u32 r);
68 u32 getDayNightRatio();
71 // peer_ids in here should be unique, except that there may be many 0s
72 core::list<Player*> m_players;
78 The server-side environment.
80 This is not thread-safe. Server uses an environment mutex.
83 #include "serverobject.h"
87 class ServerEnvironment : public Environment
90 ServerEnvironment(ServerMap *map, Server *server);
98 ServerMap & getServerMap()
108 void step(f32 dtime);
110 void serializePlayers(const std::string &savedir);
111 void deSerializePlayers(const std::string &savedir);
117 ServerActiveObject* getActiveObject(u16 id);
120 Add an active object to the environment.
121 Environment handles deletion of object.
122 Object may be deleted by environment immediately.
123 If id of object is 0, assigns a free id to it.
124 Returns the id of the object.
125 Returns 0 if not added and thus deleted.
127 u16 addActiveObject(ServerActiveObject *object);
130 Find out what new objects have been added to
131 inside a radius around a position
133 void getAddedActiveObjects(v3s16 pos, s16 radius,
134 core::map<u16, bool> ¤t_objects,
135 core::map<u16, bool> &added_objects);
138 Find out what new objects have been removed from
139 inside a radius around a position
141 void getRemovedActiveObjects(v3s16 pos, s16 radius,
142 core::map<u16, bool> ¤t_objects,
143 core::map<u16, bool> &removed_objects);
146 Get the next message emitted by some active object.
147 Returns a message with id=0 if no messages are available.
149 ActiveObjectMessage getActiveObjectMessage();
153 Remove all objects that satisfy (m_removed && m_known_by_count==0)
155 void removeRemovedObjects();
157 Convert stored objects from blocks near the players to active.
159 void activateNearObjects(s16 range_blocks);
161 Convert objects that are far away from all the players to static.
163 If range_blocks == -1, convert everything to static even if known
166 void deactivateFarObjects(s16 range_blocks);
170 core::map<u16, ServerActiveObject*> m_active_objects;
171 Queue<ActiveObjectMessage> m_active_object_messages;
172 float m_random_spawn_timer;
173 float m_send_recommended_timer;
174 IntervalLimiter m_object_management_interval;
179 #include "clientobject.h"
182 The client-side environment.
184 This is not thread-safe.
185 Must be called from main (irrlicht) thread (uses the SceneManager)
186 Client uses an environment mutex.
189 enum ClientEnvEventType
195 struct ClientEnvEvent
197 ClientEnvEventType type;
207 class ClientEnvironment : public Environment
210 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
211 ~ClientEnvironment();
218 ClientMap & getClientMap()
223 void step(f32 dtime);
225 virtual void addPlayer(Player *player);
226 LocalPlayer * getLocalPlayer();
228 void updateMeshes(v3s16 blockpos);
229 void expireMeshes(bool only_daynight_diffed);
235 ClientActiveObject* getActiveObject(u16 id);
238 Adds an active object to the environment.
239 Environment handles deletion of object.
240 Object may be deleted by environment immediately.
241 If id of object is 0, assigns a free id to it.
242 Returns the id of the object.
243 Returns 0 if not added and thus deleted.
245 u16 addActiveObject(ClientActiveObject *object);
247 void addActiveObject(u16 id, u8 type, const std::string &init_data);
248 void removeActiveObject(u16 id);
250 void processActiveObjectMessage(u16 id, const std::string &data);
253 Callbacks for activeobjects
256 void damageLocalPlayer(u8 damage);
259 Client likes to call these
262 // Get all nearby objects
263 void getActiveObjects(v3f origin, f32 max_d,
264 core::array<DistanceSortedActiveObject> &dest);
266 // Get event from queue. CEE_NONE is returned if queue is empty.
267 ClientEnvEvent getClientEvent();
271 scene::ISceneManager *m_smgr;
272 core::map<u16, ClientActiveObject*> m_active_objects;
273 Queue<ClientEnvEvent> m_client_event_queue;