Search in sources :

Example 61 with Territory

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

the class GameDataExporter method connections.

private void connections(final GameData data) {
    xmlfile.append("        <!-- Territory Connections -->\n");
    final GameMap map = data.getMap();
    final List<Connection> reverseConnectionTracker = new ArrayList<>();
    for (final Territory ter : map.getTerritories()) {
        for (final Territory nb : map.getNeighbors(ter)) {
            if (!reverseConnectionTracker.contains(new Connection(ter, nb))) {
                xmlfile.append("        <connection t1=\"").append(ter.getName()).append("\" t2=\"").append(nb.getName()).append("\"/>\n");
                reverseConnectionTracker.add(new Connection(nb, ter));
            }
        }
    }
}
Also used : Territory(games.strategy.engine.data.Territory) GameMap(games.strategy.engine.data.GameMap) ArrayList(java.util.ArrayList)

Example 62 with Territory

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

the class ProTerritoryValueUtils method findTerritoryValues.

public static Map<Territory, Double> findTerritoryValues(final PlayerID player, final List<Territory> territoriesThatCantBeHeld, final List<Territory> territoriesToAttack, final Set<Territory> territoriesToCheck) {
    final int maxLandMassSize = findMaxLandMassSize(player);
    final Map<Territory, Double> enemyCapitalsAndFactoriesMap = findEnemyCapitalsAndFactoriesValue(player, maxLandMassSize, territoriesThatCantBeHeld, territoriesToAttack);
    final Map<Territory, Double> territoryValueMap = new HashMap<>();
    for (final Territory t : territoriesToCheck) {
        if (!t.isWater()) {
            final double value = findLandValue(t, player, maxLandMassSize, enemyCapitalsAndFactoriesMap, territoriesThatCantBeHeld, territoriesToAttack);
            territoryValueMap.put(t, value);
        }
    }
    for (final Territory t : territoriesToCheck) {
        if (t.isWater()) {
            final double value = findWaterValue(t, player, maxLandMassSize, enemyCapitalsAndFactoriesMap, territoriesThatCantBeHeld, territoriesToAttack, territoryValueMap);
            territoryValueMap.put(t, value);
        }
    }
    return territoryValueMap;
}
Also used : Territory(games.strategy.engine.data.Territory) HashMap(java.util.HashMap)

Example 63 with Territory

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

Example 64 with Territory

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

the class ProTerritoryValueUtils method findLandValue.

private static double findLandValue(final Territory t, final PlayerID player, final int maxLandMassSize, final Map<Territory, Double> enemyCapitalsAndFactoriesMap, final List<Territory> territoriesThatCantBeHeld, final List<Territory> territoriesToAttack) {
    if (territoriesThatCantBeHeld.contains(t)) {
        return 0.0;
    }
    // Determine value based on enemy factory land distance
    final List<Double> values = new ArrayList<>();
    final GameData data = ProData.getData();
    final Set<Territory> nearbyEnemyCapitalsAndFactories = findNearbyEnemyCapitalsAndFactories(t, enemyCapitalsAndFactoriesMap);
    for (final Territory enemyCapitalOrFactory : nearbyEnemyCapitalsAndFactories) {
        final int distance = data.getMap().getDistance(t, enemyCapitalOrFactory, ProMatches.territoryCanPotentiallyMoveLandUnits(player, data));
        if (distance > 0) {
            values.add(enemyCapitalsAndFactoriesMap.get(enemyCapitalOrFactory) / Math.pow(2, distance));
        }
    }
    values.sort(Collections.reverseOrder());
    double capitalOrFactoryValue = 0;
    for (int i = 0; i < values.size(); i++) {
        // Decrease each additional factory value by half
        capitalOrFactoryValue += values.get(i) / Math.pow(2, i);
    }
    // Determine value based on nearby territory production
    double nearbyEnemyValue = 0;
    final Set<Territory> nearbyTerritories = data.getMap().getNeighbors(t, 2, ProMatches.territoryCanPotentiallyMoveLandUnits(player, data));
    final List<Territory> nearbyEnemyTerritories = CollectionUtils.getMatches(nearbyTerritories, ProMatches.territoryIsEnemyOrCantBeHeld(player, data, territoriesThatCantBeHeld));
    nearbyEnemyTerritories.removeAll(territoriesToAttack);
    for (final Territory nearbyEnemyTerritory : nearbyEnemyTerritories) {
        final int distance = data.getMap().getDistance(t, nearbyEnemyTerritory, ProMatches.territoryCanPotentiallyMoveLandUnits(player, data));
        if (distance > 0) {
            double value = TerritoryAttachment.getProduction(nearbyEnemyTerritory);
            if (nearbyEnemyTerritory.getOwner().isNull()) {
                // find neutral value
                value = findTerritoryAttackValue(player, nearbyEnemyTerritory) / 3;
            } else if (ProMatches.territoryIsAlliedLandAndHasNoEnemyNeighbors(player, data).test(nearbyEnemyTerritory)) {
                // reduce value for can't hold amphib allied territories
                value *= 0.1;
            }
            if (value > 0) {
                nearbyEnemyValue += (value / Math.pow(2, distance));
            }
        }
    }
    final int landMassSize = 1 + data.getMap().getNeighbors(t, 6, ProMatches.territoryCanPotentiallyMoveLandUnits(player, data)).size();
    double value = nearbyEnemyValue * landMassSize / maxLandMassSize + capitalOrFactoryValue;
    if (ProMatches.territoryHasInfraFactoryAndIsLand().test(t)) {
        // prefer territories with factories
        value *= 1.1;
    }
    return value;
}
Also used : Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList)

Example 65 with Territory

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

the class ProTransportUtils method getUnusedLocalCarrierCapacity.

public static int getUnusedLocalCarrierCapacity(final PlayerID player, final Territory t, final List<Unit> unitsToPlace) {
    final GameData data = ProData.getData();
    // Find nearby carrier capacity
    final Set<Territory> nearbyTerritories = data.getMap().getNeighbors(t, 2, ProMatches.territoryCanMoveAirUnits(player, data, false));
    nearbyTerritories.add(t);
    final List<Unit> ownedNearbyUnits = new ArrayList<>();
    int capacity = 0;
    for (final Territory nearbyTerritory : nearbyTerritories) {
        final List<Unit> units = nearbyTerritory.getUnits().getMatches(Matches.unitIsOwnedBy(player));
        if (nearbyTerritory.equals(t)) {
            units.addAll(unitsToPlace);
        }
        ownedNearbyUnits.addAll(units);
        capacity += AirMovementValidator.carrierCapacity(units, t);
    }
    // Find nearby air unit carrier cost
    final Collection<Unit> airUnits = CollectionUtils.getMatches(ownedNearbyUnits, ProMatches.unitIsOwnedAir(player));
    for (final Unit airUnit : airUnits) {
        final UnitAttachment ua = UnitAttachment.get(airUnit.getType());
        final int cost = ua.getCarrierCost();
        if (cost != -1) {
            capacity -= cost;
        }
    }
    return capacity;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) Territory(games.strategy.engine.data.Territory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

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