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"
26 #include "activeobject.h"
35 class ServerEnvironment;
36 class ActiveBlockModifier;
37 class ServerActiveObject;
39 class ServerScripting;
42 {Active, Loading} block modifier interface.
44 These are fed into ServerEnvironment at initialization time;
45 ServerEnvironment handles deleting them.
48 class ActiveBlockModifier
51 ActiveBlockModifier(){};
52 virtual ~ActiveBlockModifier(){};
54 // Set of contents to trigger on
55 virtual const std::set<std::string> &getTriggerContents() const = 0;
56 // Set of required neighbors (trigger doesn't happen if none are found)
57 // Empty = do not check neighbors
58 virtual const std::set<std::string> &getRequiredNeighbors() const = 0;
59 // Trigger interval in seconds
60 virtual float getTriggerInterval() = 0;
61 // Random chance of (1 / return value), 0 is disallowed
62 virtual u32 getTriggerChance() = 0;
63 // Whether to modify chance to simulate time lost by an unnattended block
64 virtual bool getSimpleCatchUp() = 0;
65 // This is called usually at interval for 1/chance of the nodes
66 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
67 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
68 u32 active_object_count, u32 active_object_count_wider){};
73 ActiveBlockModifier *abm;
76 ABMWithState(ActiveBlockModifier *abm_);
79 struct LoadingBlockModifierDef
81 // Set of contents to trigger on
82 std::set<std::string> trigger_contents;
84 bool run_at_every_load = false;
86 virtual ~LoadingBlockModifierDef() {}
87 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
90 struct LBMContentMapping
92 typedef std::unordered_map<content_t, std::vector<LoadingBlockModifierDef *>> lbm_map;
95 std::vector<LoadingBlockModifierDef *> lbm_list;
97 // Needs to be separate method (not inside destructor),
98 // because the LBMContentMapping may be copied and destructed
99 // many times during operation in the lbm_lookup_map.
100 void deleteContents();
101 void addLBM(LoadingBlockModifierDef *lbm_def, IGameDef *gamedef);
102 const std::vector<LoadingBlockModifierDef *> *lookup(content_t c) const;
111 // Don't call this after loadIntroductionTimes() ran.
112 void addLBMDef(LoadingBlockModifierDef *lbm_def);
114 void loadIntroductionTimes(const std::string ×,
115 IGameDef *gamedef, u32 now);
117 // Don't call this before loadIntroductionTimes() ran.
118 std::string createIntroductionTimesString();
120 // Don't call this before loadIntroductionTimes() ran.
121 void applyLBMs(ServerEnvironment *env, MapBlock *block, u32 stamp);
123 // Warning: do not make this std::unordered_map, order is relevant here
124 typedef std::map<u32, LBMContentMapping> lbm_lookup_map;
127 // Once we set this to true, we can only query,
129 bool m_query_mode = false;
131 // For m_query_mode == false:
132 // The key of the map is the LBM def's name.
133 // TODO make this std::unordered_map
134 std::map<std::string, LoadingBlockModifierDef *> m_lbm_defs;
136 // For m_query_mode == true:
137 // The key of the map is the LBM def's first introduction time.
138 lbm_lookup_map m_lbm_lookup;
140 // Returns an iterator to the LBMs that were introduced
141 // after the given time. This is guaranteed to return
142 // valid values for everything
143 lbm_lookup_map::const_iterator getLBMsIntroducedAfter(u32 time)
144 { return m_lbm_lookup.lower_bound(time); }
148 List of active blocks, used by ServerEnvironment
151 class ActiveBlockList
154 void update(std::vector<v3s16> &active_positions,
156 std::set<v3s16> &blocks_removed,
157 std::set<v3s16> &blocks_added);
159 bool contains(v3s16 p){
160 return (m_list.find(p) != m_list.end());
167 std::set<v3s16> m_list;
168 std::set<v3s16> m_forceloaded_list;
174 Operation mode for ServerEnvironment::clearObjects()
176 enum ClearObjectsMode {
177 // Load and go through every mapblock, clearing objects
178 CLEAR_OBJECTS_MODE_FULL,
180 // Clear objects immediately in loaded mapblocks;
181 // clear objects in unloaded mapblocks only when the mapblocks are next activated.
182 CLEAR_OBJECTS_MODE_QUICK,
186 The server-side environment.
188 This is not thread-safe. Server uses an environment mutex.
191 typedef std::unordered_map<u16, ServerActiveObject *> ServerActiveObjectMap;
193 class ServerEnvironment : public Environment
196 ServerEnvironment(ServerMap *map, ServerScripting *scriptIface,
197 Server *server, const std::string &path_world);
198 ~ServerEnvironment();
202 ServerMap & getServerMap();
204 //TODO find way to remove this fct!
205 ServerScripting* getScriptIface()
211 float getSendRecommendedInterval()
212 { return m_recommended_send_interval; }
214 void kickAllPlayers(AccessDeniedCode reason,
215 const std::string &str_reason, bool reconnect);
217 void saveLoadedPlayers();
218 void savePlayer(RemotePlayer *player);
219 PlayerSAO *loadPlayer(RemotePlayer *player, bool *new_player, u16 peer_id,
220 bool is_singleplayer);
221 void addPlayer(RemotePlayer *player);
222 void removePlayer(RemotePlayer *player);
223 bool removePlayerFromDatabase(const std::string &name);
226 Save and load time of day and game timer
230 // to be called instead of loadMeta if
231 // env_meta.txt doesn't exist (e.g. new world)
232 void loadDefaultMeta();
234 u32 addParticleSpawner(float exptime);
235 u32 addParticleSpawner(float exptime, u16 attached_id);
236 void deleteParticleSpawner(u32 id, bool remove_from_object = true);
239 External ActiveObject interface
240 -------------------------------------------
243 ServerActiveObject* getActiveObject(u16 id);
246 Add an active object to the environment.
247 Environment handles deletion of object.
248 Object may be deleted by environment immediately.
249 If id of object is 0, assigns a free id to it.
250 Returns the id of the object.
251 Returns 0 if not added and thus deleted.
253 u16 addActiveObject(ServerActiveObject *object);
256 Add an active object as a static object to the corresponding
258 Caller allocates memory, ServerEnvironment frees memory.
259 Return value: true if succeeded, false if failed.
260 (note: not used, pending removal from engine)
262 //bool addActiveObjectAsStatic(ServerActiveObject *object);
265 Find out what new objects have been added to
266 inside a radius around a position
268 void getAddedActiveObjects(PlayerSAO *playersao, s16 radius,
270 std::set<u16> ¤t_objects,
271 std::queue<u16> &added_objects);
274 Find out what new objects have been removed from
275 inside a radius around a position
277 void getRemovedActiveObjects(PlayerSAO *playersao, s16 radius,
279 std::set<u16> ¤t_objects,
280 std::queue<u16> &removed_objects);
283 Get the next message emitted by some active object.
284 Returns a message with id=0 if no messages are available.
286 ActiveObjectMessage getActiveObjectMessage();
288 virtual void getSelectedActiveObjects(
289 const core::line3d<f32> &shootline_on_map,
290 std::vector<PointedThing> &objects
294 Activate objects and dynamically modify for the dtime determined
295 from timestamp and additional_dtime
297 void activateBlock(MapBlock *block, u32 additional_dtime=0);
300 {Active,Loading}BlockModifiers
301 -------------------------------------------
304 void addActiveBlockModifier(ActiveBlockModifier *abm);
305 void addLoadingBlockModifierDef(LoadingBlockModifierDef *lbm);
309 -------------------------------------------
312 // Script-aware node setters
313 bool setNode(v3s16 p, const MapNode &n);
314 bool removeNode(v3s16 p);
315 bool swapNode(v3s16 p, const MapNode &n);
317 // Find all active objects inside a radius around a point
318 void getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius);
320 // Clear objects, loading and going through every MapBlock
321 void clearObjects(ClearObjectsMode mode);
323 // This makes stuff happen
324 void step(f32 dtime);
326 //check if there's a line of sight between two positions
327 bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0, v3s16 *p=NULL);
329 u32 getGameTime() const { return m_game_time; }
331 void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
332 float getMaxLagEstimate() { return m_max_lag_estimate; }
334 std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; };
336 // Sets the static object status all the active objects in the specified block
337 // This is only really needed for deleting blocks from the map
338 void setStaticForActiveObjectsInBlock(v3s16 blockpos,
339 bool static_exists, v3s16 static_block=v3s16(0,0,0));
341 RemotePlayer *getPlayer(const u16 peer_id);
342 RemotePlayer *getPlayer(const char* name);
344 static bool migratePlayersDatabase(const GameParams &game_params,
345 const Settings &cmd_args);
348 static PlayerDatabase *openPlayerDatabase(const std::string &name,
349 const std::string &savedir, const Settings &conf);
351 Internal ActiveObject interface
352 -------------------------------------------
356 Add an active object to the environment.
358 Called by addActiveObject.
360 Object may be deleted by environment immediately.
361 If id of object is 0, assigns a free id to it.
362 Returns the id of the object.
363 Returns 0 if not added and thus deleted.
365 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
368 Remove all objects that satisfy (m_removed && m_known_by_count==0)
370 void removeRemovedObjects();
373 Convert stored objects from block to active
375 void activateObjects(MapBlock *block, u32 dtime_s);
378 Convert objects that are not in active blocks to static.
380 If m_known_by_count != 0, active object is not deleted, but static
381 data is still updated.
383 If force_delete is set, active object is deleted nevertheless. It
384 shall only be set so in the destructor of the environment.
386 void deactivateFarObjects(bool force_delete);
395 ServerScripting* m_script;
399 const std::string m_path_world;
400 // Active object list
401 ServerActiveObjectMap m_active_objects;
402 // Outgoing network message buffer for active objects
403 std::queue<ActiveObjectMessage> m_active_object_messages;
405 float m_send_recommended_timer = 0.0f;
406 IntervalLimiter m_object_management_interval;
407 // List of active blocks
408 ActiveBlockList m_active_blocks;
409 IntervalLimiter m_active_blocks_management_interval;
410 IntervalLimiter m_active_block_modifier_interval;
411 IntervalLimiter m_active_blocks_nodemetadata_interval;
412 int m_active_block_interval_overload_skip = 0;
413 // Time from the beginning of the game in seconds.
414 // Incremented in step().
416 // A helper variable for incrementing the latter
417 float m_game_time_fraction_counter = 0.0f;
418 // Time of last clearObjects call (game time).
419 // When a mapblock older than this is loaded, its objects are cleared.
420 u32 m_last_clear_objects_time = 0;
421 // Active block modifiers
422 std::vector<ABMWithState> m_abms;
423 LBMManager m_lbm_mgr;
424 // An interval for generally sending object positions and stuff
425 float m_recommended_send_interval = 0.1f;
426 // Estimate for general maximum lag as determined by server.
427 // Can raise to high values like 15s with eg. map generation mods.
428 float m_max_lag_estimate = 0.1f;
430 // peer_ids in here should be unique, except that there may be many 0s
431 std::vector<RemotePlayer*> m_players;
433 PlayerDatabase *m_player_database = nullptr;
436 IntervalLimiter m_particle_management_interval;
437 std::unordered_map<u32, float> m_particle_spawners;
438 std::unordered_map<u32, u16> m_particle_spawner_attachments;