extract rc_mask, add it to exparse code.

This commit is contained in:
Enno Rehling 2018-04-28 16:14:32 +02:00
parent 3cb1d1a071
commit ac786e034c
4 changed files with 35 additions and 12 deletions

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

@ -656,23 +656,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;