User:Ackzar

From Robowiki
Jump to navigation Jump to search

Created a teambot based on Voidius's BlitzBat, heres the code:

import robocode.*; import robocode.util.Utils;

import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.Color; import java.util.Iterator; import java.util.HashMap; import java.util.ArrayList;

public class FFFFFFFUUUUUUUUUUUU extends TeamRobot { static Rectangle2D.Double _fieldRect; static Point2D.Double _destination; static String _targetName; static double _targetDistance; double enemyHeadingOld; double enemyEnergyOld = 100.0; static HashMap _enemies = new HashMap(); static Point2D.Double _myLocation;

public void run() { setAdjustGunForRobotTurn(true); setAdjustRadarForGunTurn(true); setColors(Color.white, Color.white, Color.white, Color.white, Color.white);

_fieldRect = new Rectangle2D.Double(50, 50, getBattleFieldWidth() - 100, getBattleFieldHeight() - 100); _targetDistance = Double.POSITIVE_INFINITY; _destination = null;

turnRadarRightRadians(Double.POSITIVE_INFINITY); do { Point2D.Double myLocation = _myLocation = new Point2D.Double(getX(), getY()); EnemyData targetData = (EnemyData)_enemies.get(_targetName);

///////////////////////////////// // Movement double bestRisk;

try { bestRisk = evalDestinationRisk(_destination) * .85; } catch (NullPointerException ex) { bestRisk = Double.POSITIVE_INFINITY; }

try { for (double d = 0; d < (Math.PI * 2); d += 0.1) { Point2D.Double newDest = project(myLocation, d, Math.min(_targetDistance, 100 + Math.random() * 200)); double thisRisk = evalDestinationRisk(newDest); if (_fieldRect.contains(newDest) && thisRisk < bestRisk) { bestRisk = thisRisk; _destination = newDest; } }

double angle = Utils.normalRelativeAngle( absoluteBearing(myLocation, _destination) - getHeadingRadians()); setTurnRightRadians(Math.tan(angle)); setAhead(Math.cos(angle) * Double.POSITIVE_INFINITY); } catch (NullPointerException ex) {

}

///////////////////////////////// // Radar and execute

scan(); ///////////////////////////////// } while (true); }

public void onScannedRobot(ScannedRobotEvent e) { double radarTurn = getHeadingRadians() + e.getBearingRadians() - getRadarHeadingRadians(); setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn));


double eDistance = e.getDistance();

EnemyData eData = new EnemyData(); eData.setLocation(project(_myLocation, e.getBearingRadians() + getHeadingRadians(), eDistance)); eData.energy = e.getEnergy();


       /////////////////////////////////
       // Gun
       try 
       {		
       	double bulletPower = Math.min(3.0,getEnergy());
       	double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
       	double enemyX = getX() + e.getDistance() * Math.sin(absoluteBearing);
       	double enemyY = getY() + e.getDistance() * Math.cos(absoluteBearing);
       	double enemyHeading = e.getHeadingRadians();
       	double enemyHeadingChange = enemyHeading - enemyHeadingOld;
       	double enemyVelocity = e.getVelocity();
       	enemyHeadingOld = enemyHeading;
       	double deltaTime = 0;
       	double battleFieldHeight = getBattleFieldHeight(), battleFieldWidth = getBattleFieldWidth();
       	double predictedX = enemyX, predictedY = enemyY;
       	while((++deltaTime) * (20.0 - 3.0 * bulletPower) < Point2D.Double.distance(getX(), getY(), predictedX, predictedY))
       	{		
       		predictedX += Math.sin(enemyHeading) * enemyVelocity;
       		predictedY += Math.cos(enemyHeading) * enemyVelocity;
       		enemyHeading += enemyHeadingChange;
       		if(	predictedX < 18.0 || predictedY < 18.0
       				|| predictedX > battleFieldWidth - 18.0
       				|| predictedY > battleFieldHeight - 18.0)
       		{
       			predictedX = Math.min(Math.max(18.0, predictedX), battleFieldWidth - 18.0);	
       			predictedY = Math.min(Math.max(18.0, predictedY), battleFieldHeight - 18.0);
       			break;
       		}

} double theta = Utils.normalAbsoluteAngle(Math.atan2( predictedX - getX(), predictedY - getY()));

setTurnGunRightRadians(Utils.normalRelativeAngle( theta - getGunHeadingRadians()));

       } 

catch (NullPointerException ex) { } if(!isTeammate(e.getName())) setFire(3 - ((20 - getEnergy()) / 6));

_enemies.put(e.getName(), eData);

if (eDistance < _targetDistance || e.getName().equals(_targetName)) { _targetDistance = eDistance; _targetName = e.getName(); } }

public void onRobotDeath(RobotDeathEvent e) { _enemies.remove(e.getName()); _targetDistance = Double.POSITIVE_INFINITY; }

protected double evalDestinationRisk(Point2D.Double d) { double risk = 0;

Iterator enemiesIterator = _enemies.values().iterator(); while (enemiesIterator.hasNext()) { EnemyData ed = (EnemyData)enemiesIterator.next(); double distSq = ed.distanceSq(d); int closer = 0; Iterator enemiesIterator2 = _enemies.values().iterator(); while (enemiesIterator2.hasNext()) { if (ed.distanceSq((EnemyData)enemiesIterator2.next()) < distSq) {

closer++; } }

risk += Math.max(0.5, Math.min(ed.energy / getEnergy(), 2)) * (1 + Math.abs(Math.cos(absoluteBearing(_myLocation, d) - absoluteBearing(_myLocation, ed)))) / closer / distSq / (200000 + d.distanceSq(getBattleFieldWidth() / 2, getBattleFieldHeight() / 2)); }


return risk; }

protected static double absoluteBearing(Point2D.Double source, Point2D.Double target) { return Math.atan2(target.x - source.x, target.y - source.y); }

public static Point2D.Double project(Point2D.Double sourceLocation, double angle, double length) { return new Point2D.Double(sourceLocation.x + Math.sin(angle) * length, sourceLocation.y + Math.cos(angle) * length); } }

class EnemyData extends Point2D.Double { double energy; }