summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2011-06-01 11:16:33 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2011-06-01 11:16:33 (GMT)
commit3d600c69558b4b3e3f05b860531942f1ea2cd1c1 (patch)
treea9613463a3f70f856097786ea2c47a438b459e07 /src
parent2c8c4bc567b09996ee69584a7229356cd411c51a (diff)
downloadpowder-3d600c69558b4b3e3f05b860531942f1ea2cd1c1.zip
powder-3d600c69558b4b3e3f05b860531942f1ea2cd1c1.tar.gz
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)
Diffstat (limited to 'src')
-rw-r--r--src/luaconsole.c71
-rw-r--r--src/main.c14
2 files changed, 74 insertions, 11 deletions
diff --git a/src/luaconsole.c b/src/luaconsole.c
index 9e55d76..0f1b96e 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<x1+width; nx++)
+ for (ny = y1; ny<y1+height; ny++)
+ {
+ gravmap[ny][nx] = value;
+ }
+ return 0;
+}
+
+int luatpt_reset_gravity_field(lua_State* l)
+{
+ int nx, ny;
+ int x1, y1, width, height;
+ 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));
+ 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<x1+width; nx++)
+ for (ny = y1; ny<y1+height; ny++)
+ {
+ gravx[ny][nx] = 0;
+ gravy[ny][nx] = 0;
+ }
+ return 0;
+}
+
int luatpt_reset_velocity(lua_State* l)
{
int nx, ny;
diff --git a/src/main.c b/src/main.c
index bbaee68..8b3d96a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1775,7 +1775,13 @@ int main(int argc, char *argv[])
bsy = 1180;
if (bsy<0)
bsy = 0;
-
+
+ if(ngrav_enable)
+ draw_grav(vid_buf);
+ draw_walls(vid_buf);
+ update_particles(vid_buf); //update everything
+ draw_parts(vid_buf); //draw particles
+
if(ngrav_enable){
pthread_mutex_lock(&gravmutex);
result = grav_ready;
@@ -1794,12 +1800,6 @@ int main(int argc, char *argv[])
if (!sys_pause||framerender) //Only update if not paused
memset(gravmap, 0, sizeof(gravmap)); //Clear the old gravmap
-
- if(ngrav_enable)
- draw_grav(vid_buf);
- draw_walls(vid_buf);
- update_particles(vid_buf); //update everything
- draw_parts(vid_buf); //draw particles
if (cmode==CM_PERS)
{