Search in sources :

Example 1 with AggregateResults

use of games.strategy.triplea.oddsCalculator.ta.AggregateResults in project triplea by triplea-game.

the class ProOddsCalculator method callBattleCalculator.

public ProBattleResult callBattleCalculator(final Territory t, final List<Unit> attackingUnits, final List<Unit> defendingUnits, final Set<Unit> bombardingUnits, final boolean retreatWhenOnlyAirLeft) {
    final GameData data = ProData.getData();
    if (isCanceled || attackingUnits.isEmpty() || defendingUnits.isEmpty()) {
        return new ProBattleResult();
    }
    final int minArmySize = Math.min(attackingUnits.size(), defendingUnits.size());
    final int runCount = Math.max(16, 100 - minArmySize);
    final PlayerID attacker = attackingUnits.get(0).getOwner();
    final PlayerID defender = defendingUnits.get(0).getOwner();
    if (retreatWhenOnlyAirLeft) {
        calc.setRetreatWhenOnlyAirLeft(true);
    }
    final AggregateResults results = calc.setCalculateDataAndCalculate(attacker, defender, t, attackingUnits, defendingUnits, new ArrayList<>(bombardingUnits), TerritoryEffectHelper.getEffects(t), runCount);
    if (retreatWhenOnlyAirLeft) {
        calc.setRetreatWhenOnlyAirLeft(false);
    }
    // Find battle result statistics
    final double winPercentage = results.getAttackerWinPercent() * 100;
    final List<Unit> averageAttackersRemaining = results.getAverageAttackingUnitsRemaining();
    final List<Unit> averageDefendersRemaining = results.getAverageDefendingUnitsRemaining();
    final List<Unit> mainCombatAttackers = CollectionUtils.getMatches(attackingUnits, Matches.unitCanBeInBattle(true, !t.isWater(), 1, false, true, true));
    final List<Unit> mainCombatDefenders = CollectionUtils.getMatches(defendingUnits, Matches.unitCanBeInBattle(false, !t.isWater(), 1, false, true, true));
    double tuvSwing = results.getAverageTuvSwing(attacker, mainCombatAttackers, defender, mainCombatDefenders, data);
    if (Matches.territoryIsNeutralButNotWater().test(t)) {
        // Set TUV swing for neutrals
        final double attackingUnitValue = TuvUtils.getTuv(mainCombatAttackers, ProData.unitValueMap);
        final double remainingUnitValue = results.getAverageTuvOfUnitsLeftOver(ProData.unitValueMap, ProData.unitValueMap).getFirst();
        tuvSwing = remainingUnitValue - attackingUnitValue;
    }
    final List<Unit> defendingTransportedUnits = CollectionUtils.getMatches(defendingUnits, Matches.unitIsBeingTransported());
    if (t.isWater() && !defendingTransportedUnits.isEmpty()) {
        // Add TUV swing for transported units
        final double transportedUnitValue = TuvUtils.getTuv(defendingTransportedUnits, ProData.unitValueMap);
        tuvSwing += transportedUnitValue * winPercentage / 100;
    }
    // Create battle result object
    final List<Territory> territoryList = new ArrayList<>();
    territoryList.add(t);
    return (!territoryList.isEmpty() && territoryList.stream().allMatch(Matches.territoryIsLand())) ? new ProBattleResult(winPercentage, tuvSwing, averageAttackersRemaining.stream().anyMatch(Matches.unitIsLand()), averageAttackersRemaining, averageDefendersRemaining, results.getAverageBattleRoundsFought()) : new ProBattleResult(winPercentage, tuvSwing, !averageAttackersRemaining.isEmpty(), averageAttackersRemaining, averageDefendersRemaining, results.getAverageBattleRoundsFought());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) AggregateResults(games.strategy.triplea.oddsCalculator.ta.AggregateResults) ArrayList(java.util.ArrayList) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) Unit(games.strategy.engine.data.Unit)

Aggregations

GameData (games.strategy.engine.data.GameData)1 PlayerID (games.strategy.engine.data.PlayerID)1 Territory (games.strategy.engine.data.Territory)1 Unit (games.strategy.engine.data.Unit)1 ProBattleResult (games.strategy.triplea.ai.pro.data.ProBattleResult)1 AggregateResults (games.strategy.triplea.oddsCalculator.ta.AggregateResults)1 ArrayList (java.util.ArrayList)1