Example #1
0
  /**
   * Returns change in island strength for given reinforcement
   *
   * @param region
   * @param reinforcementType
   * @param player
   * @return
   */
  private static float getIslandStrengthChange(
      Region region, ReinforcementType reinforcementType, Player player) {

    float regionUnitHealth = -1f;
    float regionIslandStrength = RegionUtils.getIslandStrength(region.island, player);

    // add reinforcement, see change in island strength
    if (reinforcementType.unitType != null) {
      region.unit = new Unit(reinforcementType.unitType, Unit.MAX_HEALTH);
    } else {
      regionUnitHealth = region.unit.health;
      region.unit.health = Unit.MAX_HEALTH;
    }

    float regionIslandReinforcementStrength = RegionUtils.getIslandStrength(region.island, player);

    // remove reinforcement
    if (reinforcementType.unitType != null) {
      region.unit = null;
    } else {
      region.unit.health = regionUnitHealth;
    }

    return (regionIslandReinforcementStrength - regionIslandStrength)
        / regionIslandReinforcementStrength;
  }
Example #2
0
  /**
   * Returns change in battle strength if unit at region switches places with neighbor.
   *
   * @param region
   * @param neighbor
   * @param includeNeighborStrengthChange true if change in neighbor strength should be included
   * @return
   */
  private static float getBattleStrengthChange(
      Region region, Neighbor neighbor, boolean includeNeighborStrengthChange) {
    // get current strengths of region, neighbor
    float regionStrength = RegionUtils.getRegionBattleStrength(region);
    float neighborStrength = RegionUtils.getRegionBattleStrength(neighbor.region);

    // swap units, see change in strengths
    Unit tempUnit = region.unit;
    region.unit = neighbor.region.unit;
    neighbor.region.unit = tempUnit;

    Player tempPlayer = region.player;
    region.player = neighbor.region.player;
    neighbor.region.player = tempPlayer;

    float regionMoveStrength = RegionUtils.getRegionBattleStrength(region);
    float neighborMoveStrength = RegionUtils.getRegionBattleStrength(neighbor.region);

    // put units back
    tempUnit = region.unit;
    region.unit = neighbor.region.unit;
    neighbor.region.unit = tempUnit;

    tempPlayer = region.player;
    region.player = neighbor.region.player;
    neighbor.region.player = tempPlayer;

    if (includeNeighborStrengthChange) {
      return (regionMoveStrength + neighborMoveStrength - regionStrength - neighborStrength + 2)
          / 4;
    } else {
      return (regionMoveStrength - regionStrength + 1) / 2;
    }
  }
Example #3
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;
  }
Example #4
0
  /**
   * Returns changes in region strengths for each possible move.
   *
   * @param region
   * @return
   */
  protected static Map<Neighbor, Float> checkMoves(Region region) {
    Map<Neighbor, Float> result = new HashMap<Neighbor, Float>();

    for (Neighbor neighbor : region.neighbors) {
      // check if neighbor can be moved to
      if (RegionUtils.canMove(region, neighbor)) {
        // get current strengths of region, neighbor
        float regionStrength = RegionUtils.getRegionBattleStrength(region);
        float neighborStrength = RegionUtils.getRegionBattleStrength(neighbor.region);

        // swap units, see change in strengths
        Unit tempUnit = region.unit;
        region.unit = neighbor.region.unit;
        neighbor.region.unit = tempUnit;

        Player tempPlayer = region.player;
        region.player = neighbor.region.player;
        neighbor.region.player = tempPlayer;

        float regionMoveStrength = RegionUtils.getRegionBattleStrength(region);
        float neighborMoveStrength = RegionUtils.getRegionBattleStrength(neighbor.region);

        // put units back
        tempUnit = region.unit;
        region.unit = neighbor.region.unit;
        neighbor.region.unit = tempUnit;

        tempPlayer = region.player;
        region.player = neighbor.region.player;
        neighbor.region.player = tempPlayer;

        result.put(
            neighbor,
            regionMoveStrength + neighborMoveStrength - regionStrength - neighborStrength);
      }
    }

    return result;
  }