public static void fire(RobotController rc) { int radius; try { if (rc.getType() == RobotType.HQ) { radius = 15; Robot[] enemies = rc.senseNearbyGameObjects(Robot.class, radius, rc.getTeam().opponent()); Direction[] dirs = Direction.values(); Robot target = null; int maxValue = 0; for (int k = 0; k < enemies.length; k++) { MapLocation loc = rc.senseRobotInfo(enemies[k]).location; int value = 2; for (int a = 0; a < 8; a++) { try { if (rc.senseObjectAtLocation(loc.add(dirs[a])).getTeam() == rc.getTeam().opponent()) { value++; } else if (rc.senseObjectAtLocation(loc.add(dirs[a])).getTeam() == rc.getTeam()) { value--; } } catch (Exception e) { e.printStackTrace(); } } rc.setIndicatorString(0, "" + value); if (value > maxValue) { maxValue = value; target = enemies[k]; } } if (target != null) { rc.attackSquare(rc.senseRobotInfo(target).location); } } else { radius = 10; Robot[] enemies = rc.senseNearbyGameObjects(Robot.class, radius, rc.getTeam().opponent()); Robot target = null; for (int k = 0; k < enemies.length; k++) { if (target == null) { target = enemies[k]; } else if (rc.senseRobotInfo(enemies[k]).health < rc.senseRobotInfo(target).health) { target = enemies[k]; } } if (target != null) { rc.attackSquare(rc.senseRobotInfo(target).location); } } } catch (Exception e) { e.printStackTrace(); } }
private static void shallWeAllIn() throws GameActionException { if (rc.senseEnemyNukeHalfDone() && Clock.getRoundNum() < 300) { rc.broadcast(Constants.attackChannel, Constants.attackAllIn); return; } int massedRobos = 0; double massedAmountNeeded = .5 * (40 + (10 * gencount) - (1 * othercount)); if (rc.senseEnemyNukeHalfDone()) massedAmountNeeded -= 10; int rallyRadius = 33; if (massedAmountNeeded > 50) rallyRadius = 63; Robot[] robos = rc.senseNearbyGameObjects(Robot.class, findRallyPoint(), rallyRadius, rc.getTeam()); for (Robot r : robos) { if (rc.senseRobotInfo(r).type == RobotType.SOLDIER) { ++massedRobos; } } if (massedRobos > massedAmountNeeded) // if we should all in... { rc.broadcast(Constants.attackChannel, Constants.attackAllIn); allInRound = Clock.getRoundNum(); } }
public static boolean containsNoiseTower(Robot[] robots, RobotController rc) throws GameActionException { for (int i = robots.length; i-- > 0; ) { if (rc.senseRobotInfo(robots[i]).type == RobotType.NOISETOWER) return true; } return false; }
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; }
public static boolean doWeNeedGenerator() throws GameActionException { if (rc.readBroadcast(Constants.campChannel) == Constants.campGenInProduction) return false; if (Clock.getRoundNum() - allInRound < 80) return false; if (rc.readBroadcast(Constants.commandChannel) == Constants.commandRally && rc.getTeamPower() > powerThreshold) return false; if (rc.getTeamPower() < minPowerThreshold && Clock.getRoundNum() > minRoundThreshold) { return true; } gencount = 0; soldiercount = 0; othercount = 0; Robot[] robos = rc.senseNearbyGameObjects(Robot.class, new MapLocation(0, 0), 1000000, rc.getTeam()); for (Robot r : robos) { RobotType rt = rc.senseRobotInfo(r).type; if (rt == RobotType.GENERATOR) ++gencount; else if (rt == RobotType.SOLDIER) ++soldiercount; else ++othercount; } double decay = .8; if (rc.hasUpgrade(Upgrade.FUSION)) { decay = .99; } if ((40 + (10 * gencount) - (1.6 * soldiercount) - (1 * othercount)) * decay < 1) { return true; } return false; }
public AttackStrategy(RobotController rc, Random rand) throws GameActionException { this.rc = rc; this.ENEMY = rc.getTeam().opponent(); this.INFO = rc.senseRobotInfo(rc.getRobot()); this.rand = rand; wanderingDirection = null; }
public SpawnStrategy(RobotController rc) throws GameActionException { this.rc = rc; this.info = rc.senseRobotInfo(rc.getRobot()); this.hqLocation = rc.senseHQLocation(); this.directionToEnemy = this.hqLocation.directionTo(rc.senseEnemyHQLocation()); // this.primarySpawnLocation = hqLocation.add(directionToEnemy); this.enemyTeam = this.info.team.opponent(); }
public static RobotInfo[] senseAllInfos(Robot[] bots, RobotController rc) throws GameActionException { RobotInfo[] ret = new RobotInfo[bots.length]; for (int i = bots.length; i-- > 0; ) { ret[i] = rc.senseRobotInfo(bots[i]); } return ret; }
public static boolean containsConstructingRobotOrNoiseTower(Robot[] robots, RobotController rc) throws GameActionException { for (int i = robots.length; i-- > 0; ) { RobotInfo info = rc.senseRobotInfo(robots[i]); if (info.type == RobotType.NOISETOWER || info.isConstructing) return true; } return false; }
public static boolean containsNonConstructingSoldier(Robot[] robots, RobotController rc) throws GameActionException { for (int i = robots.length; i-- > 0; ) { RobotInfo info = rc.senseRobotInfo(robots[i]); if (info.type == RobotType.SOLDIER && !info.isConstructing) return true; } return false; }
public static int countNonConstructingSoldiers(Robot[] robots, RobotController rc) throws GameActionException { int ret = 0; for (int i = robots.length; i-- > 0; ) { RobotInfo info = rc.senseRobotInfo(robots[i]); if (info.type == RobotType.SOLDIER && !info.isConstructing) ret++; } return ret; }
private static MapLocation findClosestRobot(Robot[] enemyRobots) throws GameActionException { int closestDist = 1000000; MapLocation closestEnemy = null; for (int i = 0; i < enemyRobots.length; i++) { Robot arobot = enemyRobots[i]; RobotInfo arobotInfo = rc.senseRobotInfo(arobot); int dist = arobotInfo.location.distanceSquaredTo(rc.getLocation()); if (dist < closestDist) { closestDist = dist; closestEnemy = arobotInfo.location; } } return closestEnemy; }
public Direction fleeWeighted(Robot[] en) throws GameActionException { Robot[] allies = sensor.getBots(Sensor.SENSE_RANGE_ALLIES); int yDir = 0; int xDir = 0; int numEnemies = en.length; int numAllies = allies.length; for (Robot e : en) { MapLocation eLoc = rc.senseRobotInfo(e).location; xDir -= eLoc.x; yDir -= eLoc.y; } for (Robot a : allies) { MapLocation aLoc = rc.senseRobotInfo(a).location; xDir += aLoc.x; yDir += aLoc.y; } xDir += numEnemies * cur.x - numAllies * cur.x; yDir += numEnemies * cur.y - numAllies * cur.y; Direction desired = cur.directionTo(cur.add(xDir, yDir)); return m.smartMove(desired); }
public static MapLocation closestNonConstructingSoldier( Robot[] robots, MapLocation here, RobotController rc) throws GameActionException { MapLocation ret = null; int bestDistSq = 999999; for (int i = robots.length; i-- > 0; ) { RobotInfo info = rc.senseRobotInfo(robots[i]); if (info.type != RobotType.SOLDIER || info.isConstructing) continue; MapLocation loc = info.location; int distSq = loc.distanceSquaredTo(here); if (distSq < bestDistSq) { bestDistSq = distSq; ret = loc; } } return ret; }
private static void runSoldier() throws GameActionException { // follow orders from HQ // Direction towardEnemy = rc.getLocation().directionTo(rc.senseEnemyHQLocation()); // BasicPathing.tryToMove(towardEnemy, true, rc, directionalLooks, allDirections);//was // Direction.SOUTH_EAST Robot[] enemyRobots = rc.senseNearbyGameObjects(Robot.class, 10000, rc.getTeam().opponent()); if (enemyRobots.length > 0) { // if there are enemies rc.setIndicatorString(0, "There are enemies"); MapLocation[] robotLocations = new MapLocation[enemyRobots.length]; for (int i = 0; i < enemyRobots.length; i++) { Robot anEnemy = enemyRobots[i]; RobotInfo anEnemyInfo = rc.senseRobotInfo(anEnemy); robotLocations[i] = anEnemyInfo.location; } MapLocation closestEnemyLoc = VectorFunctions.findClosest(robotLocations, rc.getLocation()); if (closestEnemyLoc.distanceSquaredTo(rc.getLocation()) < rc.getType().attackRadiusMaxSquared) { rc.setIndicatorString(1, "trying to shoot"); if (rc.isActive()) { rc.attackSquare(closestEnemyLoc); } } else { rc.setIndicatorString(1, "trying to go closer"); Direction towardClosest = rc.getLocation().directionTo(closestEnemyLoc); simpleMove(towardClosest); } } else { if (path.size() == 0) { MapLocation goal = getRandomLocation(); path = BreadthFirst.pathTo( VectorFunctions.mldivide(rc.getLocation(), bigBoxSize), VectorFunctions.mldivide(rc.senseEnemyHQLocation(), bigBoxSize), 100000); } // follow breadthFirst path Direction bdir = BreadthFirst.getNextDirection(path, bigBoxSize); BasicPathing.tryToMove(bdir, true, rc, directionalLooks, allDirections); } // Direction towardEnemy = rc.getLocation().directionTo(rc.senseEnemyHQLocation()); // simpleMove(towardEnemy); }
public static int[][] populate5by5board() throws GameActionException { MapLocation myLoc = rc.getLocation(); int[][] array = new int[5][5]; Robot[] nearbyRobots = rc.senseNearbyGameObjects(Robot.class, 8); // rc.setIndicatorString(2, "number of bots: "+nearbyRobots.length); for (int i = nearbyRobots.length; --i >= 0; ) { Robot aRobot = nearbyRobots[i]; RobotInfo info = rc.senseRobotInfo(aRobot); int[] index = locToIndex(myLoc, info.location, 2); if (index[0] >= 0 && index[0] <= 4 && index[1] >= 0 && index[1] <= 4) { if (info.type != RobotType.SOLDIER) { array[index[0]][index[1]] = 100; } } } return array; }
public SoldierRobot(RobotController rc) throws GameActionException { super(rc); NavSystem.init(this); if (Clock.getRoundNum() < 10) { soldierState = soldierState.SCOUTING; } else { ChannelType channel = EncampmentJobSystem.findJob(); if (channel != null) { unassigned = false; EncampmentJobSystem.updateJobTaken(); } // TODO: this will f**k up if we ever build artillery for non-nuke bots // LEARN THE STRATEGY Message message = BroadcastSystem.read(ChannelType.STRATEGY); if (message.isValid && message.body < Strategy.values().length && message.body >= 0) { strategy = Strategy.values()[message.body]; } else { // we couldn't read the strategy channel MapLocation[] alliedEncampmentSquares = rc.senseAlliedEncampmentSquares(); if (alliedEncampmentSquares.length == 0) { strategy = Strategy.ECON; } else { Robot robot = (Robot) rc.senseObjectAtLocation(alliedEncampmentSquares[0]); RobotInfo robotInfo = rc.senseRobotInfo(robot); if (robotInfo.type == RobotType.ARTILLERY) { strategy = Strategy.NUKE; } else { strategy = Strategy.ECON; } } } // rc.setIndicatorString(2, strategy.toString()); rallyPoint = findRallyPoint(); rSquared = DataCache.rushDistSquared / 4; initializeMining(); } }
public static void run(RobotController myRC) { rc = myRC; barracks = findBarracks(); while (true) { try { if (rc.getType() == RobotType.SOLDIER) { Robot[] enemyRobots = rc.senseNearbyGameObjects( Robot.class, 50000, rc.getTeam().opponent()); // list of enemy robots if (enemyRobots.length == 0) { Clock.getRoundNum(); // gets the round number if (Clock.getRoundNum() < 250) { goToLocation( barracks); // if you are before round 250 travel to the "barracks" or meeting // place. } else { goToLocation(rc.senseEnemyHQLocation()); } } else { // else attack the closest enemy int closestDist = 50000; MapLocation closestEnemy = null; for (int i = 0; i < enemyRobots.length; i++) { Robot enemyBot = enemyRobots[i]; RobotInfo arobotInfo = rc.senseRobotInfo(enemyBot); int dist = arobotInfo.location.distanceSquaredTo(rc.getLocation()); if (dist < closestDist) { closestDist = dist; closestEnemy = arobotInfo.location; } } goToLocation(closestEnemy); } } else { HqCommand(); } } catch (Exception e) { e.printStackTrace(); } rc.yield(); // end turn } }
protected void transferEnergon() throws GameActionException { for (Robot robot : myRC.senseNearbyGroundRobots()) { try { RobotInfo robotInfo = myRC.senseRobotInfo(robot); if (robotInfo.team == myRC.getTeam()) { MapLocation robotLocation = robotInfo.location; if (myRC.getLocation().isAdjacentTo(robotLocation)) myRC.transferEnergon( Math.max( 0, Math.min( robotInfo.maxEnergon - robotInfo.eventualEnergon, myRC.getEnergonLevel() / 2)), robotLocation, RobotLevel.ON_GROUND); } } catch (GameActionException e) { } } }
public static void MoveMapLocation(RobotController rc, MapLocation target, boolean sneak) { MapLocation[] pastLocations = new MapLocation[10]; int side = 45; Direction dir; Direction newDir; rand = new Random(); // we initialize pastLocations to hold our current location 5 times for (int i = 0; i < pastLocations.length; i++) { pastLocations[i] = rc.getLocation(); } // this method will run until we get to our target location while (!rc.getLocation().equals(target)) { // we put the try block inside of the while loop so an exception won't terminate the method try { dir = rc.getLocation().directionTo(target); newDir = Direction.NONE; // simple shoot at an enemy if we see one will need to be improved later Robot[] nearbyEnemies = rc.senseNearbyGameObjects(Robot.class, 10, rc.getTeam().opponent()); if (nearbyEnemies.length > 0 && rc.senseRobotInfo((Robot) nearbyEnemies[0]).health < 201) { fire(rc); } // if we can move towards target and we haven't been on the square recently then lets move else if (rc.canMove(dir) && !MapLocationInArray(rc, rc.getLocation().add(dir), pastLocations)) { newDir = dir; // if we found a direction to move then we go to it if (newDir != Direction.NONE) { // now we decide if we are going to sneak or run if (sneak) { // another check to make sure we don't throw any exceptions if (rc.isActive() && rc.canMove(newDir)) { // System.out.println(newDir); rc.sneak(newDir); } } else { // another check to make sure we don't throw any exceptions if (rc.isActive() && rc.canMove(newDir)) { rc.move(newDir); } } } side = 45; } else { // if their is a robot blocking our way then we just move in a random direction if (rc.senseObjectAtLocation(rc.getLocation().add(dir)) != null) { // newDir = directions[rand.nextInt(8)]; MoveDirection(rc, dir, sneak); } else { // rc.setIndicatorString(2, "Looking elswhere"); Direction dir2 = dir; MapLocation right; MapLocation left; dir2 = (dir.rotateRight()); while (!rc.canMove(dir2)) { dir2 = dir2.rotateRight(); } right = rc.getLocation().add(dir2); dir2 = dir.rotateLeft(); while (!rc.canMove(dir2)) { dir2 = dir2.rotateLeft(); } left = rc.getLocation().add(dir2); // left seems better so lets go that way if (left.distanceSquaredTo(target) < right.distanceSquaredTo(target)) { side = 1; } // right seems better so lets try that way else { side = 0; } // we will go hugging one side of obstacle until we get back on our original line while (!dir2.equals(dir) && !rc.getLocation().equals(target)) // && rc.canMove(dir2)) { try { if (rc.isActive()) { // rc.setIndicatorString(1, "Trying to Avoid"); // rc.setIndicatorString(2, ""+side); dir2 = rc.getLocation().directionTo(target); if (rc.canMove(dir2) && !MapLocationInArray(rc, rc.getLocation().add(dir2), pastLocations)) // && // !rc.senseTerrainTile(rc.getLocation().add(dir2).add(dir2)).equals(TerrainTile.VOID)) { // rc.setIndicatorString(0, "Going straight"); } else { for (int i = 0; i < 4; i++) { if (side == 1) { dir2 = dir2.rotateLeft(); } else { dir2 = dir2.rotateRight(); } if (rc.senseTerrainTile(rc.getLocation().add(dir2)) .equals(TerrainTile.OFF_MAP)) { dir2 = Direction.NONE; i = 48; } else if ((rc.canMove(dir2) || (rc.senseObjectAtLocation(rc.getLocation().add(dir2)) != null))) // && !MapLocationInArray(rc, rc.getLocation().add(dir2), // pastLocations))// && // !rc.senseTerrainTile(rc.getLocation().add(dir2).add(dir2)).equals(TerrainTile.VOID)) { i = 48; } else if (i == 3) { dir2 = Direction.NONE; // rc.setIndicatorString(1, "We failed to find a spot"); } } } // if we can move if (dir2 != Direction.NONE) { if (rc.isActive()) { if (rc.canMove(dir2)) { if (sneak) { rc.sneak(dir2); } else { rc.move(dir2); } } else { MoveDirection(rc, dir2, sneak); } } } else { if (side == 1) { side = 0; } else { side = 1; } } } // rc.setIndicatorString(0, "Dir: "+ dir +" Dir2: " + dir2); } catch (Exception e) { // tell the console we through an exception in utility object for debug purposes // System.out.println("Utility Exception"); // System.out.println(e.toString()); e.printStackTrace(); rc.yield(); } if (!rc.getLocation().equals(pastLocations[(pastLocations.length - 1)])) { for (int j = 0; j < (pastLocations.length - 1); j++) { pastLocations[j] = pastLocations[j + 1]; // System.out.println(pastLocations[j]); } // stick current local into array pastLocations[(pastLocations.length - 1)] = rc.getLocation(); } rc.yield(); } // rc.setIndicatorString(1, "Not trying to Avoid"); } } // now we shift everything up one in pastLocations if (rc.getLocation() != pastLocations[(pastLocations.length - 1)]) { for (int j = 0; j < (pastLocations.length - 1); j++) { pastLocations[j] = pastLocations[j + 1]; // System.out.println(pastLocations[j]); } // stick current local into array pastLocations[(pastLocations.length - 1)] = rc.getLocation(); } rc.yield(); } catch (Exception e) { // tell the console we through an exception in utility object for debug purposes System.out.println("Utility Exception"); e.printStackTrace(); // System.out.println(e.toString()); rc.yield(); } } }
public static void run(RobotController myRC) { rc = myRC; rallyPoint = findRallyPoint(); while (true) { try { if (rc.getType() == RobotType.SOLDIER) { if (rc.senseEncampmentSquare(rc.getLocation())) { if (rc.getTeamPower() > 10 * (rc.senseAlliedEncampmentSquares().length + 10)) { rc.captureEncampment(RobotType.SUPPLIER); } else { rc.captureEncampment(RobotType.GENERATOR); } } else { Robot[] enemyRobots = rc.senseNearbyGameObjects(Robot.class, 100000, rc.getTeam().opponent()); if (enemyRobots.length == 0 || (enemyRobots.length == 1 && rc.senseRobotInfo(enemyRobots[0]).type == RobotType.HQ)) { // no enemies nearby Robot[] alliedRobots = rc.senseNearbyGameObjects(Robot.class, 1000000, rc.getTeam()); if (alliedRobots.length < 5 * (rc.senseAlliedEncampmentSquares().length) + 1) { if (Math.random() < 0.1 && alliedRobots.length < 20) { if (rc.senseMine(rc.getLocation()) == null) { rc.layMine(); } } goToLocation(rallyPoint); } else { goToLocation(rc.senseEnemyHQLocation()); } } else { // someone spotted int closestDist = 100000; MapLocation closestEnemy = null; for (int i = 0; i < enemyRobots.length; i++) { Robot arobot = enemyRobots[i]; RobotInfo arobotInfo = rc.senseRobotInfo(arobot); int dist = arobotInfo.location.distanceSquaredTo(rc.getLocation()); if (dist < closestDist) { closestDist = dist; closestEnemy = arobotInfo.location; } } Robot[] nearbyAllies = rc.senseNearbyGameObjects(Robot.class, 64, rc.getTeam()); if (nearbyAllies.length > 2.5 * enemyRobots.length) { if (closestDist > 4) { goToLocation(closestEnemy); } } goToLocationAvoidMines(closestEnemy); } } } else { hqCode(); } } catch (Exception e) { // System.out.println("caught exception before it killed us:"); // e.printStackTrace(); } rc.yield(); } }
private static void goToLocationSwarm(MapLocation location, boolean defuseMines) throws GameActionException { // Swarming double dxToLocation = location.x - rc.getLocation().x; double dyToLocation = location.y - rc.getLocation().y; double distanceToLocation = Math.sqrt(rc.getLocation().distanceSquaredTo(location)); Robot[] nearbyAlliedRobots = rc.senseNearbyGameObjects(Robot.class, 14, rc.getTeam()); int totalDx = 0; int totalDy = 0; int totalNearbyerDx = 0; int totalNearbyerDy = 0; for (int i = nearbyAlliedRobots.length; --i >= 0; ) { RobotInfo robotInfo = rc.senseRobotInfo(nearbyAlliedRobots[i]); if (robotInfo.type == RobotType.SOLDIER) { MapLocation iterLocation = robotInfo.location; totalDx += (iterLocation.x - rc.getLocation().x); totalDy += (iterLocation.y - rc.getLocation().y); if (rc.getLocation().distanceSquaredTo(iterLocation) <= 5) { // for repelling totalNearbyerDx += (iterLocation.x - rc.getLocation().x); totalNearbyerDy += (iterLocation.y - rc.getLocation().y); } } } double denom = Math.sqrt(totalDx * totalDx + totalDy * totalDy); double nearbyerDenom = Math.sqrt(totalNearbyerDx * totalNearbyerDx + totalNearbyerDy * totalNearbyerDy); double addX, addY; double addXNearbyer, addYNearbyer; if (Math.abs(totalDx) < 0.01) { addX = 0; } else { addX = swarmC * totalDx / denom; } if (Math.abs(totalDy) < 0.01) { addY = 0; } else { addY = swarmC * totalDy / denom; } if (Math.abs(totalNearbyerDx) < 0.01) { addXNearbyer = 0; } else { addXNearbyer = swarmD * totalNearbyerDx / nearbyerDenom; } if (Math.abs(totalNearbyerDy) < 0.01) { addYNearbyer = 0; } else { addYNearbyer = swarmD * totalNearbyerDy / nearbyerDenom; } double finalDx = dxToLocation / distanceToLocation + addX - addXNearbyer; double finalDy = dyToLocation / distanceToLocation + addY - addYNearbyer; // rc.setIndicatorString(0, "totalDx: " + Integer.toString(totalDx) + ", finalDx: " + // Double.toString(finalDx)); // rc.setIndicatorString(1, "totalDy: " + Integer.toString(totalDy) + ", finalDy: " + // Double.toString(finalDy)); int dirOffset; double ratioCutoff = 2.5; double ratio = Math.abs(finalDx / finalDy); if (ratio > ratioCutoff) { // go along x-axis if (finalDx > 0) { dirOffset = 2; } else { dirOffset = 6; } } else if (ratio < 1 / ratioCutoff) { // go along y-axis if (finalDy > 0) { dirOffset = 4; } else { dirOffset = 0; } } else { if (finalDx > 0) { if (finalDy >= 0) { dirOffset = 3; } else { dirOffset = 1; } } else { if (finalDy > 0) { dirOffset = 5; } else { dirOffset = 7; } } } Direction dirToMoveIn = Direction.values()[dirOffset]; if (defuseMines) { NavSystem.goDirectionAndDefuse(dirToMoveIn); } else { NavSystem.goDirectionAvoidMines(dirToMoveIn); } }