Search in sources :

Example 11 with ProPurchaseOption

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;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) ProPurchaseOption(games.strategy.triplea.ai.pro.data.ProPurchaseOption) ProUtils(games.strategy.triplea.ai.pro.util.ProUtils) IPurchaseDelegate(games.strategy.triplea.delegate.remote.IPurchaseDelegate) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) HashMap(java.util.HashMap) ProResourceTracker(games.strategy.triplea.ai.pro.data.ProResourceTracker) ProBattleUtils(games.strategy.triplea.ai.pro.util.ProBattleUtils) Properties(games.strategy.triplea.Properties) ProTransportUtils(games.strategy.triplea.ai.pro.util.ProTransportUtils) TuvUtils(games.strategy.triplea.util.TuvUtils) ArrayList(java.util.ArrayList) ProLogger(games.strategy.triplea.ai.pro.logging.ProLogger) PlaceableUnits(games.strategy.triplea.delegate.dataObjects.PlaceableUnits) HashSet(java.util.HashSet) Route(games.strategy.engine.data.Route) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) ProTerritoryManager(games.strategy.triplea.ai.pro.data.ProTerritoryManager) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) MoveValidator(games.strategy.triplea.delegate.MoveValidator) Map(java.util.Map) TripleAUnit(games.strategy.triplea.TripleAUnit) ProOtherMoveOptions(games.strategy.triplea.ai.pro.data.ProOtherMoveOptions) RepairRule(games.strategy.engine.data.RepairRule) CollectionUtils(games.strategy.util.CollectionUtils) IntegerMap(games.strategy.util.IntegerMap) ProPurchaseOptionMap(games.strategy.triplea.ai.pro.data.ProPurchaseOptionMap) Unit(games.strategy.engine.data.Unit) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Territory(games.strategy.engine.data.Territory) ProMetricUtils(games.strategy.triplea.ai.pro.logging.ProMetricUtils) ProTerritoryValueUtils(games.strategy.triplea.ai.pro.util.ProTerritoryValueUtils) ProPurchaseUtils(games.strategy.triplea.ai.pro.util.ProPurchaseUtils) GameData(games.strategy.engine.data.GameData) List(java.util.List) PlayerID(games.strategy.engine.data.PlayerID) ProMatches(games.strategy.triplea.ai.pro.util.ProMatches) Matches(games.strategy.triplea.delegate.Matches) IAbstractPlaceDelegate(games.strategy.triplea.delegate.remote.IAbstractPlaceDelegate) Optional(java.util.Optional) ProOddsCalculator(games.strategy.triplea.ai.pro.util.ProOddsCalculator) ProductionRule(games.strategy.engine.data.ProductionRule) Comparator(java.util.Comparator) Collections(java.util.Collections) ProductionRule(games.strategy.engine.data.ProductionRule) ProPurchaseOption(games.strategy.triplea.ai.pro.data.ProPurchaseOption) Collection(java.util.Collection) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 12 with ProPurchaseOption

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);
    }
}
Also used : ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) Territory(games.strategy.engine.data.Territory) ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProPurchaseOption(games.strategy.triplea.ai.pro.data.ProPurchaseOption) ProOtherMoveOptions(games.strategy.triplea.ai.pro.data.ProOtherMoveOptions) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Aggregations

ProPurchaseOption (games.strategy.triplea.ai.pro.data.ProPurchaseOption)12 Territory (games.strategy.engine.data.Territory)9 Unit (games.strategy.engine.data.Unit)9 TripleAUnit (games.strategy.triplea.TripleAUnit)9 ProPlaceTerritory (games.strategy.triplea.ai.pro.data.ProPlaceTerritory)9 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)9 ArrayList (java.util.ArrayList)9 ProOtherMoveOptions (games.strategy.triplea.ai.pro.data.ProOtherMoveOptions)5 HashMap (java.util.HashMap)5 ProBattleResult (games.strategy.triplea.ai.pro.data.ProBattleResult)4 HashSet (java.util.HashSet)4 GameData (games.strategy.engine.data.GameData)2 Route (games.strategy.engine.data.Route)2 ProPurchaseOptionMap (games.strategy.triplea.ai.pro.data.ProPurchaseOptionMap)2 List (java.util.List)2 Predicate (java.util.function.Predicate)2 PlayerID (games.strategy.engine.data.PlayerID)1 ProductionRule (games.strategy.engine.data.ProductionRule)1 RepairRule (games.strategy.engine.data.RepairRule)1 Resource (games.strategy.engine.data.Resource)1