extract rc_mask, add it to exparse code.

This commit is contained in:
Enno Rehling 2018-04-28 16:14:32 +02:00
parent edadf2cbab
commit 03cff6d595
5 changed files with 49 additions and 12 deletions

View File

@ -5,6 +5,7 @@
#include "kernel/build.h"
#include "kernel/item.h"
#include "kernel/race.h"
#include "kernel/resources.h"
#include "util/log.h"
@ -156,6 +157,7 @@ static void handle_item(userdata *ud, const XML_Char *el, const XML_Char **attr)
itype = it_get_or_create(rtype);
}
for (i = 0; attr[i]; i += 2) {
char buffer[64];
if (xml_strcmp(attr[i], "weight") == 0) {
itype->weight = xml_int(attr[i + 1]);
}
@ -165,6 +167,18 @@ static void handle_item(userdata *ud, const XML_Char *el, const XML_Char **attr)
else if (xml_strcmp(attr[i], "score") == 0) {
itype->score = xml_int(attr[i + 1]);
}
else if (xml_strcmp(attr[i], "allow") == 0) {
size_t len = strlen(attr[i + 1]);
assert(len < sizeof(buffer));
memcpy(buffer, attr[i + 1], len + 1);
itype->mask_allow = rc_mask(buffer);
}
else if (xml_strcmp(attr[i], "deny") == 0) {
size_t len = strlen(attr[i + 1]);
assert(len < sizeof(buffer));
memcpy(buffer, attr[i + 1], len + 1);
itype->mask_deny = rc_mask(buffer);
}
else if (!handle_flag(&flags, attr + i, flag_names)) {
handle_bad_input(ud, el, attr[i]);
}

View File

@ -574,3 +574,20 @@ struct race * read_race_reference(struct storage *store)
void register_race_function(race_func func, const char *name) {
register_function((pf_generic)func, name);
}
static int race_mask = 1;
int rc_mask(char *list) {
int mask = 0;
char * tok = strtok(list, " ,");
while (tok) {
race * rc = rc_get_or_create(tok);
if (!rc->mask_item) {
rc->mask_item = race_mask;
race_mask = race_mask << 1;
}
mask |= rc->mask_item;
tok = strtok(NULL, " ,");
}
return mask;
}

View File

@ -197,6 +197,8 @@ extern "C" {
#define MIGRANTS_NONE 0
#define MIGRANTS_LOG10 1
int rc_migrants_formula(const race *rc);
int rc_mask(char *list);
/* Flags. Do not reorder these without changing json_race() in jsonconf.c */
#define RCF_NPC (1<<0) /* cannot be the race for a player faction (and other limits?) */

View File

@ -11,6 +11,7 @@
#include <CuTest.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
@ -172,6 +173,19 @@ static void test_racename(CuTest *tc) {
test_teardown();
}
static void test_rc_mask(CuTest *tc) {
int mask;
char list[64];
test_setup();
strcpy(list, "goblin dwarf");
mask = rc_mask(list);
CuAssertIntEquals(tc, 3, mask);
CuAssertStrEquals(tc, "goblin", list);
mask = rc_mask(list);
CuAssertIntEquals(tc, 1, mask);
test_teardown();
}
CuSuite *get_race_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -180,6 +194,7 @@ CuSuite *get_race_suite(void)
SUITE_ADD_TEST(suite, test_rc_name);
SUITE_ADD_TEST(suite, test_rc_defaults);
SUITE_ADD_TEST(suite, test_rc_find);
SUITE_ADD_TEST(suite, test_rc_mask);
SUITE_ADD_TEST(suite, test_rc_set_param);
SUITE_ADD_TEST(suite, test_rc_can_use);
SUITE_ADD_TEST(suite, test_racename);

View File

@ -667,23 +667,12 @@ static weapon_type *xml_readweapon(xmlXPathContextPtr xpath, item_type * itype)
return wtype;
}
static int race_mask = 1;
static void mask_races(xmlNodePtr node, const char *key, int *maskp) {
xmlChar *propValue = xmlGetProp(node, BAD_CAST key);
int mask = 0;
assert(maskp);
if (propValue) {
char * tok = strtok((char *)propValue, " ,");
while (tok) {
race * rc = rc_get_or_create(tok);
if (!rc->mask_item) {
rc->mask_item = race_mask;
race_mask = race_mask << 1;
}
mask |= rc->mask_item;
tok = strtok(NULL, " ,");
}
mask = rc_mask((char *)propValue);
xmlFree(propValue);
}
*maskp = mask;