Search in sources :

Example 16 with PlayerID

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

the class TriggerAttachment method setActivateTrigger.

private void setActivateTrigger(final String value) throws GameParseException {
    // triggerName:numberOfTimes:useUses:testUses:testConditions:testChance
    final String[] s = value.split(":");
    if (s.length != 6) {
        throw new GameParseException("activateTrigger must have 6 parts: triggerName:numberOfTimes:useUses:testUses:testConditions:testChance" + thisErrorMsg());
    }
    TriggerAttachment trigger = null;
    for (final PlayerID player : getData().getPlayerList().getPlayers()) {
        for (final TriggerAttachment ta : getTriggers(player, null)) {
            if (ta.getName().equals(s[0])) {
                trigger = ta;
                break;
            }
        }
        if (trigger != null) {
            break;
        }
    }
    if (trigger == null) {
        throw new GameParseException("No TriggerAttachment named: " + s[0] + thisErrorMsg());
    }
    if (trigger == this) {
        throw new GameParseException("Cannot have a trigger activate itself!" + thisErrorMsg());
    }
    String options = value;
    options = options.replaceFirst((s[0] + ":"), "");
    final int numberOfTimes = getInt(s[1]);
    if (numberOfTimes < 0) {
        throw new GameParseException("activateTrigger must be positive for the number of times to fire: " + s[1] + thisErrorMsg());
    }
    getBool(s[2]);
    getBool(s[3]);
    getBool(s[4]);
    getBool(s[5]);
    m_activateTrigger.add(Tuple.of(s[0], options));
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameParseException(games.strategy.engine.data.GameParseException)

Example 17 with PlayerID

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

the class TriggerAttachment method triggerActivateTriggerOther.

public static void triggerActivateTriggerOther(final HashMap<ICondition, Boolean> testedConditionsSoFar, 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, activateTriggerMatch());
    if (testWhen) {
        trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
    }
    if (testUses) {
        trigs = CollectionUtils.getMatches(trigs, availableUses);
    }
    for (final TriggerAttachment t : trigs) {
        if (testChance && !t.testChance(bridge)) {
            continue;
        }
        if (useUses) {
            t.use(bridge);
        }
        final int eachMultiple = getEachMultiple(t);
        for (final Tuple<String, String> tuple : t.getActivateTrigger()) {
            // numberOfTimes:useUses:testUses:testConditions:testChance
            TriggerAttachment toFire = null;
            for (final PlayerID player : data.getPlayerList().getPlayers()) {
                for (final TriggerAttachment ta : TriggerAttachment.getTriggers(player, null)) {
                    if (ta.getName().equals(tuple.getFirst())) {
                        toFire = ta;
                        break;
                    }
                }
                if (toFire != null) {
                    break;
                }
            }
            final HashSet<TriggerAttachment> toFireSet = new HashSet<>();
            toFireSet.add(toFire);
            final String[] options = tuple.getSecond().split(":");
            final int numberOfTimesToFire = getInt(options[0]);
            final boolean useUsesToFire = getBool(options[1]);
            final boolean testUsesToFire = getBool(options[2]);
            final boolean testConditionsToFire = getBool(options[3]);
            final boolean testChanceToFire = getBool(options[4]);
            if (testConditionsToFire) {
                if (!testedConditionsSoFar.containsKey(toFire)) {
                    // this should directly add the new tests to testConditionsToFire...
                    collectTestsForAllTriggers(toFireSet, bridge, new HashSet<>(testedConditionsSoFar.keySet()), testedConditionsSoFar);
                }
                if (!isSatisfiedMatch(testedConditionsSoFar).test(toFire)) {
                    continue;
                }
            }
            for (int i = 0; i < numberOfTimesToFire * eachMultiple; ++i) {
                bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + " activates a trigger called: " + MyFormatter.attachmentNameToText(toFire.getName()));
                fireTriggers(toFireSet, testedConditionsSoFar, bridge, beforeOrAfter, stepName, useUsesToFire, testUsesToFire, testChanceToFire, false);
            }
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) HashSet(java.util.HashSet)

Example 18 with PlayerID

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

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

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

the class UnitAttachment method setDestroyedWhenCapturedBy.

private void setDestroyedWhenCapturedBy(String value) throws GameParseException {
    // We can prefix this value with "BY" or "FROM" to change the setting. If no setting, default to "BY" since this
    // this is called by
    // destroyedWhenCapturedBy
    String byOrFrom = "BY";
    if (value.startsWith("BY:") && getData().getPlayerList().getPlayerId("BY") == null) {
        byOrFrom = "BY";
        value = value.replaceFirst("BY:", "");
    } else if (value.startsWith("FROM:") && getData().getPlayerList().getPlayerId("FROM") == null) {
        byOrFrom = "FROM";
        value = value.replaceFirst("FROM:", "");
    }
    final String[] temp = value.split(":");
    for (final String name : temp) {
        final PlayerID tempPlayer = getData().getPlayerList().getPlayerId(name);
        if (tempPlayer != null) {
            m_destroyedWhenCapturedBy.add(Tuple.of(byOrFrom, tempPlayer));
        } else {
            throw new GameParseException("No player named: " + name + thisErrorMsg());
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameParseException(games.strategy.engine.data.GameParseException)

Aggregations

PlayerID (games.strategy.engine.data.PlayerID)323 Territory (games.strategy.engine.data.Territory)163 Unit (games.strategy.engine.data.Unit)133 Test (org.junit.jupiter.api.Test)122 TripleAUnit (games.strategy.triplea.TripleAUnit)104 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)94 GameData (games.strategy.engine.data.GameData)90 ArrayList (java.util.ArrayList)79 UnitType (games.strategy.engine.data.UnitType)74 Route (games.strategy.engine.data.Route)67 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)46 HashSet (java.util.HashSet)44 Change (games.strategy.engine.data.Change)29 CompositeChange (games.strategy.engine.data.CompositeChange)29 IntegerMap (games.strategy.util.IntegerMap)29 List (java.util.List)29 HashMap (java.util.HashMap)28 Collection (java.util.Collection)27 Resource (games.strategy.engine.data.Resource)25 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)23