Make blocks to be loaded from disk when the active block area reaches them
authorPerttu Ahola <celeron55@gmail.com>
Sun, 27 Nov 2011 10:50:35 +0000 (12:50 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:55 +0000 (19:13 +0200)
src/environment.cpp
src/environment.h
src/server.cpp
src/server.h

index 39ecf423c7d0fac289a06aae1df85320b54ba54e..94c88d57c5da7d09e1003aceef4ffc37b40bde08 100644 (file)
@@ -273,10 +273,11 @@ void ActiveBlockList::update(core::list<v3s16> &active_positions,
 */
 
 ServerEnvironment::ServerEnvironment(ServerMap *map, lua_State *L,
-               IGameDef *gamedef):
+               IGameDef *gamedef, IBackgroundBlockEmerger *emerger):
        m_map(map),
        m_lua(L),
        m_gamedef(gamedef),
+       m_emerger(emerger),
        m_random_spawn_timer(3),
        m_send_recommended_timer(0),
        m_game_time(0),
@@ -891,8 +892,12 @@ void ServerEnvironment::step(float dtime)
                                        <<") became active"<<std::endl;*/
 
                        MapBlock *block = m_map->getBlockNoCreateNoEx(p);
-                       if(block==NULL)
+                       if(block==NULL){
+                               // Block needs to be fetched first
+                               m_emerger->queueBlockEmerge(p, false);
+                               m_active_blocks.m_list.remove(p);
                                continue;
+                       }
 
                        activateBlock(block);
                }
index 1abf7386752d0eb24e093ea70d1502125d567cb5..48e9cfa75729a679c46141cc0d393406a33b25e4 100644 (file)
@@ -120,6 +120,12 @@ public:
 private:
 };
 
+class IBackgroundBlockEmerger
+{
+public:
+       virtual void queueBlockEmerge(v3s16 blockpos, bool allow_generate)=0;
+};
+
 /*
        The server-side environment.
 
@@ -129,7 +135,8 @@ private:
 class ServerEnvironment : public Environment
 {
 public:
-       ServerEnvironment(ServerMap *map, lua_State *L, IGameDef *gamedef);
+       ServerEnvironment(ServerMap *map, lua_State *L, IGameDef *gamedef,
+                       IBackgroundBlockEmerger *emerger);
        ~ServerEnvironment();
 
        Map & getMap()
@@ -279,6 +286,8 @@ private:
        lua_State *m_lua;
        // Game definition
        IGameDef *m_gamedef;
+       // Background block emerger (the server, in practice)
+       IBackgroundBlockEmerger *m_emerger;
        // Active object list
        core::map<u16, ServerActiveObject*> m_active_objects;
        // Outgoing network message buffer for active objects
index 7229bec36bbe5f7f434af25f3ae4cb7e48c8395c..58611bed1558738998a6246e3d0073598ebd6880 100644 (file)
@@ -1078,7 +1078,8 @@ Server::Server(
        
        // Initialize Environment
        
-       m_env = new ServerEnvironment(new ServerMap(mapsavedir, this), m_lua, this);
+       m_env = new ServerEnvironment(new ServerMap(mapsavedir, this), m_lua,
+                       this, this);
 
        // Give environment reference to scripting api
        scriptapi_add_environment(m_lua, m_env);
@@ -4590,6 +4591,14 @@ void Server::notifyPlayers(const std::wstring msg)
        BroadcastChatMessage(msg);
 }
 
+void Server::queueBlockEmerge(v3s16 blockpos, bool allow_generate)
+{
+       u8 flags = 0;
+       if(!allow_generate)
+               flags |= BLOCK_EMERGE_FLAG_FROMDISK;
+       m_emerge_queue.addBlock(PEER_ID_INEXISTENT, blockpos, flags);
+}
+
 // IGameDef interface
 // Under envlock
 IToolDefManager* Server::getToolDefManager()
index 7620f5341bb291c4fa8fb1a9e24953d71d3765ac..673458882f23898d41ebb922e5f7c0210432112d 100644 (file)
@@ -366,7 +366,8 @@ private:
 };
 
 class Server : public con::PeerHandler, public MapEventReceiver,
-               public InventoryManager, public IGameDef
+               public InventoryManager, public IGameDef,
+               public IBackgroundBlockEmerger
 {
 public:
        /*
@@ -483,6 +484,8 @@ public:
        // Envlock and conlock should be locked when calling this
        void notifyPlayer(const char *name, const std::wstring msg);
        void notifyPlayers(const std::wstring msg);
+
+       void queueBlockEmerge(v3s16 blockpos, bool allow_generate);
        
        // Envlock and conlock should be locked when using Lua
        lua_State *getLua(){ return m_lua; }