Search in sources :

Example 16 with RelationshipType

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

the class ProPoliticsAi method politicalActions.

List<PoliticalActionAttachment> politicalActions() {
    final GameData data = ProData.getData();
    final PlayerID player = ProData.getPlayer();
    final float numPlayers = data.getPlayerList().getPlayers().size();
    final double round = data.getSequence().getRound();
    final ProTerritoryManager territoryManager = new ProTerritoryManager(calc);
    final PoliticsDelegate politicsDelegate = DelegateFinder.politicsDelegate(data);
    ProLogger.info("Politics for " + player.getName());
    // Find valid war actions
    final List<PoliticalActionAttachment> actionChoicesTowardsWar = AiPoliticalUtils.getPoliticalActionsTowardsWar(player, politicsDelegate.getTestedConditions(), data);
    ProLogger.trace("War options: " + actionChoicesTowardsWar);
    final List<PoliticalActionAttachment> validWarActions = CollectionUtils.getMatches(actionChoicesTowardsWar, Matches.abstractUserActionAttachmentCanBeAttempted(politicsDelegate.getTestedConditions()));
    ProLogger.trace("Valid War options: " + validWarActions);
    // Divide war actions into enemy and neutral
    final Map<PoliticalActionAttachment, List<PlayerID>> enemyMap = new HashMap<>();
    final Map<PoliticalActionAttachment, List<PlayerID>> neutralMap = new HashMap<>();
    for (final PoliticalActionAttachment action : validWarActions) {
        final List<PlayerID> warPlayers = new ArrayList<>();
        for (final String relationshipChange : action.getRelationshipChange()) {
            final String[] s = relationshipChange.split(":");
            final PlayerID player1 = data.getPlayerList().getPlayerId(s[0]);
            final PlayerID player2 = data.getPlayerList().getPlayerId(s[1]);
            final RelationshipType oldRelation = data.getRelationshipTracker().getRelationshipType(player1, player2);
            final RelationshipType newRelation = data.getRelationshipTypeList().getRelationshipType(s[2]);
            if (!oldRelation.equals(newRelation) && Matches.relationshipTypeIsAtWar().test(newRelation) && (player1.equals(player) || player2.equals(player))) {
                PlayerID warPlayer = player2;
                if (warPlayer.equals(player)) {
                    warPlayer = player1;
                }
                warPlayers.add(warPlayer);
            }
        }
        if (!warPlayers.isEmpty()) {
            if (ProUtils.isNeutralPlayer(warPlayers.get(0))) {
                neutralMap.put(action, warPlayers);
            } else {
                enemyMap.put(action, warPlayers);
            }
        }
    }
    ProLogger.debug("Neutral options: " + neutralMap);
    ProLogger.debug("Enemy options: " + enemyMap);
    final List<PoliticalActionAttachment> results = new ArrayList<>();
    if (!enemyMap.isEmpty()) {
        // Find all attack options
        territoryManager.populatePotentialAttackOptions();
        final List<ProTerritory> attackOptions = territoryManager.removePotentialTerritoriesThatCantBeConquered();
        ProLogger.trace(player.getName() + ", numAttackOptions=" + attackOptions.size() + ", options=" + attackOptions);
        // Find attack options per war action
        final Map<PoliticalActionAttachment, Double> attackPercentageMap = new HashMap<>();
        for (final PoliticalActionAttachment action : enemyMap.keySet()) {
            int count = 0;
            final List<PlayerID> enemyPlayers = enemyMap.get(action);
            for (final ProTerritory patd : attackOptions) {
                if (Matches.isTerritoryOwnedBy(enemyPlayers).test(patd.getTerritory()) || Matches.territoryHasUnitsThatMatch(Matches.unitOwnedBy(enemyPlayers)).test(patd.getTerritory())) {
                    count++;
                }
            }
            final double attackPercentage = count / (attackOptions.size() + 1.0);
            attackPercentageMap.put(action, attackPercentage);
            ProLogger.trace(enemyPlayers + ", count=" + count + ", attackPercentage=" + attackPercentage);
        }
        // Decide whether to declare war on an enemy
        final List<PoliticalActionAttachment> options = new ArrayList<>(attackPercentageMap.keySet());
        Collections.shuffle(options);
        for (final PoliticalActionAttachment action : options) {
            // 0, .05, .1, .15, etc
            final double roundFactor = (round - 1) * .05;
            final double warChance = roundFactor + attackPercentageMap.get(action) * (1 + 10 * roundFactor);
            final double random = Math.random();
            ProLogger.trace(enemyMap.get(action) + ", warChance=" + warChance + ", random=" + random);
            if (random <= warChance) {
                results.add(action);
                ProLogger.debug("---Declared war on " + enemyMap.get(action));
                break;
            }
        }
    } else if (!neutralMap.isEmpty()) {
        // Decide whether to declare war on a neutral
        final List<PoliticalActionAttachment> options = new ArrayList<>(neutralMap.keySet());
        Collections.shuffle(options);
        final double random = Math.random();
        final double warChance = .01;
        ProLogger.debug("warChance=" + warChance + ", random=" + random);
        if (random <= warChance) {
            results.add(options.get(0));
            ProLogger.debug("Declared war on " + enemyMap.get(options.get(0)));
        }
    }
    // Old code used for non-war actions
    if (Math.random() < .5) {
        final List<PoliticalActionAttachment> actionChoicesOther = AiPoliticalUtils.getPoliticalActionsOther(player, politicsDelegate.getTestedConditions(), data);
        if (actionChoicesOther != null && !actionChoicesOther.isEmpty()) {
            Collections.shuffle(actionChoicesOther);
            int i = 0;
            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() > player.getResources().getQuantity(Constants.PUS)) {
                    continue;
                }
                i++;
                if (i > maxOtherActionsPerTurn) {
                    break;
                }
                results.add(action);
            }
        }
    }
    doActions(results);
    return results;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) HashMap(java.util.HashMap) ProTerritoryManager(games.strategy.triplea.ai.pro.data.ProTerritoryManager) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) ArrayList(java.util.ArrayList) RelationshipType(games.strategy.engine.data.RelationshipType) PoliticsDelegate(games.strategy.triplea.delegate.PoliticsDelegate) PoliticalActionAttachment(games.strategy.triplea.attachments.PoliticalActionAttachment) ArrayList(java.util.ArrayList) List(java.util.List)

Example 17 with RelationshipType

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

the class AiPoliticalUtils method goesTowardsWar.

// this code has a rare risk of circular loop actions.. depending on the map
// designer
// only switches from a Neutral to an War state... won't go through
// in-between neutral states
// TODO have another look at this part.
private static boolean goesTowardsWar(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().isNeutral() && newType.getRelationshipTypeAttachment().isWar()) {
                return true;
            }
        }
    }
    return false;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) RelationshipType(games.strategy.engine.data.RelationshipType)

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