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 Active block modifier interface
119 class ServerEnvironment;
121 class ActiveBlockModifier
124 ActiveBlockModifier(){};
125 virtual ~ActiveBlockModifier(){};
127 virtual u32 getTriggerContentCount(){ return 1;}
128 virtual u8 getTriggerContent(u32 i) = 0;
129 virtual float getActiveInterval() = 0;
130 virtual u32 getActiveChance() = 0;
131 virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
135 The server-side environment.
137 This is not thread-safe. Server uses an environment mutex.
140 #include "serverobject.h"
144 class ServerEnvironment : public Environment
147 ServerEnvironment(ServerMap *map, Server *server);
148 ~ServerEnvironment();
155 ServerMap & getServerMap()
165 void step(f32 dtime);
170 void serializePlayers(const std::string &savedir);
171 void deSerializePlayers(const std::string &savedir);
174 Save and load time of day and game timer
176 void saveMeta(const std::string &savedir);
177 void loadMeta(const std::string &savedir);
183 ServerActiveObject* getActiveObject(u16 id);
186 Add an active object to the environment.
187 Environment handles deletion of object.
188 Object may be deleted by environment immediately.
189 If id of object is 0, assigns a free id to it.
190 Returns the id of the object.
191 Returns 0 if not added and thus deleted.
193 u16 addActiveObject(ServerActiveObject *object);
196 Find out what new objects have been added to
197 inside a radius around a position
199 void getAddedActiveObjects(v3s16 pos, s16 radius,
200 core::map<u16, bool> ¤t_objects,
201 core::map<u16, bool> &added_objects);
204 Find out what new objects have been removed from
205 inside a radius around a position
207 void getRemovedActiveObjects(v3s16 pos, s16 radius,
208 core::map<u16, bool> ¤t_objects,
209 core::map<u16, bool> &removed_objects);
212 Get the next message emitted by some active object.
213 Returns a message with id=0 if no messages are available.
215 ActiveObjectMessage getActiveObjectMessage();
219 Remove all objects that satisfy (m_removed && m_known_by_count==0)
221 void removeRemovedObjects();
224 Convert stored objects from block to active
226 void activateObjects(MapBlock *block);
229 Convert objects that are not in active blocks to static.
231 If m_known_by_count != 0, active object is not deleted, but static
232 data is still updated.
234 If force_delete is set, active object is deleted nevertheless. It
235 shall only be set so in the destructor of the environment.
237 void deactivateFarObjects(bool force_delete);
245 // Pointer to server (which is handling this environment)
247 // Active object list
248 core::map<u16, ServerActiveObject*> m_active_objects;
249 // Outgoing network message buffer for active objects
250 Queue<ActiveObjectMessage> m_active_object_messages;
252 float m_random_spawn_timer; // used for experimental code
253 float m_send_recommended_timer;
254 IntervalLimiter m_object_management_interval;
255 // List of active blocks
256 ActiveBlockList m_active_blocks;
257 IntervalLimiter m_active_blocks_management_interval;
258 IntervalLimiter m_active_blocks_test_interval;
259 // Time from the beginning of the game in seconds.
260 // Incremented in step().
262 // A helper variable for incrementing the latter
263 float m_game_time_fraction_counter;
268 #include "clientobject.h"
271 The client-side environment.
273 This is not thread-safe.
274 Must be called from main (irrlicht) thread (uses the SceneManager)
275 Client uses an environment mutex.
278 enum ClientEnvEventType
284 struct ClientEnvEvent
286 ClientEnvEventType type;
296 class ClientEnvironment : public Environment
299 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
300 ~ClientEnvironment();
307 ClientMap & getClientMap()
312 void step(f32 dtime);
314 virtual void addPlayer(Player *player);
315 LocalPlayer * getLocalPlayer();
317 void updateMeshes(v3s16 blockpos);
318 void expireMeshes(bool only_daynight_diffed);
320 void setTimeOfDay(u32 time)
322 u32 old_dr = getDayNightRatio();
324 Environment::setTimeOfDay(time);
326 if(getDayNightRatio() != old_dr)
328 dout_client<<DTIME<<"ClientEnvironment: DayNightRatio changed"
329 <<" -> expiring meshes"<<std::endl;
338 ClientActiveObject* getActiveObject(u16 id);
341 Adds an active object to the environment.
342 Environment handles deletion of object.
343 Object may be deleted by environment immediately.
344 If id of object is 0, assigns a free id to it.
345 Returns the id of the object.
346 Returns 0 if not added and thus deleted.
348 u16 addActiveObject(ClientActiveObject *object);
350 void addActiveObject(u16 id, u8 type, const std::string &init_data);
351 void removeActiveObject(u16 id);
353 void processActiveObjectMessage(u16 id, const std::string &data);
356 Callbacks for activeobjects
359 void damageLocalPlayer(u8 damage);
362 Client likes to call these
365 // Get all nearby objects
366 void getActiveObjects(v3f origin, f32 max_d,
367 core::array<DistanceSortedActiveObject> &dest);
369 // Get event from queue. CEE_NONE is returned if queue is empty.
370 ClientEnvEvent getClientEvent();
374 scene::ISceneManager *m_smgr;
375 core::map<u16, ClientActiveObject*> m_active_objects;
376 Queue<ClientEnvEvent> m_client_event_queue;