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);
}
}
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());
}
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);
}
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);
}
}
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());
}
}
}
Aggregations