Search in sources :

Example 11 with GameData

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

the class TriggerAttachment method triggerRelationshipChange.

public static void triggerRelationshipChange(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, relationshipChangeMatch());
    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 String relationshipChange : t.getRelationshipChange()) {
            final String[] s = relationshipChange.split(":");
            final PlayerID player1 = data.getPlayerList().getPlayerId(s[0]);
            final PlayerID player2 = data.getPlayerList().getPlayerId(s[1]);
            final RelationshipType currentRelation = data.getRelationshipTracker().getRelationshipType(player1, player2);
            if (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY) || (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_NEUTRAL) && Matches.relationshipTypeIsNeutral().test(currentRelation)) || (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_ALLIED) && Matches.relationshipTypeIsAllied().test(currentRelation)) || (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_WAR) && Matches.relationshipTypeIsAtWar().test(currentRelation)) || currentRelation.equals(data.getRelationshipTypeList().getRelationshipType(s[2]))) {
                final RelationshipType triggerNewRelation = data.getRelationshipTypeList().getRelationshipType(s[3]);
                change.add(ChangeFactory.relationshipChange(player1, player2, currentRelation, triggerNewRelation));
                bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": Changing Relationship for " + player1.getName() + " and " + player2.getName() + " from " + currentRelation.getName() + " to " + triggerNewRelation.getName());
                AbstractMoveDelegate.getBattleTracker(data).addRelationshipChangesThisTurn(player1, player2, currentRelation, triggerNewRelation);
            /*
           * creation of new battles is handled at the beginning of the battle delegate, in
           * "setupUnitsInSameTerritoryBattles", not here.
           * if (Matches.relationshipTypeIsAtWar().test(triggerNewRelation))
           * triggerMustFightBattle(player1, player2, aBridge);
           */
            }
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) RelationshipType(games.strategy.engine.data.RelationshipType) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 12 with GameData

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

the class TriggerAttachment method triggerNotifications.

// And now for the actual triggers, as called throughout the engine.
// Each trigger should be called exactly twice, once in BaseDelegate (for use with 'when'), and a second time as the
// default location for
// when 'when' is not used.
// Should be void.
public static void triggerNotifications(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, notificationMatch());
    if (testWhen) {
        trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
    }
    if (testUses) {
        trigs = CollectionUtils.getMatches(trigs, availableUses);
    }
    final Set<String> notifications = new HashSet<>();
    for (final TriggerAttachment t : trigs) {
        if (testChance && !t.testChance(bridge)) {
            continue;
        }
        if (useUses) {
            t.use(bridge);
        }
        if (!notifications.contains(t.getNotification())) {
            notifications.add(t.getNotification());
            final String notificationMessageKey = t.getNotification().trim();
            final String sounds = NotificationMessages.getInstance().getSoundsKey(notificationMessageKey);
            if (sounds != null) {
                // play to observers if we are playing to everyone
                bridge.getSoundChannelBroadcaster().playSoundToPlayers(SoundPath.CLIP_TRIGGERED_NOTIFICATION_SOUND + sounds.trim(), t.getPlayers(), null, t.getPlayers().containsAll(data.getPlayerList().getPlayers()));
            }
            final String message = NotificationMessages.getInstance().getMessage(notificationMessageKey);
            if (message != null) {
                String messageForRecord = message.trim();
                if (messageForRecord.length() > 190) {
                    // We don't want to record a giant string in the history panel, so just put a shortened version in instead.
                    messageForRecord = messageForRecord.replaceAll("\\<br.*?>", " ");
                    messageForRecord = messageForRecord.replaceAll("\\<.*?>", "");
                    if (messageForRecord.length() > 195) {
                        messageForRecord = messageForRecord.substring(0, 190) + "....";
                    }
                }
                bridge.getHistoryWriter().startEvent("Note to players " + MyFormatter.defaultNamedToTextList(t.getPlayers()) + ": " + messageForRecord);
                ((ITripleADisplay) bridge.getDisplayChannelBroadcaster()).reportMessageToPlayers(t.getPlayers(), null, ("<html>" + message.trim() + "</html>"), NOTIFICATION);
            }
        }
    }
}
Also used : GameData(games.strategy.engine.data.GameData) ITripleADisplay(games.strategy.triplea.ui.display.ITripleADisplay) HashSet(java.util.HashSet)

Example 13 with GameData

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

the class TriggerAttachment method triggerVictory.

public static void triggerVictory(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, victoryMatch());
    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);
        }
        if (t.getVictory() == null || t.getPlayers() == null) {
            continue;
        }
        final String victoryMessage = NotificationMessages.getInstance().getMessage(t.getVictory().trim());
        final String sounds = NotificationMessages.getInstance().getSoundsKey(t.getVictory().trim());
        if (victoryMessage != null) {
            if (sounds != null) {
                // only play the sound if we are also notifying everyone
                bridge.getSoundChannelBroadcaster().playSoundToPlayers(SoundPath.CLIP_TRIGGERED_VICTORY_SOUND + sounds.trim(), t.getPlayers(), null, true);
                bridge.getSoundChannelBroadcaster().playSoundToPlayers(SoundPath.CLIP_TRIGGERED_DEFEAT_SOUND + sounds.trim(), data.getPlayerList().getPlayers(), t.getPlayers(), false);
            }
            String messageForRecord = victoryMessage.trim();
            if (messageForRecord.length() > 150) {
                messageForRecord = messageForRecord.replaceAll("\\<br.*?>", " ");
                messageForRecord = messageForRecord.replaceAll("\\<.*?>", "");
                if (messageForRecord.length() > 155) {
                    messageForRecord = messageForRecord.substring(0, 150) + "....";
                }
            }
            try {
                bridge.getHistoryWriter().startEvent("Players: " + MyFormatter.defaultNamedToTextList(t.getPlayers()) + " have just won the game, with this victory: " + messageForRecord);
                final IDelegate delegateEndRound = data.getDelegateList().getDelegate("endRound");
                ((EndRoundDelegate) delegateEndRound).signalGameOver(victoryMessage.trim(), t.getPlayers(), bridge);
            } catch (final Exception e) {
                ClientLogger.logQuietly("Failed to signal game over", e);
            }
        }
    }
}
Also used : EndRoundDelegate(games.strategy.triplea.delegate.EndRoundDelegate) GameData(games.strategy.engine.data.GameData) IDelegate(games.strategy.engine.delegate.IDelegate) GameParseException(games.strategy.engine.data.GameParseException)

Example 14 with GameData

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

use of games.strategy.engine.data.GameData 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)

Aggregations

GameData (games.strategy.engine.data.GameData)204 Unit (games.strategy.engine.data.Unit)100 PlayerID (games.strategy.engine.data.PlayerID)92 Territory (games.strategy.engine.data.Territory)92 ArrayList (java.util.ArrayList)83 TripleAUnit (games.strategy.triplea.TripleAUnit)64 HashSet (java.util.HashSet)50 CompositeChange (games.strategy.engine.data.CompositeChange)40 List (java.util.List)36 HashMap (java.util.HashMap)32 Set (java.util.Set)32 Route (games.strategy.engine.data.Route)31 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)30 Collection (java.util.Collection)29 UnitType (games.strategy.engine.data.UnitType)26 Change (games.strategy.engine.data.Change)24 Test (org.junit.jupiter.api.Test)23 Resource (games.strategy.engine.data.Resource)22 TestMapGameData (games.strategy.triplea.xml.TestMapGameData)22 Map (java.util.Map)21