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"
43 // Environment will delete the map passed to the constructor
45 virtual ~Environment();
48 Step everything in environment.
53 virtual void step(f32 dtime) = 0;
55 virtual Map & getMap() = 0;
57 virtual void addPlayer(Player *player);
58 void removePlayer(u16 peer_id);
59 Player * getPlayer(u16 peer_id);
60 Player * getPlayer(const char *name);
61 Player * getRandomConnectedPlayer();
62 Player * getNearestConnectedPlayer(v3f pos);
63 core::list<Player*> getPlayers();
64 core::list<Player*> getPlayers(bool ignore_disconnected);
65 void printPlayers(std::ostream &o);
67 //void setDayNightRatio(u32 r);
68 u32 getDayNightRatio();
71 virtual void setTimeOfDay(u32 time)
82 // peer_ids in here should be unique, except that there may be many 0s
83 core::list<Player*> m_players;
85 //u32 m_daynight_ratio;
86 // Time of day in milli-hours (0-23999); determines day and night
91 List of active blocks, used by ServerEnvironment
97 void update(core::list<v3s16> &active_positions,
99 core::map<v3s16, bool> &blocks_removed,
100 core::map<v3s16, bool> &blocks_added);
102 bool contains(v3s16 p){
103 return (m_list.find(p) != NULL);
110 core::map<v3s16, bool> m_list;
116 The server-side environment.
118 This is not thread-safe. Server uses an environment mutex.
121 #include "serverobject.h"
124 class ActiveBlockModifier;
126 class ServerEnvironment : public Environment
129 ServerEnvironment(ServerMap *map, Server *server);
130 ~ServerEnvironment();
137 ServerMap & getServerMap()
147 void step(f32 dtime);
152 void serializePlayers(const std::string &savedir);
153 void deSerializePlayers(const std::string &savedir);
156 Save and load time of day and game timer
158 void saveMeta(const std::string &savedir);
159 void loadMeta(const std::string &savedir);
163 -------------------------------------------
166 ServerActiveObject* getActiveObject(u16 id);
169 Add an active object to the environment.
170 Environment handles deletion of object.
171 Object may be deleted by environment immediately.
172 If id of object is 0, assigns a free id to it.
173 Returns the id of the object.
174 Returns 0 if not added and thus deleted.
176 u16 addActiveObject(ServerActiveObject *object);
179 Find out what new objects have been added to
180 inside a radius around a position
182 void getAddedActiveObjects(v3s16 pos, s16 radius,
183 core::map<u16, bool> ¤t_objects,
184 core::map<u16, bool> &added_objects);
187 Find out what new objects have been removed from
188 inside a radius around a position
190 void getRemovedActiveObjects(v3s16 pos, s16 radius,
191 core::map<u16, bool> ¤t_objects,
192 core::map<u16, bool> &removed_objects);
195 Get the next message emitted by some active object.
196 Returns a message with id=0 if no messages are available.
198 ActiveObjectMessage getActiveObjectMessage();
202 -------------------------------------------
205 void addActiveBlockModifier(ActiveBlockModifier *abm);
209 Remove all objects that satisfy (m_removed && m_known_by_count==0)
211 void removeRemovedObjects();
214 Convert stored objects from block to active
216 void activateObjects(MapBlock *block);
219 Convert objects that are not in active blocks to static.
221 If m_known_by_count != 0, active object is not deleted, but static
222 data is still updated.
224 If force_delete is set, active object is deleted nevertheless. It
225 shall only be set so in the destructor of the environment.
227 void deactivateFarObjects(bool force_delete);
235 // Pointer to server (which is handling this environment)
237 // Active object list
238 core::map<u16, ServerActiveObject*> m_active_objects;
239 // Outgoing network message buffer for active objects
240 Queue<ActiveObjectMessage> m_active_object_messages;
242 float m_random_spawn_timer; // used for experimental code
243 float m_send_recommended_timer;
244 IntervalLimiter m_object_management_interval;
245 // List of active blocks
246 ActiveBlockList m_active_blocks;
247 IntervalLimiter m_active_blocks_management_interval;
248 IntervalLimiter m_active_blocks_test_interval;
249 IntervalLimiter m_active_blocks_nodemetadata_interval;
250 // Time from the beginning of the game in seconds.
251 // Incremented in step().
253 // A helper variable for incrementing the latter
254 float m_game_time_fraction_counter;
258 Active block modifier interface.
260 These are fed into ServerEnvironment at initialization time;
261 ServerEnvironment handles deleting them.
264 class ActiveBlockModifier
267 ActiveBlockModifier(){};
268 virtual ~ActiveBlockModifier(){};
270 //virtual core::list<u8> update(ServerEnvironment *env) = 0;
271 virtual u32 getTriggerContentCount(){ return 1;}
272 virtual u8 getTriggerContent(u32 i) = 0;
273 virtual float getActiveInterval() = 0;
274 // chance of (1 / return value), 0 is disallowed
275 virtual u32 getActiveChance() = 0;
276 // This is called usually at interval for 1/chance of the nodes
277 virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
282 #include "clientobject.h"
285 The client-side environment.
287 This is not thread-safe.
288 Must be called from main (irrlicht) thread (uses the SceneManager)
289 Client uses an environment mutex.
292 enum ClientEnvEventType
298 struct ClientEnvEvent
300 ClientEnvEventType type;
310 class ClientEnvironment : public Environment
313 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
314 ~ClientEnvironment();
321 ClientMap & getClientMap()
326 void step(f32 dtime);
328 virtual void addPlayer(Player *player);
329 LocalPlayer * getLocalPlayer();
331 void updateMeshes(v3s16 blockpos);
332 void expireMeshes(bool only_daynight_diffed);
334 void setTimeOfDay(u32 time)
336 u32 old_dr = getDayNightRatio();
338 Environment::setTimeOfDay(time);
340 if(getDayNightRatio() != old_dr)
342 dout_client<<DTIME<<"ClientEnvironment: DayNightRatio changed"
343 <<" -> expiring meshes"<<std::endl;
352 ClientActiveObject* getActiveObject(u16 id);
355 Adds an active object to the environment.
356 Environment handles deletion of object.
357 Object may be deleted by environment immediately.
358 If id of object is 0, assigns a free id to it.
359 Returns the id of the object.
360 Returns 0 if not added and thus deleted.
362 u16 addActiveObject(ClientActiveObject *object);
364 void addActiveObject(u16 id, u8 type, const std::string &init_data);
365 void removeActiveObject(u16 id);
367 void processActiveObjectMessage(u16 id, const std::string &data);
370 Callbacks for activeobjects
373 void damageLocalPlayer(u8 damage);
376 Client likes to call these
379 // Get all nearby objects
380 void getActiveObjects(v3f origin, f32 max_d,
381 core::array<DistanceSortedActiveObject> &dest);
383 // Get event from queue. CEE_NONE is returned if queue is empty.
384 ClientEnvEvent getClientEvent();
388 scene::ISceneManager *m_smgr;
389 core::map<u16, ClientActiveObject*> m_active_objects;
390 Queue<ClientEnvEvent> m_client_event_queue;