use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class MustFightBattle method writeUnitsToHistory.
private void writeUnitsToHistory(final IDelegateBridge bridge) {
if (m_headless) {
return;
}
final Set<PlayerID> playerSet = m_battleSite.getUnits().getPlayersWithUnits();
// find all attacking players (unsorted)
final Collection<PlayerID> attackers = new ArrayList<>();
for (final PlayerID current : playerSet) {
if (m_data.getRelationshipTracker().isAllied(m_attacker, current) || current.equals(m_attacker)) {
attackers.add(current);
}
}
final StringBuilder transcriptText = new StringBuilder();
// find all attacking units (unsorted)
final Collection<Unit> allAttackingUnits = new ArrayList<>();
for (final Iterator<PlayerID> attackersIter = attackers.iterator(); attackersIter.hasNext(); ) {
final PlayerID current = attackersIter.next();
final String delim;
if (attackersIter.hasNext()) {
delim = "; ";
} else {
delim = "";
}
final Collection<Unit> attackingUnits = CollectionUtils.getMatches(m_attackingUnits, Matches.unitIsOwnedBy(current));
final String verb = current.equals(m_attacker) ? "attack" : "loiter and taunt";
transcriptText.append(current.getName()).append(" ").append(verb).append(attackingUnits.isEmpty() ? "" : " with " + MyFormatter.unitsToTextNoOwner(attackingUnits)).append(delim);
allAttackingUnits.addAll(attackingUnits);
// If any attacking transports are in the battle, set their status to later restrict load/unload
if (current.equals(m_attacker)) {
final CompositeChange change = new CompositeChange();
final Collection<Unit> transports = CollectionUtils.getMatches(attackingUnits, Matches.unitCanTransport());
for (final Unit unit : transports) {
change.add(ChangeFactory.unitPropertyChange(unit, true, TripleAUnit.WAS_IN_COMBAT));
}
bridge.addChange(change);
}
}
// write attacking units to history
if (m_attackingUnits.size() > 0) {
bridge.getHistoryWriter().addChildToEvent(transcriptText.toString(), allAttackingUnits);
}
// find all defending players (unsorted)
final Collection<PlayerID> defenders = new ArrayList<>();
for (final PlayerID current : playerSet) {
if (m_data.getRelationshipTracker().isAllied(m_defender, current) || current.equals(m_defender)) {
defenders.add(current);
}
}
final StringBuilder transcriptBuilder = new StringBuilder();
// find all defending units (unsorted)
final Collection<Unit> allDefendingUnits = new ArrayList<>();
for (final Iterator<PlayerID> defendersIter = defenders.iterator(); defendersIter.hasNext(); ) {
final PlayerID current = defendersIter.next();
final String delim;
if (defendersIter.hasNext()) {
delim = "; ";
} else {
delim = "";
}
final Collection<Unit> defendingUnits = CollectionUtils.getMatches(m_defendingUnits, Matches.unitIsOwnedBy(current));
transcriptBuilder.append(current.getName()).append(" defend with ").append(MyFormatter.unitsToTextNoOwner(defendingUnits)).append(delim);
allDefendingUnits.addAll(defendingUnits);
}
// write defending units to history
if (m_defendingUnits.size() > 0) {
bridge.getHistoryWriter().addChildToEvent(transcriptBuilder.toString(), allDefendingUnits);
}
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class MustFightBattle method addAttackChange.
@Override
public Change addAttackChange(final Route route, final Collection<Unit> units, final HashMap<Unit, HashSet<Unit>> targets) {
final CompositeChange change = new CompositeChange();
// Filter out allied units if WW2V2
final Predicate<Unit> ownedBy = Matches.unitIsOwnedBy(m_attacker);
final Collection<Unit> attackingUnits = isWW2V2() ? CollectionUtils.getMatches(units, ownedBy) : units;
final Territory attackingFrom = route.getTerritoryBeforeEnd();
m_attackingFrom.add(attackingFrom);
m_attackingUnits.addAll(attackingUnits);
m_attackingFromMap.computeIfAbsent(attackingFrom, k -> new ArrayList<>());
final Collection<Unit> attackingFromMapUnits = m_attackingFromMap.get(attackingFrom);
attackingFromMapUnits.addAll(attackingUnits);
// are we amphibious
if (route.getStart().isWater() && route.getEnd() != null && !route.getEnd().isWater() && attackingUnits.stream().anyMatch(Matches.unitIsLand())) {
getAmphibiousAttackTerritories().add(route.getTerritoryBeforeEnd());
m_amphibiousLandAttackers.addAll(CollectionUtils.getMatches(attackingUnits, Matches.unitIsLand()));
m_isAmphibious = true;
}
final Map<Unit, Collection<Unit>> dependencies = TransportTracker.transporting(units);
if (!isAlliedAirIndependent()) {
dependencies.putAll(MoveValidator.carrierMustMoveWith(units, units, m_data, m_attacker));
for (final Unit carrier : dependencies.keySet()) {
final UnitAttachment ua = UnitAttachment.get(carrier.getType());
if (ua.getCarrierCapacity() == -1) {
continue;
}
final Collection<Unit> fighters = dependencies.get(carrier);
// Dependencies count both land and air units. Land units could be allied or owned, while air is just allied
// since owned already launched at beginning of turn
fighters.retainAll(CollectionUtils.getMatches(fighters, Matches.unitIsAir()));
for (final Unit fighter : fighters) {
// Set transportedBy for fighter
change.add(ChangeFactory.unitPropertyChange(fighter, carrier, TripleAUnit.TRANSPORTED_BY));
}
// remove transported fighters from battle display
m_attackingUnits.removeAll(fighters);
}
}
addDependentUnits(dependencies);
// mark units with no movement for all but air
Collection<Unit> nonAir = CollectionUtils.getMatches(attackingUnits, Matches.unitIsNotAir());
// so restrict non air to remove land units
if (m_battleSite.isWater()) {
nonAir = CollectionUtils.getMatches(nonAir, Matches.unitIsNotLand());
}
// If attacker stops in an occupied territory, movement stops (battle is optional)
if (MoveValidator.onlyIgnoredUnitsOnPath(route, m_attacker, m_data, false)) {
return change;
}
change.add(ChangeFactory.markNoMovementChange(nonAir));
return change;
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class PoliticsDelegate method getNeutralOutOfWarWithAllies.
private static void getNeutralOutOfWarWithAllies(final PoliticalActionAttachment paa, final PlayerID player, final IDelegateBridge bridge) {
final GameData data = bridge.getData();
if (!Properties.getAlliancesCanChainTogether(data)) {
return;
}
final Collection<PlayerID> players = data.getPlayerList().getPlayers();
final Collection<PlayerID> p1AlliedWith = CollectionUtils.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(player, data));
final CompositeChange change = new CompositeChange();
for (final String relationshipChangeString : paa.getRelationshipChange()) {
final String[] relationshipChange = relationshipChangeString.split(":");
final PlayerID p1 = data.getPlayerList().getPlayerId(relationshipChange[0]);
final PlayerID p2 = data.getPlayerList().getPlayerId(relationshipChange[1]);
if (!(p1.equals(player) || p2.equals(player))) {
continue;
}
final PlayerID otherPlayer = (p1.equals(player) ? p2 : p1);
final RelationshipType currentType = data.getRelationshipTracker().getRelationshipType(p1, p2);
final RelationshipType newType = data.getRelationshipTypeList().getRelationshipType(relationshipChange[2]);
if (Matches.relationshipTypeIsAtWar().test(currentType) && Matches.relationshipTypeIsAtWar().negate().test(newType)) {
final Collection<PlayerID> otherPlayersAlliedWith = CollectionUtils.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(otherPlayer, data));
if (!otherPlayersAlliedWith.contains(otherPlayer)) {
otherPlayersAlliedWith.add(otherPlayer);
}
if (!p1AlliedWith.contains(player)) {
p1AlliedWith.add(player);
}
for (final PlayerID p3 : p1AlliedWith) {
for (final PlayerID p4 : otherPlayersAlliedWith) {
final RelationshipType currentOther = data.getRelationshipTracker().getRelationshipType(p3, p4);
if (!currentOther.equals(newType) && Matches.relationshipTypeIsAtWar().test(currentOther)) {
change.add(ChangeFactory.relationshipChange(p3, p4, currentOther, newType));
bridge.getHistoryWriter().addChildToEvent(p3.getName() + " and " + p4.getName() + " sign a " + newType.getName() + " treaty");
MoveDelegate.getBattleTracker(data).addRelationshipChangesThisTurn(p3, p4, currentOther, newType);
}
}
}
}
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class PoliticsDelegate method getMyselfOutOfAlliance.
private static void getMyselfOutOfAlliance(final PoliticalActionAttachment paa, final PlayerID player, final IDelegateBridge bridge) {
final GameData data = bridge.getData();
if (!Properties.getAlliancesCanChainTogether(data)) {
return;
}
final Collection<PlayerID> players = data.getPlayerList().getPlayers();
final Collection<PlayerID> p1AlliedWith = CollectionUtils.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(player, data));
p1AlliedWith.remove(player);
final CompositeChange change = new CompositeChange();
for (final String relationshipChangeString : paa.getRelationshipChange()) {
final String[] relationshipChange = relationshipChangeString.split(":");
final PlayerID p1 = data.getPlayerList().getPlayerId(relationshipChange[0]);
final PlayerID p2 = data.getPlayerList().getPlayerId(relationshipChange[1]);
if (!(p1.equals(player) || p2.equals(player))) {
continue;
}
final PlayerID otherPlayer = (p1.equals(player) ? p2 : p1);
if (!p1AlliedWith.contains(otherPlayer)) {
continue;
}
final RelationshipType currentType = data.getRelationshipTracker().getRelationshipType(p1, p2);
final RelationshipType newType = data.getRelationshipTypeList().getRelationshipType(relationshipChange[2]);
if (Matches.relationshipTypeIsAlliedAndAlliancesCanChainTogether().test(currentType) && Matches.relationshipTypeIsAlliedAndAlliancesCanChainTogether().negate().test(newType)) {
for (final PlayerID p3 : p1AlliedWith) {
final RelationshipType currentOther = data.getRelationshipTracker().getRelationshipType(p3, player);
if (!currentOther.equals(newType)) {
change.add(ChangeFactory.relationshipChange(p3, player, currentOther, newType));
bridge.getHistoryWriter().addChildToEvent(player.getName() + " and " + p3.getName() + " sign a " + newType.getName() + " treaty");
MoveDelegate.getBattleTracker(data).addRelationshipChangesThisTurn(p3, player, currentOther, newType);
}
}
}
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class PurchaseDelegate method purchase.
@Override
public String purchase(final IntegerMap<ProductionRule> productionRules) {
final IntegerMap<Resource> costs = getCosts(productionRules);
final IntegerMap<NamedAttachable> results = getResults(productionRules);
if (!(canAfford(costs, player))) {
return NOT_ENOUGH_RESOURCES;
}
// check to see if player has too many of any building with a building limit
for (final NamedAttachable next : results.keySet()) {
if (!(next instanceof Resource)) {
final UnitType type = (UnitType) next;
final int quantity = results.getInt(type);
final UnitAttachment ua = UnitAttachment.get(type);
final int maxBuilt = ua.getMaxBuiltPerPlayer();
if (maxBuilt == 0) {
return "May not build any of this unit right now: " + type.getName();
} else if (maxBuilt > 0) {
// count how many units are yet to be placed or are in the field
int currentlyBuilt = player.getUnits().countMatches(Matches.unitIsOfType(type));
final Predicate<Unit> unitTypeOwnedBy = Matches.unitIsOfType(type).and(Matches.unitIsOwnedBy(player));
final Collection<Territory> allTerrs = getData().getMap().getTerritories();
for (final Territory t : allTerrs) {
currentlyBuilt += t.getUnits().countMatches(unitTypeOwnedBy);
}
final int allowedBuild = maxBuilt - currentlyBuilt;
if (allowedBuild - quantity < 0) {
return "May only build " + allowedBuild + " of " + type.getName() + " this turn, may only build " + maxBuilt + " total";
}
}
}
}
// remove first, since add logs PUs remaining
final Collection<Unit> totalUnits = new ArrayList<>();
final Collection<UnitType> totalUnitTypes = new ArrayList<>();
final Collection<Resource> totalResources = new ArrayList<>();
final CompositeChange changes = new CompositeChange();
// and find all added units
for (final NamedAttachable next : results.keySet()) {
if (next instanceof Resource) {
final Resource resource = (Resource) next;
final int quantity = results.getInt(resource);
final Change change = ChangeFactory.changeResourcesChange(player, resource, quantity);
changes.add(change);
for (int i = 0; i < quantity; i++) {
totalResources.add(resource);
}
} else {
final UnitType type = (UnitType) next;
final int quantity = results.getInt(type);
final Collection<Unit> units = type.create(quantity, player);
totalUnits.addAll(units);
for (int i = 0; i < quantity; i++) {
totalUnitTypes.add(type);
}
}
}
final Collection<NamedAttachable> totalAll = new ArrayList<>();
totalAll.addAll(totalUnitTypes);
totalAll.addAll(totalResources);
// add changes for added units
if (!totalUnits.isEmpty()) {
final Change change = ChangeFactory.addUnits(player, totalUnits);
changes.add(change);
}
// add changes for spent resources
final String remaining = removeFromPlayer(costs, changes);
// add history event
final String transcriptText;
if (!totalUnits.isEmpty()) {
transcriptText = player.getName() + " buy " + MyFormatter.defaultNamedToTextList(totalAll, ", ", true) + "; " + remaining;
} else {
transcriptText = player.getName() + " buy nothing; " + remaining;
}
bridge.getHistoryWriter().startEvent(transcriptText, totalUnits);
// commit changes
bridge.addChange(changes);
return null;
}
Aggregations