use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class AbstractPlaceDelegate method moveAirOntoNewCarriers.
// TODO Here's the spot for special air placement rules
protected String moveAirOntoNewCarriers(final Territory at, final Territory producer, final Collection<Unit> units, final PlayerID player, final CompositeChange placeChange) {
if (!at.isWater()) {
return null;
}
if (!canMoveExistingFightersToNewCarriers() || AirThatCantLandUtil.isLhtrCarrierProduction(getData())) {
return null;
}
if (units.stream().noneMatch(Matches.unitIsCarrier())) {
return null;
}
// do we have any spare carrier capacity
int capacity = AirMovementValidator.carrierCapacity(units, at);
// subtract fighters that have already been produced with this carrier
// this turn.
capacity -= AirMovementValidator.carrierCost(units);
if (capacity <= 0) {
return null;
}
if (!Matches.territoryIsLand().test(producer)) {
return null;
}
if (!producer.getUnits().anyMatch(Matches.unitCanProduceUnits())) {
return null;
}
final Predicate<Unit> ownedFighters = Matches.unitCanLandOnCarrier().and(Matches.unitIsOwnedBy(player));
if (!producer.getUnits().anyMatch(ownedFighters)) {
return null;
}
if (wasConquered(producer)) {
return null;
}
if (getAlreadyProduced(producer).stream().anyMatch(Matches.unitCanProduceUnits())) {
return null;
}
final List<Unit> fighters = producer.getUnits().getMatches(ownedFighters);
final Collection<Unit> movedFighters = getRemotePlayer().getNumberOfFightersToMoveToNewCarrier(fighters, producer);
if (movedFighters == null || movedFighters.isEmpty()) {
return null;
}
final Change change = ChangeFactory.moveUnits(producer, at, movedFighters);
placeChange.add(change);
return MyFormatter.unitsToTextNoOwner(movedFighters) + " moved from " + producer.getName() + " to " + at.getName();
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class AbstractPlaceDelegate method performPlaceFrom.
/**
* @param producer
* territory that produces the new units.
* @param placeableUnits
* the new units
* @param at
* territory where the new units get placed
*/
protected void performPlaceFrom(final Territory producer, final Collection<Unit> placeableUnits, final Territory at, final PlayerID player) {
final CompositeChange change = new CompositeChange();
// make sure we can place consuming units
final boolean didIt = canWeConsumeUnits(placeableUnits, at, true, change);
if (!didIt) {
throw new IllegalStateException("Something wrong with consuming/upgrading units");
}
final Collection<Unit> factoryAndInfrastructure = CollectionUtils.getMatches(placeableUnits, Matches.unitIsInfrastructure());
if (!factoryAndInfrastructure.isEmpty()) {
change.add(OriginalOwnerTracker.addOriginalOwnerChange(factoryAndInfrastructure, player));
}
// can we move planes to land there
final String movedAirTranscriptTextForHistory = moveAirOntoNewCarriers(at, producer, placeableUnits, player, change);
final Change remove = ChangeFactory.removeUnits(player, placeableUnits);
final Change place = ChangeFactory.addUnits(at, placeableUnits);
change.add(remove);
change.add(place);
final UndoablePlacement currentPlacement = new UndoablePlacement(change, producer, at, placeableUnits);
placements.add(currentPlacement);
updateUndoablePlacementIndexes();
final String transcriptText = MyFormatter.unitsToTextNoOwner(placeableUnits) + " placed in " + at.getName();
bridge.getHistoryWriter().startEvent(transcriptText, currentPlacement.getDescriptionObject());
if (movedAirTranscriptTextForHistory != null) {
bridge.getHistoryWriter().addChildToEvent(movedAirTranscriptTextForHistory);
}
bridge.addChange(change);
updateProducedMap(producer, placeableUnits);
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class TriggerAttachment method removeUnits.
private static void removeUnits(final TriggerAttachment t, final Territory terr, final IntegerMap<UnitType> utMap, final PlayerID player, final IDelegateBridge bridge) {
final CompositeChange change = new CompositeChange();
final Collection<Unit> totalRemoved = new ArrayList<>();
for (final UnitType ut : utMap.keySet()) {
final int removeNum = utMap.getInt(ut);
final Collection<Unit> toRemove = CollectionUtils.getNMatches(terr.getUnits().getUnits(), removeNum, Matches.unitIsOwnedBy(player).and(Matches.unitIsOfType(ut)));
if (!toRemove.isEmpty()) {
totalRemoved.addAll(toRemove);
change.add(ChangeFactory.removeUnits(terr, toRemove));
}
}
if (!change.isEmpty()) {
final String transcriptText = MyFormatter.attachmentNameToText(t.getName()) + ": has removed " + MyFormatter.unitsToTextNoOwner(totalRemoved) + " owned by " + player.getName() + " in " + terr.getName();
bridge.getHistoryWriter().startEvent(transcriptText, totalRemoved);
bridge.addChange(change);
}
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class TriggerAttachment method triggerRelationshipChange.
public static void triggerRelationshipChange(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, relationshipChangeMatch());
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 String relationshipChange : t.getRelationshipChange()) {
final String[] s = relationshipChange.split(":");
final PlayerID player1 = data.getPlayerList().getPlayerId(s[0]);
final PlayerID player2 = data.getPlayerList().getPlayerId(s[1]);
final RelationshipType currentRelation = data.getRelationshipTracker().getRelationshipType(player1, player2);
if (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY) || (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_NEUTRAL) && Matches.relationshipTypeIsNeutral().test(currentRelation)) || (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_ALLIED) && Matches.relationshipTypeIsAllied().test(currentRelation)) || (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_WAR) && Matches.relationshipTypeIsAtWar().test(currentRelation)) || currentRelation.equals(data.getRelationshipTypeList().getRelationshipType(s[2]))) {
final RelationshipType triggerNewRelation = data.getRelationshipTypeList().getRelationshipType(s[3]);
change.add(ChangeFactory.relationshipChange(player1, player2, currentRelation, triggerNewRelation));
bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": Changing Relationship for " + player1.getName() + " and " + player2.getName() + " from " + currentRelation.getName() + " to " + triggerNewRelation.getName());
AbstractMoveDelegate.getBattleTracker(data).addRelationshipChangesThisTurn(player1, player2, currentRelation, triggerNewRelation);
/*
* creation of new battles is handled at the beginning of the battle delegate, in
* "setupUnitsInSameTerritoryBattles", not here.
* if (Matches.relationshipTypeIsAtWar().test(triggerNewRelation))
* triggerMustFightBattle(player1, player2, aBridge);
*/
}
}
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class TriggerAttachment method triggerTerritoryPropertyChange.
public static void triggerTerritoryPropertyChange(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, territoryPropertyMatch());
if (testWhen) {
trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
}
if (testUses) {
trigs = CollectionUtils.getMatches(trigs, availableUses);
}
final CompositeChange change = new CompositeChange();
final HashSet<Territory> territoriesNeedingReDraw = new HashSet<>();
for (final TriggerAttachment t : trigs) {
if (testChance && !t.testChance(bridge)) {
continue;
}
if (useUses) {
t.use(bridge);
}
for (final Tuple<String, String> property : t.getTerritoryProperty()) {
for (final Territory territory : t.getTerritories()) {
territoriesNeedingReDraw.add(territory);
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 TerritoryAttachment, CanalAttachment
if (t.getTerritoryAttachmentName().getFirst().equals("TerritoryAttachment")) {
final TerritoryAttachment attachment = TerritoryAttachment.get(territory, t.getTerritoryAttachmentName().getSecond());
if (attachment == null) {
// water territories may not have an attachment, so this could be null
throw new IllegalStateException("Triggers: No territory attachment for:" + territory.getName());
}
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.getTerritoryAttachmentName().getSecond() + " attached to " + territory.getName());
} else if (t.getTerritoryAttachmentName().getFirst().equals("CanalAttachment")) {
final CanalAttachment attachment = CanalAttachment.get(territory, t.getTerritoryAttachmentName().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.getTerritoryAttachmentName().getSecond() + " attached to " + territory.getName());
}
// TODO add other attachment changes here if they attach to a territory
}
}
}
if (!change.isEmpty()) {
bridge.addChange(change);
for (final Territory territory : territoriesNeedingReDraw) {
territory.notifyAttachmentChanged();
}
}
}
Aggregations