Search in sources :

Example 1 with BattleResults

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

the class BattleDelegate method setupUnitsInSameTerritoryBattles.

/**
 * Setup the battles where the battle occurs because units are in the
 * same territory. This happens when subs emerge (after being submerged), and
 * when naval units are placed in enemy occupied sea zones, and also
 * when political relationships change and potentially leave units in now-hostile territories.
 */
private static void setupUnitsInSameTerritoryBattles(final BattleTracker battleTracker, final IDelegateBridge bridge) {
    final PlayerID player = bridge.getPlayerId();
    final GameData data = bridge.getData();
    final boolean ignoreTransports = isIgnoreTransportInMovement(data);
    final boolean ignoreSubs = isIgnoreSubInMovement(data);
    final Predicate<Unit> seaTransports = Matches.unitIsTransportButNotCombatTransport().and(Matches.unitIsSea());
    final Predicate<Unit> seaTranportsOrSubs = seaTransports.or(Matches.unitIsSub());
    // we want to match all sea zones with our units and enemy units
    final Predicate<Territory> anyTerritoryWithOwnAndEnemy = Matches.territoryHasUnitsOwnedBy(player).and(Matches.territoryHasEnemyUnits(player, data));
    final Predicate<Territory> enemyTerritoryAndOwnUnits = Matches.isTerritoryEnemyAndNotUnownedWater(player, data).and(Matches.territoryHasUnitsOwnedBy(player));
    final Predicate<Territory> enemyUnitsOrEnemyTerritory = anyTerritoryWithOwnAndEnemy.or(enemyTerritoryAndOwnUnits);
    final List<Territory> battleTerritories = CollectionUtils.getMatches(data.getMap().getTerritories(), enemyUnitsOrEnemyTerritory);
    for (final Territory territory : battleTerritories) {
        final List<Unit> attackingUnits = territory.getUnits().getMatches(Matches.unitIsOwnedBy(player));
        // now make sure to add any units that must move with these attacking units, so that they get included as
        // dependencies
        final Map<Unit, Collection<Unit>> transportMap = TransportTracker.transporting(territory.getUnits());
        final HashSet<Unit> dependants = new HashSet<>();
        for (final Entry<Unit, Collection<Unit>> entry : transportMap.entrySet()) {
            // only consider those transports that we are attacking with. allied and enemy transports are not added.
            if (attackingUnits.contains(entry.getKey())) {
                dependants.addAll(entry.getValue());
            }
        }
        // no duplicates
        dependants.removeAll(attackingUnits);
        // add the dependants to the attacking list
        attackingUnits.addAll(dependants);
        final List<Unit> enemyUnits = territory.getUnits().getMatches(Matches.enemyUnit(player, data));
        final IBattle bombingBattle = battleTracker.getPendingBattle(territory, true, null);
        if (bombingBattle != null) {
            // we need to remove any units which are participating in bombing raids
            attackingUnits.removeAll(bombingBattle.getAttackingUnits());
        }
        if (attackingUnits.stream().allMatch(Matches.unitIsInfrastructure())) {
            continue;
        }
        IBattle battle = battleTracker.getPendingBattle(territory, false, BattleType.NORMAL);
        if (battle == null) {
            // fires)
            if (enemyUnits.stream().allMatch(Matches.unitIsInfrastructure())) {
                landParatroopers(player, territory, bridge);
            }
            bridge.getHistoryWriter().startEvent(player.getName() + " creates battle in territory " + territory.getName());
            battleTracker.addBattle(new RouteScripted(territory), attackingUnits, player, bridge, null, null);
            battle = battleTracker.getPendingBattle(territory, false, BattleType.NORMAL);
        }
        if (battle == null) {
            continue;
        }
        if (bombingBattle != null) {
            battleTracker.addDependency(battle, bombingBattle);
        }
        if (battle.isEmpty()) {
            battle.addAttackChange(new RouteScripted(territory), attackingUnits, null);
        }
        if (!battle.getAttackingUnits().containsAll(attackingUnits)) {
            List<Unit> attackingUnitsNeedToBeAdded = new ArrayList<>(attackingUnits);
            attackingUnitsNeedToBeAdded.removeAll(battle.getAttackingUnits());
            attackingUnitsNeedToBeAdded.removeAll(battle.getDependentUnits(battle.getAttackingUnits()));
            if (territory.isWater()) {
                attackingUnitsNeedToBeAdded = CollectionUtils.getMatches(attackingUnitsNeedToBeAdded, Matches.unitIsLand().negate());
            } else {
                attackingUnitsNeedToBeAdded = CollectionUtils.getMatches(attackingUnitsNeedToBeAdded, Matches.unitIsSea().negate());
            }
            if (!attackingUnitsNeedToBeAdded.isEmpty()) {
                battle.addAttackChange(new RouteScripted(territory), attackingUnitsNeedToBeAdded, null);
            }
        }
        // Reach stalemate if all attacking and defending units are transports
        if ((ignoreTransports && !attackingUnits.isEmpty() && attackingUnits.stream().allMatch(seaTransports) && !enemyUnits.isEmpty() && enemyUnits.stream().allMatch(seaTransports)) || (!attackingUnits.isEmpty() && attackingUnits.stream().allMatch(Matches.unitHasAttackValueOfAtLeast(1).negate()) && !enemyUnits.isEmpty() && enemyUnits.stream().allMatch(Matches.unitHasDefendValueOfAtLeast(1).negate()))) {
            final BattleResults results = new BattleResults(battle, WhoWon.DRAW, data);
            battleTracker.getBattleRecords().addResultToBattle(player, battle.getBattleId(), null, 0, 0, BattleRecord.BattleResultDescription.STALEMATE, results);
            battle.cancelBattle(bridge);
            battleTracker.removeBattle(battle);
            continue;
        }
        // possibility to ignore battle altogether
        if (!attackingUnits.isEmpty()) {
            final ITripleAPlayer remotePlayer = getRemotePlayer(bridge);
            if (territory.isWater() && Properties.getSeaBattlesMayBeIgnored(data)) {
                if (!remotePlayer.selectAttackUnits(territory)) {
                    final BattleResults results = new BattleResults(battle, WhoWon.NOTFINISHED, data);
                    battleTracker.getBattleRecords().addResultToBattle(player, battle.getBattleId(), null, 0, 0, BattleRecord.BattleResultDescription.NO_BATTLE, results);
                    battle.cancelBattle(bridge);
                    battleTracker.removeBattle(battle);
                }
                continue;
            }
            // Check for ignored units
            if (ignoreTransports || ignoreSubs) {
                // if only enemy transports... attack them?
                if (ignoreTransports && !enemyUnits.isEmpty() && enemyUnits.stream().allMatch(seaTransports)) {
                    if (!remotePlayer.selectAttackTransports(territory)) {
                        final BattleResults results = new BattleResults(battle, WhoWon.NOTFINISHED, data);
                        battleTracker.getBattleRecords().addResultToBattle(player, battle.getBattleId(), null, 0, 0, BattleRecord.BattleResultDescription.NO_BATTLE, results);
                        battle.cancelBattle(bridge);
                        battleTracker.removeBattle(battle);
                    }
                    continue;
                }
                // if only enemy subs... attack them?
                if (ignoreSubs && !enemyUnits.isEmpty() && enemyUnits.stream().allMatch(Matches.unitIsSub())) {
                    if (!remotePlayer.selectAttackSubs(territory)) {
                        final BattleResults results = new BattleResults(battle, WhoWon.NOTFINISHED, data);
                        battleTracker.getBattleRecords().addResultToBattle(player, battle.getBattleId(), null, 0, 0, BattleRecord.BattleResultDescription.NO_BATTLE, results);
                        battle.cancelBattle(bridge);
                        battleTracker.removeBattle(battle);
                    }
                    continue;
                }
                // if only enemy transports and subs... attack them?
                if (ignoreSubs && ignoreTransports && !enemyUnits.isEmpty() && enemyUnits.stream().allMatch(seaTranportsOrSubs)) {
                    if (!remotePlayer.selectAttackUnits(territory)) {
                        final BattleResults results = new BattleResults(battle, WhoWon.NOTFINISHED, data);
                        battleTracker.getBattleRecords().addResultToBattle(player, battle.getBattleId(), null, 0, 0, BattleRecord.BattleResultDescription.NO_BATTLE, results);
                        battle.cancelBattle(bridge);
                        battleTracker.removeBattle(battle);
                    }
                }
            }
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) BattleResults(games.strategy.triplea.oddsCalculator.ta.BattleResults) ArrayList(java.util.ArrayList) RouteScripted(games.strategy.engine.data.RouteScripted) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) ITripleAPlayer(games.strategy.triplea.player.ITripleAPlayer) Collection(java.util.Collection) ResourceCollection(games.strategy.engine.data.ResourceCollection) HashSet(java.util.HashSet)

Example 2 with BattleResults

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

the class AirBattle method end.

private void end(final IDelegateBridge bridge) {
    // record it
    final String text;
    if (!m_attackingUnits.isEmpty()) {
        if (m_isBombingRun) {
            if (m_attackingUnits.stream().anyMatch(Matches.unitIsStrategicBomber())) {
                m_whoWon = WhoWon.ATTACKER;
                if (m_defendingUnits.isEmpty()) {
                    m_battleResultDescription = BattleRecord.BattleResultDescription.WON_WITHOUT_CONQUERING;
                } else {
                    m_battleResultDescription = BattleRecord.BattleResultDescription.WON_WITH_ENEMY_LEFT;
                }
                text = "Air Battle is over, the remaining bombers go on to their targets";
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_AIR_SUCCESSFUL, m_attacker);
            } else {
                m_whoWon = WhoWon.DRAW;
                m_battleResultDescription = BattleRecord.BattleResultDescription.STALEMATE;
                text = "Air Battle is over, the bombers have all died";
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_FAILURE, m_attacker);
            }
        } else {
            if (m_defendingUnits.isEmpty()) {
                m_whoWon = WhoWon.ATTACKER;
                m_battleResultDescription = BattleRecord.BattleResultDescription.WON_WITHOUT_CONQUERING;
                text = "Air Battle is over, the defenders have all died";
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_AIR_SUCCESSFUL, m_attacker);
            } else {
                m_whoWon = WhoWon.DRAW;
                m_battleResultDescription = BattleRecord.BattleResultDescription.STALEMATE;
                text = "Air Battle is over, neither side is eliminated";
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_STALEMATE, m_attacker);
            }
        }
    } else {
        m_whoWon = WhoWon.DEFENDER;
        m_battleResultDescription = BattleRecord.BattleResultDescription.LOST;
        text = "Air Battle is over, the attackers have all died";
        bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_FAILURE, m_attacker);
    }
    bridge.getHistoryWriter().addChildToEvent(text);
    m_battleTracker.getBattleRecords().addResultToBattle(m_attacker, m_battleID, m_defender, m_attackerLostTUV, m_defenderLostTUV, m_battleResultDescription, new BattleResults(this, m_data));
    getDisplay(bridge).battleEnd(m_battleID, "Air Battle over");
    m_isOver = true;
    m_battleTracker.removeBattle(AirBattle.this);
}
Also used : BattleResults(games.strategy.triplea.oddsCalculator.ta.BattleResults)

Example 3 with BattleResults

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

the class MustFightBattle method nobodyWins.

private void nobodyWins(final IDelegateBridge bridge) {
    m_whoWon = WhoWon.DRAW;
    getDisplay(bridge).battleEnd(m_battleID, "Stalemate");
    bridge.getHistoryWriter().addChildToEvent(m_defender.getName() + " and " + m_attacker.getName() + " reach a stalemate");
    m_battleResultDescription = BattleRecord.BattleResultDescription.STALEMATE;
    showCasualties(bridge);
    if (!m_headless) {
        m_battleTracker.getBattleRecords().addResultToBattle(m_attacker, m_battleID, m_defender, m_attackerLostTUV, m_defenderLostTUV, m_battleResultDescription, new BattleResults(this, m_data));
        bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_STALEMATE, m_attacker);
    }
    checkDefendingPlanesCanLand();
}
Also used : BattleResults(games.strategy.triplea.oddsCalculator.ta.BattleResults)

Example 4 with BattleResults

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

the class MustFightBattle method defenderWins.

private void defenderWins(final IDelegateBridge bridge) {
    m_whoWon = WhoWon.DEFENDER;
    getDisplay(bridge).battleEnd(m_battleID, m_defender.getName() + " win");
    if (Properties.getAbandonedTerritoriesMayBeTakenOverImmediately(m_data)) {
        if (CollectionUtils.getMatches(m_defendingUnits, Matches.unitIsNotInfrastructure()).size() == 0) {
            final List<Unit> allyOfAttackerUnits = m_battleSite.getUnits().getMatches(Matches.unitIsNotInfrastructure());
            if (!allyOfAttackerUnits.isEmpty()) {
                final PlayerID abandonedToPlayer = AbstractBattle.findPlayerWithMostUnits(allyOfAttackerUnits);
                bridge.getHistoryWriter().addChildToEvent(abandonedToPlayer.getName() + " takes over " + m_battleSite.getName() + " as there are no defenders left", allyOfAttackerUnits);
                // should we create a new battle records to show the ally capturing the territory (in the case where they
                // didn't already own/allied it)?
                m_battleTracker.takeOver(m_battleSite, abandonedToPlayer, bridge, null, allyOfAttackerUnits);
            }
        } else {
            // should we create a new battle records to show the defender capturing the territory (in the case where they
            // didn't already own/allied it)?
            m_battleTracker.takeOver(m_battleSite, m_defender, bridge, null, m_defendingUnits);
        }
    }
    bridge.getHistoryWriter().addChildToEvent(m_defender.getName() + " win", new ArrayList<>(m_defendingUnits));
    m_battleResultDescription = BattleRecord.BattleResultDescription.LOST;
    showCasualties(bridge);
    if (!m_headless) {
        m_battleTracker.getBattleRecords().addResultToBattle(m_attacker, m_battleID, m_defender, m_attackerLostTUV, m_defenderLostTUV, m_battleResultDescription, new BattleResults(this, m_data));
    }
    checkDefendingPlanesCanLand();
    BattleTracker.captureOrDestroyUnits(m_battleSite, m_defender, m_defender, bridge, null);
    if (!m_headless) {
        bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_FAILURE, m_attacker);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) BattleResults(games.strategy.triplea.oddsCalculator.ta.BattleResults) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 5 with BattleResults

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

the class FinishedBattle method unitsLostInPrecedingBattle.

@Override
public void unitsLostInPrecedingBattle(final IBattle battle, final Collection<Unit> units, final IDelegateBridge bridge, final boolean withdrawn) {
    final Collection<Unit> lost = getDependentUnits(units);
    lost.addAll(CollectionUtils.intersection(units, m_attackingUnits));
    if (lost.size() != 0) {
        m_attackingUnits.removeAll(lost);
        /*
       * TODO: these units are no longer in this territory, most probably. Plus they may have already been removed by
       * another "real" battle
       * class.
       * final String transcriptText = MyFormatter.unitsToText(lost) + " lost in " + m_battleSite.getName();
       * bridge.getHistoryWriter().startEvent(transcriptText);
       * final Change change = ChangeFactory.removeUnits(m_battleSite, lost);
       * bridge.addChange(change);
       */
        if (m_attackingUnits.isEmpty()) {
            final IntegerMap<UnitType> costs = TuvUtils.getCostsForTuv(m_attacker, m_data);
            final int tuvLostAttacker = (withdrawn ? 0 : TuvUtils.getTuv(lost, m_attacker, costs, m_data));
            m_attackerLostTUV += tuvLostAttacker;
            // scripted?
            m_whoWon = WhoWon.DEFENDER;
            if (!m_headless) {
                m_battleTracker.getBattleRecords().addResultToBattle(m_attacker, m_battleID, m_defender, m_attackerLostTUV, m_defenderLostTUV, BattleRecord.BattleResultDescription.LOST, new BattleResults(this, m_data));
            }
            m_battleTracker.removeBattle(this);
        }
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) BattleResults(games.strategy.triplea.oddsCalculator.ta.BattleResults) Unit(games.strategy.engine.data.Unit)

Aggregations

BattleResults (games.strategy.triplea.oddsCalculator.ta.BattleResults)12 Unit (games.strategy.engine.data.Unit)6 TripleAUnit (games.strategy.triplea.TripleAUnit)5 PlayerID (games.strategy.engine.data.PlayerID)3 CompositeChange (games.strategy.engine.data.CompositeChange)2 GameData (games.strategy.engine.data.GameData)2 Territory (games.strategy.engine.data.Territory)2 UnitType (games.strategy.engine.data.UnitType)2 ArrayList (java.util.ArrayList)2 Change (games.strategy.engine.data.Change)1 RelationshipTracker (games.strategy.engine.data.RelationshipTracker)1 Resource (games.strategy.engine.data.Resource)1 ResourceCollection (games.strategy.engine.data.ResourceCollection)1 RouteScripted (games.strategy.engine.data.RouteScripted)1 PlayerAttachment (games.strategy.triplea.attachments.PlayerAttachment)1 TerritoryAttachment (games.strategy.triplea.attachments.TerritoryAttachment)1 ITripleAPlayer (games.strategy.triplea.player.ITripleAPlayer)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1