Search in sources :

Example 26 with CompositeChange

use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.

the class MustFightBattle method clearTransportedByForAlliedAirOnCarrier.

static CompositeChange clearTransportedByForAlliedAirOnCarrier(final Collection<Unit> attackingUnits, final Territory battleSite, final PlayerID attacker, final GameData data) {
    final CompositeChange change = new CompositeChange();
    // Clear the transported_by for successfully won battles where there was an allied air unit held as cargo by an
    // carrier unit
    final Collection<Unit> carriers = CollectionUtils.getMatches(attackingUnits, Matches.unitIsCarrier());
    if (!carriers.isEmpty() && !Properties.getAlliedAirIndependent(data)) {
        final Predicate<Unit> alliedFighters = Matches.isUnitAllied(attacker, data).and(Matches.unitIsOwnedBy(attacker).negate()).and(Matches.unitIsAir()).and(Matches.unitCanLandOnCarrier());
        final Collection<Unit> alliedAirInTerr = CollectionUtils.getMatches(Sets.union(Sets.newHashSet(attackingUnits), Sets.newHashSet(battleSite.getUnits())), alliedFighters);
        for (final Unit fighter : alliedAirInTerr) {
            final TripleAUnit taUnit = (TripleAUnit) fighter;
            if (taUnit.getTransportedBy() != null) {
                final Unit carrierTransportingThisUnit = taUnit.getTransportedBy();
                if (!Matches.unitHasWhenCombatDamagedEffect(UnitAttachment.UNITSMAYNOTLEAVEALLIEDCARRIER).test(carrierTransportingThisUnit)) {
                    change.add(ChangeFactory.unitPropertyChange(fighter, null, TripleAUnit.TRANSPORTED_BY));
                }
            }
        }
    }
    return change;
}
Also used : CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit)

Example 27 with CompositeChange

use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.

the class AbstractEndTurnDelegate method start.

@Override
public void start() {
    // figure out our current PUs before we do anything else, including super methods
    final GameData data = bridge.getData();
    final Resource pus = data.getResourceList().getResource(Constants.PUS);
    final int leftOverPUs = bridge.getPlayerId().getResources().getQuantity(pus);
    final IntegerMap<Resource> leftOverResources = bridge.getPlayerId().getResources().getResourcesCopy();
    super.start();
    if (!needToInitialize) {
        return;
    }
    final StringBuilder endTurnReport = new StringBuilder();
    hasPostedTurnSummary = false;
    final PlayerAttachment pa = PlayerAttachment.get(player);
    // can't collect unless you own your own capital
    if (!canPlayerCollectIncome(player, data)) {
        endTurnReport.append(rollWarBondsForFriends(bridge, player, data));
    // we do not collect any income this turn
    } else {
        // just collect resources
        final Collection<Territory> territories = data.getMap().getTerritoriesOwnedBy(player);
        int toAdd = getProduction(territories);
        final int blockadeLoss = getBlockadeProductionLoss(player, data, bridge, endTurnReport);
        toAdd -= blockadeLoss;
        toAdd *= Properties.getPuMultiplier(data);
        int total = player.getResources().getQuantity(pus) + toAdd;
        final String transcriptText;
        if (blockadeLoss == 0) {
            transcriptText = player.getName() + " collect " + toAdd + MyFormatter.pluralize(" PU", toAdd) + "; end with " + total + MyFormatter.pluralize(" PU", total);
        } else {
            transcriptText = player.getName() + " collect " + toAdd + MyFormatter.pluralize(" PU", toAdd) + " (" + blockadeLoss + " lost to blockades)" + "; end with " + total + MyFormatter.pluralize(" PU", total);
        }
        bridge.getHistoryWriter().startEvent(transcriptText);
        endTurnReport.append(transcriptText).append("<br />");
        // do war bonds
        final int bonds = rollWarBonds(bridge, player, data);
        if (bonds > 0) {
            total += bonds;
            toAdd += bonds;
            final String bondText = player.getName() + " collect " + bonds + MyFormatter.pluralize(" PU", bonds) + " from War Bonds; end with " + total + MyFormatter.pluralize(" PU", total);
            bridge.getHistoryWriter().startEvent(bondText);
            endTurnReport.append("<br />").append(bondText).append("<br />");
        }
        if (total < 0) {
            toAdd -= total;
        }
        final Change change = ChangeFactory.changeResourcesChange(player, pus, toAdd);
        bridge.addChange(change);
        if (data.getProperties().get(Constants.PACIFIC_THEATER, false) && pa != null) {
            final Change changeVp = (ChangeFactory.attachmentPropertyChange(pa, (pa.getVps() + (toAdd / 10) + (pa.getCaptureVps() / 10)), "vps"));
            final Change changeCaptureVp = ChangeFactory.attachmentPropertyChange(pa, "0", "captureVps");
            final CompositeChange ccVp = new CompositeChange(changeVp, changeCaptureVp);
            bridge.addChange(ccVp);
        }
        endTurnReport.append("<br />").append(addOtherResources(bridge));
        endTurnReport.append("<br />").append(doNationalObjectivesAndOtherEndTurnEffects(bridge));
        final IntegerMap<Resource> income = player.getResources().getResourcesCopy();
        income.subtract(leftOverResources);
        endTurnReport.append("<br />").append(BonusIncomeUtils.addBonusIncome(income, bridge, player));
        // now we do upkeep costs, including upkeep cost as a percentage of our entire income for this turn (including
        // NOs)
        final int currentPUs = player.getResources().getQuantity(pus);
        int relationshipUpkeepCostFlat = 0;
        int relationshipUpkeepCostPercentage = 0;
        for (final Relationship r : data.getRelationshipTracker().getRelationships(player)) {
            final String[] upkeep = r.getRelationshipType().getRelationshipTypeAttachment().getUpkeepCost().split(":");
            if (upkeep.length == 1 || upkeep[1].equals(RelationshipTypeAttachment.UPKEEP_FLAT)) {
                relationshipUpkeepCostFlat += Integer.parseInt(upkeep[0]);
            } else if (upkeep[1].equals(RelationshipTypeAttachment.UPKEEP_PERCENTAGE)) {
                relationshipUpkeepCostPercentage += Integer.parseInt(upkeep[0]);
            }
        }
        relationshipUpkeepCostPercentage = Math.min(100, relationshipUpkeepCostPercentage);
        int relationshipUpkeepTotalCost = 0;
        if (relationshipUpkeepCostPercentage != 0) {
            final float gainedPus = Math.max(0, currentPUs - leftOverPUs);
            relationshipUpkeepTotalCost += Math.round(gainedPus * (relationshipUpkeepCostPercentage) / 100f);
        }
        if (relationshipUpkeepCostFlat != 0) {
            relationshipUpkeepTotalCost += relationshipUpkeepCostFlat;
        }
        // we can't remove more than we have, and we also must flip the sign
        relationshipUpkeepTotalCost = Math.min(currentPUs, relationshipUpkeepTotalCost);
        relationshipUpkeepTotalCost = -1 * relationshipUpkeepTotalCost;
        if (relationshipUpkeepTotalCost != 0) {
            final int newTotal = currentPUs + relationshipUpkeepTotalCost;
            final String transcriptText2 = player.getName() + (relationshipUpkeepTotalCost < 0 ? " pays " : " taxes ") + (-1 * relationshipUpkeepTotalCost) + MyFormatter.pluralize(" PU", relationshipUpkeepTotalCost) + " in order to maintain current relationships with other players, and ends the turn with " + newTotal + MyFormatter.pluralize(" PU", newTotal);
            bridge.getHistoryWriter().startEvent(transcriptText2);
            endTurnReport.append("<br />").append(transcriptText2).append("<br />");
            final Change upkeep = ChangeFactory.changeResourcesChange(player, pus, relationshipUpkeepTotalCost);
            bridge.addChange(upkeep);
        }
    }
    if (GameStepPropertiesHelper.isRepairUnits(data)) {
        MoveDelegate.repairMultipleHitPointUnits(bridge, bridge.getPlayerId());
    }
    if (isGiveUnitsByTerritory() && pa != null && pa.getGiveUnitControl() != null && !pa.getGiveUnitControl().isEmpty()) {
        changeUnitOwnership(bridge);
    }
    needToInitialize = false;
    showEndTurnReport(endTurnReport.toString());
}
Also used : Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) Resource(games.strategy.engine.data.Resource) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) PlayerAttachment(games.strategy.triplea.attachments.PlayerAttachment) Relationship(games.strategy.engine.data.RelationshipTracker.Relationship) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 28 with CompositeChange

use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.

the class EndTurnDelegate method createUnits.

private String createUnits(final IDelegateBridge bridge) {
    final StringBuilder endTurnReport = new StringBuilder();
    final GameData data = getData();
    final PlayerID player = data.getSequence().getStep().getPlayerId();
    final Predicate<Unit> myCreatorsMatch = Matches.unitIsOwnedBy(player).and(Matches.unitCreatesUnits());
    final CompositeChange change = new CompositeChange();
    for (final Territory t : data.getMap().getTerritories()) {
        final Collection<Unit> myCreators = CollectionUtils.getMatches(t.getUnits().getUnits(), myCreatorsMatch);
        if (myCreators != null && !myCreators.isEmpty()) {
            final Collection<Unit> toAdd = new ArrayList<>();
            final Collection<Unit> toAddSea = new ArrayList<>();
            final Collection<Unit> toAddLand = new ArrayList<>();
            for (final Unit u : myCreators) {
                final UnitAttachment ua = UnitAttachment.get(u.getType());
                final IntegerMap<UnitType> createsUnitsMap = ua.getCreatesUnitsList();
                final Collection<UnitType> willBeCreated = createsUnitsMap.keySet();
                for (final UnitType ut : willBeCreated) {
                    if (UnitAttachment.get(ut).getIsSea() && Matches.territoryIsLand().test(t)) {
                        toAddSea.addAll(ut.create(createsUnitsMap.getInt(ut), player));
                    } else if (!UnitAttachment.get(ut).getIsSea() && !UnitAttachment.get(ut).getIsAir() && Matches.territoryIsWater().test(t)) {
                        toAddLand.addAll(ut.create(createsUnitsMap.getInt(ut), player));
                    } else {
                        toAdd.addAll(ut.create(createsUnitsMap.getInt(ut), player));
                    }
                }
            }
            if (!toAdd.isEmpty()) {
                final String transcriptText = player.getName() + " creates " + MyFormatter.unitsToTextNoOwner(toAdd) + " in " + t.getName();
                bridge.getHistoryWriter().startEvent(transcriptText, toAdd);
                endTurnReport.append(transcriptText).append("<br />");
                final Change place = ChangeFactory.addUnits(t, toAdd);
                change.add(place);
            }
            if (!toAddSea.isEmpty()) {
                final Predicate<Territory> myTerrs = Matches.territoryIsWater();
                final Collection<Territory> waterNeighbors = data.getMap().getNeighbors(t, myTerrs);
                if (waterNeighbors != null && !waterNeighbors.isEmpty()) {
                    final Territory tw = getRandomTerritory(waterNeighbors, bridge);
                    final String transcriptText = player.getName() + " creates " + MyFormatter.unitsToTextNoOwner(toAddSea) + " in " + tw.getName();
                    bridge.getHistoryWriter().startEvent(transcriptText, toAddSea);
                    endTurnReport.append(transcriptText).append("<br />");
                    final Change place = ChangeFactory.addUnits(tw, toAddSea);
                    change.add(place);
                }
            }
            if (!toAddLand.isEmpty()) {
                final Predicate<Territory> myTerrs = Matches.isTerritoryOwnedBy(player).and(Matches.territoryIsLand());
                final Collection<Territory> landNeighbors = data.getMap().getNeighbors(t, myTerrs);
                if (landNeighbors != null && !landNeighbors.isEmpty()) {
                    final Territory tl = getRandomTerritory(landNeighbors, bridge);
                    final String transcriptText = player.getName() + " creates " + MyFormatter.unitsToTextNoOwner(toAddLand) + " in " + tl.getName();
                    bridge.getHistoryWriter().startEvent(transcriptText, toAddLand);
                    endTurnReport.append(transcriptText).append("<br />");
                    final Change place = ChangeFactory.addUnits(tl, toAddLand);
                    change.add(place);
                }
            }
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
    return endTurnReport.toString();
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) Unit(games.strategy.engine.data.Unit) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) UnitType(games.strategy.engine.data.UnitType) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 29 with CompositeChange

use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.

the class EndTurnDelegate method addOtherResources.

@Override
protected String addOtherResources(final IDelegateBridge bridge) {
    final StringBuilder endTurnReport = new StringBuilder();
    final GameData data = bridge.getData();
    final CompositeChange change = new CompositeChange();
    final Collection<Territory> territories = data.getMap().getTerritoriesOwnedBy(player);
    final IntegerMap<Resource> production = getResourceProduction(territories, data);
    for (final Entry<Resource, Integer> resource : production.entrySet()) {
        final Resource r = resource.getKey();
        int toAdd = resource.getValue();
        int total = player.getResources().getQuantity(r) + toAdd;
        if (total < 0) {
            toAdd -= total;
            total = 0;
        }
        final String resourceText = player.getName() + " collects " + toAdd + " " + MyFormatter.pluralize(r.getName(), toAdd) + "; ends with " + total + " " + MyFormatter.pluralize(r.getName(), total) + " total";
        bridge.getHistoryWriter().startEvent(resourceText);
        endTurnReport.append(resourceText).append("<br />");
        change.add(ChangeFactory.changeResourcesChange(player, r, toAdd));
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
    return endTurnReport.toString();
}
Also used : Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) Resource(games.strategy.engine.data.Resource) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 30 with CompositeChange

use of games.strategy.engine.data.CompositeChange in project triplea by triplea-game.

the class InitializationDelegate method initOriginalOwner.

private static void initOriginalOwner(final IDelegateBridge bridge) {
    final GameData data = bridge.getData();
    final CompositeChange changes = new CompositeChange();
    for (final Territory current : data.getMap()) {
        if (!current.getOwner().isNull()) {
            final TerritoryAttachment territoryAttachment = TerritoryAttachment.get(current);
            if (territoryAttachment == null) {
                throw new IllegalStateException("No territory attachment for " + current);
            }
            if (territoryAttachment.getOriginalOwner() == null && current.getOwner() != null) {
                changes.add(OriginalOwnerTracker.addOriginalOwnerChange(current, current.getOwner()));
            }
            final Collection<Unit> factoryAndInfrastructure = current.getUnits().getMatches(Matches.unitIsInfrastructure());
            changes.add(OriginalOwnerTracker.addOriginalOwnerChange(factoryAndInfrastructure, current.getOwner()));
        } else if (!current.isWater()) {
            final TerritoryAttachment territoryAttachment = TerritoryAttachment.get(current);
            if (territoryAttachment == null) {
                throw new IllegalStateException("No territory attachment for " + current);
            }
        }
    }
    bridge.getHistoryWriter().startEvent("Adding original owners");
    bridge.addChange(changes);
}
Also used : Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) 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