From aee76fe88d62263b0ab0af90fd62ecf0932478fa Mon Sep 17 00:00:00 2001 From: Latapostrophe Date: Thu, 21 Jan 2021 22:18:00 +0100 Subject: [PATCH] expose karthud to lua. bruh. --- src/dehacked.c | 50 ++++++++++++++++++++++++++++++++++++++++++++- src/lua_libs.h | 1 + src/lua_playerlib.c | 49 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/src/dehacked.c b/src/dehacked.c index 4e2db6975..9acab7b8a 100644 --- a/src/dehacked.c +++ b/src/dehacked.c @@ -11002,6 +11002,32 @@ static const char *const KARTSTUFF_LIST[] = { "WRONGWAY" }; +static const char *const KARTHUD_LIST[] = { + "ITEMBLINK", + "ITEMBLINKMODE", + + "RINGFRAME", + "RINGTICS", + "RINGDELAY", + "RINGSPBBLOCK", + + "LAPANIMATION", + "LAPHAND", + + "FAULT", + + "BOOSTCAM", + "DESTBOOSTCAM", + "TIMEOVERCAM", + + "ENGINESND", + "VOICES", + "TAUNTVOICES", + + "CARDANIMATION", + "YOUGOTEM", +}; + static const char *const HUDITEMS_LIST[] = { "LIVES", @@ -12244,6 +12270,7 @@ void DEH_Check(void) const size_t dehmobjs = sizeof(MOBJTYPE_LIST)/sizeof(const char*); const size_t dehpowers = sizeof(POWERS_LIST)/sizeof(const char*); const size_t dehkartstuff = sizeof(KARTSTUFF_LIST)/sizeof(const char*); + const size_t dehkarthud = sizeof(KARTHUD_LIST)/sizeof(const char*); const size_t dehcolors = sizeof(COLOR_ENUMS)/sizeof(const char*); if (dehstates != S_FIRSTFREESLOT) @@ -12256,7 +12283,10 @@ void DEH_Check(void) I_Error("You forgot to update the Dehacked powers list, you dolt!\n(%d powers defined, versus %s in the Dehacked list)\n", NUMPOWERS, sizeu1(dehpowers)); if (dehkartstuff != NUMKARTSTUFF) - I_Error("You forgot to update the Dehacked powers list, you dolt!\n(%d kartstuff defined, versus %s in the Dehacked list)\n", NUMKARTSTUFF, sizeu1(dehkartstuff)); + I_Error("You forgot to update the Dehacked kartstuff list, you dolt!\n(%d kartstuff defined, versus %s in the Dehacked list)\n", NUMKARTSTUFF, sizeu1(dehkartstuff)); + + if (dehkarthud != NUMKARTHUD) + I_Error("You forgot to update the Dehacked karthud list, you dolt!\n(%d karthud defined, versus %s in the Dehacked list)\n", NUMKARTSTUFF, sizeu1(dehkartstuff)); if (dehcolors != SKINCOLOR_FIRSTFREESLOT) I_Error("You forgot to update the Dehacked colors list, you dolt!\n(%d colors defined, versus %s in the Dehacked list)\n", SKINCOLOR_FIRSTFREESLOT, sizeu1(dehcolors)); @@ -12718,6 +12748,15 @@ static inline int lib_getenum(lua_State *L) } return 0; } + else if (!mathlib && fastncmp("khud_",word,5)) { + p = word+5; + for (i = 0; i < NUMKARTHUD; i++) + if (fasticmp(p, KARTHUD_LIST[i])) { + lua_pushinteger(L, i); + return 1; + } + return 0; + } else if (mathlib && fastncmp("K_",word,2)) { // SOCs are ALL CAPS! p = word+2; for (i = 0; i < NUMKARTSTUFF; i++) @@ -12727,6 +12766,15 @@ static inline int lib_getenum(lua_State *L) } return luaL_error(L, "kartstuff '%s' could not be found.\n", word); } + else if (mathlib && fastncmp("KHUD_",word,5)) { // SOCs are ALL CAPS! + p = word+5; + for (i = 0; i < NUMKARTHUD; i++) + if (fastcmp(p, KARTHUD_LIST[i])) { + lua_pushinteger(L, i); + return 1; + } + return luaL_error(L, "karthud '%s' could not be found.\n", word); + } else if (fastncmp("HUD_",word,4)) { p = word+4; for (i = 0; i < NUMHUDITEMS; i++) diff --git a/src/lua_libs.h b/src/lua_libs.h index 756459a55..bd2ab8ba6 100644 --- a/src/lua_libs.h +++ b/src/lua_libs.h @@ -34,6 +34,7 @@ extern lua_State *gL; #define META_SKIN "SKIN_T*" #define META_POWERS "PLAYER_T*POWERS" #define META_KARTSTUFF "PLAYER_T*KARTSTUFF" +#define META_KARTHUD "PLAYER_T*KARTHUD" #define META_COLLIDE "PLAYER_T*COLLIDE" #define META_SOUNDSID "SKIN_T*SOUNDSID" diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c index 81adeebc5..caae3bd3c 100644 --- a/src/lua_playerlib.c +++ b/src/lua_playerlib.c @@ -212,6 +212,8 @@ static int player_get(lua_State *L) LUA_PushUserdata(L, plr->powers, META_POWERS); else if (fastcmp(field,"kartstuff")) LUA_PushUserdata(L, plr->kartstuff, META_KARTSTUFF); + else if (fastcmp(field,"karthud")) + LUA_PushUserdata(L, plr->karthud, META_KARTHUD); else if (fastcmp(field,"airtime")) lua_pushinteger(L, plr->airtime); else if (fastcmp(field,"tumbleBounces")) @@ -848,13 +850,47 @@ static int kartstuff_set(lua_State *L) return 0; } -// #kartstuff -> NUMKARTSTUFF +// #karthud -> NUMKARTSTUFF static int kartstuff_len(lua_State *L) { lua_pushinteger(L, NUMKARTSTUFF); return 1; } +// karthud, ks -> karthud[ks] +static int karthud_get(lua_State *L) +{ + INT32 *karthud = *((INT32 **)luaL_checkudata(L, 1, META_KARTHUD)); + karthudtype_t ks = luaL_checkinteger(L, 2); + if (ks >= NUMKARTHUD) + return luaL_error(L, LUA_QL("karthudtype_t") " cannot be %u", ks); + lua_pushinteger(L, karthud[ks]); + return 1; +} + +// karthud, ks, value -> kartstuff[ks] = value +static int karthud_set(lua_State *L) +{ + INT32 *karthud = *((INT32 **)luaL_checkudata(L, 1, META_KARTHUD)); + karthudtype_t ks = luaL_checkinteger(L, 2); + INT32 i = (INT32)luaL_checkinteger(L, 3); + if (ks >= NUMKARTHUD) + return luaL_error(L, LUA_QL("karthudtype_t") " cannot be %u", ks); + if (hud_running) + return luaL_error(L, "Do not alter player_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter player_t in BuildCMD code!"); + karthud[ks] = i; + return 0; +} + +// #karthud -> NUMKARTHUD +static int karthud_len(lua_State *L) +{ + lua_pushinteger(L, NUMKARTHUD); + return 1; +} + #define NOFIELD luaL_error(L, LUA_QL("ticcmd_t") " has no field named " LUA_QS, field) #define NOSET luaL_error(L, LUA_QL("ticcmd_t") " field " LUA_QS " cannot be set.", field) @@ -948,6 +984,17 @@ int LUA_PlayerLib(lua_State *L) lua_setfield(L, -2, "__len"); lua_pop(L,1); + luaL_newmetatable(L, META_KARTHUD); + lua_pushcfunction(L, karthud_get); + lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, karthud_set); + lua_setfield(L, -2, "__newindex"); + + lua_pushcfunction(L, karthud_len); + lua_setfield(L, -2, "__len"); + lua_pop(L,1); + luaL_newmetatable(L, META_TICCMD); lua_pushcfunction(L, ticcmd_get); lua_setfield(L, -2, "__index");