Search in sources :

Example 56 with PlayerID

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

the class TripleAPlayer method tech.

private void tech() {
    if (getPlayerBridge().isGameOver()) {
        return;
    }
    final ITechDelegate techDelegate;
    try {
        techDelegate = (ITechDelegate) getPlayerBridge().getRemoteDelegate();
    } catch (final ClassCastException e) {
        final String errorContext = "PlayerBridge step name: " + getPlayerBridge().getStepName() + ", Remote class name: " + getPlayerBridge().getRemoteDelegate().getClass();
        // for some reason the client is not seeing or getting these errors, so print to err too
        System.err.println(errorContext);
        ClientLogger.logQuietly(errorContext, e);
        throw new IllegalStateException(errorContext, e);
    }
    final PlayerID id = getPlayerId();
    if (!soundPlayedAlreadyTechnology) {
        ClipPlayer.play(SoundPath.CLIP_PHASE_TECHNOLOGY, id);
        soundPlayedAlreadyTechnology = true;
    }
    final TechRoll techRoll = ui.getTechRolls(id);
    if (techRoll != null) {
        final TechResults techResults = techDelegate.rollTech(techRoll.getRolls(), techRoll.getTech(), techRoll.getNewTokens(), techRoll.getWhoPaysHowMuch());
        if (techResults.isError()) {
            ui.notifyError(techResults.getErrorString());
            tech();
        } else {
            ui.notifyTechResults(techResults);
        }
    }
}
Also used : ITechDelegate(games.strategy.triplea.delegate.remote.ITechDelegate) PlayerID(games.strategy.engine.data.PlayerID) TechResults(games.strategy.triplea.delegate.dataObjects.TechResults) TechRoll(games.strategy.triplea.delegate.dataObjects.TechRoll)

Example 57 with PlayerID

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

the class TripleAPlayer method purchase.

private void purchase(final boolean bid) {
    if (getPlayerBridge().isGameOver()) {
        return;
    }
    final PlayerID id = getPlayerId();
    // play a sound for this phase
    if (!bid && !soundPlayedAlreadyPurchase) {
        ClipPlayer.play(SoundPath.CLIP_PHASE_PURCHASE, id);
        soundPlayedAlreadyPurchase = true;
    }
    // Check if any factories need to be repaired
    if (id.getRepairFrontier() != null && id.getRepairFrontier().getRules() != null && !id.getRepairFrontier().getRules().isEmpty()) {
        final GameData data = getGameData();
        if (isDamageFromBombingDoneToUnitsInsteadOfTerritories(data)) {
            final Predicate<Unit> myDamaged = Matches.unitIsOwnedBy(id).and(Matches.unitHasTakenSomeBombingUnitDamage());
            final Collection<Unit> damagedUnits = new ArrayList<>();
            for (final Territory t : data.getMap().getTerritories()) {
                damagedUnits.addAll(CollectionUtils.getMatches(t.getUnits().getUnits(), myDamaged));
            }
            if (damagedUnits.size() > 0) {
                final HashMap<Unit, IntegerMap<RepairRule>> repair = ui.getRepair(id, bid, GameStepPropertiesHelper.getRepairPlayers(data, id));
                if (repair != null) {
                    final IPurchaseDelegate purchaseDel;
                    try {
                        purchaseDel = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
                    } catch (final ClassCastException e) {
                        final String errorContext = "PlayerBridge step name: " + getPlayerBridge().getStepName() + ", Remote class name: " + getPlayerBridge().getRemoteDelegate().getClass();
                        // for some reason the client is not seeing or getting these errors, so print to err too
                        System.err.println(errorContext);
                        ClientLogger.logQuietly(errorContext, e);
                        throw new IllegalStateException(errorContext, e);
                    }
                    final String error = purchaseDel.purchaseRepair(repair);
                    if (error != null) {
                        ui.notifyError(error);
                        // dont give up, keep going
                        purchase(bid);
                    }
                }
            }
        }
    }
    final IntegerMap<ProductionRule> prod = ui.getProduction(id, bid);
    if (prod == null) {
        return;
    }
    final IPurchaseDelegate purchaseDel;
    try {
        purchaseDel = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
    } catch (final ClassCastException e) {
        final String errorContext = "PlayerBridge step name: " + getPlayerBridge().getStepName() + ", Remote class name: " + getPlayerBridge().getRemoteDelegate().getClass();
        // for some reason the client is not seeing or getting these errors, so print to err too
        System.err.println(errorContext);
        ClientLogger.logQuietly(errorContext, e);
        throw new IllegalStateException(errorContext, e);
    }
    final String purchaseError = purchaseDel.purchase(prod);
    if (purchaseError != null) {
        ui.notifyError(purchaseError);
        // dont give up, keep going
        purchase(bid);
    }
}
Also used : IntegerMap(games.strategy.util.IntegerMap) PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit) IPurchaseDelegate(games.strategy.triplea.delegate.remote.IPurchaseDelegate) ProductionRule(games.strategy.engine.data.ProductionRule)

Example 58 with PlayerID

use of games.strategy.engine.data.PlayerID 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 59 with PlayerID

use of games.strategy.engine.data.PlayerID 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 60 with PlayerID

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

the class RulesAttachment method setAtWarPlayers.

private void setAtWarPlayers(final String players) throws GameParseException {
    if (players == null) {
        m_atWarPlayers = null;
        return;
    }
    final String[] s = players.split(":");
    if (s.length < 1) {
        throw new GameParseException("Empty enemy list" + thisErrorMsg());
    }
    int count = -1;
    try {
        count = getInt(s[0]);
        m_atWarCount = count;
    } catch (final Exception e) {
        m_atWarCount = 0;
    }
    if (s.length < 1 || (s.length == 1 && count != -1)) {
        throw new GameParseException("Empty enemy list" + thisErrorMsg());
    }
    m_atWarPlayers = new HashSet<>();
    for (int i = count == -1 ? 0 : 1; i < s.length; i++) {
        final PlayerID player = getData().getPlayerList().getPlayerId(s[i]);
        if (player == null) {
            throw new GameParseException("Could not find player. name:" + s[i] + thisErrorMsg());
        }
        m_atWarPlayers.add(player);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameParseException(games.strategy.engine.data.GameParseException) GameParseException(games.strategy.engine.data.GameParseException)

Aggregations

PlayerID (games.strategy.engine.data.PlayerID)323 Territory (games.strategy.engine.data.Territory)163 Unit (games.strategy.engine.data.Unit)133 Test (org.junit.jupiter.api.Test)122 TripleAUnit (games.strategy.triplea.TripleAUnit)104 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)94 GameData (games.strategy.engine.data.GameData)90 ArrayList (java.util.ArrayList)79 UnitType (games.strategy.engine.data.UnitType)74 Route (games.strategy.engine.data.Route)67 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)46 HashSet (java.util.HashSet)44 Change (games.strategy.engine.data.Change)29 CompositeChange (games.strategy.engine.data.CompositeChange)29 IntegerMap (games.strategy.util.IntegerMap)29 List (java.util.List)29 HashMap (java.util.HashMap)28 Collection (java.util.Collection)27 Resource (games.strategy.engine.data.Resource)25 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)23