package TestRobotIgaki;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import robocode.WinEvent;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import robocode.HitByBulletEvent;
import robocode.HitWallEvent;
import robocode.Event;
import java.awt.*;
// API help : https://robocode.sourceforge.io/docs/robocode/robocode/Robot.html
/**
* TestRobot - a robot by (your name here)
*/
public class TestRobot extends Robot
{
/**
* run: TestRobot's default behavior
*/
int count = 0; // Keeps track of how long we've
// been searching for our target
double gunTurnAmt; // How much to turn our gun when searching
String trackName; // Name of the robot we're currently tracking
/**
* run: Tracker's main run function
*/
public void run() {
// Set colors
setBodyColor(new Color(128, 128, 50));
setGunColor(new Color(50, 50, 20));
setRadarColor(new Color(200, 200, 70));
setScanColor(Color.white);
setBulletColor(Color.blue);
// Prepare gun
trackName = null; // Initialize to not tracking anyone
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)
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 > 5) {
gunTurnAmt = 10;
}
// If we *still* haven't seen our target after 10 turns, find another target
if (count > 11) {
trackName = null;
}
}
}
/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(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 toward it.
if (e.getDistance() > 150) {
gunTurnAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
turnGunRight(gunTurnAmt); // Try changing these to setTurnGunRight,
turnRight(e.getBearing()); // and see how much Tracker improves...
// (you'll have to make Tracker an AdvancedRobot)
ahead(e.getDistance() - 140);
return;
}
// Our target is close.
gunTurnAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
turnGunRight(gunTurnAmt);
fire(3);
// Our target is too close! Back up.
if (e.getDistance() < 100) {
if (e.getBearing() > -90 && e.getBearing() <= 90) {
back(40);
} else {
ahead(40);
}
}
scan();
}
/**
* onHitRobot: Set him as our new target
*/
public void onHitRobot(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 = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
turnGunRight(gunTurnAmt);
fire(3);
back(50);
}
/**
* onHitByBullet: What to do when you're hit by a bullet
*/
public void onHitByBullet(HitByBulletEvent e) {
double energy = getEnergy();
//double bearing = e.getBearing();
double angle = Math.random() * 90;
//if the energy is > 80%, we keep attacking
//if(getEnergy() >= 80)
//{
// turnRight(360);
// }
// > 50%, we keep atacking and avoiding a little
if(getEnergy() >= 50 && getEnergy() < 80)
{
turnRight(angle);
back(100);
fire(2);
}
//> 20%, we keep attacking and avoiding a lot
else if(getEnergy() >= 20 && getEnergy() < 50)
{
turnRight(angle);
back(200);
fire(1);
}
// < 20%, we avoid the death
else if(getEnergy() < 20)
{
turnRight(angle);
back(500);
}
}
/**
* onHitWall: What to do when you hit a wall
*/
public void onHitWall(HitWallEvent e) {
// Replace the next line with any behavior you would like
ahead(100);
}
}