Search in sources :

Example 1 with IMoveDelegate

use of games.strategy.triplea.delegate.remote.IMoveDelegate in project triplea by triplea-game.

the class WeakAi method movePlanesHomeNonCom.

private void movePlanesHomeNonCom(final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final PlayerID player, final GameData data) {
    // the preferred way to get the delegate
    final IMoveDelegate delegateRemote = (IMoveDelegate) getPlayerBridge().getRemoteDelegate();
    // this works because we are on the server
    final BattleDelegate delegate = DelegateFinder.battleDelegate(data);
    final Predicate<Territory> canLand = Matches.isTerritoryAllied(player, data).and(o -> !delegate.getBattleTracker().wasConquered(o));
    final Predicate<Territory> routeCondition = Matches.territoryHasEnemyAaForCombatOnly(player, data).negate().and(Matches.territoryIsImpassable().negate());
    for (final Territory t : delegateRemote.getTerritoriesWhereAirCantLand()) {
        final Route noAaRoute = Utils.findNearest(t, canLand, routeCondition, data);
        final Route aaRoute = Utils.findNearest(t, canLand, Matches.territoryIsImpassable().negate(), data);
        final Collection<Unit> airToLand = t.getUnits().getMatches(Matches.unitIsAir().and(Matches.unitIsOwnedBy(player)));
        // dont bother to see if all the air units have enough movement points
        // to move without aa guns firing
        // simply move first over no aa, then with aa
        // one (but hopefully not both) will be rejected
        moveUnits.add(airToLand);
        moveRoutes.add(noAaRoute);
        moveUnits.add(airToLand);
        moveRoutes.add(aaRoute);
    }
}
Also used : IMoveDelegate(games.strategy.triplea.delegate.remote.IMoveDelegate) BattleDelegate(games.strategy.triplea.delegate.BattleDelegate) Territory(games.strategy.engine.data.Territory) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route)

Example 2 with IMoveDelegate

use of games.strategy.triplea.delegate.remote.IMoveDelegate in project triplea by triplea-game.

the class ProAi method purchase.

@Override
protected void purchase(final boolean purchaseForBid, final int pusToSpend, final IPurchaseDelegate purchaseDelegate, final GameData data, final PlayerID player) {
    final long start = System.currentTimeMillis();
    BattleCalculator.clearOolCache();
    ProLogUi.notifyStartOfRound(data.getSequence().getRound(), player.getName());
    initializeData();
    if (pusToSpend <= 0) {
        return;
    }
    if (purchaseForBid) {
        calc.setData(data);
        storedPurchaseTerritories = purchaseAi.bid(pusToSpend, purchaseDelegate, data);
    } else {
        // Repair factories
        purchaseAi.repair(pusToSpend, purchaseDelegate, data, player);
        // Check if any place territories exist
        final Map<Territory, ProPurchaseTerritory> purchaseTerritories = ProPurchaseUtils.findPurchaseTerritories(player);
        final List<Territory> possibleFactoryTerritories = CollectionUtils.getMatches(data.getMap().getTerritories(), ProMatches.territoryHasNoInfraFactoryAndIsNotConqueredOwnedLand(player, data));
        if (purchaseTerritories.isEmpty() && possibleFactoryTerritories.isEmpty()) {
            ProLogger.info("No possible place or factory territories owned so exiting purchase logic");
            return;
        }
        ProLogger.info("Starting simulation for purchase phase");
        // Setup data copy and delegates
        GameData dataCopy;
        try {
            data.acquireReadLock();
            dataCopy = GameDataUtils.cloneGameData(data, true);
        } catch (final Throwable t) {
            ProLogger.log(Level.WARNING, "Error trying to clone game data for simulating phases", t);
            return;
        } finally {
            data.releaseReadLock();
        }
        calc.setData(dataCopy);
        final PlayerID playerCopy = dataCopy.getPlayerList().getPlayerId(player.getName());
        final IMoveDelegate moveDel = DelegateFinder.moveDelegate(dataCopy);
        final IDelegateBridge bridge = new ProDummyDelegateBridge(this, playerCopy, dataCopy);
        moveDel.setDelegateBridgeAndPlayer(bridge);
        // Determine turn sequence
        final List<GameStep> gameSteps = new ArrayList<>();
        for (final GameStep gameStep : dataCopy.getSequence()) {
            gameSteps.add(gameStep);
        }
        // Simulate the next phases until place/end of turn is reached then use simulated data for purchase
        final int nextStepIndex = dataCopy.getSequence().getStepIndex() + 1;
        for (int i = nextStepIndex; i < gameSteps.size(); i++) {
            final GameStep step = gameSteps.get(i);
            if (!playerCopy.equals(step.getPlayerId())) {
                continue;
            }
            dataCopy.getSequence().setRoundAndStep(dataCopy.getSequence().getRound(), step.getDisplayName(), step.getPlayerId());
            final String stepName = step.getName();
            ProLogger.info("Simulating phase: " + stepName);
            if (stepName.endsWith("NonCombatMove")) {
                ProData.initializeSimulation(this, dataCopy, playerCopy);
                final Map<Territory, ProTerritory> factoryMoveMap = nonCombatMoveAi.simulateNonCombatMove(moveDel);
                if (storedFactoryMoveMap == null) {
                    storedFactoryMoveMap = ProSimulateTurnUtils.transferMoveMap(factoryMoveMap, data, player);
                }
            } else if (stepName.endsWith("CombatMove") && !stepName.endsWith("AirborneCombatMove")) {
                ProData.initializeSimulation(this, dataCopy, playerCopy);
                final Map<Territory, ProTerritory> moveMap = combatMoveAi.doCombatMove(moveDel);
                if (storedCombatMoveMap == null) {
                    storedCombatMoveMap = ProSimulateTurnUtils.transferMoveMap(moveMap, data, player);
                }
            } else if (stepName.endsWith("Battle")) {
                ProData.initializeSimulation(this, dataCopy, playerCopy);
                ProSimulateTurnUtils.simulateBattles(dataCopy, playerCopy, bridge, calc);
            } else if (stepName.endsWith("Place") || stepName.endsWith("EndTurn")) {
                ProData.initializeSimulation(this, dataCopy, player);
                storedPurchaseTerritories = purchaseAi.purchase(purchaseDelegate, data);
                break;
            } else if (stepName.endsWith("Politics")) {
                ProData.initializeSimulation(this, dataCopy, player);
                final PoliticsDelegate politicsDelegate = DelegateFinder.politicsDelegate(dataCopy);
                politicsDelegate.setDelegateBridgeAndPlayer(bridge);
                final List<PoliticalActionAttachment> actions = politicsAi.politicalActions();
                if (storedPoliticalActions == null) {
                    storedPoliticalActions = actions;
                }
            }
        }
    }
    ProLogger.info(player.getName() + " time for purchase=" + (System.currentTimeMillis() - start));
}
Also used : IMoveDelegate(games.strategy.triplea.delegate.remote.IMoveDelegate) PlayerID(games.strategy.engine.data.PlayerID) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) ArrayList(java.util.ArrayList) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) PoliticsDelegate(games.strategy.triplea.delegate.PoliticsDelegate) PoliticalActionAttachment(games.strategy.triplea.attachments.PoliticalActionAttachment) ProDummyDelegateBridge(games.strategy.triplea.ai.pro.simulate.ProDummyDelegateBridge) GameStep(games.strategy.engine.data.GameStep) Map(java.util.Map) HashMap(java.util.HashMap) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge)

Example 3 with IMoveDelegate

use of games.strategy.triplea.delegate.remote.IMoveDelegate in project triplea by triplea-game.

the class TripleAPlayer method move.

private void move(final boolean nonCombat, final String stepName) {
    if (getPlayerBridge().isGameOver()) {
        return;
    }
    final IMoveDelegate moveDel;
    try {
        moveDel = (IMoveDelegate) 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 (nonCombat && !soundPlayedAlreadyNonCombatMove) {
        ClipPlayer.play(SoundPath.CLIP_PHASE_MOVE_NONCOMBAT, id);
        soundPlayedAlreadyNonCombatMove = true;
    }
    if (!nonCombat && !soundPlayedAlreadyCombatMove) {
        ClipPlayer.play(SoundPath.CLIP_PHASE_MOVE_COMBAT, id);
        soundPlayedAlreadyCombatMove = true;
    }
    // getMove will block until all moves are done. We recursively call this same method
    // until getMove stops blocking.
    final MoveDescription moveDescription = ui.getMove(id, getPlayerBridge(), nonCombat, stepName);
    if (moveDescription == null) {
        if (GameStepPropertiesHelper.isRemoveAirThatCanNotLand(getGameData())) {
            if (!canAirLand(true, id)) {
                // continue with the move loop
                move(nonCombat, stepName);
            }
        }
        if (!nonCombat) {
            if (canUnitsFight()) {
                move(nonCombat, stepName);
            }
        }
        return;
    }
    final String error = moveDel.move(moveDescription.getUnits(), moveDescription.getRoute(), moveDescription.getTransportsThatCanBeLoaded(), moveDescription.getDependentUnits());
    if (error != null) {
        ui.notifyError(error);
    }
    move(nonCombat, stepName);
}
Also used : IMoveDelegate(games.strategy.triplea.delegate.remote.IMoveDelegate) PlayerID(games.strategy.engine.data.PlayerID) MoveDescription(games.strategy.triplea.delegate.dataObjects.MoveDescription)

Example 4 with IMoveDelegate

use of games.strategy.triplea.delegate.remote.IMoveDelegate in project triplea by triplea-game.

the class AbstractAi method start.

@Override
public final void start(final String name) {
    super.start(name);
    final PlayerID id = getPlayerId();
    if (name.endsWith("Bid")) {
        final IPurchaseDelegate purchaseDelegate = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
        final String propertyName = id.getName() + " bid";
        final int bidAmount = getGameData().getProperties().get(propertyName, 0);
        purchase(true, bidAmount, purchaseDelegate, getGameData(), id);
    } else if (name.endsWith("Purchase")) {
        final IPurchaseDelegate purchaseDelegate = (IPurchaseDelegate) getPlayerBridge().getRemoteDelegate();
        final Resource pus = getGameData().getResourceList().getResource(Constants.PUS);
        final int leftToSpend = id.getResources().getQuantity(pus);
        purchase(false, leftToSpend, purchaseDelegate, getGameData(), id);
    } else if (name.endsWith("Tech")) {
        final ITechDelegate techDelegate = (ITechDelegate) getPlayerBridge().getRemoteDelegate();
        tech(techDelegate, getGameData(), id);
    } else if (name.endsWith("Move")) {
        final IMoveDelegate moveDel = (IMoveDelegate) getPlayerBridge().getRemoteDelegate();
        if (name.endsWith("AirborneCombatMove")) {
        // do nothing
        } else {
            move(name.endsWith("NonCombatMove"), moveDel, getGameData(), id);
        }
    } else if (name.endsWith("Battle")) {
        battle((IBattleDelegate) getPlayerBridge().getRemoteDelegate(), getGameData(), id);
    } else if (name.endsWith("Politics")) {
        politicalActions();
    } else if (name.endsWith("Place")) {
        final IAbstractPlaceDelegate placeDel = (IAbstractPlaceDelegate) getPlayerBridge().getRemoteDelegate();
        place(name.contains("Bid"), placeDel, getGameData(), id);
    } else if (name.endsWith("EndTurn")) {
        endTurn((IAbstractForumPosterDelegate) getPlayerBridge().getRemoteDelegate(), getGameData(), id);
    }
}
Also used : IMoveDelegate(games.strategy.triplea.delegate.remote.IMoveDelegate) PlayerID(games.strategy.engine.data.PlayerID) ITechDelegate(games.strategy.triplea.delegate.remote.ITechDelegate) IPurchaseDelegate(games.strategy.triplea.delegate.remote.IPurchaseDelegate) IAbstractForumPosterDelegate(games.strategy.triplea.delegate.remote.IAbstractForumPosterDelegate) IAbstractPlaceDelegate(games.strategy.triplea.delegate.remote.IAbstractPlaceDelegate) Resource(games.strategy.engine.data.Resource)

Aggregations

IMoveDelegate (games.strategy.triplea.delegate.remote.IMoveDelegate)4 PlayerID (games.strategy.engine.data.PlayerID)3 Territory (games.strategy.engine.data.Territory)2 GameData (games.strategy.engine.data.GameData)1 GameStep (games.strategy.engine.data.GameStep)1 Resource (games.strategy.engine.data.Resource)1 Route (games.strategy.engine.data.Route)1 Unit (games.strategy.engine.data.Unit)1 IDelegateBridge (games.strategy.engine.delegate.IDelegateBridge)1 TripleAUnit (games.strategy.triplea.TripleAUnit)1 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)1 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)1 ProDummyDelegateBridge (games.strategy.triplea.ai.pro.simulate.ProDummyDelegateBridge)1 PoliticalActionAttachment (games.strategy.triplea.attachments.PoliticalActionAttachment)1 BattleDelegate (games.strategy.triplea.delegate.BattleDelegate)1 PoliticsDelegate (games.strategy.triplea.delegate.PoliticsDelegate)1 MoveDescription (games.strategy.triplea.delegate.dataObjects.MoveDescription)1 IAbstractForumPosterDelegate (games.strategy.triplea.delegate.remote.IAbstractForumPosterDelegate)1 IAbstractPlaceDelegate (games.strategy.triplea.delegate.remote.IAbstractPlaceDelegate)1 IPurchaseDelegate (games.strategy.triplea.delegate.remote.IPurchaseDelegate)1