diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 4867009dd..365d5cea5 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -438,6 +438,7 @@ void LuaScriptInterface::initSimulationAPI() //Methods struct luaL_reg simulationAPIMethods [] = { {"partNeighbours", simulation_partNeighbours}, + {"partNeighbors", simulation_partNeighbours}, {"partChangeType", simulation_partChangeType}, {"partCreate", simulation_partCreate}, {"partProperty", simulation_partProperty}, @@ -479,10 +480,13 @@ void LuaScriptInterface::initSimulationAPI() {"gravityMode", simulation_gravityMode}, {"airMode", simulation_airMode}, {"waterEqualisation", simulation_waterEqualisation}, + {"waterEqualization", simulation_waterEqualisation}, {"ambientAirTemp", simulation_ambientAirTemp}, {"elementCount", simulation_elementCount}, {"parts", simulation_parts}, {"pmap", simulation_pmap}, + {"neighbours", simulation_neighbours}, + {"neighbors", simulation_neighbours}, {NULL, NULL} }; luaL_register(l, "simulation", simulationAPIMethods); @@ -556,54 +560,41 @@ void LuaScriptInterface::set_map(int x, int y, int width, int height, float valu } } -int NeighboursClosure(lua_State * l) -{ - int rx=lua_tointeger(l, lua_upvalueindex(1)); - int ry=lua_tointeger(l, lua_upvalueindex(2)); - int sx=lua_tointeger(l, lua_upvalueindex(3)); - int sy=lua_tointeger(l, lua_upvalueindex(4)); - int x=lua_tointeger(l, lua_upvalueindex(5)); - int y=lua_tointeger(l, lua_upvalueindex(6)); - int i; - do - { - x++; - if(x>rx) - { - x=-rx; - y++; - if(y>ry) - return 0; - } - if(sx+x<0 || sy+y<0 || sx+x>=XRES*CELL || sy+y>=YRES*CELL) - { - continue; - } - i=luacon_sim->pmap[y+sx][x+sx]; - } while(!i&0xFF); - lua_pushnumber(l, x); - lua_replace(l, lua_upvalueindex(5)); - lua_pushnumber(l, y); - lua_replace(l, lua_upvalueindex(6)); - lua_pushnumber(l, i>>8); - lua_pushnumber(l, x+sx); - lua_pushnumber(l, y+sy); - return 3; -} - int LuaScriptInterface::simulation_partNeighbours(lua_State * l) { - int x=luaL_checkint(l, 1); - int y=luaL_checkint(l, 2); - int rx=luaL_optint(l, 3, 2); - int ry=luaL_optint(l, 4, 2); - lua_pushnumber(l, rx); - lua_pushnumber(l, ry); - lua_pushnumber(l, x); - lua_pushnumber(l, y); - lua_pushnumber(l, -rx-1); - lua_pushnumber(l, -ry); - lua_pushcclosure(l, NeighboursClosure, 6); + lua_newtable(l); + int id = 0; + if(lua_gettop(l) == 4) + { + int x = lua_tointeger(l, 1), y = lua_tointeger(l, 2), r = lua_tointeger(l, 3), t = lua_tointeger(l, 4), rx, ry, n; + for (rx = -r; rx <= r; rx++) + for (ry = -r; ry <= r; ry++) + if (x+rx >= 0 && y+ry >= 0 && x+rx < XRES && y+ry < YRES && (rx || ry)) + { + n = luacon_sim->pmap[y+ry][x+rx]; + if(n && (n&0xFF) == t) + { + lua_pushinteger(l, n>>8); + lua_rawseti(l, -2, id++); + } + } + + } + else + { + int x = lua_tointeger(l, 1), y = lua_tointeger(l, 2), r = lua_tointeger(l, 3), rx, ry, n; + for (rx = -r; rx <= r; rx++) + for (ry = -r; ry <= r; ry++) + if (x+rx >= 0 && y+ry >= 0 && x+rx < XRES && y+ry < YRES && (rx || ry)) + { + n = luacon_sim->pmap[y+ry][x+rx]; + if(n) + { + lua_pushinteger(l, n>>8); + lua_rawseti(l, -2, id++); + } + } + } return 1; } @@ -1557,6 +1548,58 @@ int LuaScriptInterface::simulation_pmap(lua_State * l) return 1; } + +int NeighboursClosure(lua_State * l) +{ + int rx=lua_tointeger(l, lua_upvalueindex(1)); + int ry=lua_tointeger(l, lua_upvalueindex(2)); + int sx=lua_tointeger(l, lua_upvalueindex(3)); + int sy=lua_tointeger(l, lua_upvalueindex(4)); + int x=lua_tointeger(l, lua_upvalueindex(5)); + int y=lua_tointeger(l, lua_upvalueindex(6)); + int i = 0; + do + { + x++; + if(x>rx) + { + x=-rx; + y++; + if(y>ry) + return 0; + } + if(!(x && y) || sx+x<0 || sy+y<0 || sx+x>=XRES*CELL || sy+y>=YRES*CELL) + { + continue; + } + i=luacon_sim->pmap[y+sx][x+sx]; + } while(!i&0xFF); + lua_pushnumber(l, x); + lua_replace(l, lua_upvalueindex(5)); + lua_pushnumber(l, y); + lua_replace(l, lua_upvalueindex(6)); + lua_pushnumber(l, i>>8); + lua_pushnumber(l, x+sx); + lua_pushnumber(l, y+sy); + return 3; +} + +int LuaScriptInterface::simulation_neighbours(lua_State * l) +{ + int x=luaL_checkint(l, 1); + int y=luaL_checkint(l, 2); + int rx=luaL_optint(l, 3, 2); + int ry=luaL_optint(l, 4, 2); + lua_pushnumber(l, rx); + lua_pushnumber(l, ry); + lua_pushnumber(l, x); + lua_pushnumber(l, y); + lua_pushnumber(l, -rx-1); + lua_pushnumber(l, -ry); + lua_pushcclosure(l, NeighboursClosure, 6); + return 1; +} + //// Begin Renderer API void LuaScriptInterface::initRendererAPI() diff --git a/src/cat/LuaScriptInterface.h b/src/cat/LuaScriptInterface.h index 1b3d1df9a..298385f54 100644 --- a/src/cat/LuaScriptInterface.h +++ b/src/cat/LuaScriptInterface.h @@ -103,6 +103,7 @@ class LuaScriptInterface: public CommandInterface static int simulation_elementCount(lua_State * l); static int simulation_parts(lua_State * l); static int simulation_pmap(lua_State * l); + static int simulation_neighbours(lua_State * l); //Renderer void initRendererAPI();