mirror of
https://git.do.srb2.org/STJr/SRB2.git
synced 2025-03-21 18:32:08 +00:00
Lua taglib for accessing taggroups
The global "tags" can be iterated upon for every unique tag which is set in the level. If a tag is set on a sector/line/thing, it will be included. Taking the length of "tags" will give you the number of these unique tags. (If a tag is set on multiple sectors/lines/things, it will only be counted once though.) For sectors, lines and mapthings, call the field "tagged". This function takes one argument, which is the tag. The return value can be iterated over for all the sectors/lines/things with that tag. The length can also be taken for the number of such objects. If no argument is given, the global tag is default.
This commit is contained in:
parent
d004515d6a
commit
621efbfa15
10 changed files with 208 additions and 115 deletions
|
@ -276,6 +276,7 @@ set(SRB2_LUA_SOURCES
|
|||
lua_hudlib.c
|
||||
lua_infolib.c
|
||||
lua_maplib.c
|
||||
lua_taglib.c
|
||||
lua_mathlib.c
|
||||
lua_mobjlib.c
|
||||
lua_playerlib.c
|
||||
|
|
|
@ -47,6 +47,7 @@ OBJS:=$(OBJS) \
|
|||
$(OBJDIR)/lua_skinlib.o \
|
||||
$(OBJDIR)/lua_thinkerlib.o \
|
||||
$(OBJDIR)/lua_maplib.o \
|
||||
$(OBJDIR)/lua_taglib.o \
|
||||
$(OBJDIR)/lua_polyobjlib.o \
|
||||
$(OBJDIR)/lua_blockmaplib.o \
|
||||
$(OBJDIR)/lua_hudlib.o
|
||||
|
|
|
@ -379,4 +379,26 @@ Needed for some lua shenanigans.
|
|||
#define FIELDFROM( type, field, have, want ) \
|
||||
(void *)((intptr_t)(field) - offsetof (type, have) + offsetof (type, want))
|
||||
|
||||
typedef UINT8 bitarray_t;
|
||||
|
||||
#define BIT_ARRAY_SIZE(n) (((n) + 7) >> 3)
|
||||
|
||||
static inline int
|
||||
in_bit_array (const bitarray_t * const array, const int value)
|
||||
{
|
||||
return (array[value >> 3] & (1<<(value & 7)));
|
||||
}
|
||||
|
||||
static inline void
|
||||
set_bit_array (bitarray_t * const array, const int value)
|
||||
{
|
||||
array[value >> 3] |= (1<<(value & 7));
|
||||
}
|
||||
|
||||
static inline void
|
||||
unset_bit_array (bitarray_t * const array, const int value)
|
||||
{
|
||||
array[value >> 3] &= ~(1<<(value & 7));
|
||||
}
|
||||
|
||||
#endif //__DOOMTYPE__
|
||||
|
|
|
@ -93,6 +93,7 @@ int LUA_PlayerLib(lua_State *L);
|
|||
int LUA_SkinLib(lua_State *L);
|
||||
int LUA_ThinkerLib(lua_State *L);
|
||||
int LUA_MapLib(lua_State *L);
|
||||
int LUA_TagLib(lua_State *L);
|
||||
int LUA_PolyObjLib(lua_State *L);
|
||||
int LUA_BlockmapLib(lua_State *L);
|
||||
int LUA_HudLib(lua_State *L);
|
||||
|
|
|
@ -1385,25 +1385,15 @@ static int lib_iterateSectors(lua_State *L)
|
|||
|
||||
static int lib_getSector(lua_State *L)
|
||||
{
|
||||
int field;
|
||||
INLEVEL
|
||||
lua_settop(L, 2);
|
||||
lua_remove(L, 1); // dummy userdata table is unused.
|
||||
if (lua_isnumber(L, 1))
|
||||
if (lua_isnumber(L, 2))
|
||||
{
|
||||
size_t i = lua_tointeger(L, 1);
|
||||
size_t i = lua_tointeger(L, 2);
|
||||
if (i >= numsectors)
|
||||
return 0;
|
||||
LUA_PushUserdata(L, §ors[i], META_SECTOR);
|
||||
return 1;
|
||||
}
|
||||
field = luaL_checkoption(L, 1, NULL, array_opt);
|
||||
switch(field)
|
||||
{
|
||||
case 0: // iterate
|
||||
lua_pushcfunction(L, lib_iterateSectors);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1489,25 +1479,15 @@ static int lib_iterateLines(lua_State *L)
|
|||
|
||||
static int lib_getLine(lua_State *L)
|
||||
{
|
||||
int field;
|
||||
INLEVEL
|
||||
lua_settop(L, 2);
|
||||
lua_remove(L, 1); // dummy userdata table is unused.
|
||||
if (lua_isnumber(L, 1))
|
||||
if (lua_isnumber(L, 2))
|
||||
{
|
||||
size_t i = lua_tointeger(L, 1);
|
||||
size_t i = lua_tointeger(L, 2);
|
||||
if (i >= numlines)
|
||||
return 0;
|
||||
LUA_PushUserdata(L, &lines[i], META_LINE);
|
||||
return 1;
|
||||
}
|
||||
field = luaL_checkoption(L, 1, NULL, array_opt);
|
||||
switch(field)
|
||||
{
|
||||
case 0: // iterate
|
||||
lua_pushcfunction(L, lib_iterateLines);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -2358,15 +2338,13 @@ int LUA_MapLib(lua_State *L)
|
|||
//lua_setfield(L, -2, "__len");
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_newuserdata(L, 0);
|
||||
lua_createtable(L, 0, 2);
|
||||
lua_pushcfunction(L, lib_getSector);
|
||||
lua_setfield(L, -2, "__index");
|
||||
|
||||
lua_pushcfunction(L, lib_numsectors);
|
||||
lua_setfield(L, -2, "__len");
|
||||
lua_setmetatable(L, -2);
|
||||
lua_setglobal(L, "sectors");
|
||||
LUA_PushTaggableObjectArray(L, "sectors",
|
||||
lib_iterateSectors,
|
||||
lib_getSector,
|
||||
lib_numsectors,
|
||||
tags_sectors,
|
||||
&numsectors, §ors,
|
||||
sizeof (sector_t), META_SECTOR);
|
||||
|
||||
lua_newuserdata(L, 0);
|
||||
lua_createtable(L, 0, 2);
|
||||
|
@ -2378,15 +2356,13 @@ int LUA_MapLib(lua_State *L)
|
|||
lua_setmetatable(L, -2);
|
||||
lua_setglobal(L, "subsectors");
|
||||
|
||||
lua_newuserdata(L, 0);
|
||||
lua_createtable(L, 0, 2);
|
||||
lua_pushcfunction(L, lib_getLine);
|
||||
lua_setfield(L, -2, "__index");
|
||||
|
||||
lua_pushcfunction(L, lib_numlines);
|
||||
lua_setfield(L, -2, "__len");
|
||||
lua_setmetatable(L, -2);
|
||||
lua_setglobal(L, "lines");
|
||||
LUA_PushTaggableObjectArray(L, "lines",
|
||||
lib_iterateLines,
|
||||
lib_getLine,
|
||||
lib_numlines,
|
||||
tags_lines,
|
||||
&numlines, &lines,
|
||||
sizeof (line_t), META_LINE);
|
||||
|
||||
lua_newuserdata(L, 0);
|
||||
lua_createtable(L, 0, 2);
|
||||
|
|
|
@ -22,8 +22,6 @@
|
|||
#include "lua_hud.h" // hud_running errors
|
||||
#include "lua_hook.h" // hook_cmd_running errors
|
||||
|
||||
static const char *const array_opt[] ={"iterate",NULL};
|
||||
|
||||
enum mobj_e {
|
||||
mobj_valid = 0,
|
||||
mobj_x,
|
||||
|
@ -1003,25 +1001,15 @@ static int lib_iterateMapthings(lua_State *L)
|
|||
|
||||
static int lib_getMapthing(lua_State *L)
|
||||
{
|
||||
int field;
|
||||
INLEVEL
|
||||
lua_settop(L, 2);
|
||||
lua_remove(L, 1); // dummy userdata table is unused.
|
||||
if (lua_isnumber(L, 1))
|
||||
if (lua_isnumber(L, 2))
|
||||
{
|
||||
size_t i = lua_tointeger(L, 1);
|
||||
size_t i = lua_tointeger(L, 2);
|
||||
if (i >= nummapthings)
|
||||
return 0;
|
||||
LUA_PushUserdata(L, &mapthings[i], META_MAPTHING);
|
||||
return 1;
|
||||
}
|
||||
field = luaL_checkoption(L, 1, NULL, array_opt);
|
||||
switch(field)
|
||||
{
|
||||
case 0: // iterate
|
||||
lua_pushcfunction(L, lib_iterateMapthings);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1068,14 +1056,13 @@ int LUA_MobjLib(lua_State *L)
|
|||
lua_setfield(L, -2, "__len");
|
||||
lua_pop(L,1);
|
||||
|
||||
lua_newuserdata(L, 0);
|
||||
lua_createtable(L, 0, 2);
|
||||
lua_pushcfunction(L, lib_getMapthing);
|
||||
lua_setfield(L, -2, "__index");
|
||||
LUA_PushTaggableObjectArray(L, "mapthings",
|
||||
lib_iterateMapthings,
|
||||
lib_getMapthing,
|
||||
lib_nummapthings,
|
||||
tags_mapthings,
|
||||
&nummapthings, &mapthings,
|
||||
sizeof (mapthing_t), META_MAPTHING);
|
||||
|
||||
lua_pushcfunction(L, lib_nummapthings);
|
||||
lua_setfield(L, -2, "__len");
|
||||
lua_setmetatable(L, -2);
|
||||
lua_setglobal(L, "mapthings");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -53,6 +53,7 @@ static lua_CFunction liblist[] = {
|
|||
LUA_SkinLib, // skin_t, skins[]
|
||||
LUA_ThinkerLib, // thinker_t
|
||||
LUA_MapLib, // line_t, side_t, sector_t, subsector_t
|
||||
LUA_TagLib, // tags
|
||||
LUA_PolyObjLib, // polyobj_t
|
||||
LUA_BlockmapLib, // blockmap stuff
|
||||
LUA_HudLib, // HUD stuff
|
||||
|
@ -739,25 +740,37 @@ void LUA_PushLightUserdata (lua_State *L, void *data, const char *meta)
|
|||
// Pushes it to the stack and stores it in the registry.
|
||||
void LUA_PushUserdata(lua_State *L, void *data, const char *meta)
|
||||
{
|
||||
if (LUA_RawPushUserdata(L, data) == LPUSHED_NEW)
|
||||
{
|
||||
luaL_getmetatable(L, meta);
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
}
|
||||
|
||||
// Same as LUA_PushUserdata but don't set a metatable yet.
|
||||
lpushed_t LUA_RawPushUserdata(lua_State *L, void *data)
|
||||
{
|
||||
lpushed_t status = LPUSHED_NIL;
|
||||
|
||||
void **userdata;
|
||||
|
||||
if (!data) { // push a NULL
|
||||
lua_pushnil(L);
|
||||
return;
|
||||
return status;
|
||||
}
|
||||
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, LREG_VALID);
|
||||
I_Assert(lua_istable(L, -1));
|
||||
|
||||
lua_pushlightuserdata(L, data);
|
||||
lua_rawget(L, -2);
|
||||
|
||||
if (lua_isnil(L, -1)) { // no userdata? deary me, we'll have to make one.
|
||||
lua_pop(L, 1); // pop the nil
|
||||
|
||||
// create the userdata
|
||||
userdata = lua_newuserdata(L, sizeof(void *));
|
||||
*userdata = data;
|
||||
luaL_getmetatable(L, meta);
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
// Set it in the registry so we can find it again
|
||||
lua_pushlightuserdata(L, data); // k (store the userdata via the data's pointer)
|
||||
|
@ -765,8 +778,15 @@ void LUA_PushUserdata(lua_State *L, void *data, const char *meta)
|
|||
lua_rawset(L, -4);
|
||||
|
||||
// stack is left with the userdata on top, as if getting it had originally succeeded.
|
||||
|
||||
status = LPUSHED_NEW;
|
||||
}
|
||||
else
|
||||
status = LPUSHED_EXISTING;
|
||||
|
||||
lua_remove(L, -2); // remove LREG_VALID
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
// When userdata is freed, use this function to remove it from Lua.
|
||||
|
@ -1681,3 +1701,36 @@ int Lua_optoption(lua_State *L, int narg,
|
|||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void LUA_PushTaggableObjectArray
|
||||
( lua_State *L,
|
||||
const char *field,
|
||||
lua_CFunction iterator,
|
||||
lua_CFunction indexer,
|
||||
lua_CFunction counter,
|
||||
taggroup_t *garray[],
|
||||
size_t * max_elements,
|
||||
void * element_array,
|
||||
size_t sizeof_element,
|
||||
const char *meta)
|
||||
{
|
||||
lua_newuserdata(L, 0);
|
||||
lua_createtable(L, 0, 2);
|
||||
lua_createtable(L, 0, 2);
|
||||
lua_pushcfunction(L, iterator);
|
||||
lua_setfield(L, -2, "iterate");
|
||||
|
||||
LUA_InsertTaggroupIterator(L, garray,
|
||||
max_elements, element_array, sizeof_element, meta);
|
||||
|
||||
lua_createtable(L, 0, 1);
|
||||
lua_pushcfunction(L, indexer);
|
||||
lua_setfield(L, -2, "__index");
|
||||
lua_setmetatable(L, -2);
|
||||
lua_setfield(L, -2, "__index");
|
||||
|
||||
lua_pushcfunction(L, counter);
|
||||
lua_setfield(L, -2, "__len");
|
||||
lua_setmetatable(L, -2);
|
||||
lua_setglobal(L, field);
|
||||
}
|
||||
|
|
|
@ -10,10 +10,14 @@
|
|||
/// \file lua_script.h
|
||||
/// \brief Lua scripting basics
|
||||
|
||||
#ifndef LUA_SCRIPT_H
|
||||
#define LUA_SCRIPT_H
|
||||
|
||||
#include "m_fixed.h"
|
||||
#include "doomtype.h"
|
||||
#include "d_player.h"
|
||||
#include "g_state.h"
|
||||
#include "taglist.h"
|
||||
|
||||
#include "blua/lua.h"
|
||||
#include "blua/lualib.h"
|
||||
|
@ -46,12 +50,6 @@ void LUA_LoadLump(UINT16 wad, UINT16 lump, boolean noresults);
|
|||
void LUA_DumpFile(const char *filename);
|
||||
#endif
|
||||
fixed_t LUA_EvalMath(const char *word);
|
||||
void LUA_PushLightUserdata(lua_State *L, void *data, const char *meta);
|
||||
void LUA_PushUserdata(lua_State *L, void *data, const char *meta);
|
||||
void LUA_InvalidateUserdata(void *data);
|
||||
void LUA_InvalidateLevel(void);
|
||||
void LUA_InvalidateMapthings(void);
|
||||
void LUA_InvalidatePlayer(player_t *player);
|
||||
void LUA_Step(void);
|
||||
void LUA_Archive(void);
|
||||
void LUA_UnArchive(void);
|
||||
|
@ -63,6 +61,42 @@ int Lua_optoption(lua_State *L, int narg,
|
|||
const char *def, const char *const lst[]);
|
||||
void LUAh_NetArchiveHook(lua_CFunction archFunc);
|
||||
|
||||
void LUA_PushTaggableObjectArray
|
||||
( lua_State *L,
|
||||
const char *field,
|
||||
lua_CFunction iterator,
|
||||
lua_CFunction indexer,
|
||||
lua_CFunction counter,
|
||||
taggroup_t *garray[],
|
||||
size_t * max_elements,
|
||||
void * element_array,
|
||||
size_t sizeof_element,
|
||||
const char *meta);
|
||||
|
||||
void LUA_InsertTaggroupIterator
|
||||
( lua_State *L,
|
||||
taggroup_t *garray[],
|
||||
size_t * max_elements,
|
||||
void * element_array,
|
||||
size_t sizeof_element,
|
||||
const char * meta);
|
||||
|
||||
typedef enum {
|
||||
LPUSHED_NIL,
|
||||
LPUSHED_NEW,
|
||||
LPUSHED_EXISTING,
|
||||
} lpushed_t;
|
||||
|
||||
void LUA_PushLightUserdata(lua_State *L, void *data, const char *meta);
|
||||
void LUA_PushUserdata(lua_State *L, void *data, const char *meta);
|
||||
lpushed_t LUA_RawPushUserdata(lua_State *L, void *data);
|
||||
|
||||
void LUA_InvalidateUserdata(void *data);
|
||||
|
||||
void LUA_InvalidateLevel(void);
|
||||
void LUA_InvalidateMapthings(void);
|
||||
void LUA_InvalidatePlayer(player_t *player);
|
||||
|
||||
// Console wrapper
|
||||
void COM_Lua_f(void);
|
||||
|
||||
|
@ -98,3 +132,5 @@ void COM_Lua_f(void);
|
|||
|
||||
#define INLEVEL if (! ISINLEVEL)\
|
||||
return luaL_error(L, "This can only be used in a level!");
|
||||
|
||||
#endif/*LUA_SCRIPT_H*/
|
||||
|
|
|
@ -15,6 +15,11 @@
|
|||
#include "z_zone.h"
|
||||
#include "r_data.h"
|
||||
|
||||
// Bit array of whether a tag exists for sectors/lines/things.
|
||||
bitarray_t tags_available[BIT_ARRAY_SIZE (MAXTAGS)];
|
||||
|
||||
size_t num_tags;
|
||||
|
||||
// Taggroups are used to list elements of the same tag, for iteration.
|
||||
// Since elements can now have multiple tags, it means an element may appear
|
||||
// in several taggroups at the same time. These are built on level load.
|
||||
|
@ -105,6 +110,33 @@ size_t Taggroup_Find (const taggroup_t *group, const size_t id)
|
|||
return -1;
|
||||
}
|
||||
|
||||
/// Iterate thru elements in a global taggroup.
|
||||
INT32 Taggroup_Iterate
|
||||
( taggroup_t *garray[],
|
||||
const size_t max_elements,
|
||||
const mtag_t tag,
|
||||
const size_t p)
|
||||
{
|
||||
const taggroup_t *group;
|
||||
|
||||
if (tag == MTAG_GLOBAL)
|
||||
{
|
||||
if (p < max_elements)
|
||||
return p;
|
||||
return -1;
|
||||
}
|
||||
|
||||
group = garray[(UINT16)tag];
|
||||
|
||||
if (group)
|
||||
{
|
||||
if (p < group->count)
|
||||
return group->elements[p];
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// Add an element to a global taggroup.
|
||||
void Taggroup_Add (taggroup_t *garray[], const mtag_t tag, size_t id)
|
||||
{
|
||||
|
@ -120,6 +152,11 @@ void Taggroup_Add (taggroup_t *garray[], const mtag_t tag, size_t id)
|
|||
if (Taggroup_Find(group, id) != (size_t)-1)
|
||||
return;
|
||||
|
||||
if (! in_bit_array(tags_available, tag))
|
||||
num_tags++;
|
||||
|
||||
set_bit_array(tags_available, tag);
|
||||
|
||||
// Create group if empty.
|
||||
if (!group)
|
||||
{
|
||||
|
@ -161,6 +198,11 @@ void Taggroup_Remove (taggroup_t *garray[], const mtag_t tag, size_t id)
|
|||
if ((rempos = Taggroup_Find(group, id)) == (size_t)-1)
|
||||
return;
|
||||
|
||||
if (in_bit_array(tags_available, tag))
|
||||
num_tags--;
|
||||
|
||||
unset_bit_array(tags_available, tag);
|
||||
|
||||
// Strip away taggroup if no elements left.
|
||||
if (!(newcount = --group->count))
|
||||
{
|
||||
|
@ -209,6 +251,9 @@ void Taglist_InitGlobalTables(void)
|
|||
{
|
||||
size_t i, j;
|
||||
|
||||
memset(tags_available, 0, sizeof tags_available);
|
||||
num_tags = 0;
|
||||
|
||||
for (i = 0; i < MAXTAGS; i++)
|
||||
{
|
||||
tags_sectors[i] = NULL;
|
||||
|
@ -236,56 +281,17 @@ void Taglist_InitGlobalTables(void)
|
|||
|
||||
INT32 Tag_Iterate_Sectors (const mtag_t tag, const size_t p)
|
||||
{
|
||||
if (tag == MTAG_GLOBAL)
|
||||
{
|
||||
if (p < numsectors)
|
||||
return p;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tags_sectors[(UINT16)tag])
|
||||
{
|
||||
if (p < tags_sectors[(UINT16)tag]->count)
|
||||
return tags_sectors[(UINT16)tag]->elements[p];
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
return Taggroup_Iterate(tags_sectors, numsectors, tag, p);
|
||||
}
|
||||
|
||||
INT32 Tag_Iterate_Lines (const mtag_t tag, const size_t p)
|
||||
{
|
||||
if (tag == MTAG_GLOBAL)
|
||||
{
|
||||
if (p < numlines)
|
||||
return p;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tags_lines[(UINT16)tag])
|
||||
{
|
||||
if (p < tags_lines[(UINT16)tag]->count)
|
||||
return tags_lines[(UINT16)tag]->elements[p];
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
return Taggroup_Iterate(tags_lines, numlines, tag, p);
|
||||
}
|
||||
|
||||
INT32 Tag_Iterate_Things (const mtag_t tag, const size_t p)
|
||||
{
|
||||
if (tag == MTAG_GLOBAL)
|
||||
{
|
||||
if (p < nummapthings)
|
||||
return p;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tags_mapthings[(UINT16)tag])
|
||||
{
|
||||
if (p < tags_mapthings[(UINT16)tag]->count)
|
||||
return tags_mapthings[(UINT16)tag]->elements[p];
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
return Taggroup_Iterate(tags_mapthings, nummapthings, tag, p);
|
||||
}
|
||||
|
||||
INT32 Tag_FindLineSpecial(const INT16 special, const mtag_t tag)
|
||||
|
|
|
@ -43,6 +43,10 @@ typedef struct
|
|||
size_t count;
|
||||
} taggroup_t;
|
||||
|
||||
extern bitarray_t tags_available[];
|
||||
|
||||
extern size_t num_tags;
|
||||
|
||||
extern taggroup_t* tags_sectors[];
|
||||
extern taggroup_t* tags_lines[];
|
||||
extern taggroup_t* tags_mapthings[];
|
||||
|
@ -51,6 +55,12 @@ void Taggroup_Add (taggroup_t *garray[], const mtag_t tag, size_t id);
|
|||
void Taggroup_Remove (taggroup_t *garray[], const mtag_t tag, size_t id);
|
||||
size_t Taggroup_Find (const taggroup_t *group, const size_t id);
|
||||
|
||||
INT32 Taggroup_Iterate
|
||||
( taggroup_t *garray[],
|
||||
const size_t max_elements,
|
||||
const mtag_t tag,
|
||||
const size_t p);
|
||||
|
||||
void Taglist_InitGlobalTables(void);
|
||||
|
||||
INT32 Tag_Iterate_Sectors (const mtag_t tag, const size_t p);
|
||||
|
|
Loading…
Reference in a new issue