Search in sources :

Example 96 with Unit

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

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

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

the class ProTransportUtils method getUnitsToTransportFromTerritories.

// TODO: this needs fixed to consider whether a valid route exists to load all units
public static List<Unit> getUnitsToTransportFromTerritories(final PlayerID player, final Unit transport, final Set<Territory> territoriesToLoadFrom, final List<Unit> unitsToIgnore, final Predicate<Unit> validUnitMatch) {
    final List<Unit> selectedUnits = new ArrayList<>();
    // Get units if transport already loaded
    if (TransportTracker.isTransporting(transport)) {
        selectedUnits.addAll(TransportTracker.transporting(transport));
    } else {
        // Get all units that can be transported
        final List<Unit> units = new ArrayList<>();
        for (final Territory loadFrom : territoriesToLoadFrom) {
            units.addAll(loadFrom.getUnits().getMatches(validUnitMatch));
        }
        units.removeAll(unitsToIgnore);
        // Sort units by attack
        units.sort((o1, o2) -> {
            // Very rough way to add support power
            final Set<UnitSupportAttachment> supportAttachments1 = UnitSupportAttachment.get(o1.getType());
            int maxSupport1 = 0;
            for (final UnitSupportAttachment usa : supportAttachments1) {
                if (usa.getAllied() && usa.getOffence() && usa.getBonus() > maxSupport1) {
                    maxSupport1 = usa.getBonus();
                }
            }
            final int attack1 = UnitAttachment.get(o1.getType()).getAttack(player) + maxSupport1;
            final Set<UnitSupportAttachment> supportAttachments2 = UnitSupportAttachment.get(o2.getType());
            int maxSupport2 = 0;
            for (final UnitSupportAttachment usa : supportAttachments2) {
                if (usa.getAllied() && usa.getOffence() && usa.getBonus() > maxSupport2) {
                    maxSupport2 = usa.getBonus();
                }
            }
            final int attack2 = UnitAttachment.get(o2.getType()).getAttack(player) + maxSupport2;
            return attack2 - attack1;
        });
        // Get best units that can be loaded
        selectedUnits.addAll(selectUnitsToTransportFromList(transport, units));
    }
    return selectedUnits;
}
Also used : Territory(games.strategy.engine.data.Territory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) UnitSupportAttachment(games.strategy.triplea.attachments.UnitSupportAttachment) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

Example 99 with Unit

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

Example 100 with Unit

use of games.strategy.engine.data.Unit 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);
    }
}
Also used : Territory(games.strategy.engine.data.Territory) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route)

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