use of games.strategy.engine.data.Route in project triplea by triplea-game.
the class ProMoveUtils method calculateBombingRoutes.
public static void calculateBombingRoutes(final PlayerID player, final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final Map<Territory, ProTerritory> attackMap) {
final GameData data = ProData.getData();
// Loop through all territories to attack
for (final Territory t : attackMap.keySet()) {
// Loop through each unit that is attacking the current territory
for (final Unit u : attackMap.get(t).getBombers()) {
// Skip if unit is already in move to territory
final Territory startTerritory = ProData.unitTerritoryMap.get(u);
if (startTerritory == null || startTerritory.equals(t)) {
continue;
}
// Add unit to move list
final List<Unit> unitList = new ArrayList<>();
unitList.add(u);
moveUnits.add(unitList);
// Determine route and add to move list
Route route = null;
if (!unitList.isEmpty() && unitList.stream().allMatch(Matches.unitIsAir())) {
route = data.getMap().getRoute_IgnoreEnd(startTerritory, t, ProMatches.territoryCanMoveAirUnitsAndNoAa(player, data, true));
}
moveRoutes.add(route);
}
}
}
use of games.strategy.engine.data.Route in project triplea by triplea-game.
the class ProTerritoryManager method findAirMoveOptions.
private static void findAirMoveOptions(final PlayerID player, final List<Territory> myUnitTerritories, final Map<Territory, ProTerritory> moveMap, final Map<Unit, Set<Territory>> unitMoveMap, final Predicate<Territory> moveToTerritoryMatch, final List<Territory> enemyTerritories, final List<Territory> alliedTerritories, final boolean isCombatMove, final boolean isCheckingEnemyAttacks, final boolean isIgnoringRelationships) {
final GameData data = ProData.getData();
// TODO: add carriers to landing possibilities for non-enemy attacks
// Find possible carrier landing territories
final Set<Territory> possibleCarrierTerritories = new HashSet<>();
if (isCheckingEnemyAttacks || !isCombatMove) {
final Map<Unit, Set<Territory>> unitMoveMap2 = new HashMap<>();
findNavalMoveOptions(player, myUnitTerritories, new HashMap<>(), unitMoveMap2, new HashMap<>(), Matches.territoryIsWater(), enemyTerritories, false, true);
for (final Unit u : unitMoveMap2.keySet()) {
if (Matches.unitIsCarrier().test(u)) {
possibleCarrierTerritories.addAll(unitMoveMap2.get(u));
}
}
for (final Territory t : data.getMap().getTerritories()) {
if (t.getUnits().anyMatch(Matches.unitIsAlliedCarrier(player, data))) {
possibleCarrierTerritories.add(t);
}
}
}
for (final Territory myUnitTerritory : myUnitTerritories) {
// Find my air units that have movement left
final List<Unit> myAirUnits = myUnitTerritory.getUnits().getMatches(ProMatches.unitCanBeMovedAndIsOwnedAir(player, isCombatMove));
// Check each air unit individually since they can have different ranges
for (final Unit myAirUnit : myAirUnits) {
// Find range
int range = TripleAUnit.get(myAirUnit).getMovementLeft();
if (isCheckingEnemyAttacks) {
range = UnitAttachment.get(myAirUnit.getType()).getMovement(player);
if (Matches.unitCanBeGivenBonusMovementByFacilitiesInItsTerritory(myUnitTerritory, player, data).test(myAirUnit)) {
// assumes bonus of +1 for now
range++;
}
}
// Find potential territories to move to
Set<Territory> possibleMoveTerritories = data.getMap().getNeighbors(myUnitTerritory, range, ProMatches.territoryCanMoveAirUnits(player, data, isCombatMove));
if (isIgnoringRelationships) {
possibleMoveTerritories = data.getMap().getNeighbors(myUnitTerritory, range, ProMatches.territoryCanPotentiallyMoveAirUnits(player, data));
}
possibleMoveTerritories.add(myUnitTerritory);
final Set<Territory> potentialTerritories = new HashSet<>(CollectionUtils.getMatches(possibleMoveTerritories, moveToTerritoryMatch));
if (!isCombatMove && Matches.unitCanLandOnCarrier().test(myAirUnit)) {
potentialTerritories.addAll(CollectionUtils.getMatches(possibleMoveTerritories, Matches.territoryIsInList(possibleCarrierTerritories)));
}
for (final Territory potentialTerritory : potentialTerritories) {
// Find route ignoring impassable and territories with AA
Predicate<Territory> canFlyOverMatch = ProMatches.territoryCanMoveAirUnitsAndNoAa(player, data, isCombatMove);
if (isCheckingEnemyAttacks) {
canFlyOverMatch = ProMatches.territoryCanMoveAirUnits(player, data, isCombatMove);
}
final Route myRoute = data.getMap().getRoute_IgnoreEnd(myUnitTerritory, potentialTerritory, canFlyOverMatch);
if (myRoute == null) {
continue;
}
final int myRouteLength = myRoute.numberOfSteps();
final int remainingMoves = range - myRouteLength;
if (remainingMoves < 0) {
continue;
}
// Check if unit can land
if (isCombatMove && (remainingMoves < myRouteLength || myUnitTerritory.isWater())) {
final Set<Territory> possibleLandingTerritories = data.getMap().getNeighbors(potentialTerritory, remainingMoves, canFlyOverMatch);
final List<Territory> landingTerritories = CollectionUtils.getMatches(possibleLandingTerritories, ProMatches.territoryCanLandAirUnits(player, data, isCombatMove, enemyTerritories, alliedTerritories));
List<Territory> carrierTerritories = new ArrayList<>();
if (Matches.unitCanLandOnCarrier().test(myAirUnit)) {
carrierTerritories = CollectionUtils.getMatches(possibleLandingTerritories, Matches.territoryIsInList(possibleCarrierTerritories));
}
if (landingTerritories.isEmpty() && carrierTerritories.isEmpty()) {
continue;
}
}
// Populate enemy territories with air unit
if (moveMap.containsKey(potentialTerritory)) {
moveMap.get(potentialTerritory).addMaxUnit(myAirUnit);
} else {
final ProTerritory moveTerritoryData = new ProTerritory(potentialTerritory);
moveTerritoryData.addMaxUnit(myAirUnit);
moveMap.put(potentialTerritory, moveTerritoryData);
}
// Populate unit attack options map
if (unitMoveMap.containsKey(myAirUnit)) {
unitMoveMap.get(myAirUnit).add(potentialTerritory);
} else {
final Set<Territory> unitMoveTerritories = new HashSet<>();
unitMoveTerritories.add(potentialTerritory);
unitMoveMap.put(myAirUnit, unitMoveTerritories);
}
}
}
}
}
use of games.strategy.engine.data.Route in project triplea by triplea-game.
the class ProTerritoryManager method findBombardOptions.
private static void findBombardOptions(final PlayerID player, final List<Territory> myUnitTerritories, final Map<Territory, ProTerritory> moveMap, final Map<Unit, Set<Territory>> bombardMap, final List<ProTransport> transportMapList, final boolean isCheckingEnemyAttacks) {
final GameData data = ProData.getData();
// Find all transport unload from and to territories
final Set<Territory> unloadFromTerritories = new HashSet<>();
final Set<Territory> unloadToTerritories = new HashSet<>();
for (final ProTransport amphibData : transportMapList) {
unloadFromTerritories.addAll(amphibData.getSeaTransportMap().keySet());
unloadToTerritories.addAll(amphibData.getTransportMap().keySet());
}
// Loop through territories with my units
for (final Territory myUnitTerritory : myUnitTerritories) {
// Find my bombard units that have movement left
final List<Unit> mySeaUnits = myUnitTerritory.getUnits().getMatches(ProMatches.unitCanBeMovedAndIsOwnedBombard(player));
// Check each sea unit individually since they can have different ranges
for (final Unit mySeaUnit : mySeaUnits) {
// Find range
int range = TripleAUnit.get(mySeaUnit).getMovementLeft();
if (isCheckingEnemyAttacks) {
range = UnitAttachment.get(mySeaUnit.getType()).getMovement(player);
if (Matches.unitCanBeGivenBonusMovementByFacilitiesInItsTerritory(myUnitTerritory, player, data).test(mySeaUnit)) {
// assumes bonus of +1 for now
range++;
}
}
// Find list of potential territories to move to
final Set<Territory> potentialTerritories = data.getMap().getNeighbors(myUnitTerritory, range, ProMatches.territoryCanMoveSeaUnits(player, data, true));
potentialTerritories.add(myUnitTerritory);
potentialTerritories.retainAll(unloadFromTerritories);
for (final Territory bombardFromTerritory : potentialTerritories) {
// Find route over water with no enemy units blocking
Route myRoute = data.getMap().getRoute(myUnitTerritory, bombardFromTerritory, ProMatches.territoryCanMoveSeaUnitsThrough(player, data, true));
if (isCheckingEnemyAttacks) {
myRoute = data.getMap().getRoute(myUnitTerritory, bombardFromTerritory, ProMatches.territoryCanMoveSeaUnits(player, data, true));
}
if (myRoute == null) {
continue;
}
if (MoveValidator.validateCanal(myRoute, Collections.singletonList(mySeaUnit), player, data) != null) {
continue;
}
final int myRouteLength = myRoute.numberOfSteps();
if (myRouteLength > range) {
continue;
}
// Find potential unload to territories
final Set<Territory> bombardToTerritories = new HashSet<>(data.getMap().getNeighbors(bombardFromTerritory));
bombardToTerritories.retainAll(unloadToTerritories);
// Populate attack territories with bombard unit
for (final Territory bombardToTerritory : bombardToTerritories) {
if (moveMap.containsKey(bombardToTerritory)) {
// Should always contain it
moveMap.get(bombardToTerritory).addMaxBombardUnit(mySeaUnit);
moveMap.get(bombardToTerritory).addBombardOptionsMap(mySeaUnit, bombardFromTerritory);
}
}
// Populate bombard options map
if (bombardMap.containsKey(mySeaUnit)) {
bombardMap.get(mySeaUnit).addAll(bombardToTerritories);
} else {
bombardMap.put(mySeaUnit, bombardToTerritories);
}
}
}
}
}
use of games.strategy.engine.data.Route in project triplea by triplea-game.
the class BigWorldTest method testCanalMovementNotStartingInCanalZone.
@Test
public void testCanalMovementNotStartingInCanalZone() {
final Territory sz28 = territory("SZ 28 Eastern Mediterranean", gameData);
final Territory sz27 = territory("SZ 27 Aegean Sea", gameData);
final Territory sz29 = territory("SZ 29 Black Sea", gameData);
final ITestDelegateBridge bridge = getDelegateBridge(british(gameData), gameData);
bridge.setStepName("CombatMove");
final MoveDelegate moveDelegate = moveDelegate(gameData);
moveDelegate.setDelegateBridgeAndPlayer(bridge);
moveDelegate.start();
final String error = moveDelegate.move(sz28.getUnits().getUnits(), new Route(sz28, sz27, sz29));
assertError(error);
}
use of games.strategy.engine.data.Route in project triplea by triplea-game.
the class WW2V3Year41Test method testCanRetreatIntoBlitzedTerritory.
@Test
public void testCanRetreatIntoBlitzedTerritory() {
final Territory eastPoland = territory("East Poland", gameData);
final Territory ukraine = territory("Ukraine", gameData);
final Territory poland = territory("Poland", gameData);
// remove all units from east poland
removeFrom(eastPoland, eastPoland.getUnits().getUnits());
final MoveDelegate moveDelegate = moveDelegate(gameData);
final ITestDelegateBridge delegateBridge = getDelegateBridge(germans(gameData));
delegateBridge.setStepName("CombatMove");
moveDelegate.setDelegateBridgeAndPlayer(delegateBridge);
moveDelegate.start();
final Territory bulgaria = territory("Bulgaria Romania", gameData);
// attack from bulgraia
move(bulgaria.getUnits().getUnits(), new Route(bulgaria, ukraine));
// add a blitz attack
move(poland.getUnits().getMatches(Matches.unitCanBlitz()), new Route(poland, eastPoland, ukraine));
// we should not be able to retreat to east poland!
// that territory was just conquered
final MustFightBattle battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(ukraine, false, null);
assertTrue(battle.getAttackerRetreatTerritories().contains(eastPoland));
}
Aggregations