New tables in lua API: el and version: element ids and version info respectively. New lua functions: element (get element id from name) and element_func for hooking/overriding element update functions

This commit is contained in:
Simon Robertshaw 2011-11-18 19:38:08 +00:00
parent 8a962b2f96
commit f508013332
5 changed files with 125 additions and 1 deletions

View File

@ -19,11 +19,14 @@
#define LUACON_KDOWN 1
#define LUACON_KUP 2
int *lua_el_func, *lua_el_mode;
void luacon_open();
int luacon_step(int mx, int my, int selectl, int selectr);
int luacon_mouseevent(int mx, int my, int mb, int event);
int luacon_keyevent(int key, int modifier, int event);
int luacon_eval(char *command);
int luacon_part_update(int t, int i, int x, int y, int surround_space, int nt);
char *luacon_geterror();
void luacon_close();
int process_command_lua(pixel *vid_buf, char *console, char *console_error);
@ -32,6 +35,8 @@ int getPartIndex_curIdx;
//TPT Interface
int luatpt_test(lua_State* l);
int luatpt_getelement(lua_State *l);
int luatpt_element_func(lua_State *l);
int luatpt_drawtext(lua_State* l);
int luatpt_create(lua_State* l);
int luatpt_setpause(lua_State* l);

View File

@ -412,6 +412,12 @@ void clearScreen(float alpha)
glClear(GL_COLOR_BUFFER_BIT);
}
void clearScreenNP(float alpha)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
void ogl_blit(int x, int y, int w, int h, pixel *src, int pitch, int scale)
{

View File

@ -5167,6 +5167,10 @@ char *console_ui(pixel *vid_buf,char error[255],char console_more) {
ui_edit_draw(vid_buf, &ed);
ui_edit_process(mx, my, b, &ed);
sdl_blit(0, 0, (XRES+BARSIZE), YRES+MENUSIZE, vid_buf, (XRES+BARSIZE));
#ifdef OGLR
clearScreenNP(1.0f);
draw_parts_fbo();
#endif
if (sdl_key==SDLK_RETURN)
{
currentcommand = malloc(sizeof(command_history));
@ -6437,7 +6441,10 @@ void render_ui(pixel * vid_buf, int xcoord, int ycoord, int orientation)
}
sdl_blit(0, 0, (XRES+BARSIZE), YRES+MENUSIZE, vid_buf, (XRES+BARSIZE));
#ifdef OGLR
clearScreenNP(1.0f);
draw_parts_fbo();
#endif
if (sdl_key==SDLK_RETURN)
break;
if (sdl_key==SDLK_ESCAPE)

View File

@ -11,7 +11,11 @@ int *keypress_functions = NULL;
int mouseclick_function_count = 0;
int *mouseclick_functions = NULL;
int tptProperties; //Table for some TPT properties
int tptPropertiesVersion;
int tptElements; //Table for TPT element names
void luacon_open(){
int i = 0, j;
char tmpname[12];
const static struct luaL_reg tptluaapi [] = {
{"test", &luatpt_test},
{"drawtext", &luatpt_drawtext},
@ -66,6 +70,8 @@ void luacon_open(){
{"setwindowsize",&luatpt_setwindowsize},
{"watertest",&luatpt_togglewater},
{"screenshot",&luatpt_screenshot},
{"element",&luatpt_getelement},
{"element_func",&luatpt_element_func},
{NULL,NULL}
};
@ -83,6 +89,42 @@ void luacon_open(){
lua_setfield(l, tptProperties, "selectedl");
lua_pushinteger(l, 0);
lua_setfield(l, tptProperties, "selectedr");
lua_newtable(l);
tptPropertiesVersion = lua_gettop(l);
lua_pushinteger(l, SAVE_VERSION);
lua_setfield(l, tptPropertiesVersion, "major");
lua_pushinteger(l, MINOR_VERSION);
lua_setfield(l, tptPropertiesVersion, "minor");
lua_pushinteger(l, BUILD_NUM);
lua_setfield(l, tptPropertiesVersion, "build");
lua_setfield(l, tptProperties, "version");
lua_newtable(l);
tptElements = lua_gettop(l);
lua_pushinteger(l, PT_NONE);
lua_setfield(l, tptElements, "none");
lua_pushinteger(l, PT_PLEX);
lua_setfield(l, tptElements, "c4");
lua_pushinteger(l, PT_C5);
lua_setfield(l, tptElements, "c5");
for(i = 1; i < PT_NUM; i++)
{
for(j = 0; j < strlen(ptypes[i].name); j++)
tmpname[j] = tolower(ptypes[i].name[j]);
tmpname[strlen(ptypes[i].name)] = 0;
lua_pushinteger(l, i);
lua_setfield(l, tptElements, tmpname);
}
lua_setfield(l, tptProperties, "el");
//lua_setglobal(l, "pel");
lua_el_func = calloc(PT_NUM, sizeof(int));
lua_el_mode = calloc(PT_NUM, sizeof(int));
for(i = 0; i < PT_NUM; i++)
{
lua_el_mode[i] = 0;
}
}
int luacon_keyevent(int key, int modifier, int event){
int i = 0, kpcontinue = 1;
@ -151,6 +193,24 @@ int luacon_step(int mx, int my, int selectl, int selectr){
int luacon_eval(char *command){
return luaL_dostring (l, command);
}
int luacon_part_update(int t, int i, int x, int y, int surround_space, int nt)
{
int retval = 0;
if(lua_el_func[t]){
lua_rawgeti(l, LUA_REGISTRYINDEX, lua_el_func[t]);
lua_pushinteger(l, i);
lua_pushinteger(l, x);
lua_pushinteger(l, y);
lua_pushinteger(l, surround_space);
lua_pushinteger(l, nt);
lua_pcall(l, 5, 1, 0);
if(lua_isboolean(l, -1)){
retval = lua_toboolean(l, -1);
}
lua_pop(l, 1);
}
return retval;
}
char *luacon_geterror(){
char *error = lua_tostring(l, -1);
if(error==NULL || !error[0]){
@ -201,6 +261,38 @@ int luatpt_test(lua_State* l)
printf("Test successful, got %d\n", testint);
return 0;
}
int luatpt_getelement(lua_State *l)
{
int t;
char * name = luaL_optstring(l, 1, "dust");
if (!console_parse_type(name, &t, NULL))
return luaL_error(l,"Unrecognised element '%s'", name);
lua_pushinteger(l, t);
return 1;
}
int luatpt_element_func(lua_State *l)
{
if(lua_isfunction(l, 1))
{
int element = luaL_optint(l, 2, 0);
int replace = luaL_optint(l, 3, 0);
int function = luaL_ref(l, LUA_REGISTRYINDEX);
if(element > 0 && element < PT_NUM)
{
lua_el_func[element] = function;
if(replace)
lua_el_mode[element] = 2;
else
lua_el_mode[element] = 1;
return 0;
}
else
{
return luaL_error(l, "Invalid element");
}
}
return 0;
}
int luatpt_error(lua_State* l)
{
char *error = "";

View File

@ -4,6 +4,9 @@
#include <powder.h>
#include <air.h>
#include <misc.h>
#ifdef LUACONSOLE
#include <luaconsole.h>
#endif
int gravwl_timeout = 0;
@ -2094,11 +2097,22 @@ void update_particles_i(pixel *vid, int start, int inc)
}
//call the particle update function, if there is one
#ifdef LUACONSOLE
if (ptypes[t].update_func && lua_el_mode[t] != 2)
#else
if (ptypes[t].update_func)
#endif
{
if ((*(ptypes[t].update_func))(i,x,y,surround_space,nt))
continue;
}
#ifdef LUACONSOLE
if(lua_el_mode[t])
{
if(luacon_part_update(t,i,x,y,surround_space,nt))
continue;
}
#endif
if (legacy_enable)//if heat sim is off
update_legacy_all(i,x,y,surround_space,nt);