Search in sources :

Example 1 with ICondition

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

the class EndTurnDelegate method findNationalObjectiveAndTriggerResources.

/**
 * Find the resources generated by national objectives and triggers that are currently met.
 */
public static IntegerMap<Resource> findNationalObjectiveAndTriggerResources(final PlayerID player, final GameData data) {
    final IDelegateBridge bridge = new ObjectiveDummyDelegateBridge(data);
    // Find and test all the conditions for triggers and national objectives
    final Set<TriggerAttachment> triggers = new HashSet<>();
    final List<RulesAttachment> objectives = new ArrayList<>();
    final HashMap<ICondition, Boolean> testedConditions = testNationalObjectivesAndTriggers(player, data, bridge, triggers, objectives);
    // Find triggers value
    final IntegerMap<Resource> resources;
    final boolean useTriggers = Properties.getTriggers(data);
    if (useTriggers && !triggers.isEmpty()) {
        final Set<TriggerAttachment> toFireTestedAndSatisfied = new HashSet<>(CollectionUtils.getMatches(triggers, AbstractTriggerAttachment.isSatisfiedMatch(testedConditions)));
        resources = TriggerAttachment.findResourceIncome(toFireTestedAndSatisfied, bridge);
    } else {
        resources = new IntegerMap<>();
    }
    // Find national objectives value
    int pus = 0;
    for (final RulesAttachment rule : objectives) {
        final int uses = rule.getUses();
        if (uses == 0 || !rule.isSatisfied(testedConditions)) {
            continue;
        }
        pus += (rule.getObjectiveValue() * rule.getEachMultiple() * Properties.getPuMultiplier(data));
    }
    resources.add(data.getResourceList().getResource(Constants.PUS), pus);
    return resources;
}
Also used : TriggerAttachment(games.strategy.triplea.attachments.TriggerAttachment) AbstractTriggerAttachment(games.strategy.triplea.attachments.AbstractTriggerAttachment) ArrayList(java.util.ArrayList) Resource(games.strategy.engine.data.Resource) ObjectiveDummyDelegateBridge(games.strategy.triplea.ui.ObjectiveDummyDelegateBridge) RulesAttachment(games.strategy.triplea.attachments.RulesAttachment) ICondition(games.strategy.triplea.attachments.ICondition) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge) HashSet(java.util.HashSet)

Example 2 with ICondition

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

the class MoveDelegate method start.

@Override
public void start() {
    super.start();
    final GameData data = getData();
    if (needToInitialize) {
        // territory property changes triggered at beginning of combat move
        // TODO create new delegate called "start of turn" and move them there.
        // 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.
        HashMap<ICondition, Boolean> testedConditions = null;
        final Predicate<TriggerAttachment> moveCombatDelegateBeforeBonusTriggerMatch = AbstractTriggerAttachment.availableUses.and(AbstractTriggerAttachment.whenOrDefaultMatch(null, null)).and(AbstractTriggerAttachment.notificationMatch().or(TriggerAttachment.playerPropertyMatch()).or(TriggerAttachment.relationshipTypePropertyMatch()).or(TriggerAttachment.territoryPropertyMatch()).or(TriggerAttachment.territoryEffectPropertyMatch()).or(TriggerAttachment.removeUnitsMatch()).or(TriggerAttachment.changeOwnershipMatch()));
        final Predicate<TriggerAttachment> moveCombatDelegateAfterBonusTriggerMatch = AbstractTriggerAttachment.availableUses.and(AbstractTriggerAttachment.whenOrDefaultMatch(null, null)).and(TriggerAttachment.placeMatch());
        final Predicate<TriggerAttachment> moveCombatDelegateAllTriggerMatch = moveCombatDelegateBeforeBonusTriggerMatch.or(moveCombatDelegateAfterBonusTriggerMatch);
        if (GameStepPropertiesHelper.isCombatMove(data) && Properties.getTriggers(data)) {
            final HashSet<TriggerAttachment> toFirePossible = TriggerAttachment.collectForAllTriggersMatching(new HashSet<>(Collections.singleton(player)), moveCombatDelegateAllTriggerMatch);
            if (!toFirePossible.isEmpty()) {
                // collect conditions and test them for ALL triggers, both those that we will fire before and those we will
                // fire after.
                testedConditions = TriggerAttachment.collectTestsForAllTriggers(toFirePossible, bridge);
                final HashSet<TriggerAttachment> toFireBeforeBonus = TriggerAttachment.collectForAllTriggersMatching(new HashSet<>(Collections.singleton(player)), moveCombatDelegateBeforeBonusTriggerMatch);
                if (!toFireBeforeBonus.isEmpty()) {
                    // get all triggers that are satisfied based on the tested conditions.
                    final Set<TriggerAttachment> toFireTestedAndSatisfied = new HashSet<>(CollectionUtils.getMatches(toFireBeforeBonus, AbstractTriggerAttachment.isSatisfiedMatch(testedConditions)));
                    // now list out individual types to fire, once for each of the matches above.
                    TriggerAttachment.triggerNotifications(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
                    TriggerAttachment.triggerPlayerPropertyChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
                    TriggerAttachment.triggerRelationshipTypePropertyChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
                    TriggerAttachment.triggerTerritoryPropertyChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
                    TriggerAttachment.triggerTerritoryEffectPropertyChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
                    TriggerAttachment.triggerChangeOwnership(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
                    TriggerAttachment.triggerUnitRemoval(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
                }
            }
        }
        // do this at beginning of combat move)
        if (GameStepPropertiesHelper.isRepairUnits(data)) {
            MoveDelegate.repairMultipleHitPointUnits(bridge, player);
        }
        // giveMovement (like air and naval bases)
        if (GameStepPropertiesHelper.isGiveBonusMovement(data)) {
            resetAndGiveBonusMovement();
        }
        // take away all movement from allied fighters sitting on damaged carriers
        removeMovementFromAirOnDamagedAlliedCarriers(bridge, player);
        // placing triggered units at beginning of combat move, but after bonuses and repairing, etc, have been done.
        if (GameStepPropertiesHelper.isCombatMove(data) && Properties.getTriggers(data)) {
            final HashSet<TriggerAttachment> toFireAfterBonus = TriggerAttachment.collectForAllTriggersMatching(new HashSet<>(Collections.singleton(player)), moveCombatDelegateAfterBonusTriggerMatch);
            if (!toFireAfterBonus.isEmpty()) {
                // get all triggers that are satisfied based on the tested conditions.
                final Set<TriggerAttachment> toFireTestedAndSatisfied = new HashSet<>(CollectionUtils.getMatches(toFireAfterBonus, AbstractTriggerAttachment.isSatisfiedMatch(testedConditions)));
                // now list out individual types to fire, once for each of the matches above.
                TriggerAttachment.triggerUnitPlacement(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
            }
        }
        if (GameStepPropertiesHelper.isResetUnitStateAtStart(data)) {
            resetUnitStateAndDelegateState();
        }
        needToInitialize = false;
    }
}
Also used : TriggerAttachment(games.strategy.triplea.attachments.TriggerAttachment) AbstractTriggerAttachment(games.strategy.triplea.attachments.AbstractTriggerAttachment) GameData(games.strategy.engine.data.GameData) ICondition(games.strategy.triplea.attachments.ICondition) HashSet(java.util.HashSet)

Example 3 with ICondition

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

the class PoliticsDelegate method getValidActions.

@Override
public Collection<PoliticalActionAttachment> getValidActions() {
    final GameData data = bridge.getData();
    data.acquireReadLock();
    final HashMap<ICondition, Boolean> testedConditions;
    try {
        testedConditions = getTestedConditions();
    } finally {
        data.releaseReadLock();
    }
    return PoliticalActionAttachment.getValidActions(player, testedConditions, data);
}
Also used : GameData(games.strategy.engine.data.GameData) ICondition(games.strategy.triplea.attachments.ICondition)

Example 4 with ICondition

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

the class PurchaseDelegate method start.

@Override
public void start() {
    super.start();
    final GameData data = getData();
    if (needToInitialize) {
        if (Properties.getTriggers(data)) {
            // 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> purchaseDelegateTriggerMatch = AbstractTriggerAttachment.availableUses.and(AbstractTriggerAttachment.whenOrDefaultMatch(null, null)).and(TriggerAttachment.prodMatch().or(TriggerAttachment.prodFrontierEditMatch()).or(TriggerAttachment.purchaseMatch()));
            // get all possible triggers based on this match.
            final HashSet<TriggerAttachment> toFirePossible = TriggerAttachment.collectForAllTriggersMatching(new HashSet<>(Collections.singleton(player)), purchaseDelegateTriggerMatch);
            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.triggerProductionChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
                TriggerAttachment.triggerProductionFrontierEditChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
                TriggerAttachment.triggerPurchase(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
            }
        }
        needToInitialize = false;
    }
}
Also used : TriggerAttachment(games.strategy.triplea.attachments.TriggerAttachment) AbstractTriggerAttachment(games.strategy.triplea.attachments.AbstractTriggerAttachment) GameData(games.strategy.engine.data.GameData) ICondition(games.strategy.triplea.attachments.ICondition) HashSet(java.util.HashSet)

Example 5 with ICondition

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

the class TechActivationDelegate method start.

/**
 * Called before the delegate will run. In this class, this does all the
 * work.
 */
@Override
public void start() {
    super.start();
    final GameData data = getData();
    if (!needToInitialize) {
        return;
    }
    // Activate techs
    final Map<PlayerID, Collection<TechAdvance>> techMap = DelegateFinder.techDelegate(data).getAdvances();
    final Collection<TechAdvance> advances = techMap.get(player);
    if ((advances != null) && (advances.size() > 0)) {
        // Start event
        bridge.getHistoryWriter().startEvent(player.getName() + " activating " + advancesAsString(advances));
        for (final TechAdvance advance : advances) {
            TechTracker.addAdvance(player, bridge, advance);
        }
    }
    // empty
    techMap.put(player, null);
    if (Properties.getTriggers(data)) {
        // 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> techActivationDelegateTriggerMatch = TriggerAttachment.availableUses.and(TriggerAttachment.whenOrDefaultMatch(null, null)).and(TriggerAttachment.unitPropertyMatch().or(TriggerAttachment.techMatch()).or(TriggerAttachment.supportMatch()));
        // get all possible triggers based on this match.
        final HashSet<TriggerAttachment> toFirePossible = TriggerAttachment.collectForAllTriggersMatching(new HashSet<>(Collections.singleton(player)), techActivationDelegateTriggerMatch);
        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, TriggerAttachment.isSatisfiedMatch(testedConditions)));
            // now list out individual types to fire, once for each of the matches above.
            TriggerAttachment.triggerUnitPropertyChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
            TriggerAttachment.triggerTechChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
            TriggerAttachment.triggerSupportChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true);
        }
    }
    shareTechnology();
    needToInitialize = false;
}
Also used : TriggerAttachment(games.strategy.triplea.attachments.TriggerAttachment) PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) Collection(java.util.Collection) ICondition(games.strategy.triplea.attachments.ICondition) HashSet(java.util.HashSet)

Aggregations

ICondition (games.strategy.triplea.attachments.ICondition)8 GameData (games.strategy.engine.data.GameData)7 TriggerAttachment (games.strategy.triplea.attachments.TriggerAttachment)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