Add ServerEnvironment::setNode()/removeNode() to allow setting nodes from the C+...
authorPerttu Ahola <celeron55@gmail.com>
Wed, 2 Jan 2013 21:17:52 +0000 (23:17 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Wed, 2 Jan 2013 21:18:14 +0000 (23:18 +0200)
src/environment.cpp
src/environment.h
src/scriptapi.cpp

index 5bf127a174c658a7c15d4a94705046b08987ae19..020d2b43377f7c80ef27b52aa720a485d7c4866e 100644 (file)
@@ -819,6 +819,45 @@ void ServerEnvironment::addActiveBlockModifier(ActiveBlockModifier *abm)
        m_abms.push_back(ABMWithState(abm));
 }
 
+bool ServerEnvironment::setNode(v3s16 p, const MapNode &n)
+{
+       INodeDefManager *ndef = m_gamedef->ndef();
+       MapNode n_old = m_map->getNodeNoEx(p);
+       // Call destructor
+       if(ndef->get(n_old).has_on_destruct)
+               scriptapi_node_on_destruct(m_lua, p, n_old);
+       // Replace node
+       bool succeeded = m_map->addNodeWithEvent(p, n);
+       if(!succeeded)
+               return false;
+       // Call post-destructor
+       if(ndef->get(n_old).has_after_destruct)
+               scriptapi_node_after_destruct(m_lua, p, n_old);
+       // Call constructor
+       if(ndef->get(n).has_on_construct)
+               scriptapi_node_on_construct(m_lua, p, n);
+       return true;
+}
+
+bool ServerEnvironment::removeNode(v3s16 p)
+{
+       INodeDefManager *ndef = m_gamedef->ndef();
+       MapNode n_old = m_map->getNodeNoEx(p);
+       // Call destructor
+       if(ndef->get(n_old).has_on_destruct)
+               scriptapi_node_on_destruct(m_lua, p, n_old);
+       // Replace with air
+       // This is slightly optimized compared to addNodeWithEvent(air)
+       bool succeeded = m_map->removeNodeWithEvent(p);
+       if(!succeeded)
+               return false;
+       // Call post-destructor
+       if(ndef->get(n_old).has_after_destruct)
+               scriptapi_node_after_destruct(m_lua, p, n_old);
+       // Air doesn't require constructor
+       return true;
+}
+
 std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius)
 {
        std::set<u16> objects;
index 0cc53f9a6dccea2dc8f215f720def5f275a2288e..d17edeacd96e7267f69b2af4543c043fe493a9d8 100644 (file)
@@ -283,6 +283,10 @@ public:
                Other stuff
                -------------------------------------------
        */
+
+       // Script-aware node setters
+       bool setNode(v3s16 p, const MapNode &n);
+       bool removeNode(v3s16 p);
        
        // Find all active objects inside a radius around a point
        std::set<u16> getObjectsInsideRadius(v3f pos, float radius);
index a79622db327020e2d9edfd769250c16ed4daabb9..04f741ad3974a143d7224179b28b7da4780dffee 100644 (file)
@@ -3540,20 +3540,7 @@ private:
                v3s16 pos = read_v3s16(L, 2);
                MapNode n = readnode(L, 3, ndef);
                // Do it
-               MapNode n_old = env->getMap().getNodeNoEx(pos);
-               // Call destructor
-               if(ndef->get(n_old).has_on_destruct)
-                       scriptapi_node_on_destruct(L, pos, n_old);
-               // Replace node
-               bool succeeded = env->getMap().addNodeWithEvent(pos, n);
-               if(succeeded){
-                       // Call post-destructor
-                       if(ndef->get(n_old).has_after_destruct)
-                               scriptapi_node_after_destruct(L, pos, n_old);
-                       // Call constructor
-                       if(ndef->get(n).has_on_construct)
-                               scriptapi_node_on_construct(L, pos, n);
-               }
+               bool succeeded = env->setNode(pos, n);
                lua_pushboolean(L, succeeded);
                return 1;
        }
@@ -3574,20 +3561,8 @@ private:
                // parameters
                v3s16 pos = read_v3s16(L, 2);
                // Do it
-               MapNode n_old = env->getMap().getNodeNoEx(pos);
-               // Call destructor
-               if(ndef->get(n_old).has_on_destruct)
-                       scriptapi_node_on_destruct(L, pos, n_old);
-               // Replace with air
-               // This is slightly optimized compared to addNodeWithEvent(air)
-               bool succeeded = env->getMap().removeNodeWithEvent(pos);
-               if(succeeded){
-                       // Call post-destructor
-                       if(ndef->get(n_old).has_after_destruct)
-                               scriptapi_node_after_destruct(L, pos, n_old);
-               }
+               bool succeeded = env->removeNode(pos);
                lua_pushboolean(L, succeeded);
-               // Air doesn't require constructor
                return 1;
        }