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 List of active blocks, used by ServerEnvironment
102 class ActiveBlockList
105 void update(core::list<v3s16> &active_positions,
107 core::map<v3s16, bool> &blocks_removed,
108 core::map<v3s16, bool> &blocks_added);
110 bool contains(v3s16 p){
111 return (m_list.find(p) != NULL);
118 core::map<v3s16, bool> m_list;
124 The server-side environment.
126 This is not thread-safe. Server uses an environment mutex.
129 class ServerEnvironment : public Environment
132 ServerEnvironment(ServerMap *map, lua_State *L, IGameDef *gamedef);
133 ~ServerEnvironment();
138 ServerMap & getServerMap()
144 IGameDef *getGameDef()
145 { return m_gamedef; }
147 float getSendRecommendedInterval()
155 void serializePlayers(const std::string &savedir);
156 void deSerializePlayers(const std::string &savedir);
159 Save and load time of day and game timer
161 void saveMeta(const std::string &savedir);
162 void loadMeta(const std::string &savedir);
165 External ActiveObject interface
166 -------------------------------------------
169 ServerActiveObject* getActiveObject(u16 id);
172 Add an active object to the environment.
173 Environment handles deletion of object.
174 Object may be deleted by environment immediately.
175 If id of object is 0, assigns a free id to it.
176 Returns the id of the object.
177 Returns 0 if not added and thus deleted.
179 u16 addActiveObject(ServerActiveObject *object);
182 Add an active object as a static object to the corresponding
184 Caller allocates memory, ServerEnvironment frees memory.
185 Return value: true if succeeded, false if failed.
187 bool addActiveObjectAsStatic(ServerActiveObject *object);
190 Find out what new objects have been added to
191 inside a radius around a position
193 void getAddedActiveObjects(v3s16 pos, s16 radius,
194 core::map<u16, bool> ¤t_objects,
195 core::map<u16, bool> &added_objects);
198 Find out what new objects have been removed from
199 inside a radius around a position
201 void getRemovedActiveObjects(v3s16 pos, s16 radius,
202 core::map<u16, bool> ¤t_objects,
203 core::map<u16, bool> &removed_objects);
206 Get the next message emitted by some active object.
207 Returns a message with id=0 if no messages are available.
209 ActiveObjectMessage getActiveObjectMessage();
212 Activate objects and dynamically modify for the dtime determined
213 from timestamp and additional_dtime
215 void activateBlock(MapBlock *block, u32 additional_dtime=0);
218 ActiveBlockModifiers (TODO)
219 -------------------------------------------
220 NOTE: Not used currently (TODO: Use or remove)
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);
282 // Active object list
283 core::map<u16, ServerActiveObject*> m_active_objects;
284 // Outgoing network message buffer for active objects
285 Queue<ActiveObjectMessage> m_active_object_messages;
287 float m_random_spawn_timer; // used for experimental code
288 float m_send_recommended_timer;
289 IntervalLimiter m_object_management_interval;
290 // List of active blocks
291 ActiveBlockList m_active_blocks;
292 IntervalLimiter m_active_blocks_management_interval;
293 IntervalLimiter m_active_blocks_test_interval;
294 IntervalLimiter m_active_blocks_nodemetadata_interval;
295 // Time from the beginning of the game in seconds.
296 // Incremented in step().
298 // A helper variable for incrementing the latter
299 float m_game_time_fraction_counter;
303 Active block modifier interface.
305 These are fed into ServerEnvironment at initialization time;
306 ServerEnvironment handles deleting them.
308 NOTE: Not used currently (TODO: Use or remove)
311 class ActiveBlockModifier
314 ActiveBlockModifier(){};
315 virtual ~ActiveBlockModifier(){};
317 //virtual core::list<u8> update(ServerEnvironment *env) = 0;
318 virtual u32 getTriggerContentCount(){ return 1;}
319 virtual u8 getTriggerContent(u32 i) = 0;
320 virtual float getActiveInterval() = 0;
321 // chance of (1 / return value), 0 is disallowed
322 virtual u32 getActiveChance() = 0;
323 // This is called usually at interval for 1/chance of the nodes
324 virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
329 #include "clientobject.h"
332 The client-side environment.
334 This is not thread-safe.
335 Must be called from main (irrlicht) thread (uses the SceneManager)
336 Client uses an environment mutex.
339 enum ClientEnvEventType
345 struct ClientEnvEvent
347 ClientEnvEventType type;
357 class ClientEnvironment : public Environment
360 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
361 ITextureSource *texturesource, IGameDef *gamedef);
362 ~ClientEnvironment();
369 ClientMap & getClientMap()
374 void step(f32 dtime);
376 virtual void addPlayer(Player *player);
377 LocalPlayer * getLocalPlayer();
379 // Slightly deprecated
380 void updateMeshes(v3s16 blockpos, ITextureSource *tsrc);
381 void expireMeshes(bool only_daynight_diffed);
383 void setTimeOfDay(u32 time)
385 u32 old_dr = getDayNightRatio();
387 Environment::setTimeOfDay(time);
389 if(getDayNightRatio() != old_dr)
391 /*infostream<<"ClientEnvironment: DayNightRatio changed"
392 <<" -> expiring meshes"<<std::endl;*/
401 ClientActiveObject* getActiveObject(u16 id);
404 Adds an active object to the environment.
405 Environment handles deletion of object.
406 Object may be deleted by environment immediately.
407 If id of object is 0, assigns a free id to it.
408 Returns the id of the object.
409 Returns 0 if not added and thus deleted.
411 u16 addActiveObject(ClientActiveObject *object);
413 void addActiveObject(u16 id, u8 type, const std::string &init_data);
414 void removeActiveObject(u16 id);
416 void processActiveObjectMessage(u16 id, const std::string &data);
419 Callbacks for activeobjects
422 void damageLocalPlayer(u8 damage);
425 Client likes to call these
428 // Get all nearby objects
429 void getActiveObjects(v3f origin, f32 max_d,
430 core::array<DistanceSortedActiveObject> &dest);
432 // Get event from queue. CEE_NONE is returned if queue is empty.
433 ClientEnvEvent getClientEvent();
437 scene::ISceneManager *m_smgr;
438 ITextureSource *m_texturesource;
440 core::map<u16, ClientActiveObject*> m_active_objects;
441 Queue<ClientEnvEvent> m_client_event_queue;
442 IntervalLimiter m_active_object_light_update_interval;
443 IntervalLimiter m_lava_hurt_interval;