Search in sources :

Example 56 with CompositeChange

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();
}
Also used : Resource(games.strategy.engine.data.Resource) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change)

Example 57 with CompositeChange

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;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Resource(games.strategy.engine.data.Resource) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) HashSet(java.util.HashSet)

Example 58 with CompositeChange

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;
        }
    }
}
Also used : DiceType(games.strategy.engine.random.IRandomStats.DiceType) IRemote(games.strategy.engine.message.IRemote) Properties(games.strategy.triplea.Properties) TuvUtils(games.strategy.triplea.util.TuvUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UnitType(games.strategy.engine.data.UnitType) CompositeChange(games.strategy.engine.data.CompositeChange) CollectionUtils(games.strategy.util.CollectionUtils) IntegerMap(games.strategy.util.IntegerMap) Unit(games.strategy.engine.data.Unit) Interruptibles(games.strategy.util.Interruptibles) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Territory(games.strategy.engine.data.Territory) Serializable(java.io.Serializable) GameData(games.strategy.engine.data.GameData) ChangeFactory(games.strategy.engine.data.changefactory.ChangeFactory) List(java.util.List) Tuple(games.strategy.util.Tuple) PlayerID(games.strategy.engine.data.PlayerID) MyFormatter(games.strategy.triplea.formatter.MyFormatter) MapSupport(games.strategy.triplea.MapSupport) Comparator(java.util.Comparator) PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit) UnitType(games.strategy.engine.data.UnitType) CompositeChange(games.strategy.engine.data.CompositeChange) HashSet(java.util.HashSet)

Example 59 with CompositeChange

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();
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) Resource(games.strategy.engine.data.Resource) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 60 with CompositeChange

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);
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) Resource(games.strategy.engine.data.Resource) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Aggregations

CompositeChange (games.strategy.engine.data.CompositeChange)74 Unit (games.strategy.engine.data.Unit)48 TripleAUnit (games.strategy.triplea.TripleAUnit)40 GameData (games.strategy.engine.data.GameData)31 Territory (games.strategy.engine.data.Territory)25 ArrayList (java.util.ArrayList)22 PlayerID (games.strategy.engine.data.PlayerID)21 Change (games.strategy.engine.data.Change)17 UnitType (games.strategy.engine.data.UnitType)14 Resource (games.strategy.engine.data.Resource)9 HashSet (java.util.HashSet)9 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)8 IntegerMap (games.strategy.util.IntegerMap)8 Tuple (games.strategy.util.Tuple)7 Collection (java.util.Collection)7 RelationshipType (games.strategy.engine.data.RelationshipType)6 Set (java.util.Set)6 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)4 ProductionRule (games.strategy.engine.data.ProductionRule)4 HashMap (java.util.HashMap)4