use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class PurchaseDelegate method removeFromPlayer.
protected String removeFromPlayer(final IntegerMap<Resource> costs, final CompositeChange changes) {
final StringBuilder returnString = new StringBuilder("Remaining resources: ");
final IntegerMap<Resource> left = player.getResources().getResourcesCopy();
left.subtract(costs);
for (final Entry<Resource, Integer> entry : left.entrySet()) {
returnString.append(entry.getValue()).append(" ").append(entry.getKey().getName()).append("; ");
}
for (final Resource resource : costs.keySet()) {
final float quantity = costs.getInt(resource);
final int cost = (int) quantity;
final Change change = ChangeFactory.changeResourcesChange(player, resource, -cost);
changes.add(change);
}
return returnString.toString();
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class PurchaseDelegate method purchaseRepair.
@Override
public String purchaseRepair(final Map<Unit, IntegerMap<RepairRule>> repairRules) {
final IntegerMap<Resource> costs = getRepairCosts(repairRules, player);
if (!(canAfford(costs, player))) {
return NOT_ENOUGH_RESOURCES;
}
if (!Properties.getDamageFromBombingDoneToUnitsInsteadOfTerritories(getData())) {
return null;
}
// Get the map of the factories that were repaired and how much for each
final IntegerMap<Unit> repairMap = getUnitRepairs(repairRules);
if (repairMap.isEmpty()) {
return null;
}
// remove first, since add logs PUs remaining
final CompositeChange changes = new CompositeChange();
final Set<Unit> repairUnits = new HashSet<>(repairMap.keySet());
final IntegerMap<Unit> damageMap = new IntegerMap<>();
for (final Unit u : repairUnits) {
final int repairCount = repairMap.getInt(u);
// Display appropriate damaged/repaired factory and factory damage totals
if (repairCount > 0) {
final TripleAUnit taUnit = (TripleAUnit) u;
final int newDamageTotal = Math.max(0, taUnit.getUnitDamage() - repairCount);
if (newDamageTotal != taUnit.getUnitDamage()) {
damageMap.put(u, newDamageTotal);
}
}
}
if (!damageMap.isEmpty()) {
changes.add(ChangeFactory.bombingUnitDamage(damageMap));
}
// add changes for spent resources
final String remaining = removeFromPlayer(costs, changes);
// add history event
final String transcriptText;
if (!damageMap.isEmpty()) {
transcriptText = player.getName() + " repair damage of " + MyFormatter.integerUnitMapToString(repairMap, ", ", "x ", true) + "; " + remaining;
} else {
transcriptText = player.getName() + " repair nothing; " + remaining;
}
bridge.getHistoryWriter().startEvent(transcriptText, new HashSet<>(damageMap.keySet()));
// commit changes
if (!changes.isEmpty()) {
bridge.addChange(changes);
}
return null;
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class RandomStartDelegate method setupBoard.
private void setupBoard() {
final GameData data = getData();
final boolean randomTerritories = Properties.getTerritoriesAreAssignedRandomly(data);
final Predicate<Territory> pickableTerritoryMatch = getTerritoryPickableMatch();
final Predicate<PlayerID> playerCanPickMatch = getPlayerCanPickMatch();
final List<Territory> allPickableTerritories = CollectionUtils.getMatches(data.getMap().getTerritories(), pickableTerritoryMatch);
final List<PlayerID> playersCanPick = new ArrayList<>(CollectionUtils.getMatches(data.getPlayerList().getPlayers(), playerCanPickMatch));
// we need a main event
if (!playersCanPick.isEmpty()) {
bridge.getHistoryWriter().startEvent("Assigning Territories");
}
// for random:
final int[] hitRandom = (!randomTerritories ? new int[0] : bridge.getRandom(allPickableTerritories.size(), allPickableTerritories.size(), null, DiceType.ENGINE, "Picking random territories"));
int i = 0;
int pos = 0;
// divvy up territories
while (!allPickableTerritories.isEmpty() && !playersCanPick.isEmpty()) {
if (currentPickingPlayer == null || !playersCanPick.contains(currentPickingPlayer)) {
currentPickingPlayer = playersCanPick.get(0);
}
if (!Interruptibles.sleep(250)) {
return;
}
Territory picked;
if (randomTerritories) {
pos += hitRandom[i];
i++;
final IntegerMap<UnitType> costs = TuvUtils.getCostsForTuv(currentPickingPlayer, data);
final List<Unit> units = new ArrayList<>(currentPickingPlayer.getUnits().getUnits());
units.sort(Comparator.comparingInt(unit -> costs.getInt(unit.getType())));
final Set<Unit> unitsToPlace = new HashSet<>();
unitsToPlace.add(units.get(0));
picked = allPickableTerritories.get(pos % allPickableTerritories.size());
final CompositeChange change = new CompositeChange();
change.add(ChangeFactory.changeOwner(picked, currentPickingPlayer));
final Collection<Unit> factoryAndInfrastructure = CollectionUtils.getMatches(unitsToPlace, Matches.unitIsInfrastructure());
if (!factoryAndInfrastructure.isEmpty()) {
change.add(OriginalOwnerTracker.addOriginalOwnerChange(factoryAndInfrastructure, currentPickingPlayer));
}
change.add(ChangeFactory.removeUnits(currentPickingPlayer, unitsToPlace));
change.add(ChangeFactory.addUnits(picked, unitsToPlace));
bridge.getHistoryWriter().addChildToEvent(currentPickingPlayer.getName() + " receives territory " + picked.getName() + " with units " + MyFormatter.unitsToTextNoOwner(unitsToPlace), picked);
bridge.addChange(change);
} else {
Set<Unit> unitsToPlace;
while (true) {
final Tuple<Territory, Set<Unit>> pick = getRemotePlayer(currentPickingPlayer).pickTerritoryAndUnits(new ArrayList<>(allPickableTerritories), new ArrayList<>(currentPickingPlayer.getUnits().getUnits()), UNITS_PER_PICK);
picked = pick.getFirst();
unitsToPlace = pick.getSecond();
if (!allPickableTerritories.contains(picked) || !currentPickingPlayer.getUnits().getUnits().containsAll(unitsToPlace) || unitsToPlace.size() > UNITS_PER_PICK || (unitsToPlace.size() < UNITS_PER_PICK && unitsToPlace.size() < currentPickingPlayer.getUnits().getUnits().size())) {
getRemotePlayer(currentPickingPlayer).reportMessage("Chosen territory or units invalid!", "Chosen territory or units invalid!");
} else {
break;
}
}
final CompositeChange change = new CompositeChange();
change.add(ChangeFactory.changeOwner(picked, currentPickingPlayer));
final Collection<Unit> factoryAndInfrastructure = CollectionUtils.getMatches(unitsToPlace, Matches.unitIsInfrastructure());
if (!factoryAndInfrastructure.isEmpty()) {
change.add(OriginalOwnerTracker.addOriginalOwnerChange(factoryAndInfrastructure, currentPickingPlayer));
}
change.add(ChangeFactory.removeUnits(currentPickingPlayer, unitsToPlace));
change.add(ChangeFactory.addUnits(picked, unitsToPlace));
bridge.getHistoryWriter().addChildToEvent(currentPickingPlayer.getName() + " picks territory " + picked.getName() + " and places in it " + MyFormatter.unitsToTextNoOwner(unitsToPlace), unitsToPlace);
bridge.addChange(change);
}
allPickableTerritories.remove(picked);
final PlayerID lastPlayer = currentPickingPlayer;
currentPickingPlayer = getNextPlayer(playersCanPick, currentPickingPlayer);
if (!playerCanPickMatch.test(lastPlayer)) {
playersCanPick.remove(lastPlayer);
}
if (playersCanPick.isEmpty()) {
currentPickingPlayer = null;
}
}
// place any remaining units
while (!playersCanPick.isEmpty()) {
if (currentPickingPlayer == null || !playersCanPick.contains(currentPickingPlayer)) {
currentPickingPlayer = playersCanPick.get(0);
}
final List<Territory> territoriesToPickFrom = data.getMap().getTerritoriesOwnedBy(currentPickingPlayer);
Territory picked;
Set<Unit> unitsToPlace;
while (true) {
final Tuple<Territory, Set<Unit>> pick = getRemotePlayer(currentPickingPlayer).pickTerritoryAndUnits(new ArrayList<>(territoriesToPickFrom), new ArrayList<>(currentPickingPlayer.getUnits().getUnits()), UNITS_PER_PICK);
picked = pick.getFirst();
unitsToPlace = pick.getSecond();
if (!territoriesToPickFrom.contains(picked) || !currentPickingPlayer.getUnits().getUnits().containsAll(unitsToPlace) || unitsToPlace.size() > UNITS_PER_PICK || (unitsToPlace.size() < UNITS_PER_PICK && unitsToPlace.size() < currentPickingPlayer.getUnits().getUnits().size())) {
getRemotePlayer(currentPickingPlayer).reportMessage("Chosen territory or units invalid!", "Chosen territory or units invalid!");
} else {
break;
}
}
final CompositeChange change = new CompositeChange();
final Collection<Unit> factoryAndInfrastructure = CollectionUtils.getMatches(unitsToPlace, Matches.unitIsInfrastructure());
if (!factoryAndInfrastructure.isEmpty()) {
change.add(OriginalOwnerTracker.addOriginalOwnerChange(factoryAndInfrastructure, currentPickingPlayer));
}
change.add(ChangeFactory.removeUnits(currentPickingPlayer, unitsToPlace));
change.add(ChangeFactory.addUnits(picked, unitsToPlace));
bridge.getHistoryWriter().addChildToEvent(currentPickingPlayer.getName() + " places " + MyFormatter.unitsToTextNoOwner(unitsToPlace) + " in territory " + picked.getName(), unitsToPlace);
bridge.addChange(change);
final PlayerID lastPlayer = currentPickingPlayer;
currentPickingPlayer = getNextPlayer(playersCanPick, currentPickingPlayer);
if (!playerCanPickMatch.test(lastPlayer)) {
playersCanPick.remove(lastPlayer);
}
if (playersCanPick.isEmpty()) {
currentPickingPlayer = null;
}
}
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class EndTurnDelegate method addUnitCreatedResources.
private String addUnitCreatedResources(final IDelegateBridge bridge) {
// Find total unit generated resources for all owned units
final GameData data = getData();
final PlayerID player = data.getSequence().getStep().getPlayerId();
final IntegerMap<Resource> resourceTotalsMap = findUnitCreatedResources(player, data);
// Add resource changes and create end turn report string
final StringBuilder endTurnReport = new StringBuilder();
final CompositeChange change = new CompositeChange();
for (final Resource resource : resourceTotalsMap.keySet()) {
int toAdd = resourceTotalsMap.getInt(resource);
if (toAdd == 0) {
continue;
}
int total = player.getResources().getQuantity(resource) + toAdd;
if (total < 0) {
toAdd -= total;
total = 0;
}
final String transcriptText = "Units generate " + toAdd + " " + resource.getName() + "; " + player.getName() + " end with " + total + " " + resource.getName();
bridge.getHistoryWriter().startEvent(transcriptText);
endTurnReport.append(transcriptText).append("<br />");
final Change resources = ChangeFactory.changeResourcesChange(player, resource, toAdd);
change.add(resources);
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
return endTurnReport.toString();
}
use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.
the class InitializationDelegate method initDeleteAssetsOfDisabledPlayers.
private static void initDeleteAssetsOfDisabledPlayers(final IDelegateBridge bridge) {
final GameData data = bridge.getData();
if (!Properties.getDisabledPlayersAssetsDeleted(data)) {
return;
}
for (final PlayerID player : data.getPlayerList().getPlayers()) {
if (player.isNull() || !player.getIsDisabled()) {
continue;
}
// delete all the stuff they have
final CompositeChange change = new CompositeChange();
for (final Resource r : player.getResources().getResourcesCopy().keySet()) {
final int deleted = player.getResources().getQuantity(r);
if (deleted != 0) {
change.add(ChangeFactory.changeResourcesChange(player, r, -deleted));
}
}
final Collection<Unit> heldUnits = player.getUnits().getUnits();
if (!heldUnits.isEmpty()) {
change.add(ChangeFactory.removeUnits(player, heldUnits));
}
final Predicate<Unit> owned = Matches.unitIsOwnedBy(player);
for (final Territory t : data.getMap().getTerritories()) {
final Collection<Unit> terrUnits = t.getUnits().getMatches(owned);
if (!terrUnits.isEmpty()) {
change.add(ChangeFactory.removeUnits(t, terrUnits));
}
}
if (!change.isEmpty()) {
bridge.getHistoryWriter().startEvent("Remove all resources and units from: " + player.getName());
bridge.addChange(change);
}
}
}
Aggregations