diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 7895311f2..d9afd3df4 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -2072,6 +2072,75 @@ int PartsClosure(lua_State *l) return 0; } +static int NeighboursClosure(lua_State *l) +{ + int cx = lua_tointeger(l, lua_upvalueindex(1)); + int cy = lua_tointeger(l, lua_upvalueindex(2)); + int rx = lua_tointeger(l, lua_upvalueindex(3)); + int ry = lua_tointeger(l, lua_upvalueindex(4)); + int t = lua_tointeger(l, lua_upvalueindex(5)); + int x = lua_tointeger(l, lua_upvalueindex(6)); + int y = lua_tointeger(l, lua_upvalueindex(7)); + while (y <= cy + ry) + { + int px = x; + int py = y; + x += 1; + if (x > cx + rx) + { + x = cx - rx; + y += 1; + } + int r = luacon_sim->pmap[py][px]; + if (!(r && (!t || TYP(r) == t))) // * If not [exists and is of the correct type] + { + r = 0; + } + if (!r) + { + r = luacon_sim->photons[py][px]; + if (!(r && (!t || TYP(r) == t))) // * If not [exists and is of the correct type] + { + r = 0; + } + } + if (r) + { + lua_pushnumber(l, x); + lua_replace(l, lua_upvalueindex(6)); + lua_pushnumber(l, y); + lua_replace(l, lua_upvalueindex(7)); + lua_pushnumber(l, ID(r)); + lua_pushnumber(l, px); + lua_pushnumber(l, py); + return 3; + } + } + return 0; +} + +int LuaScriptInterface::simulation_neighbours(lua_State * l) +{ + int cx = luaL_checkint(l, 1); + int cy = luaL_checkint(l, 2); + int rx = luaL_optint(l, 3, 2); + int ry = luaL_optint(l, 4, 2); + int t = luaL_optint(l, 5, PT_NONE); + if (rx < 0 || ry < 0) + { + luaL_error(l, "Invalid radius"); + } + lua_pushnumber(l, cx); + lua_pushnumber(l, cy); + lua_pushnumber(l, rx); + lua_pushnumber(l, ry); + lua_pushnumber(l, t); + lua_pushnumber(l, cx - rx); + lua_pushnumber(l, cy - ry); + lua_pushcclosure(l, NeighboursClosure, 7); + return 1; +} + int LuaScriptInterface::simulation_parts(lua_State *l) { lua_pushnumber(l, 0); @@ -2105,59 +2174,6 @@ int LuaScriptInterface::simulation_photons(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+sy][x+sx]; - if(!i) - i=luacon_sim->photons[y+sy][x+sx]; - } while(!TYP(i)); - lua_pushnumber(l, x); - lua_replace(l, lua_upvalueindex(5)); - lua_pushnumber(l, y); - lua_replace(l, lua_upvalueindex(6)); - lua_pushnumber(l, ID(i)); - 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; -} - int LuaScriptInterface::simulation_framerender(lua_State * l) { if (lua_gettop(l) == 0)