Search in sources :

Example 1 with IPoliticsDelegate

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

the class TripleAPlayer method politics.

private void politics(final boolean firstRun) {
    if (getPlayerBridge().isGameOver()) {
        return;
    }
    final IPoliticsDelegate politicsDelegate;
    try {
        politicsDelegate = (IPoliticsDelegate) getPlayerBridge().getRemoteDelegate();
    } catch (final ClassCastException e) {
        final String errorContext = "PlayerBridge step name: " + getPlayerBridge().getStepName() + ", Remote class name: " + getPlayerBridge().getRemoteDelegate().getClass();
        ClientLogger.logQuietly(errorContext, e);
        throw new IllegalStateException(errorContext, e);
    }
    final PoliticalActionAttachment actionChoice = ui.getPoliticalActionChoice(getPlayerId(), firstRun, politicsDelegate);
    if (actionChoice != null) {
        politicsDelegate.attemptAction(actionChoice);
        politics(false);
    }
}
Also used : PoliticalActionAttachment(games.strategy.triplea.attachments.PoliticalActionAttachment) IPoliticsDelegate(games.strategy.triplea.delegate.remote.IPoliticsDelegate)

Example 2 with IPoliticsDelegate

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

the class AbstractAi method politicalActions.

protected void politicalActions() {
    final IPoliticsDelegate remotePoliticsDelegate = (IPoliticsDelegate) getPlayerBridge().getRemoteDelegate();
    final GameData data = getGameData();
    final PlayerID id = getPlayerId();
    final float numPlayers = data.getPlayerList().getPlayers().size();
    final PoliticsDelegate politicsDelegate = DelegateFinder.politicsDelegate(data);
    // We want to test the conditions each time to make sure they are still valid
    if (Math.random() < .5) {
        final List<PoliticalActionAttachment> actionChoicesTowardsWar = AiPoliticalUtils.getPoliticalActionsTowardsWar(id, politicsDelegate.getTestedConditions(), data);
        if (actionChoicesTowardsWar != null && !actionChoicesTowardsWar.isEmpty()) {
            Collections.shuffle(actionChoicesTowardsWar);
            int i = 0;
            // should we use bridge's random source here?
            final double random = Math.random();
            int maxWarActionsPerTurn = (random < .5 ? 0 : (random < .9 ? 1 : (random < .99 ? 2 : (int) numPlayers / 2)));
            if ((maxWarActionsPerTurn > 0) && (CollectionUtils.countMatches(data.getRelationshipTracker().getRelationships(id), Matches.relationshipIsAtWar())) / numPlayers < 0.4) {
                if (Math.random() < .9) {
                    maxWarActionsPerTurn = 0;
                } else {
                    maxWarActionsPerTurn = 1;
                }
            }
            final Iterator<PoliticalActionAttachment> actionWarIter = actionChoicesTowardsWar.iterator();
            while (actionWarIter.hasNext() && maxWarActionsPerTurn > 0) {
                final PoliticalActionAttachment action = actionWarIter.next();
                if (!Matches.abstractUserActionAttachmentCanBeAttempted(politicsDelegate.getTestedConditions()).test(action)) {
                    continue;
                }
                i++;
                if (i > maxWarActionsPerTurn) {
                    break;
                }
                remotePoliticsDelegate.attemptAction(action);
            }
        }
    } else {
        final List<PoliticalActionAttachment> actionChoicesOther = AiPoliticalUtils.getPoliticalActionsOther(id, politicsDelegate.getTestedConditions(), data);
        if (actionChoicesOther != null && !actionChoicesOther.isEmpty()) {
            Collections.shuffle(actionChoicesOther);
            int i = 0;
            // should we use bridge's random source here?
            final double random = Math.random();
            final int maxOtherActionsPerTurn = (random < .3 ? 0 : (random < .6 ? 1 : (random < .9 ? 2 : (random < .99 ? 3 : (int) numPlayers))));
            final Iterator<PoliticalActionAttachment> actionOtherIter = actionChoicesOther.iterator();
            while (actionOtherIter.hasNext() && maxOtherActionsPerTurn > 0) {
                final PoliticalActionAttachment action = actionOtherIter.next();
                if (!Matches.abstractUserActionAttachmentCanBeAttempted(politicsDelegate.getTestedConditions()).test(action)) {
                    continue;
                }
                if (action.getCostPu() > 0 && action.getCostPu() > id.getResources().getQuantity(Constants.PUS)) {
                    continue;
                }
                i++;
                if (i > maxOtherActionsPerTurn) {
                    break;
                }
                remotePoliticsDelegate.attemptAction(action);
            }
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) PoliticalActionAttachment(games.strategy.triplea.attachments.PoliticalActionAttachment) GameData(games.strategy.engine.data.GameData) IPoliticsDelegate(games.strategy.triplea.delegate.remote.IPoliticsDelegate) IPoliticsDelegate(games.strategy.triplea.delegate.remote.IPoliticsDelegate) PoliticsDelegate(games.strategy.triplea.delegate.PoliticsDelegate)

Aggregations

PoliticalActionAttachment (games.strategy.triplea.attachments.PoliticalActionAttachment)2 IPoliticsDelegate (games.strategy.triplea.delegate.remote.IPoliticsDelegate)2 GameData (games.strategy.engine.data.GameData)1 PlayerID (games.strategy.engine.data.PlayerID)1 PoliticsDelegate (games.strategy.triplea.delegate.PoliticsDelegate)1