public void run() { while (true) { if (rc.getType() == RobotType.SOLDIER) { switch (corner) { case 1: Utilities.MoveMapLocation(rc, new MapLocation(2, 2), true); break; case 2: Utilities.MoveMapLocation(rc, new MapLocation(rc.getMapWidth() - 3, 2), true); break; case 3: Utilities.MoveMapLocation(rc, new MapLocation(2, rc.getMapHeight() - 3), true); break; default: Utilities.MoveMapLocation( rc, new MapLocation(rc.getMapWidth() - 3, rc.getMapHeight() - 3), true); break; } if (rc.isActive()) { try { rc.construct(RobotType.PASTR); } catch (Exception e) { } } } } }
// this method will advance one square towards a target and try to avoid enemies as much as // possible public static void avoidEnemiesMove(RobotController rc, MapLocation target) { try { // first we will find all enemy bots near us GameObject[] nearByBots = rc.senseNearbyGameObjects(Robot.class, 15, rc.getTeam().opponent()); Direction direction = rc.getLocation().directionTo(target); // if we don't see anything then lets head towards target if (nearByBots.length == 0) { // rc.setIndicatorString(2, "No enemies detected"); // rc.setIndicatorString(1, "x: "+target.x + " y: " + target.y); direction = rc.getLocation().directionTo(target); if (rc.canMove(direction)) // && // !rc.senseTerrainTile(rc.getLocation().add(direction).add(direction)).equals(TerrainTile.VOID)) { if (rc.isActive()) { rc.move(direction); } } else { MapLocation target2 = rc.getLocation().add(direction); if (rc.senseTerrainTile(target2).equals(TerrainTile.VOID)) { int j = 0; while (rc.senseTerrainTile(target2).equals(TerrainTile.VOID)) { rc.setIndicatorString(0, "" + j); j++; target2 = target2.add(direction); } Utilities.MoveMapLocation(rc, target2, false); } /* int distanceRight = 0; int distanceLeft = 0; direction = direction.rotateRight(); while (!rc.canMove(direction) && rc.senseTerrainTile(rc.getLocation().add(direction)).equals(TerrainTile.VOID)) { direction = direction.rotateRight(); } if (rc.isActive()) { if (rc.canMove(direction)) { rc.move(direction); } } */ } } // otherwise we need to avoid them else { rc.setIndicatorString(2, "Avoiding enemies"); rc.setIndicatorString(1, "Numb of Enemies: " + nearByBots.length); // now we will calculate the distance form all 5 spots towards are target and the distance // from that spot to all enemies we can see // we will pick the one with the greatest distance int[] distancesToLocations = new int[5]; for (int k = 0; k < distancesToLocations.length; k++) { distancesToLocations[k] = 0; } MapLocation spot; Direction newDir; // first we look 90 to our right newDir = direction.rotateRight().rotateRight(); for (int j = 0; j < 5; j++) { if (rc.canMove(newDir)) { spot = rc.getLocation().add(newDir); for (int i = 0; i < nearByBots.length; i++) { // System.out.println("entering for loop"); distancesToLocations[j] += spot.distanceSquaredTo(rc.senseLocationOf(nearByBots[i])); } } else { distancesToLocations[j] = -123; } // every time through the loop we look one further to the left newDir.rotateLeft(); } int indexOfLargest = 0; int largest = distancesToLocations[0]; for (int j = 1; j < distancesToLocations.length; j++) { if (largest < distancesToLocations[j]) { indexOfLargest = j; largest = distancesToLocations[j]; } } // now we orientate newDir to the right spot newDir = direction.rotateRight().rotateRight(); for (int i = 0; i <= indexOfLargest; i++) { newDir = newDir.rotateLeft(); } while (!rc.isActive()) { rc.yield(); } // now we can finally move if (rc.isActive()) { if (rc.canMove(newDir)) { rc.move(newDir); } } } } catch (Exception e) { e.printStackTrace(); } }