Search in sources :

Example 91 with Unit

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

the class EditValidator method validateChangeHitDamage.

static String validateChangeHitDamage(final GameData data, final IntegerMap<Unit> unitDamageMap, final Territory territory) {
    if (unitDamageMap == null || unitDamageMap.isEmpty()) {
        return "Damage map is empty";
    }
    final String result = validateTerritoryBasic(data, territory);
    if (result != null) {
        return result;
    }
    final Collection<Unit> units = new ArrayList<>(unitDamageMap.keySet());
    if (!territory.getUnits().getUnits().containsAll(units)) {
        return "Selected Territory does not contain all of the selected units";
    }
    final PlayerID player = units.iterator().next().getOwner();
    // all units should be same owner
    if (units.isEmpty() || !units.stream().allMatch(Matches.unitIsOwnedBy(player))) {
        return "Not all units have the same owner";
    }
    if (units.isEmpty() || !units.stream().allMatch(Matches.unitHasMoreThanOneHitPointTotal())) {
        return "Not all units have more than one total hitpoints";
    }
    for (final Unit u : units) {
        final int dmg = unitDamageMap.getInt(u);
        if (dmg < 0 || dmg >= UnitAttachment.get(u.getType()).getHitPoints()) {
            return "Damage cannot be less than zero or equal to or greater than unit hitpoints (if you want to kill the " + "unit, use remove unit)";
        }
    }
    return null;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit)

Example 92 with Unit

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

the class InitializationDelegate method initOriginalOwner.

private static void initOriginalOwner(final IDelegateBridge bridge) {
    final GameData data = bridge.getData();
    final CompositeChange changes = new CompositeChange();
    for (final Territory current : data.getMap()) {
        if (!current.getOwner().isNull()) {
            final TerritoryAttachment territoryAttachment = TerritoryAttachment.get(current);
            if (territoryAttachment == null) {
                throw new IllegalStateException("No territory attachment for " + current);
            }
            if (territoryAttachment.getOriginalOwner() == null && current.getOwner() != null) {
                changes.add(OriginalOwnerTracker.addOriginalOwnerChange(current, current.getOwner()));
            }
            final Collection<Unit> factoryAndInfrastructure = current.getUnits().getMatches(Matches.unitIsInfrastructure());
            changes.add(OriginalOwnerTracker.addOriginalOwnerChange(factoryAndInfrastructure, current.getOwner()));
        } else if (!current.isWater()) {
            final TerritoryAttachment territoryAttachment = TerritoryAttachment.get(current);
            if (territoryAttachment == null) {
                throw new IllegalStateException("No territory attachment for " + current);
            }
        }
    }
    bridge.getHistoryWriter().startEvent("Adding original owners");
    bridge.addChange(changes);
}
Also used : Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 93 with Unit

use of games.strategy.engine.data.Unit 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 94 with Unit

use of games.strategy.engine.data.Unit 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 95 with Unit

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

the class ProTransportUtils method getUnitsToTransportThatCantMoveToHigherValue.

public static List<Unit> getUnitsToTransportThatCantMoveToHigherValue(final PlayerID player, final Unit transport, final Set<Territory> territoriesToLoadFrom, final List<Unit> unitsToIgnore, final Map<Territory, ProTerritory> moveMap, final Map<Unit, Set<Territory>> unitMoveMap, final double value) {
    final List<Unit> unitsToIgnoreOrHaveBetterLandMove = new ArrayList<>(unitsToIgnore);
    if (!TransportTracker.isTransporting(transport)) {
        // Get all units that can be transported
        final List<Unit> units = new ArrayList<>();
        for (final Territory loadFrom : territoriesToLoadFrom) {
            units.addAll(loadFrom.getUnits().getMatches(ProMatches.unitIsOwnedTransportableUnitAndCanBeLoaded(player, transport, true)));
        }
        units.removeAll(unitsToIgnore);
        // Check to see which have higher land move value
        for (final Unit u : units) {
            if (unitMoveMap.get(u) != null) {
                for (final Territory t : unitMoveMap.get(u)) {
                    if (moveMap.get(t) != null && moveMap.get(t).getValue() > value) {
                        unitsToIgnoreOrHaveBetterLandMove.add(u);
                        break;
                    }
                }
            }
        }
    }
    return getUnitsToTransportFromTerritories(player, transport, territoriesToLoadFrom, unitsToIgnoreOrHaveBetterLandMove);
}
Also used : Territory(games.strategy.engine.data.Territory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

Aggregations

Unit (games.strategy.engine.data.Unit)447 TripleAUnit (games.strategy.triplea.TripleAUnit)301 Territory (games.strategy.engine.data.Territory)255 ArrayList (java.util.ArrayList)204 PlayerID (games.strategy.engine.data.PlayerID)135 GameData (games.strategy.engine.data.GameData)103 HashSet (java.util.HashSet)92 Test (org.junit.jupiter.api.Test)91 Route (games.strategy.engine.data.Route)89 UnitType (games.strategy.engine.data.UnitType)85 CompositeChange (games.strategy.engine.data.CompositeChange)64 HashMap (java.util.HashMap)64 IntegerMap (games.strategy.util.IntegerMap)61 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)58 Collection (java.util.Collection)58 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)56 List (java.util.List)48 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)47 Change (games.strategy.engine.data.Change)44 Set (java.util.Set)43