summaryrefslogtreecommitdiff
path: root/src/cat/LuaScriptInterface.cpp
diff options
context:
space:
mode:
authormniip <mniip@mniip.com>2013-06-12 17:42:15 (GMT)
committer mniip <mniip@mniip.com>2013-06-12 17:42:15 (GMT)
commit7c25baa9a90f50f5cc9b54b62f23decc6001ee5f (patch)
treeb6007e351ddd0028e684c34348a2e0f72a6318cf /src/cat/LuaScriptInterface.cpp
parent17de12dc8bf4a429bb80ecdfb6d960194dd7aaf2 (diff)
downloadpowder-7c25baa9a90f50f5cc9b54b62f23decc6001ee5f.zip
powder-7c25baa9a90f50f5cc9b54b62f23decc6001ee5f.tar.gz
simulation.partNeighbours() iterator
// no one uses it yet, so it shouldn't break anythign really
Diffstat (limited to 'src/cat/LuaScriptInterface.cpp')
-rw-r--r--src/cat/LuaScriptInterface.cpp85
1 files changed, 50 insertions, 35 deletions
diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp
index 4d7f643..27b25cc 100644
--- a/src/cat/LuaScriptInterface.cpp
+++ b/src/cat/LuaScriptInterface.cpp
@@ -555,41 +555,55 @@ 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 ids = 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)
- {
- ids++;
- lua_pushinteger(l, n>>8);
- }
- }
-
- }
- 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)
- {
- ids++;
- lua_pushinteger(l, n>>8);
- }
- }
- }
- return ids;
+ 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_partChangeType(lua_State * l)
@@ -1508,12 +1522,13 @@ int LuaScriptInterface::simulation_elementCount(lua_State * l)
int PartsClosure(lua_State * l)
{
int i = lua_tointeger(l, lua_upvalueindex(1));
- i++;
- while(!luacon_sim->parts[i].type)
+ do
+ {
if(i>=NPART)
return 0;
else
i++;
+ } while(!luacon_sim->parts[i].type);
lua_pushnumber(l, i);
lua_replace(l, lua_upvalueindex(1));
lua_pushnumber(l, i);