Search in sources :

Example 51 with Territory

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

the class AbstractEndTurnDelegate method findEstimatedIncome.

/**
 * Find estimated income for given player. This only takes into account income from territories,
 * units, NOs, and triggers. It ignores blockades, war bonds, relationship upkeep, and bonus income.
 */
public static IntegerMap<Resource> findEstimatedIncome(final PlayerID player, final GameData data) {
    final IntegerMap<Resource> resources = new IntegerMap<>();
    // Only add territory resources if endTurn not endTurnNoPU
    for (GameStep step : data.getSequence()) {
        if (player.equals(step.getPlayerId()) && step.getDelegate().getName().equals("endTurn")) {
            final List<Territory> territories = data.getMap().getTerritoriesOwnedBy(player);
            final int pusFromTerritories = getProduction(territories, data) * Properties.getPuMultiplier(data);
            resources.add(new Resource(Constants.PUS, data), pusFromTerritories);
            resources.add(EndTurnDelegate.getResourceProduction(territories, data));
        }
    }
    // Add unit generated resources, NOs, and triggers
    resources.add(EndTurnDelegate.findUnitCreatedResources(player, data));
    resources.add(EndTurnDelegate.findNationalObjectiveAndTriggerResources(player, data));
    return resources;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) Resource(games.strategy.engine.data.Resource) GameStep(games.strategy.engine.data.GameStep)

Example 52 with Territory

use of games.strategy.engine.data.Territory 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 53 with Territory

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

the class EndTurnDelegate method findUnitCreatedResources.

/**
 * Find all of the resources that will be created by units on the map.
 */
public static IntegerMap<Resource> findUnitCreatedResources(final PlayerID player, final GameData data) {
    final IntegerMap<Resource> resourceTotalsMap = new IntegerMap<>();
    final Predicate<Unit> myCreatorsMatch = Matches.unitIsOwnedBy(player).and(Matches.unitCreatesResources());
    for (final Territory t : data.getMap().getTerritories()) {
        final Collection<Unit> myCreators = CollectionUtils.getMatches(t.getUnits().getUnits(), myCreatorsMatch);
        for (final Unit unit : myCreators) {
            final IntegerMap<Resource> generatedResourcesMap = UnitAttachment.get(unit.getType()).getCreatesResourcesList();
            resourceTotalsMap.add(generatedResourcesMap);
        }
    }
    final Resource pus = new Resource(Constants.PUS, data);
    if (resourceTotalsMap.containsKey(pus)) {
        resourceTotalsMap.put(pus, resourceTotalsMap.getInt(pus) * Properties.getPuMultiplier(data));
    }
    return resourceTotalsMap;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) Resource(games.strategy.engine.data.Resource) Unit(games.strategy.engine.data.Unit)

Example 54 with Territory

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

the class EndTurnDelegate method getResourceProduction.

/**
 * Since territory resource may contain any resource except PUs (PUs use "getProduction" instead),
 * we will now figure out the total production of non-PUs resources.
 */
public static IntegerMap<Resource> getResourceProduction(final Collection<Territory> territories, final GameData data) {
    final IntegerMap<Resource> resources = new IntegerMap<>();
    for (final Territory current : territories) {
        final TerritoryAttachment attachment = TerritoryAttachment.get(current);
        if (attachment == null) {
            throw new IllegalStateException("No attachment for owned territory:" + current.getName());
        }
        final ResourceCollection toAdd = attachment.getResources();
        if (toAdd == null) {
            continue;
        }
        // Match will check if territory is originally owned convoy center, or if contested
        if (Matches.territoryCanCollectIncomeFrom(current.getOwner(), data).test(current)) {
            resources.add(toAdd.getResourcesCopy());
        }
    }
    return resources;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) Resource(games.strategy.engine.data.Resource) ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 55 with Territory

use of games.strategy.engine.data.Territory 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)

Aggregations

Territory (games.strategy.engine.data.Territory)420 Unit (games.strategy.engine.data.Unit)254 TripleAUnit (games.strategy.triplea.TripleAUnit)195 PlayerID (games.strategy.engine.data.PlayerID)164 ArrayList (java.util.ArrayList)160 Test (org.junit.jupiter.api.Test)140 Route (games.strategy.engine.data.Route)137 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)118 GameData (games.strategy.engine.data.GameData)94 HashSet (java.util.HashSet)87 UnitType (games.strategy.engine.data.UnitType)65 HashMap (java.util.HashMap)65 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)50 Collection (java.util.Collection)47 IntegerMap (games.strategy.util.IntegerMap)45 Set (java.util.Set)41 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)39 TerritoryAttachment (games.strategy.triplea.attachments.TerritoryAttachment)37 List (java.util.List)36 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)34