Expose PreFillItemRoulette hook for Lua

This commit is contained in:
JugadorXEI 2024-10-19 20:19:29 +02:00 committed by Antonio Martinez
parent e5a20fdc3d
commit 2f7abb7dd3
5 changed files with 77 additions and 0 deletions

View file

@ -1257,6 +1257,20 @@ void K_FillItemRouletteData(const player_t *player, itemroulette_t *const roulet
K_CalculateRouletteSpeed(roulette); 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) if (ringbox == true)
{ {
// If this is being invoked by a Ring Box, it should literally never produce items. // If this is being invoked by a Ring Box, it should literally never produce items.

View file

@ -79,6 +79,7 @@ automatically.
X (GameQuit),\ X (GameQuit),\
X (PlayerCmd),/* building the player's ticcmd struct */\ X (PlayerCmd),/* building the player's ticcmd struct */\
X (VoteThinker),/* Y_VoteTicker */\ X (VoteThinker),/* Y_VoteTicker */\
X (PreFillItemRoulette),/* K_FillItemRouletteData, before special conditions but after roulette speed calc */\
#define STRING_HOOK_LIST(X) \ #define STRING_HOOK_LIST(X) \
X (SpecialExecute),\ 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_HookTeamSwitch(player_t *, int newteam, boolean fromspectators, boolean tryingautobalance, boolean tryingscramble);
int LUA_HookViewpointSwitch(player_t *player, player_t *newdisplayplayer, boolean forced); int LUA_HookViewpointSwitch(player_t *player, player_t *newdisplayplayer, boolean forced);
int LUA_HookSeenPlayer(player_t *player, player_t *seenfriend); int LUA_HookSeenPlayer(player_t *player, player_t *seenfriend);
int LUA_HookPreFillItemRoulette(player_t *player, itemroulette_t *const roulette, boolean ringbox);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"

View file

@ -24,6 +24,7 @@
#include "lua_hook.h" #include "lua_hook.h"
#include "lua_hud.h" // hud_running errors #include "lua_hud.h" // hud_running errors
#include "lua_profile.h" #include "lua_profile.h"
#include "lua_playerlib.h" // constplayer
#include "command.h" #include "command.h"
#include "m_perfstats.h" #include "m_perfstats.h"
@ -1009,4 +1010,33 @@ int LUA_HookSeenPlayer(player_t *player, player_t *seenfriend)
return hook.status; 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; boolean hook_cmd_running = false;

View file

@ -25,6 +25,8 @@
#include "lua_hook.h" // hook_cmd_running errors #include "lua_hook.h" // hook_cmd_running errors
#include "k_profiles.h" // GetPrettyRRID #include "k_profiles.h" // GetPrettyRRID
boolean constplayer = false;
static int lib_iteratePlayers(lua_State *L) static int lib_iteratePlayers(lua_State *L)
{ {
INT32 i = -1; 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!"); return luaL_error(L, "Do not alter player_t in HUD rendering code!");
if (hook_cmd_running) if (hook_cmd_running)
return luaL_error(L, "Do not alter player_t in CMD building code!"); 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")) { if (fastcmp(field,"mo")) {
mobj_t *newmo = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); mobj_t *newmo = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ));

27
src/lua_playerlib.h Normal file
View file

@ -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__