Shrinking OculusMicro
Fragment of a discussion from Help talk:Code Shrinking
Jump to navigation
Jump to search
- (edit: more details) With the MEA trick and some try catches I got it down to 769. I also wrote the surfing algorithm with a for loop. Removed absoIuteBearing method since it was used only once. I feel like a professional. =)
package dsekercioglu.micro;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import robocode.Condition;
import robocode.Rules;
import static robocode.util.Utils.normalRelativeAngle;
public class OculusMicro extends robocode.AdvancedRobot {
static float[][] statistics = new float[5][31];
static double enemyEnergy;
static double oldLatVel;
static Point2D.Double myLocation;
static Rectangle2D.Double battleField = new Rectangle2D.Double(18, 18, 764, 564);
ArrayList<MoveWave> waves = new ArrayList();
static String h1;
public void run() {
setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
setAdjustRadarForGunTurn(true);
}
public void onScannedRobot(robocode.ScannedRobotEvent e) {
double distance;
double bearing;
double absoluteBearing;
Point2D.Double enemyPosition = project(myLocation = new Point2D.Double(getX(), getY()), absoluteBearing = (bearing = e.getBearingRadians()) + getHeadingRadians(), distance = e.getDistance());
setTurnRightRadians(normalRelativeAngle(bearing + Math.PI / 2));
MoveWave w;
if ((char) (((w = new MoveWave()).speed = (20 - 3 * (enemyEnergy - (enemyEnergy = e.getEnergy())))) - 11) <= 8) {
w.dir = (float) (Math.signum(oldLatVel + 0.001) * 1.15);
w.absBearing = absoluteBearing + Math.PI;
w.bins = statistics[(byte) (Math.abs(oldLatVel)) >> 1];
w.source = (Point2D.Double) enemyPosition.clone();
waves.add(w);
addCustomEvent(w);
}
oldLatVel = getVelocity() * Math.sin(bearing);
try {
MoveWave surfWave;
//bearing is move
bearing = (surfWave = waves.get(0)).timeForImpact * 8 / distance + Math.PI;
float lowestDanger = 1000;
for (int i = -1; i <= 1; i += 2) {
float danger;
double angle;
if ((danger = surfWave.bins[surfWave.getBin(angle = (absoluteBearing + bearing * i))]) < lowestDanger && battleField.contains(project(enemyPosition, angle, distance))) {
lowestDanger = danger;
setBack(i * 100);
}
}
} catch (Exception exc) {
}
setTurnRadarLeftRadians(getRadarTurnRemaining());
h1 += (char) (e.getVelocity() * Math.sin(absoluteBearing - e.getHeadingRadians()) + 8.5);
int matchLen = 30;
try {
while (matchLen-- > 0) {
int size;
int b;
int matchi = h1.substring(0, (size = h1.length()) - (b = (int) (distance / 14))).lastIndexOf(h1.substring(size - matchLen, size));
if (matchi != -1) {
matchi += matchLen;
for (int i = 0; i < b; i++) {
absoluteBearing -= (h1.codePointAt(matchi + i) - 8) / distance;
}
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absoluteBearing - getGunHeadingRadians()));
setFire(2);
break;
}
}
} catch (Exception exc) {
}
}
static Point2D.Double project(Point2D.Double source, double angle, double distance) {
return new Point2D.Double(source.x + (distance * Math.sin(angle)), source.y + (distance * Math.cos(angle)));
}
public void onHitByBullet(robocode.HitByBulletEvent e) {
try {
MoveWave w;
int bin = (w = waves.get(0)).getBin(Math.atan((myLocation.y - w.source.y) / (myLocation.x - w.source.x)));
int i = 0;
while (true) {
w.bins[i] += 1 / (Math.pow((i++) - bin, 2) + 1);
}
} catch (Exception exc) {
}
}
public class MoveWave extends Condition {
float[] bins;
double speed;
int time;
double absBearing;
int timeForImpact;
float dir;
Point2D.Double source;
public int getBin(double angle) {
return (int) (normalRelativeAngle(angle - absBearing) * speed * dir + 15.5);
}
@Override
public boolean test() {
if ((timeForImpact = (int) (source.distance(myLocation) - (++time) * speed)) < 0) {
waves.remove(this);
}
return false;
}
}
}
Dsekercioglu (talk)