added lua command get_nodes_inside_radius
authorsapier <sapier at gmx dot net>
Sun, 15 Jan 2012 19:09:55 +0000 (20:09 +0100)
committersapier <sapier at gmx dot net>
Sun, 15 Jan 2012 19:09:55 +0000 (20:09 +0100)
src/environment.cpp
src/environment.h
src/scriptapi.cpp

index 3a294086c8ac78a6ee19b9bce7cff7a17868a76c..a3d0950f09641cd138d48e910199b3f5d98e38f4 100644 (file)
@@ -765,6 +765,21 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime)
        abmhandler.apply(block);
 }
 
+core::list<MapNode> ServerEnvironment::getNodesInsideRadius(v3s16 pos, float radius)
+{
+       core::list<MapNode> nodes;
+       for (int i = pos.X - radius; i < pos.X + radius; i ++)
+       for (int j = pos.Y - radius; j < pos.Y + radius; j ++)
+       for (int k = pos.Z - radius; k < pos.Z + radius; k ++) {
+               v3s16 current_pos = v3s16(i,j,k);
+               if (current_pos.getDistanceFrom(pos) < radius) {
+                       MapNode n = m_map->getNodeNoEx(current_pos);
+                       nodes.push_back(n);
+                       }
+               }
+       return nodes;
+}
+
 void ServerEnvironment::addActiveBlockModifier(ActiveBlockModifier *abm)
 {
        m_abms.push_back(ABMWithState(abm));
index beb49885c558bed51858189a5daba427fbb089a3..dd8390c39cd161c716a4fb792efdc251098536b1 100644 (file)
@@ -270,6 +270,9 @@ public:
                -------------------------------------------
        */
        
+       // Find all nodes inside a radius around a point
+       core::list<MapNode> getNodesInsideRadius(v3s16 pos, float radius);
+
        // Find all active objects inside a radius around a point
        std::set<u16> getObjectsInsideRadius(v3f pos, float radius);
        
index d14634276375b7b8a0caabf3ed7cc9bca786408a..39a3b06ff73597c24198ddfa20fa8f0bf954ecc9 100644 (file)
@@ -2602,6 +2602,35 @@ private:
                return 1;
        }
 
+       // EnvRef:get_objects_inside_radius(pos, radius)
+       static int l_get_nodes_inside_radius(lua_State *L)
+       {
+               // Get the table insert function
+               lua_getglobal(L, "table");
+               lua_getfield(L, -1, "insert");
+               int table_insert = lua_gettop(L);
+               // Get environemnt
+               EnvRef *o = checkobject(L, 1);
+               ServerEnvironment *env = o->m_env;
+               if(env == NULL) return 0;
+               // Do it
+               v3s16 pos = read_v3s16(L, 2);
+               float radius = luaL_checknumber(L, 3);// * BS;
+               core::list<MapNode> nodes = env->getNodesInsideRadius(pos, radius);
+               lua_newtable(L);
+               int table = lua_gettop(L);
+               for(core::list<MapNode>::Iterator
+                               i = nodes.begin(); i != nodes.end(); i++){
+                       // Insert object reference into table
+                       lua_pushvalue(L, table_insert);
+                       lua_pushvalue(L, table);
+                       pushnode(L, *i, env->getGameDef()->ndef());
+                       if(lua_pcall(L, 2, 0, 0))
+                               script_error(L, "error: %s", lua_tostring(L, -1));
+               }
+               return 1;
+       }
+
        static int gc_object(lua_State *L) {
                EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
                delete o;
@@ -2679,6 +2708,7 @@ const luaL_reg EnvRef::methods[] = {
        method(EnvRef, get_meta),
        method(EnvRef, get_player_by_name),
        method(EnvRef, get_objects_inside_radius),
+       method(EnvRef, get_nodes_inside_radius),
        {0,0}
 };