2014-03-15 16:59:03 +00:00
|
|
|
// SONIC ROBO BLAST 2
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-05-18 00:42:11 +00:00
|
|
|
// Copyright (C) 2012-2016 by John "JTE" Muniz.
|
2022-03-03 19:24:46 +00:00
|
|
|
// Copyright (C) 2012-2022 by Sonic Team Junior.
|
2014-03-15 16:59:03 +00:00
|
|
|
//
|
|
|
|
// 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_hooklib.c
|
|
|
|
/// \brief hooks for Lua scripting
|
|
|
|
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "doomstat.h"
|
|
|
|
#include "p_mobj.h"
|
2014-06-18 20:28:09 +00:00
|
|
|
#include "g_game.h"
|
2020-03-09 13:54:56 +00:00
|
|
|
#include "r_skins.h"
|
2014-03-15 16:59:03 +00:00
|
|
|
#include "b_bot.h"
|
|
|
|
#include "z_zone.h"
|
|
|
|
|
|
|
|
#include "lua_script.h"
|
|
|
|
#include "lua_libs.h"
|
|
|
|
#include "lua_hook.h"
|
|
|
|
#include "lua_hud.h" // hud_running errors
|
|
|
|
|
2020-10-10 18:08:24 +00:00
|
|
|
#include "m_perfstats.h"
|
2023-01-15 17:57:23 +00:00
|
|
|
#include "d_netcmd.h" // for cv_perfstats
|
2020-11-07 09:32:59 +00:00
|
|
|
#include "i_system.h" // I_GetPreciseTime
|
2020-10-10 18:08:24 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
/* =========================================================================
|
|
|
|
ABSTRACTION
|
|
|
|
========================================================================= */
|
|
|
|
|
2021-07-07 03:23:38 +00:00
|
|
|
#define LIST(id, M) \
|
|
|
|
static const char * const id [] = { M (TOSTR) NULL }
|
2020-11-17 12:14:45 +00:00
|
|
|
|
2021-07-07 03:23:38 +00:00
|
|
|
LIST (mobjHookNames, MOBJ_HOOK_LIST);
|
|
|
|
LIST (hookNames, HOOK_LIST);
|
2021-07-07 07:23:51 +00:00
|
|
|
LIST (hudHookNames, HUD_HOOK_LIST);
|
2021-07-07 03:23:38 +00:00
|
|
|
LIST (stringHookNames, STRING_HOOK_LIST);
|
|
|
|
|
|
|
|
#undef LIST
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
typedef struct {
|
|
|
|
int numHooks;
|
|
|
|
int *ids;
|
|
|
|
} hook_t;
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
typedef struct {
|
|
|
|
int numGeneric;
|
|
|
|
int ref;
|
|
|
|
} stringhook_t;
|
2016-12-15 20:05:54 +00:00
|
|
|
|
2020-12-12 11:06:57 +00:00
|
|
|
static hook_t hookIds[HOOK(MAX)];
|
2021-07-07 07:23:51 +00:00
|
|
|
static hook_t hudHookIds[HUD_HOOK(MAX)];
|
2020-12-12 11:06:57 +00:00
|
|
|
static hook_t mobjHookIds[NUMMOBJTYPES][MOBJ_HOOK(MAX)];
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
// Lua tables are used to lookup string hook ids.
|
2020-12-12 11:06:57 +00:00
|
|
|
static stringhook_t stringHooks[STRING_HOOK(MAX)];
|
2020-05-30 18:28:45 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
// This will be indexed by hook id, the value of which fetches the registry.
|
|
|
|
static int * hookRefs;
|
2021-07-07 02:12:47 +00:00
|
|
|
static int nextid;
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
// After a hook errors once, don't print the error again.
|
|
|
|
static UINT8 * hooksErrored;
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-12-10 16:50:23 +00:00
|
|
|
static int errorRef;
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static boolean mobj_hook_available(int hook_type, mobjtype_t mobj_type)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
(
|
2020-12-10 13:39:53 +00:00
|
|
|
mobjHookIds [MT_NULL] [hook_type].numHooks > 0 ||
|
2022-12-31 12:20:00 +00:00
|
|
|
(mobj_type < NUMMOBJTYPES && mobjHookIds[mobj_type][hook_type].numHooks > 0)
|
2020-11-17 12:14:45 +00:00
|
|
|
);
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static int hook_in_list
|
|
|
|
(
|
|
|
|
const char * const name,
|
|
|
|
const char * const * const list
|
|
|
|
){
|
|
|
|
int type;
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
for (type = 0; list[type] != NULL; ++type)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
if (strcmp(name, list[type]) == 0)
|
|
|
|
break;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
return type;
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static void get_table(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_rawget(L, -3);
|
2016-11-04 17:56:25 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
if (lua_isnil(L, -1))
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_createtable(L, 1, 0);
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_rawset(L, -5);
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_remove(L, -2);
|
|
|
|
}
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2021-07-07 02:12:47 +00:00
|
|
|
static void add_hook_to_table(lua_State *L, int n)
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
2021-07-07 02:12:47 +00:00
|
|
|
lua_pushnumber(L, nextid);
|
2020-12-10 13:39:53 +00:00
|
|
|
lua_rawseti(L, -2, n);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2020-10-21 20:48:00 +00:00
|
|
|
|
2021-07-07 02:12:47 +00:00
|
|
|
static void add_string_hook(lua_State *L, int type)
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
|
|
|
stringhook_t * hook = &stringHooks[type];
|
|
|
|
|
|
|
|
char * string = NULL;
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
switch (type)
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-12-12 11:06:57 +00:00
|
|
|
case STRING_HOOK(BotAI):
|
|
|
|
case STRING_HOOK(ShouldJingleContinue):
|
2020-11-17 12:14:45 +00:00
|
|
|
if (lua_isstring(L, 3))
|
|
|
|
{ // lowercase copy
|
|
|
|
string = Z_StrDup(lua_tostring(L, 3));
|
|
|
|
strlwr(string);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-12-12 11:06:57 +00:00
|
|
|
case STRING_HOOK(LinedefExecute):
|
2020-11-17 12:14:45 +00:00
|
|
|
string = Z_StrDup(luaL_checkstring(L, 3));
|
|
|
|
strupr(string);
|
|
|
|
break;
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
if (hook->ref > 0)
|
|
|
|
lua_getref(L, hook->ref);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
hook->ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
|
|
|
|
if (string)
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_pushstring(L, string);
|
|
|
|
get_table(L);
|
2021-07-07 02:12:47 +00:00
|
|
|
add_hook_to_table(L, 1 + lua_objlen(L, -1));
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
|
|
|
else
|
2021-07-07 02:12:47 +00:00
|
|
|
add_hook_to_table(L, ++hook->numGeneric);
|
2020-12-10 13:39:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 02:12:47 +00:00
|
|
|
static void add_hook(hook_t *map)
|
2020-12-10 13:39:53 +00:00
|
|
|
{
|
|
|
|
Z_Realloc(map->ids, (map->numHooks + 1) * sizeof *map->ids,
|
|
|
|
PU_STATIC, &map->ids);
|
2021-07-07 02:12:47 +00:00
|
|
|
map->ids[map->numHooks++] = nextid;
|
2020-12-10 13:39:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 02:12:47 +00:00
|
|
|
static void add_mobj_hook(lua_State *L, int hook_type)
|
2020-12-10 13:39:53 +00:00
|
|
|
{
|
|
|
|
mobjtype_t mobj_type = luaL_optnumber(L, 3, MT_NULL);
|
|
|
|
|
|
|
|
luaL_argcheck(L, mobj_type < NUMMOBJTYPES, 3, "invalid mobjtype_t");
|
|
|
|
|
2021-07-07 02:12:47 +00:00
|
|
|
add_hook(&mobjHookIds[mobj_type][hook_type]);
|
|
|
|
}
|
|
|
|
|
2021-07-07 07:23:51 +00:00
|
|
|
static void add_hud_hook(lua_State *L, int idx)
|
|
|
|
{
|
|
|
|
add_hook(&hudHookIds[luaL_checkoption(L,
|
|
|
|
idx, "game", hudHookNames)]);
|
|
|
|
}
|
|
|
|
|
2021-07-07 02:12:47 +00:00
|
|
|
static void add_hook_ref(lua_State *L, int idx)
|
|
|
|
{
|
|
|
|
if (!(nextid & 7))
|
|
|
|
{
|
|
|
|
Z_Realloc(hooksErrored,
|
|
|
|
BIT_ARRAY_SIZE (nextid + 1) * sizeof *hooksErrored,
|
|
|
|
PU_STATIC, &hooksErrored);
|
|
|
|
hooksErrored[nextid >> 3] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Z_Realloc(hookRefs, (nextid + 1) * sizeof *hookRefs, PU_STATIC, &hookRefs);
|
|
|
|
|
|
|
|
// set the hook function in the registry.
|
|
|
|
lua_pushvalue(L, idx);
|
|
|
|
hookRefs[nextid++] = luaL_ref(L, LUA_REGISTRYINDEX);
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
// Takes hook, function, and additional arguments (mobj type to act on, etc.)
|
|
|
|
static int lib_addHook(lua_State *L)
|
2014-08-04 03:49:33 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
const char * name;
|
|
|
|
int type;
|
2014-08-04 03:49:33 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
if (!lua_lumploading)
|
|
|
|
return luaL_error(L, "This function cannot be called from within a hook or coroutine!");
|
2014-08-04 03:49:33 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
name = luaL_checkstring(L, 1);
|
|
|
|
luaL_checktype(L, 2, LUA_TFUNCTION);
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
/* this is a very special case */
|
2020-12-12 11:06:57 +00:00
|
|
|
if (( type = hook_in_list(name, stringHookNames) ) < STRING_HOOK(MAX))
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
2021-07-07 02:12:47 +00:00
|
|
|
add_string_hook(L, type);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2020-12-12 11:06:57 +00:00
|
|
|
else if (( type = hook_in_list(name, mobjHookNames) ) < MOBJ_HOOK(MAX))
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
2021-07-07 02:12:47 +00:00
|
|
|
add_mobj_hook(L, type);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2020-12-12 11:06:57 +00:00
|
|
|
else if (( type = hook_in_list(name, hookNames) ) < HOOK(MAX))
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
2021-07-07 02:12:47 +00:00
|
|
|
add_hook(&hookIds[type]);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2021-07-07 07:23:51 +00:00
|
|
|
else if (strcmp(name, "HUD") == 0)
|
|
|
|
{
|
|
|
|
add_hud_hook(L, 3);
|
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return luaL_argerror(L, 1, lua_pushfstring(L, "invalid hook " LUA_QS, name));
|
|
|
|
}
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2021-07-07 02:12:47 +00:00
|
|
|
add_hook_ref(L, 2);/* the function */
|
2020-11-17 12:14:45 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookLib(lua_State *L)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-12-10 16:50:23 +00:00
|
|
|
lua_pushcfunction(L, LUA_GetErrorMessage);
|
|
|
|
errorRef = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_register(L, "addHook", lib_addHook);
|
2020-12-10 16:50:23 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2021-07-07 07:23:51 +00:00
|
|
|
/* TODO: remove in next backwards incompatible release */
|
|
|
|
int lib_hudadd(lua_State *L);/* yeah compiler */
|
|
|
|
int lib_hudadd(lua_State *L)
|
|
|
|
{
|
|
|
|
if (!lua_lumploading)
|
|
|
|
return luaL_error(L, "This function cannot be called from within a hook or coroutine!");
|
|
|
|
|
|
|
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
|
|
|
|
|
|
|
add_hud_hook(L, 2);
|
|
|
|
add_hook_ref(L, 1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
typedef struct Hook_State Hook_State;
|
|
|
|
typedef void (*Hook_Callback)(Hook_State *);
|
|
|
|
|
|
|
|
struct Hook_State {
|
2021-06-08 01:13:22 +00:00
|
|
|
INT32 status;/* return status to calling function */
|
2020-11-17 12:14:45 +00:00
|
|
|
void * userdata;
|
2020-12-10 13:39:53 +00:00
|
|
|
int hook_type;
|
|
|
|
mobjtype_t mobj_type;/* >0 if mobj hook */
|
2020-11-17 12:14:45 +00:00
|
|
|
const char * string;/* used to fetch table, ran first if set */
|
|
|
|
int top;/* index of last argument passed to hook */
|
2020-12-10 13:39:53 +00:00
|
|
|
int id;/* id to fetch ref */
|
2020-11-17 12:14:45 +00:00
|
|
|
int values;/* num arguments passed to hook */
|
|
|
|
int results;/* num values returned by hook */
|
|
|
|
Hook_Callback results_handler;/* callback when hook successfully returns */
|
|
|
|
};
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
enum {
|
2020-12-10 13:39:53 +00:00
|
|
|
EINDEX = 1,/* error handler */
|
|
|
|
SINDEX = 2,/* string itself is pushed in case of string hook */
|
2020-11-17 12:14:45 +00:00
|
|
|
};
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static void push_error_handler(void)
|
|
|
|
{
|
2020-12-10 16:50:23 +00:00
|
|
|
lua_getref(gL, errorRef);
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
/* repush hook string */
|
|
|
|
static void push_string(void)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_pushvalue(gL, SINDEX);
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2021-07-07 01:42:08 +00:00
|
|
|
static boolean begin_hook_values(Hook_State *hook)
|
|
|
|
{
|
|
|
|
hook->top = lua_gettop(gL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void start_hook_stack(void)
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
2015-06-10 12:06:16 +00:00
|
|
|
lua_settop(gL, 0);
|
2020-11-17 12:14:45 +00:00
|
|
|
push_error_handler();
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static boolean init_hook_type
|
|
|
|
(
|
|
|
|
Hook_State * hook,
|
|
|
|
int status,
|
|
|
|
int hook_type,
|
|
|
|
mobjtype_t mobj_type,
|
|
|
|
const char * string,
|
2020-12-10 13:39:53 +00:00
|
|
|
int nonzero
|
2020-11-17 12:14:45 +00:00
|
|
|
){
|
|
|
|
hook->status = status;
|
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
if (nonzero)
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2021-07-07 01:42:08 +00:00
|
|
|
start_hook_stack();
|
2020-11-17 12:14:45 +00:00
|
|
|
hook->hook_type = hook_type;
|
|
|
|
hook->mobj_type = mobj_type;
|
|
|
|
hook->string = string;
|
2021-07-07 01:42:08 +00:00
|
|
|
return begin_hook_values(hook);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
else
|
|
|
|
return false;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static boolean prepare_hook
|
|
|
|
(
|
|
|
|
Hook_State * hook,
|
|
|
|
int default_status,
|
|
|
|
int hook_type
|
|
|
|
){
|
|
|
|
return init_hook_type(hook, default_status,
|
|
|
|
hook_type, 0, NULL,
|
2020-12-10 13:39:53 +00:00
|
|
|
hookIds[hook_type].numHooks);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2019-12-30 04:36:24 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static boolean prepare_mobj_hook
|
|
|
|
(
|
|
|
|
Hook_State * hook,
|
|
|
|
int default_status,
|
|
|
|
int hook_type,
|
2022-12-31 12:20:00 +00:00
|
|
|
mobj_t * primary_mobj
|
2020-11-17 12:14:45 +00:00
|
|
|
){
|
2022-12-31 12:20:00 +00:00
|
|
|
const mobjtype_t mobj_type =
|
|
|
|
primary_mobj ? primary_mobj->type : NUMMOBJTYPES;
|
|
|
|
|
2022-02-01 10:27:27 +00:00
|
|
|
#ifdef PARANOIA
|
|
|
|
if (mobj_type == MT_NULL)
|
|
|
|
I_Error("MT_NULL has been passed to a mobj hook\n");
|
|
|
|
#endif
|
2020-11-17 12:14:45 +00:00
|
|
|
return init_hook_type(hook, default_status,
|
|
|
|
hook_type, mobj_type, NULL,
|
2020-12-10 13:39:53 +00:00
|
|
|
mobj_hook_available(hook_type, mobj_type));
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2020-05-30 18:24:33 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static boolean prepare_string_hook
|
|
|
|
(
|
|
|
|
Hook_State * hook,
|
|
|
|
int default_status,
|
|
|
|
int hook_type,
|
|
|
|
const char * string
|
|
|
|
){
|
|
|
|
if (init_hook_type(hook, default_status,
|
|
|
|
hook_type, 0, string,
|
|
|
|
stringHooks[hook_type].ref))
|
2019-12-30 04:36:24 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_pushstring(gL, string);
|
2021-07-07 01:42:08 +00:00
|
|
|
return begin_hook_values(hook);
|
2019-12-30 04:36:24 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-30 18:24:33 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static void init_hook_call
|
|
|
|
(
|
|
|
|
Hook_State * hook,
|
|
|
|
int results,
|
|
|
|
Hook_Callback results_handler
|
|
|
|
){
|
2021-07-07 01:42:08 +00:00
|
|
|
const int top = lua_gettop(gL);
|
|
|
|
hook->values = (top - hook->top);
|
|
|
|
hook->top = top;
|
2020-11-17 12:14:45 +00:00
|
|
|
hook->results = results;
|
|
|
|
hook->results_handler = results_handler;
|
2019-12-30 04:36:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
static void get_hook(Hook_State *hook, const int *ids, int n)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-12-10 13:39:53 +00:00
|
|
|
hook->id = ids[n];
|
|
|
|
lua_getref(gL, hookRefs[hook->id]);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
static void get_hook_from_table(Hook_State *hook, int n)
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
|
|
|
lua_rawgeti(gL, -1, n);
|
2020-12-10 13:39:53 +00:00
|
|
|
hook->id = lua_tonumber(gL, -1);
|
2021-06-11 01:09:39 +00:00
|
|
|
lua_pop(gL, 1);
|
2020-12-10 13:39:53 +00:00
|
|
|
lua_getref(gL, hookRefs[hook->id]);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2020-05-30 18:24:33 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static int call_single_hook_no_copy(Hook_State *hook)
|
|
|
|
{
|
|
|
|
if (lua_pcall(gL, hook->values, hook->results, EINDEX) == 0)
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
if (hook->results > 0)
|
|
|
|
{
|
|
|
|
(*hook->results_handler)(hook);
|
|
|
|
lua_pop(gL, hook->results);
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* print the error message once */
|
|
|
|
if (cv_debug & DBG_LUA || !in_bit_array(hooksErrored, hook->id))
|
2020-10-10 18:08:24 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
CONS_Alert(CONS_WARNING, "%s\n", lua_tostring(gL, -1));
|
|
|
|
set_bit_array(hooksErrored, hook->id);
|
2020-10-10 18:08:24 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_pop(gL, 1);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
return 1;
|
2020-05-30 18:24:33 +00:00
|
|
|
}
|
2019-12-31 03:04:27 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static int call_single_hook(Hook_State *hook)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
int i;
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
for (i = -(hook->values) + 1; i <= 0; ++i)
|
|
|
|
lua_pushvalue(gL, hook->top + i);
|
|
|
|
|
|
|
|
return call_single_hook_no_copy(hook);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int call_hook_table_for(Hook_State *hook, int n)
|
|
|
|
{
|
|
|
|
int k;
|
2020-05-30 18:24:33 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
for (k = 1; k <= n; ++k)
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-12-10 13:39:53 +00:00
|
|
|
get_hook_from_table(hook, k);
|
2020-11-17 12:14:45 +00:00
|
|
|
call_single_hook(hook);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-05-30 18:24:33 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
return n;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static int call_hook_table(Hook_State *hook)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
return call_hook_table_for(hook, lua_objlen(gL, -1));
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
static int call_mapped(Hook_State *hook, const hook_t *map)
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
2020-12-10 13:39:53 +00:00
|
|
|
int k;
|
2020-11-17 12:14:45 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
for (k = 0; k < map->numHooks; ++k)
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
2020-12-10 13:39:53 +00:00
|
|
|
get_hook(hook, map->ids, k);
|
|
|
|
call_single_hook(hook);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2020-12-10 13:39:53 +00:00
|
|
|
|
|
|
|
return map->numHooks;
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2020-10-21 20:48:00 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static int call_string_hooks(Hook_State *hook)
|
|
|
|
{
|
2020-12-10 13:39:53 +00:00
|
|
|
const stringhook_t *map = &stringHooks[hook->hook_type];
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int calls = 0;
|
2019-06-19 11:28:57 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
lua_getref(gL, map->ref);
|
2016-11-04 17:56:25 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
/* call generic string hooks first */
|
2020-12-10 13:39:53 +00:00
|
|
|
calls += call_hook_table_for(hook, map->numGeneric);
|
2019-06-19 11:28:57 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
push_string();
|
|
|
|
lua_rawget(gL, -2);
|
|
|
|
calls += call_hook_table(hook);
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
return calls;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
static int call_mobj_type_hooks(Hook_State *hook, mobjtype_t mobj_type)
|
2019-12-31 23:17:02 +00:00
|
|
|
{
|
2020-12-10 13:39:53 +00:00
|
|
|
return call_mapped(hook, &mobjHookIds[mobj_type][hook->hook_type]);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2019-12-31 23:17:02 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static int call_hooks
|
|
|
|
(
|
|
|
|
Hook_State * hook,
|
|
|
|
int results,
|
|
|
|
Hook_Callback results_handler
|
|
|
|
){
|
|
|
|
int calls = 0;
|
2020-10-21 20:48:00 +00:00
|
|
|
|
2021-07-07 01:42:08 +00:00
|
|
|
init_hook_call(hook, results, results_handler);
|
2019-12-31 23:17:02 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
if (hook->string)
|
2019-12-31 23:17:02 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
calls += call_string_hooks(hook);
|
2019-12-31 23:17:02 +00:00
|
|
|
}
|
2020-12-10 13:39:53 +00:00
|
|
|
else if (hook->mobj_type > 0)
|
2019-12-31 23:17:02 +00:00
|
|
|
{
|
2020-12-10 13:39:53 +00:00
|
|
|
/* call generic mobj hooks first */
|
|
|
|
calls += call_mobj_type_hooks(hook, MT_NULL);
|
2022-12-31 12:20:00 +00:00
|
|
|
|
|
|
|
if (hook->mobj_type < NUMMOBJTYPES)
|
|
|
|
calls += call_mobj_type_hooks(hook, hook->mobj_type);
|
2020-11-17 12:14:45 +00:00
|
|
|
|
2021-10-25 17:49:15 +00:00
|
|
|
ps_lua_mobjhooks.value.i += calls;
|
2019-12-31 23:17:02 +00:00
|
|
|
}
|
2020-12-10 13:39:53 +00:00
|
|
|
else
|
|
|
|
calls += call_mapped(hook, &hookIds[hook->hook_type]);
|
2019-12-31 23:17:02 +00:00
|
|
|
|
|
|
|
lua_settop(gL, 0);
|
2020-11-17 12:14:45 +00:00
|
|
|
|
|
|
|
return calls;
|
2019-12-31 23:17:02 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
/* =========================================================================
|
|
|
|
COMMON RESULT HANDLERS
|
|
|
|
========================================================================= */
|
2016-12-15 20:05:54 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
#define res_none NULL
|
2017-12-17 20:59:24 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static void res_true(Hook_State *hook)
|
|
|
|
{
|
|
|
|
if (lua_toboolean(gL, -1))
|
|
|
|
hook->status = true;
|
|
|
|
}
|
2020-10-21 20:48:00 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static void res_false(Hook_State *hook)
|
|
|
|
{
|
|
|
|
if (!lua_isnil(gL, -1) && !lua_toboolean(gL, -1))
|
|
|
|
hook->status = false;
|
|
|
|
}
|
2016-12-15 20:05:54 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static void res_force(Hook_State *hook)
|
|
|
|
{
|
|
|
|
if (!lua_isnil(gL, -1))
|
2016-12-15 20:05:54 +00:00
|
|
|
{
|
|
|
|
if (lua_toboolean(gL, -1))
|
2020-11-17 12:14:45 +00:00
|
|
|
hook->status = 1; // Force yes
|
|
|
|
else
|
|
|
|
hook->status = 2; // Force no
|
2016-12-15 20:05:54 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2016-12-15 20:05:54 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
/* =========================================================================
|
|
|
|
GENERALISED HOOKS
|
|
|
|
========================================================================= */
|
|
|
|
|
|
|
|
int LUA_HookMobj(mobj_t *mobj, int hook_type)
|
|
|
|
{
|
|
|
|
Hook_State hook;
|
2022-12-31 12:20:00 +00:00
|
|
|
if (prepare_mobj_hook(&hook, false, hook_type, mobj))
|
2016-12-15 20:05:54 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, mobj, META_MOBJ);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
2016-12-15 20:05:54 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2016-12-15 20:05:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_Hook2Mobj(mobj_t *t1, mobj_t *t2, int hook_type)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2022-12-31 12:20:00 +00:00
|
|
|
if (prepare_mobj_hook(&hook, 0, hook_type, t1))
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
|
|
|
LUA_PushUserdata(gL, t1, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, t2, META_MOBJ);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_force);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
|
|
|
return hook.status;
|
|
|
|
}
|
2020-10-21 20:48:00 +00:00
|
|
|
|
2020-12-12 11:06:57 +00:00
|
|
|
void LUA_HookVoid(int type)
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
|
|
|
Hook_State hook;
|
|
|
|
if (prepare_hook(&hook, 0, type))
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 0, res_none);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
void LUA_HookInt(INT32 number, int hook_type)
|
|
|
|
{
|
|
|
|
Hook_State hook;
|
|
|
|
if (prepare_hook(&hook, 0, hook_type))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_pushinteger(gL, number);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 0, res_none);
|
2020-12-12 10:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LUA_HookBool(boolean value, int hook_type)
|
|
|
|
{
|
|
|
|
Hook_State hook;
|
|
|
|
if (prepare_hook(&hook, 0, hook_type))
|
|
|
|
{
|
|
|
|
lua_pushboolean(gL, value);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 0, res_none);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-19 11:28:57 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookPlayer(player_t *player, int hook_type)
|
|
|
|
{
|
|
|
|
Hook_State hook;
|
|
|
|
if (prepare_hook(&hook, false, hook_type))
|
|
|
|
{
|
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
|
|
|
}
|
2016-12-15 20:05:54 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookTiccmd(player_t *player, ticcmd_t *cmd, int hook_type)
|
|
|
|
{
|
|
|
|
Hook_State hook;
|
|
|
|
if (prepare_hook(&hook, false, hook_type))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
|
|
|
LUA_PushUserdata(gL, cmd, META_TICCMD);
|
2019-06-19 11:28:57 +00:00
|
|
|
|
2020-12-12 11:06:57 +00:00
|
|
|
if (hook_type == HOOK(PlayerCmd))
|
2020-11-17 12:14:45 +00:00
|
|
|
hook_cmd_running = true;
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
2020-11-17 12:14:45 +00:00
|
|
|
|
2020-12-12 11:06:57 +00:00
|
|
|
if (hook_type == HOOK(PlayerCmd))
|
2020-11-17 12:14:45 +00:00
|
|
|
hook_cmd_running = false;
|
|
|
|
}
|
|
|
|
return hook.status;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 18:34:59 +00:00
|
|
|
int LUA_HookKey(event_t *event, int hook_type)
|
2021-06-29 08:49:09 +00:00
|
|
|
{
|
|
|
|
Hook_State hook;
|
|
|
|
if (prepare_hook(&hook, false, hook_type))
|
|
|
|
{
|
2021-08-14 18:34:59 +00:00
|
|
|
LUA_PushUserdata(gL, event, META_KEYEVENT);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
2021-06-29 08:49:09 +00:00
|
|
|
}
|
|
|
|
return hook.status;
|
|
|
|
}
|
|
|
|
|
2022-04-30 06:50:12 +00:00
|
|
|
void LUA_HookHUD(int hook_type, huddrawlist_h list)
|
2021-07-07 07:23:51 +00:00
|
|
|
{
|
|
|
|
const hook_t * map = &hudHookIds[hook_type];
|
|
|
|
Hook_State hook;
|
|
|
|
if (map->numHooks > 0)
|
|
|
|
{
|
|
|
|
start_hook_stack();
|
|
|
|
begin_hook_values(&hook);
|
|
|
|
|
2022-04-30 06:50:12 +00:00
|
|
|
LUA_SetHudHook(hook_type, list);
|
2021-07-07 07:23:51 +00:00
|
|
|
|
|
|
|
hud_running = true; // local hook
|
|
|
|
init_hook_call(&hook, 0, res_none);
|
|
|
|
call_mapped(&hook, map);
|
|
|
|
hud_running = false;
|
2022-04-30 06:50:12 +00:00
|
|
|
|
|
|
|
lua_pushnil(gL);
|
|
|
|
lua_setfield(gL, LUA_REGISTRYINDEX, "HUD_DRAW_LIST");
|
2021-07-07 07:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
/* =========================================================================
|
|
|
|
SPECIALIZED HOOKS
|
|
|
|
========================================================================= */
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
void LUA_HookThinkFrame(void)
|
|
|
|
{
|
2020-12-12 11:06:57 +00:00
|
|
|
const int type = HOOK(ThinkFrame);
|
2020-12-10 13:39:53 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
// variables used by perf stats
|
|
|
|
int hook_index = 0;
|
2020-12-10 11:09:24 +00:00
|
|
|
precise_t time_taken = 0;
|
2017-12-17 20:59:24 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2020-10-21 20:48:00 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
const hook_t * map = &hookIds[type];
|
2020-11-17 12:14:45 +00:00
|
|
|
int k;
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
if (prepare_hook(&hook, 0, type))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2021-07-07 01:42:08 +00:00
|
|
|
init_hook_call(&hook, 0, res_none);
|
2019-06-19 11:28:57 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
for (k = 0; k < map->numHooks; ++k)
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-12-10 13:39:53 +00:00
|
|
|
get_hook(&hook, map->ids, k);
|
2020-11-17 12:14:45 +00:00
|
|
|
|
|
|
|
if (cv_perfstats.value == 3)
|
|
|
|
{
|
|
|
|
lua_pushvalue(gL, -1);/* need the function again */
|
2020-12-10 11:09:24 +00:00
|
|
|
time_taken = I_GetPreciseTime();
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
call_single_hook(&hook);
|
|
|
|
|
|
|
|
if (cv_perfstats.value == 3)
|
|
|
|
{
|
|
|
|
lua_Debug ar;
|
2021-06-12 01:31:38 +00:00
|
|
|
time_taken = I_GetPreciseTime() - time_taken;
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_getinfo(gL, ">S", &ar);
|
|
|
|
PS_SetThinkFrameHookInfo(hook_index, time_taken, ar.short_src);
|
|
|
|
hook_index++;
|
|
|
|
}
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
|
|
|
|
lua_settop(gL, 0);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2016-12-15 20:05:54 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookMobjLineCollide(mobj_t *mobj, line_t *line)
|
|
|
|
{
|
|
|
|
Hook_State hook;
|
2022-12-31 12:20:00 +00:00
|
|
|
if (prepare_mobj_hook(&hook, 0, MOBJ_HOOK(MobjLineCollide), mobj))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, mobj, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, line, META_LINE);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_force);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookTouchSpecial(mobj_t *special, mobj_t *toucher)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2022-12-31 12:20:00 +00:00
|
|
|
if (prepare_mobj_hook(&hook, false, MOBJ_HOOK(TouchSpecial), special))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, special, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, toucher, META_MOBJ);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
|
|
|
}
|
2016-12-15 20:05:54 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static int damage_hook
|
|
|
|
(
|
|
|
|
mobj_t *target,
|
|
|
|
mobj_t *inflictor,
|
|
|
|
mobj_t *source,
|
|
|
|
INT32 damage,
|
|
|
|
UINT8 damagetype,
|
|
|
|
int hook_type,
|
|
|
|
Hook_Callback results_handler
|
|
|
|
){
|
|
|
|
Hook_State hook;
|
2022-12-31 12:20:00 +00:00
|
|
|
if (prepare_mobj_hook(&hook, 0, hook_type, target))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, target, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, inflictor, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, source, META_MOBJ);
|
2021-07-07 01:42:08 +00:00
|
|
|
if (hook_type != MOBJ_HOOK(MobjDeath))
|
2019-06-19 11:28:57 +00:00
|
|
|
lua_pushinteger(gL, damage);
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_pushinteger(gL, damagetype);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, results_handler);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookShouldDamage(mobj_t *target, mobj_t *inflictor, mobj_t *source, INT32 damage, UINT8 damagetype)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
return damage_hook(target, inflictor, source, damage, damagetype,
|
2021-07-07 01:42:08 +00:00
|
|
|
MOBJ_HOOK(ShouldDamage), res_force);
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookMobjDamage(mobj_t *target, mobj_t *inflictor, mobj_t *source, INT32 damage, UINT8 damagetype)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
return damage_hook(target, inflictor, source, damage, damagetype,
|
2021-07-07 01:42:08 +00:00
|
|
|
MOBJ_HOOK(MobjDamage), res_true);
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookMobjDeath(mobj_t *target, mobj_t *inflictor, mobj_t *source, UINT8 damagetype)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
return damage_hook(target, inflictor, source, 0, damagetype,
|
2021-07-07 01:42:08 +00:00
|
|
|
MOBJ_HOOK(MobjDeath), res_true);
|
2020-11-17 12:14:45 +00:00
|
|
|
}
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2021-07-08 00:57:28 +00:00
|
|
|
int LUA_HookMobjMoveBlocked(mobj_t *t1, mobj_t *t2, line_t *line)
|
|
|
|
{
|
|
|
|
Hook_State hook;
|
2022-12-31 12:20:00 +00:00
|
|
|
if (prepare_mobj_hook(&hook, 0, MOBJ_HOOK(MobjMoveBlocked), t1))
|
2021-07-08 00:57:28 +00:00
|
|
|
{
|
|
|
|
LUA_PushUserdata(gL, t1, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, t2, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, line, META_LINE);
|
2021-09-13 01:36:45 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
2021-07-08 00:57:28 +00:00
|
|
|
}
|
|
|
|
return hook.status;
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
typedef struct {
|
|
|
|
mobj_t * tails;
|
|
|
|
ticcmd_t * cmd;
|
|
|
|
} BotAI_State;
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static boolean checkbotkey(const char *field)
|
|
|
|
{
|
|
|
|
return lua_toboolean(gL, -1) && strcmp(lua_tostring(gL, -2), field) == 0;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static void res_botai(Hook_State *hook)
|
2020-02-12 19:16:23 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
BotAI_State *botai = hook->userdata;
|
|
|
|
|
|
|
|
int k[8];
|
|
|
|
|
|
|
|
int fields = 0;
|
|
|
|
|
|
|
|
// This turns forward, backward, left, right, jump, and spin into a proper ticcmd for tails.
|
|
|
|
if (lua_istable(gL, -8)) {
|
|
|
|
lua_pushnil(gL); // key
|
|
|
|
while (lua_next(gL, -9)) {
|
2020-12-12 11:06:57 +00:00
|
|
|
#define CHECK(n, f) (checkbotkey(f) ? (k[(n)-1] = 1) : 0)
|
2020-11-17 12:14:45 +00:00
|
|
|
if (
|
2020-12-12 11:06:57 +00:00
|
|
|
CHECK(1, "forward") || CHECK(2, "backward") ||
|
|
|
|
CHECK(3, "left") || CHECK(4, "right") ||
|
|
|
|
CHECK(5, "strafeleft") || CHECK(6, "straferight") ||
|
|
|
|
CHECK(7, "jump") || CHECK(8, "spin")
|
2020-11-17 12:14:45 +00:00
|
|
|
){
|
|
|
|
if (8 <= ++fields)
|
|
|
|
{
|
|
|
|
lua_pop(gL, 2); // pop key and value
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 19:16:23 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_pop(gL, 1); // pop value
|
2020-12-12 11:06:57 +00:00
|
|
|
#undef CHECK
|
2020-02-12 19:16:23 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
} else {
|
|
|
|
while (fields < 8)
|
2020-02-12 19:16:23 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
k[fields] = lua_toboolean(gL, -8 + fields);
|
|
|
|
fields++;
|
2020-02-12 19:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
B_KeysToTiccmd(botai->tails, botai->cmd,
|
|
|
|
k[0],k[1],k[2],k[3],k[4],k[5],k[6],k[7]);
|
|
|
|
|
|
|
|
hook->status = true;
|
2020-02-12 19:16:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookBotAI(mobj_t *sonic, mobj_t *tails, ticcmd_t *cmd)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
const char *skin = ((skin_t *)tails->skin)->name;
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
|
|
|
BotAI_State botai;
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2020-12-12 11:06:57 +00:00
|
|
|
if (prepare_string_hook(&hook, false, STRING_HOOK(BotAI), skin))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, sonic, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, tails, META_MOBJ);
|
2019-06-19 11:28:57 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
botai.tails = tails;
|
|
|
|
botai.cmd = cmd;
|
|
|
|
|
|
|
|
hook.userdata = &botai;
|
|
|
|
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 8, res_botai);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2015-06-10 11:28:09 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
void LUA_HookLinedefExecute(line_t *line, mobj_t *mo, sector_t *sector)
|
2014-06-18 20:28:09 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
|
|
|
if (prepare_string_hook
|
2020-12-12 11:06:57 +00:00
|
|
|
(&hook, 0, STRING_HOOK(LinedefExecute), line->stringargs[0]))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, line, META_LINE);
|
|
|
|
LUA_PushUserdata(gL, mo, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, sector, META_SECTOR);
|
2021-10-25 17:49:15 +00:00
|
|
|
ps_lua_mobjhooks.value.i += call_hooks(&hook, 0, res_none);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2014-06-18 20:28:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookPlayerMsg(int source, int target, int flags, char *msg)
|
2014-08-04 03:49:33 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2020-12-12 11:06:57 +00:00
|
|
|
if (prepare_hook(&hook, false, HOOK(PlayerMsg)))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, &players[source], META_PLAYER); // Source player
|
|
|
|
if (flags & 2 /*HU_CSAY*/) { // csay TODO: make HU_CSAY accessible outside hu_stuff.c
|
|
|
|
lua_pushinteger(gL, 3); // type
|
|
|
|
lua_pushnil(gL); // target
|
|
|
|
} else if (target == -1) { // sayteam
|
|
|
|
lua_pushinteger(gL, 1); // type
|
|
|
|
lua_pushnil(gL); // target
|
|
|
|
} else if (target == 0) { // say
|
|
|
|
lua_pushinteger(gL, 0); // type
|
|
|
|
lua_pushnil(gL); // target
|
|
|
|
} else { // sayto
|
|
|
|
lua_pushinteger(gL, 2); // type
|
|
|
|
LUA_PushUserdata(gL, &players[target-1], META_PLAYER); // target
|
|
|
|
}
|
|
|
|
lua_pushstring(gL, msg); // msg
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2014-08-04 03:49:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookHurtMsg(player_t *player, mobj_t *inflictor, mobj_t *source, UINT8 damagetype)
|
2016-03-03 22:07:05 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2022-12-31 12:25:52 +00:00
|
|
|
if (prepare_mobj_hook(&hook, false, MOBJ_HOOK(HurtMsg), inflictor))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
|
|
|
LUA_PushUserdata(gL, inflictor, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, source, META_MOBJ);
|
|
|
|
lua_pushinteger(gL, damagetype);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2016-03-03 22:07:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
void LUA_HookNetArchive(lua_CFunction archFunc)
|
2017-04-15 20:41:22 +00:00
|
|
|
{
|
2020-12-12 11:06:57 +00:00
|
|
|
const hook_t * map = &hookIds[HOOK(NetVars)];
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
|
|
|
/* this is a remarkable case where the stack isn't reset */
|
2020-12-10 13:39:53 +00:00
|
|
|
if (map->numHooks > 0)
|
2020-11-17 12:14:45 +00:00
|
|
|
{
|
|
|
|
// stack: tables
|
|
|
|
I_Assert(lua_gettop(gL) > 0);
|
|
|
|
I_Assert(lua_istable(gL, -1));
|
2020-10-21 20:48:00 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
push_error_handler();
|
|
|
|
lua_insert(gL, EINDEX);
|
2019-06-19 11:28:57 +00:00
|
|
|
|
2021-07-07 01:42:08 +00:00
|
|
|
begin_hook_values(&hook);
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
// tables becomes an upvalue of archFunc
|
|
|
|
lua_pushvalue(gL, -1);
|
|
|
|
lua_pushcclosure(gL, archFunc, 1);
|
|
|
|
// stack: tables, archFunc
|
2017-04-15 20:41:22 +00:00
|
|
|
|
2021-07-07 01:42:08 +00:00
|
|
|
init_hook_call(&hook, 0, res_none);
|
2020-12-10 13:39:53 +00:00
|
|
|
call_mapped(&hook, map);
|
2019-06-19 11:28:57 +00:00
|
|
|
|
2021-06-11 00:47:03 +00:00
|
|
|
lua_pop(gL, 1); // pop archFunc
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_remove(gL, EINDEX); // pop error handler
|
|
|
|
// stack: tables
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2017-04-15 20:41:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookMapThingSpawn(mobj_t *mobj, mapthing_t *mthing)
|
2017-10-02 13:08:58 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2022-12-31 12:20:00 +00:00
|
|
|
if (prepare_mobj_hook(&hook, false, MOBJ_HOOK(MapThingSpawn), mobj))
|
2020-02-20 21:40:39 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, mobj, META_MOBJ);
|
|
|
|
LUA_PushUserdata(gL, mthing, META_MAPTHING);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2017-10-02 13:08:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookFollowMobj(player_t *player, mobj_t *mobj)
|
"PlayerCanDamage" hook!
* Takes function(player, mo) input.
* Return TRUE for stating that yes, the player is in a state that can cause contact damage, do with that what you will.
* Return FALSE for stating that no, the player is weak and vulnerable and cannot cause contact damage, do with that what you will.
* Return NIL for allowing the function to continue regular operation.
Fills a different ideological niche than ShouldDamage - that's for determining whether damage dished between two objects should happen, this is for determining which way around damage should be dished when considering a player-object interaction.
Or, in other words, think of it as "ShouldDamage is whether damage that has been requested should be granted, for object-object interaction, while PlayerCanDamage is for whether global player properties should cause damage to enemies and monitors in the first place, like spinning, hammering or stomping."
2019-06-19 11:55:05 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2022-12-31 12:20:00 +00:00
|
|
|
if (prepare_mobj_hook(&hook, false, MOBJ_HOOK(FollowMobj), mobj))
|
"PlayerCanDamage" hook!
* Takes function(player, mo) input.
* Return TRUE for stating that yes, the player is in a state that can cause contact damage, do with that what you will.
* Return FALSE for stating that no, the player is weak and vulnerable and cannot cause contact damage, do with that what you will.
* Return NIL for allowing the function to continue regular operation.
Fills a different ideological niche than ShouldDamage - that's for determining whether damage dished between two objects should happen, this is for determining which way around damage should be dished when considering a player-object interaction.
Or, in other words, think of it as "ShouldDamage is whether damage that has been requested should be granted, for object-object interaction, while PlayerCanDamage is for whether global player properties should cause damage to enemies and monitors in the first place, like spinning, hammering or stomping."
2019-06-19 11:55:05 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
|
|
|
LUA_PushUserdata(gL, mobj, META_MOBJ);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
"PlayerCanDamage" hook!
* Takes function(player, mo) input.
* Return TRUE for stating that yes, the player is in a state that can cause contact damage, do with that what you will.
* Return FALSE for stating that no, the player is weak and vulnerable and cannot cause contact damage, do with that what you will.
* Return NIL for allowing the function to continue regular operation.
Fills a different ideological niche than ShouldDamage - that's for determining whether damage dished between two objects should happen, this is for determining which way around damage should be dished when considering a player-object interaction.
Or, in other words, think of it as "ShouldDamage is whether damage that has been requested should be granted, for object-object interaction, while PlayerCanDamage is for whether global player properties should cause damage to enemies and monitors in the first place, like spinning, hammering or stomping."
2019-06-19 11:55:05 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
"PlayerCanDamage" hook!
* Takes function(player, mo) input.
* Return TRUE for stating that yes, the player is in a state that can cause contact damage, do with that what you will.
* Return FALSE for stating that no, the player is weak and vulnerable and cannot cause contact damage, do with that what you will.
* Return NIL for allowing the function to continue regular operation.
Fills a different ideological niche than ShouldDamage - that's for determining whether damage dished between two objects should happen, this is for determining which way around damage should be dished when considering a player-object interaction.
Or, in other words, think of it as "ShouldDamage is whether damage that has been requested should be granted, for object-object interaction, while PlayerCanDamage is for whether global player properties should cause damage to enemies and monitors in the first place, like spinning, hammering or stomping."
2019-06-19 11:55:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookPlayerCanDamage(player_t *player, mobj_t *mobj)
|
2016-10-21 02:25:11 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2020-12-12 11:06:57 +00:00
|
|
|
if (prepare_hook(&hook, 0, HOOK(PlayerCanDamage)))
|
2019-06-19 11:28:57 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
|
|
|
LUA_PushUserdata(gL, mobj, META_MOBJ);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_force);
|
2019-06-19 11:28:57 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2016-10-21 02:25:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
void LUA_HookPlayerQuit(player_t *plr, kickreason_t reason)
|
2019-10-14 00:50:46 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2020-12-12 11:06:57 +00:00
|
|
|
if (prepare_hook(&hook, 0, HOOK(PlayerQuit)))
|
2019-10-15 01:47:20 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, plr, META_PLAYER); // Player that quit
|
|
|
|
lua_pushinteger(gL, reason); // Reason for quitting
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 0, res_none);
|
2019-10-15 01:47:20 +00:00
|
|
|
}
|
2019-10-14 00:50:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookTeamSwitch(player_t *player, int newteam, boolean fromspectators, boolean tryingautobalance, boolean tryingscramble)
|
2019-12-19 02:40:58 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2020-12-12 11:06:57 +00:00
|
|
|
if (prepare_hook(&hook, true, HOOK(TeamSwitch)))
|
2019-12-19 02:40:58 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
|
|
|
lua_pushinteger(gL, newteam);
|
|
|
|
lua_pushboolean(gL, fromspectators);
|
|
|
|
lua_pushboolean(gL, tryingautobalance);
|
|
|
|
lua_pushboolean(gL, tryingscramble);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_false);
|
2019-12-19 02:40:58 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2019-12-19 02:40:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookViewpointSwitch(player_t *player, player_t *newdisplayplayer, boolean forced)
|
2019-12-18 23:43:54 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2020-12-12 11:06:57 +00:00
|
|
|
if (prepare_hook(&hook, 0, HOOK(ViewpointSwitch)))
|
2019-12-18 23:43:54 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
|
|
|
LUA_PushUserdata(gL, newdisplayplayer, META_PLAYER);
|
|
|
|
lua_pushboolean(gL, forced);
|
2019-12-18 23:43:54 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
hud_running = true; // local hook
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_force);
|
2020-11-17 12:14:45 +00:00
|
|
|
hud_running = false;
|
2019-12-18 23:43:54 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2019-12-18 23:43:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookSeenPlayer(player_t *player, player_t *seenfriend)
|
2019-12-31 17:37:45 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2020-12-12 11:06:57 +00:00
|
|
|
if (prepare_hook(&hook, true, HOOK(SeenPlayer)))
|
2019-12-31 17:37:45 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
|
|
|
LUA_PushUserdata(gL, seenfriend, META_PLAYER);
|
2019-12-31 17:37:45 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
hud_running = true; // local hook
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_false);
|
2020-11-17 12:14:45 +00:00
|
|
|
hud_running = false;
|
2019-12-31 17:37:45 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2019-12-31 17:37:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookShouldJingleContinue(player_t *player, const char *musname)
|
2020-02-23 23:20:44 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
|
|
|
if (prepare_string_hook
|
2020-12-12 11:06:57 +00:00
|
|
|
(&hook, false, STRING_HOOK(ShouldJingleContinue), musname))
|
2020-02-23 23:20:44 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
|
|
|
push_string();
|
2020-02-23 23:20:44 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
hud_running = true; // local hook
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_true);
|
2020-11-17 12:14:45 +00:00
|
|
|
hud_running = false;
|
2020-02-23 23:20:44 +00:00
|
|
|
}
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
2020-03-25 03:55:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
boolean hook_cmd_running = false;
|
2020-03-25 03:55:25 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static void update_music_name(struct MusicChange *musicchange)
|
|
|
|
{
|
|
|
|
size_t length;
|
|
|
|
const char * new = lua_tolstring(gL, -6, &length);
|
2020-06-11 20:11:01 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
if (length < 7)
|
2020-03-25 03:55:25 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
strcpy(musicchange->newname, new);
|
|
|
|
lua_pushvalue(gL, -6);/* may as well keep it for next call */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(musicchange->newname, new, 6);
|
|
|
|
musicchange->newname[6] = '\0';
|
|
|
|
lua_pushlstring(gL, new, 6);
|
2020-03-25 03:55:25 +00:00
|
|
|
}
|
2020-07-17 05:08:38 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_replace(gL, -7);
|
2020-06-12 12:50:57 +00:00
|
|
|
}
|
2020-07-17 05:08:38 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
static void res_musicchange(Hook_State *hook)
|
2020-07-17 05:08:38 +00:00
|
|
|
{
|
2020-11-17 12:14:45 +00:00
|
|
|
struct MusicChange *musicchange = hook->userdata;
|
|
|
|
|
|
|
|
// output 1: true, false, or string musicname override
|
|
|
|
if (lua_isstring(gL, -6))
|
|
|
|
update_music_name(musicchange);
|
|
|
|
else if (lua_isboolean(gL, -6) && lua_toboolean(gL, -6))
|
|
|
|
hook->status = true;
|
|
|
|
|
|
|
|
// output 2: mflags override
|
|
|
|
if (lua_isnumber(gL, -5))
|
|
|
|
*musicchange->mflags = lua_tonumber(gL, -5);
|
|
|
|
// output 3: looping override
|
|
|
|
if (lua_isboolean(gL, -4))
|
|
|
|
*musicchange->looping = lua_toboolean(gL, -4);
|
|
|
|
// output 4: position override
|
2021-06-08 01:13:22 +00:00
|
|
|
if (lua_isnumber(gL, -3))
|
2020-11-17 12:14:45 +00:00
|
|
|
*musicchange->position = lua_tonumber(gL, -3);
|
|
|
|
// output 5: prefadems override
|
2021-06-08 01:13:22 +00:00
|
|
|
if (lua_isnumber(gL, -2))
|
2020-11-17 12:14:45 +00:00
|
|
|
*musicchange->prefadems = lua_tonumber(gL, -2);
|
|
|
|
// output 6: fadeinms override
|
2021-06-08 01:13:22 +00:00
|
|
|
if (lua_isnumber(gL, -1))
|
2020-11-17 12:14:45 +00:00
|
|
|
*musicchange->fadeinms = lua_tonumber(gL, -1);
|
2020-07-17 05:08:38 +00:00
|
|
|
}
|
2020-11-01 01:15:41 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
int LUA_HookMusicChange(const char *oldname, struct MusicChange *param)
|
2020-11-01 01:15:41 +00:00
|
|
|
{
|
2020-12-12 11:06:57 +00:00
|
|
|
const int type = HOOK(MusicChange);
|
2020-12-10 13:39:53 +00:00
|
|
|
const hook_t * map = &hookIds[type];
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
Hook_State hook;
|
2020-11-01 01:15:41 +00:00
|
|
|
|
2020-12-10 13:39:53 +00:00
|
|
|
int k;
|
|
|
|
|
|
|
|
if (prepare_hook(&hook, false, type))
|
|
|
|
{
|
2021-07-07 01:42:08 +00:00
|
|
|
init_hook_call(&hook, 6, res_musicchange);
|
|
|
|
hook.values = 7;/* values pushed later */
|
2020-11-17 12:14:45 +00:00
|
|
|
hook.userdata = param;
|
2020-11-01 01:15:41 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_pushstring(gL, oldname);/* the only constant value */
|
|
|
|
lua_pushstring(gL, param->newname);/* semi constant */
|
2020-11-01 01:15:41 +00:00
|
|
|
|
2021-12-05 16:59:33 +00:00
|
|
|
for (k = 0; k < map->numHooks; ++k)
|
2020-12-10 13:39:53 +00:00
|
|
|
{
|
|
|
|
get_hook(&hook, map->ids, k);
|
2020-11-01 01:15:41 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_pushvalue(gL, -3);
|
|
|
|
lua_pushvalue(gL, -3);
|
|
|
|
lua_pushinteger(gL, *param->mflags);
|
|
|
|
lua_pushboolean(gL, *param->looping);
|
|
|
|
lua_pushinteger(gL, *param->position);
|
|
|
|
lua_pushinteger(gL, *param->prefadems);
|
|
|
|
lua_pushinteger(gL, *param->fadeinms);
|
|
|
|
|
|
|
|
call_single_hook_no_copy(&hook);
|
2020-11-01 01:15:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
lua_settop(gL, 0);
|
|
|
|
}
|
2020-12-10 13:39:53 +00:00
|
|
|
|
2020-11-17 12:14:45 +00:00
|
|
|
return hook.status;
|
|
|
|
}
|
2021-06-08 01:13:22 +00:00
|
|
|
|
|
|
|
static void res_playerheight(Hook_State *hook)
|
|
|
|
{
|
|
|
|
if (!lua_isnil(gL, -1))
|
|
|
|
{
|
|
|
|
fixed_t returnedheight = lua_tonumber(gL, -1);
|
|
|
|
// 0 height has... strange results, but it's not problematic like negative heights are.
|
|
|
|
// when an object's height is set to a negative number directly with lua, it's forced to 0 instead.
|
|
|
|
// here, I think it's better to ignore negatives so that they don't replace any results of previous hooks!
|
|
|
|
if (returnedheight >= 0)
|
|
|
|
hook->status = returnedheight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fixed_t LUA_HookPlayerHeight(player_t *player)
|
|
|
|
{
|
|
|
|
Hook_State hook;
|
|
|
|
if (prepare_hook(&hook, -1, HOOK(PlayerHeight)))
|
|
|
|
{
|
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_playerheight);
|
2021-06-08 01:13:22 +00:00
|
|
|
}
|
|
|
|
return hook.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LUA_HookPlayerCanEnterSpinGaps(player_t *player)
|
|
|
|
{
|
|
|
|
Hook_State hook;
|
|
|
|
if (prepare_hook(&hook, 0, HOOK(PlayerCanEnterSpinGaps)))
|
|
|
|
{
|
|
|
|
LUA_PushUserdata(gL, player, META_PLAYER);
|
2021-07-07 01:42:08 +00:00
|
|
|
call_hooks(&hook, 1, res_force);
|
2021-06-08 01:13:22 +00:00
|
|
|
}
|
|
|
|
return hook.status;
|
|
|
|
}
|