Search in sources :

Example 66 with CompositeChange

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

the class TriggerAttachment method triggerTerritoryEffectPropertyChange.

public static void triggerTerritoryEffectPropertyChange(final Set<TriggerAttachment> satisfiedTriggers, final IDelegateBridge bridge, final String beforeOrAfter, final String stepName, final boolean useUses, final boolean testUses, final boolean testChance, final boolean testWhen) {
    Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, territoryEffectPropertyMatch());
    if (testWhen) {
        trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
    }
    if (testUses) {
        trigs = CollectionUtils.getMatches(trigs, availableUses);
    }
    final CompositeChange change = new CompositeChange();
    for (final TriggerAttachment t : trigs) {
        if (testChance && !t.testChance(bridge)) {
            continue;
        }
        if (useUses) {
            t.use(bridge);
        }
        for (final Tuple<String, String> property : t.getTerritoryEffectProperty()) {
            for (final TerritoryEffect territoryEffect : t.getTerritoryEffects()) {
                String newValue = property.getSecond();
                boolean clearFirst = false;
                // test if we are resetting the variable first, and if so, remove the leading "-reset-" or "-clear-"
                if (newValue.length() > 0 && (newValue.startsWith(PREFIX_CLEAR) || newValue.startsWith(PREFIX_RESET))) {
                    newValue = newValue.replaceFirst(PREFIX_CLEAR, "").replaceFirst(PREFIX_RESET, "");
                    clearFirst = true;
                }
                // covers TerritoryEffectAttachment
                if (t.getTerritoryEffectAttachmentName().getFirst().equals("TerritoryEffectAttachment")) {
                    final TerritoryEffectAttachment attachment = TerritoryEffectAttachment.get(territoryEffect, t.getTerritoryEffectAttachmentName().getSecond());
                    if (newValue.equals(attachment.getRawPropertyString(property.getFirst()))) {
                        continue;
                    }
                    if (clearFirst && newValue.length() < 1) {
                        change.add(ChangeFactory.attachmentPropertyReset(attachment, property.getFirst()));
                    } else {
                        change.add(ChangeFactory.attachmentPropertyChange(attachment, newValue, property.getFirst(), clearFirst));
                    }
                    bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": Setting " + property.getFirst() + (newValue.length() > 0 ? " to " + newValue : " cleared ") + " for " + t.getTerritoryEffectAttachmentName().getSecond() + " attached to " + territoryEffect.getName());
                }
            // TODO add other attachment changes here if they attach to a territory
            }
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
}
Also used : TerritoryEffect(games.strategy.engine.data.TerritoryEffect) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 67 with CompositeChange

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

the class TriggerAttachment method placeUnits.

private static void placeUnits(final TriggerAttachment t, final Territory terr, final IntegerMap<UnitType> utMap, final PlayerID player, final IDelegateBridge bridge) {
    // createUnits
    final List<Unit> units = new ArrayList<>();
    for (final UnitType u : utMap.keySet()) {
        units.addAll(u.create(utMap.getInt(u), player));
    }
    final CompositeChange change = new CompositeChange();
    // mark no movement
    for (final Unit unit : units) {
        change.add(ChangeFactory.markNoMovementChange(unit));
    }
    // place units
    final Collection<Unit> factoryAndInfrastructure = CollectionUtils.getMatches(units, Matches.unitIsInfrastructure());
    change.add(OriginalOwnerTracker.addOriginalOwnerChange(factoryAndInfrastructure, player));
    final String transcriptText = MyFormatter.attachmentNameToText(t.getName()) + ": " + player.getName() + " has " + MyFormatter.unitsToTextNoOwner(units) + " placed in " + terr.getName();
    bridge.getHistoryWriter().startEvent(transcriptText, units);
    final Change place = ChangeFactory.addUnits(terr, units);
    change.add(place);
    bridge.addChange(change);
}
Also used : UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 68 with CompositeChange

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

the class TriggerAttachment method triggerProductionFrontierEditChange.

public static void triggerProductionFrontierEditChange(final Set<TriggerAttachment> satisfiedTriggers, final IDelegateBridge bridge, final String beforeOrAfter, final String stepName, final boolean useUses, final boolean testUses, final boolean testChance, final boolean testWhen) {
    final GameData data = bridge.getData();
    Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, prodFrontierEditMatch());
    if (testWhen) {
        trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
    }
    if (testUses) {
        trigs = CollectionUtils.getMatches(trigs, availableUses);
    }
    final CompositeChange change = new CompositeChange();
    for (final TriggerAttachment triggerAttachment : trigs) {
        if (testChance && !triggerAttachment.testChance(bridge)) {
            continue;
        }
        if (useUses) {
            triggerAttachment.use(bridge);
        }
        triggerAttachment.getProductionRule().stream().map(s -> s.split(":")).forEach(array -> {
            final ProductionFrontier front = data.getProductionFrontierList().getProductionFrontier(array[0]);
            final String rule = array[1];
            final String ruleName = rule.replaceFirst("^-", "");
            final ProductionRule productionRule = data.getProductionRuleList().getProductionRule(ruleName);
            final boolean ruleAdded = !rule.startsWith("-");
            if (ruleAdded) {
                if (!front.getRules().contains(productionRule)) {
                    change.add(ChangeFactory.addProductionRule(productionRule, front));
                    bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(triggerAttachment.getName()) + ": " + productionRule.getName() + " added to " + front.getName());
                }
            } else {
                if (front.getRules().contains(productionRule)) {
                    change.add(ChangeFactory.removeProductionRule(productionRule, front));
                    bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(triggerAttachment.getName()) + ": " + productionRule.getName() + " removed from " + front.getName());
                }
            }
        });
    }
    if (!change.isEmpty()) {
        // TODO: we should sort the frontier list if we make changes to it...
        bridge.addChange(change);
    }
}
Also used : BattleTracker(games.strategy.triplea.delegate.BattleTracker) ITripleADisplay(games.strategy.triplea.ui.display.ITripleADisplay) Constants(games.strategy.triplea.Constants) Properties(games.strategy.triplea.Properties) IAttachment(games.strategy.engine.data.IAttachment) MutableProperty(games.strategy.engine.data.MutableProperty) DelegateFinder(games.strategy.triplea.delegate.DelegateFinder) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge) Map(java.util.Map) IntegerMap(games.strategy.util.IntegerMap) GameParseException(games.strategy.engine.data.GameParseException) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Attachable(games.strategy.engine.data.Attachable) GameData(games.strategy.engine.data.GameData) List(java.util.List) Tuple(games.strategy.util.Tuple) PlayerID(games.strategy.engine.data.PlayerID) ProductionFrontier(games.strategy.engine.data.ProductionFrontier) Matches(games.strategy.triplea.delegate.Matches) MyFormatter(games.strategy.triplea.formatter.MyFormatter) MapSupport(games.strategy.triplea.MapSupport) TerritoryEffect(games.strategy.engine.data.TerritoryEffect) TechTracker(games.strategy.triplea.delegate.TechTracker) HashMap(java.util.HashMap) Resource(games.strategy.engine.data.Resource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) UnitType(games.strategy.engine.data.UnitType) CompositeChange(games.strategy.engine.data.CompositeChange) TechnologyFrontier(games.strategy.engine.data.TechnologyFrontier) CollectionUtils(games.strategy.util.CollectionUtils) TechAdvance(games.strategy.triplea.delegate.TechAdvance) RelationshipType(games.strategy.engine.data.RelationshipType) Unit(games.strategy.engine.data.Unit) Territory(games.strategy.engine.data.Territory) IDelegate(games.strategy.engine.delegate.IDelegate) ClientLogger(games.strategy.debug.ClientLogger) ChangeFactory(games.strategy.engine.data.changefactory.ChangeFactory) NotificationMessages(games.strategy.triplea.ui.NotificationMessages) Change(games.strategy.engine.data.Change) EndRoundDelegate(games.strategy.triplea.delegate.EndRoundDelegate) OriginalOwnerTracker(games.strategy.triplea.delegate.OriginalOwnerTracker) ProductionRule(games.strategy.engine.data.ProductionRule) AbstractMoveDelegate(games.strategy.triplea.delegate.AbstractMoveDelegate) Collections(java.util.Collections) SoundPath(games.strategy.sound.SoundPath) GameData(games.strategy.engine.data.GameData) ProductionRule(games.strategy.engine.data.ProductionRule) CompositeChange(games.strategy.engine.data.CompositeChange) ProductionFrontier(games.strategy.engine.data.ProductionFrontier)

Example 69 with CompositeChange

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

the class EndRoundDelegate method end.

@Override
public void end() {
    super.end();
    final GameData data = getData();
    if (Properties.getTriggers(data)) {
        final CompositeChange change = new CompositeChange();
        for (final PlayerID player : data.getPlayerList().getPlayers()) {
            change.add(AbstractTriggerAttachment.triggerSetUsedForThisRound(player));
        }
        if (!change.isEmpty()) {
            bridge.getHistoryWriter().startEvent("Setting uses for triggers used this round.");
            bridge.addChange(change);
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 70 with CompositeChange

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

the class AbstractPlaceDelegate method canWeConsumeUnits.

protected boolean canWeConsumeUnits(final Collection<Unit> units, final Territory to, final boolean actuallyDoIt, final CompositeChange change) {
    boolean weCanConsume = true;
    final Collection<Unit> unitsAtStartOfTurnInTo = unitsAtStartOfStepInTerritory(to);
    final Collection<Unit> removedUnits = new ArrayList<>();
    final Collection<Unit> unitsWhichConsume = CollectionUtils.getMatches(units, Matches.unitConsumesUnitsOnCreation());
    for (final Unit unit : unitsWhichConsume) {
        if (Matches.unitWhichConsumesUnitsHasRequiredUnits(unitsAtStartOfTurnInTo).negate().test(unit)) {
            weCanConsume = false;
        }
        if (!weCanConsume) {
            break;
        }
        // remove units which are now consumed, then test the rest of the consuming units on the diminishing pile of units
        // which were in the
        // territory at start of turn
        final UnitAttachment ua = UnitAttachment.get(unit.getType());
        final IntegerMap<UnitType> requiredUnitsMap = ua.getConsumesUnits();
        final Collection<UnitType> requiredUnits = requiredUnitsMap.keySet();
        for (final UnitType ut : requiredUnits) {
            final int requiredNumber = requiredUnitsMap.getInt(ut);
            final Predicate<Unit> unitIsOwnedByAndOfTypeAndNotDamaged = Matches.unitIsOwnedBy(unit.getOwner()).and(Matches.unitIsOfType(ut)).and(Matches.unitHasNotTakenAnyBombingUnitDamage()).and(Matches.unitHasNotTakenAnyDamage()).and(Matches.unitIsNotDisabled());
            final Collection<Unit> unitsBeingRemoved = CollectionUtils.getNMatches(unitsAtStartOfTurnInTo, requiredNumber, unitIsOwnedByAndOfTypeAndNotDamaged);
            unitsAtStartOfTurnInTo.removeAll(unitsBeingRemoved);
            // if we should actually do it, not just test, then add to bridge
            if (actuallyDoIt && change != null) {
                final Change remove = ChangeFactory.removeUnits(to, unitsBeingRemoved);
                change.add(remove);
                removedUnits.addAll(unitsBeingRemoved);
            }
        }
    }
    if (weCanConsume && actuallyDoIt && change != null && !change.isEmpty()) {
        bridge.getHistoryWriter().startEvent("Units in " + to.getName() + " being upgraded or consumed: " + MyFormatter.unitsToTextNoOwner(removedUnits), removedUnits);
    }
    return weCanConsume;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) 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)

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