From a4149cfe37749db4dc150982510f7d011b336c24 Mon Sep 17 00:00:00 2001 From: James R Date: Wed, 15 Jan 2020 18:49:22 -0800 Subject: [PATCH 1/3] Expose G_FindMap to Lua --- src/lua_baselib.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 2a82ec512..dfc5398cd 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -2781,6 +2781,69 @@ static int lib_gBuildMapName(lua_State *L) return 1; } +static void +Lpushdim (lua_State *L, int c, struct searchdim *v) +{ + int i; + lua_createtable(L, c, 0);/* I guess narr is numeric indices??? */ + for (i = 0; i < c; ++i) + { + lua_createtable(L, 0, 2);/* and hashed indices (field)... */ + lua_pushnumber(L, v[i].pos); + lua_setfield(L, -2, "pos"); + + lua_pushnumber(L, v[i].siz); + lua_setfield(L, -2, "siz"); + lua_rawseti(L, -2, 1 + i); + } +} + +/* +I decided to make this return a table because userdata +is scary and tables let the user set their own fields. +*/ +static int lib_gFindMap(lua_State *L) +{ + const char *query = luaL_checkstring(L, 1); + + INT32 map; + char *realname; + INT32 frc; + mapsearchfreq_t *frv; + + INT32 i; + + map = G_FindMap(query, &realname, &frv, &frc); + + lua_settop(L, 0); + + lua_pushnumber(L, map); + lua_pushstring(L, realname); + + lua_createtable(L, frc, 0); + for (i = 0; i < frc; ++i) + { + lua_createtable(L, 0, 4); + lua_pushnumber(L, frv[i].mapnum); + lua_setfield(L, -2, "mapnum"); + + Lpushdim(L, frv[i].matchc, frv[i].matchd); + lua_setfield(L, -2, "matchd"); + + Lpushdim(L, frv[i].keywhc, frv[i].keywhd); + lua_setfield(L, -2, "keywhd"); + + lua_pushnumber(L, frv[i].total); + lua_setfield(L, -2, "total"); + lua_rawseti(L, -2, 1 + i); + } + + G_FreeMapSearch(frv, frc); + Z_Free(realname); + + return 3; +} + static int lib_gDoReborn(lua_State *L) { INT32 playernum = luaL_checkinteger(L, 1); @@ -3157,6 +3220,7 @@ static luaL_Reg lib[] = { // g_game {"G_AddGametype", lib_gAddGametype}, {"G_BuildMapName",lib_gBuildMapName}, + {"G_FindMap",lib_gFindMap}, {"G_DoReborn",lib_gDoReborn}, {"G_SetCustomExitVars",lib_gSetCustomExitVars}, {"G_EnoughPlayersFinished",lib_gEnoughPlayersFinished}, From bcd90b96d4f5883dd9bff923d53b631190d9bd74 Mon Sep 17 00:00:00 2001 From: James R Date: Wed, 15 Jan 2020 19:01:28 -0800 Subject: [PATCH 2/3] Add comment to lib_gFindMap so I know what the fuck I did --- src/lua_baselib.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index dfc5398cd..6964d5cc9 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -2802,6 +2802,31 @@ Lpushdim (lua_State *L, int c, struct searchdim *v) I decided to make this return a table because userdata is scary and tables let the user set their own fields. */ +/* +Returns: + +[1] => map number +[2] => map title +[3] => search frequency table + +The frequency table is unsorted. It has the following format: + +{ + ['mapnum'], + + ['matchd'] => matches in map title string + ['keywhd'] => matches in map keywords + + The above two tables have the following format: + + { + ['pos'] => offset from start of string + ['siz'] => length of match + }... + + ['total'] => the total matches +}... +*/ static int lib_gFindMap(lua_State *L) { const char *query = luaL_checkstring(L, 1); From 1e3fd7960148447e3233ef9c3f593199181962f8 Mon Sep 17 00:00:00 2001 From: James R Date: Wed, 15 Jan 2020 20:32:40 -0800 Subject: [PATCH 3/3] Expose G_FindMapByNameOrCode to Lua --- src/lua_baselib.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 6964d5cc9..c926db647 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -2869,6 +2869,29 @@ static int lib_gFindMap(lua_State *L) return 3; } +/* +Returns: + +[1] => map number +[2] => map title +*/ +static int lib_gFindMapByNameOrCode(lua_State *L) +{ + const char *query = luaL_checkstring(L, 1); + INT32 map; + char *realname; + map = G_FindMapByNameOrCode(query, &realname); + lua_pushnumber(L, map); + if (map) + { + lua_pushstring(L, realname); + Z_Free(realname); + return 2; + } + else + return 1; +} + static int lib_gDoReborn(lua_State *L) { INT32 playernum = luaL_checkinteger(L, 1); @@ -3246,6 +3269,7 @@ static luaL_Reg lib[] = { {"G_AddGametype", lib_gAddGametype}, {"G_BuildMapName",lib_gBuildMapName}, {"G_FindMap",lib_gFindMap}, + {"G_FindMapByNameOrCode",lib_gFindMapByNameOrCode}, {"G_DoReborn",lib_gDoReborn}, {"G_SetCustomExitVars",lib_gSetCustomExitVars}, {"G_EnoughPlayersFinished",lib_gEnoughPlayersFinished},