Search in sources :

Example 6 with RelationshipType

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

the class AiPoliticalUtils method awayFromAlly.

private static boolean awayFromAlly(final PoliticalActionAttachment nextAction, final PlayerID p0, final GameData data) {
    for (final String relationshipChangeString : nextAction.getRelationshipChange()) {
        final String[] relationshipChange = relationshipChangeString.split(":");
        final PlayerID p1 = data.getPlayerList().getPlayerId(relationshipChange[0]);
        final PlayerID p2 = data.getPlayerList().getPlayerId(relationshipChange[1]);
        // only continue if p1 or p2 is the AI
        if (p0.equals(p1) || p0.equals(p2)) {
            final RelationshipType currentType = data.getRelationshipTracker().getRelationshipType(p1, p2);
            final RelationshipType newType = data.getRelationshipTypeList().getRelationshipType(relationshipChange[2]);
            if (currentType.getRelationshipTypeAttachment().isAllied() && (newType.getRelationshipTypeAttachment().isNeutral() || newType.getRelationshipTypeAttachment().isWar())) {
                return true;
            }
        }
    }
    return false;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) RelationshipType(games.strategy.engine.data.RelationshipType)

Example 7 with RelationshipType

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

the class PoliticalStateOverview method getRelationshipLabel.

/**
 * Gets a label showing the coloured relationshipName between these two
 * players.
 */
private JPanel getRelationshipLabel(final PlayerID player1, final PlayerID player2) {
    RelationshipType relType = null;
    for (final Triple<PlayerID, PlayerID, RelationshipType> changesSoFar : editChanges) {
        if ((player1.equals(changesSoFar.getFirst()) && player2.equals(changesSoFar.getSecond())) || (player2.equals(changesSoFar.getFirst()) && player1.equals(changesSoFar.getSecond()))) {
            relType = changesSoFar.getThird();
        }
    }
    if (relType == null) {
        data.acquireReadLock();
        try {
            relType = data.getRelationshipTracker().getRelationshipType(player1, player2);
        } finally {
            data.releaseReadLock();
        }
    }
    final JComponent relationshipLabel = getRelationshipComponent(player1, player2, relType);
    final JPanel relationshipLabelPanel = new JPanel();
    relationshipLabelPanel.add(relationshipLabel);
    relationshipLabelPanel.setBackground(getRelationshipTypeColor(relType));
    return relationshipLabelPanel;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) JPanel(javax.swing.JPanel) RelationshipType(games.strategy.engine.data.RelationshipType) JComponent(javax.swing.JComponent)

Example 8 with RelationshipType

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

the class PoliticalStateOverview method getRelationshipComponent.

private JComponent getRelationshipComponent(final PlayerID player1, final PlayerID player2, final RelationshipType relType) {
    if (!editable) {
        return new JLabel(relType.getName());
    }
    final JButton button = new JButton(relType.getName());
    button.addActionListener(e -> {
        final List<RelationshipType> types = new ArrayList<>(data.getRelationshipTypeList().getAllRelationshipTypes());
        types.remove(data.getRelationshipTypeList().getNullRelation());
        types.remove(data.getRelationshipTypeList().getSelfRelation());
        final Object[] possibilities = types.toArray();
        final RelationshipType chosenRelationship = (RelationshipType) JOptionPane.showInputDialog(PoliticalStateOverview.this, "Change Current Relationship between " + player1.getName() + " and " + player2.getName(), "Change Current Relationship", JOptionPane.PLAIN_MESSAGE, null, possibilities, relType);
        if (chosenRelationship != null) {
            // remove any old ones
            editChanges.removeIf(changesSoFar -> (player1.equals(changesSoFar.getFirst()) && player2.equals(changesSoFar.getSecond())) || (player2.equals(changesSoFar.getFirst()) && player1.equals(changesSoFar.getSecond())));
            // see if there is actually a change
            data.acquireReadLock();
            final RelationshipType actualRelationship;
            try {
                actualRelationship = data.getRelationshipTracker().getRelationshipType(player1, player2);
            } finally {
                data.releaseReadLock();
            }
            if (!chosenRelationship.equals(actualRelationship)) {
                // add new change
                editChanges.add(Triple.of(player1, player2, chosenRelationship));
            }
            // redraw everything
            redrawPolitics();
        }
    });
    button.setBackground(getRelationshipTypeColor(relType));
    return button;
}
Also used : JButton(javax.swing.JButton) RelationshipType(games.strategy.engine.data.RelationshipType) ArrayList(java.util.ArrayList) JLabel(javax.swing.JLabel)

Example 9 with RelationshipType

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

the class PoliticsDelegate method changeRelationships.

/**
 * Changes all relationships.
 *
 * @param paa
 *        the political action to change the relationships for
 */
private void changeRelationships(final PoliticalActionAttachment paa) {
    getMyselfOutOfAlliance(paa, player, bridge);
    getNeutralOutOfWarWithAllies(paa, player, bridge);
    final CompositeChange change = new CompositeChange();
    for (final String relationshipChange : paa.getRelationshipChange()) {
        final String[] s = relationshipChange.split(":");
        final PlayerID player1 = getData().getPlayerList().getPlayerId(s[0]);
        final PlayerID player2 = getData().getPlayerList().getPlayerId(s[1]);
        final RelationshipType oldRelation = getData().getRelationshipTracker().getRelationshipType(player1, player2);
        final RelationshipType newRelation = getData().getRelationshipTypeList().getRelationshipType(s[2]);
        if (oldRelation.equals(newRelation)) {
            continue;
        }
        change.add(ChangeFactory.relationshipChange(player1, player2, oldRelation, newRelation));
        bridge.getHistoryWriter().addChildToEvent(bridge.getPlayerId().getName() + " succeeds on action: " + MyFormatter.attachmentNameToText(paa.getName()) + ": Changing Relationship for " + player1.getName() + " and " + player2.getName() + " from " + oldRelation.getName() + " to " + newRelation.getName());
        MoveDelegate.getBattleTracker(getData()).addRelationshipChangesThisTurn(player1, player2, oldRelation, newRelation);
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
    chainAlliancesTogether(bridge);
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) RelationshipType(games.strategy.engine.data.RelationshipType) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 10 with RelationshipType

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

the class GameDataExporter method relationshipInitialize.

private void relationshipInitialize(final GameData data) {
    if (data.getRelationshipTypeList().getAllRelationshipTypes().size() <= 4) {
        return;
    }
    final RelationshipTracker rt = data.getRelationshipTracker();
    xmlfile.append("        <relationshipInitialize>\n");
    final Collection<PlayerID> players = data.getPlayerList().getPlayers();
    final Collection<PlayerID> playersAlreadyDone = new HashSet<>();
    for (final PlayerID p1 : players) {
        for (final PlayerID p2 : players) {
            if (p1.equals(p2) || playersAlreadyDone.contains(p2)) {
                continue;
            }
            final RelationshipType type = rt.getRelationshipType(p1, p2);
            final int roundValue = rt.getRoundRelationshipWasCreated(p1, p2);
            xmlfile.append("            <relationship type=\"").append(type.getName()).append("\" player1=\"").append(p1.getName()).append("\" player2=\"").append(p2.getName()).append("\" roundValue=\"").append(roundValue).append("\"/>\n");
        }
        playersAlreadyDone.add(p1);
    }
    xmlfile.append("        </relationshipInitialize>\n");
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) RelationshipType(games.strategy.engine.data.RelationshipType) RelationshipTracker(games.strategy.engine.data.RelationshipTracker) HashSet(java.util.HashSet)

Aggregations

RelationshipType (games.strategy.engine.data.RelationshipType)17 PlayerID (games.strategy.engine.data.PlayerID)13 GameData (games.strategy.engine.data.GameData)6 CompositeChange (games.strategy.engine.data.CompositeChange)5 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 GameParseException (games.strategy.engine.data.GameParseException)1 RelationshipTracker (games.strategy.engine.data.RelationshipTracker)1 Relationship (games.strategy.engine.data.RelationshipTracker.Relationship)1 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)1 ProTerritoryManager (games.strategy.triplea.ai.pro.data.ProTerritoryManager)1 PoliticalActionAttachment (games.strategy.triplea.attachments.PoliticalActionAttachment)1 PoliticsDelegate (games.strategy.triplea.delegate.PoliticsDelegate)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 JButton (javax.swing.JButton)1 JComponent (javax.swing.JComponent)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1