Exemple #1
0
  /**
   * Returns collection of reinforcements for given score factor, ordered by descending score.
   *
   * @param overworld
   * @param player
   * @param minScore
   * @param scoreFactor
   * @return
   */
  protected static Collection<Move> getReinforcements(
      Overworld overworld, Player player, ScoreFactor scoreThreshold, ScoreFactor scoreFactor) {

    Map<Float, Move> reinforcements = new TreeMap<Float, Move>();

    // go through each region
    for (Region region : overworld.regions) {
      if (region.player.equals(player)
          && (region.unit == null || region.unit.health < Unit.MAX_HEALTH)) {

        // get optimal reinforcement type for region
        ReinforcementType reinforcementType = getBestReinforcement(region);

        ScoreFactor score = new ScoreFactor();
        float scoreValue = 0;
        boolean aboveThreshold = true;

        // get each score type
        for (ScoreFactor.Type type : ScoreFactor.Type.values()) {
          if (scoreThreshold.usesType(type) || scoreFactor.usesType(type)) {
            switch (type) {
              case BATTLE_STRENGTH_CHANGE:
                score.addFactor(type, reinforcementType.strengthChange);
                break;
              case ISLAND_PERCENTAGE:
                score.addFactor(type, RegionUtils.getIslandPercentage(region.island, player));
                break;
              case ISLAND_STRENGTH:
                score.addFactor(type, RegionUtils.getIslandStrength(region.island, player));
                break;
              case ISLAND_STRENGTH_CHANGE:
                score.addFactor(type, getIslandStrengthChange(region, reinforcementType, player));
                break;
              case RANDOM:
                score.addFactor(type, Utils.random().nextFloat());
                break;
              case BATTLE_LIKELIHOOD:
                // ignore
                break;
            }

            // check if score meets threshold
            if (scoreThreshold.usesType(type)) {
              aboveThreshold &= score.getScore(type) >= scoreThreshold.getScore(type);
            }

            // update score value
            if (scoreFactor.usesType(type)) {
              scoreValue += score.getScore(type) * scoreFactor.getScore(type);
            }
          }
        }

        // score above threshold
        if (aboveThreshold) {
          reinforcements.put(
              scoreValue, new Move(Move.Type.REINFORCE, region, null, reinforcementType.unitType));
          System.out.println(
              String.format(
                  "\tpossible reinforcement %f: %s", scoreValue, reinforcements.get(scoreValue)));
          System.out.println("\t\t" + score);
        }
      }
    }

    return reinforcements.values();
  }
Exemple #2
0
  /**
   * Returns collection of moves for given score factor, ordered by descending score.
   *
   * @param overworld
   * @param player
   * @param scoreThreshold
   * @param scoreFactor
   * @return
   */
  protected static Collection<Move> getMoves(
      Overworld overworld,
      Player player,
      ScoreFactor scoreThreshold,
      ScoreFactor captureScoreThreshold,
      ScoreFactor scoreFactor) {

    Map<Float, Move> moves = new TreeMap<Float, Move>();

    // go through each region
    for (Region region : overworld.regions) {
      if (region.player.equals(player) && region.unit != null && region.unit.hasMove) {

        // go through each neighbor
        for (Neighbor neighbor : region.neighbors) {
          if (RegionUtils.canMove(region, neighbor)) {

            ScoreFactor score = new ScoreFactor();
            float scoreValue = 0;
            boolean aboveThreshold = true;

            // get each score type
            for (ScoreFactor.Type type : ScoreFactor.Type.values()) {
              if (scoreThreshold.usesType(type) || scoreFactor.usesType(type)) {
                switch (type) {
                  case BATTLE_STRENGTH_CHANGE:
                    score.addFactor(type, getBattleStrengthChange(region, neighbor, true));
                    break;
                  case ISLAND_PERCENTAGE:
                    score.addFactor(type, RegionUtils.getIslandPercentage(region.island, player));
                    break;
                  case ISLAND_STRENGTH:
                    score.addFactor(type, RegionUtils.getIslandStrength(region.island, player));
                    break;
                  case RANDOM:
                    score.addFactor(type, Utils.random().nextFloat());
                    break;
                  case BATTLE_LIKELIHOOD:
                  case ISLAND_STRENGTH_CHANGE:
                    // ignore
                    break;
                }

                // check if score meets threshold
                // capture threshold
                if (!neighbor.region.player.equals(region.player) && neighbor.region.unit == null) {
                  if (captureScoreThreshold.usesType(type)) {
                    aboveThreshold &= score.getScore(type) >= captureScoreThreshold.getScore(type);
                  }
                }
                // regular move threshold
                else {
                  if (scoreThreshold.usesType(type)) {
                    aboveThreshold &= score.getScore(type) >= scoreThreshold.getScore(type);
                  }
                }

                // update score value
                if (scoreFactor.usesType(type)) {
                  scoreValue += score.getScore(type) * scoreFactor.getScore(type);
                }
              }
            }

            // score above threshold
            if (aboveThreshold) {
              moves.put(scoreValue, new Move(Move.Type.MOVE, region, neighbor.region));
              System.out.println(
                  String.format("\tpossible move %f: %s", scoreValue, moves.get(scoreValue)));
              System.out.println("\t\t" + score);
            }
          }
        }
      }
    }

    return moves.values();
  }
Exemple #3
0
  /**
   * Returns collection of attacks for given score factor, ordered by descending score.
   *
   * @param overworld
   * @param player
   * @param scoreThreshold
   * @param scoreFactor
   * @return
   */
  protected static Collection<Move> getAttacks(
      Overworld overworld, Player player, ScoreFactor scoreThreshold, ScoreFactor scoreFactor) {

    Map<Float, Move> attacks = new TreeMap<Float, Move>();

    // go through each region
    for (Region region : overworld.regions) {
      if (region.player.equals(player) && region.unit != null && region.unit.hasAttack) {

        // check each neighbor
        for (Neighbor neighbor : region.neighbors) {
          if (RegionUtils.canAttack(region, neighbor)) {

            ScoreFactor score = new ScoreFactor();
            float scoreValue = 0;
            boolean aboveThreshold = true;

            // get each score type
            for (ScoreFactor.Type type : ScoreFactor.Type.values()) {
              if (scoreThreshold.usesType(type) || scoreFactor.usesType(type)) {
                switch (type) {
                  case BATTLE_STRENGTH_CHANGE:
                    if (neighbor.type != Neighbor.Type.RANGED) {
                      score.addFactor(type, getBattleStrengthChange(region, neighbor, false));
                    } else {
                      score.addFactor(type, 0.5f);
                    }
                    break;
                  case BATTLE_LIKELIHOOD:
                    score.addFactor(
                        type, BattleUtils.calculateBattleLikelihood(region, neighbor.region));
                    break;
                  case ISLAND_PERCENTAGE:
                    score.addFactor(type, RegionUtils.getIslandPercentage(region.island, player));
                    break;
                  case ISLAND_STRENGTH:
                    score.addFactor(type, RegionUtils.getIslandStrength(region.island, player));
                    break;
                  case RANDOM:
                    score.addFactor(type, Utils.random().nextFloat());
                    break;
                  case ISLAND_STRENGTH_CHANGE:
                    // ignore
                    break;
                }

                // check if score meets threshold
                if (scoreThreshold.usesType(type)) {
                  aboveThreshold &= score.getScore(type) >= scoreThreshold.getScore(type);
                }

                // update score value
                if (scoreFactor.usesType(type)) {
                  scoreValue += score.getScore(type) * scoreFactor.getScore(type);
                }
              }
            }

            // score above threshold
            if (aboveThreshold) {
              attacks.put(scoreValue, new Move(Move.Type.ATTACK, region, neighbor.region));
              System.out.println(
                  String.format("\tpossible attack %f: %s", scoreValue, attacks.get(scoreValue)));
              System.out.println("\t\t" + score);
            }
          }
        }
      }
    }

    return attacks.values();
  }