private void defendMicro() throws GameActionException {
   Robot[] enemiesList = rc.senseNearbyGameObjects(Robot.class, 100000, rc.getTeam().opponent());
   int[] closestEnemyInfo = getClosestEnemy(enemiesList);
   MapLocation closestEnemyLocation = new MapLocation(closestEnemyInfo[1], closestEnemyInfo[2]);
   double[][] activities = enemyActivites(currentLocation, rc.getTeam());
   if (activities[2][0] + activities[1][0] + activities[0][0] == 0) {
     NavSystem.goToLocationAvoidMines(closestEnemyLocation);
     //			rc.setIndicatorString(0, "Forward1");
   }
   if (!minedUpAndReadyToGo(currentLocation)) {
     if (activities[1][0] + activities[0][0] > 2) {
       //			if (activities[1][0]>1 || activities[0][0] == 0) {
       NavSystem.goToLocationAvoidMines(DataCache.ourHQLocation);
       //				rc.setIndicatorString(0, "Back1");
     }
   } else {
     MapLocation bestLocation = currentLocation;
     int positionQuality = -1000;
     MapLocation newPosition;
     int value;
     for (int i = 8; --i >= 0; ) {
       newPosition = currentLocation.add(Direction.values()[i]);
       value = positionValue(newPosition);
       if (value > positionQuality) {
         positionQuality = value;
         bestLocation = newPosition;
         break;
       }
     }
     NavSystem.goToLocationAvoidMines(bestLocation);
     //			rc.setIndicatorString(0, "Pos");
   }
 }
 public void pushCodeGetCloser(MapLocation destination) throws GameActionException {
   if (NavSystem.navMode != NavMode.GETCLOSER || NavSystem.destination != destination) {
     NavSystem.setupGetCloser(destination);
   }
   if (rc.isActive()) {
     NavSystem.moveCloserFavorNoMines();
   }
 }
 private void chargeShieldsCode() throws GameActionException {
   if (DataCache.numEnemyRobots > 0) {
     nextSoldierState = SoldierState.PUSHING;
     pushingCode();
   } else {
     if (!ourNukeHalfDone && enemyNukeHalfDone) {
       nextSoldierState = SoldierState.ALL_IN;
       allInCode();
     } else {
       // either charge up shields next to the shields encampment, or wait at another location
       // until there is space
       //			MapLocation shieldQueueLocation = rallyPoint;
       // find an empty space next to the shields encampment
       int distanceSquaredToShields = rc.getLocation().distanceSquaredTo(shieldLocation);
       if (distanceSquaredToShields > 2) {
         // not charging yet
         for (int i = 8; --i >= 0; ) {
           // Check to see if it's empty
           MapLocation iterLocation = shieldLocation.add(DataCache.directionArray[i]);
           if (rc.senseObjectAtLocation(iterLocation) == null) {
             // Oh, there's an empty space! let's go to it
             NavSystem.goToLocation(iterLocation);
             return;
           }
         }
         // we found the shields encampment, but there are no empty spaces, so wait at the queue
         // location
         //					NavSystem.goToLocation(EncampmentJobSystem.shieldsQueueLoc);
         NavSystem.goToLocation(shieldQueueLocation);
       } else {
         // already charging
         mineDensity =
             rc.senseMineLocations(findMidPoint(), rSquared, Team.NEUTRAL).length
                 / (3.0 * rSquared);
         shieldsCutoff = (int) (DataCache.rushDist + 5 * (mineDensity * DataCache.rushDist)) + 50;
         //					rc.setIndicatorString(1, "cutoff: " + Integer.toString(shieldsCutoff));
         if (rc.getShields() > shieldsCutoff) {
           // leave
           if (!ourNukeHalfDone && enemyNukeHalfDone) {
             nextSoldierState = SoldierState.ALL_IN;
             allInCode();
           } else {
             nextSoldierState = SoldierState.RALLYING;
             rallyingCode();
           }
         }
       }
     }
   }
 }
 public void escapeHQMinesCode() throws GameActionException {
   // We need to run away from the mines surrounding our base
   Team mineTeam = rc.senseMine(rc.getLocation());
   if (mineTeam != null && mineTeam != rc.getTeam()) {
     // We need to run away from the mines surrounding our base
     if (NavSystem.safeLocationAwayFromHQMines != null) {
       NavSystem.goToLocationDontDefuseOrAvoidMines(NavSystem.safeLocationAwayFromHQMines);
     } else {
       NavSystem.goAwayFromLocationEscapeMines(DataCache.ourHQLocation);
     }
   } else {
     // No more mines, so clear out HQ mines
     nextSoldierState = SoldierState.CLEAR_OUT_HQ_MINES;
     clearOutHQMinesCode();
   }
 }
  public void rallyingCode() throws GameActionException {
    if (enemyNukeHalfDone && !ourNukeHalfDone) {
      //			nextSoldierState = SoldierState.ALL_IN;
      //			allInCode();
      // charge up
      if (shieldExists()) {
        nextSoldierState = SoldierState.CHARGE_SHIELDS;
        chargeShieldsCode();
      } else {
        nextSoldierState = SoldierState.ALL_IN;
        allInCode();
      }
    } else {
      //			int hqPowerLevel2 = Integer.MAX_VALUE;
      //			Message message2 = BroadcastSystem.read(powerChannel);
      //			if (message2.isValid) {
      //				hqPowerLevel2 = message2.body;
      //			} else {
      //				hqPowerLevel2 = (int) rc.getTeamPower();
      //			}

      int genCount = Integer.MAX_VALUE;
      Message message = BroadcastSystem.read(genCountChannel);
      if (message.isValid) {
        genCount = message.body;
      }

      // If there are enemies nearby, trigger FIGHTING SoldierState
      if (DataCache.numEnemyRobots > 0) {
        nextSoldierState = SoldierState.FIGHTING;
        fightingCode();
        //			} else if (hqPowerLevel2 < 10*(1+DataCache.numAlliedEncampments) || hqPowerLevel2 <
        // 100) {
      } else if (DataCache.numAlliedSoldiers >= (40 + 10 * genCount) / 1.5) {
        nextSoldierState = SoldierState.PUSHING;
        pushingCode();
      } else {
        mineDensity =
            rc.senseMineLocations(findMidPoint(), rSquared, Team.NEUTRAL).length / (3.0 * rSquared);
        shieldsCutoff = (int) (DataCache.rushDist + 5 * (mineDensity * DataCache.rushDist)) + 50;
        if (rc.getShields() < shieldsCutoff - 50 && shieldExists()) {
          // not enough shields
          nextSoldierState = SoldierState.CHARGE_SHIELDS;
          chargeShieldsCode();
        } else {
          boolean layedMine = false;
          if (rc.senseMine(currentLocation) == null) {
            if (rc.isActive() && Util.Random() < 0.1) {
              rc.layMine();
              layedMine = true;
            }
          }
          if (!layedMine) {
            NavSystem.goToLocation(rallyPoint);
          }
        }
      }
    }
  }
  /**
   * code to be used by capturers
   *
   * @throws GameActionException
   */
  private void captureCode() throws GameActionException {
    if (!unassigned) { // if assigned to something
      EncampmentJobSystem.updateJobTaken();
    }
    if (rc.isActive()) {
      if (rc.senseEncampmentSquare(currentLocation)
          && currentLocation.equals(EncampmentJobSystem.goalLoc)) {
        if (rc.getTeamPower() > rc.senseCaptureCost()) {
          rc.captureEncampment(EncampmentJobSystem.assignedRobotType);
        }
      } else {

        if (rc.senseNearbyGameObjects(Robot.class, 14, rc.getTeam().opponent()).length > 0) {
          unassigned = true;
          soldierState = SoldierState.FIGHTING;
        }

        if (NavSystem.navMode == NavMode.BFSMODE) {
          NavSystem.tryBFSNextTurn();
        } else if (NavSystem.navMode == NavMode.GETCLOSER) {
          NavSystem.tryMoveCloser();
        } else if (rc.getLocation().distanceSquaredTo(EncampmentJobSystem.goalLoc) <= 8) {
          NavSystem.setupGetCloser(EncampmentJobSystem.goalLoc);
          NavSystem.tryMoveCloser();
        } else {
          NavSystem.moveCloserFavorNoMines(EncampmentJobSystem.goalLoc);
          //					if (NavSystem.navMode == NavMode.NEUTRAL){
          //						NavSystem.setupSmartNav(EncampmentJobSystem.goalLoc);
          //						NavSystem.followWaypoints();
          //					} else {
          //						NavSystem.followWaypoints();
          //					}
        }
      }
    }
  }
 public void clearOutHQMinesCode() throws GameActionException {
   // Clear out a path to the HQ
   Team mineTeam1 = rc.senseMine(rc.getLocation());
   if (mineTeam1 == null
       || mineTeam1 == rc.getTeam()
           && rc.getLocation().distanceSquaredTo(DataCache.ourHQLocation) > 2) {
     NavSystem.goToLocation(DataCache.ourHQLocation);
   } else {
     // We're done
     if (strategy == Strategy.NUKE) {
       nextSoldierState = SoldierState.FINDING_START_MINE_POSITIONS;
       findingStartMinePositionsCode();
     } else {
       nextSoldierState = SoldierState.RALLYING;
       rallyingCode();
     }
   }
 }
  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 void findingStartMinePositionsCode() throws GameActionException {
   if (DataCache.numEnemyRobots == 0) {
     int distanceSquaredToMiningStartLocation =
         rc.getLocation().distanceSquaredTo(miningStartLocation);
     if (distanceSquaredToMiningStartLocation == 0
         || (distanceSquaredToMiningStartLocation <= 2
             && miningStartLocation.equals(DataCache.ourHQLocation))) {
       nextSoldierState = SoldierState.MINING;
       miningCode();
     } else if (distanceSquaredToMiningStartLocation <= 2
         && rc.senseEncampmentSquares(miningStartLocation, 0, null).length == 1) {
       // Choose another miningStartLocation
       getNewMiningStartLocation();
     } else {
       Direction dir = rc.getLocation().directionTo(miningStartLocation);
       NavSystem.goDirectionAndDefuse(dir);
     }
   } else {
     nextSoldierState = SoldierState.FIGHTING;
     fightingCode();
   }
 }
  public void microCode() throws GameActionException {
    Robot[] enemiesList = rc.senseNearbyGameObjects(Robot.class, 100000, rc.getTeam().opponent());
    int[] closestEnemyInfo = getClosestEnemy(enemiesList);
    MapLocation closestEnemyLocation = new MapLocation(closestEnemyInfo[1], closestEnemyInfo[2]);
    int enemyDistSquared = closestEnemyLocation.distanceSquaredTo(rc.getLocation());

    if (enemyDistSquared <= 2) { // if there is enemy in one dist
      //			Robot[] enemiesInOneDist = rc.senseNearbyGameObjects(Robot.class, 2,
      // rc.getTeam().opponent());
      // stay
      //			rc.setIndicatorString(0, "stay1");
      double[] our23 = getEnemies2Or3StepsAway();
      if (our23[0] < 1) { // if currently attacking encampments or idle workers
        if (our23[1] >= 1) {
          NavSystem.goAwayFromLocationAvoidMines(closestEnemyLocation);
        }
      }
    } else if (DataCache.numNearbyEnemySoldiers == 0
        || DataCache.numNearbyAlliedSoldiers
            >= 3 * DataCache.numNearbyEnemySoldiers) { // if no enemies in one, two, or three dist
      //			// if no enemies in 3-dist or we outnumber them 3 to 1
      //			NavSystem.goToLocation(closestEnemyLocation);
      if (rc.isActive()) {
        NavSystem.moveCloserFavorNoMines(closestEnemyLocation);
      }

    } else { // enemies in two or three dist
      double[] our23 = getEnemies2Or3StepsAway();
      double[] enemy23 =
          getEnemies2Or3StepsAwaySquare(closestEnemyLocation, rc.getTeam().opponent());
      //			rc.setIndicatorString(2, our23[0] + " " + our23[1] + " " + our23[2]);
      //
      //			rc.setIndicatorString(1, enemy23[0] + " " + enemy23[1] + " " + enemy23[2]);
      if (our23[1] > 0) { // closest enemy in 2 dist

        if (enemy23[0] > 0) { // if enemy has dist 1
          NavSystem.goToLocationAvoidMines(closestEnemyLocation);
          //					rc.setIndicatorString(0, "forward2");
        } else {
          if (enemy23[1] + enemy23[0] > our23[1] + our23[2] + 1 || our23[1] + our23[2] < 1) {
            // move forward
            NavSystem.goToLocationAvoidMines(closestEnemyLocation);
            //						rc.setIndicatorString(0, "forward2");
          }
          //        			else if (enemy23[0] + enemy23[1] + enemy23[2] > our23[1] + our23[2]+2) {
          //                		rc.setIndicatorString(0, "back2.5");
          //                		goAwayFromLocation(closestEnemyLocation);
          //                		//back
          //                	}
          else {
            NavSystem.goAwayFromLocationAvoidMines(closestEnemyLocation);
            //						rc.setIndicatorString(0, "back2");
          }
        }

      } else { // closest enemy is 3 dist
        if (enemy23[0] > 0) {
          NavSystem.goToLocationAvoidMines(closestEnemyLocation);
          //					rc.setIndicatorString(0, "forward4");
        } else if (enemy23[1] > 0) { // if enemy 2dist is > 0
          int closestDist = 100;
          int dist;
          MapLocation closestAllyLocation = null;
          Robot[] twoDistAllies =
              rc.senseNearbyGameObjects(Robot.class, closestEnemyLocation, 8, rc.getTeam());
          for (int i = twoDistAllies.length; --i >= 0; ) {
            Robot ally = twoDistAllies[i];
            RobotInfo arobotInfo = rc.senseRobotInfo(ally);
            dist = arobotInfo.location.distanceSquaredTo(rc.getLocation());
            if (dist < closestDist) {
              closestDist = dist;
              closestAllyLocation = arobotInfo.location;
            }
          }

          double[] ally23 = getEnemies2Or3StepsAwaySquare(closestAllyLocation, rc.getTeam());

          if (enemy23[0] + enemy23[1] + enemy23[2] > ally23[1] + ally23[2]) {
            NavSystem.goToLocationAvoidMines(closestEnemyLocation);
            //						rc.setIndicatorString(0, "forward3");
          } else {
            //						rc.setIndicatorString(0, "stay3");
            NavSystem.goAwayFromLocationAvoidMines(closestEnemyLocation);
          }
        } else {
          if (enemy23[2] - our23[2] > 5 || our23[2] < 1) {
            NavSystem.goToLocationAvoidMines(closestEnemyLocation);
          } else {
            NavSystem.goAwayFromLocationAvoidMines(closestEnemyLocation);
          }
          // otherwise, stay
        }
      }
    }
  }
  public void miningSubroutine() throws GameActionException {
    if (DataCache.numEnemyRobots == 0
        && rc.getLocation().distanceSquaredTo(DataCache.ourHQLocation)
            <= Constants.MAXIMUM_MINING_DISTANCE_SQUARED_FROM_HQ) {
      // Do mining
      if (rc.hasUpgrade(Upgrade.PICKAXE)) {
        if (rc.isActive()) {
          int x = rc.getLocation().x;
          int y = rc.getLocation().y;
          if ((2 * x + y) % 5 == 0 && rc.senseMine(rc.getLocation()) == null) {
            rc.layMine();
          } else {
            //						if (NavSystem.navMode == NavMode.NEUTRAL) {
            //							NavSystem.setupMiningNav();
            //						} else {
            //							NavSystem.followWaypoints(true, false);
            //						}
            MapLocation newLocation1 = rc.getLocation().add(miningDirConstant);
            MapLocation newLocation2 = rc.getLocation().add(miningDirConstantOpp);
            if (distanceToLine(newLocation1) <= Constants.MINING_WIDTH
                && rc.senseMine(newLocation1) == null
                && rc.senseEncampmentSquares(newLocation1, 0, null).length == 0) {
              NavSystem.goDirectionAndDefuse(miningDirConstant);
              return;
            } else if (distanceToLine(newLocation2) <= Constants.MINING_WIDTH
                && rc.senseMine(newLocation2) == null
                && rc.senseEncampmentSquares(newLocation2, 0, null).length == 0) {
              NavSystem.goDirectionAndDefuse(miningDirConstantOpp);
              return;
            }

            Direction dir = rc.getLocation().directionTo(miningDestination);
            NavSystem.goDirectionAndDefuse(dir);
          }
        }
      } else {
        // no upgrade
        if (rc.isActive()) {
          if (rc.senseMine(rc.getLocation()) == null) {
            rc.layMine();
          } else {
            //						if (NavSystem.navMode == NavMode.NEUTRAL) {
            //							NavSystem.setupMiningNav();
            //						} else {
            //							NavSystem.followWaypoints(true, false);
            //						}
            // check to see if mines on left or right are untaken
            MapLocation newLocation1 = rc.getLocation().add(miningDirConstant);
            MapLocation newLocation2 = rc.getLocation().add(miningDirConstantOpp);
            //						rc.setIndicatorString(0, Double.toString(distanceToLine(newLocation1)));
            //						rc.setIndicatorString(1, Double.toString(distanceToLine(newLocation2)));
            if (distanceToLine(newLocation1) <= Constants.MINING_WIDTH
                && rc.senseMine(newLocation1) == null
                && rc.senseEncampmentSquares(newLocation1, 0, null).length == 0) {
              NavSystem.goDirectionAndDefuse(miningDirConstant);
              return;
            } else if (distanceToLine(newLocation2) <= Constants.MINING_WIDTH
                && rc.senseMine(newLocation2) == null
                && rc.senseEncampmentSquares(newLocation2, 0, null).length == 0) {
              NavSystem.goDirectionAndDefuse(miningDirConstantOpp);
              return;
            }

            Direction dir = rc.getLocation().directionTo(miningDestination);
            NavSystem.goDirectionAndDefuse(dir);
          }
        }
      }
    } else {
      // There are enemies, so ditch mining and go defend
      soldierState = SoldierState.FIGHTING;
    }
  }
  public void miningCode() throws GameActionException {
    int hqPowerLevel = Integer.MAX_VALUE;
    Message message = BroadcastSystem.read(powerChannel);
    if (message.isValid) {
      hqPowerLevel = message.body;
    } else {
      hqPowerLevel = (int) rc.getTeamPower();
    }

    if (DataCache.numEnemyRobots > 0 && strategy != Strategy.NUKE) {
      //			nextSoldierState = SoldierState.FIGHTING;
      //			fightingCode();
      if (shieldExists()) {
        nextSoldierState = SoldierState.CHARGE_SHIELDS;
        chargeShieldsCode();
      } else {
        //				nextSoldierState = SoldierState.RALLYING;
        //				rallyingCode();
        if (Clock.getRoundNum() < 200) {
          nextSoldierState = SoldierState.PUSHING;
          pushingCode();
        } else {
          nextSoldierState = SoldierState.RALLYING;
          rallyingCode();
        }
      }
    } else if (strategy == Strategy.NUKE && DataCache.numEnemyRobots > 1) {
      //			nextSoldierState = SoldierState.FIGHTING;
      //			fightingCode();
      if (shieldExists()) {
        nextSoldierState = SoldierState.CHARGE_SHIELDS;
        chargeShieldsCode();
      } else {
        //				nextSoldierState = SoldierState.RALLYING;
        //				rallyingCode();
        if (Clock.getRoundNum() < 200) {
          nextSoldierState = SoldierState.PUSHING;
          pushingCode();
        } else {
          nextSoldierState = SoldierState.RALLYING;
          rallyingCode();
        }
      }
    } else if (strategy == Strategy.NUKE && DataCache.numEnemyRobots == 1) {
      Robot[] enemyRobots = rc.senseNearbyGameObjects(Robot.class, 14, rc.getTeam().opponent());
      if (enemyRobots.length == 1) {
        RobotInfo rinfo = rc.senseRobotInfo(enemyRobots[0]);
        NavSystem.goToLocation(rinfo.location);
      } else {
        miningSubroutine();
      }
    } else if (strategy != Strategy.NUKE
        && (hqPowerLevel < 10 * (1 + DataCache.numAlliedEncampments) || hqPowerLevel < 100)) {
      //			nextSoldierState = SoldierState.PUSHING;
      //			pushingCode();
      if (shieldExists()) {
        nextSoldierState = SoldierState.CHARGE_SHIELDS;
        chargeShieldsCode();
      }
    } else {
      miningSubroutine();
    }
  }
 /**
  * Push code that uses smart nav
  *
  * @throws GameActionException
  */
 public void pushCodeSmart() throws GameActionException {
   if (NavSystem.navMode != NavMode.SMART || NavSystem.destination != DataCache.enemyHQLocation) {
     NavSystem.setupSmartNav(DataCache.enemyHQLocation);
   }
   NavSystem.followWaypoints(true, true);
 }