mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Add capability to read table flag fields from Lua API
This commit is contained in:
parent
57710520dc
commit
2a01050a0c
7 changed files with 83 additions and 3 deletions
|
@ -842,8 +842,45 @@ void push_hit_params(lua_State *L,const HitParams ¶ms)
|
|||
u32 getflagsfield(lua_State *L, int table, const char *fieldname,
|
||||
FlagDesc *flagdesc, u32 *flagmask)
|
||||
{
|
||||
std::string flagstring = getstringfield_default(L, table, fieldname, "");
|
||||
return readFlagString(flagstring, flagdesc, flagmask);
|
||||
u32 flags = 0;
|
||||
|
||||
lua_getfield(L, table, fieldname);
|
||||
|
||||
if (lua_isstring(L, -1)) {
|
||||
std::string flagstr = lua_tostring(L, -1);
|
||||
flags = readFlagString(flagstr, flagdesc, flagmask);
|
||||
} else if (lua_istable(L, -1)) {
|
||||
flags = read_flags_table(L, -1, flagdesc, flagmask);
|
||||
}
|
||||
|
||||
lua_pop(L, 1);
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
u32 read_flags_table(lua_State *L, int table, FlagDesc *flagdesc, u32 *flagmask)
|
||||
{
|
||||
u32 flags = 0, mask = 0;
|
||||
char fnamebuf[64] = "no";
|
||||
|
||||
for (int i = 0; flagdesc[i].name; i++) {
|
||||
bool result;
|
||||
|
||||
if (getboolfield(L, table, flagdesc[i].name, result)) {
|
||||
mask |= flagdesc[i].flag;
|
||||
if (result)
|
||||
flags |= flagdesc[i].flag;
|
||||
}
|
||||
|
||||
strlcpy(fnamebuf + 2, flagdesc[i].name, sizeof(fnamebuf) - 2);
|
||||
if (getboolfield(L, table, fnamebuf, result))
|
||||
mask |= flagdesc[i].flag;
|
||||
}
|
||||
|
||||
if (flagmask)
|
||||
*flagmask = mask;
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
|
|
@ -123,6 +123,9 @@ u32 getflagsfield (lua_State *L, int table,
|
|||
const char *fieldname,
|
||||
FlagDesc *flagdesc, u32 *flagmask);
|
||||
|
||||
u32 read_flags_table (lua_State *L, int table,
|
||||
FlagDesc *flagdesc, u32 *flagmask);
|
||||
|
||||
void push_items (lua_State *L,
|
||||
const std::vector<ItemStack> &items);
|
||||
|
||||
|
|
|
@ -456,7 +456,6 @@ int ModApiMapgen::l_register_ore(lua_State *L)
|
|||
ore->height_max = getintfield_default(L, index, "height_max", 0);
|
||||
ore->flags = getflagsfield(L, index, "flags", flagdesc_ore, NULL);
|
||||
ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.);
|
||||
|
||||
lua_getfield(L, index, "wherein");
|
||||
if (lua_istable(L, -1)) {
|
||||
int i = lua_gettop(L);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue