use of games.strategy.triplea.attachments.TriggerAttachment 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;
}
use of games.strategy.triplea.attachments.TriggerAttachment 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;
}
}
use of games.strategy.triplea.attachments.TriggerAttachment 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;
}
}
use of games.strategy.triplea.attachments.TriggerAttachment 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;
}
use of games.strategy.triplea.attachments.TriggerAttachment in project triplea by triplea-game.
the class EndTurnDelegate method determineNationalObjectives.
/**
* Determine if National Objectives have been met, and then do them.
*/
private String determineNationalObjectives(final IDelegateBridge bridge) {
final GameData data = getData();
final PlayerID player = data.getSequence().getStep().getPlayerId();
// 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);
// Execute triggers
final StringBuilder endTurnReport = new StringBuilder();
final boolean useTriggers = Properties.getTriggers(data);
if (useTriggers && !triggers.isEmpty()) {
final Set<TriggerAttachment> toFireTestedAndSatisfied = new HashSet<>(CollectionUtils.getMatches(triggers, AbstractTriggerAttachment.isSatisfiedMatch(testedConditions)));
endTurnReport.append(TriggerAttachment.triggerResourceChange(toFireTestedAndSatisfied, bridge, null, null, true, true, true, true)).append("<br />");
}
// Execute national objectives
for (final RulesAttachment rule : objectives) {
int uses = rule.getUses();
if (uses == 0 || !rule.isSatisfied(testedConditions)) {
continue;
}
int toAdd = rule.getObjectiveValue();
toAdd *= Properties.getPuMultiplier(data);
toAdd *= rule.getEachMultiple();
int total = player.getResources().getQuantity(Constants.PUS) + toAdd;
if (total < 0) {
toAdd -= total;
total = 0;
}
final Change change = ChangeFactory.changeResourcesChange(player, data.getResourceList().getResource(Constants.PUS), toAdd);
bridge.addChange(change);
if (uses > 0) {
uses--;
final Change use = ChangeFactory.attachmentPropertyChange(rule, Integer.toString(uses), "uses");
bridge.addChange(use);
}
final String puMessage = MyFormatter.attachmentNameToText(rule.getName()) + ": " + player.getName() + " met a national objective for an additional " + toAdd + MyFormatter.pluralize(" PU", toAdd) + "; end with " + total + MyFormatter.pluralize(" PU", total);
bridge.getHistoryWriter().startEvent(puMessage);
endTurnReport.append(puMessage).append("<br />");
}
return endTurnReport.toString();
}
Aggregations