Search in sources :

Example 6 with Route

use of games.strategy.engine.data.Route in project triplea by triplea-game.

the class MustFightBattle method getEmptyOrFriendlySeaNeighbors.

private Collection<Territory> getEmptyOrFriendlySeaNeighbors(final PlayerID player, final Collection<Unit> unitsToRetreat) {
    Collection<Territory> possible = m_data.getMap().getNeighbors(m_battleSite);
    if (m_headless) {
        return possible;
    }
    // make sure we can move through the any canals
    final Predicate<Territory> canalMatch = t -> {
        final Route r = new Route();
        r.setStart(m_battleSite);
        r.add(t);
        return MoveValidator.validateCanal(r, unitsToRetreat, m_defender, m_data) == null;
    };
    final Predicate<Territory> match = Matches.territoryIsWater().and(Matches.territoryHasNoEnemyUnits(player, m_data)).and(canalMatch);
    possible = CollectionUtils.getMatches(possible, match);
    return possible;
}
Also used : ITripleADisplay(games.strategy.triplea.ui.display.ITripleADisplay) BattleRecord(games.strategy.triplea.delegate.dataObjects.BattleRecord) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) HashMap(java.util.HashMap) TechAttachment(games.strategy.triplea.attachments.TechAttachment) PredicateBuilder(games.strategy.util.PredicateBuilder) Properties(games.strategy.triplea.Properties) TuvUtils(games.strategy.triplea.util.TuvUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Route(games.strategy.engine.data.Route) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) Map(java.util.Map) UnitType(games.strategy.engine.data.UnitType) TechAbilityAttachment(games.strategy.triplea.attachments.TechAbilityAttachment) TripleAUnit(games.strategy.triplea.TripleAUnit) CompositeChange(games.strategy.engine.data.CompositeChange) LinkedHashSet(java.util.LinkedHashSet) CollectionUtils(games.strategy.util.CollectionUtils) IntegerMap(games.strategy.util.IntegerMap) BattleResults(games.strategy.triplea.oddsCalculator.ta.BattleResults) Unit(games.strategy.engine.data.Unit) Iterator(java.util.Iterator) Interruptibles(games.strategy.util.Interruptibles) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Territory(games.strategy.engine.data.Territory) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) GameData(games.strategy.engine.data.GameData) ChangeFactory(games.strategy.engine.data.changefactory.ChangeFactory) List(java.util.List) Tuple(games.strategy.util.Tuple) PlayerID(games.strategy.engine.data.PlayerID) Change(games.strategy.engine.data.Change) MyFormatter(games.strategy.triplea.formatter.MyFormatter) Comparator(java.util.Comparator) TerritoryEffect(games.strategy.engine.data.TerritoryEffect) Collections(java.util.Collections) SoundPath(games.strategy.sound.SoundPath) Territory(games.strategy.engine.data.Territory) Route(games.strategy.engine.data.Route)

Example 7 with Route

use of games.strategy.engine.data.Route in project triplea by triplea-game.

the class Utils method findNearest.

static Route findNearest(final Territory start, final Predicate<Territory> endCondition, final Predicate<Territory> routeCondition, final GameData data) {
    Route shortestRoute = null;
    for (final Territory t : data.getMap().getTerritories()) {
        if (endCondition.test(t)) {
            final Predicate<Territory> routeOrEnd = routeCondition.or(Matches.territoryIs(t));
            final Route r = data.getMap().getRoute(start, t, routeOrEnd);
            if (r != null) {
                if (shortestRoute == null || r.numberOfSteps() < shortestRoute.numberOfSteps()) {
                    shortestRoute = r;
                }
            }
        }
    }
    return shortestRoute;
}
Also used : Territory(games.strategy.engine.data.Territory) Route(games.strategy.engine.data.Route)

Example 8 with Route

use of games.strategy.engine.data.Route in project triplea by triplea-game.

the class WeakAi method populateTransportUnloadNonCom.

private static void populateTransportUnloadNonCom(final GameData data, final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final PlayerID player) {
    final Route amphibRoute = getAmphibRoute(player, data);
    if (amphibRoute == null) {
        return;
    }
    final Territory lastSeaZoneOnAmphib = amphibRoute.getAllTerritories().get(amphibRoute.numberOfSteps() - 1);
    final Territory landOn = amphibRoute.getEnd();
    final Predicate<Unit> landAndOwned = Matches.unitIsLand().and(Matches.unitIsOwnedBy(player));
    final List<Unit> units = lastSeaZoneOnAmphib.getUnits().getMatches(landAndOwned);
    if (units.size() > 0) {
        // just try to make the move, the engine will stop us if it doesnt work
        final Route route = new Route();
        route.setStart(lastSeaZoneOnAmphib);
        route.add(landOn);
        moveUnits.add(units);
        moveRoutes.add(route);
    }
}
Also used : Territory(games.strategy.engine.data.Territory) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route)

Example 9 with Route

use of games.strategy.engine.data.Route in project triplea by triplea-game.

the class WeakAi method populateTransportLoad.

private void populateTransportLoad(final GameData data, final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final List<Collection<Unit>> transportsToLoad, final PlayerID player) {
    if (!isAmphibAttack(player, data)) {
        return;
    }
    final Territory capitol = TerritoryAttachment.getFirstOwnedCapitalOrFirstUnownedCapital(player, data);
    if (capitol == null || !capitol.getOwner().equals(player)) {
        return;
    }
    List<Unit> unitsToLoad = capitol.getUnits().getMatches(Matches.unitIsInfrastructure().negate());
    unitsToLoad = CollectionUtils.getMatches(unitsToLoad, Matches.unitIsOwnedBy(getPlayerId()));
    for (final Territory neighbor : data.getMap().getNeighbors(capitol)) {
        if (!neighbor.isWater()) {
            continue;
        }
        final List<Unit> units = new ArrayList<>();
        for (final Unit transport : neighbor.getUnits().getMatches(Matches.unitIsOwnedBy(player))) {
            int free = TransportTracker.getAvailableCapacity(transport);
            if (free <= 0) {
                continue;
            }
            final Iterator<Unit> iter = unitsToLoad.iterator();
            while (iter.hasNext() && free > 0) {
                final Unit current = iter.next();
                final UnitAttachment ua = UnitAttachment.get(current.getType());
                if (ua.getIsAir()) {
                    continue;
                }
                if (ua.getTransportCost() <= free) {
                    iter.remove();
                    free -= ua.getTransportCost();
                    units.add(current);
                }
            }
        }
        if (units.size() > 0) {
            final Route route = new Route();
            route.setStart(capitol);
            route.add(neighbor);
            moveUnits.add(units);
            moveRoutes.add(route);
            transportsToLoad.add(neighbor.getUnits().getMatches(Matches.unitIsTransport()));
        }
    }
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) Territory(games.strategy.engine.data.Territory) ArrayList(java.util.ArrayList) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route)

Example 10 with Route

use of games.strategy.engine.data.Route in project triplea by triplea-game.

the class WeakAi method populateBomberCombat.

private static void populateBomberCombat(final GameData data, final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final PlayerID player) {
    final Predicate<Territory> enemyFactory = Matches.territoryIsEnemyNonNeutralAndHasEnemyUnitMatching(data, player, Matches.unitCanProduceUnitsAndCanBeDamaged());
    final Predicate<Unit> ownBomber = Matches.unitIsStrategicBomber().and(Matches.unitIsOwnedBy(player));
    for (final Territory t : data.getMap().getTerritories()) {
        final Collection<Unit> bombers = t.getUnits().getMatches(ownBomber);
        if (bombers.isEmpty()) {
            continue;
        }
        final Predicate<Territory> routeCond = Matches.territoryHasEnemyAaForCombatOnly(player, data).negate();
        final Route bombRoute = Utils.findNearest(t, enemyFactory, routeCond, data);
        moveUnits.add(bombers);
        moveRoutes.add(bombRoute);
    }
}
Also used : Territory(games.strategy.engine.data.Territory) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route)

Aggregations

Route (games.strategy.engine.data.Route)200 Test (org.junit.jupiter.api.Test)148 Territory (games.strategy.engine.data.Territory)132 Unit (games.strategy.engine.data.Unit)84 UnitType (games.strategy.engine.data.UnitType)83 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)77 TripleAUnit (games.strategy.triplea.TripleAUnit)70 IntegerMap (games.strategy.util.IntegerMap)69 PlayerID (games.strategy.engine.data.PlayerID)64 ArrayList (java.util.ArrayList)44 GameData (games.strategy.engine.data.GameData)29 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)26 HashSet (java.util.HashSet)21 HashMap (java.util.HashMap)20 Collection (java.util.Collection)15 List (java.util.List)12 Set (java.util.Set)12 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)11 Change (games.strategy.engine.data.Change)10 MoveValidationResult (games.strategy.triplea.delegate.dataObjects.MoveValidationResult)10