Search in sources :

Example 6 with GameMap

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

the class ParserTest method testTerritoriesCreated.

@Test
public void testTerritoriesCreated() {
    final GameMap map = gameData.getMap();
    final Collection<Territory> territories = map.getTerritories();
    assertEquals(3, territories.size());
}
Also used : Territory(games.strategy.engine.data.Territory) GameMap(games.strategy.engine.data.GameMap) Test(org.junit.jupiter.api.Test)

Example 7 with GameMap

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

the class GameDataExporter method map.

private void map(final GameData data) {
    xmlfile.append("\n");
    xmlfile.append("    <map>\n");
    xmlfile.append("        <!-- Territory Definitions -->\n");
    final GameMap map = data.getMap();
    for (final Territory ter : map.getTerritories()) {
        xmlfile.append("        <territory name=\"").append(ter.getName()).append("\"");
        if (ter.isWater()) {
            xmlfile.append(" water=\"true\"");
        }
        xmlfile.append("/>\n");
    }
    connections(data);
    xmlfile.append("    </map>\n");
}
Also used : Territory(games.strategy.engine.data.Territory) GameMap(games.strategy.engine.data.GameMap)

Example 8 with GameMap

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

the class AbstractRulesAttachment method getTerritoriesBasedOnStringName.

/**
 * Takes a string like "original", "originalNoWater", "enemy", "controlled", "controlledNoWater", "all", "map", and
 * turns it into an
 * actual list of territories.
 * Also sets territoryCount.
 */
protected Set<Territory> getTerritoriesBasedOnStringName(final String name, final Collection<PlayerID> players, final GameData data) {
    final GameMap gameMap = data.getMap();
    if (name.equals("original") || name.equals("enemy")) {
        // get all originally owned territories
        final Set<Territory> originalTerrs = new HashSet<>();
        for (final PlayerID player : players) {
            originalTerrs.addAll(OriginalOwnerTracker.getOriginallyOwned(data, player));
        }
        setTerritoryCount(String.valueOf(originalTerrs.size()));
        return originalTerrs;
    } else if (name.equals("originalNoWater")) {
        // get all originally owned territories, but no water or impassables
        final Set<Territory> originalTerrs = new HashSet<>();
        for (final PlayerID player : players) {
            originalTerrs.addAll(CollectionUtils.getMatches(OriginalOwnerTracker.getOriginallyOwned(data, player), // TODO: does this account for occupiedTerrOf???
            Matches.territoryIsNotImpassableToLandUnits(player, data)));
        }
        setTerritoryCount(String.valueOf(originalTerrs.size()));
        return originalTerrs;
    } else if (name.equals("controlled")) {
        final Set<Territory> ownedTerrs = new HashSet<>();
        for (final PlayerID player : players) {
            ownedTerrs.addAll(gameMap.getTerritoriesOwnedBy(player));
        }
        setTerritoryCount(String.valueOf(ownedTerrs.size()));
        return ownedTerrs;
    } else if (name.equals("controlledNoWater")) {
        final Set<Territory> ownedTerrsNoWater = new HashSet<>();
        for (final PlayerID player : players) {
            ownedTerrsNoWater.addAll(CollectionUtils.getMatches(gameMap.getTerritoriesOwnedBy(player), Matches.territoryIsNotImpassableToLandUnits(player, data)));
        }
        setTerritoryCount(String.valueOf(ownedTerrsNoWater.size()));
        return ownedTerrsNoWater;
    } else if (name.equals("all")) {
        final Set<Territory> allTerrs = new HashSet<>();
        for (final PlayerID player : players) {
            allTerrs.addAll(gameMap.getTerritoriesOwnedBy(player));
            allTerrs.addAll(OriginalOwnerTracker.getOriginallyOwned(data, player));
        }
        setTerritoryCount(String.valueOf(allTerrs.size()));
        return allTerrs;
    } else if (name.equals("map")) {
        final Set<Territory> allTerrs = new HashSet<>(gameMap.getTerritories());
        setTerritoryCount(String.valueOf(allTerrs.size()));
        return allTerrs;
    } else {
        // The list just contained 1 territory
        final Territory t = data.getMap().getTerritory(name);
        if (t == null) {
            throw new IllegalStateException("No territory called:" + name + thisErrorMsg());
        }
        final Set<Territory> terr = new HashSet<>();
        terr.add(t);
        setTerritoryCount(String.valueOf(1));
        return terr;
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) Set(java.util.Set) HashSet(java.util.HashSet) GameMap(games.strategy.engine.data.GameMap) HashSet(java.util.HashSet)

Example 9 with GameMap

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

the class AbstractEndTurnDelegate method getBlockadeProductionLoss.

// finds losses due to blockades, positive value returned.
private int getBlockadeProductionLoss(final PlayerID player, final GameData data, final IDelegateBridge bridge, final StringBuilder endTurnReport) {
    final PlayerAttachment playerRules = PlayerAttachment.get(player);
    if (playerRules != null && playerRules.getImmuneToBlockade()) {
        return 0;
    }
    final GameMap map = data.getMap();
    final Collection<Territory> blockable = CollectionUtils.getMatches(map.getTerritories(), Matches.territoryIsBlockadeZone());
    if (blockable.isEmpty()) {
        return 0;
    }
    final Predicate<Unit> enemyUnits = Matches.enemyUnit(player, data);
    int totalLoss = 0;
    final boolean rollDiceForBlockadeDamage = Properties.getConvoyBlockadesRollDiceForCost(data);
    final Collection<String> transcripts = new ArrayList<>();
    final HashMap<Territory, Tuple<Integer, List<Territory>>> damagePerBlockadeZone = new HashMap<>();
    boolean rolledDice = false;
    for (final Territory b : blockable) {
        // match will check for land, convoy zones, and also contested territories
        final List<Territory> viableNeighbors = CollectionUtils.getMatches(map.getNeighbors(b), Matches.isTerritoryOwnedBy(player).and(Matches.territoryCanCollectIncomeFrom(player, data)));
        final int maxLoss = getProduction(viableNeighbors);
        if (maxLoss <= 0) {
            continue;
        }
        final Collection<Unit> enemies = CollectionUtils.getMatches(b.getUnits().getUnits(), enemyUnits);
        if (enemies.isEmpty()) {
            continue;
        }
        int loss = 0;
        if (rollDiceForBlockadeDamage) {
            int numberOfDice = 0;
            for (final Unit u : enemies) {
                numberOfDice += UnitAttachment.get(u.getType()).getBlockade();
            }
            if (numberOfDice > 0) {
                // there is an issue with maps that have lots of rolls without any pause between them: they are causing the
                // cypted random source
                // (ie: live and pbem games) to lock up or error out
                // so we need to slow them down a bit, until we come up with a better solution (like aggregating all the
                // chances together, then
                // getting a ton of random numbers at once instead of one at a time)
                Interruptibles.sleep(100);
                final String transcript = "Rolling for Convoy Blockade Damage in " + b.getName();
                final int[] dice = bridge.getRandom(CONVOY_BLOCKADE_DICE_SIDES, numberOfDice, enemies.iterator().next().getOwner(), DiceType.BOMBING, transcript);
                transcripts.add(transcript + ". Rolls: " + MyFormatter.asDice(dice));
                rolledDice = true;
                for (final int d : dice) {
                    // we are zero based
                    final int roll = d + 1;
                    loss += (roll <= 3 ? roll : 0);
                }
            }
        } else {
            for (final Unit u : enemies) {
                loss += UnitAttachment.get(u.getType()).getBlockade();
            }
        }
        if (loss <= 0) {
            continue;
        }
        final int lossForBlockade = Math.min(maxLoss, loss);
        damagePerBlockadeZone.put(b, Tuple.of(lossForBlockade, viableNeighbors));
        totalLoss += lossForBlockade;
    }
    if (totalLoss <= 0 && !rolledDice) {
        return 0;
    }
    // now we need to make sure that we didn't deal more damage than the territories are worth, in the case of having
    // multiple sea zones
    // touching the same land zone.
    final List<Territory> blockadeZonesSorted = new ArrayList<>(damagePerBlockadeZone.keySet());
    blockadeZonesSorted.sort(getSingleBlockadeThenHighestToLowestBlockadeDamage(damagePerBlockadeZone));
    // we want to match highest damage to largest producer first, that is why we sort twice
    final IntegerMap<Territory> totalDamageTracker = new IntegerMap<>();
    for (final Territory b : blockadeZonesSorted) {
        final Tuple<Integer, List<Territory>> tuple = damagePerBlockadeZone.get(b);
        int damageForZone = tuple.getFirst();
        final List<Territory> terrsLosingIncome = new ArrayList<>(tuple.getSecond());
        terrsLosingIncome.sort(getSingleNeighborBlockadesThenHighestToLowestProduction(blockadeZonesSorted, map));
        final Iterator<Territory> iter = terrsLosingIncome.iterator();
        while (damageForZone > 0 && iter.hasNext()) {
            final Territory t = iter.next();
            final int maxProductionLessPreviousDamage = TerritoryAttachment.getProduction(t) - totalDamageTracker.getInt(t);
            final int damageToTerr = Math.min(damageForZone, maxProductionLessPreviousDamage);
            damageForZone -= damageToTerr;
            totalDamageTracker.put(t, damageToTerr + totalDamageTracker.getInt(t));
        }
    }
    final int realTotalLoss = Math.max(0, totalDamageTracker.totalValues());
    if (rollDiceForBlockadeDamage && (realTotalLoss > 0 || (rolledDice && !transcripts.isEmpty()))) {
        final String mainline = "Total Cost from Convoy Blockades: " + realTotalLoss;
        bridge.getHistoryWriter().startEvent(mainline);
        endTurnReport.append(mainline).append("<br />");
        for (final String t : transcripts) {
            bridge.getHistoryWriter().addChildToEvent(t);
            endTurnReport.append("* ").append(t).append("<br />");
        }
        endTurnReport.append("<br />");
    }
    return realTotalLoss;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) GameMap(games.strategy.engine.data.GameMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit) PlayerAttachment(games.strategy.triplea.attachments.PlayerAttachment) ArrayList(java.util.ArrayList) List(java.util.List) Tuple(games.strategy.util.Tuple)

Aggregations

GameMap (games.strategy.engine.data.GameMap)9 Territory (games.strategy.engine.data.Territory)8 PlayerID (games.strategy.engine.data.PlayerID)3 ArrayList (java.util.ArrayList)3 Unit (games.strategy.engine.data.Unit)2 Test (org.junit.jupiter.api.Test)2 GameData (games.strategy.engine.data.GameData)1 GameParseException (games.strategy.engine.data.GameParseException)1 UnitType (games.strategy.engine.data.UnitType)1 PlayerAttachment (games.strategy.triplea.attachments.PlayerAttachment)1 IntegerMap (games.strategy.util.IntegerMap)1 Tuple (games.strategy.util.Tuple)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1