Search in sources :

Example 1 with IBattle

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

the class ProAi method retreatQuery.

@Override
public Territory retreatQuery(final GUID battleId, final boolean submerge, final Territory battleTerritory, final Collection<Territory> possibleTerritories, final String message) {
    initializeData();
    // Get battle data
    final GameData data = getGameData();
    final PlayerID player = getPlayerId();
    final BattleDelegate delegate = DelegateFinder.battleDelegate(data);
    final IBattle battle = delegate.getBattleTracker().getPendingBattle(battleId);
    // If battle is null or amphibious then don't retreat
    if (battle == null || battleTerritory == null || battle.isAmphibious()) {
        return null;
    }
    // If attacker with more unit strength or strafing and isn't land battle with only air left then don't retreat
    final boolean isAttacker = player.equals(battle.getAttacker());
    final List<Unit> attackers = (List<Unit>) battle.getAttackingUnits();
    final List<Unit> defenders = (List<Unit>) battle.getDefendingUnits();
    final double strengthDifference = ProBattleUtils.estimateStrengthDifference(battleTerritory, attackers, defenders);
    final boolean isStrafing = isAttacker && storedStrafingTerritories.contains(battleTerritory);
    ProLogger.info(player.getName() + " checking retreat from territory " + battleTerritory + ", attackers=" + attackers.size() + ", defenders=" + defenders.size() + ", submerge=" + submerge + ", attacker=" + isAttacker + ", isStrafing=" + isStrafing);
    if ((isStrafing || (isAttacker && strengthDifference > 50)) && (battleTerritory.isWater() || attackers.stream().anyMatch(Matches.unitIsLand()))) {
        return null;
    }
    calc.setData(getGameData());
    return retreatAi.retreatQuery(battleId, battleTerritory, possibleTerritories);
}
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 2 with IBattle

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

the class ProAi method selectCasualties.

@Override
public CasualtyDetails selectCasualties(final Collection<Unit> selectFrom, final Map<Unit, Collection<Unit>> dependents, final int count, final String message, final DiceRoll dice, final PlayerID hit, final Collection<Unit> friendlyUnits, final PlayerID enemyPlayer, final Collection<Unit> enemyUnits, final boolean amphibious, final Collection<Unit> amphibiousLandAttackers, final CasualtyList defaultCasualties, final GUID battleId, final Territory battlesite, final boolean allowMultipleHitsPerUnit) {
    initializeData();
    if (defaultCasualties.size() != count) {
        throw new IllegalStateException("Select Casualties showing different numbers for number of hits to take vs total " + "size of default casualty selections");
    }
    if (defaultCasualties.getKilled().size() <= 0) {
        return new CasualtyDetails(defaultCasualties, false);
    }
    // Consider unit cost
    final CasualtyDetails myCasualties = new CasualtyDetails(false);
    myCasualties.addToDamaged(defaultCasualties.getDamaged());
    final List<Unit> selectFromSorted = new ArrayList<>(selectFrom);
    if (enemyUnits.isEmpty()) {
        selectFromSorted.sort(ProPurchaseUtils.getCostComparator());
    } else {
        // Get battle data
        final GameData data = getGameData();
        final PlayerID player = getPlayerId();
        final BattleDelegate delegate = DelegateFinder.battleDelegate(data);
        final IBattle battle = delegate.getBattleTracker().getPendingBattle(battleId);
        // If defender and could lose battle then don't consider unit cost as just trying to survive
        boolean needToCheck = true;
        final boolean isAttacker = player.equals(battle.getAttacker());
        if (!isAttacker) {
            final List<Unit> attackers = (List<Unit>) battle.getAttackingUnits();
            final List<Unit> defenders = (List<Unit>) battle.getDefendingUnits();
            defenders.removeAll(defaultCasualties.getKilled());
            final double strengthDifference = ProBattleUtils.estimateStrengthDifference(battlesite, attackers, defenders);
            int minStrengthDifference = 60;
            if (!Properties.getLowLuck(data)) {
                minStrengthDifference = 55;
            }
            if (strengthDifference > minStrengthDifference) {
                needToCheck = false;
            }
        }
        // Use bubble sort to save expensive units
        while (needToCheck) {
            needToCheck = false;
            for (int i = 0; i < selectFromSorted.size() - 1; i++) {
                final Unit unit1 = selectFromSorted.get(i);
                final Unit unit2 = selectFromSorted.get(i + 1);
                final double unitCost1 = ProPurchaseUtils.getCost(unit1);
                final double unitCost2 = ProPurchaseUtils.getCost(unit2);
                if (unitCost1 > 1.5 * unitCost2) {
                    selectFromSorted.set(i, unit2);
                    selectFromSorted.set(i + 1, unit1);
                    needToCheck = true;
                }
            }
        }
    }
    // Interleave carriers and planes
    final List<Unit> interleavedTargetList = new ArrayList<>(ProTransportUtils.interleaveUnitsCarriersAndPlanes(selectFromSorted, 0));
    for (int i = 0; i < defaultCasualties.getKilled().size(); ++i) {
        myCasualties.addToKilled(interleavedTargetList.get(i));
    }
    if (count != myCasualties.size()) {
        throw new IllegalStateException("AI chose wrong number of casualties");
    }
    return myCasualties;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) BattleDelegate(games.strategy.triplea.delegate.BattleDelegate) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit) IBattle(games.strategy.triplea.delegate.IBattle) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) List(java.util.List) ArrayList(java.util.ArrayList) CasualtyList(games.strategy.triplea.delegate.dataObjects.CasualtyList)

Example 3 with IBattle

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

the class ProAi method selectAttackSubs.

@Override
public boolean selectAttackSubs(final Territory unitTerritory) {
    initializeData();
    // Get battle data
    final GameData data = getGameData();
    final PlayerID player = getPlayerId();
    final BattleDelegate delegate = DelegateFinder.battleDelegate(data);
    final IBattle battle = delegate.getBattleTracker().getPendingBattle(unitTerritory, false, BattleType.NORMAL);
    // If battle is null then don't attack
    if (battle == null) {
        return false;
    }
    final List<Unit> attackers = (List<Unit>) battle.getAttackingUnits();
    final List<Unit> defenders = (List<Unit>) battle.getDefendingUnits();
    ProLogger.info(player.getName() + " checking sub attack in " + unitTerritory + ", attackers=" + attackers + ", defenders=" + defenders);
    calc.setData(getGameData());
    // Calculate battle results
    final ProBattleResult result = calc.calculateBattleResults(unitTerritory, attackers, defenders, new HashSet<>());
    ProLogger.debug(player.getName() + " sub attack TUVSwing=" + result.getTuvSwing());
    return result.getTuvSwing() > 0;
}
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) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) Unit(games.strategy.engine.data.Unit)

Example 4 with IBattle

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

the class ProSimulateTurnUtils method simulateBattles.

public static void simulateBattles(final GameData data, final PlayerID player, final IDelegateBridge delegateBridge, final ProOddsCalculator calc) {
    ProLogger.info("Starting battle simulation phase");
    final BattleDelegate battleDelegate = DelegateFinder.battleDelegate(data);
    final Map<BattleType, Collection<Territory>> battleTerritories = battleDelegate.getBattles().getBattles();
    for (final Entry<BattleType, Collection<Territory>> entry : battleTerritories.entrySet()) {
        for (final Territory t : entry.getValue()) {
            final IBattle battle = battleDelegate.getBattleTracker().getPendingBattle(t, entry.getKey().isBombingRun(), entry.getKey());
            final List<Unit> attackers = (List<Unit>) battle.getAttackingUnits();
            attackers.retainAll(t.getUnits().getUnits());
            final List<Unit> defenders = (List<Unit>) battle.getDefendingUnits();
            defenders.retainAll(t.getUnits().getUnits());
            final Set<Unit> bombardingUnits = new HashSet<>(battle.getBombardingUnits());
            ProLogger.debug("---" + t);
            ProLogger.debug("attackers=" + attackers);
            ProLogger.debug("defenders=" + defenders);
            ProLogger.debug("bombardingUnits=" + bombardingUnits);
            final ProBattleResult result = calc.callBattleCalculator(t, attackers, defenders, bombardingUnits);
            final List<Unit> remainingUnits = result.getAverageAttackersRemaining();
            ProLogger.debug("remainingUnits=" + remainingUnits);
            // Make updates to data
            final List<Unit> attackersToRemove = new ArrayList<>(attackers);
            attackersToRemove.removeAll(remainingUnits);
            final List<Unit> defendersToRemove = CollectionUtils.getMatches(defenders, Matches.unitIsInfrastructure().negate());
            final List<Unit> infrastructureToChangeOwner = CollectionUtils.getMatches(defenders, Matches.unitIsInfrastructure());
            ProLogger.debug("attackersToRemove=" + attackersToRemove);
            ProLogger.debug("defendersToRemove=" + defendersToRemove);
            ProLogger.debug("infrastructureToChangeOwner=" + infrastructureToChangeOwner);
            final Change attackerskilledChange = ChangeFactory.removeUnits(t, attackersToRemove);
            delegateBridge.addChange(attackerskilledChange);
            final Change defenderskilledChange = ChangeFactory.removeUnits(t, defendersToRemove);
            delegateBridge.addChange(defenderskilledChange);
            BattleTracker.captureOrDestroyUnits(t, player, player, delegateBridge, null);
            if (!checkIfCapturedTerritoryIsAlliedCapital(t, data, player, delegateBridge)) {
                delegateBridge.addChange(ChangeFactory.changeOwner(t, player));
            }
            battleDelegate.getBattleTracker().getConquered().add(t);
            battleDelegate.getBattleTracker().removeBattle(battle);
            final Territory updatedTerritory = data.getMap().getTerritory(t.getName());
            ProLogger.debug("after changes owner=" + updatedTerritory.getOwner() + ", units=" + updatedTerritory.getUnits().getUnits());
        }
    }
}
Also used : BattleDelegate(games.strategy.triplea.delegate.BattleDelegate) Territory(games.strategy.engine.data.Territory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) ArrayList(java.util.ArrayList) BattleType(games.strategy.triplea.delegate.IBattle.BattleType) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) Change(games.strategy.engine.data.Change) 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)

Example 5 with IBattle

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

the class TripleAPlayer method battle.

private void battle() {
    if (getPlayerBridge().isGameOver()) {
        return;
    }
    final IBattleDelegate battleDel;
    try {
        battleDel = (IBattleDelegate) getPlayerBridge().getRemoteDelegate();
    } catch (final ClassCastException e) {
        final String errorContext = "PlayerBridge step name: " + getPlayerBridge().getStepName() + ", Remote class name: " + getPlayerBridge().getRemoteDelegate().getClass();
        // TODO: this code is triplicated in the code..
        ClientLogger.logQuietly(errorContext, e);
        throw new IllegalStateException(errorContext, e);
    }
    final PlayerID id = getPlayerId();
    while (true) {
        if (getPlayerBridge().isGameOver()) {
            return;
        }
        final BattleListing battles = battleDel.getBattles();
        if (battles.isEmpty()) {
            final IBattle battle = battleDel.getCurrentBattle();
            if (battle != null) {
                // this should never happen, but it happened once....
                System.err.println("Current battle exists but is not on pending list:  " + battle.toString());
                battleDel.fightCurrentBattle();
            }
            return;
        }
        if (!soundPlayedAlreadyBattle) {
            ClipPlayer.play(SoundPath.CLIP_PHASE_BATTLE, id);
            soundPlayedAlreadyBattle = true;
        }
        final FightBattleDetails details = ui.getBattle(id, battles.getBattles());
        if (getPlayerBridge().isGameOver()) {
            return;
        }
        if (details != null) {
            final String error = battleDel.fightBattle(details.getWhere(), details.isBombingRaid(), details.getBattleType());
            if (error != null) {
                ui.notifyError(error);
            }
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) FightBattleDetails(games.strategy.triplea.delegate.dataObjects.FightBattleDetails) IBattle(games.strategy.triplea.delegate.IBattle) IBattleDelegate(games.strategy.triplea.delegate.remote.IBattleDelegate) BattleListing(games.strategy.triplea.delegate.dataObjects.BattleListing)

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