From fc9f263961ae84c2c517270a0be4c6d62065324d Mon Sep 17 00:00:00 2001 From: jacob1 Date: Wed, 16 Jan 2013 22:34:32 -0500 Subject: [PATCH] sim.get/setPressure, a few constants to the simulation api --- src/cat/LegacyLuaAPI.cpp | 1 - src/cat/LuaScriptInterface.cpp | 81 ++++++++++++++++++++++++++++++++++ src/cat/LuaScriptInterface.h | 9 ++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp index 7b2aa2312..951a707ef 100644 --- a/src/cat/LegacyLuaAPI.cpp +++ b/src/cat/LegacyLuaAPI.cpp @@ -1830,7 +1830,6 @@ int screenshotIndex = 0; int luatpt_screenshot(lua_State* l) { - //TODO Implement int captureUI = luaL_optint(l, 1, 0); std::vector data; if(captureUI) diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 0082f4a23..e814f78c1 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -406,6 +406,8 @@ void LuaScriptInterface::initSimulationAPI() {"partChangeType", simulation_partChangeType}, {"partCreate", simulation_partCreate}, {"partKill", simulation_partKill}, + {"setPressure", simulation_setPressure}, + {"getPressure", simulation_getPressure}, {NULL, NULL} }; luaL_register(l, "simulation", simulationAPIMethods); @@ -415,6 +417,41 @@ void LuaScriptInterface::initSimulationAPI() lua_getglobal(l, "simulation"); lua_setglobal(l, "sim"); + //Static values + lua_pushinteger(l, XRES); lua_setfield(l, simulationAPI, "XRES"); + lua_pushinteger(l, YRES); lua_setfield(l, simulationAPI, "YRES"); + lua_pushinteger(l, PT_NUM); lua_setfield(l, simulationAPI, "PT_NUM"); + lua_pushinteger(l, luacon_sim->NUM_PARTS); lua_setfield(l, simulationAPI, "NUM_PARTS"); + lua_pushinteger(l, R_TEMP); lua_setfield(l, simulationAPI, "R_TEMP"); + lua_pushinteger(l, MAX_TEMP); lua_setfield(l, simulationAPI, "MAX_TEMP"); + lua_pushinteger(l, MIN_TEMP); lua_setfield(l, simulationAPI, "MIN_TEMP"); + +} + +void LuaScriptInterface::set_map(int x, int y, int width, int height, float value, int map) // A function so this won't need to be repeated many times later +{ + int nx, ny; + if(x > (XRES/CELL)-1) + x = (XRES/CELL)-1; + if(y > (YRES/CELL)-1) + y = (YRES/CELL)-1; + if(x+width > (XRES/CELL)-1) + width = (XRES/CELL)-x; + if(y+height > (YRES/CELL)-1) + height = (YRES/CELL)-y; + for (nx = x; nxpv[ny][nx] = value; + else if (map == 2) + luacon_sim->hv[ny][nx] = value; + else if (map == 3) + luacon_sim->vx[ny][nx] = value; + else if (map == 4) + luacon_sim->vy[ny][nx] = value; + + } } int LuaScriptInterface::simulation_partNeighbours(lua_State * l) @@ -484,6 +521,50 @@ int LuaScriptInterface::simulation_partKill(lua_State * l) return 0; } +int LuaScriptInterface::simulation_setPressure(lua_State* l) +{ + int argCount = lua_gettop(l); + luaL_checktype(l, 1, LUA_TNUMBER); + luaL_checktype(l, 2, LUA_TNUMBER); + luaL_checktype(l, 3, LUA_TNUMBER); + + int x, y, width = 1, height = 1; + float value; + x = lua_tointeger(l, 1); + y = lua_tointeger(l, 2); + if (argCount == 3) + value = (float)lua_tonumber(l, 3); + else + { + luaL_checktype(l, 4, LUA_TNUMBER); + luaL_checktype(l, 5, LUA_TNUMBER); + width = lua_tointeger(l, 3); + height = lua_tointeger(l, 4); + value = (float)lua_tonumber(l, 5); + } + if(value > 256.0f) + value = 256.0f; + else if(value < -256.0f) + value = -256.0f; + + if (x*CELL<0 || y*CELL<0 || x*CELL>=XRES || y*CELL>=YRES) + return luaL_error(l, "coordinates out of range (%d,%d)", x, y); + + set_map(x, y, width, height, value, 1); + return 0; +} + +int LuaScriptInterface::simulation_getPressure(lua_State* l) +{ + luaL_checktype(l, 1, LUA_TNUMBER); + luaL_checktype(l, 2, LUA_TNUMBER); + int x = lua_tointeger(l, 1); + int y = lua_tointeger(l, 2); + if (x*CELL<0 || y*CELL<0 || x*CELL>=XRES || y*CELL>=YRES) + return luaL_error(l, "coordinates out of range (%d,%d)", x, y); + lua_pushnumber(l, luacon_sim->pv[y][x]); + return 1; +} //// Begin Renderer API diff --git a/src/cat/LuaScriptInterface.h b/src/cat/LuaScriptInterface.h index 616398aa3..9dd661b2a 100644 --- a/src/cat/LuaScriptInterface.h +++ b/src/cat/LuaScriptInterface.h @@ -55,10 +55,19 @@ class LuaScriptInterface: public CommandInterface //Simulation void initSimulationAPI(); + static void set_map(int x, int y, int width, int height, float value, int mapType); static int simulation_partNeighbours(lua_State * l); static int simulation_partChangeType(lua_State * l); static int simulation_partCreate(lua_State * l); static int simulation_partKill(lua_State * l); + static int simulation_setPressure(lua_State * l); + static int simulation_getPressure(lua_State * l); + /*static int simulation_partKill(lua_State * l); + static int simulation_partKill(lua_State * l); + static int simulation_partKill(lua_State * l); + static int simulation_partKill(lua_State * l); + static int simulation_partKill(lua_State * l); + static int simulation_partKill(lua_State * l);*/ //Renderer void initRendererAPI();