From 7dae5aa035194cb0f6ea482ed1f8d881a094c6f6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 21 Nov 2015 19:02:14 +0100 Subject: [PATCH] eliminate silly caching logic from natural armor calculation --- src/battle.c | 28 +++++++++------------------- src/battle.h | 1 + src/battle.test.c | 21 ++++++++++++++++++++- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/battle.c b/src/battle.c index f57e7b377..a897a300e 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1024,30 +1024,20 @@ static void vampirism(troop at, int damage) #define MAXRACES 128 -static int natural_armor(unit * du) +static int armor_bonus(const race *rc) { + return get_param_int(rc->parameters, "armor.stamina", -1); +} + +int natural_armor(unit * du) { - static int cookie = -1; - static int bonus[MAXRACES]; const race *rc = u_race(du); - int index, an = rc->armor; + int bonus, an = rc->armor; assert(rc); - if (cookie!=global.cookie) { - cookie = global.cookie; - memset(bonus, 0, sizeof(bonus)); - } - assert(num_races < MAXRACES); - index = rc->index; - assert(index >= 0 && index < num_races); - if (bonus[index] == 0) { - bonus[index] = - get_param_int(rc->parameters, "armor.stamina", -1); - if (bonus[index] == 0) - bonus[index] = -1; - } - if (bonus[index] > 0) { + bonus = armor_bonus(rc); + if (bonus > 0) { int sk = effskill(du, SK_STAMINA, 0); - sk /= bonus[index]; + sk /= bonus; an += sk; } return an; diff --git a/src/battle.h b/src/battle.h index 9b7b8657d..0e4fceffe 100644 --- a/src/battle.h +++ b/src/battle.h @@ -243,6 +243,7 @@ extern "C" { int count_enemies(struct battle *b, const struct fighter *af, int minrow, int maxrow, int select); + int natural_armor(struct unit * u); int calculate_armor(troop dt, const struct weapon_type *dwtype, const struct weapon_type *awtype, double *magres); bool terminate(troop dt, troop at, int type, const char *damage, bool missile); diff --git a/src/battle.test.c b/src/battle.test.c index 02213d35b..4af0575c7 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -3,6 +3,7 @@ #include "battle.h" #include "skill.h" +#include #include #include #include @@ -201,13 +202,30 @@ static void test_building_defence_bonus(CuTest * tc) test_cleanup(); } -fighter *setup_fighter(battle **bp, unit *u) { +static fighter *setup_fighter(battle **bp, unit *u) { battle *b; *bp = b = make_battle(u->region); return make_fighter(b, u, make_side(b, u->faction, 0, 0, 0), false); } +static void test_natural_armor(CuTest * tc) +{ + race *rc; + unit *u; + + test_cleanup(); + 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, natural_armor(u)); + set_param(&rc->parameters, "armor.stamina", "1"); + CuAssertIntEquals(tc, 2, natural_armor(u)); + set_param(&rc->parameters, "armor.stamina", "2"); + CuAssertIntEquals(tc, 1, natural_armor(u)); + test_cleanup(); +} + static void test_calculate_armor(CuTest * tc) { troop dt; @@ -321,6 +339,7 @@ CuSuite *get_battle_suite(void) SUITE_ADD_TEST(suite, test_building_bonus_respects_size); SUITE_ADD_TEST(suite, test_building_defence_bonus); SUITE_ADD_TEST(suite, test_calculate_armor); + SUITE_ADD_TEST(suite, test_natural_armor); SUITE_ADD_TEST(suite, test_projectile_armor); return suite; }