- optimized is_guardian

- realized the optimization can be done further up. doh.
This commit is contained in:
Enno Rehling 2008-04-26 21:39:09 +00:00
parent 371409e943
commit 93091caff0
2 changed files with 64 additions and 24 deletions

View File

@ -3611,6 +3611,7 @@ init_battle(region * r, battle **bp)
if (get_keyword(ord) == K_ATTACK) { if (get_keyword(ord) == K_ATTACK) {
unit *u2; unit *u2;
fighter *c1, *c2; fighter *c1, *c2;
ship * lsh = NULL;
if (r->planep && fval(r->planep, PFL_NOATTACK)) { if (r->planep && fval(r->planep, PFL_NOATTACK)) {
cmistake(u, ord, 271, MSG_BATTLE); cmistake(u, ord, 271, MSG_BATTLE);
@ -3652,16 +3653,14 @@ init_battle(region * r, battle **bp)
continue; continue;
} }
if ((u->ship != NULL && !fval(r->terrain, SEA_REGION)) || (lsh = leftship(u))!=NULL) {
if (is_guarded(r, u, GUARD_TRAVELTHRU)) { if (is_guarded(r, u, GUARD_TRAVELTHRU)) {
/* Fehler: "Das Schiff muß erst verlassen werden" */ if (lsh) {
if (u->ship != NULL && !fval(r->terrain, SEA_REGION)) {
cmistake(u, ord, 19, MSG_BATTLE);
continue;
}
if (leftship(u)) {
cmistake(u, ord, 234, MSG_BATTLE); cmistake(u, ord, 234, MSG_BATTLE);
} else {
/* Fehler: "Das Schiff muß erst verlassen werden" */
cmistake(u, ord, 19, MSG_BATTLE);
}
continue; continue;
} }
} }

View File

@ -852,40 +852,81 @@ bewegung_blockiert_von(unit * reisender, region * r)
} }
static boolean static boolean
is_guardian(unit * u2, unit *u, unsigned int mask) is_guardian_u(unit * u2, unit *u, unsigned int mask)
{ {
if (u2->faction == u->faction) return false; if (u2->faction == u->faction) return false;
if ((getguard(u2) & mask) == 0) return false;
if (u2->number == 0) return false;
if (alliedunit(u2, u->faction, HELP_GUARD)) return false; if (alliedunit(u2, u->faction, HELP_GUARD)) return false;
if (ucontact(u2, u)) return false; if (ucontact(u2, u)) return false;
if (besieged(u2)) return false;
if (!armedmen(u2) && !fval(u2->race, RCF_UNARMEDGUARD)) return false;
if (!cansee(u2->faction, u->region, u, 0)) return false; if (!cansee(u2->faction, u->region, u, 0)) return false;
return true; return true;
} }
static boolean
is_guardian_r(unit * u2, unsigned int mask)
{
if ((getguard(u2) & mask) == 0) return false;
if (u2->number == 0) return false;
if (besieged(u2)) return false;
if (!armedmen(u2) && !fval(u2->race, RCF_UNARMEDGUARD)) return false;
return true;
}
#define MAXGUARDCACHE 16
unit * unit *
is_guarded(region * r, unit * u, unsigned int mask) is_guarded(region * r, unit * u, unsigned int mask)
{ {
unit *u2; unit *u2 = NULL;
static unit * guardcache; int i;
static unit * guardcache[MAXGUARDCACHE], * lastguard;
if (!fval(r, RF_GUARDED)) { if (!fval(r, RF_GUARDED)) {
return NULL; return NULL;
} }
if (guardcache && guardcache->region==r) {
if (is_guardian(guardcache, u, mask)) { if (lastguard && lastguard->region==r) {
return guardcache; if (is_guardian_u(lastguard, u, mask)) {
return lastguard;
} }
} }
for (u2 = r->units; u2; u2 = u2->next) { for (i=0;i!=MAXGUARDCACHE;++i) {
if (u2!=guardcache && is_guardian(u2, u, mask)) { unit * guard = guardcache[i];
guardcache = u2; if (guard && guard!=lastguard && guard->region==r) {
if (is_guardian_u(guard, u, mask)) {
lastguard = guard;
return guard;
}
if (u2==guard) {
/* same guard twice signals we've tested everyone */
return NULL;
}
u2 = guard;
} else {
/* exhausted all the guards in the cache, but maybe we'll find one later? */
break;
}
}
/* at this point, u2 is the last unit we tested to
* be a guard (and failed), or NULL
* i is the position of the first free slot in the cache */
for (u2 = (u2?u2->next:r->units); u2; u2=u2->next) {
if (is_guardian_r(u2, mask)) {
/* u2 is a guard, so worth remembering */
if (i<MAXGUARDCACHE) guardcache[i++] = u2;
if (is_guardian_u(u2, u, mask)) {
/* u2 is our guard. stop processing (we might have to go further next time) */
return u2; return u2;
} }
} }
}
/* there are no more guards. we signal this by duplicating the last one.
* i is still the position of the first free slot in the cache */
if (i>0 && i<MAXGUARDCACHE) {
guardcache[i] = guardcache[i-1];
}
return NULL; return NULL;
} }