private void tryClearRubble(Direction direction) throws GameActionException { MapLocation nextLocation = rc.getLocation().add(direction); double rubble = rc.senseRubble(nextLocation); if (rubble > 100) { rc.clearRubble(direction); } }
// move to a maplocation public static void moveToLocation(MapLocation m) throws GameActionException { MapLocation currentLoc = rc.getLocation(); Direction directionToM = currentLoc.directionTo(m); Direction actualDirectionToMove = directionToM; // deal with the slug trail - trim it to size if (past10Locations.size() > 10) { while (past10Locations.size() > 10) { past10Locations.remove(past10Locations.size() - 1); } } past10Locations.add(currentLoc); MapLocation locationToMoveTo = currentLoc.add(directionToM); if (canMoveThere( actualDirectionToMove, locationToMoveTo)) // make sure it's not part of the slug trail and it's not blocked by // rubble and you can move there { moveInDirection(actualDirectionToMove); } else { // first, check if you should remove rubble. only if the surrounding squares are empty // boolean shouldRemoveRubble = false; int directionsWithRubble = 0; for (Direction d : Direction.values()) { MapLocation added = goal.add(d); boolean isFullOfRubble = rc.senseRubble(added) > 50; if (isFullOfRubble) { directionsWithRubble++; } } if (directionsWithRubble > 2) // if it's surrounded then dig { if (rc.isCoreReady()) { if (actualDirectionToMove.equals(Direction.OMNI) == false) { rc.clearRubble(actualDirectionToMove); } } } else // if not, path around it { Direction right = actualDirectionToMove.rotateRight(); MapLocation rightLoc = currentLoc.add(right); while (right.equals(actualDirectionToMove) == false) { if (canMoveThere(right, rightLoc)) { moveInDirection(right); right = actualDirectionToMove; } else { right = right.rotateRight(); rightLoc = currentLoc.add(right); } } } } }
/** * Expensive method. Should call upon initialization only. * * @param rc * @throws GameActionException */ static void senseRubble(RobotController rc) throws GameActionException { int roundNum = rc.getRoundNum(); for (int x = -straightSight; x <= straightSight; ++x) { for (int y = -straightSight; y <= straightSight; ++y) { MapLocation loc = rc.getLocation().add(x, y); if (rc.canSense(loc)) { rubbleTimes[loc.x % MAP_MOD][loc.y % MAP_MOD] = roundNum; mapRubble[loc.x % MAP_MOD][loc.y % MAP_MOD] = rc.senseRubble(loc); } } } }
/** * Archons automatically collect parts on their current location. This method checks for nearby * parts within a radius squared distance of 9 and moves towards the nearest parts. * * @param rc must be archon * @return whether parts were collected * @throws GameActionException */ private static void collectParts(RobotController rc) throws GameActionException { int lowestDistanceIndex = 0; MapLocation myLocation = rc.getLocation(); MapLocation[] parts = rc.sensePartLocations(PART_COLLECTION_RADIUS_SQUARED); for (int i = 0; i < parts.length; i++) { if (myLocation.distanceSquaredTo(parts[i]) < myLocation.distanceSquaredTo(parts[lowestDistanceIndex]) && rc.senseRubble(parts[lowestDistanceIndex]) < 200) { lowestDistanceIndex = i; } if (myLocation.distanceSquaredTo(parts[i]) == myLocation.distanceSquaredTo(parts[lowestDistanceIndex])) { lowestDistanceIndex = rc.senseParts(parts[i]) > rc.senseParts(parts[lowestDistanceIndex]) ? i : lowestDistanceIndex; } } if (rc.isCoreReady() && parts.length > 0 && rc.senseRubble(parts[lowestDistanceIndex]) < 200) { moveTowards(rc, myLocation.directionTo(parts[lowestDistanceIndex])); } }
public static void clear_rubble() throws GameActionException { if (!rc.isCoreReady()) return; if (Scanner.can_see_targets()) return; if (Scanner.there_is_hidden_enemy()) return; Direction direction_to_clear = current_location.directionTo(destination); if (direction_to_clear == Direction.NONE || direction_to_clear == Direction.OMNI) return; if (rc.senseRubble(current_location.add(direction_to_clear)) < 1) return; rc.clearRubble(direction_to_clear); rc.setIndicatorString(1, "Clearing: " + direction_to_clear.toString()); }
/** * Finds adjacent with lowest rubble (greater than 0) and clears it. * * @param rc * @return * @throws GameActionException */ private static void clearRubble(RobotController rc) throws GameActionException { MapLocation myLocation = rc.getLocation(); MapLocation[] adjacentLocations = MapLocation.getAllMapLocationsWithinRadiusSq(myLocation, ONE_SQUARE_RADIUS); int lowestRubbleIndex = -1; for (int i = 0; i < adjacentLocations.length; i++) { double rubble = rc.senseRubble(adjacentLocations[i]); if (lowestRubbleIndex == -1) { lowestRubbleIndex = rubble > 0 ? i : lowestRubbleIndex; } else { lowestRubbleIndex = rubble > 0 && rubble < rc.senseRubble(adjacentLocations[lowestRubbleIndex]) ? i : lowestRubbleIndex; } } if (lowestRubbleIndex >= 0) { Direction dir = myLocation.directionTo(adjacentLocations[lowestRubbleIndex]); if (rc.isCoreReady() && !dir.equals(Direction.OMNI) && !dir.equals(Direction.NONE)) { rc.clearRubble(dir); } } }
protected void tryMoveToward(MapLocation location) throws GameActionException { MapLocation currentLocation = rc.getLocation(); Direction moveDirection = currentLocation.directionTo(location); if (location.isAdjacentTo(currentLocation)) { double rubble = rc.senseRubble(location); if (rubble >= 100) { rc.clearRubble(moveDirection); return; } } tryMove(moveDirection); }
// move in a given direction public static void moveInDirection(Direction d) throws GameActionException { MapLocation m = rc.getLocation().add(d); if (rc.senseRubble(m) < tooMuchRubble || Math.random() < probClearRubbleAnyways) // if it's less than 20, just move there { if (rc.isCoreReady() && rc.canMove(d)) { rc.move(d); } } else // clear it { if (rc.isCoreReady()) { rc.clearRubble(d); } } }
protected void gatherMapInfo(MapLocation targetObjLoc) throws GameActionException { // This is for a single place, used for rescue updates if (rc.canSense(targetObjLoc)) { RobotInfo targetRobot = rc.senseRobotAtLocation(targetObjLoc); if (targetRobot != null) { if (targetRobot.team.equals(Team.NEUTRAL)) { // IS a neutral robot memory.addNeutralRobotMapInfo(targetObjLoc, targetRobot); } else if (targetRobot.team.equals(Team.ZOMBIE)) { memory.addMapInfo( targetObjLoc, (int) targetRobot.health, RobotConstants.mapTypes.ZOMBIE_DEN); } } memory.addMapInfo( targetObjLoc, (int) rc.senseRubble(targetObjLoc), RobotConstants.mapTypes.RUBBLE); memory.addMapInfo( targetObjLoc, (int) rc.senseParts(targetObjLoc), RobotConstants.mapTypes.PARTS); } }
protected void gatherMapInfo() throws GameActionException { // rc.setIndicatorString(0, "Sensing"); RobotInfo[] enemyRobotsInRange = rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, Team.ZOMBIE); for (int n = 0; n < enemyRobotsInRange.length; n++) { if (enemyRobotsInRange[n].type == RobotType.ZOMBIEDEN) { memory.addMapInfo( enemyRobotsInRange[n].location, (int) enemyRobotsInRange[n].health, RobotConstants.mapTypes.ZOMBIE_DEN); } } RobotInfo[] neutralRobotsInRange = rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, Team.NEUTRAL); for (int n = 0; n < neutralRobotsInRange.length; n++) { memory.addNeutralRobotMapInfo(neutralRobotsInRange[n].location, neutralRobotsInRange[n]); } MapLocation[] partsLoc = rc.sensePartLocations(rc.getType().sensorRadiusSquared); for (int n = 0; n < partsLoc.length; n++) { memory.addMapInfo( partsLoc[n], (int) rc.senseParts(partsLoc[n]), RobotConstants.mapTypes.PARTS); } for (int x = -robotSenseRadius / (int) Math.sqrt(2) + rc.getLocation().x; x < robotSenseRadius / (int) Math.sqrt(2) + rc.getLocation().x; x++) { for (int y = -robotSenseRadius / (int) Math.sqrt(2) + rc.getLocation().y; y < robotSenseRadius / (int) Math.sqrt(2) + rc.getLocation().y; y++) { MapLocation underView = new MapLocation(x, y); if (rc.canSense(underView) && rc.onTheMap(underView)) { int rubbles = (int) rc.senseRubble(underView); if (rubbles > 0) { memory.addMapInfo(underView, rubbles, RobotConstants.mapTypes.RUBBLE); } } } } }
/** * Checks if rc can move in direction dir (runs isCoreReady and canMove). If so, moves. If not, * moves rc in any of the four directions nearest dir, if possible. * * @param rc * @param dir * @return true if rc moves else false * @throws GameActionException */ private static void moveTowards(RobotController rc, Direction dir) throws GameActionException { if (rc.isCoreReady() && !dir.equals(Direction.OMNI) && !dir.equals(Direction.NONE)) { Direction[] moveRight = { dir, dir.rotateRight(), dir.rotateLeft(), dir.rotateRight().rotateRight(), dir.rotateLeft().rotateLeft() }; Direction[] moveLeft = { dir, dir.rotateLeft(), dir.rotateRight(), dir.rotateLeft().rotateLeft(), dir.rotateRight().rotateRight() }; Direction[] nearDirections = Math.random() >= .5 ? moveRight : moveLeft; // 50% chance robot tries to move to right first for (Direction nearDir : nearDirections) { if (rc.canMove(nearDir) && rc.isCoreReady()) { rc.move(nearDir); } } if (rc.getType() != RobotType.TTM && rc.getType() != RobotType.TTM) // these types can't clear rubble { if (rc.isCoreReady() && rc.onTheMap(rc.getLocation().add(dir)) && rc.senseRubble(rc.getLocation().add(dir)) > RUBBLE_LOWER_CLEAR_THRESHOLD) { clearRubble(rc); } } } }
public static void forwardishIfProtection(Direction movingDirection2) throws GameActionException { boolean stationArchon = RobotPlayer.stationArchon; // boolean tempRoamingArchon = RobotPlayer.tempRoamingArchon; if (stationArchon) return; // or do other stuff int id = RobotPlayer.id; // System.out.println(Integer.toString(id)); // int waitTurns = 1; RobotController rc = RobotPlayer.rc; int waitTurns = rc.getType() == RobotType.ARCHON ? 1 : 1; // if(rc.getRoundNum()%waitTurns ==0){// leader let the soldiers catch up if (true) { for (int i : possibleDirections) { Direction candidateDirection = Direction.values()[(movingDirection2.ordinal() + i + 8) % 8]; MapLocation candidateLocation = rc.getLocation().add(candidateDirection.dx, candidateDirection.dy); if (patient > 0) { // if (rc.getType() == RobotType.ARCHON) // System.out.println("should be an archon moving now"); MapLocation oldLoc = rc.getLocation(); if (rc.canMove(candidateDirection) && !pastLocations.contains(candidateLocation) && enoughProtectionAhead(candidateLocation)) { if (!pastLocations.contains(oldLoc)) { pastLocations.add(rc.getLocation()); } if (pastLocations.size() > 5) pastLocations.remove(0); rc.move(candidateDirection); if (rc.getType() == RobotType.ARCHON && !RobotPlayer.stationArchon) { // System.out.println("Im getting this far"); // System.out.println("I should have moved in direction: "+ // candidateDirection.toString()); } // if (rc.getType() == RobotTypse.ARCHON) // System.out.println("should have moved an archon new location " + // rc.getLocation().toString()+ "old : " + oldLoc) ; patient = Math.min(patient + 1, 30); return; } } else { if (rc.canMove(candidateDirection) && enoughProtectionAhead(candidateLocation)) { rc.move(candidateDirection); patient = Math.min(patient + 1, 30); return; } else { // dig ! if (rc.senseRubble(candidateLocation) > GameConstants.RUBBLE_OBSTRUCTION_THRESH) { rc.clearRubble(candidateDirection); return; } } } } patient = patient - 5; } }
// Assumes that you cannot move in that location private static boolean shouldMine(RobotController rc, Direction dir) { MapLocation myLoc = rc.getLocation(); MapLocation dirLoc = myLoc.add(dir); double rubble = rc.senseRubble(dirLoc); return rubble >= 50; }
/** * Move and update history * * @param rc * @param dir */ static void move(RobotController rc, Direction dir) throws GameActionException { rc.move(dir); MapLocation loc = rc.getLocation(); history[historySize++] = loc; int roundNum = rc.getRoundNum(); switch (dir) { case NORTH_EAST: case EAST: case SOUTH_EAST: for (int y = -straightSight; y <= straightSight; ++y) { MapLocation senseLocation = loc.add(straightSight, y); if (rc.canSense(senseLocation)) { rubbleTimes[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = roundNum; mapRubble[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = rc.senseRubble(senseLocation); } } break; case SOUTH_WEST: case WEST: case NORTH_WEST: for (int y = -straightSight; y <= straightSight; ++y) { MapLocation senseLocation = loc.add(-straightSight, y); if (rc.canSense(senseLocation)) { rubbleTimes[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = roundNum; mapRubble[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = rc.senseRubble(senseLocation); } } break; default: break; } switch (dir) { case NORTH_WEST: case NORTH: case NORTH_EAST: for (int x = -straightSight; x <= straightSight; ++x) { MapLocation senseLocation = loc.add(x, straightSight); if (rc.canSense(senseLocation)) { rubbleTimes[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = roundNum; mapRubble[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = rc.senseRubble(senseLocation); } } break; case SOUTH_EAST: case SOUTH: case SOUTH_WEST: for (int x = -straightSight; x <= straightSight; ++x) { MapLocation senseLocation = loc.add(x, -straightSight); if (rc.canSense(senseLocation)) { rubbleTimes[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = roundNum; mapRubble[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = rc.senseRubble(senseLocation); } } break; default: break; } if (robotType == RobotType.ARCHON) mapParts[loc.x % MAP_MOD][loc.y % MAP_MOD] = 0; }
public static void run(RobotController rc) { int myAttackRange = 0; Random rand = new Random(rc.getID()); Team myTeam = rc.getTeam(); Team enemyTeam = myTeam.opponent(); 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 = RobotPlayer.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(); } } }
/** * 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(); } } } }