Search in sources :

Example 6 with PlaceableUnits

use of games.strategy.triplea.delegate.dataObjects.PlaceableUnits in project triplea by triplea-game.

the class WeakAi method placeAllWeCanOn.

private static void placeAllWeCanOn(final GameData data, final Territory placeAt, final IAbstractPlaceDelegate placeDelegate, final PlayerID player) {
    final PlaceableUnits pu = placeDelegate.getPlaceableUnits(player.getUnits().getUnits(), placeAt);
    if (pu.getErrorMessage() != null) {
        return;
    }
    int placementLeft = pu.getMaxUnits();
    if (placementLeft == -1) {
        placementLeft = Integer.MAX_VALUE;
    }
    final List<Unit> seaUnits = new ArrayList<>(player.getUnits().getMatches(Matches.unitIsSea()));
    if (seaUnits.size() > 0) {
        final Route amphibRoute = getAmphibRoute(player, data);
        Territory seaPlaceAt = null;
        if (amphibRoute != null) {
            seaPlaceAt = amphibRoute.getAllTerritories().get(1);
        } else {
            final Set<Territory> seaNeighbors = data.getMap().getNeighbors(placeAt, Matches.territoryIsWater());
            if (!seaNeighbors.isEmpty()) {
                seaPlaceAt = seaNeighbors.iterator().next();
            }
        }
        if (seaPlaceAt != null) {
            final int seaPlacement = Math.min(placementLeft, seaUnits.size());
            placementLeft -= seaPlacement;
            final Collection<Unit> toPlace = seaUnits.subList(0, seaPlacement);
            doPlace(seaPlaceAt, toPlace, placeDelegate);
        }
    }
    final List<Unit> landUnits = new ArrayList<>(player.getUnits().getMatches(Matches.unitIsLand()));
    if (!landUnits.isEmpty()) {
        final int landPlaceCount = Math.min(placementLeft, landUnits.size());
        final Collection<Unit> toPlace = landUnits.subList(0, landPlaceCount);
        doPlace(placeAt, toPlace, placeDelegate);
    }
}
Also used : Territory(games.strategy.engine.data.Territory) PlaceableUnits(games.strategy.triplea.delegate.dataObjects.PlaceableUnits) ArrayList(java.util.ArrayList) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route)

Example 7 with PlaceableUnits

use of games.strategy.triplea.delegate.dataObjects.PlaceableUnits in project triplea by triplea-game.

the class PlaceDelegateTest method testCanProduce.

@Test
public void testCanProduce() {
    final IntegerMap<UnitType> map = new IntegerMap<>();
    map.add(infantry, 2);
    final PlaceableUnits response = delegate.getPlaceableUnits(GameDataTestUtil.getUnits(map, british), westCanada);
    assertFalse(response.isError());
}
Also used : IntegerMap(games.strategy.util.IntegerMap) PlaceableUnits(games.strategy.triplea.delegate.dataObjects.PlaceableUnits) UnitType(games.strategy.engine.data.UnitType) Test(org.junit.jupiter.api.Test)

Example 8 with PlaceableUnits

use of games.strategy.triplea.delegate.dataObjects.PlaceableUnits in project triplea by triplea-game.

the class PlaceDelegateTest method testAlreadyProducedUnits.

@Test
public void testAlreadyProducedUnits() {
    final IntegerMap<UnitType> map = new IntegerMap<>();
    final Map<Territory, Collection<Unit>> alreadyProduced = new HashMap<>();
    alreadyProduced.put(westCanada, getInfantry(2, british));
    delegate.setProduced(alreadyProduced);
    map.add(infantry, 1);
    final PlaceableUnits response = delegate.getPlaceableUnits(GameDataTestUtil.getUnits(map, british), westCanada);
    assertTrue(response.getMaxUnits() == 0);
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) PlaceableUnits(games.strategy.triplea.delegate.dataObjects.PlaceableUnits) HashMap(java.util.HashMap) UnitType(games.strategy.engine.data.UnitType) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 9 with PlaceableUnits

use of games.strategy.triplea.delegate.dataObjects.PlaceableUnits in project triplea by triplea-game.

the class ProPurchaseAi method placeLandUnits.

private void placeLandUnits(final List<ProPlaceTerritory> prioritizedLandTerritories, final IAbstractPlaceDelegate placeDelegate, final boolean isConstruction) {
    ProLogger.info("Place land with isConstruction=" + isConstruction + ", units=" + player.getUnits().getUnits());
    Predicate<Unit> unitMatch = Matches.unitIsNotConstruction();
    if (isConstruction) {
        unitMatch = Matches.unitIsConstruction();
    }
    // Loop through prioritized territories and place land units
    for (final ProPlaceTerritory placeTerritory : prioritizedLandTerritories) {
        final Territory t = placeTerritory.getTerritory();
        ProLogger.debug("Checking land place for " + t.getName());
        // Check if any units can be placed
        final PlaceableUnits placeableUnits = placeDelegate.getPlaceableUnits(player.getUnits().getMatches(unitMatch), t);
        if (placeableUnits.isError()) {
            ProLogger.trace(t + " can't place units with error: " + placeableUnits.getErrorMessage());
            continue;
        }
        // Find remaining unit production
        int remainingUnitProduction = placeableUnits.getMaxUnits();
        if (remainingUnitProduction == -1) {
            remainingUnitProduction = Integer.MAX_VALUE;
        }
        ProLogger.trace(t + ", remainingUnitProduction=" + remainingUnitProduction);
        // Place as many units as possible
        final List<Unit> unitsThatCanBePlaced = new ArrayList<>(placeableUnits.getUnits());
        final int landPlaceCount = Math.min(remainingUnitProduction, unitsThatCanBePlaced.size());
        final List<Unit> unitsToPlace = unitsThatCanBePlaced.subList(0, landPlaceCount);
        ProLogger.trace(t + ", placedUnits=" + unitsToPlace);
        doPlace(t, unitsToPlace, placeDelegate);
    }
}
Also used : ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) Territory(games.strategy.engine.data.Territory) PlaceableUnits(games.strategy.triplea.delegate.dataObjects.PlaceableUnits) ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ArrayList(java.util.ArrayList) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 10 with PlaceableUnits

use of games.strategy.triplea.delegate.dataObjects.PlaceableUnits in project triplea by triplea-game.

the class ProPurchaseAi method placeDefenders.

private void placeDefenders(final Map<Territory, ProPurchaseTerritory> placeNonConstructionTerritories, final List<ProPlaceTerritory> needToDefendTerritories, final IAbstractPlaceDelegate placeDelegate) {
    ProLogger.info("Place defenders with units=" + player.getUnits().getUnits());
    final ProOtherMoveOptions enemyAttackOptions = territoryManager.getEnemyAttackOptions();
    // Loop through prioritized territories and purchase defenders
    for (final ProPlaceTerritory placeTerritory : needToDefendTerritories) {
        final Territory t = placeTerritory.getTerritory();
        ProLogger.debug("Placing defenders for " + t.getName() + ", enemyAttackers=" + enemyAttackOptions.getMax(t).getMaxUnits() + ", amphibEnemyAttackers=" + enemyAttackOptions.getMax(t).getMaxAmphibUnits() + ", defenders=" + placeTerritory.getDefendingUnits());
        // Check if any units can be placed
        final PlaceableUnits placeableUnits = placeDelegate.getPlaceableUnits(player.getUnits().getMatches(Matches.unitIsNotConstruction()), t);
        if (placeableUnits.isError()) {
            ProLogger.trace(t + " can't place units with error: " + placeableUnits.getErrorMessage());
            continue;
        }
        // Find remaining unit production
        int remainingUnitProduction = placeableUnits.getMaxUnits();
        if (remainingUnitProduction == -1) {
            remainingUnitProduction = Integer.MAX_VALUE;
        }
        ProLogger.trace(t + ", remainingUnitProduction=" + remainingUnitProduction);
        // Place defenders and check battle results
        final List<Unit> unitsThatCanBePlaced = new ArrayList<>(placeableUnits.getUnits());
        final int landPlaceCount = Math.min(remainingUnitProduction, unitsThatCanBePlaced.size());
        final List<Unit> unitsToPlace = new ArrayList<>();
        ProBattleResult finalResult = new ProBattleResult();
        for (int i = 0; i < landPlaceCount; i++) {
            // Add defender
            unitsToPlace.add(unitsThatCanBePlaced.get(i));
            // Find current battle result
            final Set<Unit> enemyAttackingUnits = new HashSet<>(enemyAttackOptions.getMax(t).getMaxUnits());
            enemyAttackingUnits.addAll(enemyAttackOptions.getMax(t).getMaxAmphibUnits());
            final List<Unit> defenders = new ArrayList<>(placeTerritory.getDefendingUnits());
            defenders.addAll(unitsToPlace);
            finalResult = calc.calculateBattleResults(t, new ArrayList<>(enemyAttackingUnits), defenders, enemyAttackOptions.getMax(t).getMaxBombardUnits());
            // Break if it can be held
            if ((!t.equals(ProData.myCapital) && !finalResult.isHasLandUnitRemaining() && finalResult.getTuvSwing() <= 0) || (t.equals(ProData.myCapital) && finalResult.getWinPercentage() < (100 - ProData.winPercentage) && finalResult.getTuvSwing() <= 0)) {
                break;
            }
        }
        // Check to see if its worth trying to defend the territory
        if (!finalResult.isHasLandUnitRemaining() || finalResult.getTuvSwing() < placeTerritory.getMinBattleResult().getTuvSwing() || t.equals(ProData.myCapital)) {
            ProLogger.trace(t + ", placedUnits=" + unitsToPlace + ", TUVSwing=" + finalResult.getTuvSwing());
            doPlace(t, unitsToPlace, placeDelegate);
        } else {
            setCantHoldPlaceTerritory(placeTerritory, placeNonConstructionTerritories);
            ProLogger.trace(t + ", unable to defend with placedUnits=" + unitsToPlace + ", TUVSwing=" + finalResult.getTuvSwing() + ", minTUVSwing=" + placeTerritory.getMinBattleResult().getTuvSwing());
        }
    }
}
Also used : ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) Territory(games.strategy.engine.data.Territory) PlaceableUnits(games.strategy.triplea.delegate.dataObjects.PlaceableUnits) ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ArrayList(java.util.ArrayList) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) ProOtherMoveOptions(games.strategy.triplea.ai.pro.data.ProOtherMoveOptions) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) HashSet(java.util.HashSet)

Aggregations

PlaceableUnits (games.strategy.triplea.delegate.dataObjects.PlaceableUnits)10 Territory (games.strategy.engine.data.Territory)6 Unit (games.strategy.engine.data.Unit)6 Test (org.junit.jupiter.api.Test)6 TripleAUnit (games.strategy.triplea.TripleAUnit)5 UnitType (games.strategy.engine.data.UnitType)4 IntegerMap (games.strategy.util.IntegerMap)4 ArrayList (java.util.ArrayList)3 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)2 ProPlaceTerritory (games.strategy.triplea.ai.pro.data.ProPlaceTerritory)2 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)2 Route (games.strategy.engine.data.Route)1 ProBattleResult (games.strategy.triplea.ai.pro.data.ProBattleResult)1 ProOtherMoveOptions (games.strategy.triplea.ai.pro.data.ProOtherMoveOptions)1 PlayerAttachment (games.strategy.triplea.attachments.PlayerAttachment)1 IAbstractPlaceDelegate (games.strategy.triplea.delegate.remote.IAbstractPlaceDelegate)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1