Search in sources :

Example 11 with CompositeChange

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

the class TriggerAttachment method triggerProductionChange.

public static void triggerProductionChange(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, prodMatch());
    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 PlayerID player : t.getPlayers()) {
            change.add(ChangeFactory.changeProductionFrontier(player, t.getFrontier()));
            bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": " + player.getName() + " has their production frontier changed to: " + t.getFrontier().getName());
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 12 with CompositeChange

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

the class TriggerAttachment method triggerUnitPropertyChange.

public static void triggerUnitPropertyChange(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, unitPropertyMatch());
    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.getUnitProperty()) {
            for (final UnitType unitType : t.getUnitType()) {
                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 UnitAttachment, UnitSupportAttachment
                if (t.getUnitAttachmentName().getFirst().equals("UnitAttachment")) {
                    final UnitAttachment attachment = UnitAttachment.get(unitType, t.getUnitAttachmentName().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.getUnitAttachmentName().getSecond() + " attached to " + unitType.getName());
                } else if (t.getUnitAttachmentName().getFirst().equals("UnitSupportAttachment")) {
                    final UnitSupportAttachment attachment = UnitSupportAttachment.get(unitType, t.getUnitAttachmentName().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.getUnitAttachmentName().getSecond() + " attached to " + unitType.getName());
                }
            // TODO add other attachment changes here if they attach to a unitType
            }
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 13 with CompositeChange

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

the class TriggerAttachment method triggerSupportChange.

public static void triggerSupportChange(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, supportMatch());
    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 PlayerID player : t.getPlayers()) {
            for (final String usaString : t.getSupport().keySet()) {
                UnitSupportAttachment usa = null;
                for (final UnitSupportAttachment support : UnitSupportAttachment.get(data)) {
                    if (support.getName().equals(usaString)) {
                        usa = support;
                        break;
                    }
                }
                if (usa == null) {
                    throw new IllegalStateException("Could not find unitSupportAttachment. name:" + usaString);
                }
                final List<PlayerID> p = new ArrayList<>(usa.getPlayers());
                if (p.contains(player)) {
                    if (!t.getSupport().get(usa.getName())) {
                        p.remove(player);
                        change.add(ChangeFactory.attachmentPropertyChange(usa, p, "players"));
                        bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": " + player.getName() + " is removed from " + usa.toString());
                    }
                } else {
                    if (t.getSupport().get(usa.getName())) {
                        p.add(player);
                        change.add(ChangeFactory.attachmentPropertyChange(usa, p, "players"));
                        bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": " + player.getName() + " is added to " + usa.toString());
                    }
                }
            }
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 14 with CompositeChange

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

the class TriggerAttachment method triggerPlayerPropertyChange.

public static void triggerPlayerPropertyChange(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, playerPropertyMatch());
    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.getPlayerProperty()) {
            for (final PlayerID player : t.getPlayers()) {
                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 PlayerAttachment, TriggerAttachment, RulesAttachment, TechAttachment
                if (t.getPlayerAttachmentName().getFirst().equals("PlayerAttachment")) {
                    final PlayerAttachment attachment = PlayerAttachment.get(player, t.getPlayerAttachmentName().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.getPlayerAttachmentName().getSecond() + " attached to " + player.getName());
                } else if (t.getPlayerAttachmentName().getFirst().equals("RulesAttachment")) {
                    final RulesAttachment attachment = RulesAttachment.get(player, t.getPlayerAttachmentName().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.getPlayerAttachmentName().getSecond() + " attached to " + player.getName());
                } else if (t.getPlayerAttachmentName().getFirst().equals("TriggerAttachment")) {
                    final TriggerAttachment attachment = TriggerAttachment.get(player, t.getPlayerAttachmentName().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.getPlayerAttachmentName().getSecond() + " attached to " + player.getName());
                } else if (t.getPlayerAttachmentName().getFirst().equals("TechAttachment")) {
                    final TechAttachment attachment = TechAttachment.get(player, t.getPlayerAttachmentName().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.getPlayerAttachmentName().getSecond() + " attached to " + player.getName());
                } else if (t.getPlayerAttachmentName().getFirst().equals("PoliticalActionAttachment")) {
                    final PoliticalActionAttachment attachment = PoliticalActionAttachment.get(player, t.getPlayerAttachmentName().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.getPlayerAttachmentName().getSecond() + " attached to " + player.getName());
                } else if (t.getPlayerAttachmentName().getFirst().equals("UserActionAttachment")) {
                    final UserActionAttachment attachment = UserActionAttachment.get(player, t.getPlayerAttachmentName().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.getPlayerAttachmentName().getSecond() + " attached to " + player.getName());
                }
            // TODO add other attachment changes here if they attach to a player
            }
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 15 with CompositeChange

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

the class TriggerAttachment method triggerRelationshipTypePropertyChange.

public static void triggerRelationshipTypePropertyChange(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, relationshipTypePropertyMatch());
    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.getRelationshipTypeProperty()) {
            for (final RelationshipType relationshipType : t.getRelationshipTypes()) {
                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 RelationshipTypeAttachment
                if (t.getRelationshipTypeAttachmentName().getFirst().equals("RelationshipTypeAttachment")) {
                    final RelationshipTypeAttachment attachment = RelationshipTypeAttachment.get(relationshipType, t.getRelationshipTypeAttachmentName().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.getRelationshipTypeAttachmentName().getSecond() + " attached to " + relationshipType.getName());
                }
            // TODO add other attachment changes here if they attach to a territory
            }
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
}
Also used : RelationshipType(games.strategy.engine.data.RelationshipType) 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