From 2ecbf89f1a6e4145956e416007a8d22e771aaed3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 4 Feb 2017 23:16:16 +0100 Subject: [PATCH] clean up and test various race.parameters --- src/battle.c | 6 +----- src/battle.test.c | 3 +++ src/kernel/faction.c | 5 +---- src/kernel/faction.test.c | 21 ++++++++++++++++++++- src/kernel/race.c | 9 +++++++++ src/kernel/race.h | 6 ++++++ src/kernel/race.test.c | 1 + src/market.c | 4 ++-- src/market.h | 4 ++++ src/market.test.c | 14 ++++++++++++++ 10 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/battle.c b/src/battle.c index ad613a69f..9316d35dd 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1014,17 +1014,13 @@ static void vampirism(troop at, int damage) #define MAXRACES 128 -static int armor_bonus(const race *rc) { - return get_param_int(rc->parameters, "armor.stamina", -1); -} - int natural_armor(unit * du) { const race *rc = u_race(du); int an; assert(rc); - an = armor_bonus(rc); + an = rc_armor_bonus(rc); if (an > 0) { int sk = effskill(du, SK_STAMINA, 0); return rc->armor + sk / an; diff --git a/src/battle.test.c b/src/battle.test.c index 03535e16e..e8187c772 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -223,10 +223,13 @@ static void test_natural_armor(CuTest * tc) rc = test_create_race("human"); u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, 0)); 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"); + CuAssertIntEquals(tc, 1, rc_armor_bonus(rc)); CuAssertIntEquals(tc, 2, natural_armor(u)); set_param(&rc->parameters, "armor.stamina", "2"); + CuAssertIntEquals(tc, 2, rc_armor_bonus(rc)); CuAssertIntEquals(tc, 1, natural_armor(u)); test_cleanup(); } diff --git a/src/kernel/faction.c b/src/kernel/faction.c index e3cb4508a..1c8ff4cb8 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -753,12 +753,9 @@ int count_migrants(const faction * f) return count_faction(f, COUNT_MIGRANTS); } -#define MIGRANTS_NONE 0 -#define MIGRANTS_LOG10 1 - int count_maxmigrants(const faction * f) { - int formula = f->race->parameters ? get_param_int(f->race->parameters, "migrants.formula", MIGRANTS_NONE) : MIGRANTS_NONE; + int formula = rc_migrants_formula(f->race); if (formula == MIGRANTS_LOG10) { int nsize = count_all(f); diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index b18121e2d..43956df59 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -3,9 +3,10 @@ #include #include #include +#include #include #include -#include +#include #include #include #include @@ -191,9 +192,27 @@ static void test_set_origin_bug(CuTest *tc) { test_cleanup(); } +static void test_max_migrants(CuTest *tc) { + faction *f; + unit *u; + race *rc; + + test_setup(); + rc = test_create_race("human"); + f = test_create_faction(rc); + u = test_create_unit(f, test_create_region(0, 0, 0)); + CuAssertIntEquals(tc, 0, count_maxmigrants(f)); + set_param(&rc->parameters, "migrants.formula", "1"); + CuAssertIntEquals(tc, 0, count_maxmigrants(f)); + scale_number(u, 250); + CuAssertIntEquals(tc, 13, count_maxmigrants(f)); + test_cleanup(); +} + CuSuite *get_faction_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_max_migrants); SUITE_ADD_TEST(suite, test_addfaction); SUITE_ADD_TEST(suite, test_remove_empty_factions); SUITE_ADD_TEST(suite, test_destroyfaction_allies); diff --git a/src/kernel/race.c b/src/kernel/race.c index 59d263108..123d64022 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -290,6 +290,15 @@ double rc_maxaura(const race *rc) { return rc->maxaura / 100.0; } +int rc_armor_bonus(const race *rc) { + return get_param_int(rc->parameters, "armor.stamina", 0); +} + +int rc_migrants_formula(const race *rc) +{ + return rc->parameters ? get_param_int(rc->parameters, "migrants.formula", MIGRANTS_NONE) : MIGRANTS_NONE; +} + 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 82d8cdf69..7c4600ae5 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -186,6 +186,12 @@ extern "C" { double rc_magres(const struct race *rc); double rc_maxaura(const struct race *rc); + int rc_armor_bonus(const struct race *rc); + +#define MIGRANTS_NONE 0 +#define MIGRANTS_LOG10 1 + int rc_migrants_formula(const race *rc); + /* 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?) */ #define RCF_KILLPEASANTS (1<<1) /* a monster that eats peasants */ diff --git a/src/kernel/race.test.c b/src/kernel/race.test.c index eaf497168..b730676a4 100644 --- a/src/kernel/race.test.c +++ b/src/kernel/race.test.c @@ -23,6 +23,7 @@ static void test_rc_defaults(CuTest *tc) { test_setup(); rc = rc_get_or_create("human"); CuAssertStrEquals(tc, "human", rc->_name); + CuAssertIntEquals(tc, 0, rc_armor_bonus(rc)); CuAssertIntEquals(tc, 0, rc->magres); CuAssertDblEquals(tc, 0.0, rc_magres(rc), 0.0); CuAssertIntEquals(tc, 0, rc->healing); diff --git a/src/market.c b/src/market.c index 42819325f..254d3a483 100644 --- a/src/market.c +++ b/src/market.c @@ -66,7 +66,7 @@ attrib_type at_market = { NULL, NULL, NULL, ATF_UNIQUE }; -static int rc_luxury_trade(const struct race *rc) +int rc_luxury_trade(const struct race *rc) { if (rc) { return get_param_int(rc->parameters, "luxury_trade", 1000); @@ -74,7 +74,7 @@ static int rc_luxury_trade(const struct race *rc) return 1000; } -static int rc_herb_trade(const struct race *rc) +int rc_herb_trade(const struct race *rc) { if (rc) { return get_param_int(rc->parameters, "herb_trade", 500); diff --git a/src/market.h b/src/market.h index 74dc8db17..8f1d39eb4 100644 --- a/src/market.h +++ b/src/market.h @@ -19,10 +19,14 @@ without prior permission by the authors of Eressea. extern "C" { #endif struct building; + struct race; bool markets_module(void); void do_markets(void); + int rc_luxury_trade(const struct race *rc); + int rc_herb_trade(const struct race *rc); + #ifdef __cplusplus } #endif diff --git a/src/market.test.c b/src/market.test.c index 6f1d74b88..1e0195a57 100644 --- a/src/market.test.c +++ b/src/market.test.c @@ -80,9 +80,23 @@ static void test_market_curse(CuTest * tc) CuAssertIntEquals(tc, 35, i_get(u->items, ltype)); } +static void test_rc_trade(CuTest *tc) { + race *rc; + test_setup(); + 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"); + CuAssertIntEquals(tc, 100, rc_luxury_trade(rc)); + CuAssertIntEquals(tc, 50, rc_herb_trade(rc)); + test_cleanup(); +} + CuSuite *get_market_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_market_curse); + SUITE_ADD_TEST(suite, test_rc_trade); return suite; }