From fd9583df3d5b72715344fd80fa732c6a9368ce13 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 3 Feb 2017 17:43:40 +0100 Subject: [PATCH] fix healing for elves in a forest. rename _magres back to magres. rc->parameters is not linked to config_changed invalidation. add tests. --- src/battle.test.c | 4 ++-- src/kernel/jsonconf.c | 2 +- src/kernel/jsonconf.test.c | 2 +- src/kernel/race.c | 2 +- src/kernel/race.h | 2 +- src/kernel/race.test.c | 3 ++- src/kernel/unit.c | 22 ++++++++++++++++++++++ src/kernel/unit.h | 1 + src/kernel/unit.test.c | 28 ++++++++++++++++++++++++++++ src/kernel/xmlreader.c | 2 +- src/laws.c | 26 +------------------------- src/magic.test.c | 6 +++--- src/tests.c | 3 +++ 13 files changed, 67 insertions(+), 36 deletions(-) diff --git a/src/battle.test.c b/src/battle.test.c index 155b3ae0c..03535e16e 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -352,12 +352,12 @@ static void test_magic_resistance(CuTest *tc) calculate_armor(dt, 0, 0, &magres); CuAssertDblEquals_Msg(tc, "skill bonus", 0.1, magic_resistance(du), 0.01); CuAssertDblEquals_Msg(tc, "skill reduction", 0.9, magres, 0.01); - rc->_magres = 50; /* percentage, gets added to skill bonus */ + rc->magres = 50; /* percentage, gets added to skill bonus */ calculate_armor(dt, 0, 0, &magres); CuAssertDblEquals_Msg(tc, "race bonus", 0.6, magic_resistance(du), 0.01); CuAssertDblEquals_Msg(tc, "race reduction", 0.4, magres, 0.01); - rc->_magres = 150; /* should not cause negative damage multiplier */ + rc->magres = 150; /* should not cause negative damage multiplier */ CuAssertDblEquals_Msg(tc, "magic resistance is never > 0.9", 0.9, magic_resistance(du), 0.01); calculate_armor(dt, 0, 0, &magres); CuAssertDblEquals_Msg(tc, "damage reduction is never < 0.1", 0.1, magres, 0.01); diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 0cfba4e34..dcc4c9c0c 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -458,7 +458,7 @@ static void json_race(cJSON *json, race *rc) { break; case cJSON_Number: if (strcmp(child->string, "magres") == 0) { - rc->_magres = child->valueint; + rc->magres = child->valueint; } else if (strcmp(child->string, "maxaura") == 0) { rc->maxaura = (float)child->valuedouble; diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 7ac6a93cc..5b0590bf8 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -163,7 +163,7 @@ static void test_races(CuTest * tc) CuAssertPtrNotNull(tc, rc); CuAssertIntEquals(tc, RCF_NPC | RCF_WALK | RCF_UNDEAD, rc->flags); CuAssertStrEquals(tc, "1d4", rc->def_damage); - CuAssertIntEquals(tc, 100, rc->_magres); + CuAssertIntEquals(tc, 100, rc->magres); CuAssertDblEquals(tc, 1.0, rc_magres(rc), 0.0); CuAssertDblEquals(tc, 2.0, rc->maxaura, 0.0); CuAssertDblEquals(tc, 3.0, rc->regaura, 0.0); diff --git a/src/kernel/race.c b/src/kernel/race.c index 3dc708ed1..16eff8695 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -283,7 +283,7 @@ bool r_insectstalled(const region * r) } double rc_magres(const struct race *rc) { - return rc->_magres / 100.0; + return rc->magres / 100.0; } const char* rc_name(const race * rc, name_t n, char *name, size_t size) { diff --git a/src/kernel/race.h b/src/kernel/race.h index 72e1269c9..853e1041f 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -116,7 +116,7 @@ extern "C" { typedef struct race { char *_name; - int _magres; + int magres; float healing; double maxaura; /* Faktor auf Maximale Aura */ double regaura; /* Faktor auf Regeneration */ diff --git a/src/kernel/race.test.c b/src/kernel/race.test.c index d40f252bc..c07808099 100644 --- a/src/kernel/race.test.c +++ b/src/kernel/race.test.c @@ -23,8 +23,9 @@ static void test_rc_defaults(CuTest *tc) { test_setup(); rc = rc_get_or_create("human"); CuAssertStrEquals(tc, "human", rc->_name); - CuAssertIntEquals(tc, 0, rc->_magres); + CuAssertIntEquals(tc, 0, rc->magres); CuAssertDblEquals(tc, 0.0, rc_magres(rc), 0.0); + CuAssertDblEquals(tc, 0.0, rc->healing, 0.0); CuAssertDblEquals(tc, 0.0, rc->maxaura, 0.0); CuAssertDblEquals(tc, 1.0, rc->recruit_multi, 0.0); CuAssertDblEquals(tc, 1.0, rc->regaura, 0.0); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 29766d4ef..354a52a06 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -2047,3 +2047,25 @@ bool has_limited_skills(const struct unit * u) return false; } +double u_heal_factor(const unit * u) +{ + const race * rc = u_race(u); + if (rc->healing>0) { + return rc->healing; + } + if (r_isforest(u->region)) { + static int rc_cache; + static const race *rc_elf; + if (rc_changed(&rc_cache)) { + rc_elf = get_race(RC_ELF); + } + if (rc == rc_elf) { + double elf_regen = 1.0; + if (rc->parameters) { + elf_regen = get_param_flt(rc->parameters, "regen.forest", elf_regen); + } + return elf_regen; + } + } + return 1.0; +} diff --git a/src/kernel/unit.h b/src/kernel/unit.h index e846ca1f4..14096c872 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -190,6 +190,7 @@ extern "C" { bool leave(struct unit *u, bool force); bool can_leave(struct unit *u); + double u_heal_factor(const struct unit * u); void u_set_building(struct unit * u, struct building * b); void u_set_ship(struct unit * u, struct ship * sh); void leave_ship(struct unit * u); diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index b0a2891c5..ad4e1761a 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -483,6 +484,32 @@ static void test_name_unit(CuTest *tc) { test_cleanup(); } +static void test_heal_factor(CuTest *tc) { + unit * u; + region *r; + race *rc; + terrain_type *t_plain; + + test_setup(); + t_plain = test_create_terrain("plain", LAND_REGION|FOREST_REGION); + rc = rc_get_or_create("human"); + u = test_create_unit(test_create_faction(rc), r = test_create_region(0, 0, t_plain)); + rsettrees(r, 1, r->terrain->size / TREESIZE); + rsettrees(r, 2, 0); + CuAssertTrue(tc, r_isforest(r)); + CuAssertDblEquals(tc, 1.0, u_heal_factor(u), 0.0); + rc->healing = 2.0; + CuAssertDblEquals(tc, 2.0, u_heal_factor(u), 0.0); + rc->healing = 0.0; + rc = rc_get_or_create("elf"); + CuAssertPtrEquals(tc, (void *)rc, (void *)get_race(RC_ELF)); + u_setrace(u, get_race(RC_ELF)); + CuAssertDblEquals(tc, 1.0, u_heal_factor(u), 0.0); + set_param(&rc->parameters, "regen.forest", "1.5"); + CuAssertDblEquals(tc, 1.5, u_heal_factor(u), 0.0); + test_cleanup(); +} + CuSuite *get_unit_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -507,5 +534,6 @@ CuSuite *get_unit_suite(void) SUITE_ADD_TEST(suite, test_limited_skills); SUITE_ADD_TEST(suite, test_renumber_unit); SUITE_ADD_TEST(suite, test_name_unit); + SUITE_ADD_TEST(suite, test_heal_factor); return suite; } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 3d7276bf3..87771ba6b 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -1635,7 +1635,7 @@ static int parse_races(xmlDocPtr doc) rc->def_damage = strdup((const char *)propValue); xmlFree(propValue); - rc->_magres = xml_ivalue(node, "magres", rc->_magres); + rc->magres = xml_ivalue(node, "magres", rc->magres); rc->healing = (float)xml_fvalue(node, "healing", rc->healing); rc->maxaura = (float)xml_fvalue(node, "maxaura", rc->maxaura); rc->regaura = (float)xml_fvalue(node, "regaura", rc->regaura); diff --git a/src/laws.c b/src/laws.c index 6b0bfda8d..98ff5d9f6 100644 --- a/src/laws.c +++ b/src/laws.c @@ -3271,30 +3271,6 @@ static int use_item(unit * u, const item_type * itype, int amount, struct order } } -static double heal_factor(const unit * u) -{ - const race * rc = u_race(u); - if (rc->healing>0) { - return rc->healing; - } - if (r_isforest(u->region)) { - static int rc_cache; - static const race *rc_elf; - if (rc_changed(&rc_cache)) { - rc_elf = get_race(RC_ELF); - } - if (rc==rc_elf) { - static int config; - static double elf_regen = 1.0; - if (config_changed(&config)) { - elf_regen = get_param_flt(u_race(u)->parameters, "regen.forest", 1.0F); - } - return elf_regen; - } - } - return 1.0; -} - void monthly_healing(void) { region *r; @@ -3334,7 +3310,7 @@ void monthly_healing(void) continue; } - p *= heal_factor(u); + p *= u_heal_factor(u); if (u->hp < umhp) { double maxheal = MAX(u->number, umhp / 20.0); int addhp; diff --git a/src/magic.test.c b/src/magic.test.c index 29534b33b..b32211fd4 100644 --- a/src/magic.test.c +++ b/src/magic.test.c @@ -413,11 +413,11 @@ static void test_magic_resistance(CuTest *tc) { test_setup(); rc = test_create_race("human"); u = test_create_unit(test_create_faction(rc), test_create_region(0, 0, 0)); - CuAssertDblEquals(tc, rc->_magres/100.0, magic_resistance(u), 0.01); - rc->_magres = 100; + CuAssertDblEquals(tc, rc->magres/100.0, magic_resistance(u), 0.01); + rc->magres = 100; CuAssertDblEquals_Msg(tc, "magic resistance is capped at 0.9", 0.9, magic_resistance(u), 0.01); rc = test_create_race("braineater"); - rc->_magres = 100; + rc->magres = 100; u_setrace(u, rc); CuAssertDblEquals_Msg(tc, "brain eaters outside astral space have 50% magres", 0.5, magic_resistance(u), 0.01); u->region->_plane = get_astralplane(); diff --git a/src/tests.c b/src/tests.c index eb2631afe..5c9ce821d 100644 --- a/src/tests.c +++ b/src/tests.c @@ -217,6 +217,9 @@ terrain_type * test_create_terrain(const char * name, unsigned int flags) { terrain_type * t = get_or_create_terrain(name); + if (flags & LAND_REGION) { + t->size = 1000; + } t->flags = flags; return t; }