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.
|
2023-03-31 12:53:31 +00:00
|
|
|
// Copyright (C) 2012-2023 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_thinkerlib.c
|
|
|
|
/// \brief thinker library for Lua scripting
|
|
|
|
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "p_local.h"
|
|
|
|
#include "lua_script.h"
|
|
|
|
#include "lua_libs.h"
|
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
#define META_ITERATIONSTATE "iteration state"
|
|
|
|
|
2019-04-21 12:52:55 +00:00
|
|
|
/*static const char *const iter_opt[] = {
|
2014-03-15 16:59:03 +00:00
|
|
|
"all",
|
|
|
|
"mobj",
|
|
|
|
NULL};
|
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
static const actionf_p1 iter_funcs[] = {
|
|
|
|
NULL,
|
|
|
|
(actionf_p1)P_MobjThinker
|
2019-04-21 12:52:55 +00:00
|
|
|
};*/
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
struct iterationState {
|
|
|
|
actionf_p1 filter;
|
|
|
|
int next;
|
|
|
|
};
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
static int iterationState_gc(lua_State *L)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2017-05-27 19:03:27 +00:00
|
|
|
struct iterationState *it = luaL_checkudata(L, -1, META_ITERATIONSTATE);
|
|
|
|
if (it->next != LUA_REFNIL)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2017-05-27 19:03:27 +00:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, it->next);
|
|
|
|
it->next = LUA_REFNIL;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
2017-05-27 19:03:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
#define push_thinker(th) {\
|
|
|
|
if ((th)->function.acp1 == (actionf_p1)P_MobjThinker) \
|
|
|
|
LUA_PushUserdata(L, (th), META_MOBJ); \
|
|
|
|
else \
|
|
|
|
lua_pushlightuserdata(L, (th)); \
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
static int lib_iterateThinkers(lua_State *L)
|
|
|
|
{
|
|
|
|
thinker_t *th = NULL, *next = NULL;
|
2017-05-31 14:18:05 +00:00
|
|
|
struct iterationState *it;
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2019-07-30 16:48:13 +00:00
|
|
|
INLEVEL
|
2017-01-18 22:02:28 +00:00
|
|
|
|
2017-05-31 14:18:05 +00:00
|
|
|
it = luaL_checkudata(L, 1, META_ITERATIONSTATE);
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
lua_settop(L, 2);
|
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
if (lua_isnil(L, 2))
|
2019-04-21 12:52:55 +00:00
|
|
|
th = &thlist[THINK_MOBJ];
|
2017-05-27 19:03:27 +00:00
|
|
|
else if (lua_isuserdata(L, 2))
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2017-05-27 19:03:27 +00:00
|
|
|
if (lua_islightuserdata(L, 2))
|
|
|
|
th = lua_touserdata(L, 2);
|
|
|
|
else
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2017-05-27 19:03:27 +00:00
|
|
|
th = *(thinker_t **)lua_touserdata(L, -1);
|
|
|
|
if (!th)
|
|
|
|
{
|
|
|
|
if (it->next == LUA_REFNIL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, it->next);
|
|
|
|
if (lua_islightuserdata(L, -1))
|
|
|
|
next = lua_touserdata(L, -1);
|
|
|
|
else
|
|
|
|
next = *(thinker_t **)lua_touserdata(L, -1);
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, it->next);
|
|
|
|
it->next = LUA_REFNIL;
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
if (th && !next)
|
|
|
|
next = th->next;
|
|
|
|
if (!next)
|
|
|
|
return luaL_error(L, "next thinker invalidated during iteration");
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2019-04-21 12:52:55 +00:00
|
|
|
for (; next != &thlist[THINK_MOBJ]; next = next->next)
|
2017-05-27 19:03:27 +00:00
|
|
|
if (!it->filter || next->function.acp1 == it->filter)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2017-05-27 19:03:27 +00:00
|
|
|
push_thinker(next);
|
2019-04-21 12:52:55 +00:00
|
|
|
if (next->next != &thlist[THINK_MOBJ])
|
2017-05-27 19:03:27 +00:00
|
|
|
{
|
|
|
|
push_thinker(next->next);
|
|
|
|
it->next = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_startIterate(lua_State *L)
|
|
|
|
{
|
2017-05-27 19:03:27 +00:00
|
|
|
struct iterationState *it;
|
|
|
|
|
2019-07-30 16:48:13 +00:00
|
|
|
INLEVEL
|
2017-05-31 14:18:05 +00:00
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
|
|
|
it = lua_newuserdata(L, sizeof(struct iterationState));
|
|
|
|
luaL_getmetatable(L, META_ITERATIONSTATE);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
2019-04-21 12:52:55 +00:00
|
|
|
it->filter = (actionf_p1)P_MobjThinker; //iter_funcs[luaL_checkoption(L, 1, "mobj", iter_opt)];
|
2017-05-27 19:03:27 +00:00
|
|
|
it->next = LUA_REFNIL;
|
2014-03-15 16:59:03 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:03:27 +00:00
|
|
|
#undef push_thinker
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
int LUA_ThinkerLib(lua_State *L)
|
|
|
|
{
|
2017-05-27 19:03:27 +00:00
|
|
|
luaL_newmetatable(L, META_ITERATIONSTATE);
|
2023-10-28 09:06:30 +00:00
|
|
|
LUA_SetCFunctionField(L, "__gc", iterationState_gc);
|
2017-05-27 19:03:27 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
lua_createtable(L, 0, 1);
|
2017-05-27 19:03:27 +00:00
|
|
|
lua_pushcfunction(L, lib_iterateThinkers);
|
|
|
|
lua_pushcclosure(L, lib_startIterate, 1);
|
2014-03-15 16:59:03 +00:00
|
|
|
lua_setfield(L, -2, "iterate");
|
2019-04-21 12:52:55 +00:00
|
|
|
lua_setglobal(L, "mobjs");
|
2014-03-15 16:59:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|