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
34 #include "common_irrlicht.h"
39 #include "activeobject.h"
42 class ActiveBlockModifier;
43 class ServerActiveObject;
44 typedef struct lua_State lua_State;
51 // Environment will delete the map passed to the constructor
53 virtual ~Environment();
56 Step everything in environment.
61 virtual void step(f32 dtime) = 0;
63 virtual Map & getMap() = 0;
65 virtual void addPlayer(Player *player);
66 void removePlayer(u16 peer_id);
67 Player * getPlayer(u16 peer_id);
68 Player * getPlayer(const char *name);
69 Player * getRandomConnectedPlayer();
70 Player * getNearestConnectedPlayer(v3f pos);
71 core::list<Player*> getPlayers();
72 core::list<Player*> getPlayers(bool ignore_disconnected);
73 void printPlayers(std::ostream &o);
75 //void setDayNightRatio(u32 r);
76 u32 getDayNightRatio();
79 virtual void setTimeOfDay(u32 time)
90 // peer_ids in here should be unique, except that there may be many 0s
91 core::list<Player*> m_players;
93 //u32 m_daynight_ratio;
94 // Time of day in milli-hours (0-23999); determines day and night
99 Active block modifier interface.
101 These are fed into ServerEnvironment at initialization time;
102 ServerEnvironment handles deleting them.
105 class ActiveBlockModifier
108 ActiveBlockModifier(){};
109 virtual ~ActiveBlockModifier(){};
111 // Set of contents to trigger on
112 virtual std::set<std::string> getTriggerContents()=0;
113 // Set of required neighbors (trigger doesn't happen if none are found)
114 // Empty = do not check neighbors
115 virtual std::set<std::string> getRequiredNeighbors()
116 { return std::set<std::string>(); }
117 // Trigger interval in seconds
118 virtual float getTriggerInterval() = 0;
119 // Random chance of (1 / return value), 0 is disallowed
120 virtual u32 getTriggerChance() = 0;
121 // This is called usually at interval for 1/chance of the nodes
122 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
123 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
124 u32 active_object_count, u32 active_object_count_wider){};
129 ActiveBlockModifier *abm;
132 ABMWithState(ActiveBlockModifier *abm_):
138 List of active blocks, used by ServerEnvironment
141 class ActiveBlockList
144 void update(core::list<v3s16> &active_positions,
146 core::map<v3s16, bool> &blocks_removed,
147 core::map<v3s16, bool> &blocks_added);
149 bool contains(v3s16 p){
150 return (m_list.find(p) != NULL);
157 core::map<v3s16, bool> m_list;
162 class IBackgroundBlockEmerger
165 virtual void queueBlockEmerge(v3s16 blockpos, bool allow_generate)=0;
169 The server-side environment.
171 This is not thread-safe. Server uses an environment mutex.
174 class ServerEnvironment : public Environment
177 ServerEnvironment(ServerMap *map, lua_State *L, IGameDef *gamedef,
178 IBackgroundBlockEmerger *emerger);
179 ~ServerEnvironment();
184 ServerMap & getServerMap()
190 IGameDef *getGameDef()
191 { return m_gamedef; }
193 float getSendRecommendedInterval()
201 void serializePlayers(const std::string &savedir);
202 void deSerializePlayers(const std::string &savedir);
205 Save and load time of day and game timer
207 void saveMeta(const std::string &savedir);
208 void loadMeta(const std::string &savedir);
211 External ActiveObject interface
212 -------------------------------------------
215 ServerActiveObject* getActiveObject(u16 id);
218 Add an active object to the environment.
219 Environment handles deletion of object.
220 Object may be deleted by environment immediately.
221 If id of object is 0, assigns a free id to it.
222 Returns the id of the object.
223 Returns 0 if not added and thus deleted.
225 u16 addActiveObject(ServerActiveObject *object);
228 Add an active object as a static object to the corresponding
230 Caller allocates memory, ServerEnvironment frees memory.
231 Return value: true if succeeded, false if failed.
233 bool addActiveObjectAsStatic(ServerActiveObject *object);
236 Find out what new objects have been added to
237 inside a radius around a position
239 void getAddedActiveObjects(v3s16 pos, s16 radius,
240 core::map<u16, bool> ¤t_objects,
241 core::map<u16, bool> &added_objects);
244 Find out what new objects have been removed from
245 inside a radius around a position
247 void getRemovedActiveObjects(v3s16 pos, s16 radius,
248 core::map<u16, bool> ¤t_objects,
249 core::map<u16, bool> &removed_objects);
252 Get the next message emitted by some active object.
253 Returns a message with id=0 if no messages are available.
255 ActiveObjectMessage getActiveObjectMessage();
258 Activate objects and dynamically modify for the dtime determined
259 from timestamp and additional_dtime
261 void activateBlock(MapBlock *block, u32 additional_dtime=0);
264 ActiveBlockModifiers (TODO)
265 -------------------------------------------
266 NOTE: Not used currently (TODO: Use or remove)
269 void addActiveBlockModifier(ActiveBlockModifier *abm);
273 // Clear all objects, loading and going through every MapBlock
274 void clearAllObjects();
276 void step(f32 dtime);
281 Internal ActiveObject interface
282 -------------------------------------------
286 Add an active object to the environment.
288 Called by addActiveObject.
290 Object may be deleted by environment immediately.
291 If id of object is 0, assigns a free id to it.
292 Returns the id of the object.
293 Returns 0 if not added and thus deleted.
295 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed);
298 Remove all objects that satisfy (m_removed && m_known_by_count==0)
300 void removeRemovedObjects();
303 Convert stored objects from block to active
305 void activateObjects(MapBlock *block);
308 Convert objects that are not in active blocks to static.
310 If m_known_by_count != 0, active object is not deleted, but static
311 data is still updated.
313 If force_delete is set, active object is deleted nevertheless. It
314 shall only be set so in the destructor of the environment.
316 void deactivateFarObjects(bool force_delete);
328 // Background block emerger (the server, in practice)
329 IBackgroundBlockEmerger *m_emerger;
330 // Active object list
331 core::map<u16, ServerActiveObject*> m_active_objects;
332 // Outgoing network message buffer for active objects
333 Queue<ActiveObjectMessage> m_active_object_messages;
335 float m_random_spawn_timer; // used for experimental code
336 float m_send_recommended_timer;
337 IntervalLimiter m_object_management_interval;
338 // List of active blocks
339 ActiveBlockList m_active_blocks;
340 IntervalLimiter m_active_blocks_management_interval;
341 IntervalLimiter m_active_block_modifier_interval;
342 IntervalLimiter m_active_blocks_nodemetadata_interval;
343 // Time from the beginning of the game in seconds.
344 // Incremented in step().
346 // A helper variable for incrementing the latter
347 float m_game_time_fraction_counter;
348 core::list<ABMWithState> m_abms;
353 #include "clientobject.h"
356 The client-side environment.
358 This is not thread-safe.
359 Must be called from main (irrlicht) thread (uses the SceneManager)
360 Client uses an environment mutex.
363 enum ClientEnvEventType
369 struct ClientEnvEvent
371 ClientEnvEventType type;
382 class ClientEnvironment : public Environment
385 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
386 ITextureSource *texturesource, IGameDef *gamedef,
387 IrrlichtDevice *device);
388 ~ClientEnvironment();
393 ClientMap & getClientMap()
396 IGameDef *getGameDef()
397 { return m_gamedef; }
399 void step(f32 dtime);
401 virtual void addPlayer(Player *player);
402 LocalPlayer * getLocalPlayer();
404 // Slightly deprecated
405 void updateMeshes(v3s16 blockpos);
406 void expireMeshes(bool only_daynight_diffed);
408 void setTimeOfDay(u32 time)
410 u32 old_dr = getDayNightRatio();
412 Environment::setTimeOfDay(time);
414 if(getDayNightRatio() != old_dr)
416 /*infostream<<"ClientEnvironment: DayNightRatio changed"
417 <<" -> expiring meshes"<<std::endl;*/
426 ClientActiveObject* getActiveObject(u16 id);
429 Adds an active object to the environment.
430 Environment handles deletion of object.
431 Object may be deleted by environment immediately.
432 If id of object is 0, assigns a free id to it.
433 Returns the id of the object.
434 Returns 0 if not added and thus deleted.
436 u16 addActiveObject(ClientActiveObject *object);
438 void addActiveObject(u16 id, u8 type, const std::string &init_data);
439 void removeActiveObject(u16 id);
441 void processActiveObjectMessage(u16 id, const std::string &data);
444 Callbacks for activeobjects
447 void damageLocalPlayer(u8 damage, bool handle_hp=true);
450 Client likes to call these
453 // Get all nearby objects
454 void getActiveObjects(v3f origin, f32 max_d,
455 core::array<DistanceSortedActiveObject> &dest);
457 // Get event from queue. CEE_NONE is returned if queue is empty.
458 ClientEnvEvent getClientEvent();
462 scene::ISceneManager *m_smgr;
463 ITextureSource *m_texturesource;
465 IrrlichtDevice *m_irr;
466 core::map<u16, ClientActiveObject*> m_active_objects;
467 Queue<ClientEnvEvent> m_client_event_queue;
468 IntervalLimiter m_active_object_light_update_interval;
469 IntervalLimiter m_lava_hurt_interval;