Search in sources :

Example 21 with ProPlaceTerritory

use of games.strategy.triplea.ai.pro.data.ProPlaceTerritory in project triplea by triplea-game.

the class ProPurchaseAi method placeDefenders.

private void placeDefenders(final Map<Territory, ProPurchaseTerritory> placeNonConstructionTerritories, final List<ProPlaceTerritory> needToDefendTerritories, final IAbstractPlaceDelegate placeDelegate) {
    ProLogger.info("Place defenders with units=" + player.getUnits().getUnits());
    final ProOtherMoveOptions enemyAttackOptions = territoryManager.getEnemyAttackOptions();
    // Loop through prioritized territories and purchase defenders
    for (final ProPlaceTerritory placeTerritory : needToDefendTerritories) {
        final Territory t = placeTerritory.getTerritory();
        ProLogger.debug("Placing defenders for " + t.getName() + ", enemyAttackers=" + enemyAttackOptions.getMax(t).getMaxUnits() + ", amphibEnemyAttackers=" + enemyAttackOptions.getMax(t).getMaxAmphibUnits() + ", defenders=" + placeTerritory.getDefendingUnits());
        // Check if any units can be placed
        final PlaceableUnits placeableUnits = placeDelegate.getPlaceableUnits(player.getUnits().getMatches(Matches.unitIsNotConstruction()), t);
        if (placeableUnits.isError()) {
            ProLogger.trace(t + " can't place units with error: " + placeableUnits.getErrorMessage());
            continue;
        }
        // Find remaining unit production
        int remainingUnitProduction = placeableUnits.getMaxUnits();
        if (remainingUnitProduction == -1) {
            remainingUnitProduction = Integer.MAX_VALUE;
        }
        ProLogger.trace(t + ", remainingUnitProduction=" + remainingUnitProduction);
        // Place defenders and check battle results
        final List<Unit> unitsThatCanBePlaced = new ArrayList<>(placeableUnits.getUnits());
        final int landPlaceCount = Math.min(remainingUnitProduction, unitsThatCanBePlaced.size());
        final List<Unit> unitsToPlace = new ArrayList<>();
        ProBattleResult finalResult = new ProBattleResult();
        for (int i = 0; i < landPlaceCount; i++) {
            // Add defender
            unitsToPlace.add(unitsThatCanBePlaced.get(i));
            // Find current battle result
            final Set<Unit> enemyAttackingUnits = new HashSet<>(enemyAttackOptions.getMax(t).getMaxUnits());
            enemyAttackingUnits.addAll(enemyAttackOptions.getMax(t).getMaxAmphibUnits());
            final List<Unit> defenders = new ArrayList<>(placeTerritory.getDefendingUnits());
            defenders.addAll(unitsToPlace);
            finalResult = calc.calculateBattleResults(t, new ArrayList<>(enemyAttackingUnits), defenders, enemyAttackOptions.getMax(t).getMaxBombardUnits());
            // Break if it can be held
            if ((!t.equals(ProData.myCapital) && !finalResult.isHasLandUnitRemaining() && finalResult.getTuvSwing() <= 0) || (t.equals(ProData.myCapital) && finalResult.getWinPercentage() < (100 - ProData.winPercentage) && finalResult.getTuvSwing() <= 0)) {
                break;
            }
        }
        // Check to see if its worth trying to defend the territory
        if (!finalResult.isHasLandUnitRemaining() || finalResult.getTuvSwing() < placeTerritory.getMinBattleResult().getTuvSwing() || t.equals(ProData.myCapital)) {
            ProLogger.trace(t + ", placedUnits=" + unitsToPlace + ", TUVSwing=" + finalResult.getTuvSwing());
            doPlace(t, unitsToPlace, placeDelegate);
        } else {
            setCantHoldPlaceTerritory(placeTerritory, placeNonConstructionTerritories);
            ProLogger.trace(t + ", unable to defend with placedUnits=" + unitsToPlace + ", TUVSwing=" + finalResult.getTuvSwing() + ", minTUVSwing=" + placeTerritory.getMinBattleResult().getTuvSwing());
        }
    }
}
Also used : ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) Territory(games.strategy.engine.data.Territory) PlaceableUnits(games.strategy.triplea.delegate.dataObjects.PlaceableUnits) ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ArrayList(java.util.ArrayList) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) ProOtherMoveOptions(games.strategy.triplea.ai.pro.data.ProOtherMoveOptions) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) HashSet(java.util.HashSet)

Aggregations

ProPlaceTerritory (games.strategy.triplea.ai.pro.data.ProPlaceTerritory)21 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)21 Territory (games.strategy.engine.data.Territory)20 Unit (games.strategy.engine.data.Unit)18 TripleAUnit (games.strategy.triplea.TripleAUnit)17 ArrayList (java.util.ArrayList)16 ProPurchaseOption (games.strategy.triplea.ai.pro.data.ProPurchaseOption)10 HashSet (java.util.HashSet)10 ProOtherMoveOptions (games.strategy.triplea.ai.pro.data.ProOtherMoveOptions)9 ProBattleResult (games.strategy.triplea.ai.pro.data.ProBattleResult)7 HashMap (java.util.HashMap)7 ProPurchaseOptionMap (games.strategy.triplea.ai.pro.data.ProPurchaseOptionMap)4 ProTerritoryManager (games.strategy.triplea.ai.pro.data.ProTerritoryManager)4 PlaceableUnits (games.strategy.triplea.delegate.dataObjects.PlaceableUnits)4 GameData (games.strategy.engine.data.GameData)3 ProductionRule (games.strategy.engine.data.ProductionRule)3 Route (games.strategy.engine.data.Route)3 ProResourceTracker (games.strategy.triplea.ai.pro.data.ProResourceTracker)3 TerritoryAttachment (games.strategy.triplea.attachments.TerritoryAttachment)3 PlayerID (games.strategy.engine.data.PlayerID)2