Search in sources :

Example 6 with GameStep

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

the class ProUtils method getOtherPlayersInTurnOrder.

public static List<PlayerID> getOtherPlayersInTurnOrder(final PlayerID player) {
    final GameData data = ProData.getData();
    final List<PlayerID> players = new ArrayList<>();
    final GameSequence sequence = data.getSequence();
    final int startIndex = sequence.getStepIndex();
    for (int i = 0; i < sequence.size(); i++) {
        int currentIndex = startIndex + i;
        if (currentIndex >= sequence.size()) {
            currentIndex -= sequence.size();
        }
        final GameStep step = sequence.getStep(currentIndex);
        final PlayerID stepPlayer = step.getPlayerId();
        if (step.getName().endsWith("CombatMove") && stepPlayer != null && !stepPlayer.equals(player) && !players.contains(stepPlayer)) {
            players.add(step.getPlayerId());
        }
    }
    return players;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) GameStep(games.strategy.engine.data.GameStep) GameSequence(games.strategy.engine.data.GameSequence)

Example 7 with GameStep

use of games.strategy.engine.data.GameStep 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)

Example 8 with GameStep

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

the class FileMenu method addPostPbem.

private JMenuItem addPostPbem() {
    final JMenuItem menuPbem = new JMenuItem(SwingAction.of("Post PBEM/PBF Gamesave", e -> {
        if (!PBEMMessagePoster.gameDataHasPlayByEmailOrForumMessengers(gameData)) {
            return;
        }
        final String title = "Manual Gamesave Post";
        try {
            gameData.acquireReadLock();
            final GameStep step = gameData.getSequence().getStep();
            final PlayerID currentPlayer = (step == null ? PlayerID.NULL_PLAYERID : (step.getPlayerId() == null ? PlayerID.NULL_PLAYERID : step.getPlayerId()));
            final int round = gameData.getSequence().getRound();
            final HistoryLog historyLog = new HistoryLog();
            historyLog.printFullTurn(gameData, true, GameStepPropertiesHelper.getTurnSummaryPlayers(gameData));
            final PBEMMessagePoster poster = new PBEMMessagePoster(gameData, currentPlayer, round, title);
            PBEMMessagePoster.postTurn(title, historyLog, true, poster, null, frame, null);
        } finally {
            gameData.releaseReadLock();
        }
    }));
    menuPbem.setMnemonic(KeyEvent.VK_P);
    menuPbem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    return menuPbem;
}
Also used : KeyStroke(javax.swing.KeyStroke) SystemProperties(games.strategy.engine.framework.system.SystemProperties) GameStepPropertiesHelper(games.strategy.triplea.delegate.GameStepPropertiesHelper) JMenu(javax.swing.JMenu) HistoryLog(games.strategy.triplea.ui.history.HistoryLog) KeyEvent(java.awt.event.KeyEvent) JOptionPane(javax.swing.JOptionPane) File(java.io.File) TripleAFrame(games.strategy.triplea.ui.TripleAFrame) GameData(games.strategy.engine.data.GameData) PlayerID(games.strategy.engine.data.PlayerID) JMenuItem(javax.swing.JMenuItem) MacQuitMenuWrapper(games.strategy.triplea.ui.MacQuitMenuWrapper) IGame(games.strategy.engine.framework.IGame) GameStep(games.strategy.engine.data.GameStep) PBEMMessagePoster(games.strategy.engine.pbem.PBEMMessagePoster) Toolkit(java.awt.Toolkit) SwingAction(games.strategy.ui.SwingAction) PlayerID(games.strategy.engine.data.PlayerID) GameStep(games.strategy.engine.data.GameStep) HistoryLog(games.strategy.triplea.ui.history.HistoryLog) JMenuItem(javax.swing.JMenuItem) PBEMMessagePoster(games.strategy.engine.pbem.PBEMMessagePoster)

Example 9 with GameStep

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

the class PlayerOrderComparator method compare.

/**
 * sort based on first step that isn't a bid related step.
 */
@Override
public int compare(final PlayerID p1, final PlayerID p2) {
    if (p1.equals(p2)) {
        return 0;
    }
    gameData.acquireReadLock();
    final GameSequence sequence;
    try {
        sequence = gameData.getSequence();
    } finally {
        gameData.releaseReadLock();
    }
    for (final GameStep s : sequence) {
        if (s.getPlayerId() == null) {
            continue;
        }
        gameData.acquireReadLock();
        final IDelegate delegate;
        try {
            delegate = s.getDelegate();
        } finally {
            gameData.releaseReadLock();
        }
        if (delegate != null && delegate.getClass() != null) {
            final String delegateClassName = delegate.getClass().getName();
            if (delegateClassName.equals("games.strategy.triplea.delegate.InitializationDelegate") || delegateClassName.equals("games.strategy.triplea.delegate.BidPurchaseDelegate") || delegateClassName.equals("games.strategy.triplea.delegate.BidPlaceDelegate") || delegateClassName.equals("games.strategy.triplea.delegate.EndRoundDelegate")) {
                continue;
            }
        } else if (s.getName() != null && (s.getName().endsWith("Bid") || s.getName().endsWith("BidPlace"))) {
            continue;
        }
        if (s.getPlayerId().equals(p1)) {
            return -1;
        } else if (s.getPlayerId().equals(p2)) {
            return 1;
        }
    }
    return 0;
}
Also used : GameStep(games.strategy.engine.data.GameStep) IDelegate(games.strategy.engine.delegate.IDelegate) GameSequence(games.strategy.engine.data.GameSequence)

Aggregations

GameStep (games.strategy.engine.data.GameStep)9 PlayerID (games.strategy.engine.data.PlayerID)5 GameData (games.strategy.engine.data.GameData)4 GameSequence (games.strategy.engine.data.GameSequence)2 Territory (games.strategy.engine.data.Territory)2 IDelegate (games.strategy.engine.delegate.IDelegate)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Resource (games.strategy.engine.data.Resource)1 AutoSave (games.strategy.engine.delegate.AutoSave)1 IDelegateBridge (games.strategy.engine.delegate.IDelegateBridge)1 IGame (games.strategy.engine.framework.IGame)1 SystemProperties (games.strategy.engine.framework.system.SystemProperties)1 PBEMMessagePoster (games.strategy.engine.pbem.PBEMMessagePoster)1 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)1 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)1 ProDummyDelegateBridge (games.strategy.triplea.ai.pro.simulate.ProDummyDelegateBridge)1 PoliticalActionAttachment (games.strategy.triplea.attachments.PoliticalActionAttachment)1 BidPlaceDelegate (games.strategy.triplea.delegate.BidPlaceDelegate)1 GameStepPropertiesHelper (games.strategy.triplea.delegate.GameStepPropertiesHelper)1