channel XML race.parameters through a single, tested, function

This commit is contained in:
Enno Rehling 2017-02-05 16:55:51 +01:00
parent 0b3d15f4b3
commit bd836b76e1
9 changed files with 64 additions and 23 deletions

View File

@ -225,10 +225,10 @@ static void test_natural_armor(CuTest * tc)
set_level(u, SK_STAMINA, 2);
CuAssertIntEquals(tc, 0, rc_armor_bonus(rc));
CuAssertIntEquals(tc, 0, natural_armor(u));
set_param(&rc->parameters, "armor.stamina", "1");
rc_set_param(rc, "armor.stamina", "1");
CuAssertIntEquals(tc, 1, rc_armor_bonus(rc));
CuAssertIntEquals(tc, 2, natural_armor(u));
set_param(&rc->parameters, "armor.stamina", "2");
rc_set_param(rc, "armor.stamina", "2");
CuAssertIntEquals(tc, 2, rc_armor_bonus(rc));
CuAssertIntEquals(tc, 1, natural_armor(u));
test_cleanup();

View File

@ -422,10 +422,7 @@ static void expandrecruit(region * r, request * recruitorders)
static int recruit_cost(const faction * f, const race * rc)
{
if (is_monsters(f) || f->race == rc) {
return rc->recruitcost;
}
else if (valid_race(f, rc)) {
if (is_monsters(f) || valid_race(f, rc)) {
return rc->recruitcost;
}
return -1;

View File

@ -558,15 +558,20 @@ void faction_setpassword(faction * f, const char *pwhash)
f->_password = strdup(pwhash);
}
const race *other_race(const race *rc) {
if (rc->parameters) {
const char *str = get_param(rc->parameters, "other_race");
return str ? rc_find(str) : NULL;
}
return NULL;
}
bool valid_race(const struct faction *f, const struct race *rc)
{
if (f->race == rc)
return true;
else {
const char *str = get_param(f->race->parameters, "other_race");
if (str)
return rc_find(str) == rc;
return false;
return other_race(f->race) == rc;
}
}

View File

@ -209,6 +209,22 @@ static void test_max_migrants(CuTest *tc) {
test_cleanup();
}
static void test_valid_race(CuTest *tc) {
race * rc1, *rc2;
faction *f;
test_setup();
rc1 = test_create_race("human");
rc2 = test_create_race("elf");
f = test_create_faction(rc1);
CuAssertTrue(tc, valid_race(f, rc1));
CuAssertTrue(tc, !valid_race(f, rc2));
rc_set_param(rc1, "other_race", "elf");
CuAssertTrue(tc, valid_race(f, rc1));
CuAssertTrue(tc, valid_race(f, rc2));
test_cleanup();
}
CuSuite *get_faction_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -222,5 +238,6 @@ CuSuite *get_faction_suite(void)
SUITE_ADD_TEST(suite, test_set_origin);
SUITE_ADD_TEST(suite, test_set_origin_bug);
SUITE_ADD_TEST(suite, test_check_passwd);
SUITE_ADD_TEST(suite, test_valid_race);
return suite;
}

View File

@ -299,6 +299,20 @@ int rc_migrants_formula(const race *rc)
return (rc->flags&RCF_MIGRANTS) ? MIGRANTS_LOG10 : MIGRANTS_NONE;
}
void rc_set_param(struct race *rc, const char *key, const char *value) {
if (strcmp(key, "recruit_multi") == 0) {
rc->recruit_multi = atof(value);
}
else if (strcmp(key, "migrants.formula") == 0) {
if (value[0] == '1') {
rc->flags |= RCF_MIGRANTS;
}
}
else {
set_param(&rc->parameters, key, value);
}
}
const char* rc_name(const race * rc, name_t n, char *name, size_t size) {
const char * postfix = 0;
if (!rc) {

View File

@ -184,6 +184,8 @@ extern "C" {
const char * rc_name_s(const race *rc, name_t n);
const char * rc_name(const race *rc, name_t n, char *name, size_t size);
void rc_set_param(struct race *rc, const char *key, const char *value);
double rc_magres(const struct race *rc);
double rc_maxaura(const struct race *rc);
int rc_armor_bonus(const struct race *rc);

View File

@ -84,6 +84,21 @@ static void test_old_race(CuTest *tc)
test_cleanup();
}
static void test_rc_set_param(CuTest *tc) {
race *rc;
test_setup();
rc = test_create_race("human");
CuAssertPtrEquals(tc, NULL, rc->parameters);
rc_set_param(rc, "hodor", "HODOR");
CuAssertStrEquals(tc, "HODOR", get_param(rc->parameters, "hodor"));
rc_set_param(rc, "recruit_multi", "0.5");
CuAssertDblEquals(tc, 0.5, rc->recruit_multi, 0.0);
rc_set_param(rc, "migrants.formula", "1");
CuAssertIntEquals(tc, RCF_MIGRANTS, rc->flags&RCF_MIGRANTS);
CuAssertIntEquals(tc, MIGRANTS_LOG10, rc_migrants_formula(rc));
test_cleanup();
}
CuSuite *get_race_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -92,6 +107,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_set_param);
return suite;
}

View File

@ -1734,17 +1734,7 @@ static int parse_races(xmlDocPtr doc)
else if (strcmp((const char *)child->name, "param") == 0) {
xmlChar *propName = xmlGetProp(child, BAD_CAST "name");
xmlChar *propValue = xmlGetProp(child, BAD_CAST "value");
if (strcmp((const char *)propName, "recruit_multi")==0) {
rc->recruit_multi = atof((const char *)propValue);
}
else if (strcmp((const char *)propName, "migrants.formula") == 0) {
if (propValue[0] == '1') {
rc->flags |= RCF_MIGRANTS;
}
}
else {
set_param(&rc->parameters, (const char *)propName, (const char *)propValue);
}
rc_set_param(rc, (const char *)propName, (const char *)propValue);
xmlFree(propName);
xmlFree(propValue);
}

View File

@ -86,8 +86,8 @@ static void test_rc_trade(CuTest *tc) {
rc = test_create_race("human");
CuAssertIntEquals(tc, 1000, rc_luxury_trade(rc));
CuAssertIntEquals(tc, 500, rc_herb_trade(rc));
set_param(&rc->parameters, "luxury_trade", "100");
set_param(&rc->parameters, "herb_trade", "50");
rc_set_param(rc, "luxury_trade", "100");
rc_set_param(rc, "herb_trade", "50");
CuAssertIntEquals(tc, 100, rc_luxury_trade(rc));
CuAssertIntEquals(tc, 50, rc_herb_trade(rc));
test_cleanup();