Search in sources :

Example 6 with TriggerAttachment

use of games.strategy.triplea.attachments.TriggerAttachment in project triplea by triplea-game.

the class BaseTripleADelegate method triggerWhenTriggerAttachments.

private void triggerWhenTriggerAttachments(final String beforeOrAfter) {
    final GameData data = getData();
    if (Properties.getTriggers(data)) {
        final String stepName = data.getSequence().getStep().getName();
        // we use AND in order to make sure there are uses and when is set correctly.
        final Predicate<TriggerAttachment> baseDelegateWhenTriggerMatch = TriggerAttachment.availableUses.and(TriggerAttachment.whenOrDefaultMatch(beforeOrAfter, stepName));
        TriggerAttachment.collectAndFireTriggers(new HashSet<>(data.getPlayerList().getPlayers()), baseDelegateWhenTriggerMatch, bridge, beforeOrAfter, stepName);
    }
    PoliticsDelegate.chainAlliancesTogether(bridge);
}
Also used : TriggerAttachment(games.strategy.triplea.attachments.TriggerAttachment) GameData(games.strategy.engine.data.GameData)

Example 7 with TriggerAttachment

use of games.strategy.triplea.attachments.TriggerAttachment in project triplea by triplea-game.

the class EndRoundDelegate method start.

@Override
public void start() {
    super.start();
    if (gameOver) {
        return;
    }
    String victoryMessage;
    final GameData data = getData();
    if (isPacificTheater()) {
        final PlayerID japanese = data.getPlayerList().getPlayerId(Constants.PLAYER_NAME_JAPANESE);
        final PlayerAttachment pa = PlayerAttachment.get(japanese);
        if (pa != null && pa.getVps() >= 22) {
            victoryMessage = "Axis achieve VP victory";
            bridge.getHistoryWriter().startEvent(victoryMessage);
            final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance(data.getAllianceTracker().getAlliancesPlayerIsIn(japanese).iterator().next());
            signalGameOver(victoryMessage, winners, bridge);
        }
    }
    // Check for Winning conditions
    if (isTotalVictory()) {
        // Check for Win by Victory Cities
        victoryMessage = " achieve TOTAL VICTORY with ";
        checkVictoryCities(bridge, victoryMessage, " Total Victory VCs");
    }
    if (isHonorableSurrender()) {
        victoryMessage = " achieve an HONORABLE VICTORY with ";
        checkVictoryCities(bridge, victoryMessage, " Honorable Victory VCs");
    }
    if (isProjectionOfPower()) {
        victoryMessage = " achieve victory through a PROJECTION OF POWER with ";
        checkVictoryCities(bridge, victoryMessage, " Projection of Power VCs");
    }
    if (isEconomicVictory()) {
        // Check for regular economic victory
        for (final String allianceName : data.getAllianceTracker().getAlliances()) {
            final int victoryAmount = getEconomicVictoryAmount(data, allianceName);
            final Set<PlayerID> teamMembers = data.getAllianceTracker().getPlayersInAlliance(allianceName);
            int teamProd = 0;
            for (final PlayerID player : teamMembers) {
                teamProd += getProduction(player);
                if (teamProd >= victoryAmount) {
                    victoryMessage = allianceName + " achieve economic victory";
                    bridge.getHistoryWriter().startEvent(victoryMessage);
                    final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance(allianceName);
                    // Added this to end the game on victory conditions
                    signalGameOver(victoryMessage, winners, bridge);
                }
            }
        }
    }
    // now check for generic trigger based victories
    if (isTriggeredVictory()) {
        // First set up a match for what we want to have fire as a default in this delegate. List out as a composite match
        // OR.
        // use 'null, null' because this is the Default firing location for any trigger that does NOT have 'when' set.
        final Predicate<TriggerAttachment> endRoundDelegateTriggerMatch = AbstractTriggerAttachment.availableUses.and(AbstractTriggerAttachment.whenOrDefaultMatch(null, null)).and(TriggerAttachment.activateTriggerMatch().or(TriggerAttachment.victoryMatch()));
        // get all possible triggers based on this match.
        final HashSet<TriggerAttachment> toFirePossible = TriggerAttachment.collectForAllTriggersMatching(new HashSet<>(data.getPlayerList().getPlayers()), endRoundDelegateTriggerMatch);
        if (!toFirePossible.isEmpty()) {
            // get all conditions possibly needed by these triggers, and then test them.
            final HashMap<ICondition, Boolean> testedConditions = TriggerAttachment.collectTestsForAllTriggers(toFirePossible, bridge);
            // get all triggers that are satisfied based on the tested conditions.
            final Set<TriggerAttachment> toFireTestedAndSatisfied = new HashSet<>(CollectionUtils.getMatches(toFirePossible, AbstractTriggerAttachment.isSatisfiedMatch(testedConditions)));
            // now list out individual types to fire, once for each of the matches above.
            TriggerAttachment.triggerActivateTriggerOther(testedConditions, toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
            // will call
            TriggerAttachment.triggerVictory(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
        // signalGameOver itself
        }
    }
    if (isWW2V2() || isWW2V3()) {
        return;
    }
    final PlayerList playerList = data.getPlayerList();
    // now test older maps that only use these 5 players, to see if someone has won
    final PlayerID russians = playerList.getPlayerId(Constants.PLAYER_NAME_RUSSIANS);
    final PlayerID germans = playerList.getPlayerId(Constants.PLAYER_NAME_GERMANS);
    final PlayerID british = playerList.getPlayerId(Constants.PLAYER_NAME_BRITISH);
    final PlayerID japanese = playerList.getPlayerId(Constants.PLAYER_NAME_JAPANESE);
    final PlayerID americans = playerList.getPlayerId(Constants.PLAYER_NAME_AMERICANS);
    if (germans == null || russians == null || british == null || japanese == null || americans == null || playerList.size() > 5) {
        return;
    }
    // Quick check to see who still owns their own capital
    final boolean russia = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(russians, data).getOwner().equals(russians);
    final boolean germany = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(germans, data).getOwner().equals(germans);
    final boolean britain = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(british, data).getOwner().equals(british);
    final boolean japan = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(japanese, data).getOwner().equals(japanese);
    final boolean america = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(americans, data).getOwner().equals(americans);
    int count = 0;
    if (!russia) {
        count++;
    }
    if (!britain) {
        count++;
    }
    if (!america) {
        count++;
    }
    victoryMessage = " achieve a military victory";
    if (germany && japan && count >= 2) {
        bridge.getHistoryWriter().startEvent("Axis" + victoryMessage);
        final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance("Axis");
        signalGameOver("Axis" + victoryMessage, winners, bridge);
    }
    if (russia && !germany && britain && !japan && america) {
        bridge.getHistoryWriter().startEvent("Allies" + victoryMessage);
        final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance("Allies");
        signalGameOver("Allies" + victoryMessage, winners, bridge);
    }
}
Also used : TriggerAttachment(games.strategy.triplea.attachments.TriggerAttachment) AbstractTriggerAttachment(games.strategy.triplea.attachments.AbstractTriggerAttachment) PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) PlayerList(games.strategy.engine.data.PlayerList) PlayerAttachment(games.strategy.triplea.attachments.PlayerAttachment) ICondition(games.strategy.triplea.attachments.ICondition) HashSet(java.util.HashSet)

Aggregations

TriggerAttachment (games.strategy.triplea.attachments.TriggerAttachment)7 GameData (games.strategy.engine.data.GameData)6 ICondition (games.strategy.triplea.attachments.ICondition)6 HashSet (java.util.HashSet)6 AbstractTriggerAttachment (games.strategy.triplea.attachments.AbstractTriggerAttachment)5 PlayerID (games.strategy.engine.data.PlayerID)3 RulesAttachment (games.strategy.triplea.attachments.RulesAttachment)2 ArrayList (java.util.ArrayList)2 Change (games.strategy.engine.data.Change)1 CompositeChange (games.strategy.engine.data.CompositeChange)1 PlayerList (games.strategy.engine.data.PlayerList)1 Resource (games.strategy.engine.data.Resource)1 IDelegateBridge (games.strategy.engine.delegate.IDelegateBridge)1 PlayerAttachment (games.strategy.triplea.attachments.PlayerAttachment)1 ObjectiveDummyDelegateBridge (games.strategy.triplea.ui.ObjectiveDummyDelegateBridge)1 Collection (java.util.Collection)1