Search in sources :

Example 46 with CompositeChange

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

the class MoveDelegate method repairedChangeInto.

private static void repairedChangeInto(final Set<Unit> units, final Territory territory, final IDelegateBridge bridge) {
    final List<Unit> changesIntoUnits = CollectionUtils.getMatches(units, Matches.unitWhenHitPointsRepairedChangesInto());
    final CompositeChange changes = new CompositeChange();
    final List<Unit> unitsToRemove = new ArrayList<>();
    final List<Unit> unitsToAdd = new ArrayList<>();
    for (final Unit unit : changesIntoUnits) {
        final Map<Integer, Tuple<Boolean, UnitType>> map = UnitAttachment.get(unit.getType()).getWhenHitPointsRepairedChangesInto();
        if (map.containsKey(unit.getHits())) {
            final boolean translateAttributes = map.get(unit.getHits()).getFirst();
            final UnitType unitType = map.get(unit.getHits()).getSecond();
            final List<Unit> toAdd = unitType.create(1, unit.getOwner());
            if (translateAttributes) {
                final Change translate = TripleAUnit.translateAttributesToOtherUnits(unit, toAdd, territory);
                changes.add(translate);
            }
            unitsToRemove.add(unit);
            unitsToAdd.addAll(toAdd);
        }
    }
    if (!unitsToRemove.isEmpty()) {
        bridge.addChange(changes);
        final String removeText = MyFormatter.unitsToText(unitsToRemove) + " removed in " + territory.getName();
        bridge.getHistoryWriter().addChildToEvent(removeText, new ArrayList<>(unitsToRemove));
        bridge.addChange(ChangeFactory.removeUnits(territory, unitsToRemove));
        final String addText = MyFormatter.unitsToText(unitsToAdd) + " added in " + territory.getName();
        bridge.getHistoryWriter().addChildToEvent(addText, new ArrayList<>(unitsToAdd));
        bridge.addChange(ChangeFactory.addUnits(territory, unitsToAdd));
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange) Tuple(games.strategy.util.Tuple)

Example 47 with CompositeChange

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

the class MovePerformer method markMovementChange.

private Change markMovementChange(final Collection<Unit> units, final Route route, final PlayerID id) {
    final GameData data = bridge.getData();
    final CompositeChange change = new CompositeChange();
    final Territory routeStart = route.getStart();
    final TerritoryAttachment taRouteStart = TerritoryAttachment.get(routeStart);
    final Territory routeEnd = route.getEnd();
    TerritoryAttachment taRouteEnd = null;
    if (routeEnd != null) {
        taRouteEnd = TerritoryAttachment.get(routeEnd);
    }
    // only units owned by us need to be marked
    final RelationshipTracker relationshipTracker = data.getRelationshipTracker();
    for (final Unit baseUnit : CollectionUtils.getMatches(units, Matches.unitIsOwnedBy(id))) {
        final TripleAUnit unit = (TripleAUnit) baseUnit;
        int moved = route.getMovementCost(unit);
        final UnitAttachment ua = UnitAttachment.get(unit.getType());
        if (ua.getIsAir()) {
            if (taRouteStart != null && taRouteStart.getAirBase() && relationshipTracker.isAllied(route.getStart().getOwner(), unit.getOwner())) {
                moved--;
            }
            if (taRouteEnd != null && taRouteEnd.getAirBase() && relationshipTracker.isAllied(route.getEnd().getOwner(), unit.getOwner())) {
                moved--;
            }
        }
        change.add(ChangeFactory.unitPropertyChange(unit, moved + unit.getAlreadyMoved(), TripleAUnit.ALREADY_MOVED));
    }
    // if entered a non blitzed conquered territory, mark with 0 movement
    if (GameStepPropertiesHelper.isCombatMove(data) && (MoveDelegate.getEmptyNeutral(route).size() != 0 || hasConqueredNonBlitzed(route))) {
        for (final Unit unit : CollectionUtils.getMatches(units, Matches.unitIsLand())) {
            change.add(ChangeFactory.markNoMovementChange(Collections.singleton(unit)));
        }
    }
    if (routeEnd != null && Properties.getSubsCanEndNonCombatMoveWithEnemies(data) && GameStepPropertiesHelper.isNonCombatMove(data, false) && routeEnd.getUnits().anyMatch(Matches.unitIsEnemyOf(data, id).and(Matches.unitIsDestroyer()))) {
        // if there is an enemy destroyer there
        for (final Unit unit : CollectionUtils.getMatches(units, Matches.unitIsSub().and(Matches.unitIsAir().negate()))) {
            change.add(ChangeFactory.markNoMovementChange(Collections.singleton(unit)));
        }
    }
    return change;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) RelationshipTracker(games.strategy.engine.data.RelationshipTracker) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit)

Example 48 with CompositeChange

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

the class MustFightBattle method endBattle.

private void endBattle(final IDelegateBridge bridge) {
    clearWaitingToDieAndDamagedChangesInto(bridge);
    m_isOver = true;
    m_battleTracker.removeBattle(this);
    // Must clear transportedby for allied air on carriers for both attacking units and retreating units
    final CompositeChange clearAlliedAir = clearTransportedByForAlliedAirOnCarrier(m_attackingUnits, m_battleSite, m_attacker, m_data);
    if (!clearAlliedAir.isEmpty()) {
        bridge.addChange(clearAlliedAir);
    }
    final CompositeChange clearAlliedAirRetreated = clearTransportedByForAlliedAirOnCarrier(m_attackingUnitsRetreated, m_battleSite, m_attacker, m_data);
    if (!clearAlliedAirRetreated.isEmpty()) {
        bridge.addChange(clearAlliedAirRetreated);
    }
}
Also used : CompositeChange(games.strategy.engine.data.CompositeChange)

Example 49 with CompositeChange

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

the class MustFightBattle method attackerWins.

private void attackerWins(final IDelegateBridge bridge) {
    m_whoWon = WhoWon.ATTACKER;
    getDisplay(bridge).battleEnd(m_battleID, m_attacker.getName() + " win");
    if (m_headless) {
        return;
    }
    // do we need to change ownership
    if (m_attackingUnits.stream().anyMatch(Matches.unitIsNotAir())) {
        if (Matches.isTerritoryEnemyAndNotUnownedWater(m_attacker, m_data).test(m_battleSite)) {
            m_battleTracker.addToConquered(m_battleSite);
        }
        m_battleTracker.takeOver(m_battleSite, m_attacker, bridge, null, m_attackingUnits);
        m_battleResultDescription = BattleRecord.BattleResultDescription.CONQUERED;
    } else {
        m_battleResultDescription = BattleRecord.BattleResultDescription.WON_WITHOUT_CONQUERING;
    }
    // Clear the transported_by for successfully off loaded units
    final Collection<Unit> transports = CollectionUtils.getMatches(m_attackingUnits, Matches.unitIsTransport());
    if (!transports.isEmpty()) {
        final CompositeChange change = new CompositeChange();
        final Collection<Unit> dependents = getTransportDependents(transports);
        if (!dependents.isEmpty()) {
            for (final Unit unit : dependents) {
                // clear the loaded by ONLY for Combat unloads. NonCombat unloads are handled elsewhere.
                if (Matches.unitWasUnloadedThisTurn().test(unit)) {
                    change.add(ChangeFactory.unitPropertyChange(unit, null, TripleAUnit.TRANSPORTED_BY));
                }
            }
            bridge.addChange(change);
        }
    }
    bridge.getHistoryWriter().addChildToEvent(m_attacker.getName() + " win", new ArrayList<>(m_attackingUnits));
    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));
    }
    if (!m_headless) {
        if (Matches.territoryIsWater().test(m_battleSite)) {
            if (!m_attackingUnits.isEmpty() && m_attackingUnits.stream().allMatch(Matches.unitIsAir())) {
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_AIR_SUCCESSFUL, m_attacker);
            } else {
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_SEA_SUCCESSFUL, m_attacker);
            }
        } else {
            // have capture sounds for that
            if (!m_attackingUnits.isEmpty() && m_attackingUnits.stream().allMatch(Matches.unitIsAir())) {
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_AIR_SUCCESSFUL, m_attacker);
            }
        }
    }
}
Also used : BattleResults(games.strategy.triplea.oddsCalculator.ta.BattleResults) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 50 with CompositeChange

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

the class MustFightBattle method damagedChangeInto.

private void damagedChangeInto(final List<Unit> units, final IDelegateBridge bridge) {
    final List<Unit> damagedUnits = CollectionUtils.getMatches(units, Matches.unitWhenHitPointsDamagedChangesInto().and(Matches.unitHasTakenSomeDamage()));
    final CompositeChange changes = new CompositeChange();
    final List<Unit> unitsToRemove = new ArrayList<>();
    final List<Unit> unitsToAdd = new ArrayList<>();
    for (final Unit unit : damagedUnits) {
        final Map<Integer, Tuple<Boolean, UnitType>> map = UnitAttachment.get(unit.getType()).getWhenHitPointsDamagedChangesInto();
        if (map.containsKey(unit.getHits())) {
            final boolean translateAttributes = map.get(unit.getHits()).getFirst();
            final UnitType unitType = map.get(unit.getHits()).getSecond();
            final List<Unit> toAdd = unitType.create(1, unit.getOwner());
            if (translateAttributes) {
                final Change translate = TripleAUnit.translateAttributesToOtherUnits(unit, toAdd, m_battleSite);
                changes.add(translate);
            }
            unitsToRemove.add(unit);
            unitsToAdd.addAll(toAdd);
        }
    }
    if (!unitsToRemove.isEmpty()) {
        bridge.addChange(changes);
        remove(unitsToRemove, bridge, m_battleSite, null);
        final String transcriptText = MyFormatter.unitsToText(unitsToAdd) + " added in " + m_battleSite.getName();
        bridge.getHistoryWriter().addChildToEvent(transcriptText, new ArrayList<>(unitsToAdd));
        bridge.addChange(ChangeFactory.addUnits(m_battleSite, unitsToAdd));
        bridge.addChange(ChangeFactory.markNoMovementChange(unitsToAdd));
        units.addAll(unitsToAdd);
        getDisplay(bridge).changedUnitsNotification(m_battleID, unitsToRemove.get(0).getOwner(), unitsToRemove, unitsToAdd, null);
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange) Tuple(games.strategy.util.Tuple)

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