Search in sources :

Example 21 with ProductionRule

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

the class VictoryTest method testMultipleResourcesToPurchase.

@Test
public void testMultipleResourcesToPurchase() {
    testBridge.setStepName("italianPurchase");
    purchaseDelegate.setDelegateBridgeAndPlayer(testBridge);
    purchaseDelegate.start();
    final IntegerMap<ProductionRule> purchaseList = new IntegerMap<>();
    final ProductionRule armourtest = gameData.getProductionRuleList().getProductionRule("buyArmourtest");
    assertNotNull(armourtest);
    italianResources.subtract(armourtest.getCosts());
    purchaseList.add(armourtest, 1);
    final String error = purchaseDelegate.purchase(purchaseList);
    assertEquals(null, error);
    assertEquals(italianResources, italians.getResources().getResourcesCopy());
}
Also used : IntegerMap(games.strategy.util.IntegerMap) ProductionRule(games.strategy.engine.data.ProductionRule) Test(org.junit.jupiter.api.Test)

Example 22 with ProductionRule

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

the class EditProductionPanel method initRules.

@Override
protected void initRules(final PlayerID player, final IntegerMap<ProductionRule> initialPurchase) {
    this.data.acquireReadLock();
    try {
        id = player;
        final Set<UnitType> unitsAllowed = new HashSet<>();
        if (player.getProductionFrontier() != null) {
            for (final ProductionRule productionRule : player.getProductionFrontier()) {
                final Rule rule = new Rule(productionRule, player);
                for (final Entry<NamedAttachable, Integer> entry : productionRule.getResults().entrySet()) {
                    if (UnitType.class.isAssignableFrom(entry.getKey().getClass())) {
                        unitsAllowed.add((UnitType) entry.getKey());
                    }
                }
                final int initialQuantity = initialPurchase.getInt(productionRule);
                rule.setQuantity(initialQuantity);
                rules.add(rule);
            }
        }
        // use the units on a map.
        for (final Territory t : data.getMap()) {
            for (final Unit u : t.getUnits()) {
                if (u.getOwner().equals(player)) {
                    final UnitType ut = u.getType();
                    if (!unitsAllowed.contains(ut)) {
                        unitsAllowed.add(ut);
                        final IntegerMap<NamedAttachable> result = new IntegerMap<>();
                        result.add(ut, 1);
                        final IntegerMap<Resource> cost = new IntegerMap<>();
                        cost.add(data.getResourceList().getResource(Constants.PUS), 1);
                        final ProductionRule newRule = new ProductionRule(ut.getName(), data, result, cost);
                        final Rule rule = new Rule(newRule, player);
                        rule.setQuantity(0);
                        rules.add(rule);
                    }
                }
            }
        }
        // now check if we have the art for anything that is left
        for (final UnitType ut : data.getUnitTypeList().getAllUnitTypes()) {
            if (!unitsAllowed.contains(ut)) {
                try {
                    final UnitImageFactory imageFactory = uiContext.getUnitImageFactory();
                    if (imageFactory != null) {
                        final Optional<Image> unitImage = imageFactory.getImage(ut, player, false, false);
                        if (unitImage.isPresent()) {
                            unitsAllowed.add(ut);
                            final IntegerMap<NamedAttachable> result = new IntegerMap<>();
                            result.add(ut, 1);
                            final IntegerMap<Resource> cost = new IntegerMap<>();
                            cost.add(data.getResourceList().getResource(Constants.PUS), 1);
                            final ProductionRule newRule = new ProductionRule(ut.getName(), data, result, cost);
                            final Rule rule = new Rule(newRule, player);
                            rule.setQuantity(0);
                            rules.add(rule);
                        }
                    }
                } catch (final Exception e) {
                // ignore
                }
            }
        }
    } finally {
        this.data.releaseReadLock();
    }
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) NamedAttachable(games.strategy.engine.data.NamedAttachable) Resource(games.strategy.engine.data.Resource) Unit(games.strategy.engine.data.Unit) Image(java.awt.Image) ProductionRule(games.strategy.engine.data.ProductionRule) UnitType(games.strategy.engine.data.UnitType) ProductionRule(games.strategy.engine.data.ProductionRule) HashSet(java.util.HashSet) UnitImageFactory(games.strategy.triplea.image.UnitImageFactory)

Example 23 with ProductionRule

use of games.strategy.engine.data.ProductionRule 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 24 with ProductionRule

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

the class ProPurchaseAi method purchase.

Map<Territory, ProPurchaseTerritory> purchase(final IPurchaseDelegate purchaseDelegate, final GameData startOfTurnData) {
    // Current data fields
    data = ProData.getData();
    this.startOfTurnData = startOfTurnData;
    player = ProData.getPlayer();
    resourceTracker = new ProResourceTracker(player);
    territoryManager = new ProTerritoryManager(calc);
    isBid = false;
    final ProPurchaseOptionMap purchaseOptions = ProData.purchaseOptions;
    ProLogger.info("Starting purchase phase with resources: " + resourceTracker);
    if (!player.getUnits().getUnits().isEmpty()) {
        ProLogger.info("Starting purchase phase with unplaced units=" + player.getUnits().getUnits());
    }
    // Find all purchase/place territories
    final Map<Territory, ProPurchaseTerritory> purchaseTerritories = ProPurchaseUtils.findPurchaseTerritories(player);
    final Set<Territory> placeTerritories = new HashSet<>(CollectionUtils.getMatches(data.getMap().getTerritoriesOwnedBy(player), Matches.territoryIsLand()));
    for (final Territory t : purchaseTerritories.keySet()) {
        for (final ProPlaceTerritory ppt : purchaseTerritories.get(t).getCanPlaceTerritories()) {
            placeTerritories.add(ppt.getTerritory());
        }
    }
    // Determine max enemy attack units and current allied defenders
    territoryManager.populateEnemyAttackOptions(new ArrayList<>(), new ArrayList<>(placeTerritories));
    findDefendersInPlaceTerritories(purchaseTerritories);
    // Prioritize land territories that need defended and purchase additional defenders
    final List<ProPlaceTerritory> needToDefendLandTerritories = prioritizeTerritoriesToDefend(purchaseTerritories, true);
    purchaseDefenders(purchaseTerritories, needToDefendLandTerritories, purchaseOptions.getLandFodderOptions(), purchaseOptions.getAirOptions(), true);
    // Find strategic value for each territory
    ProLogger.info("Find strategic value for place territories");
    final Map<Territory, Double> territoryValueMap = ProTerritoryValueUtils.findTerritoryValues(player, new ArrayList<>(), new ArrayList<>());
    for (final Territory t : purchaseTerritories.keySet()) {
        for (final ProPlaceTerritory ppt : purchaseTerritories.get(t).getCanPlaceTerritories()) {
            ppt.setStrategicValue(territoryValueMap.get(ppt.getTerritory()));
            ProLogger.debug(ppt.getTerritory() + ", strategicValue=" + territoryValueMap.get(ppt.getTerritory()));
        }
    }
    // Prioritize land place options purchase AA then land units
    final List<ProPlaceTerritory> prioritizedLandTerritories = prioritizeLandTerritories(purchaseTerritories);
    purchaseAaUnits(purchaseTerritories, prioritizedLandTerritories, purchaseOptions.getAaOptions());
    purchaseLandUnits(purchaseTerritories, prioritizedLandTerritories, purchaseOptions, territoryValueMap);
    // Prioritize sea territories that need defended and purchase additional defenders
    final List<ProPlaceTerritory> needToDefendSeaTerritories = prioritizeTerritoriesToDefend(purchaseTerritories, false);
    purchaseDefenders(purchaseTerritories, needToDefendSeaTerritories, purchaseOptions.getSeaDefenseOptions(), purchaseOptions.getAirOptions(), false);
    // Determine whether to purchase new land factory
    final Map<Territory, ProPurchaseTerritory> factoryPurchaseTerritories = new HashMap<>();
    purchaseFactory(factoryPurchaseTerritories, purchaseTerritories, prioritizedLandTerritories, purchaseOptions, false);
    // Prioritize sea place options and purchase units
    final List<ProPlaceTerritory> prioritizedSeaTerritories = prioritizeSeaTerritories(purchaseTerritories);
    purchaseSeaAndAmphibUnits(purchaseTerritories, prioritizedSeaTerritories, territoryValueMap, purchaseOptions);
    // Try to use any remaining PUs on high value units
    purchaseUnitsWithRemainingProduction(purchaseTerritories, purchaseOptions.getLandOptions(), purchaseOptions.getAirOptions());
    upgradeUnitsWithRemainingPUs(purchaseTerritories, purchaseOptions);
    // Try to purchase land/sea factory with extra PUs
    purchaseFactory(factoryPurchaseTerritories, purchaseTerritories, prioritizedLandTerritories, purchaseOptions, true);
    // Add factory purchase territory to list if not empty
    if (!factoryPurchaseTerritories.isEmpty()) {
        purchaseTerritories.putAll(factoryPurchaseTerritories);
    }
    // Determine final count of each production rule
    final IntegerMap<ProductionRule> purchaseMap = populateProductionRuleMap(purchaseTerritories, purchaseOptions);
    // Purchase units
    ProMetricUtils.collectPurchaseStats(purchaseMap);
    final String error = purchaseDelegate.purchase(purchaseMap);
    if (error != null) {
        ProLogger.warn("Purchase error: " + error);
    }
    return purchaseTerritories;
}
Also used : ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) Territory(games.strategy.engine.data.Territory) HashMap(java.util.HashMap) ProTerritoryManager(games.strategy.triplea.ai.pro.data.ProTerritoryManager) ProPurchaseOptionMap(games.strategy.triplea.ai.pro.data.ProPurchaseOptionMap) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) ProductionRule(games.strategy.engine.data.ProductionRule) ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProResourceTracker(games.strategy.triplea.ai.pro.data.ProResourceTracker) HashSet(java.util.HashSet)

Example 25 with ProductionRule

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

the class PurchasePanel method totalUnitNumberPurchased.

private static int totalUnitNumberPurchased(final IntegerMap<ProductionRule> purchase) {
    int totalUnits = 0;
    final Collection<ProductionRule> rules = purchase.keySet();
    for (final ProductionRule current : rules) {
        totalUnits += purchase.getInt(current) * current.getResults().totalValues();
    }
    return totalUnits;
}
Also used : ProductionRule(games.strategy.engine.data.ProductionRule)

Aggregations

ProductionRule (games.strategy.engine.data.ProductionRule)27 IntegerMap (games.strategy.util.IntegerMap)14 NamedAttachable (games.strategy.engine.data.NamedAttachable)10 Resource (games.strategy.engine.data.Resource)10 UnitType (games.strategy.engine.data.UnitType)10 Unit (games.strategy.engine.data.Unit)9 GameData (games.strategy.engine.data.GameData)8 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)8 Territory (games.strategy.engine.data.Territory)8 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 PlayerID (games.strategy.engine.data.PlayerID)7 ArrayList (java.util.ArrayList)7 List (java.util.List)5 Map (java.util.Map)5 RepairRule (games.strategy.engine.data.RepairRule)4 Matches (games.strategy.triplea.delegate.Matches)4 CollectionUtils (games.strategy.util.CollectionUtils)4 Collection (java.util.Collection)4 LinkedHashMap (java.util.LinkedHashMap)4