/** * If rc finds a zombie den, it signals out to surrounding robots If rc has no weapon delay, * attacks in the following priority: 1) adjacent robots (if robot is a bigzombie or standard * zombie move away every other turn to kite it) 2) big zombies 3) nearest enemy * * @param rc RobotController which will attack * @param RobotController * @throws GameActionException * @return true if this robot attacked else false */ private static void attackFirst(RobotController rc) throws GameActionException { boolean equalHealth = true; int lowestHealthIndex = -1; int lowestDistanceIndex = -1; int attackIndex = -1; RobotInfo[] enemies = rc.senseHostileRobots(rc.getLocation(), rc.getType().attackRadiusSquared); if (rc.isWeaponReady() && enemies.length > 0) { for (int i = 0; i < enemies.length; i++) { if (enemies[i].type == RobotType.ZOMBIEDEN) { rc.broadcastSignal(rc.getType().sensorRadiusSquared * 2); } if (attackIndex < 0 && (rc.getLocation()).isAdjacentTo(enemies[i].location)) { attackIndex = i; // TODO test this part - work on kiting if ((enemies[i].type == RobotType.BIGZOMBIE || enemies[i].type == RobotType.STANDARDZOMBIE) && rc.getRoundNum() % 2 == 0 && rc.isCoreReady()) { moveAwayFromEnemy(rc, rc.getLocation().directionTo(enemies[i].location)); } if (rc.isWeaponReady()) { rc.attackLocation(enemies[i].location); } } if (rc.isWeaponReady() && enemies[i].type == RobotType.BIGZOMBIE) { attackIndex = i; rc.attackLocation(enemies[i].location); } if (attackIndex < 0) { lowestHealthIndex = lowestHealthIndex < 0 ? 0 : lowestHealthIndex; lowestDistanceIndex = lowestDistanceIndex < 0 ? 0 : lowestDistanceIndex; equalHealth = equalHealth && enemies[i].health == enemies[lowestHealthIndex].health; lowestDistanceIndex = rc.getLocation().distanceSquaredTo(enemies[i].location) < rc.getLocation().distanceSquaredTo(enemies[lowestDistanceIndex].location) ? i : lowestDistanceIndex; lowestHealthIndex = enemies[i].health < enemies[lowestHealthIndex].health ? i : lowestHealthIndex; } } if (attackIndex < 0 && enemies.length > 0) { attackIndex = equalHealth ? lowestDistanceIndex : lowestHealthIndex; } if (attackIndex >= 0 && rc.isWeaponReady()) { rc.attackLocation(enemies[attackIndex].location); } } }
public static void run() throws GameActionException { rc = RobotPlayer.rc; rand = new Random(rc.getID()); // build scouts right away buildRobot(RobotType.SCOUT); while (true) { /* * INPUT */ if (rc.getLocation().equals(goal)) { goal = null; // you made it to the goal past10Locations = new ArrayList<MapLocation>(); // delete the slug trail after you reach your goal } // sense locations around you nearbyMapLocations = MapLocation.getAllMapLocationsWithinRadiusSq( rc.getLocation(), rc.getType().sensorRadiusSquared); // parts locations nearbyPartsLocations = rc.sensePartLocations(RobotType.ARCHON.sensorRadiusSquared); // find the nearest mapLocation with the most parts double maxParts = 0; MapLocation nearbyLocationWithMostParts = null; for (MapLocation loc : nearbyPartsLocations) { // add to locationsWithParts arraylist if (locationsWithParts.contains(loc) == false) { locationsWithParts.add(loc); } // find the location with the most parts double partsAtLoc = rc.senseParts(loc); if (partsAtLoc > maxParts) { maxParts = partsAtLoc; nearbyLocationWithMostParts = loc; } } // read signals Signal[] signals = rc.emptySignalQueue(); for (Signal signal : signals) { // check if the signal has parts at the location int[] message = signal.getMessage(); if (message != null && message[0] == Utility.PARTS_CODE) { // add that location to the locationsWithParts arraylist locationsWithParts.add(signal.getLocation()); } } // sense robots MapLocation myLoc = rc.getLocation(); robots = rc.senseNearbyRobots(); foes = new ArrayList<RobotInfo>(); foesWithinAttackRange = new ArrayList<RobotInfo>(); for (RobotInfo robot : robots) { if (robot.team == Team.ZOMBIE || robot.team == rc.getTeam().opponent()) // if the robot is a foe { foes.add(robot); if (myLoc.distanceSquaredTo(robot.location) < robot.type.attackRadiusSquared) { foesWithinAttackRange.add(robot); } } } int nearbyFoes = foes.size(); int nearbyFoesInAttackRange = foesWithinAttackRange.size(); /*//check stats double health = rc.getHealth(); int infectedTurns = rc.getInfectedTurns(); int robotsAlive = rc.getRobotCount(); */ /* * OUPUT */ // what to do if (nearbyFoes == 0) // if there are no foes in sight { if (rc.getTeamParts() >= RobotType.TURRET.partCost) // build if you can { buildRobots(); } else { if (maxParts > 0 && goal == null) // if there are parts nearby { // make that the goal goal = nearbyLocationWithMostParts; } else if (goal == null) // if there aren't and there is no goal { // build something or find new parts // 80% build, 20% new parts if (locationsWithParts.size() > 0 && rand.nextFloat() > .8) { goal = locationsWithParts.get(0); locationsWithParts.remove(0); goalIsASafeLocation = false; } // calculate the next goal - maybe a new parts location you got via signal } else if (goal != null) // if there is a goal, move there { moveToLocation(goal); } } } else // there are foes nearby { // message for help! if (Math.random() < probSignal) { rc.broadcastSignal(archonInTroubleSignalRadiusSquared); } if (nearbyFoesInAttackRange > 0) { goal = findSaferLocation(); rc.setIndicatorString(0, "" + goal.x + " " + goal.y); goalIsASafeLocation = true; moveToLocation(goal); } } Clock.yield(); } }
/** * run() is the method that is called when a robot is instantiated in the Battlecode world. If * this method returns, the robot dies! */ @SuppressWarnings("unused") public static void run(RobotController rc) { // You can instantiate variables here. Direction[] directions = { Direction.NORTH, Direction.NORTH_EAST, Direction.EAST, Direction.SOUTH_EAST, Direction.SOUTH, Direction.SOUTH_WEST, Direction.WEST, Direction.NORTH_WEST }; RobotType[] robotTypes = { RobotType.SCOUT, RobotType.SOLDIER, RobotType.SOLDIER, RobotType.SOLDIER, RobotType.GUARD, RobotType.GUARD, RobotType.VIPER, RobotType.TURRET }; Random rand = new Random(rc.getID()); int myAttackRange = 0; Team myTeam = rc.getTeam(); Team enemyTeam = myTeam.opponent(); if (rc.getType() == RobotType.ARCHON) { try { // Any code here gets executed exactly once at the beginning of the game. } catch (Exception e) { // Throwing an uncaught exception makes the robot die, so we need to catch exceptions. // Caught exceptions will result in a bytecode penalty. System.out.println(e.getMessage()); e.printStackTrace(); } while (true) { /* // This is a loop to prevent the run() method from returning. Because of the Clock.yield() // at the end of it, the loop will iterate once per game round. try { int fate = rand.nextInt(1000); // Check if this ARCHON's core is ready if (fate % 10 == 2) { // Send a message signal containing the data (6370, 6147) rc.broadcastMessageSignal(6370, 6147, 80); } Signal[] signals = rc.emptySignalQueue(); if (signals.length > 0) { // Set an indicator string that can be viewed in the client rc.setIndicatorString(0, "I received a signal this turn!"); } else { rc.setIndicatorString(0, "I don't any signal buddies"); } if (rc.isCoreReady()) { if (fate < 800) { // Choose a random direction to try to move in Direction dirToMove = directions[fate % 8]; // Check the rubble in that direction if (rc.senseRubble(rc.getLocation().add(dirToMove)) >= GameConstants.RUBBLE_OBSTRUCTION_THRESH) { // Too much rubble, so I should clear it rc.clearRubble(dirToMove); // Check if I can move in this direction } else if (rc.canMove(dirToMove)) { // Move rc.move(dirToMove); } } else { // Choose a random unit to build RobotType typeToBuild = robotTypes[fate % 8]; // Check for sufficient parts if (rc.hasBuildRequirements(typeToBuild)) { // Choose a random direction to try to build in Direction dirToBuild = directions[rand.nextInt(8)]; for (int i = 0; i < 8; i++) { // If possible, build in this direction if (rc.canBuild(dirToBuild, typeToBuild)) { rc.build(dirToBuild, typeToBuild); break; } else { // Rotate the direction to try dirToBuild = dirToBuild.rotateLeft(); } } } } } Clock.yield(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } */ } } else if (rc.getType() != RobotType.TURRET) { try { // Any code here gets executed exactly once at the beginning of the game. myAttackRange = rc.getType().attackRadiusSquared; } catch (Exception e) { // Throwing an uncaught exception makes the robot die, so we need to catch exceptions. // Caught exceptions will result in a bytecode penalty. System.out.println(e.getMessage()); e.printStackTrace(); } while (true) { // This is a loop to prevent the run() method from returning. Because of the Clock.yield() // at the end of it, the loop will iterate once per game round. try { int fate = rand.nextInt(1000); if (fate % 5 == 3) { // Send a normal signal rc.broadcastSignal(80); } boolean shouldAttack = false; // If this robot type can attack, check for enemies within range and attack one if (myAttackRange > 0) { RobotInfo[] enemiesWithinRange = rc.senseNearbyRobots(myAttackRange, enemyTeam); RobotInfo[] zombiesWithinRange = rc.senseNearbyRobots(myAttackRange, Team.ZOMBIE); if (enemiesWithinRange.length > 0) { shouldAttack = true; // Check if weapon is ready if (rc.isWeaponReady()) { rc.attackLocation( enemiesWithinRange[rand.nextInt(enemiesWithinRange.length)].location); } } else if (zombiesWithinRange.length > 0) { shouldAttack = true; // Check if weapon is ready if (rc.isWeaponReady()) { rc.attackLocation( zombiesWithinRange[rand.nextInt(zombiesWithinRange.length)].location); } } } if (!shouldAttack) { if (rc.isCoreReady()) { if (fate < 600) { // Choose a random direction to try to move in Direction dirToMove = directions[fate % 8]; // Check the rubble in that direction if (rc.senseRubble(rc.getLocation().add(dirToMove)) >= GameConstants.RUBBLE_OBSTRUCTION_THRESH) { // Too much rubble, so I should clear it rc.clearRubble(dirToMove); // Check if I can move in this direction } else if (rc.canMove(dirToMove)) { // Move rc.move(dirToMove); } } } } Clock.yield(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } } else if (rc.getType() == RobotType.TURRET) { try { myAttackRange = rc.getType().attackRadiusSquared; } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } while (true) { // This is a loop to prevent the run() method from returning. Because of the Clock.yield() // at the end of it, the loop will iterate once per game round. try { // If this robot type can attack, check for enemies within range and attack one if (rc.isWeaponReady()) { RobotInfo[] enemiesWithinRange = rc.senseNearbyRobots(myAttackRange, enemyTeam); RobotInfo[] zombiesWithinRange = rc.senseNearbyRobots(myAttackRange, Team.ZOMBIE); if (enemiesWithinRange.length > 0) { for (RobotInfo enemy : enemiesWithinRange) { // Check whether the enemy is in a valid attack range (turrets have a minimum range) if (rc.canAttackLocation(enemy.location)) { rc.attackLocation(enemy.location); break; } } } else if (zombiesWithinRange.length > 0) { for (RobotInfo zombie : zombiesWithinRange) { if (rc.canAttackLocation(zombie.location)) { rc.attackLocation(zombie.location); break; } } } } Clock.yield(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } } }