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 ServerEnvironment;
43 class ActiveBlockModifier;
44 class ServerActiveObject;
45 typedef struct lua_State lua_State;
52 // Environment will delete the map passed to the constructor
54 virtual ~Environment();
57 Step everything in environment.
62 virtual void step(f32 dtime) = 0;
64 virtual Map & getMap() = 0;
66 virtual void addPlayer(Player *player);
67 void removePlayer(u16 peer_id);
68 Player * getPlayer(u16 peer_id);
69 Player * getPlayer(const char *name);
70 Player * getRandomConnectedPlayer();
71 Player * getNearestConnectedPlayer(v3f pos);
72 core::list<Player*> getPlayers();
73 core::list<Player*> getPlayers(bool ignore_disconnected);
74 void printPlayers(std::ostream &o);
76 //void setDayNightRatio(u32 r);
77 u32 getDayNightRatio();
80 virtual void setTimeOfDay(u32 time)
91 // peer_ids in here should be unique, except that there may be many 0s
92 core::list<Player*> m_players;
94 //u32 m_daynight_ratio;
95 // Time of day in milli-hours (0-23999); determines day and night
100 Active block modifier interface.
102 These are fed into ServerEnvironment at initialization time;
103 ServerEnvironment handles deleting them.
106 class ActiveBlockModifier
109 ActiveBlockModifier(){};
110 virtual ~ActiveBlockModifier(){};
112 // Set of contents to trigger on
113 virtual std::set<std::string> getTriggerContents()=0;
114 // Set of required neighbors (trigger doesn't happen if none are found)
115 // Empty = do not check neighbors
116 virtual std::set<std::string> getRequiredNeighbors()
117 { return std::set<std::string>(); }
118 // Trigger interval in seconds
119 virtual float getTriggerInterval() = 0;
120 // Random chance of (1 / return value), 0 is disallowed
121 virtual u32 getTriggerChance() = 0;
122 // This is called usually at interval for 1/chance of the nodes
123 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
124 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
125 u32 active_object_count, u32 active_object_count_wider){};
130 ActiveBlockModifier *abm;
133 ABMWithState(ActiveBlockModifier *abm_);
137 List of active blocks, used by ServerEnvironment
140 class ActiveBlockList
143 void update(core::list<v3s16> &active_positions,
145 core::map<v3s16, bool> &blocks_removed,
146 core::map<v3s16, bool> &blocks_added);
148 bool contains(v3s16 p){
149 return (m_list.find(p) != NULL);
156 core::map<v3s16, bool> m_list;
161 class IBackgroundBlockEmerger
164 virtual void queueBlockEmerge(v3s16 blockpos, bool allow_generate)=0;
168 The server-side environment.
170 This is not thread-safe. Server uses an environment mutex.
173 class ServerEnvironment : public Environment
176 ServerEnvironment(ServerMap *map, lua_State *L, IGameDef *gamedef,
177 IBackgroundBlockEmerger *emerger);
178 ~ServerEnvironment();
183 ServerMap & getServerMap()
189 IGameDef *getGameDef()
190 { return m_gamedef; }
192 float getSendRecommendedInterval()
200 void serializePlayers(const std::string &savedir);
201 void deSerializePlayers(const std::string &savedir);
204 Save and load time of day and game timer
206 void saveMeta(const std::string &savedir);
207 void loadMeta(const std::string &savedir);
210 External ActiveObject interface
211 -------------------------------------------
214 ServerActiveObject* getActiveObject(u16 id);
217 Add an active object to the environment.
218 Environment handles deletion of object.
219 Object may be deleted by environment immediately.
220 If id of object is 0, assigns a free id to it.
221 Returns the id of the object.
222 Returns 0 if not added and thus deleted.
224 u16 addActiveObject(ServerActiveObject *object);
227 Add an active object as a static object to the corresponding
229 Caller allocates memory, ServerEnvironment frees memory.
230 Return value: true if succeeded, false if failed.
232 bool addActiveObjectAsStatic(ServerActiveObject *object);
235 Find out what new objects have been added to
236 inside a radius around a position
238 void getAddedActiveObjects(v3s16 pos, s16 radius,
239 core::map<u16, bool> ¤t_objects,
240 core::map<u16, bool> &added_objects);
243 Find out what new objects have been removed from
244 inside a radius around a position
246 void getRemovedActiveObjects(v3s16 pos, s16 radius,
247 core::map<u16, bool> ¤t_objects,
248 core::map<u16, bool> &removed_objects);
251 Get the next message emitted by some active object.
252 Returns a message with id=0 if no messages are available.
254 ActiveObjectMessage getActiveObjectMessage();
257 Activate objects and dynamically modify for the dtime determined
258 from timestamp and additional_dtime
260 void activateBlock(MapBlock *block, u32 additional_dtime=0);
264 -------------------------------------------
267 void addActiveBlockModifier(ActiveBlockModifier *abm);
271 -------------------------------------------
274 // Find all active objects inside a radius around a point
275 std::set<u16> getObjectsInsideRadius(v3f pos, float radius);
277 // Clear all objects, loading and going through every MapBlock
278 void clearAllObjects();
280 // This makes stuff happen
281 void step(f32 dtime);
286 Internal ActiveObject interface
287 -------------------------------------------
291 Add an active object to the environment.
293 Called by addActiveObject.
295 Object may be deleted by environment immediately.
296 If id of object is 0, assigns a free id to it.
297 Returns the id of the object.
298 Returns 0 if not added and thus deleted.
300 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed);
303 Remove all objects that satisfy (m_removed && m_known_by_count==0)
305 void removeRemovedObjects();
308 Convert stored objects from block to active
310 void activateObjects(MapBlock *block);
313 Convert objects that are not in active blocks to static.
315 If m_known_by_count != 0, active object is not deleted, but static
316 data is still updated.
318 If force_delete is set, active object is deleted nevertheless. It
319 shall only be set so in the destructor of the environment.
321 void deactivateFarObjects(bool force_delete);
333 // Background block emerger (the server, in practice)
334 IBackgroundBlockEmerger *m_emerger;
335 // Active object list
336 core::map<u16, ServerActiveObject*> m_active_objects;
337 // Outgoing network message buffer for active objects
338 Queue<ActiveObjectMessage> m_active_object_messages;
340 float m_random_spawn_timer; // used for experimental code
341 float m_send_recommended_timer;
342 IntervalLimiter m_object_management_interval;
343 // List of active blocks
344 ActiveBlockList m_active_blocks;
345 IntervalLimiter m_active_blocks_management_interval;
346 IntervalLimiter m_active_block_modifier_interval;
347 IntervalLimiter m_active_blocks_nodemetadata_interval;
348 // Time from the beginning of the game in seconds.
349 // Incremented in step().
351 // A helper variable for incrementing the latter
352 float m_game_time_fraction_counter;
353 core::list<ABMWithState> m_abms;
358 #include "clientobject.h"
359 class ClientSimpleObject;
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();
414 void addSimpleObject(ClientSimpleObject *simple);
420 ClientActiveObject* getActiveObject(u16 id);
423 Adds an active object to the environment.
424 Environment handles deletion of object.
425 Object may be deleted by environment immediately.
426 If id of object is 0, assigns a free id to it.
427 Returns the id of the object.
428 Returns 0 if not added and thus deleted.
430 u16 addActiveObject(ClientActiveObject *object);
432 void addActiveObject(u16 id, u8 type, const std::string &init_data);
433 void removeActiveObject(u16 id);
435 void processActiveObjectMessage(u16 id, const std::string &data);
438 Callbacks for activeobjects
441 void damageLocalPlayer(u8 damage, bool handle_hp=true);
444 Client likes to call these
447 // Get all nearby objects
448 void getActiveObjects(v3f origin, f32 max_d,
449 core::array<DistanceSortedActiveObject> &dest);
451 // Get event from queue. CEE_NONE is returned if queue is empty.
452 ClientEnvEvent getClientEvent();
456 scene::ISceneManager *m_smgr;
457 ITextureSource *m_texturesource;
459 IrrlichtDevice *m_irr;
460 core::map<u16, ClientActiveObject*> m_active_objects;
461 core::list<ClientSimpleObject*> m_simple_objects;
462 Queue<ClientEnvEvent> m_client_event_queue;
463 IntervalLimiter m_active_object_light_update_interval;
464 IntervalLimiter m_lava_hurt_interval;