Search in sources :

Example 6 with IBattle

use of games.strategy.triplea.delegate.IBattle in project triplea by triplea-game.

the class ProAi method scrambleUnitsQuery.

@Override
public HashMap<Territory, Collection<Unit>> scrambleUnitsQuery(final Territory scrambleTo, final Map<Territory, Tuple<Collection<Unit>, Collection<Unit>>> possibleScramblers) {
    initializeData();
    // Get battle data
    final GameData data = getGameData();
    final PlayerID player = getPlayerId();
    final BattleDelegate delegate = DelegateFinder.battleDelegate(data);
    final IBattle battle = delegate.getBattleTracker().getPendingBattle(scrambleTo, false, BattleType.NORMAL);
    // If battle is null then don't scramble
    if (battle == null) {
        return null;
    }
    final List<Unit> attackers = (List<Unit>) battle.getAttackingUnits();
    final List<Unit> defenders = (List<Unit>) battle.getDefendingUnits();
    ProLogger.info(player.getName() + " checking scramble to " + scrambleTo + ", attackers=" + attackers.size() + ", defenders=" + defenders.size() + ", possibleScramblers=" + possibleScramblers);
    calc.setData(getGameData());
    return scrambleAi.scrambleUnitsQuery(scrambleTo, possibleScramblers);
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) BattleDelegate(games.strategy.triplea.delegate.BattleDelegate) GameData(games.strategy.engine.data.GameData) IBattle(games.strategy.triplea.delegate.IBattle) List(java.util.List) ArrayList(java.util.ArrayList) CasualtyList(games.strategy.triplea.delegate.dataObjects.CasualtyList) Unit(games.strategy.engine.data.Unit)

Example 7 with IBattle

use of games.strategy.triplea.delegate.IBattle in project triplea by triplea-game.

the class ProRetreatAi method retreatQuery.

Territory retreatQuery(final GUID battleId, final Territory battleTerritory, final Collection<Territory> possibleTerritories) {
    // Get battle data
    final GameData data = ProData.getData();
    final PlayerID player = ProData.getPlayer();
    final BattleDelegate delegate = DelegateFinder.battleDelegate(data);
    final IBattle battle = delegate.getBattleTracker().getPendingBattle(battleId);
    // Get units and determine if attacker
    final boolean isAttacker = player.equals(battle.getAttacker());
    final List<Unit> attackers = (List<Unit>) battle.getAttackingUnits();
    final List<Unit> defenders = (List<Unit>) battle.getDefendingUnits();
    // Calculate battle results
    final ProBattleResult result = calc.calculateBattleResults(battleTerritory, attackers, defenders, new HashSet<>());
    // Determine if it has a factory
    int isFactory = 0;
    if (ProMatches.territoryHasInfraFactoryAndIsLand().test(battleTerritory)) {
        isFactory = 1;
    }
    // Determine production value and if it is a capital
    int production = 0;
    int isCapital = 0;
    final TerritoryAttachment ta = TerritoryAttachment.get(battleTerritory);
    if (ta != null) {
        production = ta.getProduction();
        if (ta.isCapital()) {
            isCapital = 1;
        }
    }
    // Calculate current attack value
    double territoryValue = 0;
    if (result.isHasLandUnitRemaining() || attackers.stream().noneMatch(Matches.unitIsAir())) {
        territoryValue = result.getWinPercentage() / 100 * (2 * production * (1 + isFactory) * (1 + isCapital));
    }
    double battleValue = result.getTuvSwing() + territoryValue;
    if (!isAttacker) {
        battleValue = -battleValue;
    }
    // Decide if we should retreat
    if (battleValue < 0) {
        // Retreat to capital if available otherwise the territory with highest defense strength
        Territory retreatTerritory = null;
        double maxStrength = Double.NEGATIVE_INFINITY;
        final Territory myCapital = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(player, data);
        for (final Territory t : possibleTerritories) {
            if (t.equals(myCapital)) {
                retreatTerritory = t;
                break;
            }
            final double strength = ProBattleUtils.estimateStrength(t, t.getUnits().getMatches(Matches.isUnitAllied(player, data)), new ArrayList<>(), false);
            if (strength > maxStrength) {
                retreatTerritory = t;
                maxStrength = strength;
            }
        }
        ProLogger.debug(player.getName() + " retreating from territory " + battleTerritory + " to " + retreatTerritory + " because AttackValue=" + battleValue + ", TUVSwing=" + result.getTuvSwing() + ", possibleTerritories=" + possibleTerritories.size());
        return retreatTerritory;
    }
    ProLogger.debug(player.getName() + " not retreating from territory " + battleTerritory + " with AttackValue=" + battleValue + ", TUVSwing=" + result.getTuvSwing());
    return null;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) BattleDelegate(games.strategy.triplea.delegate.BattleDelegate) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) Unit(games.strategy.engine.data.Unit) IBattle(games.strategy.triplea.delegate.IBattle) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with IBattle

use of games.strategy.triplea.delegate.IBattle in project triplea by triplea-game.

the class ProScrambleAi method scrambleUnitsQuery.

HashMap<Territory, Collection<Unit>> scrambleUnitsQuery(final Territory scrambleTo, final Map<Territory, Tuple<Collection<Unit>, Collection<Unit>>> possibleScramblers) {
    // Get battle data
    final GameData data = ProData.getData();
    final PlayerID player = ProData.getPlayer();
    final BattleDelegate delegate = DelegateFinder.battleDelegate(data);
    final IBattle battle = delegate.getBattleTracker().getPendingBattle(scrambleTo, false, BattleType.NORMAL);
    // Check if defense already wins
    final List<Unit> attackers = (List<Unit>) battle.getAttackingUnits();
    final List<Unit> defenders = (List<Unit>) battle.getDefendingUnits();
    final Set<Unit> bombardingUnits = new HashSet<>(battle.getBombardingUnits());
    final ProBattleResult minResult = calc.calculateBattleResults(scrambleTo, attackers, defenders, bombardingUnits);
    ProLogger.debug(scrambleTo + ", minTUVSwing=" + minResult.getTuvSwing() + ", minWin%=" + minResult.getWinPercentage());
    if (minResult.getTuvSwing() <= 0 && minResult.getWinPercentage() < (100 - ProData.minWinPercentage)) {
        return null;
    }
    // Check if max defense is worse
    final Set<Unit> allScramblers = new HashSet<>();
    final Map<Territory, List<Unit>> possibleMaxScramblerMap = new HashMap<>();
    for (final Territory t : possibleScramblers.keySet()) {
        final int maxCanScramble = BattleDelegate.getMaxScrambleCount(possibleScramblers.get(t).getFirst());
        List<Unit> canScrambleAir = new ArrayList<>(possibleScramblers.get(t).getSecond());
        if (maxCanScramble < canScrambleAir.size()) {
            canScrambleAir.sort(Comparator.comparingDouble(o -> ProBattleUtils.estimateStrength(scrambleTo, Collections.singletonList(o), new ArrayList<>(), false)));
            canScrambleAir = canScrambleAir.subList(0, maxCanScramble);
        }
        allScramblers.addAll(canScrambleAir);
        possibleMaxScramblerMap.put(t, canScrambleAir);
    }
    defenders.addAll(allScramblers);
    final ProBattleResult maxResult = calc.calculateBattleResults(scrambleTo, attackers, defenders, bombardingUnits);
    ProLogger.debug(scrambleTo + ", maxTUVSwing=" + maxResult.getTuvSwing() + ", maxWin%=" + maxResult.getWinPercentage());
    if (maxResult.getTuvSwing() >= minResult.getTuvSwing()) {
        return null;
    }
    // Loop through all units and determine attack options
    final Map<Unit, Set<Territory>> unitDefendOptions = new HashMap<>();
    for (final Territory t : possibleMaxScramblerMap.keySet()) {
        final Set<Territory> possibleTerritories = data.getMap().getNeighbors(t, ProMatches.territoryCanMoveSeaUnits(player, data, true));
        possibleTerritories.add(t);
        final Set<Territory> battleTerritories = new HashSet<>();
        for (final Territory possibleTerritory : possibleTerritories) {
            final IBattle possibleBattle = delegate.getBattleTracker().getPendingBattle(possibleTerritory, false, BattleType.NORMAL);
            if (possibleBattle != null) {
                battleTerritories.add(possibleTerritory);
            }
        }
        for (final Unit u : possibleMaxScramblerMap.get(t)) {
            unitDefendOptions.put(u, battleTerritories);
        }
    }
    // Sort units by number of defend options and cost
    final Map<Unit, Set<Territory>> sortedUnitDefendOptions = ProSortMoveOptionsUtils.sortUnitMoveOptions(unitDefendOptions);
    // Add one scramble unit at a time and check if final result is better than min result
    final List<Unit> unitsToScramble = new ArrayList<>();
    ProBattleResult result = minResult;
    for (final Unit u : sortedUnitDefendOptions.keySet()) {
        unitsToScramble.add(u);
        final List<Unit> currentDefenders = (List<Unit>) battle.getDefendingUnits();
        currentDefenders.addAll(unitsToScramble);
        result = calc.calculateBattleResults(scrambleTo, attackers, currentDefenders, bombardingUnits);
        ProLogger.debug(scrambleTo + ", TUVSwing=" + result.getTuvSwing() + ", Win%=" + result.getWinPercentage() + ", addedUnit=" + u);
        if (result.getTuvSwing() <= 0 && result.getWinPercentage() < (100 - ProData.minWinPercentage)) {
            break;
        }
    }
    if (result.getTuvSwing() >= minResult.getTuvSwing()) {
        return null;
    }
    // Return units to scramble
    final HashMap<Territory, Collection<Unit>> scrambleMap = new HashMap<>();
    for (final Territory t : possibleScramblers.keySet()) {
        for (final Unit u : possibleScramblers.get(t).getSecond()) {
            if (unitsToScramble.contains(u)) {
                if (scrambleMap.containsKey(t)) {
                    scrambleMap.get(t).add(u);
                } else {
                    final Collection<Unit> units = new ArrayList<>();
                    units.add(u);
                    scrambleMap.put(t, units);
                }
            }
        }
    }
    return scrambleMap;
}
Also used : ProSortMoveOptionsUtils(games.strategy.triplea.ai.pro.util.ProSortMoveOptionsUtils) Unit(games.strategy.engine.data.Unit) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) IBattle(games.strategy.triplea.delegate.IBattle) Collection(java.util.Collection) Set(java.util.Set) Territory(games.strategy.engine.data.Territory) HashMap(java.util.HashMap) ProBattleUtils(games.strategy.triplea.ai.pro.util.ProBattleUtils) ArrayList(java.util.ArrayList) ProLogger(games.strategy.triplea.ai.pro.logging.ProLogger) HashSet(java.util.HashSet) GameData(games.strategy.engine.data.GameData) BattleType(games.strategy.triplea.delegate.IBattle.BattleType) List(java.util.List) DelegateFinder(games.strategy.triplea.delegate.DelegateFinder) Tuple(games.strategy.util.Tuple) PlayerID(games.strategy.engine.data.PlayerID) ProMatches(games.strategy.triplea.ai.pro.util.ProMatches) Map(java.util.Map) BattleDelegate(games.strategy.triplea.delegate.BattleDelegate) ProOddsCalculator(games.strategy.triplea.ai.pro.util.ProOddsCalculator) Comparator(java.util.Comparator) Collections(java.util.Collections) PlayerID(games.strategy.engine.data.PlayerID) BattleDelegate(games.strategy.triplea.delegate.BattleDelegate) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) Unit(games.strategy.engine.data.Unit) IBattle(games.strategy.triplea.delegate.IBattle) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

IBattle (games.strategy.triplea.delegate.IBattle)8 PlayerID (games.strategy.engine.data.PlayerID)7 Unit (games.strategy.engine.data.Unit)7 BattleDelegate (games.strategy.triplea.delegate.BattleDelegate)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 GameData (games.strategy.engine.data.GameData)6 ProBattleResult (games.strategy.triplea.ai.pro.data.ProBattleResult)4 CasualtyList (games.strategy.triplea.delegate.dataObjects.CasualtyList)4 Territory (games.strategy.engine.data.Territory)3 BattleType (games.strategy.triplea.delegate.IBattle.BattleType)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Change (games.strategy.engine.data.Change)1 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)1 ProLogger (games.strategy.triplea.ai.pro.logging.ProLogger)1 ProBattleUtils (games.strategy.triplea.ai.pro.util.ProBattleUtils)1 ProMatches (games.strategy.triplea.ai.pro.util.ProMatches)1 ProOddsCalculator (games.strategy.triplea.ai.pro.util.ProOddsCalculator)1 ProSortMoveOptionsUtils (games.strategy.triplea.ai.pro.util.ProSortMoveOptionsUtils)1