// In this function the HQ spawns a soldier ideally toward the enemy base but in any direction // otherwise public static void SpawnSoldiers(RobotController rc) { try { if (rc.isActive() && rc.getType() == RobotType.HQ) { Direction toEnemy = rc.getLocation().directionTo(rc.senseEnemyHQLocation()); if (rc.senseObjectAtLocation(rc.getLocation().add(toEnemy)) == null) { } else { for (int i = 0; i < 7; i++) { toEnemy = toEnemy.rotateLeft(); if (rc.senseObjectAtLocation(rc.getLocation().add(toEnemy)) == null) { i = 47; } else if (i == 6) { toEnemy = Direction.NONE; } } } if (toEnemy != Direction.NONE) { if (rc.isActive()) { if (rc.getType() == RobotType.HQ) { rc.spawn(toEnemy); } } } } } catch (Exception e) { e.printStackTrace(); System.out.println("Utility Exception"); } }
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(); } }
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 boolean scan(RobotInfo[] objects, int limit) { RobotInfo info = null; RobotInfo mediocre = null; RobotInfo worst = null; target = null; double target_evaluation = 0; double testval = 0; for (int i = 0; i < limit; i++) { info = objects[i]; if (info.type == RobotType.TOWER) { if (bot.type == RobotType.SCOUT) continue; /* if (!sense.isVulnerableEnemyTower(info)) { continue; }*/ } if (bot.type == RobotType.SCOUT) { if (info.flux < GameConstants.UNIT_UPKEEP) continue; } if (rc.canAttackSquare(info.location)) { if (info.type == RobotType.TOWER) { worst = info; continue; } if (info.type == RobotType.SCOUT) { mediocre = info; continue; } else { target = info; return true; } } if (target == null || (testval = evaluate(info)) > target_evaluation) { target = info; target_evaluation = testval; } } if (target == null) { if (mediocre == null) { if (worst != null) { try { GameObject node = rc.senseObjectAtLocation(worst.location, RobotLevel.POWER_NODE); if (node != null) { for (MapLocation n : ((PowerNode) node).neighbors()) { // TODO: use hash map for (int i = 0; i < sense.allied_nodes_count; i++) { if (n.equals(sense.allied_nodes[i].getLocation())) { target = worst; break; } } } } } catch (GameActionException e) { e.printStackTrace(); } } } else { target = mediocre; } } return target != null; }