// All combat units (Soldiers, Bashers, Tanks, Drones, Launchers, Commander) private static void runCombat() { while (true) { threats.update(); myLoc = rc.getLocation(); // Move if we can and want to if (rc.isCoreReady()) { boolean ignoreThreat = overwhelms(); if (!ignoreThreat && shouldRetreat()) { if (rc.isWeaponReady() && myType.loadingDelay == 0) attackWeakest(); doRetreatMove(); // Pull back if in range of the enemy guns } else { boolean engaged = false; if (rc.isCoreReady() && inCombat(4)) engaged = doCloseWithEnemyMove(ignoreThreat); if (rc.isCoreReady() && !engaged) // Close with enemy might not do a move if the enemy is a drone out of // reach doAdvanceMove(); } } // Attack if there is an enemy in sight if (myType == RobotType.LAUNCHER) doLaunch(); else if (rc.isWeaponReady()) attackWeakest(); doTransfer(); rc.yield(); } }
// Drones private static void runDrone() { moveDir = Direction.NORTH; droneMoveCurrent = 1; droneMoveMax = 2; patrolClockwise = true; droneCentred = false; // We haven't made it to the centre of our spiral yet while (true) { threats.update(); myLoc = rc.getLocation(); // Attack if there is an enemy in sight if (rc.isWeaponReady()) attackWeakest(); // Move if we can and want to if (rc.isCoreReady()) { if (shouldRetreat()) { doRetreatMove(); // Pull back if in range of the enemy guns } else if (Clock.getRoundNum() < 600) { doPatrol(); } else { doSupply(); } } doTransfer(); rc.yield(); } }
private static void runTower() { while (true) { threats.update(); // Attack if there is an enemy in sight if (rc.isWeaponReady()) attackWeakest(); rc.yield(); } }
// Factories and supply depots private static void runBuilding() { // Most builds spawn units if (myType.canSpawn()) strategy = new BuildStrategy(rc); while (true) { if (rc.isCoreReady() && myType.canSpawn()) { threats.update(); RobotType build = strategy.getBuildOrder(); if (build != null) trySpawn(rc.getLocation().directionTo(threats.enemyHQ), build); } doTransfer(); rc.yield(); } }
// Beavers private static void runBeaver() { strategy = new BuildStrategy(rc); rand = new Random(rc.getID()); while (true) { threats.update(); myLoc = rc.getLocation(); if (rc.isCoreReady()) { RobotType build = strategy.getBuildOrder(); if (build != null) tryBuild(rc.getLocation().directionTo(threats.enemyHQ), build); } // Attack if there is an enemy in sight if (rc.isWeaponReady()) attackWeakest(); double ore = rc.senseOre(rc.getLocation()); // Move if we can and want to if (rc.isCoreReady()) { boolean ignoreThreat = overwhelms(); if (!ignoreThreat && shouldRetreat()) { doRetreatMove(); // Pull back if in range of the enemy guns } else { doMinerMove(); if (ore == 0 && rc.isCoreReady()) { // We didn't find ore nearby doSearchMove(); } } } // Mine if possible if (rc.isCoreReady() && ore > 0) { try { rc.mine(); } catch (GameActionException e) { System.out.println("Mining Exception"); // e.printStackTrace(); } } doTransfer(); rc.yield(); } }
// Computers private static void runOther() { int numTowers = -1; while (true) { threats.update(); myLoc = rc.getLocation(); // Move if we can and want to if (rc.isCoreReady() && myType.canMove()) { if (shouldRetreat()) doRetreatMove(); // Pull back if in range of the enemy guns else doAdvanceMove(); // Move towards our HQ } doTransfer(); // Perform a background breadth first search to the enemy HQ if (myType == RobotType.COMPUTER && Clock.getBytecodesLeft() > 1000) { bfs.work(threats.enemyHQ, Bfs.PRIORITY_HIGH, 1000, numTowers != threats.enemyTowers.length); } numTowers = threats.enemyTowers.length; rc.yield(); } }
// HQ is responsible for collating unit counts and broadcasting them each turn // It also needs to pass on its supply each turn and fire if there are enemies in range private static void runHQ() { // double lastOre = 500; strategy = new BuildStrategy(rc); while (true) { threats.update(); strategy.broadcast(); // double oreIncome = rc.getTeamOre() - lastOre + strategy.oreSpent(); // System.out.println("Ore income " + oreIncome + " per miner = " + (oreIncome-5) / // (strategy.units(RobotType.MINER) + strategy.units(RobotType.BEAVER))); // See if we need to spawn a beaver if (rc.isCoreReady()) { RobotType build = strategy.getBuildOrder(); if (build != null) { trySpawn(rc.getLocation().directionTo(threats.enemyHQ), build); } } // Attack if there is an enemy in sight if (rc.isWeaponReady()) { int senseRange = RobotType.HQ.attackRadiusSquared; MapLocation[] myTowers = rc.senseTowerLocations(); if (myTowers.length >= 5) { senseRange = 52; // This is slightly larger than the real range but does include all possible // enemies that can be hit with splash attackRange = GameConstants.HQ_BUFFED_ATTACK_RADIUS_SQUARED; } else if (myTowers.length >= 2) { attackRange = GameConstants.HQ_BUFFED_ATTACK_RADIUS_SQUARED; } else { attackRange = senseRange; } RobotInfo[] enemies = rc.senseNearbyRobots(senseRange, enemyTeam); // Pick the first valid target MapLocation best = null; for (RobotInfo e : enemies) { int range = e.location.distanceSquaredTo(myLoc); if (range <= attackRange) { best = e.location; break; } if (myTowers.length >= 5) { // Check for tiles adjacent to the enemy as they might be in splash range Direction d = e.location.directionTo(myLoc); if (e.location.add(d).distanceSquaredTo(myLoc) <= attackRange) { best = e.location.add(d); break; } if (e.location.add(d.rotateLeft()).distanceSquaredTo(myLoc) <= attackRange) { best = e.location.add(d.rotateLeft()); break; } if (e.location.add(d.rotateRight()).distanceSquaredTo(myLoc) <= attackRange) { best = e.location.add(d.rotateRight()); break; } } } if (best != null) { try { rc.attackLocation(best); } catch (GameActionException e) { System.out.println("HQ attack exception"); // e.printStackTrace(); } } } doTransfer(); // lastOre = rc.getTeamOre(); rc.yield(); } }