3 Copyright (C) 2010-2017 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 SERVER_ENVIRONMENT_HEADER
21 #define SERVER_ENVIRONMENT_HEADER
23 #include "environment.h"
29 class ServerEnvironment;
30 class ActiveBlockModifier;
31 class ServerActiveObject;
36 {Active, Loading} block modifier interface.
38 These are fed into ServerEnvironment at initialization time;
39 ServerEnvironment handles deleting them.
42 class ActiveBlockModifier
45 ActiveBlockModifier(){};
46 virtual ~ActiveBlockModifier(){};
48 // Set of contents to trigger on
49 virtual std::set<std::string> getTriggerContents()=0;
50 // Set of required neighbors (trigger doesn't happen if none are found)
51 // Empty = do not check neighbors
52 virtual std::set<std::string> getRequiredNeighbors()
53 { return std::set<std::string>(); }
54 // Trigger interval in seconds
55 virtual float getTriggerInterval() = 0;
56 // Random chance of (1 / return value), 0 is disallowed
57 virtual u32 getTriggerChance() = 0;
58 // Whether to modify chance to simulate time lost by an unnattended block
59 virtual bool getSimpleCatchUp() = 0;
60 // This is called usually at interval for 1/chance of the nodes
61 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
62 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
63 u32 active_object_count, u32 active_object_count_wider){};
68 ActiveBlockModifier *abm;
71 ABMWithState(ActiveBlockModifier *abm_);
74 struct LoadingBlockModifierDef
76 // Set of contents to trigger on
77 std::set<std::string> trigger_contents;
79 bool run_at_every_load;
81 virtual ~LoadingBlockModifierDef() {}
82 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
85 struct LBMContentMapping
87 typedef std::map<content_t, std::vector<LoadingBlockModifierDef *> > container_map;
90 std::vector<LoadingBlockModifierDef *> lbm_list;
92 // Needs to be separate method (not inside destructor),
93 // because the LBMContentMapping may be copied and destructed
94 // many times during operation in the lbm_lookup_map.
95 void deleteContents();
96 void addLBM(LoadingBlockModifierDef *lbm_def, IGameDef *gamedef);
97 const std::vector<LoadingBlockModifierDef *> *lookup(content_t c) const;
109 // Don't call this after loadIntroductionTimes() ran.
110 void addLBMDef(LoadingBlockModifierDef *lbm_def);
112 void loadIntroductionTimes(const std::string ×,
113 IGameDef *gamedef, u32 now);
115 // Don't call this before loadIntroductionTimes() ran.
116 std::string createIntroductionTimesString();
118 // Don't call this before loadIntroductionTimes() ran.
119 void applyLBMs(ServerEnvironment *env, MapBlock *block, u32 stamp);
121 // Warning: do not make this std::unordered_map, order is relevant here
122 typedef std::map<u32, LBMContentMapping> lbm_lookup_map;
125 // Once we set this to true, we can only query,
129 // For m_query_mode == false:
130 // The key of the map is the LBM def's name.
131 // TODO make this std::unordered_map
132 std::map<std::string, LoadingBlockModifierDef *> m_lbm_defs;
134 // For m_query_mode == true:
135 // The key of the map is the LBM def's first introduction time.
136 lbm_lookup_map m_lbm_lookup;
138 // Returns an iterator to the LBMs that were introduced
139 // after the given time. This is guaranteed to return
140 // valid values for everything
141 lbm_lookup_map::const_iterator getLBMsIntroducedAfter(u32 time)
142 { return m_lbm_lookup.lower_bound(time); }
146 List of active blocks, used by ServerEnvironment
149 class ActiveBlockList
152 void update(std::vector<v3s16> &active_positions,
154 std::set<v3s16> &blocks_removed,
155 std::set<v3s16> &blocks_added);
157 bool contains(v3s16 p){
158 return (m_list.find(p) != m_list.end());
165 std::set<v3s16> m_list;
166 std::set<v3s16> m_forceloaded_list;
172 Operation mode for ServerEnvironment::clearObjects()
174 enum ClearObjectsMode {
175 // Load and go through every mapblock, clearing objects
176 CLEAR_OBJECTS_MODE_FULL,
178 // Clear objects immediately in loaded mapblocks;
179 // clear objects in unloaded mapblocks only when the mapblocks are next activated.
180 CLEAR_OBJECTS_MODE_QUICK,
184 The server-side environment.
186 This is not thread-safe. Server uses an environment mutex.
189 typedef UNORDERED_MAP<u16, ServerActiveObject *> ActiveObjectMap;
191 class ServerEnvironment : public Environment
194 ServerEnvironment(ServerMap *map, GameScripting *scriptIface,
195 Server *server, const std::string &path_world);
196 ~ServerEnvironment();
200 ServerMap & getServerMap();
202 //TODO find way to remove this fct!
203 GameScripting* getScriptIface()
209 float getSendRecommendedInterval()
210 { return m_recommended_send_interval; }
212 void kickAllPlayers(AccessDeniedCode reason,
213 const std::string &str_reason, bool reconnect);
215 void saveLoadedPlayers();
216 void savePlayer(RemotePlayer *player);
217 RemotePlayer *loadPlayer(const std::string &playername, PlayerSAO *sao);
218 void addPlayer(RemotePlayer *player);
219 void removePlayer(RemotePlayer *player);
222 Save and load time of day and game timer
226 // to be called instead of loadMeta if
227 // env_meta.txt doesn't exist (e.g. new world)
228 void loadDefaultMeta();
230 u32 addParticleSpawner(float exptime);
231 u32 addParticleSpawner(float exptime, u16 attached_id);
232 void deleteParticleSpawner(u32 id, bool remove_from_object = true);
235 External ActiveObject interface
236 -------------------------------------------
239 ServerActiveObject* getActiveObject(u16 id);
242 Add an active object to the environment.
243 Environment handles deletion of object.
244 Object may be deleted by environment immediately.
245 If id of object is 0, assigns a free id to it.
246 Returns the id of the object.
247 Returns 0 if not added and thus deleted.
249 u16 addActiveObject(ServerActiveObject *object);
252 Add an active object as a static object to the corresponding
254 Caller allocates memory, ServerEnvironment frees memory.
255 Return value: true if succeeded, false if failed.
256 (note: not used, pending removal from engine)
258 //bool addActiveObjectAsStatic(ServerActiveObject *object);
261 Find out what new objects have been added to
262 inside a radius around a position
264 void getAddedActiveObjects(PlayerSAO *playersao, s16 radius,
266 std::set<u16> ¤t_objects,
267 std::queue<u16> &added_objects);
270 Find out what new objects have been removed from
271 inside a radius around a position
273 void getRemovedActiveObjects(PlayerSAO *playersao, s16 radius,
275 std::set<u16> ¤t_objects,
276 std::queue<u16> &removed_objects);
279 Get the next message emitted by some active object.
280 Returns a message with id=0 if no messages are available.
282 ActiveObjectMessage getActiveObjectMessage();
285 Activate objects and dynamically modify for the dtime determined
286 from timestamp and additional_dtime
288 void activateBlock(MapBlock *block, u32 additional_dtime=0);
291 {Active,Loading}BlockModifiers
292 -------------------------------------------
295 void addActiveBlockModifier(ActiveBlockModifier *abm);
296 void addLoadingBlockModifierDef(LoadingBlockModifierDef *lbm);
300 -------------------------------------------
303 // Script-aware node setters
304 bool setNode(v3s16 p, const MapNode &n);
305 bool removeNode(v3s16 p);
306 bool swapNode(v3s16 p, const MapNode &n);
308 // Find all active objects inside a radius around a point
309 void getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius);
311 // Clear objects, loading and going through every MapBlock
312 void clearObjects(ClearObjectsMode mode);
314 // This makes stuff happen
315 void step(f32 dtime);
317 //check if there's a line of sight between two positions
318 bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0, v3s16 *p=NULL);
320 u32 getGameTime() { return m_game_time; }
322 void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
323 float getMaxLagEstimate() { return m_max_lag_estimate; }
325 std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; };
327 // Sets the static object status all the active objects in the specified block
328 // This is only really needed for deleting blocks from the map
329 void setStaticForActiveObjectsInBlock(v3s16 blockpos,
330 bool static_exists, v3s16 static_block=v3s16(0,0,0));
332 RemotePlayer *getPlayer(const u16 peer_id);
333 RemotePlayer *getPlayer(const char* name);
337 Internal ActiveObject interface
338 -------------------------------------------
342 Add an active object to the environment.
344 Called by addActiveObject.
346 Object may be deleted by environment immediately.
347 If id of object is 0, assigns a free id to it.
348 Returns the id of the object.
349 Returns 0 if not added and thus deleted.
351 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
354 Remove all objects that satisfy (m_removed && m_known_by_count==0)
356 void removeRemovedObjects();
359 Convert stored objects from block to active
361 void activateObjects(MapBlock *block, u32 dtime_s);
364 Convert objects that are not in active blocks to static.
366 If m_known_by_count != 0, active object is not deleted, but static
367 data is still updated.
369 If force_delete is set, active object is deleted nevertheless. It
370 shall only be set so in the destructor of the environment.
372 void deactivateFarObjects(bool force_delete);
381 GameScripting* m_script;
385 const std::string m_path_world;
386 // Active object list
387 ActiveObjectMap m_active_objects;
388 // Outgoing network message buffer for active objects
389 std::queue<ActiveObjectMessage> m_active_object_messages;
391 float m_send_recommended_timer;
392 IntervalLimiter m_object_management_interval;
393 // List of active blocks
394 ActiveBlockList m_active_blocks;
395 IntervalLimiter m_active_blocks_management_interval;
396 IntervalLimiter m_active_block_modifier_interval;
397 IntervalLimiter m_active_blocks_nodemetadata_interval;
398 int m_active_block_interval_overload_skip;
399 // Time from the beginning of the game in seconds.
400 // Incremented in step().
402 // A helper variable for incrementing the latter
403 float m_game_time_fraction_counter;
404 // Time of last clearObjects call (game time).
405 // When a mapblock older than this is loaded, its objects are cleared.
406 u32 m_last_clear_objects_time;
407 // Active block modifiers
408 std::vector<ABMWithState> m_abms;
409 LBMManager m_lbm_mgr;
410 // An interval for generally sending object positions and stuff
411 float m_recommended_send_interval;
412 // Estimate for general maximum lag as determined by server.
413 // Can raise to high values like 15s with eg. map generation mods.
414 float m_max_lag_estimate;
416 // peer_ids in here should be unique, except that there may be many 0s
417 std::vector<RemotePlayer*> m_players;
420 IntervalLimiter m_particle_management_interval;
421 UNORDERED_MAP<u32, float> m_particle_spawners;
422 UNORDERED_MAP<u32, u16> m_particle_spawner_attachments;