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;
}
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);
}
}
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);
}
}
}
}
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;
}
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;
}
Aggregations