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 virtual std::set<std::string> getTriggerContents()=0;
112 virtual float getTriggerInterval() = 0;
113 // chance of (1 / return value), 0 is disallowed
114 virtual u32 getTriggerChance() = 0;
115 // This is called usually at interval for 1/chance of the nodes
116 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
117 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
118 u32 active_object_count, u32 active_object_count_wider){};
123 ActiveBlockModifier *abm;
126 ABMWithState(ActiveBlockModifier *abm_):
132 List of active blocks, used by ServerEnvironment
135 class ActiveBlockList
138 void update(core::list<v3s16> &active_positions,
140 core::map<v3s16, bool> &blocks_removed,
141 core::map<v3s16, bool> &blocks_added);
143 bool contains(v3s16 p){
144 return (m_list.find(p) != NULL);
151 core::map<v3s16, bool> m_list;
156 class IBackgroundBlockEmerger
159 virtual void queueBlockEmerge(v3s16 blockpos, bool allow_generate)=0;
163 The server-side environment.
165 This is not thread-safe. Server uses an environment mutex.
168 class ServerEnvironment : public Environment
171 ServerEnvironment(ServerMap *map, lua_State *L, IGameDef *gamedef,
172 IBackgroundBlockEmerger *emerger);
173 ~ServerEnvironment();
178 ServerMap & getServerMap()
184 IGameDef *getGameDef()
185 { return m_gamedef; }
187 float getSendRecommendedInterval()
195 void serializePlayers(const std::string &savedir);
196 void deSerializePlayers(const std::string &savedir);
199 Save and load time of day and game timer
201 void saveMeta(const std::string &savedir);
202 void loadMeta(const std::string &savedir);
205 External ActiveObject interface
206 -------------------------------------------
209 ServerActiveObject* getActiveObject(u16 id);
212 Add an active object to the environment.
213 Environment handles deletion of object.
214 Object may be deleted by environment immediately.
215 If id of object is 0, assigns a free id to it.
216 Returns the id of the object.
217 Returns 0 if not added and thus deleted.
219 u16 addActiveObject(ServerActiveObject *object);
222 Add an active object as a static object to the corresponding
224 Caller allocates memory, ServerEnvironment frees memory.
225 Return value: true if succeeded, false if failed.
227 bool addActiveObjectAsStatic(ServerActiveObject *object);
230 Find out what new objects have been added to
231 inside a radius around a position
233 void getAddedActiveObjects(v3s16 pos, s16 radius,
234 core::map<u16, bool> ¤t_objects,
235 core::map<u16, bool> &added_objects);
238 Find out what new objects have been removed from
239 inside a radius around a position
241 void getRemovedActiveObjects(v3s16 pos, s16 radius,
242 core::map<u16, bool> ¤t_objects,
243 core::map<u16, bool> &removed_objects);
246 Get the next message emitted by some active object.
247 Returns a message with id=0 if no messages are available.
249 ActiveObjectMessage getActiveObjectMessage();
252 Activate objects and dynamically modify for the dtime determined
253 from timestamp and additional_dtime
255 void activateBlock(MapBlock *block, u32 additional_dtime=0);
258 ActiveBlockModifiers (TODO)
259 -------------------------------------------
260 NOTE: Not used currently (TODO: Use or remove)
263 void addActiveBlockModifier(ActiveBlockModifier *abm);
267 // Clear all objects, loading and going through every MapBlock
268 void clearAllObjects();
270 void step(f32 dtime);
275 Internal ActiveObject interface
276 -------------------------------------------
280 Add an active object to the environment.
282 Called by addActiveObject.
284 Object may be deleted by environment immediately.
285 If id of object is 0, assigns a free id to it.
286 Returns the id of the object.
287 Returns 0 if not added and thus deleted.
289 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed);
292 Remove all objects that satisfy (m_removed && m_known_by_count==0)
294 void removeRemovedObjects();
297 Convert stored objects from block to active
299 void activateObjects(MapBlock *block);
302 Convert objects that are not in active blocks to static.
304 If m_known_by_count != 0, active object is not deleted, but static
305 data is still updated.
307 If force_delete is set, active object is deleted nevertheless. It
308 shall only be set so in the destructor of the environment.
310 void deactivateFarObjects(bool force_delete);
322 // Background block emerger (the server, in practice)
323 IBackgroundBlockEmerger *m_emerger;
324 // Active object list
325 core::map<u16, ServerActiveObject*> m_active_objects;
326 // Outgoing network message buffer for active objects
327 Queue<ActiveObjectMessage> m_active_object_messages;
329 float m_random_spawn_timer; // used for experimental code
330 float m_send_recommended_timer;
331 IntervalLimiter m_object_management_interval;
332 // List of active blocks
333 ActiveBlockList m_active_blocks;
334 IntervalLimiter m_active_blocks_management_interval;
335 IntervalLimiter m_active_block_modifier_interval;
336 IntervalLimiter m_active_blocks_nodemetadata_interval;
337 // Time from the beginning of the game in seconds.
338 // Incremented in step().
340 // A helper variable for incrementing the latter
341 float m_game_time_fraction_counter;
342 core::list<ABMWithState> m_abms;
347 #include "clientobject.h"
350 The client-side environment.
352 This is not thread-safe.
353 Must be called from main (irrlicht) thread (uses the SceneManager)
354 Client uses an environment mutex.
357 enum ClientEnvEventType
363 struct ClientEnvEvent
365 ClientEnvEventType type;
375 class ClientEnvironment : public Environment
378 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
379 ITextureSource *texturesource, IGameDef *gamedef);
380 ~ClientEnvironment();
385 ClientMap & getClientMap()
388 IGameDef *getGameDef()
389 { return m_gamedef; }
391 void step(f32 dtime);
393 virtual void addPlayer(Player *player);
394 LocalPlayer * getLocalPlayer();
396 // Slightly deprecated
397 void updateMeshes(v3s16 blockpos);
398 void expireMeshes(bool only_daynight_diffed);
400 void setTimeOfDay(u32 time)
402 u32 old_dr = getDayNightRatio();
404 Environment::setTimeOfDay(time);
406 if(getDayNightRatio() != old_dr)
408 /*infostream<<"ClientEnvironment: DayNightRatio changed"
409 <<" -> expiring meshes"<<std::endl;*/
418 ClientActiveObject* getActiveObject(u16 id);
421 Adds an active object to the environment.
422 Environment handles deletion of object.
423 Object may be deleted by environment immediately.
424 If id of object is 0, assigns a free id to it.
425 Returns the id of the object.
426 Returns 0 if not added and thus deleted.
428 u16 addActiveObject(ClientActiveObject *object);
430 void addActiveObject(u16 id, u8 type, const std::string &init_data);
431 void removeActiveObject(u16 id);
433 void processActiveObjectMessage(u16 id, const std::string &data);
436 Callbacks for activeobjects
439 void damageLocalPlayer(u8 damage);
442 Client likes to call these
445 // Get all nearby objects
446 void getActiveObjects(v3f origin, f32 max_d,
447 core::array<DistanceSortedActiveObject> &dest);
449 // Get event from queue. CEE_NONE is returned if queue is empty.
450 ClientEnvEvent getClientEvent();
454 scene::ISceneManager *m_smgr;
455 ITextureSource *m_texturesource;
457 core::map<u16, ClientActiveObject*> m_active_objects;
458 Queue<ClientEnvEvent> m_client_event_queue;
459 IntervalLimiter m_active_object_light_update_interval;
460 IntervalLimiter m_lava_hurt_interval;