Search in sources :

Example 1 with ITripleADisplay

use of games.strategy.triplea.ui.display.ITripleADisplay 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 2 with ITripleADisplay

use of games.strategy.triplea.ui.display.ITripleADisplay in project triplea by triplea-game.

the class MustFightBattle method fight.

@Override
public void fight(final IDelegateBridge bridge) {
    // remove units that may already be dead due to a previous event (like they died from a strategic bombing raid,
    // rocket attack, etc)
    removeUnitsThatNoLongerExist();
    if (m_stack.isExecuting()) {
        final ITripleADisplay display = getDisplay(bridge);
        display.showBattle(m_battleID, m_battleSite, getBattleTitle(), removeNonCombatants(m_attackingUnits, true, false, false, false), removeNonCombatants(m_defendingUnits, false, false, false, false), m_killed, m_attackingWaitingToDie, m_defendingWaitingToDie, m_dependentUnits, m_attacker, m_defender, isAmphibious(), getBattleType(), m_amphibiousLandAttackers);
        display.listBattleSteps(m_battleID, m_stepStrings);
        m_stack.execute(bridge);
        return;
    }
    bridge.getHistoryWriter().startEvent("Battle in " + m_battleSite, m_battleSite);
    removeAirNoLongerInTerritory();
    writeUnitsToHistory(bridge);
    // destroyed in combat, and therefore not include factories, aaguns, and infrastructure.
    if (CollectionUtils.getMatches(m_attackingUnits, Matches.unitIsNotInfrastructure()).size() == 0) {
        endBattle(bridge);
        defenderWins(bridge);
        return;
    }
    // therefore not include factories, aaguns, and infrastructure.
    if (CollectionUtils.getMatches(m_defendingUnits, Matches.unitIsNotInfrastructure()).size() == 0) {
        endBattle(bridge);
        attackerWins(bridge);
        return;
    }
    addDependentUnits(TransportTracker.transporting(m_defendingUnits));
    addDependentUnits(TransportTracker.transporting(m_attackingUnits));
    // determine any AA
    updateOffensiveAaUnits();
    updateDefendingAaUnits();
    m_stepStrings = determineStepStrings(true);
    final ITripleADisplay display = getDisplay(bridge);
    display.showBattle(m_battleID, m_battleSite, getBattleTitle(), removeNonCombatants(m_attackingUnits, true, false, false, false), removeNonCombatants(m_defendingUnits, false, false, false, false), m_killed, m_attackingWaitingToDie, m_defendingWaitingToDie, m_dependentUnits, m_attacker, m_defender, isAmphibious(), getBattleType(), m_amphibiousLandAttackers);
    display.listBattleSteps(m_battleID, m_stepStrings);
    if (!m_headless) {
        // take the casualties with least movement first
        if (isAmphibious()) {
            sortAmphib(m_attackingUnits);
        } else {
            BattleCalculator.sortPreBattle(m_attackingUnits);
        }
        BattleCalculator.sortPreBattle(m_defendingUnits);
        // play a sound
        if (m_attackingUnits.stream().anyMatch(Matches.unitIsSea()) || m_defendingUnits.stream().anyMatch(Matches.unitIsSea())) {
            if ((!m_attackingUnits.isEmpty() && m_attackingUnits.stream().allMatch(Matches.unitIsSub())) || (m_attackingUnits.stream().anyMatch(Matches.unitIsSub()) && m_defendingUnits.stream().anyMatch(Matches.unitIsSub()))) {
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_SEA_SUBS, m_attacker);
            } else {
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_SEA_NORMAL, m_attacker);
            }
        } else if (!m_attackingUnits.isEmpty() && m_attackingUnits.stream().allMatch(Matches.unitIsAir()) && !m_defendingUnits.isEmpty() && m_defendingUnits.stream().allMatch(Matches.unitIsAir())) {
            bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_AIR, m_attacker);
        } else {
            bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_LAND, m_attacker);
        }
    }
    // push on stack in opposite order of execution
    pushFightLoopOnStack(true);
    m_stack.execute(bridge);
}
Also used : ITripleADisplay(games.strategy.triplea.ui.display.ITripleADisplay)

Aggregations

ITripleADisplay (games.strategy.triplea.ui.display.ITripleADisplay)2 GameData (games.strategy.engine.data.GameData)1 HashSet (java.util.HashSet)1