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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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 "irrlichttypes_extrabloated.h"
37 #include "activeobject.h"
38 #include "util/container.h"
39 #include "util/numeric.h"
44 class ServerEnvironment;
45 class ActiveBlockModifier;
46 class ServerActiveObject;
47 typedef struct lua_State lua_State;
57 // Environment will delete the map passed to the constructor
59 virtual ~Environment();
62 Step everything in environment.
67 virtual void step(f32 dtime) = 0;
69 virtual Map & getMap() = 0;
71 virtual void addPlayer(Player *player);
72 void removePlayer(u16 peer_id);
73 Player * getPlayer(u16 peer_id);
74 Player * getPlayer(const char *name);
75 Player * getRandomConnectedPlayer();
76 Player * getNearestConnectedPlayer(v3f pos);
77 core::list<Player*> getPlayers();
78 core::list<Player*> getPlayers(bool ignore_disconnected);
79 void printPlayers(std::ostream &o);
81 u32 getDayNightRatio();
84 virtual void setTimeOfDay(u32 time)
87 m_time_of_day_f = (float)time / 24000.0;
91 { return m_time_of_day; }
94 { return m_time_of_day_f; }
96 void stepTimeOfDay(float dtime);
98 void setTimeOfDaySpeed(float speed)
99 { m_time_of_day_speed = speed; }
101 float getTimeOfDaySpeed()
102 { return m_time_of_day_speed; }
105 // peer_ids in here should be unique, except that there may be many 0s
106 core::list<Player*> m_players;
107 // Time of day in milli-hours (0-23999); determines day and night
109 // Time of day in 0...1
110 float m_time_of_day_f;
111 float m_time_of_day_speed;
112 // Used to buffer dtime for adding to m_time_of_day
113 float m_time_counter;
117 Active block modifier interface.
119 These are fed into ServerEnvironment at initialization time;
120 ServerEnvironment handles deleting them.
123 class ActiveBlockModifier
126 ActiveBlockModifier(){};
127 virtual ~ActiveBlockModifier(){};
129 // Set of contents to trigger on
130 virtual std::set<std::string> getTriggerContents()=0;
131 // Set of required neighbors (trigger doesn't happen if none are found)
132 // Empty = do not check neighbors
133 virtual std::set<std::string> getRequiredNeighbors()
134 { return std::set<std::string>(); }
135 // Trigger interval in seconds
136 virtual float getTriggerInterval() = 0;
137 // Random chance of (1 / return value), 0 is disallowed
138 virtual u32 getTriggerChance() = 0;
139 // This is called usually at interval for 1/chance of the nodes
140 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
141 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
142 u32 active_object_count, u32 active_object_count_wider){};
147 ActiveBlockModifier *abm;
150 ABMWithState(ActiveBlockModifier *abm_);
154 List of active blocks, used by ServerEnvironment
157 class ActiveBlockList
160 void update(core::list<v3s16> &active_positions,
162 core::map<v3s16, bool> &blocks_removed,
163 core::map<v3s16, bool> &blocks_added);
165 bool contains(v3s16 p){
166 return (m_list.find(p) != NULL);
173 core::map<v3s16, bool> m_list;
178 class IBackgroundBlockEmerger
181 virtual void queueBlockEmerge(v3s16 blockpos, bool allow_generate)=0;
185 The server-side environment.
187 This is not thread-safe. Server uses an environment mutex.
190 class ServerEnvironment : public Environment
193 ServerEnvironment(ServerMap *map, lua_State *L, IGameDef *gamedef,
194 IBackgroundBlockEmerger *emerger);
195 ~ServerEnvironment();
199 ServerMap & getServerMap();
204 IGameDef *getGameDef()
205 { return m_gamedef; }
207 float getSendRecommendedInterval()
208 { return m_recommended_send_interval; }
213 void serializePlayers(const std::string &savedir);
214 void deSerializePlayers(const std::string &savedir);
217 Save and load time of day and game timer
219 void saveMeta(const std::string &savedir);
220 void loadMeta(const std::string &savedir);
223 External ActiveObject interface
224 -------------------------------------------
227 ServerActiveObject* getActiveObject(u16 id);
230 Add an active object to the environment.
231 Environment handles deletion of object.
232 Object may be deleted by environment immediately.
233 If id of object is 0, assigns a free id to it.
234 Returns the id of the object.
235 Returns 0 if not added and thus deleted.
237 u16 addActiveObject(ServerActiveObject *object);
240 Add an active object as a static object to the corresponding
242 Caller allocates memory, ServerEnvironment frees memory.
243 Return value: true if succeeded, false if failed.
245 bool addActiveObjectAsStatic(ServerActiveObject *object);
248 Find out what new objects have been added to
249 inside a radius around a position
251 void getAddedActiveObjects(v3s16 pos, s16 radius,
252 core::map<u16, bool> ¤t_objects,
253 core::map<u16, bool> &added_objects);
256 Find out what new objects have been removed from
257 inside a radius around a position
259 void getRemovedActiveObjects(v3s16 pos, s16 radius,
260 core::map<u16, bool> ¤t_objects,
261 core::map<u16, bool> &removed_objects);
264 Get the next message emitted by some active object.
265 Returns a message with id=0 if no messages are available.
267 ActiveObjectMessage getActiveObjectMessage();
270 Activate objects and dynamically modify for the dtime determined
271 from timestamp and additional_dtime
273 void activateBlock(MapBlock *block, u32 additional_dtime=0);
277 -------------------------------------------
280 void addActiveBlockModifier(ActiveBlockModifier *abm);
284 -------------------------------------------
287 // Find all active objects inside a radius around a point
288 std::set<u16> getObjectsInsideRadius(v3f pos, float radius);
290 // Clear all objects, loading and going through every MapBlock
291 void clearAllObjects();
293 // This makes stuff happen
294 void step(f32 dtime);
299 Internal ActiveObject interface
300 -------------------------------------------
304 Add an active object to the environment.
306 Called by addActiveObject.
308 Object may be deleted by environment immediately.
309 If id of object is 0, assigns a free id to it.
310 Returns the id of the object.
311 Returns 0 if not added and thus deleted.
313 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
316 Remove all objects that satisfy (m_removed && m_known_by_count==0)
318 void removeRemovedObjects();
321 Convert stored objects from block to active
323 void activateObjects(MapBlock *block, u32 dtime_s);
326 Convert objects that are not in active blocks to static.
328 If m_known_by_count != 0, active object is not deleted, but static
329 data is still updated.
331 If force_delete is set, active object is deleted nevertheless. It
332 shall only be set so in the destructor of the environment.
334 void deactivateFarObjects(bool force_delete);
346 // Background block emerger (the server, in practice)
347 IBackgroundBlockEmerger *m_emerger;
348 // Active object list
349 core::map<u16, ServerActiveObject*> m_active_objects;
350 // Outgoing network message buffer for active objects
351 Queue<ActiveObjectMessage> m_active_object_messages;
353 float m_random_spawn_timer; // used for experimental code
354 float m_send_recommended_timer;
355 IntervalLimiter m_object_management_interval;
356 // List of active blocks
357 ActiveBlockList m_active_blocks;
358 IntervalLimiter m_active_blocks_management_interval;
359 IntervalLimiter m_active_block_modifier_interval;
360 IntervalLimiter m_active_blocks_nodemetadata_interval;
361 int m_active_block_interval_overload_skip;
362 // Time from the beginning of the game in seconds.
363 // Incremented in step().
365 // A helper variable for incrementing the latter
366 float m_game_time_fraction_counter;
367 core::list<ABMWithState> m_abms;
368 // An interval for generally sending object positions and stuff
369 float m_recommended_send_interval;
374 #include "clientobject.h"
375 class ClientSimpleObject;
378 The client-side environment.
380 This is not thread-safe.
381 Must be called from main (irrlicht) thread (uses the SceneManager)
382 Client uses an environment mutex.
385 enum ClientEnvEventType
391 struct ClientEnvEvent
393 ClientEnvEventType type;
404 class ClientEnvironment : public Environment
407 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
408 ITextureSource *texturesource, IGameDef *gamedef,
409 IrrlichtDevice *device);
410 ~ClientEnvironment();
413 ClientMap & getClientMap();
415 IGameDef *getGameDef()
416 { return m_gamedef; }
418 void step(f32 dtime);
420 virtual void addPlayer(Player *player);
421 LocalPlayer * getLocalPlayer();
427 void addSimpleObject(ClientSimpleObject *simple);
433 ClientActiveObject* getActiveObject(u16 id);
436 Adds an active object to the environment.
437 Environment handles deletion of object.
438 Object may be deleted by environment immediately.
439 If id of object is 0, assigns a free id to it.
440 Returns the id of the object.
441 Returns 0 if not added and thus deleted.
443 u16 addActiveObject(ClientActiveObject *object);
445 void addActiveObject(u16 id, u8 type, const std::string &init_data);
446 void removeActiveObject(u16 id);
448 void processActiveObjectMessage(u16 id, const std::string &data);
451 Callbacks for activeobjects
454 void damageLocalPlayer(u8 damage, bool handle_hp=true);
457 Client likes to call these
460 // Get all nearby objects
461 void getActiveObjects(v3f origin, f32 max_d,
462 core::array<DistanceSortedActiveObject> &dest);
464 // Get event from queue. CEE_NONE is returned if queue is empty.
465 ClientEnvEvent getClientEvent();
467 std::vector<core::vector2d<int> > attachment_list; // X is child ID, Y is parent ID
471 scene::ISceneManager *m_smgr;
472 ITextureSource *m_texturesource;
474 IrrlichtDevice *m_irr;
475 core::map<u16, ClientActiveObject*> m_active_objects;
476 core::list<ClientSimpleObject*> m_simple_objects;
477 Queue<ClientEnvEvent> m_client_event_queue;
478 IntervalLimiter m_active_object_light_update_interval;
479 IntervalLimiter m_lava_hurt_interval;