Search in sources :

Example 21 with CompositeChange

use of games.strategy.engine.data.CompositeChange 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 22 with CompositeChange

use of games.strategy.engine.data.CompositeChange 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 23 with CompositeChange

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

the class MustFightBattle method retreatFromNonCombat.

/**
 * Retreat landed units from allied territory when their transport retreats.
 */
private Change retreatFromNonCombat(Collection<Unit> units, final Territory retreatTo) {
    final CompositeChange change = new CompositeChange();
    units = CollectionUtils.getMatches(units, Matches.unitIsTransport());
    final Collection<Unit> retreated = getTransportDependents(units);
    if (!retreated.isEmpty()) {
        for (final Unit unit : units) {
            final Territory retreatedFrom = TransportTracker.getTerritoryTransportHasUnloadedTo(unit);
            if (retreatedFrom != null) {
                reLoadTransports(units, change);
                change.add(ChangeFactory.moveUnits(retreatedFrom, retreatTo, retreated));
            }
        }
    }
    return change;
}
Also used : Territory(games.strategy.engine.data.Territory) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 24 with CompositeChange

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

the class MustFightBattle method retreatFromDependents.

private Change retreatFromDependents(final Collection<Unit> units, final Territory retreatTo, final Collection<IBattle> dependentBattles) {
    final CompositeChange change = new CompositeChange();
    for (final IBattle dependent : dependentBattles) {
        final Route route = new Route();
        route.setStart(m_battleSite);
        route.add(dependent.getTerritory());
        final Collection<Unit> retreatedUnits = dependent.getDependentUnits(units);
        dependent.removeAttack(route, retreatedUnits);
        reLoadTransports(units, change);
        change.add(ChangeFactory.moveUnits(dependent.getTerritory(), retreatTo, retreatedUnits));
    }
    return change;
}
Also used : CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route)

Example 25 with CompositeChange

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

the class MustFightBattle method retreatUnitsAndPlanes.

private void retreatUnitsAndPlanes(final Collection<Unit> retreating, final Territory to, final boolean defender, final IDelegateBridge bridge) {
    // Remove air from battle
    final Collection<Unit> units = defender ? m_defendingUnits : m_attackingUnits;
    final Collection<Unit> unitsRetreated = defender ? m_defendingUnitsRetreated : m_attackingUnitsRetreated;
    units.removeAll(CollectionUtils.getMatches(units, Matches.unitIsAir()));
    // add all land units' dependents
    retreating.addAll(getDependentUnits(units));
    // our own air units don't retreat with land units
    final Predicate<Unit> notMyAir = Matches.unitIsNotAir().or(Matches.unitIsOwnedBy(m_attacker).negate());
    final Collection<Unit> nonAirRetreating = CollectionUtils.getMatches(retreating, notMyAir);
    final String transcriptText = MyFormatter.unitsToTextNoOwner(nonAirRetreating) + " retreated to " + to.getName();
    bridge.getHistoryWriter().addChildToEvent(transcriptText, new ArrayList<>(nonAirRetreating));
    final CompositeChange change = new CompositeChange();
    change.add(ChangeFactory.moveUnits(m_battleSite, to, nonAirRetreating));
    if (m_isOver) {
        final Collection<IBattle> dependentBattles = m_battleTracker.getBlocked(this);
        // If there are no dependent battles, check landings in allied territories
        if (dependentBattles.isEmpty()) {
            change.add(retreatFromNonCombat(nonAirRetreating, to));
        // Else retreat the units from combat when their transport retreats
        } else {
            change.add(retreatFromDependents(nonAirRetreating, to, dependentBattles));
        }
    }
    bridge.addChange(change);
    units.removeAll(nonAirRetreating);
    unitsRetreated.addAll(nonAirRetreating);
    if (units.isEmpty() || m_isOver) {
        endBattle(bridge);
        if (defender) {
            attackerWins(bridge);
        } else {
            defenderWins(bridge);
        }
    } else {
        getDisplay(bridge).notifyRetreat(m_battleID, retreating);
    }
}
Also used : TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange)

Aggregations

CompositeChange (games.strategy.engine.data.CompositeChange)74 Unit (games.strategy.engine.data.Unit)48 TripleAUnit (games.strategy.triplea.TripleAUnit)40 GameData (games.strategy.engine.data.GameData)31 Territory (games.strategy.engine.data.Territory)25 ArrayList (java.util.ArrayList)22 PlayerID (games.strategy.engine.data.PlayerID)21 Change (games.strategy.engine.data.Change)17 UnitType (games.strategy.engine.data.UnitType)14 Resource (games.strategy.engine.data.Resource)9 HashSet (java.util.HashSet)9 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)8 IntegerMap (games.strategy.util.IntegerMap)8 Tuple (games.strategy.util.Tuple)7 Collection (java.util.Collection)7 RelationshipType (games.strategy.engine.data.RelationshipType)6 Set (java.util.Set)6 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)4 ProductionRule (games.strategy.engine.data.ProductionRule)4 HashMap (java.util.HashMap)4