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..951a707ef 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; @@ -628,7 +629,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 +637,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 +678,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 +699,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; } @@ -1404,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) @@ -1708,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) { @@ -1819,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..88fe9344f 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -406,6 +406,11 @@ void LuaScriptInterface::initSimulationAPI() {"partChangeType", simulation_partChangeType}, {"partCreate", simulation_partCreate}, {"partKill", simulation_partKill}, + {"pressure", simulation_pressure}, + {"ambientHeat", simulation_ambientHeat}, + {"velocityX", simulation_velocityX}, + {"velocityY", simulation_velocityY}, + {"gravMap", simulation_gravMap}, {NULL, NULL} }; luaL_register(l, "simulation", simulationAPIMethods); @@ -415,6 +420,42 @@ 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; + 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 + } } int LuaScriptInterface::simulation_partNeighbours(lua_State * l) @@ -484,6 +525,186 @@ int LuaScriptInterface::simulation_partKill(lua_State * l) return 0; } +int LuaScriptInterface::simulation_pressure(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->pv[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, 1); + return 0; +} + +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); + + 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; +} + +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 diff --git a/src/cat/LuaScriptInterface.h b/src/cat/LuaScriptInterface.h index 616398aa3..f7b1a13d3 100644 --- a/src/cat/LuaScriptInterface.h +++ b/src/cat/LuaScriptInterface.h @@ -55,10 +55,16 @@ 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_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 void initRendererAPI();