use of games.strategy.engine.data.Unit in project triplea by triplea-game.
the class MustFightBattle method retreatFromNonCombat.
/**
* Retreat landed units from allied territory when their transport retreats.
*/
private Change retreatFromNonCombat(Collection<Unit> units, final Territory retreatTo) {
final CompositeChange change = new CompositeChange();
units = CollectionUtils.getMatches(units, Matches.unitIsTransport());
final Collection<Unit> retreated = getTransportDependents(units);
if (!retreated.isEmpty()) {
for (final Unit unit : units) {
final Territory retreatedFrom = TransportTracker.getTerritoryTransportHasUnloadedTo(unit);
if (retreatedFrom != null) {
reLoadTransports(units, change);
change.add(ChangeFactory.moveUnits(retreatedFrom, retreatTo, retreated));
}
}
}
return change;
}
use of games.strategy.engine.data.Unit in project triplea by triplea-game.
the class MustFightBattle method retreatFromDependents.
private Change retreatFromDependents(final Collection<Unit> units, final Territory retreatTo, final Collection<IBattle> dependentBattles) {
final CompositeChange change = new CompositeChange();
for (final IBattle dependent : dependentBattles) {
final Route route = new Route();
route.setStart(m_battleSite);
route.add(dependent.getTerritory());
final Collection<Unit> retreatedUnits = dependent.getDependentUnits(units);
dependent.removeAttack(route, retreatedUnits);
reLoadTransports(units, change);
change.add(ChangeFactory.moveUnits(dependent.getTerritory(), retreatTo, retreatedUnits));
}
return change;
}
use of games.strategy.engine.data.Unit 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.Unit in project triplea by triplea-game.
the class MustFightBattle method retreatUnitsAndPlanes.
private void retreatUnitsAndPlanes(final Collection<Unit> retreating, final Territory to, final boolean defender, final IDelegateBridge bridge) {
// Remove air from battle
final Collection<Unit> units = defender ? m_defendingUnits : m_attackingUnits;
final Collection<Unit> unitsRetreated = defender ? m_defendingUnitsRetreated : m_attackingUnitsRetreated;
units.removeAll(CollectionUtils.getMatches(units, Matches.unitIsAir()));
// add all land units' dependents
retreating.addAll(getDependentUnits(units));
// our own air units don't retreat with land units
final Predicate<Unit> notMyAir = Matches.unitIsNotAir().or(Matches.unitIsOwnedBy(m_attacker).negate());
final Collection<Unit> nonAirRetreating = CollectionUtils.getMatches(retreating, notMyAir);
final String transcriptText = MyFormatter.unitsToTextNoOwner(nonAirRetreating) + " retreated to " + to.getName();
bridge.getHistoryWriter().addChildToEvent(transcriptText, new ArrayList<>(nonAirRetreating));
final CompositeChange change = new CompositeChange();
change.add(ChangeFactory.moveUnits(m_battleSite, to, nonAirRetreating));
if (m_isOver) {
final Collection<IBattle> dependentBattles = m_battleTracker.getBlocked(this);
// If there are no dependent battles, check landings in allied territories
if (dependentBattles.isEmpty()) {
change.add(retreatFromNonCombat(nonAirRetreating, to));
// Else retreat the units from combat when their transport retreats
} else {
change.add(retreatFromDependents(nonAirRetreating, to, dependentBattles));
}
}
bridge.addChange(change);
units.removeAll(nonAirRetreating);
unitsRetreated.addAll(nonAirRetreating);
if (units.isEmpty() || m_isOver) {
endBattle(bridge);
if (defender) {
attackerWins(bridge);
} else {
defenderWins(bridge);
}
} else {
getDisplay(bridge).notifyRetreat(m_battleID, retreating);
}
}
use of games.strategy.engine.data.Unit in project triplea by triplea-game.
the class MustFightBattle method sortAmphib.
/**
* In an amphibious assault, sort on who is unloading from transports first as this will allow the marines with higher
* scores to get killed last.
*/
private void sortAmphib(final List<Unit> units) {
final Comparator<Unit> decreasingMovement = UnitComparator.getLowestToHighestMovementComparator();
units.sort(Comparator.comparing(Unit::getType, Comparator.comparing(UnitType::getName)).thenComparing((u1, u2) -> {
final UnitAttachment ua = UnitAttachment.get(u1.getType());
final UnitAttachment ua2 = UnitAttachment.get(u2.getType());
if (ua.getIsMarine() != 0 && ua2.getIsMarine() != 0) {
return compareAccordingToAmphibious(u1, u2);
}
return 0;
}).thenComparing(decreasingMovement));
}
Aggregations