clean up and test various race.parameters

This commit is contained in:
Enno Rehling 2017-02-04 23:16:16 +01:00
parent 2be1868ff0
commit 2ecbf89f1a
10 changed files with 61 additions and 12 deletions

View File

@ -1014,17 +1014,13 @@ static void vampirism(troop at, int damage)
#define MAXRACES 128 #define MAXRACES 128
static int armor_bonus(const race *rc) {
return get_param_int(rc->parameters, "armor.stamina", -1);
}
int natural_armor(unit * du) int natural_armor(unit * du)
{ {
const race *rc = u_race(du); const race *rc = u_race(du);
int an; int an;
assert(rc); assert(rc);
an = armor_bonus(rc); an = rc_armor_bonus(rc);
if (an > 0) { if (an > 0) {
int sk = effskill(du, SK_STAMINA, 0); int sk = effskill(du, SK_STAMINA, 0);
return rc->armor + sk / an; return rc->armor + sk / an;

View File

@ -223,10 +223,13 @@ static void test_natural_armor(CuTest * tc)
rc = test_create_race("human"); rc = test_create_race("human");
u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, 0)); u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, 0));
set_level(u, SK_STAMINA, 2); set_level(u, SK_STAMINA, 2);
CuAssertIntEquals(tc, 0, rc_armor_bonus(rc));
CuAssertIntEquals(tc, 0, natural_armor(u)); CuAssertIntEquals(tc, 0, natural_armor(u));
set_param(&rc->parameters, "armor.stamina", "1"); set_param(&rc->parameters, "armor.stamina", "1");
CuAssertIntEquals(tc, 1, rc_armor_bonus(rc));
CuAssertIntEquals(tc, 2, natural_armor(u)); CuAssertIntEquals(tc, 2, natural_armor(u));
set_param(&rc->parameters, "armor.stamina", "2"); set_param(&rc->parameters, "armor.stamina", "2");
CuAssertIntEquals(tc, 2, rc_armor_bonus(rc));
CuAssertIntEquals(tc, 1, natural_armor(u)); CuAssertIntEquals(tc, 1, natural_armor(u));
test_cleanup(); test_cleanup();
} }

View File

@ -753,12 +753,9 @@ int count_migrants(const faction * f)
return count_faction(f, COUNT_MIGRANTS); return count_faction(f, COUNT_MIGRANTS);
} }
#define MIGRANTS_NONE 0
#define MIGRANTS_LOG10 1
int count_maxmigrants(const faction * f) 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) { if (formula == MIGRANTS_LOG10) {
int nsize = count_all(f); int nsize = count_all(f);

View File

@ -3,9 +3,10 @@
#include <kernel/ally.h> #include <kernel/ally.h>
#include <kernel/alliance.h> #include <kernel/alliance.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/plane.h>
#include <kernel/race.h> #include <kernel/race.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/plane.h> #include <kernel/unit.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <util/language.h> #include <util/language.h>
#include <util/password.h> #include <util/password.h>
@ -191,9 +192,27 @@ static void test_set_origin_bug(CuTest *tc) {
test_cleanup(); 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 *get_faction_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_max_migrants);
SUITE_ADD_TEST(suite, test_addfaction); SUITE_ADD_TEST(suite, test_addfaction);
SUITE_ADD_TEST(suite, test_remove_empty_factions); SUITE_ADD_TEST(suite, test_remove_empty_factions);
SUITE_ADD_TEST(suite, test_destroyfaction_allies); SUITE_ADD_TEST(suite, test_destroyfaction_allies);

View File

@ -290,6 +290,15 @@ double rc_maxaura(const race *rc) {
return rc->maxaura / 100.0; 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* rc_name(const race * rc, name_t n, char *name, size_t size) {
const char * postfix = 0; const char * postfix = 0;
if (!rc) { if (!rc) {

View File

@ -186,6 +186,12 @@ extern "C" {
double rc_magres(const struct race *rc); double rc_magres(const struct race *rc);
double rc_maxaura(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 */ /* 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_NPC (1<<0) /* cannot be the race for a player faction (and other limits?) */
#define RCF_KILLPEASANTS (1<<1) /* a monster that eats peasants */ #define RCF_KILLPEASANTS (1<<1) /* a monster that eats peasants */

View File

@ -23,6 +23,7 @@ static void test_rc_defaults(CuTest *tc) {
test_setup(); test_setup();
rc = rc_get_or_create("human"); rc = rc_get_or_create("human");
CuAssertStrEquals(tc, "human", rc->_name); CuAssertStrEquals(tc, "human", rc->_name);
CuAssertIntEquals(tc, 0, rc_armor_bonus(rc));
CuAssertIntEquals(tc, 0, rc->magres); CuAssertIntEquals(tc, 0, rc->magres);
CuAssertDblEquals(tc, 0.0, rc_magres(rc), 0.0); CuAssertDblEquals(tc, 0.0, rc_magres(rc), 0.0);
CuAssertIntEquals(tc, 0, rc->healing); CuAssertIntEquals(tc, 0, rc->healing);

View File

@ -66,7 +66,7 @@ attrib_type at_market = {
NULL, NULL, NULL, ATF_UNIQUE NULL, NULL, NULL, ATF_UNIQUE
}; };
static int rc_luxury_trade(const struct race *rc) int rc_luxury_trade(const struct race *rc)
{ {
if (rc) { if (rc) {
return get_param_int(rc->parameters, "luxury_trade", 1000); return get_param_int(rc->parameters, "luxury_trade", 1000);
@ -74,7 +74,7 @@ static int rc_luxury_trade(const struct race *rc)
return 1000; return 1000;
} }
static int rc_herb_trade(const struct race *rc) int rc_herb_trade(const struct race *rc)
{ {
if (rc) { if (rc) {
return get_param_int(rc->parameters, "herb_trade", 500); return get_param_int(rc->parameters, "herb_trade", 500);

View File

@ -19,10 +19,14 @@ without prior permission by the authors of Eressea.
extern "C" { extern "C" {
#endif #endif
struct building; struct building;
struct race;
bool markets_module(void); bool markets_module(void);
void do_markets(void); void do_markets(void);
int rc_luxury_trade(const struct race *rc);
int rc_herb_trade(const struct race *rc);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -80,9 +80,23 @@ static void test_market_curse(CuTest * tc)
CuAssertIntEquals(tc, 35, i_get(u->items, ltype)); 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 *get_market_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_market_curse); SUITE_ADD_TEST(suite, test_market_curse);
SUITE_ADD_TEST(suite, test_rc_trade);
return suite; return suite;
} }