From e9b86365c8e6bbb27332ad352ad5f1453dc1870e Mon Sep 17 00:00:00 2001 From: jacob1 Date: Wed, 16 Jan 2013 11:42:06 -0500 Subject: [PATCH 1/5] log error messages for update functions --- .gitignore | 1 + src/cat/LegacyLuaAPI.cpp | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 9729c38d2..9ac6e3872 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ *.la *~ *.pref +*.lua stdout.txt stderr.txt build/* diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp index 04d1866ef..ca43fc8b3 100644 --- a/src/cat/LegacyLuaAPI.cpp +++ b/src/cat/LegacyLuaAPI.cpp @@ -628,7 +628,7 @@ int luatpt_getelement(lua_State *l) int luacon_elementReplacement(UPDATE_FUNC_ARGS) { - int retval = 0; + int retval = 0, callret; if(lua_el_func[parts[i].type]){ lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, lua_el_func[parts[i].type]); lua_pushinteger(luacon_ci->l, i); @@ -636,7 +636,9 @@ int luacon_elementReplacement(UPDATE_FUNC_ARGS) lua_pushinteger(luacon_ci->l, y); lua_pushinteger(luacon_ci->l, surround_space); lua_pushinteger(luacon_ci->l, nt); - lua_pcall(luacon_ci->l, 5, 1, 0); + callret = lua_pcall(luacon_ci->l, 5, 1, 0); + if (callret) + luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); if(lua_isboolean(luacon_ci->l, -1)){ retval = lua_toboolean(luacon_ci->l, -1); } @@ -675,13 +677,15 @@ int luatpt_element_func(lua_State *l) int luacon_graphicsReplacement(GRAPHICS_FUNC_ARGS) { - int cache = 0; + int cache = 0, callret; lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, lua_gr_func[cpart->type]); lua_pushinteger(luacon_ci->l, 0); lua_pushinteger(luacon_ci->l, *colr); lua_pushinteger(luacon_ci->l, *colg); lua_pushinteger(luacon_ci->l, *colb); - lua_pcall(luacon_ci->l, 4, 10, 0); + callret = lua_pcall(luacon_ci->l, 4, 10, 0); + if (callret) + luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); cache = luaL_optint(luacon_ci->l, -10, 0); *pixel_mode = luaL_optint(luacon_ci->l, -9, *pixel_mode); @@ -694,6 +698,7 @@ int luacon_graphicsReplacement(GRAPHICS_FUNC_ARGS) *fireg = luaL_optint(luacon_ci->l, -2, *fireg); *fireb = luaL_optint(luacon_ci->l, -1, *fireb); lua_pop(luacon_ci->l, 10); + return cache; } From 639d4fc43c7b89593fc0fa33886c4949bc754c01 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Wed, 16 Jan 2013 14:05:31 -0500 Subject: [PATCH 2/5] implement tpt.set_shortcuts --- src/cat/LegacyLuaAPI.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp index ca43fc8b3..7b2aa2312 100644 --- a/src/cat/LegacyLuaAPI.cpp +++ b/src/cat/LegacyLuaAPI.cpp @@ -488,6 +488,7 @@ int luacon_elementwrite(lua_State* l){ free(key); return 0; } +bool shortcuts = true; int luacon_keyevent(int key, int modifier, int event){ int i = 0, kpcontinue = 1, callret; char tempkey[] = {key, 0}; @@ -509,7 +510,7 @@ int luacon_keyevent(int key, int modifier, int event){ lua_pop(luacon_ci->l, 1); } } - return kpcontinue; + return kpcontinue && shortcuts; } int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel){ int i = 0, mpcontinue = 1, callret; @@ -1409,7 +1410,12 @@ int luatpt_get_name(lua_State* l) int luatpt_set_shortcuts(lua_State* l) { - return luaL_error(l, "set_shortcuts: deprecated"); + int shortcut = luaL_optint(l, 1, 0); + if (shortcut) + shortcuts = true; + else + shortcuts = false; + return 0; } int luatpt_delete(lua_State* l) @@ -1713,7 +1719,7 @@ int luatpt_setfire(lua_State* l) } int luatpt_setdebug(lua_State* l) { - return luaL_error(l, "setdebug: Deprecated"); + return luaL_error(l, "setdebug: Deprecated"); //TODO: maybe use the debugInfo thing in GameController to implement this } int luatpt_setfpscap(lua_State* l) { From fc9f263961ae84c2c517270a0be4c6d62065324d Mon Sep 17 00:00:00 2001 From: jacob1 Date: Wed, 16 Jan 2013 22:34:32 -0500 Subject: [PATCH 3/5] 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(); From f9f79e4f530c38dd93b32505d630797a7ec2002e Mon Sep 17 00:00:00 2001 From: jacob1 Date: Wed, 16 Jan 2013 22:53:46 -0500 Subject: [PATCH 4/5] combine pressure functions, add sim.ambientHeat, sim.velocityX, sim.velocityY --- src/cat/LuaScriptInterface.cpp | 131 +++++++++++++++++++++++++++++---- src/cat/LuaScriptInterface.h | 12 +-- 2 files changed, 122 insertions(+), 21 deletions(-) diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index e814f78c1..4528b2b09 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -406,8 +406,10 @@ void LuaScriptInterface::initSimulationAPI() {"partChangeType", simulation_partChangeType}, {"partCreate", simulation_partCreate}, {"partKill", simulation_partKill}, - {"setPressure", simulation_setPressure}, - {"getPressure", simulation_getPressure}, + {"pressure", simulation_pressure}, + {"ambientHeat", simulation_ambientHeat}, + {"velocityX", simulation_velocityX}, + {"velocityY", simulation_velocityY}, {NULL, NULL} }; luaL_register(l, "simulation", simulationAPIMethods); @@ -521,17 +523,24 @@ int LuaScriptInterface::simulation_partKill(lua_State * l) return 0; } -int LuaScriptInterface::simulation_setPressure(lua_State* l) +int LuaScriptInterface::simulation_pressure(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 = 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); - int x, y, width = 1, height = 1; + if (argCount == 2) + { + lua_pushnumber(l, luacon_sim->pv[y][x]); + return 1; + } + int width = 1, height = 1; float value; - x = lua_tointeger(l, 1); - y = lua_tointeger(l, 2); + luaL_checktype(l, 3, LUA_TNUMBER); if (argCount == 3) value = (float)lua_tonumber(l, 3); else @@ -547,23 +556,119 @@ int LuaScriptInterface::simulation_setPressure(lua_State* l) 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) +int LuaScriptInterface::simulation_ambientHeat(lua_State* l) { + int argCount = lua_gettop(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; + + if (argCount == 2) + { + lua_pushnumber(l, luacon_sim->hv[y][x]); + return 1; + } + int width = 1, height = 1; + float value; + luaL_checktype(l, 3, LUA_TNUMBER); + 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 > MAX_TEMP) + value = MAX_TEMP; + else if(value < MIN_TEMP) + value = MIN_TEMP; + + set_map(x, y, width, height, value, 2); + return 0; +} + +int LuaScriptInterface::simulation_velocityX(lua_State* l) +{ + int argCount = lua_gettop(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); + + if (argCount == 2) + { + lua_pushnumber(l, luacon_sim->vx[y][x]); + return 1; + } + int width = 1, height = 1; + float value; + luaL_checktype(l, 3, LUA_TNUMBER); + 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; + + set_map(x, y, width, height, value, 3); + return 0; +} + +int LuaScriptInterface::simulation_velocityY(lua_State* l) +{ + int argCount = lua_gettop(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); + + if (argCount == 2) + { + lua_pushnumber(l, luacon_sim->vy[y][x]); + return 1; + } + int width = 1, height = 1; + float value; + luaL_checktype(l, 3, LUA_TNUMBER); + 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; + + set_map(x, y, width, height, value, 4); + return 0; } //// Begin Renderer API diff --git a/src/cat/LuaScriptInterface.h b/src/cat/LuaScriptInterface.h index 9dd661b2a..c88b973d4 100644 --- a/src/cat/LuaScriptInterface.h +++ b/src/cat/LuaScriptInterface.h @@ -60,14 +60,10 @@ class LuaScriptInterface: public CommandInterface 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);*/ + static int simulation_pressure(lua_State * l); + static int simulation_velocityX(lua_State * l); + static int simulation_velocityY(lua_State * l); + static int simulation_ambientHeat(lua_State * l); //Renderer void initRendererAPI(); From 17cd0e74f26b28101edf1ded343e83820d3f4357 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Wed, 16 Jan 2013 23:48:09 -0500 Subject: [PATCH 5/5] add gravmap setting. Getting doesn't work right now, and maybe i should check if gravx/y can be modified too --- src/cat/LuaScriptInterface.cpp | 37 +++++++++++++++++++++++++++++++++- src/cat/LuaScriptInterface.h | 1 + 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 4528b2b09..88fe9344f 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -410,6 +410,7 @@ void LuaScriptInterface::initSimulationAPI() {"ambientHeat", simulation_ambientHeat}, {"velocityX", simulation_velocityX}, {"velocityY", simulation_velocityY}, + {"gravMap", simulation_gravMap}, {NULL, NULL} }; luaL_register(l, "simulation", simulationAPIMethods); @@ -452,7 +453,8 @@ void LuaScriptInterface::set_map(int x, int y, int width, int height, float valu luacon_sim->vx[ny][nx] = value; else if (map == 4) luacon_sim->vy[ny][nx] = value; - + else if (map == 5) + luacon_sim->gravmap[ny*XRES/CELL+nx] = value; //gravx/y don't seem to work, but this does. opposite of tpt } } @@ -671,6 +673,39 @@ int LuaScriptInterface::simulation_velocityY(lua_State* l) return 0; } +int LuaScriptInterface::simulation_gravMap(lua_State* l) +{ + int argCount = lua_gettop(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); + + /*if (argCount == 2) + { + lua_pushnumber(l, luacon_sim->gravmap[y*XRES/CELL+x]); + return 1; + }*/ + int width = 1, height = 1; + float value; + luaL_checktype(l, 3, LUA_TNUMBER); + 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); + } + + set_map(x, y, width, height, value, 5); + return 0; +} + //// Begin Renderer API void LuaScriptInterface::initRendererAPI() diff --git a/src/cat/LuaScriptInterface.h b/src/cat/LuaScriptInterface.h index c88b973d4..f7b1a13d3 100644 --- a/src/cat/LuaScriptInterface.h +++ b/src/cat/LuaScriptInterface.h @@ -63,6 +63,7 @@ class LuaScriptInterface: public CommandInterface static int simulation_pressure(lua_State * l); static int simulation_velocityX(lua_State * l); static int simulation_velocityY(lua_State * l); + static int simulation_gravMap(lua_State * l); static int simulation_ambientHeat(lua_State * l); //Renderer