Search in sources :

Example 61 with Unit

use of games.strategy.engine.data.Unit in project triplea by triplea-game.

the class MustFightBattle method reLoadTransports.

void reLoadTransports(final Collection<Unit> units, final CompositeChange change) {
    final Collection<Unit> transports = CollectionUtils.getMatches(units, Matches.unitCanTransport());
    // Put units back on their transports
    for (final Unit transport : transports) {
        final Collection<Unit> unloaded = TransportTracker.unloaded(transport);
        for (final Unit load : unloaded) {
            final Change loadChange = TransportTracker.loadTransportChange((TripleAUnit) transport, load);
            change.add(loadChange);
        }
    }
}
Also used : CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 62 with Unit

use of games.strategy.engine.data.Unit in project triplea by triplea-game.

the class MustFightBattle method removeAttack.

@Override
public void removeAttack(final Route route, final Collection<Unit> units) {
    m_attackingUnits.removeAll(units);
    // the route could be null, in the case of a unit in a territory where a sub is submerged.
    if (route == null) {
        return;
    }
    final Territory attackingFrom = route.getTerritoryBeforeEnd();
    Collection<Unit> attackingFromMapUnits = m_attackingFromMap.get(attackingFrom);
    // handle possible null pointer
    if (attackingFromMapUnits == null) {
        attackingFromMapUnits = new ArrayList<>();
    }
    attackingFromMapUnits.removeAll(units);
    if (attackingFromMapUnits.isEmpty()) {
        m_attackingFrom.remove(attackingFrom);
    }
    // deal with amphibious assaults
    if (attackingFrom.isWater()) {
        if (route.getEnd() != null && !route.getEnd().isWater() && units.stream().anyMatch(Matches.unitIsLand())) {
            m_amphibiousLandAttackers.removeAll(CollectionUtils.getMatches(units, Matches.unitIsLand()));
        }
        // that territory is no longer an amphibious assault
        if (attackingFromMapUnits.stream().noneMatch(Matches.unitIsLand())) {
            getAmphibiousAttackTerritories().remove(attackingFrom);
            // do we have any amphibious attacks left?
            m_isAmphibious = !getAmphibiousAttackTerritories().isEmpty();
        }
    }
    for (final Collection<Unit> dependents : m_dependentUnits.values()) {
        dependents.removeAll(units);
    }
}
Also used : Territory(games.strategy.engine.data.Territory) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 63 with Unit

use of games.strategy.engine.data.Unit in project triplea by triplea-game.

the class MustFightBattle method submergeUnits.

private void submergeUnits(final Collection<Unit> submerging, final boolean defender, final IDelegateBridge bridge) {
    final String transcriptText = MyFormatter.unitsToText(submerging) + " Submerged";
    final Collection<Unit> units = defender ? m_defendingUnits : m_attackingUnits;
    final Collection<Unit> unitsRetreated = defender ? m_defendingUnitsRetreated : m_attackingUnitsRetreated;
    final CompositeChange change = new CompositeChange();
    for (final Unit u : submerging) {
        change.add(ChangeFactory.unitPropertyChange(u, true, TripleAUnit.SUBMERGED));
    }
    bridge.addChange(change);
    units.removeAll(submerging);
    unitsRetreated.addAll(submerging);
    if (!units.isEmpty() && !m_isOver) {
        getDisplay(bridge).notifyRetreat(m_battleID, submerging);
    }
    bridge.getHistoryWriter().addChildToEvent(transcriptText, new ArrayList<>(submerging));
}
Also used : TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 64 with Unit

use of games.strategy.engine.data.Unit in project triplea by triplea-game.

the class MustFightBattle method landParatroops.

private void landParatroops(final IDelegateBridge bridge) {
    if (TechAttachment.isAirTransportable(m_attacker)) {
        final Collection<Unit> airTransports = CollectionUtils.getMatches(m_battleSite.getUnits().getUnits(), Matches.unitIsAirTransport());
        if (!airTransports.isEmpty()) {
            final Collection<Unit> dependents = getDependentUnits(airTransports);
            if (!dependents.isEmpty()) {
                final CompositeChange change = new CompositeChange();
                // remove dependency from paratroopers by unloading the air transports
                for (final Unit unit : dependents) {
                    change.add(TransportTracker.unloadAirTransportChange((TripleAUnit) unit, m_battleSite, false));
                }
                bridge.addChange(change);
                // remove bombers from m_dependentUnits
                for (final Unit unit : airTransports) {
                    m_dependentUnits.remove(unit);
                }
            }
        }
    }
}
Also used : TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit)

Example 65 with Unit

use of games.strategy.engine.data.Unit 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)

Aggregations

Unit (games.strategy.engine.data.Unit)447 TripleAUnit (games.strategy.triplea.TripleAUnit)301 Territory (games.strategy.engine.data.Territory)255 ArrayList (java.util.ArrayList)204 PlayerID (games.strategy.engine.data.PlayerID)135 GameData (games.strategy.engine.data.GameData)103 HashSet (java.util.HashSet)92 Test (org.junit.jupiter.api.Test)91 Route (games.strategy.engine.data.Route)89 UnitType (games.strategy.engine.data.UnitType)85 CompositeChange (games.strategy.engine.data.CompositeChange)64 HashMap (java.util.HashMap)64 IntegerMap (games.strategy.util.IntegerMap)61 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)58 Collection (java.util.Collection)58 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)56 List (java.util.List)48 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)47 Change (games.strategy.engine.data.Change)44 Set (java.util.Set)43