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"
39 #include "activeobject.h"
42 class ActiveBlockModifier;
43 class ServerActiveObject;
48 // Environment will delete the map passed to the constructor
50 virtual ~Environment();
53 Step everything in environment.
58 virtual void step(f32 dtime) = 0;
60 virtual Map & getMap() = 0;
62 virtual void addPlayer(Player *player);
63 void removePlayer(u16 peer_id);
64 Player * getPlayer(u16 peer_id);
65 Player * getPlayer(const char *name);
66 Player * getRandomConnectedPlayer();
67 Player * getNearestConnectedPlayer(v3f pos);
68 core::list<Player*> getPlayers();
69 core::list<Player*> getPlayers(bool ignore_disconnected);
70 void printPlayers(std::ostream &o);
72 //void setDayNightRatio(u32 r);
73 u32 getDayNightRatio();
76 virtual void setTimeOfDay(u32 time)
87 // peer_ids in here should be unique, except that there may be many 0s
88 core::list<Player*> m_players;
90 //u32 m_daynight_ratio;
91 // Time of day in milli-hours (0-23999); determines day and night
96 List of active blocks, used by ServerEnvironment
102 void update(core::list<v3s16> &active_positions,
104 core::map<v3s16, bool> &blocks_removed,
105 core::map<v3s16, bool> &blocks_added);
107 bool contains(v3s16 p){
108 return (m_list.find(p) != NULL);
115 core::map<v3s16, bool> m_list;
121 The server-side environment.
123 This is not thread-safe. Server uses an environment mutex.
126 class ServerEnvironment : public Environment
129 ServerEnvironment(ServerMap *map, Server *server);
130 ~ServerEnvironment();
137 ServerMap & getServerMap()
147 void step(f32 dtime);
152 void serializePlayers(const std::string &savedir);
153 void deSerializePlayers(const std::string &savedir);
156 Save and load time of day and game timer
158 void saveMeta(const std::string &savedir);
159 void loadMeta(const std::string &savedir);
162 External ActiveObject interface
163 -------------------------------------------
166 ServerActiveObject* getActiveObject(u16 id);
169 Add an active object to the environment.
170 Environment handles deletion of object.
171 Object may be deleted by environment immediately.
172 If id of object is 0, assigns a free id to it.
173 Returns the id of the object.
174 Returns 0 if not added and thus deleted.
176 u16 addActiveObject(ServerActiveObject *object);
179 Add an active object as a static object to the corresponding
181 Caller allocates memory, ServerEnvironment frees memory.
182 Return value: true if succeeded, false if failed.
184 bool addActiveObjectAsStatic(ServerActiveObject *object);
187 Find out what new objects have been added to
188 inside a radius around a position
190 void getAddedActiveObjects(v3s16 pos, s16 radius,
191 core::map<u16, bool> ¤t_objects,
192 core::map<u16, bool> &added_objects);
195 Find out what new objects have been removed from
196 inside a radius around a position
198 void getRemovedActiveObjects(v3s16 pos, s16 radius,
199 core::map<u16, bool> ¤t_objects,
200 core::map<u16, bool> &removed_objects);
203 Get the next message emitted by some active object.
204 Returns a message with id=0 if no messages are available.
206 ActiveObjectMessage getActiveObjectMessage();
209 Activate objects and dynamically modify for the dtime determined
210 from timestamp and additional_dtime
212 void activateBlock(MapBlock *block, u32 additional_dtime=0);
215 ActiveBlockModifiers (TODO)
216 -------------------------------------------
219 void addActiveBlockModifier(ActiveBlockModifier *abm);
224 Internal ActiveObject interface
225 -------------------------------------------
229 Add an active object to the environment.
231 Called by addActiveObject.
233 Object may be deleted by environment immediately.
234 If id of object is 0, assigns a free id to it.
235 Returns the id of the object.
236 Returns 0 if not added and thus deleted.
238 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed);
241 Remove all objects that satisfy (m_removed && m_known_by_count==0)
243 void removeRemovedObjects();
246 Convert stored objects from block to active
248 void activateObjects(MapBlock *block);
251 Convert objects that are not in active blocks to static.
253 If m_known_by_count != 0, active object is not deleted, but static
254 data is still updated.
256 If force_delete is set, active object is deleted nevertheless. It
257 shall only be set so in the destructor of the environment.
259 void deactivateFarObjects(bool force_delete);
267 // Pointer to server (which is handling this environment)
269 // Active object list
270 core::map<u16, ServerActiveObject*> m_active_objects;
271 // Outgoing network message buffer for active objects
272 Queue<ActiveObjectMessage> m_active_object_messages;
274 float m_random_spawn_timer; // used for experimental code
275 float m_send_recommended_timer;
276 IntervalLimiter m_object_management_interval;
277 // List of active blocks
278 ActiveBlockList m_active_blocks;
279 IntervalLimiter m_active_blocks_management_interval;
280 IntervalLimiter m_active_blocks_test_interval;
281 IntervalLimiter m_active_blocks_nodemetadata_interval;
282 // Time from the beginning of the game in seconds.
283 // Incremented in step().
285 // A helper variable for incrementing the latter
286 float m_game_time_fraction_counter;
290 Active block modifier interface.
292 These are fed into ServerEnvironment at initialization time;
293 ServerEnvironment handles deleting them.
296 class ActiveBlockModifier
299 ActiveBlockModifier(){};
300 virtual ~ActiveBlockModifier(){};
302 //virtual core::list<u8> update(ServerEnvironment *env) = 0;
303 virtual u32 getTriggerContentCount(){ return 1;}
304 virtual u8 getTriggerContent(u32 i) = 0;
305 virtual float getActiveInterval() = 0;
306 // chance of (1 / return value), 0 is disallowed
307 virtual u32 getActiveChance() = 0;
308 // This is called usually at interval for 1/chance of the nodes
309 virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
314 #include "clientobject.h"
317 The client-side environment.
319 This is not thread-safe.
320 Must be called from main (irrlicht) thread (uses the SceneManager)
321 Client uses an environment mutex.
324 enum ClientEnvEventType
330 struct ClientEnvEvent
332 ClientEnvEventType type;
342 class ClientEnvironment : public Environment
345 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
346 ~ClientEnvironment();
353 ClientMap & getClientMap()
358 void step(f32 dtime);
360 virtual void addPlayer(Player *player);
361 LocalPlayer * getLocalPlayer();
363 void updateMeshes(v3s16 blockpos);
364 void expireMeshes(bool only_daynight_diffed);
366 void setTimeOfDay(u32 time)
368 u32 old_dr = getDayNightRatio();
370 Environment::setTimeOfDay(time);
372 if(getDayNightRatio() != old_dr)
374 dout_client<<DTIME<<"ClientEnvironment: DayNightRatio changed"
375 <<" -> expiring meshes"<<std::endl;
384 ClientActiveObject* getActiveObject(u16 id);
387 Adds an active object to the environment.
388 Environment handles deletion of object.
389 Object may be deleted by environment immediately.
390 If id of object is 0, assigns a free id to it.
391 Returns the id of the object.
392 Returns 0 if not added and thus deleted.
394 u16 addActiveObject(ClientActiveObject *object);
396 void addActiveObject(u16 id, u8 type, const std::string &init_data);
397 void removeActiveObject(u16 id);
399 void processActiveObjectMessage(u16 id, const std::string &data);
402 Callbacks for activeobjects
405 void damageLocalPlayer(u8 damage);
408 Client likes to call these
411 // Get all nearby objects
412 void getActiveObjects(v3f origin, f32 max_d,
413 core::array<DistanceSortedActiveObject> &dest);
415 // Get event from queue. CEE_NONE is returned if queue is empty.
416 ClientEnvEvent getClientEvent();
420 scene::ISceneManager *m_smgr;
421 core::map<u16, ClientActiveObject*> m_active_objects;
422 Queue<ClientEnvEvent> m_client_event_queue;
423 IntervalLimiter m_active_object_light_update_interval;
424 IntervalLimiter m_lava_hurt_interval;