diff --git a/src/battle.test.c b/src/battle.test.c index e8187c772..946d5fcee 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -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(); diff --git a/src/economy.c b/src/economy.c index 2d9fe6666..dcbc7431e 100644 --- a/src/economy.c +++ b/src/economy.c @@ -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; diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 28ac2e203..30a623e44 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -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; } } diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 1b1a0236b..c01731964 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -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; } diff --git a/src/kernel/race.c b/src/kernel/race.c index 7f6fe15c2..0e9371e39 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -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) { diff --git a/src/kernel/race.h b/src/kernel/race.h index 67eea00f4..b500ed6a1 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -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); diff --git a/src/kernel/race.test.c b/src/kernel/race.test.c index b730676a4..132457cb4 100644 --- a/src/kernel/race.test.c +++ b/src/kernel/race.test.c @@ -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; } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index a0172313a..c07e19809 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -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); } diff --git a/src/market.test.c b/src/market.test.c index 1e0195a57..b4d5f05f5 100644 --- a/src/market.test.c +++ b/src/market.test.c @@ -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();