use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class TriggerAttachment method triggerTerritoryEffectPropertyChange.
public static void triggerTerritoryEffectPropertyChange(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) {
Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, territoryEffectPropertyMatch());
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 Tuple<String, String> property : t.getTerritoryEffectProperty()) {
for (final TerritoryEffect territoryEffect : t.getTerritoryEffects()) {
String newValue = property.getSecond();
boolean clearFirst = false;
// test if we are resetting the variable first, and if so, remove the leading "-reset-" or "-clear-"
if (newValue.length() > 0 && (newValue.startsWith(PREFIX_CLEAR) || newValue.startsWith(PREFIX_RESET))) {
newValue = newValue.replaceFirst(PREFIX_CLEAR, "").replaceFirst(PREFIX_RESET, "");
clearFirst = true;
}
// covers TerritoryEffectAttachment
if (t.getTerritoryEffectAttachmentName().getFirst().equals("TerritoryEffectAttachment")) {
final TerritoryEffectAttachment attachment = TerritoryEffectAttachment.get(territoryEffect, t.getTerritoryEffectAttachmentName().getSecond());
if (newValue.equals(attachment.getRawPropertyString(property.getFirst()))) {
continue;
}
if (clearFirst && newValue.length() < 1) {
change.add(ChangeFactory.attachmentPropertyReset(attachment, property.getFirst()));
} else {
change.add(ChangeFactory.attachmentPropertyChange(attachment, newValue, property.getFirst(), clearFirst));
}
bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": Setting " + property.getFirst() + (newValue.length() > 0 ? " to " + newValue : " cleared ") + " for " + t.getTerritoryEffectAttachmentName().getSecond() + " attached to " + territoryEffect.getName());
}
// TODO add other attachment changes here if they attach to a territory
}
}
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class TriggerAttachment method placeUnits.
private static void placeUnits(final TriggerAttachment t, final Territory terr, final IntegerMap<UnitType> utMap, final PlayerID player, final IDelegateBridge bridge) {
// createUnits
final List<Unit> units = new ArrayList<>();
for (final UnitType u : utMap.keySet()) {
units.addAll(u.create(utMap.getInt(u), player));
}
final CompositeChange change = new CompositeChange();
// mark no movement
for (final Unit unit : units) {
change.add(ChangeFactory.markNoMovementChange(unit));
}
// place units
final Collection<Unit> factoryAndInfrastructure = CollectionUtils.getMatches(units, Matches.unitIsInfrastructure());
change.add(OriginalOwnerTracker.addOriginalOwnerChange(factoryAndInfrastructure, player));
final String transcriptText = MyFormatter.attachmentNameToText(t.getName()) + ": " + player.getName() + " has " + MyFormatter.unitsToTextNoOwner(units) + " placed in " + terr.getName();
bridge.getHistoryWriter().startEvent(transcriptText, units);
final Change place = ChangeFactory.addUnits(terr, units);
change.add(place);
bridge.addChange(change);
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class TriggerAttachment method triggerProductionFrontierEditChange.
public static void triggerProductionFrontierEditChange(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, prodFrontierEditMatch());
if (testWhen) {
trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
}
if (testUses) {
trigs = CollectionUtils.getMatches(trigs, availableUses);
}
final CompositeChange change = new CompositeChange();
for (final TriggerAttachment triggerAttachment : trigs) {
if (testChance && !triggerAttachment.testChance(bridge)) {
continue;
}
if (useUses) {
triggerAttachment.use(bridge);
}
triggerAttachment.getProductionRule().stream().map(s -> s.split(":")).forEach(array -> {
final ProductionFrontier front = data.getProductionFrontierList().getProductionFrontier(array[0]);
final String rule = array[1];
final String ruleName = rule.replaceFirst("^-", "");
final ProductionRule productionRule = data.getProductionRuleList().getProductionRule(ruleName);
final boolean ruleAdded = !rule.startsWith("-");
if (ruleAdded) {
if (!front.getRules().contains(productionRule)) {
change.add(ChangeFactory.addProductionRule(productionRule, front));
bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(triggerAttachment.getName()) + ": " + productionRule.getName() + " added to " + front.getName());
}
} else {
if (front.getRules().contains(productionRule)) {
change.add(ChangeFactory.removeProductionRule(productionRule, front));
bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(triggerAttachment.getName()) + ": " + productionRule.getName() + " removed from " + front.getName());
}
}
});
}
if (!change.isEmpty()) {
// TODO: we should sort the frontier list if we make changes to it...
bridge.addChange(change);
}
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class EndRoundDelegate method end.
@Override
public void end() {
super.end();
final GameData data = getData();
if (Properties.getTriggers(data)) {
final CompositeChange change = new CompositeChange();
for (final PlayerID player : data.getPlayerList().getPlayers()) {
change.add(AbstractTriggerAttachment.triggerSetUsedForThisRound(player));
}
if (!change.isEmpty()) {
bridge.getHistoryWriter().startEvent("Setting uses for triggers used this round.");
bridge.addChange(change);
}
}
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class AbstractPlaceDelegate method canWeConsumeUnits.
protected boolean canWeConsumeUnits(final Collection<Unit> units, final Territory to, final boolean actuallyDoIt, final CompositeChange change) {
boolean weCanConsume = true;
final Collection<Unit> unitsAtStartOfTurnInTo = unitsAtStartOfStepInTerritory(to);
final Collection<Unit> removedUnits = new ArrayList<>();
final Collection<Unit> unitsWhichConsume = CollectionUtils.getMatches(units, Matches.unitConsumesUnitsOnCreation());
for (final Unit unit : unitsWhichConsume) {
if (Matches.unitWhichConsumesUnitsHasRequiredUnits(unitsAtStartOfTurnInTo).negate().test(unit)) {
weCanConsume = false;
}
if (!weCanConsume) {
break;
}
// remove units which are now consumed, then test the rest of the consuming units on the diminishing pile of units
// which were in the
// territory at start of turn
final UnitAttachment ua = UnitAttachment.get(unit.getType());
final IntegerMap<UnitType> requiredUnitsMap = ua.getConsumesUnits();
final Collection<UnitType> requiredUnits = requiredUnitsMap.keySet();
for (final UnitType ut : requiredUnits) {
final int requiredNumber = requiredUnitsMap.getInt(ut);
final Predicate<Unit> unitIsOwnedByAndOfTypeAndNotDamaged = Matches.unitIsOwnedBy(unit.getOwner()).and(Matches.unitIsOfType(ut)).and(Matches.unitHasNotTakenAnyBombingUnitDamage()).and(Matches.unitHasNotTakenAnyDamage()).and(Matches.unitIsNotDisabled());
final Collection<Unit> unitsBeingRemoved = CollectionUtils.getNMatches(unitsAtStartOfTurnInTo, requiredNumber, unitIsOwnedByAndOfTypeAndNotDamaged);
unitsAtStartOfTurnInTo.removeAll(unitsBeingRemoved);
// if we should actually do it, not just test, then add to bridge
if (actuallyDoIt && change != null) {
final Change remove = ChangeFactory.removeUnits(to, unitsBeingRemoved);
change.add(remove);
removedUnits.addAll(unitsBeingRemoved);
}
}
}
if (weCanConsume && actuallyDoIt && change != null && !change.isEmpty()) {
bridge.getHistoryWriter().startEvent("Units in " + to.getName() + " being upgraded or consumed: " + MyFormatter.unitsToTextNoOwner(removedUnits), removedUnits);
}
return weCanConsume;
}
Aggregations