package bayen; import java.awt.geom.Point2D; import robocode.AdvancedRobot; import robocode.ScannedRobotEvent; import robocode.util.Utils; public class UbaRamLT extends AdvancedRobot{ public void run(){ setAdjustRadarForGunTurn(true); setAdjustGunForRobotTurn(true); do{ turnRadarRightRadians(Double.POSITIVE_INFINITY); } while(true); } public void onScannedRobot(ScannedRobotEvent e){ double bulletPower = Math.min(3.0,getEnergy()); double myX = getX(); double myY = getY(); 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 enemyVelocity = e.getVelocity(); double deltaTime = 0; double battleFieldHeight = getBattleFieldHeight(), battleFieldWidth = getBattleFieldWidth(); double predictedX = enemyX, predictedY = enemyY; while((++deltaTime) * (8) < Point2D.Double.distance(myX, myY, predictedX, predictedY)){ predictedX += Math.sin(enemyHeading) * enemyVelocity; predictedY += Math.cos(enemyHeading) * enemyVelocity; 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())); deltaTime = 0; predictedX = enemyX; predictedY = enemyY; setTurnRadarRightRadians(Utils.normalRelativeAngle(absoluteBearing - getRadarHeadingRadians())); setTurnRightRadians(Utils.normalRelativeAngle(theta - getHeadingRadians())); setAhead(Double.POSITIVE_INFINITY); while((++deltaTime) * (11) < Point2D.Double.distance(myX, myY, predictedX, predictedY)){ predictedX += Math.sin(enemyHeading) * enemyVelocity; predictedY += Math.cos(enemyHeading) * enemyVelocity; 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; } } theta = Utils.normalAbsoluteAngle(Math.atan2(predictedX - getX(), predictedY - getY())); setTurnGunRightRadians(Utils.normalRelativeAngle(theta - getGunHeadingRadians())); if(e.getDistance() < 200) fire(3); } }
The first improvement I should do, is to do something like BackAsFront. Instead of sometimes turning more than 90 degrees at start, you then take the shortest path to where you want to go. -- GrubbmGait