From b564a169d80a0ff6db33de6e03f505803d8f05cf Mon Sep 17 00:00:00 2001 From: Monster Iestyn Date: Wed, 17 Jun 2015 21:00:10 +0100 Subject: [PATCH 1/3] Starting work for getting sector.lines in Lua: it WORKS at the least, but I have no way to determine the size of the array itself as of yet --- src/lua_libs.h | 1 + src/lua_maplib.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/lua_libs.h b/src/lua_libs.h index d19ad8857..25552eacb 100644 --- a/src/lua_libs.h +++ b/src/lua_libs.h @@ -42,6 +42,7 @@ extern lua_State *gL; #define META_CVAR "CONSVAR_T*" +#define META_SECTORLINES "SECTOR_T*LINES" #define META_SIDENUM "LINE_T*SIDENUM" #define META_HUDINFO "HUDINFO_T*" diff --git a/src/lua_maplib.c b/src/lua_maplib.c index e5cc30c12..40acc6dff 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -37,6 +37,7 @@ enum sector_e { sector_thinglist, sector_heightsec, sector_camsec, + sector_lines, sector_ffloors }; @@ -52,6 +53,7 @@ static const char *const sector_opt[] = { "thinglist", "heightsec", "camsec", + "lines", "ffloors", NULL}; @@ -260,6 +262,49 @@ static int sector_iterate(lua_State *L) return 3; } +// sector.lines, i -> sector.lines[i] +// sector.lines.valid, for validity checking +static int sectorlines_get(lua_State *L) +{ + line_t **seclines = *((line_t ***)luaL_checkudata(L, 1, META_SECTORLINES)); + size_t i; + //size_t numoflines; + lua_settop(L, 2); + if (!lua_isnumber(L, 2)) + { + int field = luaL_checkoption(L, 2, NULL, valid_opt); + if (!seclines) + { + if (field == 0) { + lua_pushboolean(L, 0); + return 1; + } + return luaL_error(L, "accessed sector_t doesn't exist anymore."); + } else if (field == 0) { + lua_pushboolean(L, 1); + return 1; + } + } + + /* \TODO: figure out how to find size of seclines array, rather than the size of a pointer! + Testing for sectors[0].lines in GFZ1 with a test Lua script: + sizeof(seclines) returns 4 + sizeof(*seclines) returns 4 + sizeof(**seclines) returns 84, presumably the size of line_t + You can probably see why I haven't been successful yet, hopefully + //CONS_Printf("sizeof(seclines): %d\n", sizeof(seclines)); + //CONS_Printf("sizeof(seclines[0]): %d\n", sizeof(seclines[0]));*/ + + /*numoflines = sizeof(seclines) / sizeof(seclines[0]); + if (!numoflines) + return luaL_error(L, "no lines found!");*/ + i = (size_t)lua_tointeger(L, 2); + /*if (i > numoflines) + return 0;*/ + LUA_PushUserdata(L, seclines[i], META_LINE); + return 1; +} + static int sector_get(lua_State *L) { sector_t *sector = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR)); @@ -325,6 +370,9 @@ static int sector_get(lua_State *L) return 0; LUA_PushUserdata(L, §ors[sector->camsec], META_SECTOR); return 1; + case sector_lines: // lines + LUA_PushUserdata(L, sector->lines, META_SECTORLINES); + return 1; case sector_ffloors: // ffloors lua_pushcfunction(L, lib_iterateSectorFFloors); LUA_PushUserdata(L, sector->ffloors, META_FFLOOR); @@ -1178,6 +1226,11 @@ static int mapheaderinfo_get(lua_State *L) int LUA_MapLib(lua_State *L) { + luaL_newmetatable(L, META_SECTORLINES); + lua_pushcfunction(L, sectorlines_get); + lua_setfield(L, -2, "__index"); + lua_pop(L, 1); + luaL_newmetatable(L, META_SECTOR); lua_pushcfunction(L, sector_get); lua_setfield(L, -2, "__index"); From 79e3e2351d17f4f223d75295635122257219242d Mon Sep 17 00:00:00 2001 From: Monster Iestyn Date: Wed, 20 Jan 2016 14:56:52 +0000 Subject: [PATCH 2/3] Finally bothered to add in a method to obtain sector.lines' size internally to prevent going out of bounds. Admittedly I knew of this particular method from the start but wanted to avoid it in favour of a less-hacky looking method of getting sector.lines' size ...but there was none to be found at all. --- src/lua_maplib.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 1307540fb..77651b209 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -268,7 +268,7 @@ static int sectorlines_get(lua_State *L) { line_t **seclines = *((line_t ***)luaL_checkudata(L, 1, META_SECTORLINES)); size_t i; - //size_t numoflines; + size_t numoflines = 0; lua_settop(L, 2); if (!lua_isnumber(L, 2)) { @@ -286,21 +286,22 @@ static int sectorlines_get(lua_State *L) } } - /* \TODO: figure out how to find size of seclines array, rather than the size of a pointer! - Testing for sectors[0].lines in GFZ1 with a test Lua script: - sizeof(seclines) returns 4 - sizeof(*seclines) returns 4 - sizeof(**seclines) returns 84, presumably the size of line_t - You can probably see why I haven't been successful yet, hopefully - //CONS_Printf("sizeof(seclines): %d\n", sizeof(seclines)); - //CONS_Printf("sizeof(seclines[0]): %d\n", sizeof(seclines[0]));*/ + // check first linedef to figure which of its sectors owns this sector->lines pointer + // then check that sector's linecount to get a maximum index + //if (!seclines[0]) + //return luaL_error(L, "no lines found!"); // no first linedef????? + if (seclines[0]->frontsector->lines == seclines) + numoflines = seclines[0]->frontsector->linecount; + else if (seclines[0]->backsector && seclines[0]->backsector->lines == seclines) // check backsector exists first + numoflines = seclines[0]->backsector->linecount; + //if neither sector has it then ??? - /*numoflines = sizeof(seclines) / sizeof(seclines[0]); if (!numoflines) - return luaL_error(L, "no lines found!");*/ + return luaL_error(L, "no lines found!"); + i = (size_t)lua_tointeger(L, 2); - /*if (i > numoflines) - return 0;*/ + if (i >= numoflines) + return 0; LUA_PushUserdata(L, seclines[i], META_LINE); return 1; } From 5abdb08a25da14e1378adb937a4418e92dfcc609 Mon Sep 17 00:00:00 2001 From: Monster Iestyn Date: Wed, 20 Jan 2016 16:03:17 +0000 Subject: [PATCH 3/3] #sector.lines now returns the number of linedefs in the sector --- src/lua_maplib.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 77651b209..d585c479f 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -306,6 +306,23 @@ static int sectorlines_get(lua_State *L) return 1; } +static int sectorlines_num(lua_State *L) +{ + line_t **seclines = *((line_t ***)luaL_checkudata(L, 1, META_SECTORLINES)); + size_t numoflines = 0; + // check first linedef to figure which of its sectors owns this sector->lines pointer + // then check that sector's linecount to get a maximum index + //if (!seclines[0]) + //return luaL_error(L, "no lines found!"); // no first linedef????? + if (seclines[0]->frontsector->lines == seclines) + numoflines = seclines[0]->frontsector->linecount; + else if (seclines[0]->backsector && seclines[0]->backsector->lines == seclines) // check backsector exists first + numoflines = seclines[0]->backsector->linecount; + //if neither sector has it then ??? + lua_pushinteger(L, numoflines); + return 1; +} + static int sector_get(lua_State *L) { sector_t *sector = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR)); @@ -1282,6 +1299,9 @@ int LUA_MapLib(lua_State *L) luaL_newmetatable(L, META_SECTORLINES); lua_pushcfunction(L, sectorlines_get); lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, sectorlines_num); + lua_setfield(L, -2, "__len"); lua_pop(L, 1); luaL_newmetatable(L, META_SECTOR);