Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created June 13, 2018 16:07
Show Gist options
  • Save cbilson/9d6ad23798728142e162f773bd662961 to your computer and use it in GitHub Desktop.
Save cbilson/9d6ad23798728142e162f773bd662961 to your computer and use it in GitHub Desktop.
Radar.java
import java.awt.Color;
import ihs.apcs.spacebattle.BasicEnvironment;
import ihs.apcs.spacebattle.BasicSpaceship;
import ihs.apcs.spacebattle.ObjectStatus;
import ihs.apcs.spacebattle.Point;
import ihs.apcs.spacebattle.RadarResults;
import ihs.apcs.spacebattle.RegistrationData;
import ihs.apcs.spacebattle.commands.BrakeCommand;
import ihs.apcs.spacebattle.commands.IdleCommand;
import ihs.apcs.spacebattle.commands.RadarCommand;
import ihs.apcs.spacebattle.commands.RotateCommand;
import ihs.apcs.spacebattle.commands.ShipCommand;
import ihs.apcs.spacebattle.commands.ThrustCommand;
public class MySurvivor extends BasicSpaceship {
Point center;
@Override
public ShipCommand getNextCommand(BasicEnvironment env) {
// Figure stuff out
RadarResults radar = env.getRadar();
ObjectStatus ship = env.getShipStatus();
double speed = ship.getSpeed();
ship.getShieldLevel();
int turn = ship.getPosition().getAngleTo(center);
turn -= ship.getOrientation();
double dist = ship.getPosition().getDistanceTo(center);
if (turn < 0)
turn += 360;
if (speed < 50.0)
return new ThrustCommand('B', 1.0, 1.0);
// telemetry
//System.out.println("speed: " + speed + ", turn: " + turn + ", dist: " + dist + ".....");
if (radar == null) {
System.out.println("RADAR!");
return new RadarCommand(5);
}
for (ObjectStatus o : radar.getByType("Ship")) {
if (o.getPosition().getDistanceTo(ship.getPosition()) < 100.0) {
int angleToThing = ship.getPosition().getAngleTo(o.getPosition());
int heading = ship.getOrientation();
int angleDiff = angleToThing - heading;
System.out.println("Something is close and angle "
+ heading);
if (angleDiff > 15 || angleDiff < -15)
continue;
System.out.println("WOOT! WOOT! Evasive action!");
return new ThrustCommand('L', 1.0, 1.0);
}
}
// decide what to do
if (turn > 5 && turn < 355) {
//System.out.println("Rotate");
return new RotateCommand(turn);
}
if (dist < 150.0) {
//System.out.println("Brake");
return new BrakeCommand(0.01);
}
if (speed < 50.0) {
//System.out.println("Thrust");
return new ThrustCommand('B', 1.0, 1.0);
}
//System.out.println("Idle");
return new IdleCommand(0.5);
}
@Override
public RegistrationData registerShip(int numImages, int w, int h) {
center = new Point(w / 2, h / 2);
return new RegistrationData("Chris's2 Ship", new Color(255, 0, 0), 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment