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
37 #include "activeobject.h"
38 #include "util/numeric.h"
42 class ServerEnvironment;
43 class ActiveBlockModifier;
44 class ServerActiveObject;
47 class IBackgroundBlockEmerger;
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 std::list<Player*> getPlayers();
78 std::list<Player*> getPlayers(bool ignore_disconnected);
80 u32 getDayNightRatio();
83 virtual void setTimeOfDay(u32 time)
86 m_time_of_day_f = (float)time / 24000.0;
90 { return m_time_of_day; }
93 { return m_time_of_day_f; }
95 void stepTimeOfDay(float dtime);
97 void setTimeOfDaySpeed(float speed)
98 { m_time_of_day_speed = speed; }
100 float getTimeOfDaySpeed()
101 { return m_time_of_day_speed; }
104 // peer_ids in here should be unique, except that there may be many 0s
105 std::list<Player*> m_players;
106 // Time of day in milli-hours (0-23999); determines day and night
108 // Time of day in 0...1
109 float m_time_of_day_f;
110 float m_time_of_day_speed;
111 // Used to buffer dtime for adding to m_time_of_day
112 float m_time_counter;
116 Active block modifier interface.
118 These are fed into ServerEnvironment at initialization time;
119 ServerEnvironment handles deleting them.
122 class ActiveBlockModifier
125 ActiveBlockModifier(){};
126 virtual ~ActiveBlockModifier(){};
128 // Set of contents to trigger on
129 virtual std::set<std::string> getTriggerContents()=0;
130 // Set of required neighbors (trigger doesn't happen if none are found)
131 // Empty = do not check neighbors
132 virtual std::set<std::string> getRequiredNeighbors()
133 { return std::set<std::string>(); }
134 // Trigger interval in seconds
135 virtual float getTriggerInterval() = 0;
136 // Random chance of (1 / return value), 0 is disallowed
137 virtual u32 getTriggerChance() = 0;
138 // This is called usually at interval for 1/chance of the nodes
139 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
140 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
141 u32 active_object_count, u32 active_object_count_wider){};
146 ActiveBlockModifier *abm;
149 ABMWithState(ActiveBlockModifier *abm_);
153 List of active blocks, used by ServerEnvironment
156 class ActiveBlockList
159 void update(std::list<v3s16> &active_positions,
161 std::set<v3s16> &blocks_removed,
162 std::set<v3s16> &blocks_added);
164 bool contains(v3s16 p){
165 return (m_list.find(p) != m_list.end());
172 std::set<v3s16> m_list;
178 The server-side environment.
180 This is not thread-safe. Server uses an environment mutex.
183 class ServerEnvironment : public Environment
186 ServerEnvironment(ServerMap *map, GameScripting *scriptIface,
188 IBackgroundBlockEmerger *emerger);
189 ~ServerEnvironment();
193 ServerMap & getServerMap();
195 //TODO find way to remove this fct!
196 GameScripting* getScriptIface()
199 IGameDef *getGameDef()
200 { return m_gamedef; }
202 float getSendRecommendedInterval()
203 { return m_recommended_send_interval; }
208 void serializePlayers(const std::string &savedir);
209 void deSerializePlayers(const std::string &savedir);
212 Save and load time of day and game timer
214 void saveMeta(const std::string &savedir);
215 void loadMeta(const std::string &savedir);
218 External ActiveObject interface
219 -------------------------------------------
222 ServerActiveObject* getActiveObject(u16 id);
225 Add an active object to the environment.
226 Environment handles deletion of object.
227 Object may be deleted by environment immediately.
228 If id of object is 0, assigns a free id to it.
229 Returns the id of the object.
230 Returns 0 if not added and thus deleted.
232 u16 addActiveObject(ServerActiveObject *object);
235 Add an active object as a static object to the corresponding
237 Caller allocates memory, ServerEnvironment frees memory.
238 Return value: true if succeeded, false if failed.
239 (note: not used, pending removal from engine)
241 //bool addActiveObjectAsStatic(ServerActiveObject *object);
244 Find out what new objects have been added to
245 inside a radius around a position
247 void getAddedActiveObjects(v3s16 pos, s16 radius,
248 std::set<u16> ¤t_objects,
249 std::set<u16> &added_objects);
252 Find out what new objects have been removed from
253 inside a radius around a position
255 void getRemovedActiveObjects(v3s16 pos, s16 radius,
256 std::set<u16> ¤t_objects,
257 std::set<u16> &removed_objects);
260 Get the next message emitted by some active object.
261 Returns a message with id=0 if no messages are available.
263 ActiveObjectMessage getActiveObjectMessage();
266 Activate objects and dynamically modify for the dtime determined
267 from timestamp and additional_dtime
269 void activateBlock(MapBlock *block, u32 additional_dtime=0);
273 -------------------------------------------
276 void addActiveBlockModifier(ActiveBlockModifier *abm);
280 -------------------------------------------
283 // Script-aware node setters
284 bool setNode(v3s16 p, const MapNode &n);
285 bool removeNode(v3s16 p);
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);
296 //check if there's a line of sight between two positions
297 bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0);
299 u32 getGameTime() { return m_game_time; }
301 void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
302 float getMaxLagEstimate() { return m_max_lag_estimate; }
306 Internal ActiveObject interface
307 -------------------------------------------
311 Add an active object to the environment.
313 Called by addActiveObject.
315 Object may be deleted by environment immediately.
316 If id of object is 0, assigns a free id to it.
317 Returns the id of the object.
318 Returns 0 if not added and thus deleted.
320 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
323 Remove all objects that satisfy (m_removed && m_known_by_count==0)
325 void removeRemovedObjects();
328 Convert stored objects from block to active
330 void activateObjects(MapBlock *block, u32 dtime_s);
333 Convert objects that are not in active blocks to static.
335 If m_known_by_count != 0, active object is not deleted, but static
336 data is still updated.
338 If force_delete is set, active object is deleted nevertheless. It
339 shall only be set so in the destructor of the environment.
341 void deactivateFarObjects(bool force_delete);
350 GameScripting* m_script;
353 // Background block emerger (the EmergeManager, in practice)
354 IBackgroundBlockEmerger *m_emerger;
355 // Active object list
356 std::map<u16, ServerActiveObject*> m_active_objects;
357 // Outgoing network message buffer for active objects
358 std::list<ActiveObjectMessage> m_active_object_messages;
360 float m_random_spawn_timer; // used for experimental code
361 float m_send_recommended_timer;
362 IntervalLimiter m_object_management_interval;
363 // List of active blocks
364 ActiveBlockList m_active_blocks;
365 IntervalLimiter m_active_blocks_management_interval;
366 IntervalLimiter m_active_block_modifier_interval;
367 IntervalLimiter m_active_blocks_nodemetadata_interval;
368 int m_active_block_interval_overload_skip;
369 // Time from the beginning of the game in seconds.
370 // Incremented in step().
372 // A helper variable for incrementing the latter
373 float m_game_time_fraction_counter;
374 std::list<ABMWithState> m_abms;
375 // An interval for generally sending object positions and stuff
376 float m_recommended_send_interval;
377 // Estimate for general maximum lag as determined by server.
378 // Can raise to high values like 15s with eg. map generation mods.
379 float m_max_lag_estimate;
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 std::list<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;