Search in sources :

Example 6 with TerritoryAttachment

use of games.strategy.triplea.attachments.TerritoryAttachment 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 7 with TerritoryAttachment

use of games.strategy.triplea.attachments.TerritoryAttachment in project triplea by triplea-game.

the class EndRoundDelegate method checkVictoryCities.

private void checkVictoryCities(final IDelegateBridge bridge, final String victoryMessage, final String victoryType) {
    final GameData data = bridge.getData();
    final Collection<Territory> territories = data.getMap().getTerritories();
    for (final String allianceName : data.getAllianceTracker().getAlliances()) {
        final int vcAmount = getVcAmount(data, allianceName, victoryType);
        final Set<PlayerID> teamMembers = data.getAllianceTracker().getPlayersInAlliance(allianceName);
        int teamVCs = 0;
        for (final Territory t : territories) {
            if (Matches.isTerritoryOwnedBy(teamMembers).test(t)) {
                final TerritoryAttachment ta = TerritoryAttachment.get(t);
                if (ta != null) {
                    teamVCs += ta.getVictoryCity();
                }
            }
        }
        if (teamVCs >= vcAmount) {
            bridge.getHistoryWriter().startEvent(allianceName + victoryMessage + vcAmount + " Victory Cities!");
            final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance(allianceName);
            // Added this to end the game on victory conditions
            signalGameOver(allianceName + victoryMessage + vcAmount + " Victory Cities!", winners, bridge);
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment)

Example 8 with TerritoryAttachment

use of games.strategy.triplea.attachments.TerritoryAttachment 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)

Example 9 with TerritoryAttachment

use of games.strategy.triplea.attachments.TerritoryAttachment in project triplea by triplea-game.

the class GameDataExporter method printAttachmentOptionsBasedOnOriginalXml.

private static String printAttachmentOptionsBasedOnOriginalXml(final ArrayList<Tuple<String, String>> attachmentPlusValues, final IAttachment attachment) {
    if (attachmentPlusValues.isEmpty()) {
        return "";
    }
    final StringBuilder sb = new StringBuilder();
    boolean alreadyHasOccupiedTerrOf = false;
    for (final Tuple<String, String> current : attachmentPlusValues) {
        sb.append("            <option name=\"").append(current.getFirst()).append("\" value=\"").append(current.getSecond()).append("\"/>\n");
        if (current.getFirst().equals("occupiedTerrOf")) {
            alreadyHasOccupiedTerrOf = true;
        }
    }
    // add occupiedTerrOf until we fix engine to only use originalOwner
    if (!alreadyHasOccupiedTerrOf && attachment instanceof TerritoryAttachment) {
        final TerritoryAttachment ta = (TerritoryAttachment) attachment;
        if (ta.getOriginalOwner() != null) {
            sb.append("            <option name=\"occupiedTerrOf\" value=\"").append(ta.getOriginalOwner().getName()).append("\"/>\n");
        }
    }
    return sb.toString();
}
Also used : TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment)

Example 10 with TerritoryAttachment

use of games.strategy.triplea.attachments.TerritoryAttachment in project triplea by triplea-game.

the class ProTerritoryValueUtils method findEnemyCapitalsAndFactoriesValue.

private static Map<Territory, Double> findEnemyCapitalsAndFactoriesValue(final PlayerID player, final int maxLandMassSize, final List<Territory> territoriesThatCantBeHeld, final List<Territory> territoriesToAttack) {
    // Get all enemy factories and capitals (check if most territories have factories and if so remove them)
    final GameData data = ProData.getData();
    final List<Territory> allTerritories = data.getMap().getTerritories();
    final Set<Territory> enemyCapitalsAndFactories = new HashSet<>(CollectionUtils.getMatches(allTerritories, ProMatches.territoryHasInfraFactoryAndIsOwnedByPlayersOrCantBeHeld(player, ProUtils.getPotentialEnemyPlayers(player), territoriesThatCantBeHeld)));
    final int numPotentialEnemyTerritories = CollectionUtils.countMatches(allTerritories, Matches.isTerritoryOwnedBy(ProUtils.getPotentialEnemyPlayers(player)));
    if (enemyCapitalsAndFactories.size() * 2 >= numPotentialEnemyTerritories) {
        enemyCapitalsAndFactories.clear();
    }
    enemyCapitalsAndFactories.addAll(ProUtils.getLiveEnemyCapitals(data, player));
    enemyCapitalsAndFactories.removeAll(territoriesToAttack);
    // Find value for each enemy capital and factory
    final Map<Territory, Double> enemyCapitalsAndFactoriesMap = new HashMap<>();
    for (final Territory t : enemyCapitalsAndFactories) {
        // Get factory production if factory
        int factoryProduction = 0;
        if (ProMatches.territoryHasInfraFactoryAndIsLand().test(t)) {
            factoryProduction = TerritoryAttachment.getProduction(t);
        }
        // Get player production if capital
        double playerProduction = 0;
        final TerritoryAttachment ta = TerritoryAttachment.get(t);
        if (ta != null && ta.isCapital()) {
            playerProduction = ProUtils.getPlayerProduction(t.getOwner(), data);
        }
        // Calculate value
        final int isNeutral = t.getOwner().isNull() ? 1 : 0;
        final int landMassSize = 1 + data.getMap().getNeighbors(t, 6, ProMatches.territoryCanPotentiallyMoveLandUnits(player, data)).size();
        final double value = Math.sqrt(factoryProduction + Math.sqrt(playerProduction)) * 32 / (1 + 3 * isNeutral) * landMassSize / maxLandMassSize;
        enemyCapitalsAndFactoriesMap.put(t, value);
    }
    return enemyCapitalsAndFactoriesMap;
}
Also used : Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) HashMap(java.util.HashMap) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) HashSet(java.util.HashSet)

Aggregations

TerritoryAttachment (games.strategy.triplea.attachments.TerritoryAttachment)36 Territory (games.strategy.engine.data.Territory)29 Unit (games.strategy.engine.data.Unit)24 TripleAUnit (games.strategy.triplea.TripleAUnit)19 ArrayList (java.util.ArrayList)14 PlayerID (games.strategy.engine.data.PlayerID)13 GameData (games.strategy.engine.data.GameData)9 HashSet (java.util.HashSet)8 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)7 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)6 UnitType (games.strategy.engine.data.UnitType)5 ProBattleResult (games.strategy.triplea.ai.pro.data.ProBattleResult)5 CompositeChange (games.strategy.engine.data.CompositeChange)4 Resource (games.strategy.engine.data.Resource)4 ProOtherMoveOptions (games.strategy.triplea.ai.pro.data.ProOtherMoveOptions)4 PlayerAttachment (games.strategy.triplea.attachments.PlayerAttachment)4 IntegerMap (games.strategy.util.IntegerMap)4 RelationshipTracker (games.strategy.engine.data.RelationshipTracker)3 ProPlaceTerritory (games.strategy.triplea.ai.pro.data.ProPlaceTerritory)3 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)3