Skip to content

Commit

Permalink
Clean up more code to use more concise helper. (#12531)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvitkine committed Apr 21, 2024
1 parent 186c994 commit 4e4642d
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 83 deletions.
Expand Up @@ -368,7 +368,7 @@ private static List<MoveDescription> calculateCombatMoveSea(
if (dontMoveFrom.contains(owned)) {
continue;
}
final List<Unit> units = owned.getUnitCollection().getMatches(attackable);
final List<Unit> units = owned.getMatches(attackable);
unitsAlreadyMoved.addAll(units);
moves.add(new MoveDescription(units, new Route(owned, t)));
}
Expand Down
Expand Up @@ -413,11 +413,8 @@ private static void changeUnitOwnership(final IDelegateBridge bridge) {
newOwners.retainAll(possibleNewOwners);
for (final GamePlayer newOwner : newOwners) {
final Collection<Unit> units =
currTerritory
.getUnitCollection()
.getMatches(
Matches.unitIsOwnedBy(player)
.and(Matches.unitCanBeGivenByTerritoryTo(newOwner)));
currTerritory.getMatches(
Matches.unitIsOwnedBy(player).and(Matches.unitCanBeGivenByTerritoryTo(newOwner)));
if (!units.isEmpty()) {
change.add(ChangeFactory.changeOwner(units, newOwner, currTerritory));
changeList.add(Tuple.of(currTerritory, units));
Expand Down
Expand Up @@ -545,7 +545,7 @@ private void freePlacementCapacity(
if (getAlreadyProduced(producer).stream().anyMatch(Matches.unitCanProduceUnits())) {
return null;
}
final List<Unit> fighters = producer.getUnitCollection().getMatches(ownedFighters);
final List<Unit> fighters = producer.getMatches(ownedFighters);
final Collection<Unit> movedFighters =
bridge.getRemotePlayer().getNumberOfFightersToMoveToNewCarrier(fighters, producer);
if (movedFighters == null || movedFighters.isEmpty()) {
Expand Down Expand Up @@ -1145,7 +1145,7 @@ protected int getMaxUnitsToBePlacedFrom(
Matches.unitIsOwnedAndIsFactoryOrCanProduceUnits(player)
.and(Matches.unitIsBeingTransported().negate())
.and(producer.isWater() ? Matches.unitIsLand().negate() : Matches.unitIsSea().negate());
final Collection<Unit> factoryUnits = producer.getUnitCollection().getMatches(factoryMatch);
final Collection<Unit> factoryUnits = producer.getMatches(factoryMatch);
// boolean placementRestrictedByFactory = isPlacementRestrictedByFactory();
final boolean unitPlacementPerTerritoryRestricted =
Properties.getUnitPlacementPerTerritoryRestricted(properties);
Expand Down Expand Up @@ -1609,8 +1609,7 @@ private boolean wasOwnedUnitThatCanProduceUnitsOrIsFactoryInTerritoryAtStartOfSt
* value may be null.
*/
private GamePlayer getOriginalFactoryOwner(final Territory territory) {
final Collection<Unit> factoryUnits =
territory.getUnitCollection().getMatches(Matches.unitCanProduceUnits());
final Collection<Unit> factoryUnits = territory.getMatches(Matches.unitCanProduceUnits());
if (factoryUnits.isEmpty()) {
throw new IllegalStateException("No factory in territory:" + territory);
}
Expand Down
Expand Up @@ -41,13 +41,13 @@ void removeAirThatCantLand(
final GamePlayer player, final boolean spareAirInSeaZonesBesideFactories) {
final GameState data = bridge.getData();
final GameMap map = data.getMap();
final Predicate<Territory> hasNeighboringFriendlyFactoryMatch =
Matches.territoryHasAlliedIsFactoryOrCanProduceUnits(player);
for (final Territory current : getTerritoriesWhereAirCantLand(player)) {
final Predicate<Unit> ownedAir = Matches.unitIsAir().and(Matches.alliedUnit(player));
final Collection<Unit> air = current.getUnitCollection().getMatches(ownedAir);
final Collection<Unit> air = current.getMatches(ownedAir);
final boolean hasNeighboringFriendlyFactory =
map.getNeighbors(current, Matches.territoryHasAlliedIsFactoryOrCanProduceUnits(player))
.size()
> 0;
!map.getNeighbors(current, hasNeighboringFriendlyFactoryMatch).isEmpty();
final boolean skip =
spareAirInSeaZonesBesideFactories && current.isWater() && hasNeighboringFriendlyFactory;
if (!skip) {
Expand All @@ -64,8 +64,7 @@ private void removeAirThatCantLand(
toRemove.addAll(airUnits);
} else { // on water we may just no have enough carriers
// find the carrier capacity
final Collection<Unit> carriers =
territory.getUnitCollection().getMatches(Matches.alliedUnit(player));
final Collection<Unit> carriers = territory.getMatches(Matches.alliedUnit(player));
int capacity = AirMovementValidator.carrierCapacity(carriers, territory);
for (final Unit unit : airUnits) {
final UnitAttachment ua = unit.getUnitAttachment();
Expand Down
Expand Up @@ -353,7 +353,7 @@ private static void removeMovementFromAirOnDamagedAlliedCarriers(
.and(Matches.unitHasMovementLeft());
final CompositeChange change = new CompositeChange();
for (final Territory t : data.getMap().getTerritories()) {
final Collection<Unit> ownedFighters = t.getUnitCollection().getMatches(ownedFightersMatch);
final Collection<Unit> ownedFighters = t.getMatches(ownedFightersMatch);
if (ownedFighters.isEmpty()) {
continue;
}
Expand Down Expand Up @@ -560,29 +560,28 @@ private static int getLargestRepairRateForThisUnit(
.and(Matches.unitCanRepairOthers())
.and(Matches.unitCanRepairThisUnit(unitToBeRepaired, territoryUnitIsIn));
final Set<Unit> repairUnitsForThisUnit =
new HashSet<>(territoryUnitIsIn.getUnitCollection().getMatches(repairUnit));
new HashSet<>(territoryUnitIsIn.getMatches(repairUnit));
if (Matches.unitIsSea().test(unitToBeRepaired)) {
final List<Territory> neighbors =
new ArrayList<>(data.getMap().getNeighbors(territoryUnitIsIn, Matches.territoryIsLand()));
final Collection<Territory> neighbors =
data.getMap().getNeighbors(territoryUnitIsIn, Matches.territoryIsLand());
for (final Territory current : neighbors) {
final Predicate<Unit> repairUnitLand =
Matches.alliedUnit(owner)
.and(Matches.unitCanRepairOthers())
.and(Matches.unitCanRepairThisUnit(unitToBeRepaired, current))
.and(Matches.unitIsLand());
repairUnitsForThisUnit.addAll(current.getUnitCollection().getMatches(repairUnitLand));
repairUnitsForThisUnit.addAll(current.getMatches(repairUnitLand));
}
} else if (Matches.unitIsLand().test(unitToBeRepaired)) {
final List<Territory> neighbors =
new ArrayList<>(
data.getMap().getNeighbors(territoryUnitIsIn, Matches.territoryIsWater()));
final Collection<Territory> neighbors =
data.getMap().getNeighbors(territoryUnitIsIn, Matches.territoryIsWater());
for (final Territory current : neighbors) {
final Predicate<Unit> repairUnitSea =
Matches.alliedUnit(owner)
.and(Matches.unitCanRepairOthers())
.and(Matches.unitCanRepairThisUnit(unitToBeRepaired, current))
.and(Matches.unitIsSea());
repairUnitsForThisUnit.addAll(current.getUnitCollection().getMatches(repairUnitSea));
repairUnitsForThisUnit.addAll(current.getMatches(repairUnitSea));
}
}
int largest = 0;
Expand Down
Expand Up @@ -176,7 +176,7 @@ public void execute(final ExecutionStack stack, final IDelegateBridge bridge) {
boolean ignoreBattle = false;
// could it be a bombing raid
final Collection<Unit> enemyUnits =
route.getEnd().getUnitCollection().getMatches(Matches.enemyUnit(gamePlayer));
route.getEnd().getMatches(Matches.enemyUnit(gamePlayer));
final Collection<Unit> enemyTargetsTotal =
CollectionUtils.getMatches(
enemyUnits,
Expand Down
Expand Up @@ -121,8 +121,7 @@ private void findRocketTargetsAndFireIfNeeded(
break;
}
final Collection<Unit> enemyUnits =
CollectionUtils.getMatches(
targetTerritory.getUnitCollection(),
targetTerritory.getMatches(
Matches.enemyUnit(player).and(Matches.unitIsBeingTransported().negate()));
final Collection<Unit> enemyTargetsTotal =
CollectionUtils.getMatches(
Expand Down Expand Up @@ -262,9 +261,8 @@ private void fireRocket(
Properties.getDamageFromBombingDoneToUnitsInsteadOfTerritories(data.getProperties());
// unit damage vs territory damage
final Collection<Unit> enemyUnits =
attackedTerritory
.getUnitCollection()
.getMatches(Matches.enemyUnit(player).and(Matches.unitIsBeingTransported().negate()));
attackedTerritory.getMatches(
Matches.enemyUnit(player).and(Matches.unitIsBeingTransported().negate()));
final Collection<Unit> enemyTargetsTotal =
CollectionUtils.getMatches(
enemyUnits, Matches.unitIsAtMaxDamageOrNotCanBeDamaged(attackedTerritory).negate());
Expand Down
Expand Up @@ -139,10 +139,7 @@ public String performMove(final MoveDescription move) {
currentMove.addChange(airborneChange);
// make the bases start filling up their capacity
final Collection<Unit> basesAtStart =
route
.getStart()
.getUnitCollection()
.getMatches(MoveValidator.getAirborneBaseMatch(player, data));
route.getStart().getMatches(MoveValidator.getAirborneBaseMatch(player, data));
final Change fillLaunchCapacity =
getNewAssignmentOfNumberLaunchedChange(units.size(), basesAtStart, player, data);
currentMove.addChange(fillLaunchCapacity);
Expand Down
Expand Up @@ -118,11 +118,10 @@ protected void undoSpecific(final IDelegateBridge bridge) {
if (routeUnitUsedToMove != null) {
final Territory end = routeUnitUsedToMove.getEnd();
final Collection<Unit> enemyTargetsTotal =
end.getUnitCollection()
.getMatches(
Matches.enemyUnit(bridge.getGamePlayer())
.and(Matches.unitCanBeDamaged())
.and(Matches.unitIsBeingTransported().negate()));
end.getMatches(
Matches.enemyUnit(bridge.getGamePlayer())
.and(Matches.unitCanBeDamaged())
.and(Matches.unitIsBeingTransported().negate()));
final Collection<Unit> enemyTargets =
CollectionUtils.getMatches(
enemyTargetsTotal,
Expand Down
Expand Up @@ -29,9 +29,7 @@ Collection<Territory> getTerritoriesWhereUnitsCantFight(final GamePlayer player)
.build();
final int countAllOwnedUnits = current.getUnitCollection().countMatches(ownedUnitsMatch);
final Collection<Unit> nonCombatUnits =
current
.getUnitCollection()
.getMatches(ownedUnitsMatch.and(Matches.unitCanAttack(player).negate()));
current.getMatches(ownedUnitsMatch.and(Matches.unitCanAttack(player).negate()));
if (nonCombatUnits.isEmpty() || nonCombatUnits.size() != countAllOwnedUnits) {
continue;
}
Expand Down
Expand Up @@ -85,14 +85,10 @@ public void updateDefendingUnits() {
// fill in defenders
if (isBombingRun) {
defendingUnits =
battleSite
.getUnitCollection()
.getMatches(defendingBombingRaidInterceptors(battleSite, attacker, gameData));
battleSite.getMatches(defendingBombingRaidInterceptors(battleSite, attacker, gameData));
} else {
defendingUnits =
battleSite
.getUnitCollection()
.getMatches(defendingGroundSeaBattleInterceptors(attacker, gameData));
battleSite.getMatches(defendingGroundSeaBattleInterceptors(attacker, gameData));
}
}

Expand Down Expand Up @@ -353,12 +349,10 @@ private void makeBattle(final IDelegateBridge bridge) {
if (!bombers.isEmpty()) {
Map<Unit, Set<Unit>> targets = null;
final Collection<Unit> enemyTargetsTotal =
battleSite
.getUnitCollection()
.getMatches(
Matches.enemyUnit(bridge.getGamePlayer())
.and(Matches.unitCanBeDamaged())
.and(Matches.unitIsBeingTransported().negate()));
battleSite.getMatches(
Matches.enemyUnit(bridge.getGamePlayer())
.and(Matches.unitCanBeDamaged())
.and(Matches.unitIsBeingTransported().negate()));
for (final Unit unit : bombers) {
final Collection<Unit> enemyTargets =
CollectionUtils.getMatches(
Expand Down Expand Up @@ -575,9 +569,7 @@ public static int getMaxInterceptionCount(final Territory t, final Collection<Un
return Integer.MAX_VALUE;
}
int result = 0;
for (final Unit base :
t.getUnitCollection()
.getMatches(Matches.unitIsAirBase().and(Matches.unitIsNotDisabled()))) {
for (final Unit base : t.getMatches(Matches.unitIsAirBase().and(Matches.unitIsNotDisabled()))) {
final int baseMax = base.getUnitAttachment().getMaxInterceptCount();
if (baseMax == -1) {
return Integer.MAX_VALUE;
Expand Down
Expand Up @@ -156,8 +156,7 @@ public MustFightBattle(
final GameData data,
final BattleTracker battleTracker) {
super(battleSite, attacker, battleTracker, data);
defendingUnits.addAll(
this.battleSite.getUnitCollection().getMatches(Matches.enemyUnit(attacker)));
defendingUnits.addAll(this.battleSite.getMatches(Matches.enemyUnit(attacker)));
maxRounds =
battleSite.isWater()
? Properties.getSeaBattleRounds(data.getProperties())
Expand All @@ -166,7 +165,7 @@ public MustFightBattle(

void resetDefendingUnits(final GamePlayer attacker) {
defendingUnits.clear();
defendingUnits.addAll(battleSite.getUnitCollection().getMatches(Matches.enemyUnit(attacker)));
defendingUnits.addAll(battleSite.getMatches(Matches.enemyUnit(attacker)));
}

/** Used for head-less battles. */
Expand Down
Expand Up @@ -233,8 +233,7 @@ private static IntegerMap<Territory> populateStaticAlliedAndBuildingCarrierCapac
startingSpace.add(t, producedCarrierCapacity);
carriersInProductionQueue.clear();
}
final Collection<Unit> alliedCarriers =
t.getUnitCollection().getMatches(carrierAlliedNotOwned);
final Collection<Unit> alliedCarriers = t.getMatches(carrierAlliedNotOwned);
alliedCarriers.removeAll(movedCarriersAndTheirFighters.keySet());
final int alliedCarrierCapacity = carrierCapacity(alliedCarriers, t);
startingSpace.add(t, alliedCarrierCapacity);
Expand Down Expand Up @@ -378,9 +377,7 @@ private static void validateAirCaughtByMovingCarriersAndOwnedAndAlliedAir(
// owned units, not to
// carry our selected units.
carrierSpotCapacity =
carrierCapacity(
carrierSpot.getUnitCollection().getMatches(alliedNotOwnedCarrierMatch),
carrierSpot);
carrierCapacity(carrierSpot.getMatches(alliedNotOwnedCarrierMatch), carrierSpot);
landingSpotsWithCarrierCapacity.put(carrierSpot, carrierSpotCapacity);
}
// we have allied air here, so we need to account for them before moving any carriers
Expand Down Expand Up @@ -551,7 +548,7 @@ private static BigDecimal maxMovementLeftForAllOwnedCarriers(
BigDecimal max = BigDecimal.ZERO;
final Predicate<Unit> ownedCarrier = Matches.unitIsCarrier().and(Matches.unitIsOwnedBy(player));
for (final Territory t : data.getMap().getTerritories()) {
for (final Unit carrier : t.getUnitCollection().getMatches(ownedCarrier)) {
for (final Unit carrier : t.getMatches(ownedCarrier)) {
max = max.max(carrier.getMovementLeft());
}
}
Expand Down Expand Up @@ -619,8 +616,7 @@ private static List<Unit> getAirUnitsToValidate(
ownedAir.addAll(CollectionUtils.getMatches(units, ownedAirMatch));

// Remove suicide units if combat move and any enemy units at destination
final Collection<Unit> enemyUnitsAtEnd =
route.getEnd().getUnitCollection().getMatches(Matches.enemyUnit(player));
final Collection<Unit> enemyUnitsAtEnd = route.getEnd().getMatches(Matches.enemyUnit(player));
if (!enemyUnitsAtEnd.isEmpty() && GameStepPropertiesHelper.isCombatMove(player.getData())) {
ownedAir.removeIf(Matches.unitIsSuicideOnAttack());
}
Expand Down Expand Up @@ -797,9 +793,8 @@ public static int carrierCapacity(final Unit unit, final Territory territoryUnit
}
int cargo = 0;
final Collection<Unit> airCargo =
territoryUnitsAreCurrentlyIn
.getUnitCollection()
.getMatches(Matches.unitIsAir().and(Matches.unitCanLandOnCarrier()));
territoryUnitsAreCurrentlyIn.getMatches(
Matches.unitIsAir().and(Matches.unitCanLandOnCarrier()));
for (final Unit airUnit : airCargo) {
if (airUnit.getTransportedBy() != null && airUnit.getTransportedBy().equals(unit)) {
// capacity = are cargo only
Expand Down Expand Up @@ -830,7 +825,7 @@ private static boolean getEditMode(final GameState data) {

public static Collection<Unit> getFriendly(
final Territory territory, final GamePlayer player, final GameState data) {
return territory.getUnitCollection().getMatches(Matches.alliedUnit(player));
return territory.getMatches(Matches.alliedUnit(player));
}

private static boolean areNeutralsPassableByAir(final GameState data) {
Expand Down
Expand Up @@ -595,10 +595,8 @@ && enemyDestroyerOnPath(route, player)) {
return result.setErrorReturnResult("Cannot move submarines under destroyers");
}
// Can't advance to battle unless only ignored units on route, only air units to sea, or only
// units that can enter
// territories with enemy units during NCM
if (end.getUnitCollection()
.anyMatch(Matches.enemyUnit(player).and(Matches.unitIsSubmerged().negate()))
// units that can enter territories with enemy units during NCM
if (end.anyUnitsMatch(Matches.enemyUnit(player).and(Matches.unitIsSubmerged().negate()))
&& !onlyIgnoredUnitsOnPath(route, player, false)
&& !(end.isWater() && units.stream().allMatch(Matches.unitIsAir()))
&& !(Properties.getSubsCanEndNonCombatMoveWithEnemies(data.getProperties())
Expand Down
Expand Up @@ -1072,7 +1072,6 @@ void testSbrRolls() {
gameData
.getMap()
.getTerritory("United Kingdom")
.getUnitCollection()
.getMatches(Matches.unitIsStrategicBomber())
.get(0);
// default 1 roll
Expand Down
Expand Up @@ -123,16 +123,14 @@ public static void addAttackers(final Territory t) {
.noneMatch(Matches.unitIsOwnedBy(currentPanel.getAttacker()))) {
// Find possible attackers (enemies) of the current defender.
// Count how many units each one has and find the max.
final List<Unit> units =
t.getUnitCollection().getMatches(Matches.enemyUnit(currentPanel.getDefender()));
final List<Unit> units = t.getMatches(Matches.enemyUnit(currentPanel.getDefender()));

final GamePlayer gamePlayer = new IntegerMap<>(units, Unit::getOwner).maxKey();
if (gamePlayer != null) {
currentPanel.setAttacker(gamePlayer);
}
}
currentPanel.addAttackingUnits(
t.getUnitCollection().getMatches(Matches.unitIsOwnedBy(currentPanel.getAttacker())));
currentPanel.addAttackingUnits(t.getMatches(Matches.unitIsOwnedBy(currentPanel.getAttacker())));
}

public static void addDefenders(final Territory t) {
Expand All @@ -150,7 +148,7 @@ public static void addDefenders(final Territory t) {
.ifPresent(currentDialog.panel::setDefender);
}
currentDialog.panel.addDefendingUnits(
t.getUnitCollection().getMatches(Matches.alliedUnit(currentDialog.panel.getDefender())));
t.getMatches(Matches.alliedUnit(currentDialog.panel.getDefender())));
currentDialog.pack();
}

Expand Down

0 comments on commit 4e4642d

Please sign in to comment.