Search in sources :

Example 1 with Properties

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);
            }
        }
    }
}
Also used : ProUtils(games.strategy.triplea.ai.pro.util.ProUtils) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) HashMap(java.util.HashMap) ProBattleUtils(games.strategy.triplea.ai.pro.util.ProBattleUtils) PredicateBuilder(games.strategy.util.PredicateBuilder) Properties(games.strategy.triplea.Properties) ProTransportUtils(games.strategy.triplea.ai.pro.util.ProTransportUtils) ArrayList(java.util.ArrayList) ProLogger(games.strategy.triplea.ai.pro.logging.ProLogger) HashSet(java.util.HashSet) Route(games.strategy.engine.data.Route) TransportTracker(games.strategy.triplea.delegate.TransportTracker) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) MoveValidator(games.strategy.triplea.delegate.MoveValidator) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) TripleAUnit(games.strategy.triplea.TripleAUnit) CollectionUtils(games.strategy.util.CollectionUtils) Unit(games.strategy.engine.data.Unit) Predicate(java.util.function.Predicate) Collection(java.util.Collection) TerritoryEffectHelper(games.strategy.triplea.delegate.TerritoryEffectHelper) Set(java.util.Set) Territory(games.strategy.engine.data.Territory) ProData(games.strategy.triplea.ai.pro.ProData) GameData(games.strategy.engine.data.GameData) List(java.util.List) PlayerID(games.strategy.engine.data.PlayerID) ProMatches(games.strategy.triplea.ai.pro.util.ProMatches) Matches(games.strategy.triplea.delegate.Matches) ProOddsCalculator(games.strategy.triplea.ai.pro.util.ProOddsCalculator) Comparator(java.util.Comparator) Collections(java.util.Collections) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) HashMap(java.util.HashMap) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) Route(games.strategy.engine.data.Route) HashSet(java.util.HashSet)

Aggregations

GameData (games.strategy.engine.data.GameData)1 PlayerID (games.strategy.engine.data.PlayerID)1 Route (games.strategy.engine.data.Route)1 Territory (games.strategy.engine.data.Territory)1 Unit (games.strategy.engine.data.Unit)1 Properties (games.strategy.triplea.Properties)1 TripleAUnit (games.strategy.triplea.TripleAUnit)1 ProData (games.strategy.triplea.ai.pro.ProData)1 ProLogger (games.strategy.triplea.ai.pro.logging.ProLogger)1 ProBattleUtils (games.strategy.triplea.ai.pro.util.ProBattleUtils)1 ProMatches (games.strategy.triplea.ai.pro.util.ProMatches)1 ProOddsCalculator (games.strategy.triplea.ai.pro.util.ProOddsCalculator)1 ProTransportUtils (games.strategy.triplea.ai.pro.util.ProTransportUtils)1 ProUtils (games.strategy.triplea.ai.pro.util.ProUtils)1 TerritoryAttachment (games.strategy.triplea.attachments.TerritoryAttachment)1 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)1 Matches (games.strategy.triplea.delegate.Matches)1 MoveValidator (games.strategy.triplea.delegate.MoveValidator)1 TerritoryEffectHelper (games.strategy.triplea.delegate.TerritoryEffectHelper)1 TransportTracker (games.strategy.triplea.delegate.TransportTracker)1