From 26b6ae4e3023db3b29b66d538eef36e562c940ed Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 7 Nov 2015 18:14:38 +0100 Subject: [PATCH] github issue #374 stick some assertions in natural_armor refactor it for readability eliminate a static memory leak reset num_races to zero at end of test --- src/battle.c | 33 +++++++++++++++++++++------------ src/kernel/race.c | 1 + 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/battle.c b/src/battle.c index 97afb3de4..f57e7b377 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1022,23 +1022,32 @@ static void vampirism(troop at, int damage) } } +#define MAXRACES 128 + static int natural_armor(unit * du) { - static int *bonus = 0; - int an = u_race(du)->armor; - if (bonus == 0) { - assert(num_races > 0); - bonus = calloc((size_t)num_races, sizeof(int)); + static int cookie = -1; + static int bonus[MAXRACES]; + const race *rc = u_race(du); + int index, an = rc->armor; + + assert(rc); + if (cookie!=global.cookie) { + cookie = global.cookie; + memset(bonus, 0, sizeof(bonus)); } - if (bonus[u_race(du)->index] == 0) { - bonus[u_race(du)->index] = - get_param_int(u_race(du)->parameters, "armor.stamina", -1); - if (bonus[u_race(du)->index] == 0) - bonus[u_race(du)->index] = -1; + 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[u_race(du)->index] > 0) { + if (bonus[index] > 0) { int sk = effskill(du, SK_STAMINA, 0); - sk /= bonus[u_race(du)->index]; + sk /= bonus[index]; an += sk; } return an; diff --git a/src/kernel/race.c b/src/kernel/race.c index 191c4f27d..7d881fe7f 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -146,6 +146,7 @@ void free_races(void) { free(races); races = rc; } + num_races = 0; } static race *rc_find_i(const char *name)