Wanderer.java

The Wanderer picks a random spot on the board and goes there. Once there, it picks another random spot. If it can't get to the goal spot (the goal spot is off the board, someone's in the way, etc.) then the Wanderer chases the ball for a while, and then picks a new random spot. A Wanderer in the Center position always picks the ball's location as the goal spot.

Oh. If the ball happens to get in the way, kick it.

package Riddler.Flash;

import sockey.SoccerClient;
import java.awt.*;

public class Wanderer extends SoccerClient
{
    Point goal= new Point(0,0);
    Point ball= new Point(0,0);
    int count= 0;
    public static final int b=3;
    
    public void step() {
        int spin=0;

        // If we're the center, then always go after the ball.
        if (getPosition()== ID_PCTR) {
            ping(ID_BALL, goal, true);
            accelerate(goal.y, 100);
            spin= goal.y;
        } else {
        // If we're not the center, then pick a random point and go there.
        // If we can't get there in a decent amount of time, then go chase
        // the ball for a while.
            if (count++ > 2000) {
                // We've been chasing the ball for a while.  Go back to
                // a random target.
                goal.x= (int)((Math.random()-0.5)*size.width);
                goal.y= (int)((Math.random()-0.5)*size.height);
                debugMsg("Wandering...");
                count= 0; // reset the counter
                spin= wannabe(ID_CENTER, goal.x, goal.y);
            } else if (count > 1000) {
                // We didn't get to the point we wanted.  Go chase the ball
                // for a while.
                spin= wannabe(ID_BALL, 0, 0);
            } else {
                // Go for the selected random point (goal).  Wannabe returns
                // the angle we're accelerating, or 90 if we've reached our
                // destination.
                spin= wannabe(ID_CENTER, goal.x, goal.y);
            }
            if (count==100) {
                debugMsg("Wandering...");
            } else if (count==1001) {
                debugMsg("Stymied: going for the ball");
            }
            getVel(pt);
            // If wannabe says we're there (spin==90) and we aren't moving
            // very fast (pt==(0,0)), then we must have arrived at our point.
            // Pick a new point and reset the counter.
            if (spin==90 && pt.x==0 && pt.y==0) {
                debugMsg("Got there!");
                goal.x= (int)((Math.random()-0.5)*size.width);
                goal.y= (int)((Math.random()-0.5)*size.height);
                count= 0;
            }

        }
        // If the ball is nearby, then face the ball.
        ping(ID_BALL, ball, true);
        if (ball.x<60) {spin= ball.y;}
        // If the ball is kickable, then kick it with whichever side is
        // CLOSEST to the ball.  (subtract the angle we're facing from the
        // global angle to the ball, and spin into it)
        if (kickable()) {
            int where= anglesub(ball.y, getAng());
            setAngGoal(ball.y + ((where>0)?-90:90));
        } else {
            setAngGoal(spin);
        }
    }

}