Search in sources :

Example 1 with PoliticalActionAttachment

use of games.strategy.triplea.attachments.PoliticalActionAttachment in project triplea by triplea-game.

the class PoliticsDelegate method actionIsAccepted.

/**
 * Get a list of players that should accept this action and then ask each
 * player if it accepts this action.
 *
 * @param paa
 *        the politicalActionAttachment that should be accepted
 */
private boolean actionIsAccepted(final PoliticalActionAttachment paa) {
    final GameData data = getData();
    final Predicate<PoliticalActionAttachment> intoAlliedChainOrIntoOrOutOfWar = Matches.politicalActionIsRelationshipChangeOf(null, Matches.relationshipTypeIsAlliedAndAlliancesCanChainTogether().negate(), Matches.relationshipTypeIsAlliedAndAlliancesCanChainTogether(), data).or(Matches.politicalActionIsRelationshipChangeOf(null, Matches.relationshipTypeIsAtWar().negate(), Matches.relationshipTypeIsAtWar(), data)).or(Matches.politicalActionIsRelationshipChangeOf(null, Matches.relationshipTypeIsAtWar(), Matches.relationshipTypeIsAtWar().negate(), data));
    if (!Properties.getAlliancesCanChainTogether(data) || !intoAlliedChainOrIntoOrOutOfWar.test(paa)) {
        for (final PlayerID player : paa.getActionAccept()) {
            if (!(getRemotePlayer(player)).acceptAction(this.player, PoliticsText.getInstance().getAcceptanceQuestion(paa.getText()), true)) {
                return false;
            }
        }
    } else {
        // if alliances chain together, then our allies must have a say in anyone becoming a new ally/enemy
        final LinkedHashSet<PlayerID> playersWhoNeedToAccept = new LinkedHashSet<>();
        playersWhoNeedToAccept.addAll(paa.getActionAccept());
        playersWhoNeedToAccept.addAll(CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAlliedAndAlliancesCanChainTogether(player, data)));
        for (final PlayerID player : paa.getActionAccept()) {
            playersWhoNeedToAccept.addAll(CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAlliedAndAlliancesCanChainTogether(player, data)));
        }
        playersWhoNeedToAccept.removeAll(paa.getActionAccept());
        for (final PlayerID player : playersWhoNeedToAccept) {
            String actionText = PoliticsText.getInstance().getAcceptanceQuestion(paa.getText());
            if (actionText.equals("NONE")) {
                actionText = this.player.getName() + " wants to take the following action: " + MyFormatter.attachmentNameToText(paa.getName()) + " \r\n Do you approve?";
            } else {
                actionText = this.player.getName() + " wants to take the following action: " + MyFormatter.attachmentNameToText(paa.getName()) + ".  Do you approve? \r\n\r\n " + this.player.getName() + " will ask " + MyFormatter.defaultNamedToTextList(paa.getActionAccept()) + ", the following question: \r\n " + actionText;
            }
            if (!(getRemotePlayer(player)).acceptAction(this.player, actionText, true)) {
                return false;
            }
        }
        for (final PlayerID player : paa.getActionAccept()) {
            if (!(getRemotePlayer(player)).acceptAction(this.player, PoliticsText.getInstance().getAcceptanceQuestion(paa.getText()), true)) {
                return false;
            }
        }
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PlayerID(games.strategy.engine.data.PlayerID) PoliticalActionAttachment(games.strategy.triplea.attachments.PoliticalActionAttachment) GameData(games.strategy.engine.data.GameData)

Example 2 with PoliticalActionAttachment

use of games.strategy.triplea.attachments.PoliticalActionAttachment 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 3 with PoliticalActionAttachment

use of games.strategy.triplea.attachments.PoliticalActionAttachment 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)

Example 4 with PoliticalActionAttachment

use of games.strategy.triplea.attachments.PoliticalActionAttachment in project triplea by triplea-game.

the class AiPoliticalUtils method getPoliticalActionsOther.

public static List<PoliticalActionAttachment> getPoliticalActionsOther(final PlayerID id, final HashMap<ICondition, Boolean> testedConditions, final GameData data) {
    final List<PoliticalActionAttachment> warActions = getPoliticalActionsTowardsWar(id, testedConditions, data);
    final Collection<PoliticalActionAttachment> validActions = PoliticalActionAttachment.getValidActions(id, testedConditions, data);
    validActions.removeAll(warActions);
    final List<PoliticalActionAttachment> acceptableActions = new ArrayList<>();
    for (final PoliticalActionAttachment nextAction : validActions) {
        if (warActions.contains(nextAction)) {
            continue;
        }
        if (goesTowardsWar(nextAction, id, data) && Math.random() < .5) {
            continue;
        }
        if (awayFromAlly(nextAction, id, data) && Math.random() < .9) {
            continue;
        }
        if (isFree(nextAction)) {
            acceptableActions.add(nextAction);
        } else if (CollectionUtils.countMatches(validActions, Matches.politicalActionHasCostBetween(0, 0)) > 1) {
            if (Math.random() < .9 && isAcceptableCost(nextAction, id, data)) {
                acceptableActions.add(nextAction);
            }
        } else {
            if (Math.random() < .4 && isAcceptableCost(nextAction, id, data)) {
                acceptableActions.add(nextAction);
            }
        }
    }
    return acceptableActions;
}
Also used : PoliticalActionAttachment(games.strategy.triplea.attachments.PoliticalActionAttachment) ArrayList(java.util.ArrayList)

Example 5 with PoliticalActionAttachment

use of games.strategy.triplea.attachments.PoliticalActionAttachment in project triplea by triplea-game.

the class PoliticsPanel method politicalActionButtonPanel.

private JPanel politicalActionButtonPanel(final JDialog parent) {
    final JPanel politicalActionButtonPanel = new JPanel();
    politicalActionButtonPanel.setLayout(new GridBagLayout());
    int row = 0;
    final Insets insets = new Insets(1, 1, 1, 1);
    for (final PoliticalActionAttachment paa : validPoliticalActions) {
        politicalActionButtonPanel.add(getOtherPlayerFlags(paa), new GridBagConstraints(0, row, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
        final JButton button = new JButton(getActionButtonText(paa));
        button.addActionListener(ae -> {
            selectPoliticalActionButton.setEnabled(false);
            doneButton.setEnabled(false);
            validPoliticalActions = null;
            choice = paa;
            parent.setVisible(false);
            release();
        });
        politicalActionButtonPanel.add(button, new GridBagConstraints(1, row, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
        politicalActionButtonPanel.add(getActionDescriptionLabel(paa), new GridBagConstraints(2, row, 1, 1, 5.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
        row++;
    }
    return politicalActionButtonPanel;
}
Also used : JPanel(javax.swing.JPanel) PoliticalActionAttachment(games.strategy.triplea.attachments.PoliticalActionAttachment) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) JButton(javax.swing.JButton)

Aggregations

PoliticalActionAttachment (games.strategy.triplea.attachments.PoliticalActionAttachment)8 GameData (games.strategy.engine.data.GameData)5 PlayerID (games.strategy.engine.data.PlayerID)4 PoliticsDelegate (games.strategy.triplea.delegate.PoliticsDelegate)4 ArrayList (java.util.ArrayList)3 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)2 IPoliticsDelegate (games.strategy.triplea.delegate.remote.IPoliticsDelegate)2 HashMap (java.util.HashMap)2 GameStep (games.strategy.engine.data.GameStep)1 RelationshipType (games.strategy.engine.data.RelationshipType)1 Territory (games.strategy.engine.data.Territory)1 IDelegateBridge (games.strategy.engine.delegate.IDelegateBridge)1 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)1 ProTerritoryManager (games.strategy.triplea.ai.pro.data.ProTerritoryManager)1 ProDummyDelegateBridge (games.strategy.triplea.ai.pro.simulate.ProDummyDelegateBridge)1 IMoveDelegate (games.strategy.triplea.delegate.remote.IMoveDelegate)1 GridBagConstraints (java.awt.GridBagConstraints)1 GridBagLayout (java.awt.GridBagLayout)1 Insets (java.awt.Insets)1 LinkedHashSet (java.util.LinkedHashSet)1