use of games.strategy.triplea.Properties in project triplea by triplea-game.
the class ProTerritoryManager method findScrambleOptions.
private static void findScrambleOptions(final PlayerID player, final Map<Territory, ProTerritory> moveMap) {
final GameData data = ProData.getData();
if (!Properties.getScrambleRulesInEffect(data)) {
return;
}
// Find scramble properties
final boolean fromIslandOnly = Properties.getScrambleFromIslandOnly(data);
final boolean toSeaOnly = Properties.getScrambleToSeaOnly(data);
final int maxScrambleDistance = StreamSupport.stream(data.getUnitTypeList().spliterator(), false).map(UnitAttachment::get).filter(UnitAttachment::getCanScramble).mapToInt(UnitAttachment::getMaxScrambleDistance).max().orElse(0);
final Predicate<Unit> airbasesCanScramble = Matches.unitIsEnemyOf(data, player).and(Matches.unitIsAirBase()).and(Matches.unitIsNotDisabled()).and(Matches.unitIsBeingTransported().negate());
final Predicate<Territory> canScramble = PredicateBuilder.of(Matches.territoryIsWater().or(Matches.isTerritoryEnemy(player, data))).and(Matches.territoryHasUnitsThatMatch(Matches.unitCanScramble().and(Matches.unitIsEnemyOf(data, player)).and(Matches.unitIsNotDisabled()))).and(Matches.territoryHasUnitsThatMatch(airbasesCanScramble)).andIf(fromIslandOnly, Matches.territoryIsIsland()).build();
// Find potential territories to scramble from
final HashMap<Territory, HashSet<Territory>> scrambleTerrs = new HashMap<>();
for (final Territory t : moveMap.keySet()) {
if (t.isWater() || !toSeaOnly) {
final HashSet<Territory> canScrambleFrom = new HashSet<>(CollectionUtils.getMatches(data.getMap().getNeighbors(t, maxScrambleDistance), canScramble));
if (!canScrambleFrom.isEmpty()) {
scrambleTerrs.put(t, canScrambleFrom);
}
}
}
if (scrambleTerrs.isEmpty()) {
return;
}
// Find potential max units that can be scrambled to each territory
for (final Territory to : scrambleTerrs.keySet()) {
for (final Territory from : scrambleTerrs.get(to)) {
// Find potential scramble units from territory
final Collection<Unit> airbases = from.getUnits().getMatches(airbasesCanScramble);
final int maxCanScramble = getMaxScrambleCount(airbases);
final Route toBattleRoute = data.getMap().getRoute_IgnoreEnd(from, to, Matches.territoryIsNotImpassable());
List<Unit> canScrambleAir = from.getUnits().getMatches(Matches.unitIsEnemyOf(data, player).and(Matches.unitCanScramble()).and(Matches.unitIsNotDisabled()).and(Matches.unitWasScrambled().negate()).and(Matches.unitCanScrambleOnRouteDistance(toBattleRoute)));
// Add max scramble units
if (maxCanScramble > 0 && !canScrambleAir.isEmpty()) {
if (maxCanScramble < canScrambleAir.size()) {
canScrambleAir.sort(Comparator.comparingDouble(o -> ProBattleUtils.estimateStrength(to, Collections.singletonList(o), new ArrayList<>(), false)));
canScrambleAir = canScrambleAir.subList(0, maxCanScramble);
}
moveMap.get(to).getMaxScrambleUnits().addAll(canScrambleAir);
}
}
}
}
Aggregations