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 (actually it only contains the brightness)
34 #include "common_irrlicht.h"
39 #include "activeobject.h"
42 class ActiveBlockModifier;
43 class ServerActiveObject;
44 typedef struct lua_State lua_State;
49 // Environment will delete the map passed to the constructor
51 virtual ~Environment();
54 Step everything in environment.
59 virtual void step(f32 dtime) = 0;
61 virtual Map & getMap() = 0;
63 virtual void addPlayer(Player *player);
64 void removePlayer(u16 peer_id);
65 Player * getPlayer(u16 peer_id);
66 Player * getPlayer(const char *name);
67 Player * getRandomConnectedPlayer();
68 Player * getNearestConnectedPlayer(v3f pos);
69 core::list<Player*> getPlayers();
70 core::list<Player*> getPlayers(bool ignore_disconnected);
71 void printPlayers(std::ostream &o);
73 //void setDayNightRatio(u32 r);
74 u32 getDayNightRatio();
77 virtual void setTimeOfDay(u32 time)
88 // peer_ids in here should be unique, except that there may be many 0s
89 core::list<Player*> m_players;
91 //u32 m_daynight_ratio;
92 // Time of day in milli-hours (0-23999); determines day and night
97 List of active blocks, used by ServerEnvironment
100 class ActiveBlockList
103 void update(core::list<v3s16> &active_positions,
105 core::map<v3s16, bool> &blocks_removed,
106 core::map<v3s16, bool> &blocks_added);
108 bool contains(v3s16 p){
109 return (m_list.find(p) != NULL);
116 core::map<v3s16, bool> m_list;
122 The server-side environment.
124 This is not thread-safe. Server uses an environment mutex.
127 class ServerEnvironment : public Environment
130 ServerEnvironment(ServerMap *map, lua_State *L);
131 ~ServerEnvironment();
138 ServerMap & getServerMap()
151 void serializePlayers(const std::string &savedir);
152 void deSerializePlayers(const std::string &savedir);
155 Save and load time of day and game timer
157 void saveMeta(const std::string &savedir);
158 void loadMeta(const std::string &savedir);
161 External ActiveObject interface
162 -------------------------------------------
165 ServerActiveObject* getActiveObject(u16 id);
168 Add an active object to the environment.
169 Environment handles deletion of object.
170 Object may be deleted by environment immediately.
171 If id of object is 0, assigns a free id to it.
172 Returns the id of the object.
173 Returns 0 if not added and thus deleted.
175 u16 addActiveObject(ServerActiveObject *object);
178 Add an active object as a static object to the corresponding
180 Caller allocates memory, ServerEnvironment frees memory.
181 Return value: true if succeeded, false if failed.
183 bool addActiveObjectAsStatic(ServerActiveObject *object);
186 Find out what new objects have been added to
187 inside a radius around a position
189 void getAddedActiveObjects(v3s16 pos, s16 radius,
190 core::map<u16, bool> ¤t_objects,
191 core::map<u16, bool> &added_objects);
194 Find out what new objects have been removed from
195 inside a radius around a position
197 void getRemovedActiveObjects(v3s16 pos, s16 radius,
198 core::map<u16, bool> ¤t_objects,
199 core::map<u16, bool> &removed_objects);
202 Get the next message emitted by some active object.
203 Returns a message with id=0 if no messages are available.
205 ActiveObjectMessage getActiveObjectMessage();
208 Activate objects and dynamically modify for the dtime determined
209 from timestamp and additional_dtime
211 void activateBlock(MapBlock *block, u32 additional_dtime=0);
214 ActiveBlockModifiers (TODO)
215 -------------------------------------------
218 void addActiveBlockModifier(ActiveBlockModifier *abm);
222 // Clear all objects, loading and going through every MapBlock
223 void clearAllObjects();
225 void step(f32 dtime);
230 Internal ActiveObject interface
231 -------------------------------------------
235 Add an active object to the environment.
237 Called by addActiveObject.
239 Object may be deleted by environment immediately.
240 If id of object is 0, assigns a free id to it.
241 Returns the id of the object.
242 Returns 0 if not added and thus deleted.
244 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed);
247 Remove all objects that satisfy (m_removed && m_known_by_count==0)
249 void removeRemovedObjects();
252 Convert stored objects from block to active
254 void activateObjects(MapBlock *block);
257 Convert objects that are not in active blocks to static.
259 If m_known_by_count != 0, active object is not deleted, but static
260 data is still updated.
262 If force_delete is set, active object is deleted nevertheless. It
263 shall only be set so in the destructor of the environment.
265 void deactivateFarObjects(bool force_delete);
275 // Active object list
276 core::map<u16, ServerActiveObject*> m_active_objects;
277 // Outgoing network message buffer for active objects
278 Queue<ActiveObjectMessage> m_active_object_messages;
280 float m_random_spawn_timer; // used for experimental code
281 float m_send_recommended_timer;
282 IntervalLimiter m_object_management_interval;
283 // List of active blocks
284 ActiveBlockList m_active_blocks;
285 IntervalLimiter m_active_blocks_management_interval;
286 IntervalLimiter m_active_blocks_test_interval;
287 IntervalLimiter m_active_blocks_nodemetadata_interval;
288 // Time from the beginning of the game in seconds.
289 // Incremented in step().
291 // A helper variable for incrementing the latter
292 float m_game_time_fraction_counter;
296 Active block modifier interface.
298 These are fed into ServerEnvironment at initialization time;
299 ServerEnvironment handles deleting them.
302 class ActiveBlockModifier
305 ActiveBlockModifier(){};
306 virtual ~ActiveBlockModifier(){};
308 //virtual core::list<u8> update(ServerEnvironment *env) = 0;
309 virtual u32 getTriggerContentCount(){ return 1;}
310 virtual u8 getTriggerContent(u32 i) = 0;
311 virtual float getActiveInterval() = 0;
312 // chance of (1 / return value), 0 is disallowed
313 virtual u32 getActiveChance() = 0;
314 // This is called usually at interval for 1/chance of the nodes
315 virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
320 #include "clientobject.h"
323 The client-side environment.
325 This is not thread-safe.
326 Must be called from main (irrlicht) thread (uses the SceneManager)
327 Client uses an environment mutex.
330 enum ClientEnvEventType
336 struct ClientEnvEvent
338 ClientEnvEventType type;
348 class ClientEnvironment : public Environment
351 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
352 ~ClientEnvironment();
359 ClientMap & getClientMap()
364 void step(f32 dtime);
366 virtual void addPlayer(Player *player);
367 LocalPlayer * getLocalPlayer();
369 void updateMeshes(v3s16 blockpos);
370 void expireMeshes(bool only_daynight_diffed);
372 void setTimeOfDay(u32 time)
374 u32 old_dr = getDayNightRatio();
376 Environment::setTimeOfDay(time);
378 if(getDayNightRatio() != old_dr)
380 dout_client<<DTIME<<"ClientEnvironment: DayNightRatio changed"
381 <<" -> expiring meshes"<<std::endl;
390 ClientActiveObject* getActiveObject(u16 id);
393 Adds an active object to the environment.
394 Environment handles deletion of object.
395 Object may be deleted by environment immediately.
396 If id of object is 0, assigns a free id to it.
397 Returns the id of the object.
398 Returns 0 if not added and thus deleted.
400 u16 addActiveObject(ClientActiveObject *object);
402 void addActiveObject(u16 id, u8 type, const std::string &init_data);
403 void removeActiveObject(u16 id);
405 void processActiveObjectMessage(u16 id, const std::string &data);
408 Callbacks for activeobjects
411 void damageLocalPlayer(u8 damage);
414 Client likes to call these
417 // Get all nearby objects
418 void getActiveObjects(v3f origin, f32 max_d,
419 core::array<DistanceSortedActiveObject> &dest);
421 // Get event from queue. CEE_NONE is returned if queue is empty.
422 ClientEnvEvent getClientEvent();
426 scene::ISceneManager *m_smgr;
427 core::map<u16, ClientActiveObject*> m_active_objects;
428 Queue<ClientEnvEvent> m_client_event_queue;
429 IntervalLimiter m_active_object_light_update_interval;
430 IntervalLimiter m_lava_hurt_interval;