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());
}
}
}
Aggregations