From e7e0ef994baaae1414537fd55d8cdbd734994bd1 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 4 Mar 2007 21:30:39 +0000 Subject: [PATCH] DEBUG_FAST macro and extra code to verify the FASTCOUNT code. --- src/common/kernel/battle.c | 55 +++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/src/common/kernel/battle.c b/src/common/kernel/battle.c index a63fcdcb6..1b6063cce 100644 --- a/src/common/kernel/battle.c +++ b/src/common/kernel/battle.c @@ -84,6 +84,8 @@ static FILE *bdebug; # define DAMAGE_QUOTIENT 1 /* damage += skilldiff/DAMAGE_QUOTIENT */ #endif +#define DEBUG_FAST /* should be disabled when I'm sure it works */ + typedef enum combatmagic { DO_PRECOMBATSPELL, DO_POSTCOMBATSPELL @@ -487,7 +489,7 @@ get_unitrow(const fighter * af, const side * vs) b->rowcache.result = get_row(af->side, row, vs); return b->rowcache.result; } -#if 1 /* validation code */ +#ifdef DEBUG_FAST /* validation code */ { int i = get_row(af->side, row, vs); assert(i==b->rowcache.result); @@ -1262,20 +1264,45 @@ count_allies(const side * as, int minrow, int maxrow, int select) return count; } +static int +count_enemies_i(battle * b, const fighter * af, int minrow, int maxrow, int select) +{ + side *es, *as = af->side; + int i = 0; + + for (es = b->sides; es; es = es->next) { + if (as==NULL || enemy(es, as)) { + int offset = 0; + if (select&SELECT_DISTANCE) { + offset = get_unitrow(af, es) - FIGHT_ROW; + } + i += count_side(es, as, minrow-offset, maxrow-offset, select); + if (i>0 && (select&SELECT_FIND)) break; + } + } + return i; +} + int count_enemies(battle * b, const fighter * af, int minrow, int maxrow, int select) { - int i = 0; - side *es, *as = af->side; - #ifdef FASTCOUNT int sr = statusrow(af->status); + side *as = af->side; if (b->alive==b->fast.alive && as==b->fast.side && sr==b->fast.status && minrow==b->fast.minrow && maxrow==b->fast.maxrow) { if (b->fast.enemies[select]>=0) { +#ifdef DEBUG_FAST + int i = count_enemies_i(b, af, minrow, maxrow, select); + assert(i==b->fast.enemies[select]); +#endif return b->fast.enemies[select]; } else if (select&SELECT_FIND) { if (b->fast.enemies[select-SELECT_FIND]>=0) { +#ifdef DEBUG_FAST + int i = count_enemies_i(b, af, minrow, maxrow, select); + assert((i>0)==(b->fast.enemies[select-SELECT_FIND]>0)); +#endif return b->fast.enemies[select-SELECT_FIND]; } } @@ -1287,23 +1314,15 @@ count_enemies(battle * b, const fighter * af, int minrow, int maxrow, int select b->fast.maxrow = maxrow; memset(b->fast.enemies, -1, sizeof(b->fast.enemies)); } - #endif - if (maxrowsides; es; es = es->next) { - if (as==NULL || enemy(es, as)) { - int offset = 0; - if (select&SELECT_DISTANCE) { - offset = get_unitrow(af, es) - FIGHT_ROW; - } - i += count_side(es, as, minrow-offset, maxrow-offset, select); - if (i>0 && (select&SELECT_FIND)) break; - } - } + if (maxrow>=FIRST_ROW) { + int i = count_enemies_i(b, af, minrow, maxrow, select); #ifdef FASTCOUNT - b->fast.enemies[select] = i; + b->fast.enemies[select] = i; #endif - return i; + return i; + } + return 0; } troop