use of games.strategy.triplea.ai.pro.data.ProPurchaseOption in project triplea by triplea-game.
the class ProPurchaseAi method populateProductionRuleMap.
private IntegerMap<ProductionRule> populateProductionRuleMap(final Map<Territory, ProPurchaseTerritory> purchaseTerritories, final ProPurchaseOptionMap purchaseOptions) {
ProLogger.info("Populate production rule map");
final List<Unit> unplacedUnits = player.getUnits().getMatches(Matches.unitIsNotSea());
final IntegerMap<ProductionRule> purchaseMap = new IntegerMap<>();
for (final ProPurchaseOption ppo : purchaseOptions.getAllOptions()) {
final int numUnits = (int) purchaseTerritories.values().stream().map(ProPurchaseTerritory::getCanPlaceTerritories).flatMap(Collection::stream).map(ProPlaceTerritory::getPlaceUnits).flatMap(Collection::stream).filter(u -> u.getType().equals(ppo.getUnitType())).filter(u -> !unplacedUnits.contains(u)).count();
if (numUnits > 0) {
final int numProductionRule = numUnits / ppo.getQuantity();
purchaseMap.put(ppo.getProductionRule(), numProductionRule);
ProLogger.info(numProductionRule + " " + ppo.getProductionRule());
}
}
return purchaseMap;
}
use of games.strategy.triplea.ai.pro.data.ProPurchaseOption in project triplea by triplea-game.
the class ProPurchaseAi method purchaseAaUnits.
private void purchaseAaUnits(final Map<Territory, ProPurchaseTerritory> purchaseTerritories, final List<ProPlaceTerritory> prioritizedLandTerritories, final List<ProPurchaseOption> specialPurchaseOptions) {
if (resourceTracker.isEmpty()) {
return;
}
ProLogger.info("Purchase AA units with resources: " + resourceTracker);
final ProOtherMoveOptions enemyAttackOptions = territoryManager.getEnemyAttackOptions();
// Loop through prioritized territories and purchase AA units
for (final ProPlaceTerritory placeTerritory : prioritizedLandTerritories) {
final Territory t = placeTerritory.getTerritory();
ProLogger.debug("Checking AA place for " + t);
// Check if any enemy attackers
if (enemyAttackOptions.getMax(t) == null) {
continue;
}
// Check remaining production
final int remainingUnitProduction = purchaseTerritories.get(t).getRemainingUnitProduction();
ProLogger.debug(t + ", remainingUnitProduction=" + remainingUnitProduction);
if (remainingUnitProduction <= 0) {
continue;
}
// Check if territory needs AA
final boolean enemyCanBomb = enemyAttackOptions.getMax(t).getMaxUnits().stream().anyMatch(Matches.unitIsStrategicBomber());
final boolean territoryCanBeBombed = t.getUnits().anyMatch(Matches.unitCanProduceUnitsAndCanBeDamaged());
final boolean hasAaBombingDefense = t.getUnits().anyMatch(Matches.unitIsAaForBombingThisUnitOnly());
ProLogger.debug(t + ", enemyCanBomb=" + enemyCanBomb + ", territoryCanBeBombed=" + territoryCanBeBombed + ", hasAABombingDefense=" + hasAaBombingDefense);
if (!enemyCanBomb || !territoryCanBeBombed || hasAaBombingDefense) {
continue;
}
// Remove options that cost too much PUs or production
final List<ProPurchaseOption> purchaseOptionsForTerritory = ProPurchaseUtils.findPurchaseOptionsForTerritory(player, specialPurchaseOptions, t, isBid);
ProPurchaseUtils.removeInvalidPurchaseOptions(player, startOfTurnData, purchaseOptionsForTerritory, resourceTracker, remainingUnitProduction, new ArrayList<>(), purchaseTerritories);
if (purchaseOptionsForTerritory.isEmpty()) {
continue;
}
// Determine most cost efficient units that can be produced in this territory
ProPurchaseOption bestAaOption = null;
int minCost = Integer.MAX_VALUE;
for (final ProPurchaseOption ppo : purchaseOptionsForTerritory) {
final boolean isAaForBombing = Matches.unitTypeIsAaForBombingThisUnitOnly().test(ppo.getUnitType());
if (isAaForBombing && ppo.getCost() < minCost && !Matches.unitTypeConsumesUnitsOnCreation().test(ppo.getUnitType())) {
bestAaOption = ppo;
minCost = ppo.getCost();
}
}
// Check if there aren't any available units
if (bestAaOption == null) {
continue;
}
ProLogger.trace("Best AA unit: " + bestAaOption.getUnitType().getName());
// Create new temp units
resourceTracker.purchase(bestAaOption);
final List<Unit> unitsToPlace = bestAaOption.getUnitType().create(bestAaOption.getQuantity(), player, true);
placeTerritory.getPlaceUnits().addAll(unitsToPlace);
ProLogger.trace(t + ", placedUnits=" + unitsToPlace);
}
}
Aggregations