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