diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2011-11-18 19:38:08 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2011-11-18 19:38:08 (GMT) |
| commit | f50801333214e79492dd72c23f05cd4bc84fcf54 (patch) | |
| tree | 3b6f934b80c45f1c696c0f8b1ae5f67c743b25b4 /src/luaconsole.c | |
| parent | 8a962b2f9605672627c077d2878c03fab53495a4 (diff) | |
| download | powder-f50801333214e79492dd72c23f05cd4bc84fcf54.zip powder-f50801333214e79492dd72c23f05cd4bc84fcf54.tar.gz | |
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
Diffstat (limited to 'src/luaconsole.c')
| -rw-r--r-- | src/luaconsole.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/luaconsole.c b/src/luaconsole.c index b0b8a53..d0f55d8 100644 --- a/src/luaconsole.c +++ b/src/luaconsole.c @@ -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 = ""; |
