From 3d600c69558b4b3e3f05b860531942f1ea2cd1c1 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Wed, 1 Jun 2011 12:16:33 +0100 Subject: [PATCH] Add gravity field manipulation to Lua api, move gravity processing to after the particle update (Means a delay of 1 frame, but we get the ability to manipulate the field with Lua) --- build/test.lua | 6 +++- includes/luaconsole.h | 4 ++- src/luaconsole.c | 71 ++++++++++++++++++++++++++++++++++++++++--- src/main.c | 14 ++++----- 4 files changed, 82 insertions(+), 13 deletions(-) diff --git a/build/test.lua b/build/test.lua index fbb062fbd..a41bdd772 100644 --- a/build/test.lua +++ b/build/test.lua @@ -10,5 +10,9 @@ function do_step() end tpt.drawtext(numberthing, 50, "Oh my god, this is amazing", 255, 255, 255, 255) tpt.drawtext(mousex, mousey, "Oh my god, this is amazing", 255, 255, 255, 255) - return true + tpt.reset_velocity(10, 10, 20, 20) + tpt.reset_gravity_field(10, 10, 20, 20) + tpt.set_pressure(10, 10, 20, 20) + tpt.set_gravity(75, 45, 1, 1, 8) + return false end diff --git a/includes/luaconsole.h b/includes/luaconsole.h index 187bf01b5..c64661903 100644 --- a/includes/luaconsole.h +++ b/includes/luaconsole.h @@ -21,7 +21,9 @@ int luatpt_setpause(lua_State* l); int luatpt_togglepause(lua_State* l); int luatpt_setconsole(lua_State* l); int luatpt_log(lua_State* l); -int luatpt_reset_pressure(lua_State* l); +int luatpt_set_pressure(lua_State* l); +int luatpt_set_gravity(lua_State* l); +int luatpt_reset_gravity_field(lua_State* l); int luatpt_reset_velocity(lua_State* l); int luatpt_reset_spark(lua_State* l); int luatpt_set_property(lua_State* l); diff --git a/src/luaconsole.c b/src/luaconsole.c index 9e55d760e..0f1b96e1e 100644 --- a/src/luaconsole.c +++ b/src/luaconsole.c @@ -14,7 +14,9 @@ void luacon_open(){ {"toggle_pause", &luatpt_togglepause}, {"set_console", &luatpt_setconsole}, {"log", &luatpt_log}, - {"reset_pressure", &luatpt_reset_pressure}, + {"set_pressure", &luatpt_set_pressure}, + {"set_gravity", &luatpt_set_gravity}, + {"reset_gravity_field", &luatpt_reset_gravity_field}, {"reset_velocity", &luatpt_reset_velocity}, {"reset_spark", &luatpt_reset_spark}, {"set_property", &luatpt_set_property}, @@ -35,6 +37,7 @@ void luacon_open(){ luaL_openlib(l, "tpt", tptluaapi, 0); } int luacon_step(int mx, int my, int mb, int mbq, char key){ + int tempret = 0; if(step_function && step_function[0]){ //Set mouse globals lua_pushinteger(l, mbq); @@ -46,8 +49,12 @@ int luacon_step(int mx, int my, int mb, int mbq, char key){ lua_setfield(l, LUA_GLOBALSINDEX, "mouseb"); lua_setfield(l, LUA_GLOBALSINDEX, "mousebq"); lua_getfield(l, LUA_GLOBALSINDEX, step_function); - lua_call(l, 0, 0); - return lua_toboolean(l, -1); + lua_call(l, 0, 1); + if(lua_isboolean(l, -1)){ + tempret = lua_toboolean(l, -1); + lua_pop(l, 1); + return tempret; + } } return 0; } @@ -171,7 +178,7 @@ int luatpt_log(lua_State* l) return 0; } -int luatpt_reset_pressure(lua_State* l) +int luatpt_set_pressure(lua_State* l) { int nx, ny; int x1, y1, width, height; @@ -202,6 +209,62 @@ int luatpt_reset_pressure(lua_State* l) return 0; } +int luatpt_set_gravity(lua_State* l) +{ + int nx, ny; + int x1, y1, width, height; + float value; + x1 = abs(luaL_optint(l, 1, 0)); + y1 = abs(luaL_optint(l, 2, 0)); + width = abs(luaL_optint(l, 3, XRES/CELL)); + height = abs(luaL_optint(l, 4, YRES/CELL)); + value = (float)luaL_optint(l, 5, 0.0f); + if(value > 256.0f) + value = 256.0f; + else if(value < -256.0f) + value = -256.0f; + + if(x1 > (XRES/CELL)-1) + x1 = (XRES/CELL)-1; + if(y1 > (YRES/CELL)-1) + y1 = (YRES/CELL)-1; + if(x1+width > (XRES/CELL)-1) + width = (XRES/CELL)-x1; + if(y1+height > (YRES/CELL)-1) + height = (YRES/CELL)-y1; + for (nx = x1; nx (XRES/CELL)-1) + x1 = (XRES/CELL)-1; + if(y1 > (YRES/CELL)-1) + y1 = (YRES/CELL)-1; + if(x1+width > (XRES/CELL)-1) + width = (XRES/CELL)-x1; + if(y1+height > (YRES/CELL)-1) + height = (YRES/CELL)-y1; + for (nx = x1; nx