Search in sources :

Example 1 with IPurchaseDelegate

use of games.strategy.triplea.delegate.remote.IPurchaseDelegate in project triplea by triplea-game.

the class ProPurchaseAi method bid.

/**
 * Default settings for bidding:
 * 1) Limit one bid unit in a territory or sea zone (until set in all territories then 2, etc).
 * 2) The nation placing a unit in a territory or sea zone must have started with a unit in said territory or sea zone
 * prior to placing the bid.
 */
Map<Territory, ProPurchaseTerritory> bid(final int pus, final IPurchaseDelegate purchaseDelegate, final GameData startOfTurnData) {
    // Current data fields
    data = ProData.getData();
    this.startOfTurnData = startOfTurnData;
    player = ProData.getPlayer();
    resourceTracker = new ProResourceTracker(pus, data);
    territoryManager = new ProTerritoryManager(calc);
    isBid = true;
    final ProPurchaseOptionMap purchaseOptions = ProData.purchaseOptions;
    ProLogger.info("Starting bid phase with resources: " + resourceTracker);
    if (!player.getUnits().getUnits().isEmpty()) {
        ProLogger.info("Starting bid phase with unplaced units=" + player.getUnits().getUnits());
    }
    // Find all purchase/place territories
    final Map<Territory, ProPurchaseTerritory> purchaseTerritories = ProPurchaseUtils.findBidTerritories(player);
    int previousNumUnits = 0;
    while (true) {
        // Determine max enemy attack units and current allied defenders
        territoryManager.populateEnemyAttackOptions(new ArrayList<>(), new ArrayList<>(purchaseTerritories.keySet()));
        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 ProPurchaseTerritory t : purchaseTerritories.values()) {
            for (final ProPlaceTerritory ppt : 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);
        // 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);
        // Check if no remaining PUs or no unit built this iteration
        final int numUnits = purchaseTerritories.values().stream().map(ProPurchaseTerritory::getCanPlaceTerritories).map(t -> t.get(0)).map(ProPlaceTerritory::getPlaceUnits).mapToInt(List::size).sum();
        if (resourceTracker.isEmpty() || numUnits == previousNumUnits) {
            break;
        }
        previousNumUnits = numUnits;
        ProPurchaseUtils.incrementUnitProductionForBidTerritories(purchaseTerritories);
    }
    // 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) 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) ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) Territory(games.strategy.engine.data.Territory) 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)

Example 2 with IPurchaseDelegate

use of games.strategy.triplea.delegate.remote.IPurchaseDelegate in project triplea by triplea-game.

the class TripleAPlayer method purchase.

private void purchase(final boolean bid) {
    if (getPlayerBridge().isGameOver()) {
        return;
    }
    final PlayerID id = getPlayerId();
    // play a sound for this phase
    if (!bid && !soundPlayedAlreadyPurchase) {
        ClipPlayer.play(SoundPath.CLIP_PHASE_PURCHASE, id);
        soundPlayedAlreadyPurchase = true;
    }
    // Check if any factories need to be repaired
    if (id.getRepairFrontier() != null && id.getRepairFrontier().getRules() != null && !id.getRepairFrontier().getRules().isEmpty()) {
        final GameData data = getGameData();
        if (isDamageFromBombingDoneToUnitsInsteadOfTerritories(data)) {
            final Predicate<Unit> myDamaged = Matches.unitIsOwnedBy(id).and(Matches.unitHasTakenSomeBombingUnitDamage());
            final Collection<Unit> damagedUnits = new ArrayList<>();
            for (final Territory t : data.getMap().getTerritories()) {
                damagedUnits.addAll(CollectionUtils.getMatches(t.getUnits().getUnits(), myDamaged));
            }
            if (damagedUnits.size() > 0) {
                final HashMap<Unit, IntegerMap<RepairRule>> repair = ui.getRepair(id, bid, GameStepPropertiesHelper.getRepairPlayers(data, id));
                if (repair != null) {
                    final IPurchaseDelegate purchaseDel;
                    try {
                        purchaseDel = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
                    } catch (final ClassCastException e) {
                        final String errorContext = "PlayerBridge step name: " + getPlayerBridge().getStepName() + ", Remote class name: " + getPlayerBridge().getRemoteDelegate().getClass();
                        // for some reason the client is not seeing or getting these errors, so print to err too
                        System.err.println(errorContext);
                        ClientLogger.logQuietly(errorContext, e);
                        throw new IllegalStateException(errorContext, e);
                    }
                    final String error = purchaseDel.purchaseRepair(repair);
                    if (error != null) {
                        ui.notifyError(error);
                        // dont give up, keep going
                        purchase(bid);
                    }
                }
            }
        }
    }
    final IntegerMap<ProductionRule> prod = ui.getProduction(id, bid);
    if (prod == null) {
        return;
    }
    final IPurchaseDelegate purchaseDel;
    try {
        purchaseDel = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
    } catch (final ClassCastException e) {
        final String errorContext = "PlayerBridge step name: " + getPlayerBridge().getStepName() + ", Remote class name: " + getPlayerBridge().getRemoteDelegate().getClass();
        // for some reason the client is not seeing or getting these errors, so print to err too
        System.err.println(errorContext);
        ClientLogger.logQuietly(errorContext, e);
        throw new IllegalStateException(errorContext, e);
    }
    final String purchaseError = purchaseDel.purchase(prod);
    if (purchaseError != null) {
        ui.notifyError(purchaseError);
        // dont give up, keep going
        purchase(bid);
    }
}
Also used : IntegerMap(games.strategy.util.IntegerMap) PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit) IPurchaseDelegate(games.strategy.triplea.delegate.remote.IPurchaseDelegate) ProductionRule(games.strategy.engine.data.ProductionRule)

Example 3 with IPurchaseDelegate

use of games.strategy.triplea.delegate.remote.IPurchaseDelegate in project triplea by triplea-game.

the class AbstractAi method start.

@Override
public final void start(final String name) {
    super.start(name);
    final PlayerID id = getPlayerId();
    if (name.endsWith("Bid")) {
        final IPurchaseDelegate purchaseDelegate = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
        final String propertyName = id.getName() + " bid";
        final int bidAmount = getGameData().getProperties().get(propertyName, 0);
        purchase(true, bidAmount, purchaseDelegate, getGameData(), id);
    } else if (name.endsWith("Purchase")) {
        final IPurchaseDelegate purchaseDelegate = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
        final Resource pus = getGameData().getResourceList().getResource(Constants.PUS);
        final int leftToSpend = id.getResources().getQuantity(pus);
        purchase(false, leftToSpend, purchaseDelegate, getGameData(), id);
    } else if (name.endsWith("Tech")) {
        final ITechDelegate techDelegate = (ITechDelegate) getPlayerBridge().getRemoteDelegate();
        tech(techDelegate, getGameData(), id);
    } else if (name.endsWith("Move")) {
        final IMoveDelegate moveDel = (IMoveDelegate) getPlayerBridge().getRemoteDelegate();
        if (name.endsWith("AirborneCombatMove")) {
        // do nothing
        } else {
            move(name.endsWith("NonCombatMove"), moveDel, getGameData(), id);
        }
    } else if (name.endsWith("Battle")) {
        battle((IBattleDelegate) getPlayerBridge().getRemoteDelegate(), getGameData(), id);
    } else if (name.endsWith("Politics")) {
        politicalActions();
    } else if (name.endsWith("Place")) {
        final IAbstractPlaceDelegate placeDel = (IAbstractPlaceDelegate) getPlayerBridge().getRemoteDelegate();
        place(name.contains("Bid"), placeDel, getGameData(), id);
    } else if (name.endsWith("EndTurn")) {
        endTurn((IAbstractForumPosterDelegate) getPlayerBridge().getRemoteDelegate(), getGameData(), id);
    }
}
Also used : IMoveDelegate(games.strategy.triplea.delegate.remote.IMoveDelegate) PlayerID(games.strategy.engine.data.PlayerID) ITechDelegate(games.strategy.triplea.delegate.remote.ITechDelegate) IPurchaseDelegate(games.strategy.triplea.delegate.remote.IPurchaseDelegate) IAbstractForumPosterDelegate(games.strategy.triplea.delegate.remote.IAbstractForumPosterDelegate) IAbstractPlaceDelegate(games.strategy.triplea.delegate.remote.IAbstractPlaceDelegate) Resource(games.strategy.engine.data.Resource)

Aggregations

PlayerID (games.strategy.engine.data.PlayerID)3 IPurchaseDelegate (games.strategy.triplea.delegate.remote.IPurchaseDelegate)3 GameData (games.strategy.engine.data.GameData)2 ProductionRule (games.strategy.engine.data.ProductionRule)2 Territory (games.strategy.engine.data.Territory)2 Unit (games.strategy.engine.data.Unit)2 IAbstractPlaceDelegate (games.strategy.triplea.delegate.remote.IAbstractPlaceDelegate)2 IntegerMap (games.strategy.util.IntegerMap)2 ArrayList (java.util.ArrayList)2 RepairRule (games.strategy.engine.data.RepairRule)1 Resource (games.strategy.engine.data.Resource)1 Route (games.strategy.engine.data.Route)1 Properties (games.strategy.triplea.Properties)1 TripleAUnit (games.strategy.triplea.TripleAUnit)1 ProBattleResult (games.strategy.triplea.ai.pro.data.ProBattleResult)1 ProOtherMoveOptions (games.strategy.triplea.ai.pro.data.ProOtherMoveOptions)1 ProPlaceTerritory (games.strategy.triplea.ai.pro.data.ProPlaceTerritory)1 ProPurchaseOption (games.strategy.triplea.ai.pro.data.ProPurchaseOption)1 ProPurchaseOptionMap (games.strategy.triplea.ai.pro.data.ProPurchaseOptionMap)1 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)1