private MapLocation getAverageEnemyArchonLocation(final RobotController robotController) { if (this.averageEnemyArchonLocation == null) { MapLocation[] enemyArchonLocations = robotController.getInitialArchonLocations(robotController.getTeam().opponent()); if (enemyArchonLocations.length == 0) { return null; } int totalX = 0; int totalY = 0; for (int i = 0; i < enemyArchonLocations.length; i++) { totalX += enemyArchonLocations[i].x; totalY += enemyArchonLocations[i].y; } this.averageEnemyArchonLocation = new MapLocation( totalX / enemyArchonLocations.length, totalY / enemyArchonLocations.length); } return this.averageEnemyArchonLocation; }
private void tryClearRubble(Direction direction) throws GameActionException { MapLocation nextLocation = rc.getLocation().add(direction); double rubble = rc.senseRubble(nextLocation); if (rubble > 100) { rc.clearRubble(direction); } }
public static boolean expandOrRally() throws GameActionException // true for expand { // rush distance // double rushDistance = rc.senseHQLocation().distanceSquaredTo(rc.senseEnemyHQLocation()); // number of encampments if (!expandPhase) { singleExpand(); return false; } int numAlliedCamps = rc.senseAlliedEncampmentSquares().length; if (numAlliedCamps > 13) { rc.broadcast(Constants.commandChannel, Constants.commandRally); expandPhase = false; return false; } if (numAlliedCamps >= optimalBuildings) { rc.broadcast(Constants.commandChannel, Constants.commandRally); expandPhase = false; return false; } rc.broadcast(Constants.commandChannel, Constants.commandExpand); return true; }
public static void workOnCache(RobotController rc, int bytecodeLimit) { if (cacheFinished) return; int mapWidth = rc.getMapWidth(); int mapHeight = rc.getMapHeight(); int x, y; if (workStarted) { x = workLocX; y = workLocY; } else { x = mapWidth - 1; y = mapHeight - 1; workStarted = true; } outerloop: while (x >= 0) { while (y >= 0) { terrain[x][y] = rc.senseTerrainTile(new MapLocation(x, y)); y--; // TODO: this is in a tight loop so it would be nice to do this check less often if (Clock.getBytecodeNum() > bytecodeLimit) break outerloop; } y = mapHeight - 1; x--; } workLocX = x; workLocY = y; if (x < 0) cacheFinished = true; }
protected MapLocation[] findEnemy() throws GameActionException { ArrayList<MapLocation> enemyLocations = new ArrayList<MapLocation>(); ArrayList<Robot> allRobots = new ArrayList<Robot>(); Robot[] airRobots = myRC.senseNearbyAirRobots(); Robot[] groundRobots = myRC.senseNearbyGroundRobots(); allRobots.addAll(Arrays.asList(airRobots)); allRobots.addAll(Arrays.asList(groundRobots)); for (Robot robot : allRobots) { try { RobotInfo info = myRC.senseRobotInfo(robot); if (!myRC.getTeam().equals(info.team)) { int distance = myRC.getLocation().distanceSquaredTo(info.location); if (distance < enemyDistance) { enemyDistance = distance; } enemyLocations.add(info.location); } } catch (GameActionException e) { } } MapLocation[] retLocations = new MapLocation[enemyLocations.size()]; retLocations = enemyLocations.toArray(retLocations); return retLocations; }
private static void broadcastUpdatedBuildOrder() throws GameActionException { boolean scrambledEggs = false; // scramble check for (int i = 0; i < buildOrder.length; i++) { int tmp = rc.readBroadcast(Constants.buildOrderBeginChannel + i); if (!(tmp == 0 || tmp == Constants.buildOrderArt || tmp == Constants.buildOrderGen || tmp == Constants.buildOrderSup || tmp == Constants.buildOrderHeal || tmp == Constants.buildOrderShield)) { scrambledEggs = true; break; } } if (!scrambledEggs) { for (int i = 0; i < buildOrder.length; i++) { buildOrder[i] = rc.readBroadcast(Constants.buildOrderBeginChannel + i); } } else { for (int i = 0; i < buildOrder.length; i++) { rc.broadcast(Constants.buildOrderBeginChannel + i, buildOrder[i]); } } }
// This method will attempt to spawn in the given direction (or as close to it as possible) static boolean trySpawn(Direction d, RobotType type) { if (!rc.hasSpawnRequirements(type)) return false; int offsetIndex = 0; int[] offsets = {0, 1, -1, 2, -2, 3, -3, 4}; int dirint = directionToInt(d); while (offsetIndex < 8) { int i = (dirint + offsets[offsetIndex] + 8) % 8; Direction spawn = directions[i]; if (rc.canSpawn(spawn, type) && rc.hasSpawnRequirements(type)) { try { rc.spawn(spawn, type); strategy.addUnit(type); } catch (GameActionException e) { System.out.println("Spawn exception"); // e.printStackTrace(); } return true; } offsetIndex++; } return false; }
private static void flashTowards(MapLocation m, boolean ignoreThreat) { // We want to pick a safe tile that is within flash range (10) and nearest to the destination // If we are allowed to ignore threat store the nearest threatened tile in case there are no // safe ones // We don't bother with moves to adjacent tiles! MapLocation[] inRange = MapLocation.getAllMapLocationsWithinRadiusSq(myLoc, GameConstants.FLASH_RANGE_SQUARED); MapLocation bestSafe = myLoc; MapLocation best = myLoc; // The closest regardless of threat try { for (MapLocation target : inRange) { if (!target.equals(myLoc) && rc.isPathable(myType, target) && !rc.isLocationOccupied(target) && target.distanceSquaredTo(myLoc) > 1) { if (target.distanceSquaredTo(m) < bestSafe.distanceSquaredTo(m) && !threats.isThreatened(target)) bestSafe = target; if (target.distanceSquaredTo(m) < best.distanceSquaredTo(m)) best = target; } } if (!bestSafe.equals(myLoc)) rc.castFlash(bestSafe); else if (ignoreThreat && !best.equals(myLoc)) rc.castFlash(best); } catch (GameActionException e) { System.out.println("Flash exception"); // e.printStackTrace(); } }
public static void run(RobotController theRC) { rc = theRC; // These don't change so get them once and use the local variable (performance) myType = rc.getType(); myTeam = rc.getTeam(); enemyTeam = myTeam.opponent(); myHQ = rc.senseHQLocation(); myLoc = rc.getLocation(); senseRange = myType.sensorRadiusSquared; attackRange = myType.attackRadiusSquared; maxRounds = rc.getRoundLimit(); if (myType == RobotType.MISSILE) runMissile(); if (myType.canMove()) { bfs = new Bfs(rc); // We need to check the breadth first search results to move optimally } threats = new Threats(rc); if (myType == RobotType.HQ) runHQ(); else if (myType == RobotType.TOWER) runTower(); else if (myType.isBuilding) runBuilding(); else if (myType.canBuild()) runBeaver(); else if (myType.canMine()) runMiner(); // Includes Beavers else if (myType == RobotType.DRONE) runDrone(); else if (myType.canAttack() || myType == RobotType.LAUNCHER) runCombat(); else runOther(); }
protected boolean tryBuild(RobotType robotType, Direction initialDirection) throws GameActionException { if (!rc.isCoreReady()) { return false; } if (rc.getTeamParts() < robotType.partCost) { return false; } int startingPosition = getDirectionNumber(initialDirection); if (startingPosition < 0) { startingPosition = 0; } int[] d = {0, 1, 7, 2, 6, 3, 5, 4}; for (int i = 0; i < 8; i++) { Direction direction = directions[(startingPosition + d[i]) % 8]; if (rc.canBuild(direction, robotType)) { rc.build(direction, robotType); return true; } } return false; }
private static void goToLocation(MapLocation whereToGo) throws GameActionException { // if (rc.isActive()) { // if(encampmentLocationArray == null){ // find encampments // encampmentLocationArray = rc.senseAllEncampmentSquares(); // } // if (counter < 10 && rc.senseMine(rc.getLocation()) == null) { // lay mines behind robots // if(rc.senseMine(rc.getLocation())==null) // rc.layMine(); // Send all robots to the passed in argument. int dist = rc.getLocation().distanceSquaredTo(whereToGo); if (dist > 0) { // dist > 0 && rc.isActive() Direction dir = rc.getLocation().directionTo(whereToGo); Direction curDir = dir; int[] directionOffSets = {0, 1, -1, 2, -2}; lookForDir: for (int d : directionOffSets) { curDir = Direction.values()[(dir.ordinal() + d + 8) % 8]; if (rc.canMove(curDir)) { break lookForDir; } } Team mine = rc.senseMine(rc.getLocation().add(curDir)); if (mine != null && mine != rc.getTeam()) { rc.defuseMine(rc.getLocation().add(curDir)); } else { rc.move(curDir); rc.setIndicatorString(0, "Last direction moved: " + dir.toString()); } } }
public static void yield() { if (my_type == RobotType.ARCHON) rc.setIndicatorString(0, "Beta 1.5"); if (rc.getRoundNum() % 25 == 0) Scanner.reset_health(); Clock.yield(); }
// if soldier is in range of stuff but doesn't see it, sets it to null public static void resetLocations(RobotController rc) throws GameActionException { if (nearestTurretLocation != null && myLoc.distanceSquaredTo(nearestTurretLocation) <= 13 && (rc.senseRobotAtLocation(nearestTurretLocation) == null || rc.senseRobotAtLocation(nearestTurretLocation).type != RobotType.TURRET || !rc.senseRobotAtLocation(nearestTurretLocation).team.equals(enemyTeam))) { if (nearestTurretLocation.equals(currentDestination)) currentDestination = null; nearestTurretLocation = null; } if (nearestEnemyLocation != null && myLoc.distanceSquaredTo(nearestEnemyLocation) <= 13 && (rc.senseRobotAtLocation(nearestEnemyLocation) == null || !rc.senseRobotAtLocation(nearestEnemyLocation).team.equals(enemyTeam))) { if (nearestEnemyLocation.equals(currentDestination)) currentDestination = null; nearestEnemyLocation = null; } if (nearestDenLocation != null && rc.canSense(nearestDenLocation) && (rc.senseRobotAtLocation(nearestDenLocation) == null || rc.senseRobotAtLocation(nearestDenLocation).type != RobotType.ZOMBIEDEN)) { if (nearestDenLocation.equals(currentDestination)) currentDestination = null; nearestDenLocation = null; } if (nearestDistressedArchon != null && myLoc.distanceSquaredTo(nearestDistressedArchon) <= 13 && (rc.senseRobotAtLocation(nearestDistressedArchon) == null || !rc.senseRobotAtLocation(nearestDistressedArchon).team.equals(enemyTeam)) || distressedArchonTurns > 5) { if (nearestDistressedArchon != null && nearestDistressedArchon.equals(currentDestination)) currentDestination = null; nearestDistressedArchon = null; distressedArchonTurns = 0; } }
public static MapLocation spotOfTrollTower(RobotController rc, MapLocation pastr) { MapLocation target = pastr; int width = rc.getMapWidth(); int height = rc.getMapHeight(); int dist = 350; if (pastr.x < width / 2) { while (target.distanceSquaredTo(pastr) < dist && target.x > 0) { target = target.add(Direction.WEST); } } else { while (target.distanceSquaredTo(pastr) < dist && target.x < rc.getMapWidth() - 1) { target = target.add(Direction.EAST); } } if (pastr.y < height / 2) { while (target.distanceSquaredTo(pastr) < dist && target.y < height - 1) { target = target.add(Direction.SOUTH); } } else { while (target.distanceSquaredTo(pastr) < dist && target.y > 0) { target = target.add(Direction.NORTH); } } while (rc.senseTerrainTile(target) == TerrainTile.VOID && target.x < width / 2) { target = target.add(Direction.EAST); } while (rc.senseTerrainTile(target) == TerrainTile.VOID && target.x > width / 2) { target = target.add(Direction.WEST); } return target; }
static void activateCommon(RobotController rc, MapLocation loc) throws GameActionException { rc.activate(loc); lastBuiltLocation = loc; RobotInfo info = rc.senseRobotAtLocation(lastBuiltLocation); addInfo(info); lastBuiltId = info.ID; }
public static MapLocation spotOfPastr(RobotController rc) { MapLocation target; int[] lookPlaces = {1, 1, 0, 3, 6, 7, 4, 5, 2, 3, 0, 2, 2, 3, 1, 4, 5, 3, 2, 5, 6}; int counter = 0; Direction dir; int corner = findBestCorner(rc); rand = new Random(); switch (corner) { case 1: target = new MapLocation(2, 2); break; case 2: target = new MapLocation(rc.getMapWidth() - 3, 2); break; case 3: target = new MapLocation(2, rc.getMapHeight() - 3); break; default: target = new MapLocation(rc.getMapWidth() - 3, rc.getMapHeight() - 3); break; } while (rc.senseTerrainTile(target).equals(TerrainTile.VOID)) { dir = directions[rand.nextInt(8)]; target = target.add(dir); counter++; } return target; }
private static void goToLocationAvoidMines(MapLocation whereToGo) throws GameActionException { int dist = rc.getLocation().distanceSquaredTo(whereToGo); if (dist > 0 && rc.isActive()) { Direction dir = rc.getLocation().directionTo(whereToGo); goDirectionAvoidMines(dir); } }
public static void fireCircle(RobotController rc, int radius, MapLocation center) { for (int k = 0; k < directions.length; k++) { while (!rc.isActive()) {} MapLocation toFire = center.add(directions[k], radius); try { if (toFire.x >= 0 && toFire.x < rc.getMapWidth() && toFire.y >= 0 && toFire.y < rc.getMapHeight()) { rc.attackSquare(toFire); rc.yield(); } } catch (Exception e) { } while (!rc.isActive()) {} toFire = center; for (int a = 0; a < radius / 2; a++) { toFire = toFire.add(directions[k]); toFire = toFire.add(directions[(k + 1) % directions.length]); } try { if (toFire.x >= 0 && toFire.x < rc.getMapWidth() && toFire.y >= 0 && toFire.y < rc.getMapHeight()) { rc.attackSquare(toFire); rc.yield(); } } catch (Exception e) { } } }
public static void hqCode() throws GameActionException { if (rc.isActive()) { // Spawn a soldier Direction dir = rc.getLocation().directionTo(rc.senseEnemyHQLocation()); if (rc.canMove(dir)) rc.spawn(dir); } }
// 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(); } }
// TODO this method needs a lot of work...isn't doing things properly even if there's just an // archon making bots with open space around it. Add onto that later, when there are more bots // around, // others will need to move forward in order for those just next to the archon to move. private static void moveAwayFromArchons(RobotController rc) throws GameActionException { int mySenseRadius = rc.getType().sensorRadiusSquared; int archonReserveDistance = 9; // @Hope make this more finessed archonReserveDistance = archonReserveDistance < mySenseRadius ? archonReserveDistance : mySenseRadius; List<RobotInfo> robots = Arrays.asList(rc.senseNearbyRobots(archonReserveDistance, myTeam)); List<RobotInfo> archons = new ArrayList<>(); for (RobotInfo robot : robots) { if (robot.type == RobotType.ARCHON) { archons.add(robot); } } boolean tooClose = false; MapLocation myLocation = rc.getLocation(); for (RobotInfo archon : archons) { if (!tooClose && myLocation.distanceSquaredTo(archon.location) < archonReserveDistance) { tooClose = true; } } if (tooClose) { for (RobotInfo archon : archons) { if (rc.isCoreReady()) moveTowards(rc, rc.getLocation().directionTo(archon.location).opposite()); } } }
public static MapLocation spotOfSensorTower(RobotController rc) { rand = new Random(); MapLocation target = null; int corner = Utilities.findBestCorner(rc); switch (corner) { case 1: target = new MapLocation(5, 5); break; case 2: target = new MapLocation(rc.getMapWidth() - 6, 5); break; case 3: target = new MapLocation(5, rc.getMapHeight() - 6); break; default: target = new MapLocation(rc.getMapWidth() - 6, rc.getMapHeight() - 6); break; } Direction dir = directions[rand.nextInt(8)]; // make sure we don't try to build on a void space while (rc.senseTerrainTile(target).equals(TerrainTile.VOID)) { target = target.add(dir); dir = directions[rand.nextInt(8)]; } return target; }
private static void leadZombiesToEnemy(RobotController rc) throws GameActionException { // TODO stop anti-kiting if (rc.isCoreReady()) { if (rc.canMove(Direction.SOUTH_WEST)) { rc.move(Direction.SOUTH_WEST); } } }
/** * Goes through a list of adjacent locations. If a wall is detected, robot tries to move away from * it. * * @param rc * @throws GameActionException */ private static void moveAwayFromWalls(RobotController rc) throws GameActionException { MapLocation myLocation = rc.getLocation(); for (Direction dir : DIRECTIONS) { if (!rc.onTheMap(myLocation.add(dir)) && rc.isCoreReady()) { moveTowards(rc, dir.opposite()); } } }
/** * helper fcn to compute if location contains a bad bomb * * @param rc * @param loc * @return */ private static boolean badBomb(MapLocation loc) { Team isBomb = rc.senseMine(loc); if (isBomb == null || isBomb == rc.getTeam()) { return false; } else { return true; } }
private static MapLocation findRallyPoint() { MapLocation enemyLoc = rc.senseEnemyHQLocation(); MapLocation ourLoc = rc.senseHQLocation(); int x = (enemyLoc.x + 3 * ourLoc.x) / 4; int y = (enemyLoc.y + 3 * ourLoc.y) / 4; MapLocation rallyPoint = new MapLocation(x, y); return rallyPoint; }
private static MapLocation findBarracks() { MapLocation ourLoc = rc.senseHQLocation(); MapLocation enemyLoc = rc.senseEnemyHQLocation(); int x = (enemyLoc.x + 3 * ourLoc.x) / 4; // makes the meeting place 1/4 of the way to the enemy. int y = (enemyLoc.y + 3 * ourLoc.y) / 4; MapLocation barracks = new MapLocation(x, y); return barracks; }
private static void moveOrDefuse(Direction dir) throws GameActionException { MapLocation ahead = rc.getLocation().add(dir); if (rc.senseMine(ahead) != null) { rc.defuseMine(ahead); } else { rc.move(dir); } }
static void buildCommon(RobotController rc, Direction dir, RobotType robotType) throws GameActionException { rc.build(dir, robotType); lastBuiltLocation = rc.getLocation().add(dir); RobotInfo info = rc.senseRobotAtLocation(lastBuiltLocation); addInfo(info); lastBuiltId = info.ID; }
/** * Repairs the first-sensed neutral robot, if rc's core is ready * * @param rc must be an archon * @throws GameActionException */ private static boolean activateFirst(RobotController rc) throws GameActionException { boolean hasActed = false; RobotInfo[] nearbyNeutralRobots = rc.senseNearbyRobots(ONE_SQUARE_RADIUS, Team.NEUTRAL); if (nearbyNeutralRobots.length > 0) { rc.activate(nearbyNeutralRobots[0].location); hasActed = true; } return hasActed; }