use of games.strategy.triplea.attachments.UnitAttachment in project triplea by triplea-game.
the class ProTransportUtils method getUnusedLocalCarrierCapacity.
public static int getUnusedLocalCarrierCapacity(final PlayerID player, final Territory t, final List<Unit> unitsToPlace) {
final GameData data = ProData.getData();
// Find nearby carrier capacity
final Set<Territory> nearbyTerritories = data.getMap().getNeighbors(t, 2, ProMatches.territoryCanMoveAirUnits(player, data, false));
nearbyTerritories.add(t);
final List<Unit> ownedNearbyUnits = new ArrayList<>();
int capacity = 0;
for (final Territory nearbyTerritory : nearbyTerritories) {
final List<Unit> units = nearbyTerritory.getUnits().getMatches(Matches.unitIsOwnedBy(player));
if (nearbyTerritory.equals(t)) {
units.addAll(unitsToPlace);
}
ownedNearbyUnits.addAll(units);
capacity += AirMovementValidator.carrierCapacity(units, t);
}
// Find nearby air unit carrier cost
final Collection<Unit> airUnits = CollectionUtils.getMatches(ownedNearbyUnits, ProMatches.unitIsOwnedAir(player));
for (final Unit airUnit : airUnits) {
final UnitAttachment ua = UnitAttachment.get(airUnit.getType());
final int cost = ua.getCarrierCost();
if (cost != -1) {
capacity -= cost;
}
}
return capacity;
}
use of games.strategy.triplea.attachments.UnitAttachment in project triplea by triplea-game.
the class ProTransportUtils method interleaveUnitsCarriersAndPlanes.
public static List<Unit> interleaveUnitsCarriersAndPlanes(final List<Unit> units, final int planesThatDontNeedToLand) {
if (units.stream().noneMatch(Matches.unitIsCarrier()) || units.stream().noneMatch(Matches.unitCanLandOnCarrier())) {
return units;
}
// Clone the current list
final ArrayList<Unit> result = new ArrayList<>(units);
Unit seekedCarrier = null;
int indexToPlaceCarrierAt = -1;
int spaceLeftOnSeekedCarrier = -1;
int processedPlaneCount = 0;
final List<Unit> filledCarriers = new ArrayList<>();
// Loop through all units, starting from the right, and rearrange units
for (int i = result.size() - 1; i >= 0; i--) {
final Unit unit = result.get(i);
final UnitAttachment ua = UnitAttachment.get(unit.getType());
if (ua.getCarrierCost() > 0 || i == 0) {
// If we haven't ignored enough trailing planes and not last unit
if (processedPlaneCount < planesThatDontNeedToLand && i > 0) {
// Increase number of trailing planes ignored
processedPlaneCount++;
// And skip any processing
continue;
}
// If this is the first carrier seek and not last unit
if (seekedCarrier == null && i > 0) {
final int seekedCarrierIndex = AiUtils.getIndexOfLastUnitMatching(result, Matches.unitIsCarrier().and(Matches.isNotInList(filledCarriers)), result.size() - 1);
if (seekedCarrierIndex == -1) {
// No carriers left
break;
}
seekedCarrier = result.get(seekedCarrierIndex);
// Tell the code to insert carrier to the right of this plane
indexToPlaceCarrierAt = i + 1;
spaceLeftOnSeekedCarrier = UnitAttachment.get(seekedCarrier.getType()).getCarrierCapacity();
}
if (ua.getCarrierCost() > 0) {
spaceLeftOnSeekedCarrier -= ua.getCarrierCost();
}
// If the carrier has been filled or overflowed or last unit
if (indexToPlaceCarrierAt > 0 && (spaceLeftOnSeekedCarrier <= 0 || i == 0)) {
if (spaceLeftOnSeekedCarrier < 0) {
// Increment current unit index, so we re-process this unit (since it can't fit on the current carrier)
i++;
}
// If the seeked carrier is earlier in the list
if (result.indexOf(seekedCarrier) < i) {
// Move the carrier up to the planes by: removing carrier, then reinserting it
// (index decreased cause removal of carrier reduced indexes)
result.remove(seekedCarrier);
result.add(indexToPlaceCarrierAt - 1, seekedCarrier);
// We removed carrier in earlier part of list, so decrease index
i--;
filledCarriers.add(seekedCarrier);
// Find the next carrier
seekedCarrier = AiUtils.getLastUnitMatching(result, Matches.unitIsCarrier().and(Matches.isNotInList(filledCarriers)), result.size() - 1);
if (seekedCarrier == null) {
// No carriers left
break;
}
// Place next carrier right before this plane (which just filled the old carrier that was just moved)
indexToPlaceCarrierAt = i;
spaceLeftOnSeekedCarrier = UnitAttachment.get(seekedCarrier.getType()).getCarrierCapacity();
} else {
// If it's later in the list
final int oldIndex = result.indexOf(seekedCarrier);
int carrierPlaceLocation = indexToPlaceCarrierAt;
// Place carrier where it's supposed to go
result.remove(seekedCarrier);
if (oldIndex < indexToPlaceCarrierAt) {
carrierPlaceLocation--;
}
result.add(carrierPlaceLocation, seekedCarrier);
filledCarriers.add(seekedCarrier);
// Move the planes down to the carrier
final List<Unit> planesBetweenHereAndCarrier = new ArrayList<>();
for (int i2 = i; i2 < carrierPlaceLocation; i2++) {
final Unit unit2 = result.get(i2);
final UnitAttachment ua2 = UnitAttachment.get(unit2.getType());
if (ua2.getCarrierCost() > 0) {
planesBetweenHereAndCarrier.add(unit2);
}
}
// Invert list, so they are inserted in the same order
Collections.reverse(planesBetweenHereAndCarrier);
int planeMoveCount = 0;
for (final Unit plane : planesBetweenHereAndCarrier) {
result.remove(plane);
// Insert each plane right before carrier (index decreased cause removal of carrier reduced indexes)
result.add(carrierPlaceLocation - 1, plane);
planeMoveCount++;
}
// Find the next carrier
seekedCarrier = AiUtils.getLastUnitMatching(result, Matches.unitIsCarrier().and(Matches.isNotInList(filledCarriers)), result.size() - 1);
if (seekedCarrier == null) {
// No carriers left
break;
}
// Since we only moved planes up, just reduce next carrier place index by plane move count
indexToPlaceCarrierAt = carrierPlaceLocation - planeMoveCount;
spaceLeftOnSeekedCarrier = UnitAttachment.get(seekedCarrier.getType()).getCarrierCapacity();
}
}
}
}
return result;
}
use of games.strategy.triplea.attachments.UnitAttachment in project triplea by triplea-game.
the class ProTransportUtils method getAirThatCantLandOnCarrier.
public static List<Unit> getAirThatCantLandOnCarrier(final PlayerID player, final Territory t, final List<Unit> units) {
final GameData data = ProData.getData();
int capacity = AirMovementValidator.carrierCapacity(units, t);
final Collection<Unit> airUnits = CollectionUtils.getMatches(units, ProMatches.unitIsAlliedAir(player, data));
final List<Unit> airThatCantLand = new ArrayList<>();
for (final Unit airUnit : airUnits) {
final UnitAttachment ua = UnitAttachment.get(airUnit.getType());
final int cost = ua.getCarrierCost();
if (cost != -1) {
if (cost <= capacity) {
capacity -= cost;
} else {
airThatCantLand.add(airUnit);
}
}
}
return airThatCantLand;
}
use of games.strategy.triplea.attachments.UnitAttachment in project triplea by triplea-game.
the class ProTransportUtils method getUnusedCarrierCapacity.
public static int getUnusedCarrierCapacity(final PlayerID player, final Territory t, final List<Unit> unitsToPlace) {
final List<Unit> units = new ArrayList<>(unitsToPlace);
units.addAll(t.getUnits().getUnits());
int capacity = AirMovementValidator.carrierCapacity(units, t);
final Collection<Unit> airUnits = CollectionUtils.getMatches(units, ProMatches.unitIsOwnedAir(player));
for (final Unit airUnit : airUnits) {
final UnitAttachment ua = UnitAttachment.get(airUnit.getType());
final int cost = ua.getCarrierCost();
if (cost != -1) {
capacity -= cost;
}
}
return capacity;
}
use of games.strategy.triplea.attachments.UnitAttachment in project triplea by triplea-game.
the class ProTransportUtils method validateCarrierCapacity.
public static boolean validateCarrierCapacity(final PlayerID player, final Territory t, final List<Unit> existingUnits, final Unit newUnit) {
final GameData data = ProData.getData();
int capacity = AirMovementValidator.carrierCapacity(existingUnits, t);
final Collection<Unit> airUnits = CollectionUtils.getMatches(existingUnits, ProMatches.unitIsAlliedAir(player, data));
airUnits.add(newUnit);
for (final Unit airUnit : airUnits) {
final UnitAttachment ua = UnitAttachment.get(airUnit.getType());
final int cost = ua.getCarrierCost();
if (cost != -1) {
capacity -= cost;
}
}
return capacity >= 0;
}
Aggregations