Search in sources :

Example 1 with ProDummyDelegateBridge

use of games.strategy.triplea.ai.pro.simulate.ProDummyDelegateBridge in project triplea by triplea-game.

the class ProPurchaseUtils method canUnitsBePlaced.

public static boolean canUnitsBePlaced(final List<Unit> units, final PlayerID player, final Territory t, final boolean isBid) {
    final GameData data = ProData.getData();
    AbstractPlaceDelegate placeDelegate = (AbstractPlaceDelegate) data.getDelegateList().getDelegate("place");
    if (isBid) {
        placeDelegate = (AbstractPlaceDelegate) data.getDelegateList().getDelegate("placeBid");
    }
    final IDelegateBridge bridge = new ProDummyDelegateBridge(ProData.getProAi(), player, data);
    placeDelegate.setDelegateBridgeAndPlayer(bridge);
    final String s = placeDelegate.canUnitsBePlaced(t, units, player);
    return s == null;
}
Also used : GameData(games.strategy.engine.data.GameData) ProDummyDelegateBridge(games.strategy.triplea.ai.pro.simulate.ProDummyDelegateBridge) AbstractPlaceDelegate(games.strategy.triplea.delegate.AbstractPlaceDelegate) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge)

Example 2 with ProDummyDelegateBridge

use of games.strategy.triplea.ai.pro.simulate.ProDummyDelegateBridge in project triplea by triplea-game.

the class ProAi method purchase.

@Override
protected void purchase(final boolean purchaseForBid, final int pusToSpend, final IPurchaseDelegate purchaseDelegate, final GameData data, final PlayerID player) {
    final long start = System.currentTimeMillis();
    BattleCalculator.clearOolCache();
    ProLogUi.notifyStartOfRound(data.getSequence().getRound(), player.getName());
    initializeData();
    if (pusToSpend <= 0) {
        return;
    }
    if (purchaseForBid) {
        calc.setData(data);
        storedPurchaseTerritories = purchaseAi.bid(pusToSpend, purchaseDelegate, data);
    } else {
        // Repair factories
        purchaseAi.repair(pusToSpend, purchaseDelegate, data, player);
        // Check if any place territories exist
        final Map<Territory, ProPurchaseTerritory> purchaseTerritories = ProPurchaseUtils.findPurchaseTerritories(player);
        final List<Territory> possibleFactoryTerritories = CollectionUtils.getMatches(data.getMap().getTerritories(), ProMatches.territoryHasNoInfraFactoryAndIsNotConqueredOwnedLand(player, data));
        if (purchaseTerritories.isEmpty() && possibleFactoryTerritories.isEmpty()) {
            ProLogger.info("No possible place or factory territories owned so exiting purchase logic");
            return;
        }
        ProLogger.info("Starting simulation for purchase phase");
        // Setup data copy and delegates
        GameData dataCopy;
        try {
            data.acquireReadLock();
            dataCopy = GameDataUtils.cloneGameData(data, true);
        } catch (final Throwable t) {
            ProLogger.log(Level.WARNING, "Error trying to clone game data for simulating phases", t);
            return;
        } finally {
            data.releaseReadLock();
        }
        calc.setData(dataCopy);
        final PlayerID playerCopy = dataCopy.getPlayerList().getPlayerId(player.getName());
        final IMoveDelegate moveDel = DelegateFinder.moveDelegate(dataCopy);
        final IDelegateBridge bridge = new ProDummyDelegateBridge(this, playerCopy, dataCopy);
        moveDel.setDelegateBridgeAndPlayer(bridge);
        // Determine turn sequence
        final List<GameStep> gameSteps = new ArrayList<>();
        for (final GameStep gameStep : dataCopy.getSequence()) {
            gameSteps.add(gameStep);
        }
        // Simulate the next phases until place/end of turn is reached then use simulated data for purchase
        final int nextStepIndex = dataCopy.getSequence().getStepIndex() + 1;
        for (int i = nextStepIndex; i < gameSteps.size(); i++) {
            final GameStep step = gameSteps.get(i);
            if (!playerCopy.equals(step.getPlayerId())) {
                continue;
            }
            dataCopy.getSequence().setRoundAndStep(dataCopy.getSequence().getRound(), step.getDisplayName(), step.getPlayerId());
            final String stepName = step.getName();
            ProLogger.info("Simulating phase: " + stepName);
            if (stepName.endsWith("NonCombatMove")) {
                ProData.initializeSimulation(this, dataCopy, playerCopy);
                final Map<Territory, ProTerritory> factoryMoveMap = nonCombatMoveAi.simulateNonCombatMove(moveDel);
                if (storedFactoryMoveMap == null) {
                    storedFactoryMoveMap = ProSimulateTurnUtils.transferMoveMap(factoryMoveMap, data, player);
                }
            } else if (stepName.endsWith("CombatMove") && !stepName.endsWith("AirborneCombatMove")) {
                ProData.initializeSimulation(this, dataCopy, playerCopy);
                final Map<Territory, ProTerritory> moveMap = combatMoveAi.doCombatMove(moveDel);
                if (storedCombatMoveMap == null) {
                    storedCombatMoveMap = ProSimulateTurnUtils.transferMoveMap(moveMap, data, player);
                }
            } else if (stepName.endsWith("Battle")) {
                ProData.initializeSimulation(this, dataCopy, playerCopy);
                ProSimulateTurnUtils.simulateBattles(dataCopy, playerCopy, bridge, calc);
            } else if (stepName.endsWith("Place") || stepName.endsWith("EndTurn")) {
                ProData.initializeSimulation(this, dataCopy, player);
                storedPurchaseTerritories = purchaseAi.purchase(purchaseDelegate, data);
                break;
            } else if (stepName.endsWith("Politics")) {
                ProData.initializeSimulation(this, dataCopy, player);
                final PoliticsDelegate politicsDelegate = DelegateFinder.politicsDelegate(dataCopy);
                politicsDelegate.setDelegateBridgeAndPlayer(bridge);
                final List<PoliticalActionAttachment> actions = politicsAi.politicalActions();
                if (storedPoliticalActions == null) {
                    storedPoliticalActions = actions;
                }
            }
        }
    }
    ProLogger.info(player.getName() + " time for purchase=" + (System.currentTimeMillis() - start));
}
Also used : IMoveDelegate(games.strategy.triplea.delegate.remote.IMoveDelegate) PlayerID(games.strategy.engine.data.PlayerID) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) ArrayList(java.util.ArrayList) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) PoliticsDelegate(games.strategy.triplea.delegate.PoliticsDelegate) PoliticalActionAttachment(games.strategy.triplea.attachments.PoliticalActionAttachment) ProDummyDelegateBridge(games.strategy.triplea.ai.pro.simulate.ProDummyDelegateBridge) GameStep(games.strategy.engine.data.GameStep) Map(java.util.Map) HashMap(java.util.HashMap) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge)

Aggregations

GameData (games.strategy.engine.data.GameData)2 IDelegateBridge (games.strategy.engine.delegate.IDelegateBridge)2 ProDummyDelegateBridge (games.strategy.triplea.ai.pro.simulate.ProDummyDelegateBridge)2 GameStep (games.strategy.engine.data.GameStep)1 PlayerID (games.strategy.engine.data.PlayerID)1 Territory (games.strategy.engine.data.Territory)1 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)1 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)1 PoliticalActionAttachment (games.strategy.triplea.attachments.PoliticalActionAttachment)1 AbstractPlaceDelegate (games.strategy.triplea.delegate.AbstractPlaceDelegate)1 PoliticsDelegate (games.strategy.triplea.delegate.PoliticsDelegate)1 IMoveDelegate (games.strategy.triplea.delegate.remote.IMoveDelegate)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1