Search in sources :

Example 6 with UnitType

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

the class BidPlaceDelegate method getUnitsToBePlacedLand.

// Return collection of bid units which can placed in a land territory
@Override
protected Collection<Unit> getUnitsToBePlacedLand(final Territory to, final Collection<Unit> units, final PlayerID player) {
    final Collection<Unit> unitsAtStartOfTurnInTo = unitsAtStartOfStepInTerritory(to);
    final Collection<Unit> placeableUnits = new ArrayList<>();
    // we add factories and constructions later
    final Predicate<Unit> groundUnits = Matches.unitIsLand().and(Matches.unitIsNotConstruction());
    final Predicate<Unit> airUnits = Matches.unitIsAir().and(Matches.unitIsNotConstruction());
    placeableUnits.addAll(CollectionUtils.getMatches(units, groundUnits));
    placeableUnits.addAll(CollectionUtils.getMatches(units, airUnits));
    if (units.stream().anyMatch(Matches.unitIsConstruction())) {
        final IntegerMap<String> constructionsMap = howManyOfEachConstructionCanPlace(to, to, units, player);
        final Collection<Unit> skipUnit = new ArrayList<>();
        for (final Unit currentUnit : CollectionUtils.getMatches(units, Matches.unitIsConstruction())) {
            final int maxUnits = howManyOfConstructionUnit(currentUnit, constructionsMap);
            if (maxUnits > 0) {
                // max placement by constructionType of each unitType
                if (skipUnit.contains(currentUnit)) {
                    continue;
                }
                placeableUnits.addAll(CollectionUtils.getNMatches(units, maxUnits, Matches.unitIsOfType(currentUnit.getType())));
                skipUnit.addAll(CollectionUtils.getMatches(units, Matches.unitIsOfType(currentUnit.getType())));
            }
        }
    }
    // remove any units that require other units to be consumed on creation (veqryn)
    if (placeableUnits.stream().anyMatch(Matches.unitConsumesUnitsOnCreation())) {
        final Collection<Unit> unitsWhichConsume = CollectionUtils.getMatches(placeableUnits, Matches.unitConsumesUnitsOnCreation());
        for (final Unit unit : unitsWhichConsume) {
            if (Matches.unitWhichConsumesUnitsHasRequiredUnits(unitsAtStartOfTurnInTo).negate().test(unit)) {
                placeableUnits.remove(unit);
            }
        }
    }
    // now check stacking limits
    final Collection<Unit> placeableUnits2 = new ArrayList<>();
    final Collection<UnitType> typesAlreadyChecked = new ArrayList<>();
    for (final Unit currentUnit : placeableUnits) {
        final UnitType ut = currentUnit.getType();
        if (typesAlreadyChecked.contains(ut)) {
            continue;
        }
        typesAlreadyChecked.add(ut);
        placeableUnits2.addAll(CollectionUtils.getNMatches(placeableUnits, UnitAttachment.getMaximumNumberOfThisUnitTypeToReachStackingLimit("placementLimit", ut, to, player, getData()), Matches.unitIsOfType(ut)));
    }
    return placeableUnits2;
}
Also used : UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

Example 7 with UnitType

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

the class AbstractPlaceDelegate method getUnitsToBePlacedAllDefault.

protected Collection<Unit> getUnitsToBePlacedAllDefault(final Territory to, final Collection<Unit> allUnits, final PlayerID player) {
    final boolean water = to.isWater();
    if (water && (!isWW2V2() && !isUnitPlacementInEnemySeas()) && to.getUnits().anyMatch(Matches.enemyUnit(player, getData()))) {
        return null;
    }
    final Collection<Unit> units = new ArrayList<>(allUnits);
    // if water, remove land. if land, remove water.
    units.removeAll(CollectionUtils.getMatches(units, water ? Matches.unitIsLand() : Matches.unitIsSea()));
    final Collection<Unit> placeableUnits = new ArrayList<>();
    final Collection<Unit> unitsAtStartOfTurnInTo = unitsAtStartOfStepInTerritory(to);
    final Collection<Unit> allProducedUnits = unitsPlacedInTerritorySoFar(to);
    final boolean isBid = GameStepPropertiesHelper.isBid(getData());
    final boolean wasFactoryThereAtStart = wasOwnedUnitThatCanProduceUnitsOrIsFactoryInTerritoryAtStartOfStep(to, player);
    // we add factories and constructions later
    if (water || wasFactoryThereAtStart || (!water && isPlayerAllowedToPlacementAnyTerritoryOwnedLand(player))) {
        final Predicate<Unit> seaOrLandMatch = water ? Matches.unitIsSea() : Matches.unitIsLand();
        placeableUnits.addAll(CollectionUtils.getMatches(units, seaOrLandMatch.and(Matches.unitIsNotConstruction())));
        if (!water) {
            placeableUnits.addAll(CollectionUtils.getMatches(units, Matches.unitIsAir().and(Matches.unitIsNotConstruction())));
        } else if (((isBid || canProduceFightersOnCarriers() || AirThatCantLandUtil.isLhtrCarrierProduction(getData())) && allProducedUnits.stream().anyMatch(Matches.unitIsCarrier())) || ((isBid || canProduceNewFightersOnOldCarriers() || AirThatCantLandUtil.isLhtrCarrierProduction(getData())) && to.getUnits().anyMatch(Matches.unitIsCarrier()))) {
            placeableUnits.addAll(CollectionUtils.getMatches(units, Matches.unitIsAir().and(Matches.unitCanLandOnCarrier())));
        }
    }
    if (units.stream().anyMatch(Matches.unitIsConstruction())) {
        final IntegerMap<String> constructionsMap = howManyOfEachConstructionCanPlace(to, to, units, player);
        final Collection<Unit> skipUnits = new ArrayList<>();
        for (final Unit currentUnit : CollectionUtils.getMatches(units, Matches.unitIsConstruction())) {
            final int maxUnits = howManyOfConstructionUnit(currentUnit, constructionsMap);
            if (maxUnits > 0) {
                // max placement by constructionType of each unitType
                if (skipUnits.contains(currentUnit)) {
                    continue;
                }
                placeableUnits.addAll(CollectionUtils.getNMatches(units, maxUnits, Matches.unitIsOfType(currentUnit.getType())));
                skipUnits.addAll(CollectionUtils.getMatches(units, Matches.unitIsOfType(currentUnit.getType())));
            }
        }
    }
    // remove any units that require other units to be consumed on creation, if we don't have enough to consume (veqryn)
    if (placeableUnits.stream().anyMatch(Matches.unitConsumesUnitsOnCreation())) {
        final Collection<Unit> unitsWhichConsume = CollectionUtils.getMatches(placeableUnits, Matches.unitConsumesUnitsOnCreation());
        for (final Unit unit : unitsWhichConsume) {
            if (Matches.unitWhichConsumesUnitsHasRequiredUnits(unitsAtStartOfTurnInTo).negate().test(unit)) {
                placeableUnits.remove(unit);
            }
        }
    }
    // now check stacking limits
    final Collection<Unit> placeableUnits2 = new ArrayList<>();
    final Collection<UnitType> typesAlreadyChecked = new ArrayList<>();
    for (final Unit currentUnit : placeableUnits) {
        final UnitType ut = currentUnit.getType();
        if (typesAlreadyChecked.contains(ut)) {
            continue;
        }
        typesAlreadyChecked.add(ut);
        placeableUnits2.addAll(CollectionUtils.getNMatches(placeableUnits, UnitAttachment.getMaximumNumberOfThisUnitTypeToReachStackingLimit("placementLimit", ut, to, player, getData()), Matches.unitIsOfType(ut)));
    }
    if (!isUnitPlacementRestrictions()) {
        return placeableUnits2;
    }
    final Collection<Unit> placeableUnits3 = new ArrayList<>();
    for (final Unit currentUnit : placeableUnits2) {
        final UnitAttachment ua = UnitAttachment.get(currentUnit.getType());
        // Can be null!
        final TerritoryAttachment ta = TerritoryAttachment.get(to);
        if (ua.getCanOnlyBePlacedInTerritoryValuedAtX() != -1 && ua.getCanOnlyBePlacedInTerritoryValuedAtX() > (ta == null ? 0 : ta.getProduction())) {
            continue;
        }
        if (unitWhichRequiresUnitsHasRequiredUnits(to, false).negate().test(currentUnit)) {
            continue;
        }
        if (Matches.unitCanOnlyPlaceInOriginalTerritories().test(currentUnit) && !Matches.territoryIsOriginallyOwnedBy(player).test(to)) {
            continue;
        }
        // account for any unit placement restrictions by territory
        final String[] terrs = ua.getUnitPlacementRestrictions();
        final Collection<Territory> listedTerrs = getListedTerritories(terrs);
        if (!listedTerrs.contains(to)) {
            placeableUnits3.add(currentUnit);
        }
    }
    return placeableUnits3;
}
Also used : Territory(games.strategy.engine.data.Territory) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) ArrayList(java.util.ArrayList) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) UnitType(games.strategy.engine.data.UnitType)

Example 8 with UnitType

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

the class AbstractPlaceDelegate method canUnitsBePlaced.

public String canUnitsBePlaced(final Territory to, final Collection<Unit> units, final PlayerID player) {
    final Collection<Unit> allowedUnits = getUnitsToBePlaced(to, units, player);
    if (allowedUnits == null || !allowedUnits.containsAll(units)) {
        return "Cannot place these units in " + to.getName();
    }
    final IntegerMap<String> constructionMap = howManyOfEachConstructionCanPlace(to, to, units, player);
    for (final Unit currentUnit : CollectionUtils.getMatches(units, Matches.unitIsConstruction())) {
        final UnitAttachment ua = UnitAttachment.get(currentUnit.getType());
        /*
       * if (ua.getIsFactory() && !ua.getIsConstruction())
       * constructionMap.add("factory", -1);
       * else
       */
        constructionMap.add(ua.getConstructionType(), -1);
    }
    if (!constructionMap.isPositive()) {
        return "Too many constructions in " + to.getName();
    }
    final List<Territory> capitalsListOwned = new ArrayList<>(TerritoryAttachment.getAllCurrentlyOwnedCapitals(player, getData()));
    if (!capitalsListOwned.contains(to) && isPlacementInCapitalRestricted(player)) {
        return "Cannot place these units outside of the capital";
    }
    if (to.isWater()) {
        final String canLand = validateNewAirCanLandOnCarriers(to, units);
        if (canLand != null) {
            return canLand;
        }
    } else {
        // make sure we own the territory
        if (!to.getOwner().equals(player)) {
            if (GameStepPropertiesHelper.isBid(getData())) {
                final PlayerAttachment pa = PlayerAttachment.get(to.getOwner());
                if ((pa == null || pa.getGiveUnitControl() == null || !pa.getGiveUnitControl().contains(player)) && !to.getUnits().anyMatch(Matches.unitIsOwnedBy(player))) {
                    return "You don't own " + to.getName();
                }
            } else {
                return "You don't own " + to.getName();
            }
        }
        // make sure all units are land
        if (units.isEmpty() || !units.stream().allMatch(Matches.unitIsNotSea())) {
            return "Cant place sea units on land";
        }
    }
    // make sure we can place consuming units
    if (!canWeConsumeUnits(units, to, false, null)) {
        return "Not Enough Units To Upgrade or Be Consumed";
    }
    // now check for stacking limits
    final Collection<UnitType> typesAlreadyChecked = new ArrayList<>();
    for (final Unit currentUnit : units) {
        final UnitType ut = currentUnit.getType();
        if (typesAlreadyChecked.contains(ut)) {
            continue;
        }
        typesAlreadyChecked.add(ut);
        final int maxForThisType = UnitAttachment.getMaximumNumberOfThisUnitTypeToReachStackingLimit("placementLimit", ut, to, player, getData());
        if (CollectionUtils.countMatches(units, Matches.unitIsOfType(ut)) > maxForThisType) {
            return "UnitType " + ut.getName() + " is over stacking limit of " + maxForThisType;
        }
    }
    if (!PlayerAttachment.getCanTheseUnitsMoveWithoutViolatingStackingLimit("placementLimit", units, to, player, getData())) {
        return "Units Cannot Go Over Stacking Limit";
    }
    // now return null (valid placement) if we have placement restrictions disabled in game options
    if (!isUnitPlacementRestrictions()) {
        return null;
    }
    // account for any unit placement restrictions by territory
    for (final Unit currentUnit : units) {
        final UnitAttachment ua = UnitAttachment.get(currentUnit.getType());
        // Can be null!
        final TerritoryAttachment ta = TerritoryAttachment.get(to);
        if (ua.getCanOnlyBePlacedInTerritoryValuedAtX() != -1 && ua.getCanOnlyBePlacedInTerritoryValuedAtX() > (ta == null ? 0 : ta.getProduction())) {
            return "Cannot place these units in " + to.getName() + " due to Unit Placement Restrictions on Territory Value";
        }
        final String[] terrs = ua.getUnitPlacementRestrictions();
        final Collection<Territory> listedTerrs = getListedTerritories(terrs);
        if (listedTerrs.contains(to)) {
            return "Cannot place these units in " + to.getName() + " due to Unit Placement Restrictions";
        }
        if (Matches.unitCanOnlyPlaceInOriginalTerritories().test(currentUnit) && !Matches.territoryIsOriginallyOwnedBy(player).test(to)) {
            return "Cannot place these units in " + to.getName() + " as territory is not originally owned";
        }
    }
    return null;
}
Also used : Territory(games.strategy.engine.data.Territory) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) ArrayList(java.util.ArrayList) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) PlayerAttachment(games.strategy.triplea.attachments.PlayerAttachment) UnitType(games.strategy.engine.data.UnitType)

Example 9 with UnitType

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

the class TerritoryEffectAttachment method setUnitsNotAllowed.

private void setUnitsNotAllowed(final String unitsNotAllowedUnitTypes) throws GameParseException {
    final String[] s = unitsNotAllowedUnitTypes.split(":");
    if (s.length < 1) {
        throw new GameParseException("unitsNotAllowed must have at least one unitType" + thisErrorMsg());
    }
    for (final String unitTypeName : s) {
        final UnitType ut = getData().getUnitTypeList().getUnitType(unitTypeName);
        if (ut == null) {
            throw new GameParseException("No unit called:" + unitTypeName + thisErrorMsg());
        }
        m_unitsNotAllowed.add(ut);
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) GameParseException(games.strategy.engine.data.GameParseException)

Example 10 with UnitType

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

the class TerritoryEffectAttachment method setCombatEffect.

@InternalDoNotExport
private void setCombatEffect(final String combatEffect, final boolean defending) throws GameParseException {
    final String[] s = combatEffect.split(":");
    if (s.length < 2) {
        throw new GameParseException("combatDefenseEffect and combatOffenseEffect must have a count and at least one unitType" + thisErrorMsg());
    }
    final Iterator<String> iter = Arrays.asList(s).iterator();
    final int effect = getInt(iter.next());
    while (iter.hasNext()) {
        final String unitTypeToProduce = iter.next();
        final UnitType ut = getData().getUnitTypeList().getUnitType(unitTypeToProduce);
        if (ut == null) {
            throw new GameParseException("No unit called:" + unitTypeToProduce + thisErrorMsg());
        }
        if (defending) {
            m_combatDefenseEffect.put(ut, effect);
        } else {
            m_combatOffenseEffect.put(ut, effect);
        }
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) GameParseException(games.strategy.engine.data.GameParseException) InternalDoNotExport(games.strategy.engine.data.annotations.InternalDoNotExport)

Aggregations

UnitType (games.strategy.engine.data.UnitType)211 Test (org.junit.jupiter.api.Test)108 IntegerMap (games.strategy.util.IntegerMap)86 Route (games.strategy.engine.data.Route)76 Unit (games.strategy.engine.data.Unit)76 PlayerID (games.strategy.engine.data.PlayerID)64 Territory (games.strategy.engine.data.Territory)58 TripleAUnit (games.strategy.triplea.TripleAUnit)49 ArrayList (java.util.ArrayList)44 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)36 GameParseException (games.strategy.engine.data.GameParseException)29 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)23 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)21 HashSet (java.util.HashSet)17 GameData (games.strategy.engine.data.GameData)15 Change (games.strategy.engine.data.Change)14 CompositeChange (games.strategy.engine.data.CompositeChange)14 Resource (games.strategy.engine.data.Resource)13 NamedAttachable (games.strategy.engine.data.NamedAttachable)11 ProductionRule (games.strategy.engine.data.ProductionRule)11