3 Copyright (C) 2010-2013 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
35 #include "irrlichttypes_extrabloated.h"
38 #include "activeobject.h"
39 #include "util/container.h"
40 #include "util/numeric.h"
44 class ServerEnvironment;
45 class ActiveBlockModifier;
46 class ServerActiveObject;
47 typedef struct lua_State lua_State;
58 // Environment will delete the map passed to the constructor
60 virtual ~Environment();
63 Step everything in environment.
68 virtual void step(f32 dtime) = 0;
70 virtual Map & getMap() = 0;
72 virtual void addPlayer(Player *player);
73 void removePlayer(u16 peer_id);
74 Player * getPlayer(u16 peer_id);
75 Player * getPlayer(const char *name);
76 Player * getRandomConnectedPlayer();
77 Player * getNearestConnectedPlayer(v3f pos);
78 std::list<Player*> getPlayers();
79 std::list<Player*> getPlayers(bool ignore_disconnected);
80 void printPlayers(std::ostream &o);
82 u32 getDayNightRatio();
85 virtual void setTimeOfDay(u32 time)
88 m_time_of_day_f = (float)time / 24000.0;
92 { return m_time_of_day; }
95 { return m_time_of_day_f; }
97 void stepTimeOfDay(float dtime);
99 void setTimeOfDaySpeed(float speed)
100 { m_time_of_day_speed = speed; }
102 float getTimeOfDaySpeed()
103 { return m_time_of_day_speed; }
106 // peer_ids in here should be unique, except that there may be many 0s
107 std::list<Player*> m_players;
108 // Time of day in milli-hours (0-23999); determines day and night
110 // Time of day in 0...1
111 float m_time_of_day_f;
112 float m_time_of_day_speed;
113 // Used to buffer dtime for adding to m_time_of_day
114 float m_time_counter;
118 Active block modifier interface.
120 These are fed into ServerEnvironment at initialization time;
121 ServerEnvironment handles deleting them.
124 class ActiveBlockModifier
127 ActiveBlockModifier(){};
128 virtual ~ActiveBlockModifier(){};
130 // Set of contents to trigger on
131 virtual std::set<std::string> getTriggerContents()=0;
132 // Set of required neighbors (trigger doesn't happen if none are found)
133 // Empty = do not check neighbors
134 virtual std::set<std::string> getRequiredNeighbors()
135 { return std::set<std::string>(); }
136 // Trigger interval in seconds
137 virtual float getTriggerInterval() = 0;
138 // Random chance of (1 / return value), 0 is disallowed
139 virtual u32 getTriggerChance() = 0;
140 // This is called usually at interval for 1/chance of the nodes
141 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
142 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
143 u32 active_object_count, u32 active_object_count_wider){};
148 ActiveBlockModifier *abm;
151 ABMWithState(ActiveBlockModifier *abm_);
155 List of active blocks, used by ServerEnvironment
158 class ActiveBlockList
161 void update(std::list<v3s16> &active_positions,
163 std::set<v3s16> &blocks_removed,
164 std::set<v3s16> &blocks_added);
166 bool contains(v3s16 p){
167 return (m_list.find(p) != m_list.end());
174 std::set<v3s16> m_list;
179 class IBackgroundBlockEmerger
182 virtual void queueBlockEmerge(v3s16 blockpos, bool allow_generate)=0;
186 The server-side environment.
188 This is not thread-safe. Server uses an environment mutex.
191 class ServerEnvironment : public Environment
194 ServerEnvironment(ServerMap *map, ScriptApi *iface, IGameDef *gamedef,
195 IBackgroundBlockEmerger *emerger);
196 ~ServerEnvironment();
200 ServerMap & getServerMap();
202 //TODO find way to remove this fct!
203 ScriptApi* getScriptIface()
206 IGameDef *getGameDef()
207 { return m_gamedef; }
209 float getSendRecommendedInterval()
210 { return m_recommended_send_interval; }
215 void serializePlayers(const std::string &savedir);
216 void deSerializePlayers(const std::string &savedir);
219 Save and load time of day and game timer
221 void saveMeta(const std::string &savedir);
222 void loadMeta(const std::string &savedir);
225 External ActiveObject interface
226 -------------------------------------------
229 ServerActiveObject* getActiveObject(u16 id);
232 Add an active object to the environment.
233 Environment handles deletion of object.
234 Object may be deleted by environment immediately.
235 If id of object is 0, assigns a free id to it.
236 Returns the id of the object.
237 Returns 0 if not added and thus deleted.
239 u16 addActiveObject(ServerActiveObject *object);
242 Add an active object as a static object to the corresponding
244 Caller allocates memory, ServerEnvironment frees memory.
245 Return value: true if succeeded, false if failed.
246 (note: not used, pending removal from engine)
248 //bool addActiveObjectAsStatic(ServerActiveObject *object);
251 Find out what new objects have been added to
252 inside a radius around a position
254 void getAddedActiveObjects(v3s16 pos, s16 radius,
255 std::set<u16> ¤t_objects,
256 std::set<u16> &added_objects);
259 Find out what new objects have been removed from
260 inside a radius around a position
262 void getRemovedActiveObjects(v3s16 pos, s16 radius,
263 std::set<u16> ¤t_objects,
264 std::set<u16> &removed_objects);
267 Get the next message emitted by some active object.
268 Returns a message with id=0 if no messages are available.
270 ActiveObjectMessage getActiveObjectMessage();
273 Activate objects and dynamically modify for the dtime determined
274 from timestamp and additional_dtime
276 void activateBlock(MapBlock *block, u32 additional_dtime=0);
280 -------------------------------------------
283 void addActiveBlockModifier(ActiveBlockModifier *abm);
287 -------------------------------------------
290 // Script-aware node setters
291 bool setNode(v3s16 p, const MapNode &n);
292 bool removeNode(v3s16 p);
294 // Find all active objects inside a radius around a point
295 std::set<u16> getObjectsInsideRadius(v3f pos, float radius);
297 // Clear all objects, loading and going through every MapBlock
298 void clearAllObjects();
300 // This makes stuff happen
301 void step(f32 dtime);
303 //check if there's a line of sight between two positions
304 bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0);
309 Internal ActiveObject interface
310 -------------------------------------------
314 Add an active object to the environment.
316 Called by addActiveObject.
318 Object may be deleted by environment immediately.
319 If id of object is 0, assigns a free id to it.
320 Returns the id of the object.
321 Returns 0 if not added and thus deleted.
323 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
326 Remove all objects that satisfy (m_removed && m_known_by_count==0)
328 void removeRemovedObjects();
331 Convert stored objects from block to active
333 void activateObjects(MapBlock *block, u32 dtime_s);
336 Convert objects that are not in active blocks to static.
338 If m_known_by_count != 0, active object is not deleted, but static
339 data is still updated.
341 If force_delete is set, active object is deleted nevertheless. It
342 shall only be set so in the destructor of the environment.
344 void deactivateFarObjects(bool force_delete);
356 // Background block emerger (the server, in practice)
357 IBackgroundBlockEmerger *m_emerger;
358 // Active object list
359 std::map<u16, ServerActiveObject*> m_active_objects;
360 // Outgoing network message buffer for active objects
361 Queue<ActiveObjectMessage> m_active_object_messages;
363 float m_random_spawn_timer; // used for experimental code
364 float m_send_recommended_timer;
365 IntervalLimiter m_object_management_interval;
366 // List of active blocks
367 ActiveBlockList m_active_blocks;
368 IntervalLimiter m_active_blocks_management_interval;
369 IntervalLimiter m_active_block_modifier_interval;
370 IntervalLimiter m_active_blocks_nodemetadata_interval;
371 int m_active_block_interval_overload_skip;
372 // Time from the beginning of the game in seconds.
373 // Incremented in step().
375 // A helper variable for incrementing the latter
376 float m_game_time_fraction_counter;
377 std::list<ABMWithState> m_abms;
378 // An interval for generally sending object positions and stuff
379 float m_recommended_send_interval;
384 #include "clientobject.h"
385 class ClientSimpleObject;
388 The client-side environment.
390 This is not thread-safe.
391 Must be called from main (irrlicht) thread (uses the SceneManager)
392 Client uses an environment mutex.
395 enum ClientEnvEventType
402 struct ClientEnvEvent
404 ClientEnvEventType type;
418 class ClientEnvironment : public Environment
421 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
422 ITextureSource *texturesource, IGameDef *gamedef,
423 IrrlichtDevice *device);
424 ~ClientEnvironment();
427 ClientMap & getClientMap();
429 IGameDef *getGameDef()
430 { return m_gamedef; }
432 void step(f32 dtime);
434 virtual void addPlayer(Player *player);
435 LocalPlayer * getLocalPlayer();
441 void addSimpleObject(ClientSimpleObject *simple);
447 ClientActiveObject* getActiveObject(u16 id);
450 Adds an active object to the environment.
451 Environment handles deletion of object.
452 Object may be deleted by environment immediately.
453 If id of object is 0, assigns a free id to it.
454 Returns the id of the object.
455 Returns 0 if not added and thus deleted.
457 u16 addActiveObject(ClientActiveObject *object);
459 void addActiveObject(u16 id, u8 type, const std::string &init_data);
460 void removeActiveObject(u16 id);
462 void processActiveObjectMessage(u16 id, const std::string &data);
465 Callbacks for activeobjects
468 void damageLocalPlayer(u8 damage, bool handle_hp=true);
469 void updateLocalPlayerBreath(u16 breath);
472 Client likes to call these
475 // Get all nearby objects
476 void getActiveObjects(v3f origin, f32 max_d,
477 std::vector<DistanceSortedActiveObject> &dest);
479 // Get event from queue. CEE_NONE is returned if queue is empty.
480 ClientEnvEvent getClientEvent();
482 std::vector<core::vector2d<int> > attachment_list; // X is child ID, Y is parent ID
484 std::list<std::string> getPlayerNames()
485 { return m_player_names; }
486 void addPlayerName(std::string name)
487 { m_player_names.push_back(name); }
488 void removePlayerName(std::string name)
489 { m_player_names.remove(name); }
493 scene::ISceneManager *m_smgr;
494 ITextureSource *m_texturesource;
496 IrrlichtDevice *m_irr;
497 std::map<u16, ClientActiveObject*> m_active_objects;
498 std::list<ClientSimpleObject*> m_simple_objects;
499 Queue<ClientEnvEvent> m_client_event_queue;
500 IntervalLimiter m_active_object_light_update_interval;
501 IntervalLimiter m_lava_hurt_interval;
502 IntervalLimiter m_drowning_interval;
503 IntervalLimiter m_breathing_interval;
504 std::list<std::string> m_player_names;