Example #1
0
  /**
   * Returns random reinforcement.
   *
   * @param overworld overworld
   * @param player player
   * @param forceReinforcement true if we want to force reinforcements to happen, if possible
   * @param onlyNew true if we only want new reinforcements
   * @return random reinforcement
   */
  protected static Move getRandomReinforcement(
      Overworld overworld, Player player, boolean forceReinforcement, boolean onlyNew) {

    // player can't make any reinforcements
    if (player.reinforcements <= 0) {
      return new Move(Move.Type.END_PHASE);
    }

    List<Region> regions = PlayerUtils.getPlayerRegions(overworld, player, false);

    // choose random region to take action with
    int regionIndex = Utils.random().nextInt(regions.size());
    int regionStartIndex = regionIndex;

    // check if region can take action

    // if region can't be reinforced, check other regions if we force reinforcements
    while (forceReinforcement
        && ((onlyNew && regions.get(regionIndex).unit != null)
            || (regions.get(regionIndex).unit != null
                && regions.get(regionIndex).unit.health == Unit.MAX_HEALTH))) {
      regionIndex++;

      if (regionIndex >= regions.size()) {
        regionIndex = 0;
      }

      // exit loop if we've checked all regions
      if (regionIndex == regionStartIndex) {
        break;
      }
    }

    // reinforce new region
    if (regions.get(regionIndex).unit == null) {
      return new Move(
          Move.Type.REINFORCE,
          regions.get(regionIndex),
          Unit.Type.values()[Utils.random().nextInt(Unit.Type.values().length)]);
    }
    // reinforce existing region
    else if (!onlyNew && regions.get(regionIndex).unit.health < Unit.MAX_HEALTH) {
      return new Move(Move.Type.REINFORCE, regions.get(regionIndex));
    }

    return new Move(Move.Type.END_PHASE);
  }
Example #2
0
  /**
   * Returns best reinforcement for given region.
   *
   * @param region
   * @return
   */
  protected static ReinforcementType getBestReinforcement(Region region) {

    ReinforcementType result = new ReinforcementType();

    // existing unit
    if (region.unit != null) {
      float originalRegionHealth = region.unit.health;
      float originalBattleStrength = RegionUtils.getRegionBattleStrength(region);

      // simulate max health
      region.unit.health = Unit.MAX_HEALTH;
      result.strengthChange = RegionUtils.getRegionBattleStrength(region) - originalBattleStrength;
      region.unit.health = originalRegionHealth;
    }
    // new unit
    else {

      // get optimal unit type
      // check each possible unit type
      for (Unit.Type unitType : Unit.Type.values()) {
        region.unit = new Unit(unitType, Unit.MAX_HEALTH);

        float regionBattleStrength = RegionUtils.getRegionBattleStrength(region);

        if (result.unitType == null || regionBattleStrength > result.strengthChange) {
          result.strengthChange = regionBattleStrength;
          result.unitType = unitType;
        }
      }

      // reset region to have no unit
      region.unit = null;
    }

    return result;
  }