]> git.lizzy.rs Git - minetest.git/blobdiff - src/environment.h
MSVC: Generate debug information for release builds (#4674)
[minetest.git] / src / environment.h
index 7100bc5d359c6b0b967da570ffb9214ec66f5452..4bee40e57f223936bf81aebe8d4cd1be8064641f 100644 (file)
@@ -54,6 +54,7 @@ class ClientMap;
 class GameScripting;
 class Player;
 class RemotePlayer;
+class PlayerSAO;
 
 class Environment
 {
@@ -72,15 +73,6 @@ class Environment
 
        virtual Map & getMap() = 0;
 
-       virtual void addPlayer(Player *player);
-       void removePlayer(Player *player);
-       Player * getPlayer(u16 peer_id);
-       Player * getPlayer(const char *name);
-       Player * getRandomConnectedPlayer();
-       Player * getNearestConnectedPlayer(v3f pos);
-       std::vector<Player*> getPlayers();
-       std::vector<Player*> getPlayers(bool ignore_disconnected);
-
        u32 getDayNightRatio();
 
        // 0-23999
@@ -91,40 +83,37 @@ class Environment
        void stepTimeOfDay(float dtime);
 
        void setTimeOfDaySpeed(float speed);
-       float getTimeOfDaySpeed();
 
-       void setDayNightRatioOverride(bool enable, u32 value)
-       {
-               m_day_night_ratio_override_storage = value | ((u64)enable << 63);
-       }
+       void setDayNightRatioOverride(bool enable, u32 value);
+
+       u32 getDayCount();
 
        // counter used internally when triggering ABMs
        u32 m_added_objects;
 
 protected:
-       // peer_ids in here should be unique, except that there may be many 0s
-       std::vector<Player*> m_players;
-
-       // Time of day in milli-hours (0-23999); determines day and night
-       Atomic<u32> m_time_of_day;
+       GenericAtomic<float> m_time_of_day_speed;
 
        /*
-        * Below: values managed by m_time_floats_lock
+        * Below: values managed by m_time_lock
        */
+       // Time of day in milli-hours (0-23999); determines day and night
+       u32 m_time_of_day;
        // Time of day in 0...1
        float m_time_of_day_f;
-       float m_time_of_day_speed;
        // Stores the skew created by the float -> u32 conversion
        // to be applied at next conversion, so that there is no real skew.
        float m_time_conversion_skew;
+       // Overriding the day-night ratio is useful for custom sky visuals
+       bool m_enable_day_night_ratio_override;
+       u32 m_day_night_ratio_override;
+       // Days from the server start, accounts for time shift
+       // in game (e.g. /time or bed usage)
+       Atomic<u32> m_day_count;
        /*
-        * Above: values managed by m_time_floats_lock
+        * Above: values managed by m_time_lock
        */
 
-       // Overriding the day-night ratio is useful for custom sky visuals
-       // lowest 32 bits store the overriden ratio, highest bit stores whether its enabled
-       Atomic<u64> m_day_night_ratio_override_storage;
-
        /* TODO: Add a callback function so these can be updated when a setting
         *       changes.  At this point in time it doesn't matter (e.g. /set
         *       is documented to change server settings only)
@@ -135,15 +124,18 @@ class Environment
         *       a later release.
         */
        bool m_cache_enable_shaders;
+       float m_cache_active_block_mgmt_interval;
+       float m_cache_abm_interval;
+       float m_cache_nodetimer_interval;
 
 private:
-       Mutex m_time_floats_lock;
+       Mutex m_time_lock;
 
        DISABLE_CLASS_COPY(Environment);
 };
 
 /*
-       Active block modifier interface.
+       {Active, Loading} block modifier interface.
 
        These are fed into ServerEnvironment at initialization time;
        ServerEnvironment handles deleting them.
@@ -181,6 +173,77 @@ struct ABMWithState
        ABMWithState(ActiveBlockModifier *abm_);
 };
 
+struct LoadingBlockModifierDef
+{
+       // Set of contents to trigger on
+       std::set<std::string> trigger_contents;
+       std::string name;
+       bool run_at_every_load;
+
+       virtual ~LoadingBlockModifierDef() {}
+       virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
+};
+
+struct LBMContentMapping
+{
+       typedef std::map<content_t, std::vector<LoadingBlockModifierDef *> > container_map;
+       container_map map;
+
+       std::vector<LoadingBlockModifierDef *> lbm_list;
+
+       // Needs to be separate method (not inside destructor),
+       // because the LBMContentMapping may be copied and destructed
+       // many times during operation in the lbm_lookup_map.
+       void deleteContents();
+       void addLBM(LoadingBlockModifierDef *lbm_def, IGameDef *gamedef);
+       const std::vector<LoadingBlockModifierDef *> *lookup(content_t c) const;
+};
+
+class LBMManager
+{
+public:
+       LBMManager():
+               m_query_mode(false)
+       {}
+
+       ~LBMManager();
+
+       // Don't call this after loadIntroductionTimes() ran.
+       void addLBMDef(LoadingBlockModifierDef *lbm_def);
+
+       void loadIntroductionTimes(const std::string &times,
+               IGameDef *gamedef, u32 now);
+
+       // Don't call this before loadIntroductionTimes() ran.
+       std::string createIntroductionTimesString();
+
+       // Don't call this before loadIntroductionTimes() ran.
+       void applyLBMs(ServerEnvironment *env, MapBlock *block, u32 stamp);
+
+       // Warning: do not make this std::unordered_map, order is relevant here
+       typedef std::map<u32, LBMContentMapping> lbm_lookup_map;
+
+private:
+       // Once we set this to true, we can only query,
+       // not modify
+       bool m_query_mode;
+
+       // For m_query_mode == false:
+       // The key of the map is the LBM def's name.
+       // TODO make this std::unordered_map
+       std::map<std::string, LoadingBlockModifierDef *> m_lbm_defs;
+
+       // For m_query_mode == true:
+       // The key of the map is the LBM def's first introduction time.
+       lbm_lookup_map m_lbm_lookup;
+
+       // Returns an iterator to the LBMs that were introduced
+       // after the given time. This is guaranteed to return
+       // valid values for everything
+       lbm_lookup_map::const_iterator getLBMsIntroducedAfter(u32 time)
+       { return m_lbm_lookup.lower_bound(time); }
+};
+
 /*
        List of active blocks, used by ServerEnvironment
 */
@@ -207,12 +270,26 @@ class ActiveBlockList
 private:
 };
 
+/*
+       Operation mode for ServerEnvironment::clearObjects()
+*/
+enum ClearObjectsMode {
+       // Load and go through every mapblock, clearing objects
+       CLEAR_OBJECTS_MODE_FULL,
+
+       // Clear objects immediately in loaded mapblocks;
+       // clear objects in unloaded mapblocks only when the mapblocks are next activated.
+       CLEAR_OBJECTS_MODE_QUICK,
+};
+
 /*
        The server-side environment.
 
        This is not thread-safe. Server uses an environment mutex.
 */
 
+typedef UNORDERED_MAP<u16, ServerActiveObject *> ActiveObjectMap;
+
 class ServerEnvironment : public Environment
 {
 public:
@@ -239,13 +316,22 @@ class ServerEnvironment : public Environment
        // Save players
        void saveLoadedPlayers();
        void savePlayer(RemotePlayer *player);
-       Player *loadPlayer(const std::string &playername);
+       RemotePlayer *loadPlayer(const std::string &playername, PlayerSAO *sao);
+       void addPlayer(RemotePlayer *player);
+       void removePlayer(RemotePlayer *player);
 
        /*
                Save and load time of day and game timer
        */
        void saveMeta();
        void loadMeta();
+       // to be called instead of loadMeta if
+       // env_meta.txt doesn't exist (e.g. new world)
+       void loadDefaultMeta();
+
+       u32 addParticleSpawner(float exptime);
+       u32 addParticleSpawner(float exptime, u16 attached_id);
+       void deleteParticleSpawner(u32 id, bool remove_from_object = true);
 
        /*
                External ActiveObject interface
@@ -277,7 +363,7 @@ class ServerEnvironment : public Environment
                Find out what new objects have been added to
                inside a radius around a position
        */
-       void getAddedActiveObjects(Player *player, s16 radius,
+       void getAddedActiveObjects(PlayerSAO *playersao, s16 radius,
                        s16 player_radius,
                        std::set<u16> &current_objects,
                        std::queue<u16> &added_objects);
@@ -286,7 +372,7 @@ class ServerEnvironment : public Environment
                Find out what new objects have been removed from
                inside a radius around a position
        */
-       void getRemovedActiveObjects(Player* player, s16 radius,
+       void getRemovedActiveObjects(PlayerSAO *playersao, s16 radius,
                        s16 player_radius,
                        std::set<u16> &current_objects,
                        std::queue<u16> &removed_objects);
@@ -304,11 +390,12 @@ class ServerEnvironment : public Environment
        void activateBlock(MapBlock *block, u32 additional_dtime=0);
 
        /*
-               ActiveBlockModifiers
+               {Active,Loading}BlockModifiers
                -------------------------------------------
        */
 
        void addActiveBlockModifier(ActiveBlockModifier *abm);
+       void addLoadingBlockModifierDef(LoadingBlockModifierDef *lbm);
 
        /*
                Other stuff
@@ -323,8 +410,8 @@ class ServerEnvironment : public Environment
        // Find all active objects inside a radius around a point
        void getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius);
 
-       // Clear all objects, loading and going through every MapBlock
-       void clearAllObjects();
+       // Clear objects, loading and going through every MapBlock
+       void clearObjects(ClearObjectsMode mode);
 
        // This makes stuff happen
        void step(f32 dtime);
@@ -344,6 +431,8 @@ class ServerEnvironment : public Environment
        void setStaticForActiveObjectsInBlock(v3s16 blockpos,
                bool static_exists, v3s16 static_block=v3s16(0,0,0));
 
+       RemotePlayer *getPlayer(const u16 peer_id);
+       RemotePlayer *getPlayer(const char* name);
 private:
 
        /*
@@ -397,7 +486,7 @@ class ServerEnvironment : public Environment
        // World path
        const std::string m_path_world;
        // Active object list
-       std::map<u16, ServerActiveObject*> m_active_objects;
+       ActiveObjectMap m_active_objects;
        // Outgoing network message buffer for active objects
        std::queue<ActiveObjectMessage> m_active_object_messages;
        // Some timers
@@ -414,12 +503,25 @@ class ServerEnvironment : public Environment
        u32 m_game_time;
        // A helper variable for incrementing the latter
        float m_game_time_fraction_counter;
+       // Time of last clearObjects call (game time).
+       // When a mapblock older than this is loaded, its objects are cleared.
+       u32 m_last_clear_objects_time;
+       // Active block modifiers
        std::vector<ABMWithState> m_abms;
+       LBMManager m_lbm_mgr;
        // An interval for generally sending object positions and stuff
        float m_recommended_send_interval;
        // Estimate for general maximum lag as determined by server.
        // Can raise to high values like 15s with eg. map generation mods.
        float m_max_lag_estimate;
+
+       // peer_ids in here should be unique, except that there may be many 0s
+       std::vector<RemotePlayer*> m_players;
+
+       // Particles
+       IntervalLimiter m_particle_management_interval;
+       UNORDERED_MAP<u32, float> m_particle_spawners;
+       UNORDERED_MAP<u32, u16> m_particle_spawner_attachments;
 };
 
 #ifndef SERVER
@@ -476,8 +578,8 @@ class ClientEnvironment : public Environment
 
        void step(f32 dtime);
 
-       virtual void addPlayer(Player *player);
-       LocalPlayer * getLocalPlayer();
+       virtual void setLocalPlayer(LocalPlayer *player);
+       LocalPlayer *getLocalPlayer() { return m_local_player; }
 
        /*
                ClientSimpleObjects
@@ -527,24 +629,20 @@ class ClientEnvironment : public Environment
 
        u16 attachement_parent_ids[USHRT_MAX + 1];
 
-       std::list<std::string> getPlayerNames()
-       { return m_player_names; }
-       void addPlayerName(std::string name)
-       { m_player_names.push_back(name); }
-       void removePlayerName(std::string name)
-       { m_player_names.remove(name); }
+       const std::list<std::string> &getPlayerNames() { return m_player_names; }
+       void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
+       void removePlayerName(const std::string &name) { m_player_names.remove(name); }
        void updateCameraOffset(v3s16 camera_offset)
        { m_camera_offset = camera_offset; }
-       v3s16 getCameraOffset()
-       { return m_camera_offset; }
-
+       v3s16 getCameraOffset() const { return m_camera_offset; }
 private:
        ClientMap *m_map;
+       LocalPlayer *m_local_player;
        scene::ISceneManager *m_smgr;
        ITextureSource *m_texturesource;
        IGameDef *m_gamedef;
        IrrlichtDevice *m_irr;
-       std::map<u16, ClientActiveObject*> m_active_objects;
+       UNORDERED_MAP<u16, ClientActiveObject*> m_active_objects;
        std::vector<ClientSimpleObject*> m_simple_objects;
        std::queue<ClientEnvEvent> m_client_event_queue;
        IntervalLimiter m_active_object_light_update_interval;