Skip to content

Instantly share code, notes, and snippets.

@manuel14
Created November 13, 2013 19:51
Show Gist options
  • Save manuel14/7455244 to your computer and use it in GitHub Desktop.
Save manuel14/7455244 to your computer and use it in GitHub Desktop.
lightning storm
// $Id: RandomMovementBot.java,v 1.2 2004/01/04 18:52:08 peter Exp $
package EH.kms;
import robocode.*;
import robocode.util.Utils;
import java.awt.geom.*;
public class LightningStorm extends AdvancedRobot {
// random movement constants and variables
// Credit goes to PEZ for RandomMovementBot
static final double MAX_VELOCITY = 8;
static final double WALL_MARGIN = 25;
Point2D robotLocation;
Point2D enemyLocation;
double enemyDistance;
double enemyAbsoluteBearing;
double movementLateralAngle = 0.2;
// gun variables
static final int maxMatchLen = 30;
static final int firePower = 2;
static final int fireSpeed = 20 - firePower * 3;
static int distance;
public void run() {
setAdjustRadarForGunTurn(true);
setAdjustGunForRobotTurn(true);
do {
turnRadarRightRadians(distance = 1000000);
} while (true);
}
public void onScannedRobot(ScannedRobotEvent e) {
// movement
robotLocation = new Point2D.Double(getX(), getY());
enemyAbsoluteBearing = getHeadingRadians() + e.getBearingRadians();
enemyDistance = e.getDistance();
enemyLocation = vectorToLocation(enemyAbsoluteBearing, enemyDistance, robotLocation);
move();
int matchLen = maxMatchLen;
double a;
int dist;
int matchPos;
int i;
// variables
if ((a = getVelocity()) == 0) {
}
if ((dist = (int) e.getDistance()) < distance + 32) {
a = e.getBearingRadians() + getHeadingRadians();
setTurnRadarRightRadians(Double.POSITIVE_INFINITY
* Utils.normalRelativeAngle(a - getRadarHeadingRadians()));
enemyHistory =
String.valueOf((char) Math.rint(
e.getVelocity() * Math.sin(e.getHeadingRadians() - a)))
.concat(enemyHistory);
while(
(matchPos =
enemyHistory.indexOf(
enemyHistory.substring(0, matchLen--),
i = dist / fireSpeed))
< 0);
do {
a += (double) (short) enemyHistory.charAt(--matchPos) / dist;
} while (--i > 0);
setTurnGunRightRadians(
Utils.normalRelativeAngle(a - getGunHeadingRadians()));
if (getEnergy() < 0.5) {
return;
}
setFire(firePower);
}
}
public void onRobotDeath(RobotDeathEvent e) {
distance = 1000000;
}
// Always try to move a bit further away from the enemy.
// Only when the walls forces us we will close in on the enemy. We never bounce of walls.
void move() {
if (Math.random() < 0.06) {
movementLateralAngle *= -1;
}
Point2D robotDestination = null;
double tries = 0;
do {
robotDestination = vectorToLocation(absoluteBearing(enemyLocation, robotLocation) + movementLateralAngle,
enemyDistance * (1.1 - tries / 100.0), enemyLocation);
tries++;
} while (tries < 100 && !fieldRectangle(WALL_MARGIN).contains(robotDestination));
goTo(robotDestination);
}
RoundRectangle2D fieldRectangle(double margin) {
return new RoundRectangle2D.Double(margin, margin,
getBattleFieldWidth() - margin * 2, getBattleFieldHeight() - margin * 2, 75, 75);
}
void goTo(Point2D destination) {
double angle = Utils.normalRelativeAngle(absoluteBearing(robotLocation, destination) - getHeadingRadians());
double turnAngle = Math.atan(Math.tan(angle));
setTurnRightRadians(turnAngle);
setAhead(robotLocation.distance(destination) * (angle == turnAngle ? 1 : -1));
// Hit the brake pedal hard if we need to turn sharply
setMaxVelocity(Math.abs(getTurnRemaining()) > 33 ? 0 : MAX_VELOCITY);
}
static Point2D vectorToLocation(double angle, double length, Point2D sourceLocation) {
return vectorToLocation(angle, length, sourceLocation, new Point2D.Double());
}
static Point2D vectorToLocation(double angle, double length, Point2D sourceLocation, Point2D targetLocation) {
targetLocation.setLocation(sourceLocation.getX() + Math.sin(angle) * length,
sourceLocation.getY() + Math.cos(angle) * length);
return targetLocation;
}
static double absoluteBearing(Point2D source, Point2D target) {
return Math.atan2(target.getX() - source.getX(), target.getY() - source.getY());
}
static String enemyHistory = ""
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 1
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 2
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char)-1
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char) 0 + (char) 0 + (char) 0
+ (char) 0 + (char)-2 + (char)-4 + (char)-6
+ (char)-8 + (char)-8 + (char)-8 + (char)-8
+ (char)-8 + (char)-8 + (char)-8 + (char)-8
+ (char)-8 + (char)-8 + (char)-8 + (char)-8
+ (char)-8 + (char)-8 + (char)-8 + (char)-8
+ (char)-8 + (char)-8 + (char)-8 + (char)-8
+ (char)-8 + (char)-8 + (char)-8 + (char)-8
+ (char)-8 + (char)-8 + (char)-8 + (char)-8
+ (char)-8 + (char)-8 + (char)-8 + (char)-8
+ (char)-8 + (char)-8 + (char)-8 + (char)-8
+ (char)-8 + (char)-8 + (char)-8 + (char)-8
+ (char)-7 + (char)-6 + (char)-5 + (char)-4
+ (char)-3 + (char)-2 + (char)-1 + (char)0
+ (char) 2 + (char) 4 + (char) 6 + (char) 8
+ (char) 8 + (char) 8 + (char) 8 + (char) 8
+ (char) 8 + (char) 8 + (char) 8 + (char) 8
+ (char) 8 + (char) 8 + (char) 8 + (char) 8
+ (char) 8 + (char) 8 + (char) 8 + (char) 8
+ (char) 8 + (char) 8 + (char) 8 + (char) 8
+ (char) 8 + (char) 8 + (char) 8 + (char) 8
+ (char) 8 + (char) 8 + (char) 8 + (char) 8
+ (char) 8 + (char) 8 + (char) 8 + (char) 8
+ (char) 8 + (char) 8 + (char) 8 + (char) 8
+ (char) 8 + (char) 8 + (char) 8 + (char) 8
+ (char) 7 + (char) 6 + (char) 5 + (char) 4
+ (char) 3 + (char) 2 + (char) 1 + (char) 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment