diff --git a/LUASTUFF.md b/LUASTUFF.md index 83ec76545..28e77bed8 100644 --- a/LUASTUFF.md +++ b/LUASTUFF.md @@ -192,6 +192,18 @@ from `0` to `#musicdefs-1`, or string indices (which is equal to calling `S_Find Hook is called whenever `S_ShowMusicCredit` (either from game or mod) is called. Takes musicdef as only argument, returning true will overwrite vanilla behavior (not show music credit), can be used to implement custom music credit pop-ups. +## addHook("SetupVote", function(result, gt, secondgt, prevmap)) + +Hook is called whenever server sets up options for vote screen. `result` is table where lua can store options to, +(for example, `result[1] = 1` will force green hills as first option), gt is current gametype, secondgt is +alternative gametype picked for 3rd entry (for example, gt can be `GT_RACE`, and secondgt can be `GT_MATCH`) +prevmap is the previous map that was played (small note - in game code, it is offset by -1, but for lua its offset back, +so if previous gamemap was 1, prevmap would also be 1. Which means you can safely do mapheaderinfo[prevmap] to get info on previous map) + +Limitations: currently, game can only change gametype for 3rd option in map (meaning for example, if current gametype is race, and you +put battle map in first option, when it gets picked that map will be still run in race gametype). That means, game will only handle maps with +different gametype if it is put in 3rd vote option. + # Other changes ## P_PlayRinglossSound(source, damager) diff --git a/src/d_netcmd.c b/src/d_netcmd.c index fe056369a..7a0ab193d 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -2509,6 +2509,14 @@ void D_SetupVote(void) UINT8 gt = (cv_kartgametypepreference.value == -1) ? gametype : cv_kartgametypepreference.value; UINT8 secondgt = G_SometimesGetDifferentGametype(gt); INT16 votebuffer[4] = {-1,-1,-1,0}; + INT16 luamaps[4] = {0, 0, 0, 0}; + + LUA_HookSetupVote(luamaps, sizeof(luamaps)/sizeof(luamaps[0]), gt, secondgt); + + // Correct secondgt if needed + UINT8 typeoflevel = mapheaderinfo[luamaps[2]-1]->typeoflevel; + if (luamaps[2] && (typeoflevel & G_TOLFlag(secondgt&(~0x80))) == 0) + secondgt = ((typeoflevel & TOL_RACE) ? GT_RACE : GT_MATCH)|(secondgt&0x80); if (cv_kartencore.value && gt == GT_RACE) WRITEUINT8(p, (gt|0x80)); @@ -2517,10 +2525,13 @@ void D_SetupVote(void) WRITEUINT8(p, secondgt); secondgt &= ~0x80; + for (i = 0; i < 4; i++) { UINT16 m; - if (i == 2) // sometimes a different gametype + if (luamaps[i]) + m = luamaps[i]-1; + else if (i == 2) // sometimes a different gametype m = G_RandMap(G_TOLFlag(secondgt), prevmap, false, 0, true, votebuffer); else if (i >= 3) // unknown-random and force-unknown MAP HELL m = G_RandMap(G_TOLFlag(gt), prevmap, false, (i-2), (i < 4), votebuffer); diff --git a/src/lua_hook.h b/src/lua_hook.h index c31a58a67..64c91cc24 100644 --- a/src/lua_hook.h +++ b/src/lua_hook.h @@ -66,6 +66,7 @@ X (IntermissionThinker),/* Y_Ticker */\ X (VoteThinker),/*SRB2KART*/\ X (ServerJoin),/* SRB2KART - Saturn 32p*/\ + X (SetupVote),/*SRB2KART - Saturn*/ #define STRING_HOOK_LIST(X) \ X (BotAI),/* B_BuildTailsTiccmd by skin name */\ @@ -140,3 +141,8 @@ int LUA_HookShouldSquish(player_t *player, mobj_t *inflictor, mobj_t *source); / boolean LUA_HookPlayerSpin(player_t *player, mobj_t *inflictor, mobj_t *source); // SRB2KART: Hook for K_SpinPlayer. Allows Lua to execute code and/or overwrite its behavior. boolean LUA_HookPlayerExplode(player_t *player, mobj_t *inflictor, mobj_t *source); // SRB2KART: Hook for K_ExplodePlayer. Allows Lua to execute code and/or overwrite its behavior. boolean LUA_HookPlayerSquish(player_t *player, mobj_t *inflictor, mobj_t *source); // SRB2KART: Hook for K_SquishPlayer. Allows Lua to execute code and/or overwrite its behavior. + +// SRB2KART - Saturn: Allows changing vote level picks +// (note: function format in lua doesn't include maxresults, so its function(result, gametype, secondgametype, prevmap) +// to get maxresults use #result) +void LUA_HookSetupVote(INT16 result[], INT16 maxresults, UINT8 gt, UINT8 secondgt); diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 45ba164c3..28055ffeb 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -1138,3 +1138,43 @@ boolean LUA_HookPlayerExplode(player_t *player, mobj_t *inflictor, mobj_t *sourc { return kartdamage_hook(player, inflictor, source, HOOK(PlayerExplode), res_true); } + +void LUA_HookSetupVote(INT16 result[], INT16 maxresults, UINT8 gt, UINT8 secondgt) +{ + Hook_State hook; + if (prepare_hook(&hook, 0, HOOK(SetupVote))) + { + // { 0, 0, 0, 0, } + lua_newtable(gL); + for (int i = 1; i <= maxresults; ++i) + { + lua_pushinteger(gL, 0); + lua_rawseti(gL, -2, i); + } + + // call_hooks does lua_settop(gL, 0) so we need to store our table somewhere + lua_pushvalue(gL, -1); + lua_setfield(gL, LUA_REGISTRYINDEX, "SetupVoteResults"); + + lua_pushinteger(gL, gt); + lua_pushinteger(gL, secondgt); + lua_pushinteger(gL, prevmap+1); // prevmap is gamemap-1 so we offset it back + + call_hooks(&hook, 0, res_none); + + lua_getfield(gL, LUA_REGISTRYINDEX, "SetupVoteResults"); + for (int i = 1; i <= maxresults; ++i) + { + lua_rawgeti(gL, -1, i); + result[i-1] = lua_tointeger(gL, -1); + lua_pop(gL, 1); + + if (result[i-1] && (result[i-1] < 0 || !mapheaderinfo[result[i-1]-1])) + { + CONS_Alert(CONS_WARNING, "Unknown map #%d for vote\n", result[i-1]); + result[i-1] = 0; + } + } + lua_pop(gL, 1); + } +}