use of games.strategy.engine.data.Unit 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;
}
use of games.strategy.engine.data.Unit 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;
}
use of games.strategy.engine.data.Unit 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;
}
use of games.strategy.engine.data.Unit in project triplea by triplea-game.
the class AbstractPlaceDelegate method canProduce.
/**
* Tests if this territory can produce units. (Does not check if it has space left to do so)
*
* @param producer
* - Territory doing the producing.
* @param to
* - Territory to be placed in.
* @param units
* - Units to be placed.
* @param player
* - Player doing the placing.
* @param simpleCheck
* - If true you return true even if a factory is not present. Used when you do not want an infinite loop
* (getAllProducers ->
* canProduce -> howManyOfEachConstructionCanPlace -> getAllProducers -> etc)
* @return - null if allowed to produce, otherwise an error String.
*/
protected String canProduce(final Territory producer, final Territory to, final Collection<Unit> units, final PlayerID player, final boolean simpleCheck) {
// units can be null if we are just testing the territory itself...
final Collection<Unit> testUnits = (units == null ? new ArrayList<>() : units);
final boolean canProduceInConquered = isPlacementAllowedInCapturedTerritory(player);
if (!producer.getOwner().equals(player)) {
// sea constructions require either owning the sea zone or owning a surrounding land territory
if (producer.isWater() && testUnits.stream().anyMatch(Matches.unitIsSea().and(Matches.unitIsConstruction()))) {
boolean ownedNeighbor = false;
for (final Territory current : getData().getMap().getNeighbors(to, Matches.territoryIsLand())) {
if (current.getOwner().equals(player) && (canProduceInConquered || !wasConquered(current))) {
ownedNeighbor = true;
break;
}
}
if (!ownedNeighbor) {
return producer.getName() + " is not owned by you, and you have no owned neighbors which can produce";
}
} else {
return producer.getName() + " is not owned by you";
}
}
// make sure the territory wasnt conquered this turn
if (!canProduceInConquered && wasConquered(producer)) {
return producer.getName() + " was conquered this turn and cannot produce till next turn";
}
if (isPlayerAllowedToPlacementAnyTerritoryOwnedLand(player) && Matches.territoryIsLand().test(to) && Matches.isTerritoryOwnedBy(player).test(to)) {
return null;
}
if (isPlayerAllowedToPlacementAnySeaZoneByOwnedLand(player) && Matches.territoryIsWater().test(to) && Matches.isTerritoryOwnedBy(player).test(producer)) {
return null;
}
if (simpleCheck) {
return null;
}
// make sure some unit has fullfilled requiresUnits requirements
if (isUnitPlacementRestrictions() && !testUnits.isEmpty() && testUnits.stream().noneMatch(unitWhichRequiresUnitsHasRequiredUnits(producer, true))) {
return "You do not have the required units to build in " + producer.getName();
}
if (to.isWater() && (!isWW2V2() && !isUnitPlacementInEnemySeas()) && to.getUnits().anyMatch(Matches.enemyUnit(player, getData()))) {
return "Cannot place sea units with enemy naval units";
}
// make sure there is a factory
if (wasOwnedUnitThatCanProduceUnitsOrIsFactoryInTerritoryAtStartOfStep(producer, player)) {
return null;
}
// check to see if we are producing a factory or construction
if (testUnits.stream().anyMatch(Matches.unitIsConstruction())) {
if (howManyOfEachConstructionCanPlace(to, producer, testUnits, player).totalValues() > 0) {
return null;
}
return "No more Constructions Allowed in " + producer.getName();
}
// check we havent just put a factory there (should we be checking producer?)
if (getAlreadyProduced(producer).stream().anyMatch(Matches.unitCanProduceUnits()) || getAlreadyProduced(to).stream().anyMatch(Matches.unitCanProduceUnits())) {
return "Factory in " + producer.getName() + " cant produce until 1 turn after it is created";
}
return "No Factory in " + producer.getName();
}
use of games.strategy.engine.data.Unit in project triplea by triplea-game.
the class AbstractPlaceDelegate method getMaxUnitsToBePlacedMap.
/**
* Returns -1 somewhere in the map if can place unlimited units.
*/
protected IntegerMap<Territory> getMaxUnitsToBePlacedMap(final Collection<Unit> units, final Territory to, final PlayerID player, final boolean countSwitchedProductionToNeighbors) {
final IntegerMap<Territory> maxUnitsToBePlacedMap = new IntegerMap<>();
final List<Territory> producers = getAllProducers(to, player, units);
if (producers.isEmpty()) {
return maxUnitsToBePlacedMap;
}
producers.sort(getBestProducerComparator(to, units, player));
final Collection<Territory> notUsableAsOtherProducers = new ArrayList<>(producers);
final Map<Territory, Integer> currentAvailablePlacementForOtherProducers = new HashMap<>();
for (final Territory producerTerritory : producers) {
final Collection<Unit> unitsCanBePlacedByThisProducer = (isUnitPlacementRestrictions() ? CollectionUtils.getMatches(units, unitWhichRequiresUnitsHasRequiredUnits(producerTerritory, true)) : new ArrayList<>(units));
final int prodT = getMaxUnitsToBePlacedFrom(producerTerritory, unitsCanBePlacedByThisProducer, to, player, countSwitchedProductionToNeighbors, notUsableAsOtherProducers, currentAvailablePlacementForOtherProducers);
maxUnitsToBePlacedMap.put(producerTerritory, prodT);
}
return maxUnitsToBePlacedMap;
}
Aggregations