TajBot (Robocode bot)

Copyright Tristan Aubrey-Jones December 2005.

Enemy.java

home Home   up Up   ( Download )


package tajy; import robocode.*; /** * MyClass - a class by (your name here) */ public class Enemy { private String name; private Vector position; private Vector heading; private double velocity; private long scanTime; private double distance; private double hitMe = 1; private long lastHitMe = 1; private double missedHim = 1; private double gotHim = 1; private long lastRatioClean = 0; private double current = 1; private double energy; private CycleSpotter cycle; public Enemy(String name) { this.name = name; cycle = new CycleSpotter(1024); } /** * Returns the ratio of hits to misses. * @return */ public double hitRatio() { return gotHim / missedHim; } public CycleSpotter getPatternWatcher() { return cycle; } public void setTargeted(boolean b) { if (b) current = 2; else current = 1; } public void shotMe(Robot r) { hitMe += 2; lastHitMe = r.getTime(); } public void missedHim() { missedHim++; } public void gotHim() { gotHim++; } public String getName() { return name; } public double getPriority() { double e = 1; if (energy < 10) e = 20; return (e * current * hitRatio()) / (distance * 5); } public double getThreat() { return hitMe * getPriority(); } public Vector getPosition() { return position; } public double getDistance(Vector m) { m = position.subtract(m); return m.getDistance(); } public Vector getHeading() { return heading; } public double getVelocity() { return velocity; } public double getTimeSinceScan(double currentTime) { return currentTime - scanTime; } public long getTimeSinceScan(long currentTime) { return currentTime - scanTime; } public void hit(HitRobotEvent e, Robot me) { distance = 0.1; double bearing = e.getBearing(); Vector m = new Vector(me.getX(), me.getY()); position = BotMaths.getTargetCoord(m, me.getHeading(), bearing, distance); } public void scanned(ScannedRobotEvent e, Robot me) { // calculate target's coordinate Vector m = new Vector(me.getX(), me.getY()); position = BotMaths.getTargetCoord(m, me.getHeading(), e.getBearing(), e.getDistance()); // store the circumstance data scanTime = me.getTime(); velocity = e.getVelocity(); distance = e.getDistance(); energy = e.getEnergy(); // log the target's position cycle.add(scanTime, position.getX(), position.getY()); // store the target's heading and velocity heading = Vector.fromAngleDegrees(e.getHeading(), e.getVelocity()); // clean hit ratio /*if (lastRatioClean + 300 <= me.getTime()) { gotHim = 1; missedHim = 1; hitMe = 1; lastRatioClean = me.getTime(); }*/ } }