summaryrefslogtreecommitdiff
path: root/src/cat/LuaScriptInterface.cpp
diff options
context:
space:
mode:
authorjacob1 <jfu614@gmail.com>2013-06-13 14:57:54 (GMT)
committer jacob1 <jfu614@gmail.com>2013-06-13 14:57:54 (GMT)
commit688d82b4b940ed54c88e64f07301d6a077987f12 (patch)
treee2c74b7626ee11124112f2fc1d832601e16f279c /src/cat/LuaScriptInterface.cpp
parent99a15a19d081a3691a1a684300c39eb4084976cb (diff)
downloadpowder-688d82b4b940ed54c88e64f07301d6a077987f12.zip
powder-688d82b4b940ed54c88e64f07301d6a077987f12.tar.gz
add back sim.partNeighbors + some alternate spellings
Diffstat (limited to 'src/cat/LuaScriptInterface.cpp')
-rw-r--r--src/cat/LuaScriptInterface.cpp133
1 files changed, 88 insertions, 45 deletions
diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp
index 4867009..365d5ce 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 LuaScriptInterface::simulation_partNeighbours(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
+ lua_newtable(l);
+ int id = 0;
+ if(lua_gettop(l) == 4)
{
- 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 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++);
+ }
+ }
-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);
+ }
+ 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()