// scanbot3.c by Chris Dolan // Scans for enemy, shoots and charges. Has 3 scan modes: coarse, medium and // fine. Shoots 3 bullets each time it finds an enemy (good improvement on // shooting time/scanning time ratio). #include "robots.h" #define True 1 #define False 0 /* For wide scans, use coarsest resolution (21 degree width, +-10) Inside range threshhold 1, just do medium scans (7 degree width, +-3) Inside range threshhold 1, do intermdiate-tight scans (3 degree width, +-1) For anything further, use maximum resolution (1 degree width, +-0) */ #define RANGETHRESH1 500 #define RANGETHRESH2 1500 /* Do not fire a cannon closer than this range */ #define CANNON_MIN 201 /* 201 ==> 1 point of damage to me */ /* wide is last known angle to enemy */ /* Start wide=(number>>0) so we never get negative value (wide%360 < 0 if wide < 0) */ static int wide=720; /* Fire cannon 3 times */ int cannon3(int dir, int range) { return cannon(dir, range) + cannon(dir, range) + cannon(dir, range); } /* Replace all subsequent occurrences of cannon() with cannon3() */ #define cannon cannon3 /* Just look around as quickly as possible to get a rough direction to enemy */ void widescan(void) { register int range; for (; True; wide += 20) { range = scan(wide, 10); if (range > 0) { return; } } } /* Fine resolution search for enemy. The farther away the enemy is, the finer resolution we need to shove a cannon shell right up his butt */ int tightscan(int range) { register int r, i; if (range > RANGETHRESH1) { /* finer scan, else just leave it alone */ if (range > RANGETHRESH2) { /* finest scan */ for (i=-3;i<=3;i++) { r = scan(wide+i, 0); if (r > 0) { wide = wide+i; return r; } } } else { for (i=-3;i<=3;i+=3) { r = scan(wide+i, 1); if (r > 0) { wide = wide+i; return r; } } } } return range; } /* Scan, charge, shoot, not necessarily in that order */ /* Check left, middle, right in that order since widescan goes right to left */ main(void) { register int range; widescan(); while (1) { drive(wide, 100); /* Charge! */ range = scan(wide+7, 3); /* check left... */ if (range > 0) { wide += 7; range = tightscan(range); cannon(wide, (range>=CANNON_MIN ? range : CANNON_MIN)); } else { range = scan(wide, 3); /* check middle... */ if (range > 0) { range = tightscan(range); cannon(wide, (range>=CANNON_MIN ? range : CANNON_MIN)); } else { range = scan(wide-7, 3); /* check right */ if (range > 0) { wide -= 7; range = tightscan(range); cannon(wide, (range>=CANNON_MIN ? range : CANNON_MIN)); } else { /* Damn, lost him... Back up a few and re-scan*/ /* drive(wide, 0); */ /* Don't hit a wall! */ wide -= 20; widescan(); } } } } }