Basilisk

From RoboWiki
Jump to: navigation, search
Basilisk Sub-pages:
Version History
Basilisk
Author(s) User:Slugzilla
Extends AdvancedRobot
Targeting Guessfactor Targeting
Movement Stop and go and Random Movement
Released 2019
Current Version 2.19
Code License RWPCL
[Basilisk Download]

Contents

Background Information

What's special about it?

It's my first robot =)

How competitive is it?

Currently #15 in the MiniRumble and #118 in the RoboRumble.

Strategy

How does it move?

It starts with stop and go, but if it gets hit too much, it will switch to random movement.

How does it fire?

It uses guessfactor targeting.

How does it dodge bullets?

Stop and go dodges the simple targeters, random movement dodges the rest =)

How does the melee strategy differ from one-on-one strategy?

It doesn't have a melee strategy.

Additional Information

Where did you get the name?

I wanted a robot that was mean, lean, and green. Basilisks are green, and I think they're pretty mean, at least according to the legends =)

Can I use your code?

Sure! See code below or decompile the .jar file It's licensed under the RWPCL.

What's next for your robot?

Top 10!!!

Does it have any White Whales?

Partial is it's arch-nemesis!!!

What other robot(s) is it based on?

Credits:

Based off of RandomMovementBot and the GFTargetingBot by PEZ

Uses the Turn Multiplier Lock from the RoboWiki

Dive protection is from by Tityus

The favorite distance idea was inspired by Raiko

Uses Raiko's reverse direction formula

Stop and go at the beginning and then swapping to random movement was inspired by Cotillion and BlackWidow

Improved mode swaps from EpeeistMicro and Cotillion

Uses Cotillions stop and go move amount formula

Special thanks to Dsekercioglu for giving me ideas for beating the bullet shielders, and help identifying bugs

And of course, a huge thank you to all the contributors on the RoboWiki for your awesome tutorials and open source robots!

Code

Version 2.15

package slugzilla;
import robocode.*;
import java.awt.geom.*;
import robocode.util.*;
import java.awt.*;
import static robocode.util.Utils.normalRelativeAngleDegrees;
 
public class Basilisk extends AdvancedRobot {
 
	private static double lateralDirection;
	private static double lastEnemyVelocity;
	static final double MAX_VELOCITY = 8;
    static final double WALL_MARGIN = 25;
	static double enemyFirePower;
	static double enemyBulletVelocity;
    Point2D robotLocation;
    Point2D enemyLocation;
    double enemyDistance;
    double enemyAbsoluteBearing;
	double enemyVelocity;
    double movementLateralAngle;
	double direction = 1;
	static int bullets;
	static double hits;
	static double enemyEnergy;
	static double deltaT;
	static double lastDeltaT;
	static double lastReverseTime;
 
    public void run() {
        setAdjustRadarForGunTurn(true);
 		setAdjustGunForRobotTurn(true);
		setBodyColor(new Color(40, 100, 100));
		setGunColor(new Color(34, 50, 50));
		setRadarColor(new Color(0, 255, 0));
		setScanColor(new Color(0, 255, 0));
	 	turnRadarRightRadians(Double.POSITIVE_INFINITY);
        do {
            scan();
        } while (true);
    }
 
    public void onScannedRobot(ScannedRobotEvent e) {
		//radar ↓
		double radarTurn = getHeadingRadians() + e.getBearingRadians() - getRadarHeadingRadians();
 
		//movement ↓
        robotLocation = new Point2D.Double(getX(), getY());
        enemyAbsoluteBearing = getHeadingRadians() + e.getBearingRadians();
        enemyDistance = e.getDistance();
		enemyVelocity = e.getVelocity();
        enemyLocation = vectorToLocation(enemyAbsoluteBearing, enemyDistance, robotLocation);
		enemyBulletVelocity = 20 - 3*enemyFirePower;
		//we move far away when close, but stabilize and move perpendicular when far ↓
		movementLateralAngle = Math.max(enemyDistance/2000, 0.075);
		if (enemyEnergy > e.getEnergy() && enemyEnergy - 3 <= e.getEnergy()) {
			enemyFirePower = enemyEnergy - e.getEnergy();
		}
			if (bullets >= 1 && enemyDistance > 100) {
			considerChangingDirection();
		} 
 
		for (int i = 0; i < 2; i++) {
			Point2D robotDestination = null;
			double tries = 0;
			do {
				robotDestination = vectorToLocation(absoluteBearing(enemyLocation, robotLocation) + movementLateralAngle*direction,
					enemyDistance * (1.1 - tries / 100.0), enemyLocation);	
			tries++;
 
			} while (tries < 100 && !fieldRectangle(WALL_MARGIN).contains(robotDestination));	
				double angle = Utils.normalRelativeAngle(absoluteBearing(robotLocation, robotDestination) - getHeadingRadians());
				double turnAngle = Math.atan(Math.tan(angle));
		        setTurnRightRadians(turnAngle);	
				//random movement ↓
				if (bullets >= 1) {
					setAhead(Double.POSITIVE_INFINITY * (angle == turnAngle ? 1 : -1));
				//stop and go ↓	
				} else { 		
					if (enemyEnergy > e.getEnergy() && getDistanceRemaining() == 0) {
						setAhead(((3 + (int)(enemyFirePower * 1.999999)) << 3 ) * (angle == turnAngle ? 1 : -1));
					}	
				}
				// Hit the brake pedal hard if we need to turn sharply
				setMaxVelocity(Math.abs(getTurnRemaining()) > 33 ? 2 : MAX_VELOCITY);
 
			if (tries < 30 + i * 5) {
				break;
			}	
			// dive protection ↓
			direction *= -1;
			lastDeltaT = deltaT;
			lastReverseTime = getTime();
		}	
		enemyEnergy = e.getEnergy();
		//gun ↓
 
		if (enemyVelocity != 0) {
			lateralDirection = GFTUtils.sign(enemyVelocity * Math.sin(e.getHeadingRadians() - enemyAbsoluteBearing));
		}
		GFTWave wave = new GFTWave(this);
 
		wave.bulletPower = Math.min(Math.min(e.getEnergy()/4, getEnergy()/8), Math.min(0.05 * Math.round((1.5+200/enemyDistance)/0.05), 2.95));
		wave.MAX_ESCAPE_ANGLE = (Math.asin(8/(20 - 3 * wave.bulletPower)));
		wave.BIN_WIDTH = wave.MAX_ESCAPE_ANGLE / wave.MIDDLE_BIN;
		wave.gunLocation = new Point2D.Double(getX(), getY());
		GFTWave.targetLocation = GFTUtils.project(wave.gunLocation, enemyAbsoluteBearing, enemyDistance);
		wave.lateralDirection = lateralDirection;
		wave.setSegmentations(enemyDistance, enemyVelocity, lastEnemyVelocity);
		lastEnemyVelocity = enemyVelocity;
		wave.bearing = enemyAbsoluteBearing;
 
		setTurnGunRightRadians(Utils.normalRelativeAngle(enemyAbsoluteBearing - getGunHeadingRadians() + wave.mostVisitedBearingOffset()) + ((Math.random()/500)-0.002));
		if (getEnergy() > 0.4) {
		//fire at will! ↓
			setFire(wave.bulletPower);
		}
		if (getEnergy() >= wave.bulletPower) {
			addCustomEvent(wave);
		}
		//radar ↓
    	setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn));
    }
	//movement ↓
	//random movement  ↓
    void considerChangingDirection() {
		deltaT = getTime() - lastReverseTime;
		double theta;
		theta = 0.5952*(20D - 3D*enemyFirePower)/enemyDistance;
		if (Math.random() > Math.pow(theta, theta)) {
			if (deltaT >= lastDeltaT +2 || deltaT <= lastDeltaT -2) {
				direction *= -1;
				lastDeltaT = deltaT;
				lastReverseTime = getTime();
			}
		}
    }
    RoundRectangle2D fieldRectangle(double margin) {
        return new RoundRectangle2D.Double(margin, margin,
	    getBattleFieldWidth() - margin * 2, getBattleFieldHeight() - margin * 2, 75, 75);
    }
 
    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());
    }
 
	public void onHitByBullet(HitByBulletEvent e) {
		//enemyEnergy += 20 - enemyBulletVelocity;		
		enemyEnergy += 2 * enemyFirePower;
		if ((hits += (5 / enemyBulletVelocity)) > getRoundNum() + 2) {
			bullets++;
		}
    }
 
	public void onBulletHit(BulletHitEvent e) {
		enemyEnergy -= 10;
	}
}
//gun ↓
class GFTWave extends Condition {
	static Point2D targetLocation;
	double bulletPower;
	Point2D gunLocation;
	double bearing;
	double lateralDirection;
	// 1000 is the maximum space away in a 800 by 600 battlefield
	private static final double MAX_DISTANCE = 1000;
	//segment into 5 sections ↓
	private static final int DISTANCE_INDEXES = 5;
	private static final int VELOCITY_INDEXES = 5;
	static final int BINS = 25;
	static final int MIDDLE_BIN = (BINS - 1) / 2;
	static double MAX_ESCAPE_ANGLE;
	static double BIN_WIDTH;
 
	private static int[][][][] statBuffers = new int[DISTANCE_INDEXES][VELOCITY_INDEXES][VELOCITY_INDEXES][BINS];
 
	private int[] buffer;
	private AdvancedRobot robot;
	private double distanceTraveled;
 
	GFTWave(AdvancedRobot _robot) {
		this.robot = _robot;
	}
	public boolean test() {
		advance();
		//remove passed waves
		if (hasArrived()) {
			buffer[currentBin()]++;
			robot.removeCustomEvent(this);
		}
		return false;
	}
	double mostVisitedBearingOffset() {
		return (lateralDirection * BIN_WIDTH) * (mostVisitedBin() - MIDDLE_BIN);
	}
	void setSegmentations(double distance, double velocity, double lastVelocity) {
		int distanceIndex = (int)(distance / (MAX_DISTANCE / DISTANCE_INDEXES));
		int velocityIndex = (int)Math.abs(velocity / 2);
		int lastVelocityIndex = (int)Math.abs(lastVelocity / 2);
 
		buffer = statBuffers[distanceIndex][velocityIndex][lastVelocityIndex];
	}
	private void advance() {
		distanceTraveled += GFTUtils.bulletVelocity(bulletPower);
	}
	private boolean hasArrived() {
		return distanceTraveled > gunLocation.distance(targetLocation) - 18;
	}
	private int currentBin() {
		int bin = (int)Math.round(((Utils.normalRelativeAngle(GFTUtils.absoluteBearing(gunLocation, targetLocation) - bearing)) /
				(lateralDirection * BIN_WIDTH)) + MIDDLE_BIN);
		return GFTUtils.minMax(bin, 0, BINS - 1);
	}
	private int mostVisitedBin() {
		int mostVisited = MIDDLE_BIN;
		for (int i = 0; i < BINS; i++) {
			if (buffer[i] > buffer[mostVisited]) {
				mostVisited = i;
			}
		}
		return mostVisited;
	}	
}
//helpful utilities ↓
class GFTUtils {
	static double bulletVelocity(double power) {
		return 20 - 3 * power;
	}
	static Point2D project(Point2D sourceLocation, double angle, double length) {
		return new Point2D.Double(sourceLocation.getX() + Math.sin(angle) * length,
				sourceLocation.getY() + Math.cos(angle) * length);
	}
	static double absoluteBearing(Point2D source, Point2D target) {
		return Math.atan2(target.getX() - source.getX(), target.getY() - source.getY());
	}
	static int sign(double v) {
		return v < 0 ? -1 : 1;
	}
	static int minMax(int v, int min, int max) {
		return Math.max(min, Math.min(max, v));
	}
}
Personal tools