diff --git a/src/k_roulette.c b/src/k_roulette.c index 1421ae1cf..10c475610 100644 --- a/src/k_roulette.c +++ b/src/k_roulette.c @@ -1256,6 +1256,20 @@ void K_FillItemRouletteData(const player_t *player, itemroulette_t *const roulet K_CalculateRouletteSpeed(roulette); } + + // The roulette should be init'd first, and the speed should be + // set if applicable before you can override anything. + { + if (LUA_HookPreFillItemRoulette(player, roulette, ringbox)) + { + // If somehow there's no items, add sad. + if (roulette->itemList.len == 0) { + K_AddItemToReel(player, roulette, KITEM_SAD); + } + return; + } + + } if (ringbox == true) { diff --git a/src/lua_hook.h b/src/lua_hook.h index 12c85be94..1f392d868 100644 --- a/src/lua_hook.h +++ b/src/lua_hook.h @@ -79,6 +79,7 @@ automatically. X (GameQuit),\ X (PlayerCmd),/* building the player's ticcmd struct */\ X (VoteThinker),/* Y_VoteTicker */\ + X (PreFillItemRoulette),/* K_FillItemRouletteData, before special conditions but after roulette speed calc */\ #define STRING_HOOK_LIST(X) \ X (SpecialExecute),\ @@ -146,6 +147,7 @@ void LUA_HookPlayerQuit(player_t *, kickreason_t); //int LUA_HookTeamSwitch(player_t *, int newteam, boolean fromspectators, boolean tryingautobalance, boolean tryingscramble); int LUA_HookViewpointSwitch(player_t *player, player_t *newdisplayplayer, boolean forced); int LUA_HookSeenPlayer(player_t *player, player_t *seenfriend); +int LUA_HookPreFillItemRoulette(player_t *player, itemroulette_t *const roulette, boolean ringbox); #ifdef __cplusplus } // extern "C" diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 676e40b77..0d1771f74 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -24,6 +24,7 @@ #include "lua_hook.h" #include "lua_hud.h" // hud_running errors #include "lua_profile.h" +#include "lua_playerlib.h" // constplayer #include "command.h" #include "m_perfstats.h" @@ -1009,4 +1010,33 @@ int LUA_HookSeenPlayer(player_t *player, player_t *seenfriend) return hook.status; } +static int roulette_hook( + player_t *player, + itemroulette_t *const roulette, + boolean ringbox, + int hook_type, + Hook_Callback results_handler) +{ + Hook_State hook; + if (prepare_hook(&hook, false, hook_type)) + { + if (player == NULL) { + lua_pushnil(gL); + } else { + LUA_PushUserdata(gL, player, META_PLAYER); + } + LUA_PushUserdata(gL, roulette, META_ITEMROULETTE); + lua_pushboolean(gL, ringbox); + constplayer = true; // Do not allow players to be modified. + call_hooks(&hook, 1, results_handler); + constplayer = false; // You're good. + } + return hook.status; +} + +int LUA_HookPreFillItemRoulette(player_t *player, itemroulette_t *const roulette, boolean ringbox) +{ + return roulette_hook(player, roulette, ringbox, HOOK(PreFillItemRoulette), res_true); +} + boolean hook_cmd_running = false; diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c index 9ca6c5853..8ba0b1393 100644 --- a/src/lua_playerlib.c +++ b/src/lua_playerlib.c @@ -25,6 +25,8 @@ #include "lua_hook.h" // hook_cmd_running errors #include "k_profiles.h" // GetPrettyRRID +boolean constplayer = false; + static int lib_iteratePlayers(lua_State *L) { INT32 i = -1; @@ -795,6 +797,8 @@ static int player_set(lua_State *L) 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 CMD building code!"); + if (constplayer) + return luaL_error(L, "Do not alter player_t while modifying the roulette!"); if (fastcmp(field,"mo")) { mobj_t *newmo = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); diff --git a/src/lua_playerlib.h b/src/lua_playerlib.h new file mode 100644 index 000000000..d2d09f5a5 --- /dev/null +++ b/src/lua_playerlib.h @@ -0,0 +1,27 @@ +// DR. ROBOTNIK'S RING RACERS +//----------------------------------------------------------------------------- +// Copyright (C) 2024 by Kart Krew. +// Copyright (C) 2022 by Sonic Team Junior. +// Copyright (C) 2016 by John "JTE" Muniz. +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file lua_playerlib.h +/// \brief LUA Player library header. + +#ifndef __LUA_PLAYER_H__ +#define __LUA_PLAYER_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +extern boolean constplayer; + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // __LUA_PLAYER_H__