Skip to content

Commit 2a01050

Browse files
committedFeb 9, 2014
Add capability to read table flag fields from Lua API
1 parent 5771052 commit 2a01050

File tree

7 files changed

+83
-3
lines changed

7 files changed

+83
-3
lines changed
 

‎doc/lua_api.txt

+22
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ All default ores are of the uniformly-distributed scatter type.
410410

411411
Ore attributes
412412
-------------------
413+
See section Flag Specifier Format.
413414
Currently supported flags: absheight
414415
- absheight
415416
Also produce this same ore between the height range of -height_max and -height_min.
@@ -451,6 +452,7 @@ Important note: Node aliases cannot be used for a raw schematic provided when re
451452

452453
Schematic attributes
453454
---------------------
455+
See section Flag Specifier Format.
454456
Currently supported flags: place_center_x, place_center_y, place_center_z
455457
- place_center_x
456458
Placement of this decoration is centered along the X axis.
@@ -525,6 +527,26 @@ pointed_thing:
525527
{type="node", under=pos, above=pos}
526528
{type="object", ref=ObjectRef}
527529

530+
Flag Specifier Format
531+
-----------------------
532+
Flags using the standardized flag specifier format can be specified in either of two ways, by string or table.
533+
The string format is a comma-delimited set of flag names; whitespace and unrecognized flag fields are ignored.
534+
Specifying a flag in the string sets the flag, and specifying a flag prefixed by the string "no" explicitly
535+
clears the flag from whatever the default may be.
536+
In addition to the standard string flag format, the schematic flags field can also be a table of flag names
537+
to boolean values representing whether or not the flag is set. Additionally, if a field with the flag name
538+
prefixed with "no" is present, mapped to a boolean of any value, the specified flag is unset.
539+
540+
e.g. A flag field of value
541+
{place_center_x = true, place_center_y=false, place_center_z=true}
542+
is equivalent to
543+
{place_center_x = true, noplace_center_y=true, place_center_z=true}
544+
which is equivalent to
545+
"place_center_x, noplace_center_y, place_center_z"
546+
or even
547+
"place_center_x, place_center_z"
548+
since, by default, no schematic attributes are set.
549+
528550
Items
529551
------
530552
Node (register_node):

‎src/porting.h

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
8888
#define strtoull(x, y, z) _strtoui64(x, y, z)
8989
#define strcasecmp(x, y) stricmp(x, y)
9090
#define strncasecmp(x, y, n) strnicmp(x, y, n)
91+
92+
// We can't simply alias strlcpy() to MSVC's strcpy_s(), since strcpy_s
93+
// by default raises an assertion error and aborts the program if the
94+
// buffer is too small. So we need to define our own.
95+
#define strlcpy(x, y, n) mystrlcpy(x, y, n)
9196
#else
9297
#define ALIGNOF(x) __alignof__(x)
9398
#endif

‎src/script/common/c_content.cpp

+39-2
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,45 @@ void push_hit_params(lua_State *L,const HitParams &params)
842842
u32 getflagsfield(lua_State *L, int table, const char *fieldname,
843843
FlagDesc *flagdesc, u32 *flagmask)
844844
{
845-
std::string flagstring = getstringfield_default(L, table, fieldname, "");
846-
return readFlagString(flagstring, flagdesc, flagmask);
845+
u32 flags = 0;
846+
847+
lua_getfield(L, table, fieldname);
848+
849+
if (lua_isstring(L, -1)) {
850+
std::string flagstr = lua_tostring(L, -1);
851+
flags = readFlagString(flagstr, flagdesc, flagmask);
852+
} else if (lua_istable(L, -1)) {
853+
flags = read_flags_table(L, -1, flagdesc, flagmask);
854+
}
855+
856+
lua_pop(L, 1);
857+
858+
return flags;
859+
}
860+
861+
u32 read_flags_table(lua_State *L, int table, FlagDesc *flagdesc, u32 *flagmask)
862+
{
863+
u32 flags = 0, mask = 0;
864+
char fnamebuf[64] = "no";
865+
866+
for (int i = 0; flagdesc[i].name; i++) {
867+
bool result;
868+
869+
if (getboolfield(L, table, flagdesc[i].name, result)) {
870+
mask |= flagdesc[i].flag;
871+
if (result)
872+
flags |= flagdesc[i].flag;
873+
}
874+
875+
strlcpy(fnamebuf + 2, flagdesc[i].name, sizeof(fnamebuf) - 2);
876+
if (getboolfield(L, table, fnamebuf, result))
877+
mask |= flagdesc[i].flag;
878+
}
879+
880+
if (flagmask)
881+
*flagmask = mask;
882+
883+
return flags;
847884
}
848885

849886
/******************************************************************************/

‎src/script/common/c_content.h

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ u32 getflagsfield (lua_State *L, int table,
123123
const char *fieldname,
124124
FlagDesc *flagdesc, u32 *flagmask);
125125

126+
u32 read_flags_table (lua_State *L, int table,
127+
FlagDesc *flagdesc, u32 *flagmask);
128+
126129
void push_items (lua_State *L,
127130
const std::vector<ItemStack> &items);
128131

‎src/script/lua_api/l_mapgen.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ int ModApiMapgen::l_register_ore(lua_State *L)
456456
ore->height_max = getintfield_default(L, index, "height_max", 0);
457457
ore->flags = getflagsfield(L, index, "flags", flagdesc_ore, NULL);
458458
ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.);
459-
460459
lua_getfield(L, index, "wherein");
461460
if (lua_istable(L, -1)) {
462461
int i = lua_gettop(L);

‎src/util/string.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ std::string writeFlagString(u32 flags, FlagDesc *flagdesc, u32 flagmask)
163163
return result;
164164
}
165165

166+
size_t mystrlcpy(char *dst, const char *src, size_t size)
167+
{
168+
size_t srclen = strlen(src) + 1;
169+
size_t copylen = MYMIN(srclen, size);
170+
171+
if (copylen > 0) {
172+
memcpy(dst, src, copylen);
173+
dst[copylen - 1] = '\0';
174+
}
175+
176+
return srclen;
177+
}
178+
166179
char *mystrtok_r(char *s, const char *sep, char **lasts)
167180
{
168181
char *t;

‎src/util/string.h

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ std::string urlencode(std::string str);
321321
std::string urldecode(std::string str);
322322
u32 readFlagString(std::string str, FlagDesc *flagdesc, u32 *flagmask);
323323
std::string writeFlagString(u32 flags, FlagDesc *flagdesc, u32 flagmask);
324+
size_t mystrlcpy(char *dst, const char *src, size_t size);
324325
char *mystrtok_r(char *s, const char *sep, char **lasts);
325326
u64 read_seed(const char *str);
326327

0 commit comments

Comments
 (0)
Please sign in to comment.