Search in sources :

Example 51 with GameData

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

the class ProMoveUtils method doMove.

public static void doMove(final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final List<Collection<Unit>> transportsToLoad, final IMoveDelegate moveDel) {
    final GameData data = ProData.getData();
    // Group non-amphib units of the same type moving on the same route
    if (transportsToLoad == null) {
        for (int i = 0; i < moveRoutes.size(); i++) {
            final Route r = moveRoutes.get(i);
            for (int j = i + 1; j < moveRoutes.size(); j++) {
                final Route r2 = moveRoutes.get(j);
                if (r.equals(r2)) {
                    moveUnits.get(j).addAll(moveUnits.get(i));
                    moveUnits.remove(i);
                    moveRoutes.remove(i);
                    i--;
                    break;
                }
            }
        }
    }
    // Move units
    for (int i = 0; i < moveRoutes.size(); i++) {
        if (!ProData.isSimulation) {
            ProUtils.pause();
        }
        if (moveRoutes.get(i) == null || moveRoutes.get(i).getEnd() == null || moveRoutes.get(i).getStart() == null) {
            ProLogger.warn(data.getSequence().getRound() + "-" + data.getSequence().getStep().getName() + ": route not valid " + moveRoutes.get(i) + " units: " + moveUnits.get(i));
            continue;
        }
        final String result;
        if (transportsToLoad == null || transportsToLoad.get(i) == null) {
            result = moveDel.move(moveUnits.get(i), moveRoutes.get(i));
        } else {
            result = moveDel.move(moveUnits.get(i), moveRoutes.get(i), transportsToLoad.get(i));
        }
        if (result != null) {
            ProLogger.warn(data.getSequence().getRound() + "-" + data.getSequence().getStep().getName() + ": could not move " + moveUnits.get(i) + " over " + moveRoutes.get(i) + " because: " + result);
        }
    }
}
Also used : GameData(games.strategy.engine.data.GameData) Route(games.strategy.engine.data.Route)

Example 52 with GameData

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

the class ProMoveUtils method calculateMoveRoutes.

public static void calculateMoveRoutes(final PlayerID player, final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final Map<Territory, ProTerritory> attackMap, final boolean isCombatMove) {
    final GameData data = ProData.getData();
    // Find all amphib units
    final Set<Unit> amphibUnits = attackMap.values().stream().map(ProTerritory::getAmphibAttackMap).map(Map::entrySet).flatMap(Collection::stream).flatMap(e -> Stream.concat(Stream.of(e.getKey()), e.getValue().stream())).collect(Collectors.toSet());
    // Loop through all territories to attack
    for (final Territory t : attackMap.keySet()) {
        // Loop through each unit that is attacking the current territory
        for (final Unit u : attackMap.get(t).getUnits()) {
            // Skip amphib units
            if (amphibUnits.contains(u)) {
                continue;
            }
            // Skip if unit is already in move to territory
            final Territory startTerritory = ProData.unitTerritoryMap.get(u);
            if (startTerritory == null || startTerritory.equals(t)) {
                continue;
            }
            // Add unit to move list
            final List<Unit> unitList = new ArrayList<>();
            unitList.add(u);
            moveUnits.add(unitList);
            // If carrier has dependent allied fighters then move them too
            if (Matches.unitIsCarrier().test(u)) {
                final Map<Unit, Collection<Unit>> carrierMustMoveWith = MoveValidator.carrierMustMoveWith(startTerritory.getUnits().getUnits(), startTerritory, data, player);
                if (carrierMustMoveWith.containsKey(u)) {
                    unitList.addAll(carrierMustMoveWith.get(u));
                }
            }
            // Determine route and add to move list
            Route route = null;
            if (unitList.stream().anyMatch(Matches.unitIsSea())) {
                // Sea unit (including carriers with planes)
                route = data.getMap().getRoute_IgnoreEnd(startTerritory, t, ProMatches.territoryCanMoveSeaUnitsThrough(player, data, isCombatMove));
            } else if (!unitList.isEmpty() && unitList.stream().allMatch(Matches.unitIsLand())) {
                // Land unit
                route = data.getMap().getRoute_IgnoreEnd(startTerritory, t, ProMatches.territoryCanMoveLandUnitsThrough(player, data, u, startTerritory, isCombatMove, new ArrayList<>()));
            } else if (!unitList.isEmpty() && unitList.stream().allMatch(Matches.unitIsAir())) {
                // Air unit
                route = data.getMap().getRoute_IgnoreEnd(startTerritory, t, ProMatches.territoryCanMoveAirUnitsAndNoAa(player, data, isCombatMove));
            }
            if (route == null) {
                ProLogger.warn(data.getSequence().getRound() + "-" + data.getSequence().getStep().getName() + ": route is null " + startTerritory + " to " + t + ", units=" + unitList);
            }
            moveRoutes.add(route);
        }
    }
}
Also used : Unit(games.strategy.engine.data.Unit) Collection(java.util.Collection) Set(java.util.Set) Territory(games.strategy.engine.data.Territory) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) ProLogger(games.strategy.triplea.ai.pro.logging.ProLogger) ProData(games.strategy.triplea.ai.pro.ProData) GameData(games.strategy.engine.data.GameData) Route(games.strategy.engine.data.Route) List(java.util.List) TransportTracker(games.strategy.triplea.delegate.TransportTracker) Stream(java.util.stream.Stream) PlayerID(games.strategy.engine.data.PlayerID) IMoveDelegate(games.strategy.triplea.delegate.remote.IMoveDelegate) Matches(games.strategy.triplea.delegate.Matches) MoveValidator(games.strategy.triplea.delegate.MoveValidator) Map(java.util.Map) TripleAUnit(games.strategy.triplea.TripleAUnit) Collections(java.util.Collections) Territory(games.strategy.engine.data.Territory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) GameData(games.strategy.engine.data.GameData) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) Route(games.strategy.engine.data.Route)

Example 53 with GameData

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

the class ProMoveUtils method calculateBombingRoutes.

public static void calculateBombingRoutes(final PlayerID player, final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final Map<Territory, ProTerritory> attackMap) {
    final GameData data = ProData.getData();
    // Loop through all territories to attack
    for (final Territory t : attackMap.keySet()) {
        // Loop through each unit that is attacking the current territory
        for (final Unit u : attackMap.get(t).getBombers()) {
            // Skip if unit is already in move to territory
            final Territory startTerritory = ProData.unitTerritoryMap.get(u);
            if (startTerritory == null || startTerritory.equals(t)) {
                continue;
            }
            // Add unit to move list
            final List<Unit> unitList = new ArrayList<>();
            unitList.add(u);
            moveUnits.add(unitList);
            // Determine route and add to move list
            Route route = null;
            if (!unitList.isEmpty() && unitList.stream().allMatch(Matches.unitIsAir())) {
                route = data.getMap().getRoute_IgnoreEnd(startTerritory, t, ProMatches.territoryCanMoveAirUnitsAndNoAa(player, data, true));
            }
            moveRoutes.add(route);
        }
    }
}
Also used : 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) TripleAUnit(games.strategy.triplea.TripleAUnit) Route(games.strategy.engine.data.Route)

Example 54 with GameData

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

the class ProOddsCalculator method callBattleCalculator.

public ProBattleResult callBattleCalculator(final Territory t, final List<Unit> attackingUnits, final List<Unit> defendingUnits, final Set<Unit> bombardingUnits, final boolean retreatWhenOnlyAirLeft) {
    final GameData data = ProData.getData();
    if (isCanceled || attackingUnits.isEmpty() || defendingUnits.isEmpty()) {
        return new ProBattleResult();
    }
    final int minArmySize = Math.min(attackingUnits.size(), defendingUnits.size());
    final int runCount = Math.max(16, 100 - minArmySize);
    final PlayerID attacker = attackingUnits.get(0).getOwner();
    final PlayerID defender = defendingUnits.get(0).getOwner();
    if (retreatWhenOnlyAirLeft) {
        calc.setRetreatWhenOnlyAirLeft(true);
    }
    final AggregateResults results = calc.setCalculateDataAndCalculate(attacker, defender, t, attackingUnits, defendingUnits, new ArrayList<>(bombardingUnits), TerritoryEffectHelper.getEffects(t), runCount);
    if (retreatWhenOnlyAirLeft) {
        calc.setRetreatWhenOnlyAirLeft(false);
    }
    // Find battle result statistics
    final double winPercentage = results.getAttackerWinPercent() * 100;
    final List<Unit> averageAttackersRemaining = results.getAverageAttackingUnitsRemaining();
    final List<Unit> averageDefendersRemaining = results.getAverageDefendingUnitsRemaining();
    final List<Unit> mainCombatAttackers = CollectionUtils.getMatches(attackingUnits, Matches.unitCanBeInBattle(true, !t.isWater(), 1, false, true, true));
    final List<Unit> mainCombatDefenders = CollectionUtils.getMatches(defendingUnits, Matches.unitCanBeInBattle(false, !t.isWater(), 1, false, true, true));
    double tuvSwing = results.getAverageTuvSwing(attacker, mainCombatAttackers, defender, mainCombatDefenders, data);
    if (Matches.territoryIsNeutralButNotWater().test(t)) {
        // Set TUV swing for neutrals
        final double attackingUnitValue = TuvUtils.getTuv(mainCombatAttackers, ProData.unitValueMap);
        final double remainingUnitValue = results.getAverageTuvOfUnitsLeftOver(ProData.unitValueMap, ProData.unitValueMap).getFirst();
        tuvSwing = remainingUnitValue - attackingUnitValue;
    }
    final List<Unit> defendingTransportedUnits = CollectionUtils.getMatches(defendingUnits, Matches.unitIsBeingTransported());
    if (t.isWater() && !defendingTransportedUnits.isEmpty()) {
        // Add TUV swing for transported units
        final double transportedUnitValue = TuvUtils.getTuv(defendingTransportedUnits, ProData.unitValueMap);
        tuvSwing += transportedUnitValue * winPercentage / 100;
    }
    // Create battle result object
    final List<Territory> territoryList = new ArrayList<>();
    territoryList.add(t);
    return (!territoryList.isEmpty() && territoryList.stream().allMatch(Matches.territoryIsLand())) ? new ProBattleResult(winPercentage, tuvSwing, averageAttackersRemaining.stream().anyMatch(Matches.unitIsLand()), averageAttackersRemaining, averageDefendersRemaining, results.getAverageBattleRoundsFought()) : new ProBattleResult(winPercentage, tuvSwing, !averageAttackersRemaining.isEmpty(), averageAttackersRemaining, averageDefendersRemaining, results.getAverageBattleRoundsFought());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) AggregateResults(games.strategy.triplea.oddsCalculator.ta.AggregateResults) ArrayList(java.util.ArrayList) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) Unit(games.strategy.engine.data.Unit)

Example 55 with GameData

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

the class ProPurchaseUtils method canUnitsBePlaced.

public static boolean canUnitsBePlaced(final List<Unit> units, final PlayerID player, final Territory t, final boolean isBid) {
    final GameData data = ProData.getData();
    AbstractPlaceDelegate placeDelegate = (AbstractPlaceDelegate) data.getDelegateList().getDelegate("place");
    if (isBid) {
        placeDelegate = (AbstractPlaceDelegate) data.getDelegateList().getDelegate("placeBid");
    }
    final IDelegateBridge bridge = new ProDummyDelegateBridge(ProData.getProAi(), player, data);
    placeDelegate.setDelegateBridgeAndPlayer(bridge);
    final String s = placeDelegate.canUnitsBePlaced(t, units, player);
    return s == null;
}
Also used : GameData(games.strategy.engine.data.GameData) ProDummyDelegateBridge(games.strategy.triplea.ai.pro.simulate.ProDummyDelegateBridge) AbstractPlaceDelegate(games.strategy.triplea.delegate.AbstractPlaceDelegate) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge)

Aggregations

GameData (games.strategy.engine.data.GameData)204 Unit (games.strategy.engine.data.Unit)100 PlayerID (games.strategy.engine.data.PlayerID)92 Territory (games.strategy.engine.data.Territory)92 ArrayList (java.util.ArrayList)83 TripleAUnit (games.strategy.triplea.TripleAUnit)64 HashSet (java.util.HashSet)50 CompositeChange (games.strategy.engine.data.CompositeChange)40 List (java.util.List)36 HashMap (java.util.HashMap)32 Set (java.util.Set)32 Route (games.strategy.engine.data.Route)31 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)30 Collection (java.util.Collection)29 UnitType (games.strategy.engine.data.UnitType)26 Change (games.strategy.engine.data.Change)24 Test (org.junit.jupiter.api.Test)23 Resource (games.strategy.engine.data.Resource)22 TestMapGameData (games.strategy.triplea.xml.TestMapGameData)22 Map (java.util.Map)21