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 virtual void setTimeOfDay(u32 time)
82 // peer_ids in here should be unique, except that there may be many 0s
83 core::list<Player*> m_players;
85 //u32 m_daynight_ratio;
86 // Time of day in milli-hours (0-23999); determines day and night
91 List of active blocks, used by ServerEnvironment
97 void update(core::list<v3s16> &active_positions,
99 core::map<v3s16, bool> &blocks_removed,
100 core::map<v3s16, bool> &blocks_added);
102 bool contains(v3s16 p){
103 return (m_list.find(p) != NULL);
110 core::map<v3s16, bool> m_list;
116 Active block modifier interface
119 class ActiveBlockModifier
122 ActiveBlockModifier(){};
123 virtual ~ActiveBlockModifier(){};
129 The server-side environment.
131 This is not thread-safe. Server uses an environment mutex.
134 #include "serverobject.h"
138 class ServerEnvironment : public Environment
141 ServerEnvironment(ServerMap *map, Server *server);
142 ~ServerEnvironment();
149 ServerMap & getServerMap()
159 void step(f32 dtime);
164 void serializePlayers(const std::string &savedir);
165 void deSerializePlayers(const std::string &savedir);
168 Save and load time of day and game timer
170 void saveMeta(const std::string &savedir);
171 void loadMeta(const std::string &savedir);
177 ServerActiveObject* getActiveObject(u16 id);
180 Add an active object to the environment.
181 Environment handles deletion of object.
182 Object may be deleted by environment immediately.
183 If id of object is 0, assigns a free id to it.
184 Returns the id of the object.
185 Returns 0 if not added and thus deleted.
187 u16 addActiveObject(ServerActiveObject *object);
190 Find out what new objects have been added to
191 inside a radius around a position
193 void getAddedActiveObjects(v3s16 pos, s16 radius,
194 core::map<u16, bool> ¤t_objects,
195 core::map<u16, bool> &added_objects);
198 Find out what new objects have been removed from
199 inside a radius around a position
201 void getRemovedActiveObjects(v3s16 pos, s16 radius,
202 core::map<u16, bool> ¤t_objects,
203 core::map<u16, bool> &removed_objects);
206 Get the next message emitted by some active object.
207 Returns a message with id=0 if no messages are available.
209 ActiveObjectMessage getActiveObjectMessage();
213 Remove all objects that satisfy (m_removed && m_known_by_count==0)
215 void removeRemovedObjects();
218 Convert stored objects from block to active
220 void activateObjects(MapBlock *block);
223 Convert objects that are not in active blocks to static.
225 If m_known_by_count != 0, active object is not deleted, but static
226 data is still updated.
228 If force_delete is set, active object is deleted nevertheless. It
229 shall only be set so in the destructor of the environment.
231 void deactivateFarObjects(bool force_delete);
239 // Pointer to server (which is handling this environment)
241 // Active object list
242 core::map<u16, ServerActiveObject*> m_active_objects;
243 // Outgoing network message buffer for active objects
244 Queue<ActiveObjectMessage> m_active_object_messages;
246 float m_random_spawn_timer; // used for experimental code
247 float m_send_recommended_timer;
248 IntervalLimiter m_object_management_interval;
249 // List of active blocks
250 ActiveBlockList m_active_blocks;
251 IntervalLimiter m_active_blocks_management_interval;
252 IntervalLimiter m_active_blocks_test_interval;
253 // Time from the beginning of the game in seconds.
254 // Incremented in step().
256 // A helper variable for incrementing the latter
257 float m_game_time_fraction_counter;
262 #include "clientobject.h"
265 The client-side environment.
267 This is not thread-safe.
268 Must be called from main (irrlicht) thread (uses the SceneManager)
269 Client uses an environment mutex.
272 enum ClientEnvEventType
278 struct ClientEnvEvent
280 ClientEnvEventType type;
290 class ClientEnvironment : public Environment
293 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
294 ~ClientEnvironment();
301 ClientMap & getClientMap()
306 void step(f32 dtime);
308 virtual void addPlayer(Player *player);
309 LocalPlayer * getLocalPlayer();
311 void updateMeshes(v3s16 blockpos);
312 void expireMeshes(bool only_daynight_diffed);
314 void setTimeOfDay(u32 time)
316 u32 old_dr = getDayNightRatio();
318 Environment::setTimeOfDay(time);
320 if(getDayNightRatio() != old_dr)
322 dout_client<<DTIME<<"ClientEnvironment: DayNightRatio changed"
323 <<" -> expiring meshes"<<std::endl;
332 ClientActiveObject* getActiveObject(u16 id);
335 Adds an active object to the environment.
336 Environment handles deletion of object.
337 Object may be deleted by environment immediately.
338 If id of object is 0, assigns a free id to it.
339 Returns the id of the object.
340 Returns 0 if not added and thus deleted.
342 u16 addActiveObject(ClientActiveObject *object);
344 void addActiveObject(u16 id, u8 type, const std::string &init_data);
345 void removeActiveObject(u16 id);
347 void processActiveObjectMessage(u16 id, const std::string &data);
350 Callbacks for activeobjects
353 void damageLocalPlayer(u8 damage);
356 Client likes to call these
359 // Get all nearby objects
360 void getActiveObjects(v3f origin, f32 max_d,
361 core::array<DistanceSortedActiveObject> &dest);
363 // Get event from queue. CEE_NONE is returned if queue is empty.
364 ClientEnvEvent getClientEvent();
368 scene::ISceneManager *m_smgr;
369 core::map<u16, ClientActiveObject*> m_active_objects;
370 Queue<ClientEnvEvent> m_client_event_queue;