cylon.c <rrognlie@gamerz.net> // Randomly move around the arena. While moving, scan back and forth up // to 45 degrees (like the cylon robots from "Battlestar Galactica". // If it sees something, it shoots it... #include "robots.h" int Distance(int x1, int y1, int x2, int y2) { int dx = x1-x2; int dy = y1-y2; return sqrt(dx*dx + dy*dy); } void Goto(int x, int y) { int dir = atan2(y-loc_y(),x-loc_x()); int dist = Distance(x,y,loc_x(),loc_y()); int t = time(); int sc = 0; int sd = 1; int range=0; drive(dir,100); while (speed() && time()-t < dist) { if (abs(sc) == 45) sd *= -1; sc += 3*sd; range = scan(dir+sc,5); if (range > 200 && range < 7000) cannon(dir+sc,range); int tdir = atan2(y-loc_y(),x-loc_x()); if (tdir != dir) drive(dir=tdir,100); } drive(dir,0); while (speed()) ; } int main() { while (1) Goto(rand(9000)+500,rand(9000)+500); } tracker.c <rrognlie@gamerz.net> // 1) Move around the arena in a box pattern. Full speed. staying // about 2000 units inside from the wall. // 2) Scan for a target, and shoot it. If you had seen a target, and // have lost sight, back up the scan a bit and continue scanning. #include "robots.h" #define BORDER 2000 // Shoot at a target if it's in range (<= 7000 units) *and* it's far // enough away that we'll only be slightly damaged (>200 units) by the // resulting explosion. inline void shoot(int dir,int range) { if (range > 200 && range <= 7000) cannon(dir,range); } int main() { int sdir=0; // current scan direction int dir=0; // current movement direction int range; // range to opponent int hadfix=0; // did I have a fix on my opponent from the last scan? int cx,cy; // current x and y position drive(dir,100); // start moving right away. Don't *ever* sit still! while (1) { int tdir=dir; // save current direction cx = loc_x(); cy = loc_y(); // do we need to change direction? (e.g. are we approaching a wall?) if (cx > 10000-BORDER) if (cy < 10000-BORDER) tdir = 90; // approaching east wall else tdir = 180; // approaching northeast corner else if (cx < BORDER) if (cy < BORDER) tdir = 0; // approaching southwest corner else tdir = 270; // approaching west wall else if (cy > 10000-BORDER) tdir = 180; // approaching north wall else if (cy < BORDER) tdir = 0; // approaching south wall // if speed() == 0, restart the drive unit... // if dir != tdir, we need to change direction... if (!speed() || dir != tdir) drive(dir=tdir,100); if ((range=scan(sdir,10))) { // scan for a target... shoot(sdir,range); // got one. shoot it! hadfix=1; // remember we saw a target } else if (hadfix) { // did we lose a target? sdir += 40; // back up the scan hadfix=0; // forget we had a target } else sdir -= 20; // increment the scan } } |