From 891459b53b688351e32fadcb7ad82dac23f8b9ce Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Mon, 1 Dec 2025 22:26:52 +0100 Subject: [PATCH] Change all voice arrays to be exposed as arrays in Lua For the sake of consistency and future expansion! Also expose mobj->voice (read-only for now, hopefully), and fix missing NOSET for player->voice_id --- src/lua_baselib.c | 3 ++- src/lua_libs.h | 4 ++- src/lua_mobjlib.c | 11 ++++++++ src/lua_playerlib.c | 2 ++ src/lua_voicelib.c | 62 +++++++++++++++++++++++++++++++++++++++------ 5 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 2c1d8dc0f..565f40f4c 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -251,7 +251,8 @@ static const struct { {META_MATRIX, "matrix_t"}, {META_QUATERNION, "quaternion_t"}, {META_VOICE, "kartvoice_t"}, - {META_VOICE_ARRAY, "kartvoice_t.array"}, + {META_VOICE_ARRAY, "kartvoice_t.array[]"}, + {META_VOICE_ARRAY1, "kartvoice_t.array[1]"}, {NULL, NULL} }; diff --git a/src/lua_libs.h b/src/lua_libs.h index e1da50bc0..7367b50df 100644 --- a/src/lua_libs.h +++ b/src/lua_libs.h @@ -124,7 +124,9 @@ extern lua_State *gL; #define META_QUATERNION "QUATERNION_T" #define META_VOICE "KARTVOICE_T*" -#define META_VOICE_ARRAY "KARTVOICE_T*ARRAY" +#define META_VOICE_ARRAY "KARTVOICE_T*[]" +// TODO: kill this one +#define META_VOICE_ARRAY1 "KARTVOICE_T*[1]" boolean luaL_checkboolean(lua_State *L, int narg); diff --git a/src/lua_mobjlib.c b/src/lua_mobjlib.c index ec47bcfba..da2dba14b 100644 --- a/src/lua_mobjlib.c +++ b/src/lua_mobjlib.c @@ -64,6 +64,7 @@ enum mobj_e { mobj_eflags, mobj_renderflags, mobj_skin, + mobj_voice, mobj_color, mobj_bnext, mobj_bprev, @@ -161,6 +162,7 @@ static const char *const mobj_opt[] = { "eflags", "renderflags", "skin", + "voice", "color", "bnext", "bprev", @@ -427,6 +429,9 @@ static int mobj_get(lua_State *L) return 0; lua_pushstring(L, ((skin_t *)mo->skin)->name); break; + case mobj_voice: // just push the struct damnit! + LUA_PushUserdata(L, mo->voice, META_VOICE); + break; case mobj_color: lua_pushinteger(L, mo->color); break; @@ -901,6 +906,12 @@ static int mobj_set(lua_State *L) } return luaL_error(L, "mobj.skin '%s' not found!", skin); } + case mobj_voice: + // as much as i want to allow ironman without overwriting the player's skin, + // letting the mobj's voice desync with the skin has some nasty implications at the moment + return NOSET; + //mo->voice = *((kartvoice_t **)luaL_checkudata(L, 3, META_VOICE)); + //break; case mobj_color: { UINT16 newcolor = (UINT16)luaL_checkinteger(L,3); diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c index 9ecb5ce15..e08b6c6b8 100644 --- a/src/lua_playerlib.c +++ b/src/lua_playerlib.c @@ -1507,6 +1507,8 @@ static int player_set(lua_State *L) } case player_skin: return NOSET; + case player_voice_id: + return NOSET; case player_availabilities: return NOSET; case player_score: diff --git a/src/lua_voicelib.c b/src/lua_voicelib.c index 4bee3ac81..7e7e809bb 100644 --- a/src/lua_voicelib.c +++ b/src/lua_voicelib.c @@ -75,10 +75,10 @@ static int voice_get(lua_State* L) lua_pushstring(L, voice->name); break; case voicevars_win: - LUA_PushUserdata(L, &S_sfx[voice->win], META_SFXINFO); + LUA_PushUserdata(L, &voice->win, META_VOICE_ARRAY1); break; case voicevars_lose: - LUA_PushUserdata(L, &S_sfx[voice->lose], META_SFXINFO); + LUA_PushUserdata(L, &voice->lose, META_VOICE_ARRAY1); break; case voicevars_pain: LUA_PushUserdata(L, voice->pain, META_VOICE_ARRAY); @@ -90,13 +90,13 @@ static int voice_get(lua_State* L) LUA_PushUserdata(L, voice->boost, META_VOICE_ARRAY); break; case voicevars_overtake: - LUA_PushUserdata(L, &S_sfx[voice->overtake], META_SFXINFO); + LUA_PushUserdata(L, &voice->overtake, META_VOICE_ARRAY1); break; case voicevars_hitem: - LUA_PushUserdata(L, &S_sfx[voice->hitem], META_SFXINFO); + LUA_PushUserdata(L, &voice->hitem, META_VOICE_ARRAY1); break; case voicevars_power: - LUA_PushUserdata(L, &S_sfx[voice->power], META_SFXINFO); + LUA_PushUserdata(L, &voice->power, META_VOICE_ARRAY1); break; default: return RNOFIELD; @@ -181,9 +181,9 @@ static int voice_array_get(lua_State *L) INT32 i = luaL_checkinteger(L, 2); if (i < 0 || i >= VOICEARRAYSIZE) - return luaL_error(L, "index %d out of range (0 - %d)", i, VOICEARRAYSIZE); + return luaL_error(L, "index %d out of range (0 - %d)", i, VOICEARRAYSIZE-1); - LUA_PushUserdata(L, &S_sfx[voice_array[i]], META_SFXINFO); + lua_pushinteger(L, voice_array[i]); return 1; } @@ -193,7 +193,7 @@ static int voice_array_set(lua_State *L) INT32 i = luaL_checkinteger(L, 2); if (i < 0 || i >= VOICEARRAYSIZE) - return luaL_error(L, "index %d out of range (0 - %d)", i, VOICEARRAYSIZE); + return luaL_error(L, "index %d out of range (0 - %d)", i, VOICEARRAYSIZE-1); voice_array[i] = (sfxenum_t)luaL_checkinteger(L, 3); return 0; @@ -206,6 +206,41 @@ static int voice_array_num(lua_State *L) return 1; } +// i would've liked to shove the length of the voice array into the userdata... +// but this codebase is not ready for that, sooooo... have yet ANOTHER userdata type! + +// voice_array, i -> voice.array[i] +static int voice_array1_get(lua_State *L) +{ + sfxenum_t *voice_array = *((sfxenum_t **)luaL_checkudata(L, 1, META_VOICE_ARRAY1)); + INT32 i = luaL_checkinteger(L, 2); + + if (i < 0 || i >= 1) + return luaL_error(L, "index %d out of range (0 - %d)", i, 1-1); + + lua_pushinteger(L, voice_array[i]); + return 1; +} + +static int voice_array1_set(lua_State *L) +{ + sfxenum_t *voice_array = *((sfxenum_t **)luaL_checkudata(L, 1, META_VOICE_ARRAY1)); + INT32 i = luaL_checkinteger(L, 2); + + if (i < 0 || i >= 1) + return luaL_error(L, "index %d out of range (0 - %d)", i, 1-1); + + voice_array[i] = (sfxenum_t)luaL_checkinteger(L, 3); + return 0; +} + +// #voice.array -> 1 +static int voice_array1_num(lua_State *L) +{ + lua_pushinteger(L, 1); + return 1; +} + int LUA_VoiceLib(lua_State* L) { luaL_newmetatable(L, META_VOICE); @@ -230,5 +265,16 @@ int LUA_VoiceLib(lua_State* L) lua_setfield(L, -2, "__len"); lua_pop(L,1); + luaL_newmetatable(L, META_VOICE_ARRAY1); + lua_pushcfunction(L, voice_array1_get); + lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, voice_array1_set); + lua_setfield(L, -2, "__newindex"); + + lua_pushcfunction(L, voice_array1_num); + lua_setfield(L, -2, "__len"); + lua_pop(L,1); + return 0; }