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;
44 typedef struct lua_State lua_State;
49 // Environment will delete the map passed to the constructor
51 virtual ~Environment();
54 Step everything in environment.
59 virtual void step(f32 dtime) = 0;
61 virtual Map & getMap() = 0;
63 virtual void addPlayer(Player *player);
64 void removePlayer(u16 peer_id);
65 Player * getPlayer(u16 peer_id);
66 Player * getPlayer(const char *name);
67 Player * getRandomConnectedPlayer();
68 Player * getNearestConnectedPlayer(v3f pos);
69 core::list<Player*> getPlayers();
70 core::list<Player*> getPlayers(bool ignore_disconnected);
71 void printPlayers(std::ostream &o);
73 //void setDayNightRatio(u32 r);
74 u32 getDayNightRatio();
77 virtual void setTimeOfDay(u32 time)
88 // peer_ids in here should be unique, except that there may be many 0s
89 core::list<Player*> m_players;
91 //u32 m_daynight_ratio;
92 // Time of day in milli-hours (0-23999); determines day and night
97 List of active blocks, used by ServerEnvironment
100 class ActiveBlockList
103 void update(core::list<v3s16> &active_positions,
105 core::map<v3s16, bool> &blocks_removed,
106 core::map<v3s16, bool> &blocks_added);
108 bool contains(v3s16 p){
109 return (m_list.find(p) != NULL);
116 core::map<v3s16, bool> m_list;
122 The server-side environment.
124 This is not thread-safe. Server uses an environment mutex.
127 class ServerEnvironment : public Environment
130 ServerEnvironment(ServerMap *map, lua_State *L);
131 ~ServerEnvironment();
138 ServerMap & getServerMap()
148 float getSendRecommendedInterval()
156 void serializePlayers(const std::string &savedir);
157 void deSerializePlayers(const std::string &savedir);
160 Save and load time of day and game timer
162 void saveMeta(const std::string &savedir);
163 void loadMeta(const std::string &savedir);
166 External ActiveObject interface
167 -------------------------------------------
170 ServerActiveObject* getActiveObject(u16 id);
173 Add an active object to the environment.
174 Environment handles deletion of object.
175 Object may be deleted by environment immediately.
176 If id of object is 0, assigns a free id to it.
177 Returns the id of the object.
178 Returns 0 if not added and thus deleted.
180 u16 addActiveObject(ServerActiveObject *object);
183 Add an active object as a static object to the corresponding
185 Caller allocates memory, ServerEnvironment frees memory.
186 Return value: true if succeeded, false if failed.
188 bool addActiveObjectAsStatic(ServerActiveObject *object);
191 Find out what new objects have been added to
192 inside a radius around a position
194 void getAddedActiveObjects(v3s16 pos, s16 radius,
195 core::map<u16, bool> ¤t_objects,
196 core::map<u16, bool> &added_objects);
199 Find out what new objects have been removed from
200 inside a radius around a position
202 void getRemovedActiveObjects(v3s16 pos, s16 radius,
203 core::map<u16, bool> ¤t_objects,
204 core::map<u16, bool> &removed_objects);
207 Get the next message emitted by some active object.
208 Returns a message with id=0 if no messages are available.
210 ActiveObjectMessage getActiveObjectMessage();
213 Activate objects and dynamically modify for the dtime determined
214 from timestamp and additional_dtime
216 void activateBlock(MapBlock *block, u32 additional_dtime=0);
219 ActiveBlockModifiers (TODO)
220 -------------------------------------------
223 void addActiveBlockModifier(ActiveBlockModifier *abm);
227 // Clear all objects, loading and going through every MapBlock
228 void clearAllObjects();
230 void step(f32 dtime);
235 Internal ActiveObject interface
236 -------------------------------------------
240 Add an active object to the environment.
242 Called by addActiveObject.
244 Object may be deleted by environment immediately.
245 If id of object is 0, assigns a free id to it.
246 Returns the id of the object.
247 Returns 0 if not added and thus deleted.
249 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed);
252 Remove all objects that satisfy (m_removed && m_known_by_count==0)
254 void removeRemovedObjects();
257 Convert stored objects from block to active
259 void activateObjects(MapBlock *block);
262 Convert objects that are not in active blocks to static.
264 If m_known_by_count != 0, active object is not deleted, but static
265 data is still updated.
267 If force_delete is set, active object is deleted nevertheless. It
268 shall only be set so in the destructor of the environment.
270 void deactivateFarObjects(bool force_delete);
280 // Active object list
281 core::map<u16, ServerActiveObject*> m_active_objects;
282 // Outgoing network message buffer for active objects
283 Queue<ActiveObjectMessage> m_active_object_messages;
285 float m_random_spawn_timer; // used for experimental code
286 float m_send_recommended_timer;
287 IntervalLimiter m_object_management_interval;
288 // List of active blocks
289 ActiveBlockList m_active_blocks;
290 IntervalLimiter m_active_blocks_management_interval;
291 IntervalLimiter m_active_blocks_test_interval;
292 IntervalLimiter m_active_blocks_nodemetadata_interval;
293 // Time from the beginning of the game in seconds.
294 // Incremented in step().
296 // A helper variable for incrementing the latter
297 float m_game_time_fraction_counter;
301 Active block modifier interface.
303 These are fed into ServerEnvironment at initialization time;
304 ServerEnvironment handles deleting them.
307 class ActiveBlockModifier
310 ActiveBlockModifier(){};
311 virtual ~ActiveBlockModifier(){};
313 //virtual core::list<u8> update(ServerEnvironment *env) = 0;
314 virtual u32 getTriggerContentCount(){ return 1;}
315 virtual u8 getTriggerContent(u32 i) = 0;
316 virtual float getActiveInterval() = 0;
317 // chance of (1 / return value), 0 is disallowed
318 virtual u32 getActiveChance() = 0;
319 // This is called usually at interval for 1/chance of the nodes
320 virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
325 #include "clientobject.h"
328 The client-side environment.
330 This is not thread-safe.
331 Must be called from main (irrlicht) thread (uses the SceneManager)
332 Client uses an environment mutex.
335 enum ClientEnvEventType
341 struct ClientEnvEvent
343 ClientEnvEventType type;
353 class ClientEnvironment : public Environment
356 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
357 ~ClientEnvironment();
364 ClientMap & getClientMap()
369 void step(f32 dtime);
371 virtual void addPlayer(Player *player);
372 LocalPlayer * getLocalPlayer();
374 void updateMeshes(v3s16 blockpos);
375 void expireMeshes(bool only_daynight_diffed);
377 void setTimeOfDay(u32 time)
379 u32 old_dr = getDayNightRatio();
381 Environment::setTimeOfDay(time);
383 if(getDayNightRatio() != old_dr)
385 dout_client<<DTIME<<"ClientEnvironment: DayNightRatio changed"
386 <<" -> expiring meshes"<<std::endl;
395 ClientActiveObject* getActiveObject(u16 id);
398 Adds an active object to the environment.
399 Environment handles deletion of object.
400 Object may be deleted by environment immediately.
401 If id of object is 0, assigns a free id to it.
402 Returns the id of the object.
403 Returns 0 if not added and thus deleted.
405 u16 addActiveObject(ClientActiveObject *object);
407 void addActiveObject(u16 id, u8 type, const std::string &init_data);
408 void removeActiveObject(u16 id);
410 void processActiveObjectMessage(u16 id, const std::string &data);
413 Callbacks for activeobjects
416 void damageLocalPlayer(u8 damage);
419 Client likes to call these
422 // Get all nearby objects
423 void getActiveObjects(v3f origin, f32 max_d,
424 core::array<DistanceSortedActiveObject> &dest);
426 // Get event from queue. CEE_NONE is returned if queue is empty.
427 ClientEnvEvent getClientEvent();
431 scene::ISceneManager *m_smgr;
432 core::map<u16, ClientActiveObject*> m_active_objects;
433 Queue<ClientEnvEvent> m_client_event_queue;
434 IntervalLimiter m_active_object_light_update_interval;
435 IntervalLimiter m_lava_hurt_interval;