/** run: Tracker's main run function */ @Override public void run_(solomon s) { // Prepare gun trackName = null; // Initialize to not tracking anyone s.setAdjustGunForRobotTurn(true); // Keep the gun still when we turn gunTurnAmt = 10; // Initialize gunTurn to 10 // Loop forever while (true) { // turn the Gun (looks for enemy) s.turnGunRight(gunTurnAmt); // Keep track of how long we've been looking count++; // If we've haven't seen our target for 2 turns, look left if (count > 2) { gunTurnAmt = -10; } // If we still haven't seen our target for 5 turns, look right if (count > 3) { gunTurnAmt = 10; } // If we *still* haven't seen our target after 10 turns, find another target if (count > 11) { trackName = null; } } }
/** onScannedRobot: Here's the good stuff */ @Override public void onScannedRobot_(solomon s, ScannedRobotEvent e) { // If we have a target, and this isn't it, return immediately // so we can get more ScannedRobotEvents. if (trackName != null && !e.getName().equals(trackName)) { return; } // If we don't have a target, well, now we do! if (trackName == null) { trackName = e.getName(); // out.println("Tracking " + trackName); } // This is our target. Reset count (see the run method) count = 0; // If our target is too far away, turn and move torward it. if (e.getDistance() > 150) { gunTurnAmt = normalRelativeAngle(e.getBearing() + (s.getHeading() - s.getRadarHeading())); s.turnGunRight(gunTurnAmt); // Try changing these to setTurnGunRight, s.turnRight(e.getBearing()); // and see how much Tracker improves... // (you'll have to make Tracker an AdvancedRobot) s.ahead(e.getDistance() - 135); return; } // Our target is close. gunTurnAmt = normalRelativeAngle(e.getBearing() + (s.getHeading() - s.getRadarHeading())); s.turnGunRight(gunTurnAmt); s.fire(3); // Our target is too close! Back up. if (e.getDistance() < 100) { if (e.getBearing() > -90 && e.getBearing() <= 90) { s.back(30); } else { s.ahead(40); } } s.scan(); }
/** onHitRobot: Set him as our new target */ @Override public void onHitRobot_(solomon s, HitRobotEvent e) { // Only print if he's not already our target. if (trackName != null && !trackName.equals(e.getName())) { // out.println("Tracking " + e.getName() + " due to collision"); } // Set the target trackName = e.getName(); // Back up a bit. // Note: We won't get scan events while we're doing this! // An AdvancedRobot might use setBack(); execute(); gunTurnAmt = normalRelativeAngle(e.getBearing() + (s.getHeading() - s.getRadarHeading())); s.turnGunRight(gunTurnAmt); s.fire(3); s.back(50); }