Skip to content

Commit

Permalink
Small cleanups to placement code. (#11886)
Browse files Browse the repository at this point in the history
* Small cleanups to placement code.

No logic changes.

* Mirror change in another file.
  • Loading branch information
asvitkine authored Aug 18, 2023
1 parent 221f475 commit 5ad0d79
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package games.strategy.triplea.delegate;

import static games.strategy.triplea.delegate.move.validation.UnitStackingLimitFilter.PLACEMENT_LIMIT;
import static java.util.function.Predicate.not;

import games.strategy.engine.data.Change;
import games.strategy.engine.data.CompositeChange;
Expand Down Expand Up @@ -884,10 +885,10 @@ protected Collection<Unit> getUnitsToBePlaced(
&& to.anyUnitsMatch(Matches.enemyUnit(player))) {
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> units =
CollectionUtils.getMatches(
allUnits, water ? not(Matches.unitIsLand()) : not(Matches.unitIsSea()));
final Collection<Unit> placeableUnits = new ArrayList<>();
final Collection<Unit> unitsAtStartOfTurnInTo = unitsAtStartOfStepInTerritory(to);
final Collection<Unit> allProducedUnits = unitsPlacedInTerritorySoFar(to);
Expand Down Expand Up @@ -944,17 +945,10 @@ protected Collection<Unit> getUnitsToBePlaced(
}
// 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);
}
}
}
placeableUnits.removeIf(
Matches.unitConsumesUnitsOnCreation()
.and(not(Matches.unitWhichConsumesUnitsHasRequiredUnits(unitsAtStartOfTurnInTo))));

final Collection<Unit> placeableUnits2;
if (Properties.getUnitPlacementRestrictions(properties)) {
final int territoryProduction = TerritoryAttachment.getProduction(to);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package games.strategy.triplea.delegate;

import static games.strategy.triplea.delegate.move.validation.UnitStackingLimitFilter.PLACEMENT_LIMIT;
import static java.util.function.Predicate.not;

import games.strategy.engine.data.GamePlayer;
import games.strategy.engine.data.Territory;
Expand Down Expand Up @@ -147,17 +148,9 @@ protected Collection<Unit> getUnitsToBePlaced(
}
}
// 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);
}
}
}
placeableUnits.removeIf(
Matches.unitConsumesUnitsOnCreation()
.and(not(Matches.unitWhichConsumesUnitsHasRequiredUnits(unitsAtStartOfTurnInTo))));
// now check stacking limits
return UnitStackingLimitFilter.filterUnits(placeableUnits, PLACEMENT_LIMIT, player, to);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import games.strategy.engine.data.Territory;
import games.strategy.engine.data.Unit;
import games.strategy.engine.data.events.GameDataChangeListener;
import games.strategy.engine.data.properties.GameProperties;
import games.strategy.engine.player.PlayerBridge;
import games.strategy.triplea.Properties;
import games.strategy.triplea.attachments.PlayerAttachment;
Expand Down Expand Up @@ -219,9 +220,10 @@ private PlaceableUnits getUnitsToPlace(final Territory territory) {
// get the units that can be placed on this territory.
Collection<Unit> units = getCurrentPlayer().getUnits();
if (territory.isWater()) {
if (!(Properties.getProduceFightersOnCarriers(getData().getProperties())
|| Properties.getProduceNewFightersOnOldCarriers(getData().getProperties())
|| Properties.getLhtrCarrierProductionRules(getData().getProperties())
GameProperties properties = getData().getProperties();
if (!(Properties.getProduceFightersOnCarriers(properties)
|| Properties.getProduceNewFightersOnOldCarriers(properties)
|| Properties.getLhtrCarrierProductionRules(properties)
|| GameStepPropertiesHelper.isBid(getData()))) {
units = CollectionUtils.getMatches(units, Matches.unitIsSea());
} else {
Expand Down

0 comments on commit 5ad0d79

Please sign in to comment.