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);
265 -------------------------------------------
268 void addActiveBlockModifier(ActiveBlockModifier *abm);
272 -------------------------------------------
275 // Find all active objects inside a radius around a point
276 std::set<u16> getObjectsInsideRadius(v3f pos, float radius);
278 // Clear all objects, loading and going through every MapBlock
279 void clearAllObjects();
281 // This makes stuff happen
282 void step(f32 dtime);
287 Internal ActiveObject interface
288 -------------------------------------------
292 Add an active object to the environment.
294 Called by addActiveObject.
296 Object may be deleted by environment immediately.
297 If id of object is 0, assigns a free id to it.
298 Returns the id of the object.
299 Returns 0 if not added and thus deleted.
301 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed);
304 Remove all objects that satisfy (m_removed && m_known_by_count==0)
306 void removeRemovedObjects();
309 Convert stored objects from block to active
311 void activateObjects(MapBlock *block);
314 Convert objects that are not in active blocks to static.
316 If m_known_by_count != 0, active object is not deleted, but static
317 data is still updated.
319 If force_delete is set, active object is deleted nevertheless. It
320 shall only be set so in the destructor of the environment.
322 void deactivateFarObjects(bool force_delete);
334 // Background block emerger (the server, in practice)
335 IBackgroundBlockEmerger *m_emerger;
336 // Active object list
337 core::map<u16, ServerActiveObject*> m_active_objects;
338 // Outgoing network message buffer for active objects
339 Queue<ActiveObjectMessage> m_active_object_messages;
341 float m_random_spawn_timer; // used for experimental code
342 float m_send_recommended_timer;
343 IntervalLimiter m_object_management_interval;
344 // List of active blocks
345 ActiveBlockList m_active_blocks;
346 IntervalLimiter m_active_blocks_management_interval;
347 IntervalLimiter m_active_block_modifier_interval;
348 IntervalLimiter m_active_blocks_nodemetadata_interval;
349 // Time from the beginning of the game in seconds.
350 // Incremented in step().
352 // A helper variable for incrementing the latter
353 float m_game_time_fraction_counter;
354 core::list<ABMWithState> m_abms;
359 #include "clientobject.h"
362 The client-side environment.
364 This is not thread-safe.
365 Must be called from main (irrlicht) thread (uses the SceneManager)
366 Client uses an environment mutex.
369 enum ClientEnvEventType
375 struct ClientEnvEvent
377 ClientEnvEventType type;
388 class ClientEnvironment : public Environment
391 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
392 ITextureSource *texturesource, IGameDef *gamedef,
393 IrrlichtDevice *device);
394 ~ClientEnvironment();
399 ClientMap & getClientMap()
402 IGameDef *getGameDef()
403 { return m_gamedef; }
405 void step(f32 dtime);
407 virtual void addPlayer(Player *player);
408 LocalPlayer * getLocalPlayer();
410 // Slightly deprecated
411 void updateMeshes(v3s16 blockpos);
412 void expireMeshes(bool only_daynight_diffed);
414 void setTimeOfDay(u32 time)
416 u32 old_dr = getDayNightRatio();
418 Environment::setTimeOfDay(time);
420 if(getDayNightRatio() != old_dr)
422 /*infostream<<"ClientEnvironment: DayNightRatio changed"
423 <<" -> expiring meshes"<<std::endl;*/
432 ClientActiveObject* getActiveObject(u16 id);
435 Adds an active object to the environment.
436 Environment handles deletion of object.
437 Object may be deleted by environment immediately.
438 If id of object is 0, assigns a free id to it.
439 Returns the id of the object.
440 Returns 0 if not added and thus deleted.
442 u16 addActiveObject(ClientActiveObject *object);
444 void addActiveObject(u16 id, u8 type, const std::string &init_data);
445 void removeActiveObject(u16 id);
447 void processActiveObjectMessage(u16 id, const std::string &data);
450 Callbacks for activeobjects
453 void damageLocalPlayer(u8 damage, bool handle_hp=true);
456 Client likes to call these
459 // Get all nearby objects
460 void getActiveObjects(v3f origin, f32 max_d,
461 core::array<DistanceSortedActiveObject> &dest);
463 // Get event from queue. CEE_NONE is returned if queue is empty.
464 ClientEnvEvent getClientEvent();
468 scene::ISceneManager *m_smgr;
469 ITextureSource *m_texturesource;
471 IrrlichtDevice *m_irr;
472 core::map<u16, ClientActiveObject*> m_active_objects;
473 Queue<ClientEnvEvent> m_client_event_queue;
474 IntervalLimiter m_active_object_light_update_interval;
475 IntervalLimiter m_lava_hurt_interval;