Search in sources :

Example 16 with UnitAttachment

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;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) Territory(games.strategy.engine.data.Territory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

Example 17 with UnitAttachment

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;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

Example 18 with UnitAttachment

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;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

Example 19 with UnitAttachment

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;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

Example 20 with UnitAttachment

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;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) GameData(games.strategy.engine.data.GameData) Unit(games.strategy.engine.data.Unit)

Aggregations

UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)60 Unit (games.strategy.engine.data.Unit)45 ArrayList (java.util.ArrayList)25 TripleAUnit (games.strategy.triplea.TripleAUnit)23 UnitType (games.strategy.engine.data.UnitType)20 GameData (games.strategy.engine.data.GameData)19 Territory (games.strategy.engine.data.Territory)19 PlayerID (games.strategy.engine.data.PlayerID)12 CompositeChange (games.strategy.engine.data.CompositeChange)8 TerritoryAttachment (games.strategy.triplea.attachments.TerritoryAttachment)7 IntegerMap (games.strategy.util.IntegerMap)6 Collection (java.util.Collection)6 Set (java.util.Set)6 Change (games.strategy.engine.data.Change)5 Resource (games.strategy.engine.data.Resource)5 Tuple (games.strategy.util.Tuple)5 HashSet (java.util.HashSet)5 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)4 Route (games.strategy.engine.data.Route)4 List (java.util.List)4