Search in sources :

Example 31 with Change

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

the class TechnologyDelegate method chargeForTechRolls.

private void chargeForTechRolls(final int rolls, final IntegerMap<PlayerID> whoPaysHowMuch) {
    final Resource pus = getData().getResourceList().getResource(Constants.PUS);
    int cost = rolls * getTechCost();
    if (whoPaysHowMuch == null || whoPaysHowMuch.isEmpty()) {
        final String transcriptText = bridge.getPlayerId().getName() + " spend " + cost + " on tech rolls";
        bridge.getHistoryWriter().startEvent(transcriptText);
        final Change charge = ChangeFactory.changeResourcesChange(bridge.getPlayerId(), pus, -cost);
        bridge.addChange(charge);
    } else {
        for (final Entry<PlayerID, Integer> entry : whoPaysHowMuch.entrySet()) {
            final PlayerID p = entry.getKey();
            final int pays = Math.min(cost, entry.getValue());
            if (pays <= 0) {
                continue;
            }
            cost -= pays;
            final String transcriptText = p.getName() + " spend " + pays + " on tech rolls";
            bridge.getHistoryWriter().startEvent(transcriptText);
            final Change charge = ChangeFactory.changeResourcesChange(p, pus, -pays);
            bridge.addChange(charge);
        }
    }
    if (isWW2V3TechModel()) {
        final Resource tokens = getData().getResourceList().getResource(Constants.TECH_TOKENS);
        final Change newTokens = ChangeFactory.changeResourcesChange(bridge.getPlayerId(), tokens, rolls);
        bridge.addChange(newTokens);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Resource(games.strategy.engine.data.Resource) Change(games.strategy.engine.data.Change)

Example 32 with Change

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

the class UserActionDelegate method chargeForAction.

/**
 * Subtract money from the players wallet
 *
 * @param uaa
 *        the UserActionAttachment this the money is charged for.
 */
private void chargeForAction(final UserActionAttachment uaa) {
    final Resource pus = getData().getResourceList().getResource(Constants.PUS);
    final int cost = uaa.getCostPu();
    if (cost > 0) {
        // don't notify user of spending money anymore
        // notifyMoney(uaa, true);
        final String transcriptText = bridge.getPlayerId().getName() + " spend " + cost + " PU on User Action: " + MyFormatter.attachmentNameToText(uaa.getName());
        bridge.getHistoryWriter().startEvent(transcriptText);
        final Change charge = ChangeFactory.changeResourcesChange(bridge.getPlayerId(), pus, -cost);
        bridge.addChange(charge);
    } else {
        final String transcriptText = bridge.getPlayerId().getName() + " takes action: " + MyFormatter.attachmentNameToText(uaa.getName());
        // we must start an event anyway
        bridge.getHistoryWriter().startEvent(transcriptText);
    }
}
Also used : Resource(games.strategy.engine.data.Resource) Change(games.strategy.engine.data.Change)

Example 33 with Change

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

the class MoveDelegate method resetUnitStateAndDelegateState.

private void resetUnitStateAndDelegateState() {
    // while not a 'unit state', this is fine here for now. since we only have one instance of this delegate, as long as
    // it gets cleared once per player's turn block, we are fine.
    pusLost.clear();
    final Change change = getResetUnitStateChange(getData());
    if (!change.isEmpty()) {
        // if no non-combat occurred, we may have cleanup left from combat
        // that we need to spawn an event for
        bridge.getHistoryWriter().startEvent(CLEANING_UP_DURING_MOVEMENT_PHASE);
        bridge.addChange(change);
    }
}
Also used : CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change)

Example 34 with Change

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

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

the class MovePerformer method markTransportsMovement.

/**
 * Marks transports and units involved in unloading with no movement left.
 */
private void markTransportsMovement(final Collection<Unit> arrived, final Map<Unit, Unit> transporting, final Route route) {
    if (transporting == null) {
        return;
    }
    final GameData data = bridge.getData();
    final Predicate<Unit> paratroopNAirTransports = Matches.unitIsAirTransport().or(Matches.unitIsAirTransportable());
    final boolean paratroopsLanding = arrived.stream().anyMatch(paratroopNAirTransports) && MoveValidator.allLandUnitsAreBeingParatroopered(arrived);
    final Map<Unit, Collection<Unit>> dependentAirTransportableUnits = MoveValidator.getDependents(CollectionUtils.getMatches(arrived, Matches.unitCanTransport()));
    // add newly created dependents
    if (m_newDependents != null) {
        for (final Entry<Unit, Collection<Unit>> entry : m_newDependents.entrySet()) {
            Collection<Unit> dependents = dependentAirTransportableUnits.get(entry.getKey());
            if (dependents != null) {
                dependents.addAll(entry.getValue());
            } else {
                dependents = entry.getValue();
            }
            dependentAirTransportableUnits.put(entry.getKey(), dependents);
        }
    }
    // So they can all continue to move normally
    if (!paratroopsLanding && !dependentAirTransportableUnits.isEmpty()) {
        final Collection<Unit> airTransports = CollectionUtils.getMatches(arrived, Matches.unitIsAirTransport());
        airTransports.addAll(dependentAirTransportableUnits.keySet());
        MovePanel.clearDependents(airTransports);
    }
    // load the transports
    if (route.isLoad() || paratroopsLanding) {
        // mark transports as having transported
        for (final Unit load : transporting.keySet()) {
            final Unit transport = transporting.get(load);
            if (!TransportTracker.transporting(transport).contains(load)) {
                final Change change = TransportTracker.loadTransportChange((TripleAUnit) transport, load);
                m_currentMove.addChange(change);
                m_currentMove.load(transport);
                bridge.addChange(change);
            }
        }
        if (transporting.isEmpty()) {
            for (final Unit airTransport : dependentAirTransportableUnits.keySet()) {
                for (final Unit unit : dependentAirTransportableUnits.get(airTransport)) {
                    final Change change = TransportTracker.loadTransportChange((TripleAUnit) airTransport, unit);
                    m_currentMove.addChange(change);
                    m_currentMove.load(airTransport);
                    bridge.addChange(change);
                }
            }
        }
    }
    if (route.isUnload() || paratroopsLanding) {
        final Set<Unit> units = new HashSet<>();
        units.addAll(transporting.values());
        units.addAll(transporting.keySet());
        // if there are multiple units on a single transport, the transport will be in units list multiple times
        if (transporting.isEmpty()) {
            units.addAll(dependentAirTransportableUnits.keySet());
            for (final Collection<Unit> airTransport : dependentAirTransportableUnits.values()) {
                units.addAll(airTransport);
            }
        }
        // any pending battles in the unloading zone?
        final BattleTracker tracker = getBattleTracker();
        final boolean pendingBattles = tracker.getPendingBattle(route.getStart(), false, BattleType.NORMAL) != null;
        for (final Unit unit : units) {
            if (Matches.unitIsAir().test(unit)) {
                continue;
            }
            final Unit transportedBy = ((TripleAUnit) unit).getTransportedBy();
            // we will unload our paratroopers after they land in battle (after aa guns fire)
            if (paratroopsLanding && transportedBy != null && Matches.unitIsAirTransport().test(transportedBy) && GameStepPropertiesHelper.isCombatMove(data) && Matches.territoryHasNonSubmergedEnemyUnits(player, data).test(route.getEnd())) {
                continue;
            }
            // unload the transports
            final Change change1 = TransportTracker.unloadTransportChange((TripleAUnit) unit, m_currentMove.getRoute().getEnd(), pendingBattles);
            m_currentMove.addChange(change1);
            m_currentMove.unload(unit);
            bridge.addChange(change1);
            // set noMovement
            final Change change2 = ChangeFactory.markNoMovementChange(Collections.singleton(unit));
            m_currentMove.addChange(change2);
            bridge.addChange(change2);
        }
    }
}
Also used : GameData(games.strategy.engine.data.GameData) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) Collection(java.util.Collection) HashSet(java.util.HashSet)

Aggregations

Change (games.strategy.engine.data.Change)70 CompositeChange (games.strategy.engine.data.CompositeChange)44 Unit (games.strategy.engine.data.Unit)40 TripleAUnit (games.strategy.triplea.TripleAUnit)29 PlayerID (games.strategy.engine.data.PlayerID)25 Territory (games.strategy.engine.data.Territory)23 ArrayList (java.util.ArrayList)20 GameData (games.strategy.engine.data.GameData)19 UnitType (games.strategy.engine.data.UnitType)16 Resource (games.strategy.engine.data.Resource)12 Test (org.junit.jupiter.api.Test)11 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)9 Route (games.strategy.engine.data.Route)9 HashSet (java.util.HashSet)8 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)6 Collection (java.util.Collection)6 IntegerMap (games.strategy.util.IntegerMap)5 Tuple (games.strategy.util.Tuple)4 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)3 ITripleAPlayer (games.strategy.triplea.player.ITripleAPlayer)3