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
This commit is contained in:
Enno Rehling 2015-11-07 18:14:38 +01:00
parent d780988862
commit 26b6ae4e30
2 changed files with 22 additions and 12 deletions

View File

@ -1022,23 +1022,32 @@ static void vampirism(troop at, int damage)
} }
} }
#define MAXRACES 128
static int natural_armor(unit * du) static int natural_armor(unit * du)
{ {
static int *bonus = 0; static int cookie = -1;
int an = u_race(du)->armor; static int bonus[MAXRACES];
if (bonus == 0) { const race *rc = u_race(du);
assert(num_races > 0); int index, an = rc->armor;
bonus = calloc((size_t)num_races, sizeof(int));
assert(rc);
if (cookie!=global.cookie) {
cookie = global.cookie;
memset(bonus, 0, sizeof(bonus));
} }
if (bonus[u_race(du)->index] == 0) { assert(num_races < MAXRACES);
bonus[u_race(du)->index] = index = rc->index;
get_param_int(u_race(du)->parameters, "armor.stamina", -1); assert(index >= 0 && index < num_races);
if (bonus[u_race(du)->index] == 0) if (bonus[index] == 0) {
bonus[u_race(du)->index] = -1; 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); int sk = effskill(du, SK_STAMINA, 0);
sk /= bonus[u_race(du)->index]; sk /= bonus[index];
an += sk; an += sk;
} }
return an; return an;

View File

@ -146,6 +146,7 @@ void free_races(void) {
free(races); free(races);
races = rc; races = rc;
} }
num_races = 0;
} }
static race *rc_find_i(const char *name) static race *rc_find_i(const char *name)