Skip to content

Commit

Permalink
Remove RelationshipTracker param from many matchers. (#10473)
Browse files Browse the repository at this point in the history
* Remove RelationshipTracker param from many matchers.

Now that all players have a GameData set, we can always get the relationship tracker from GamePlayer, thus allowing us to eliminate lots of params.

Updates test code to properly mock this to account for this.
  • Loading branch information
asvitkine authored May 27, 2022
1 parent 13308d3 commit b55dce2
Show file tree
Hide file tree
Showing 64 changed files with 429 additions and 760 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import games.strategy.triplea.attachments.RulesAttachment;
import games.strategy.triplea.attachments.TechAttachment;
import games.strategy.triplea.delegate.Matches;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -234,6 +235,22 @@ public TechAttachment getTechAttachment() {
return (TechAttachment) getAttachment(Constants.TECH_ATTACHMENT_NAME);
}

public final boolean isAllied(GamePlayer other) {
return getData().getRelationshipTracker().isAllied(this, other);
}

public final boolean isAtWar(GamePlayer other) {
return getData().getRelationshipTracker().isAtWar(this, other);
}

public final boolean isAtWarWithAnyOfThesePlayers(Collection<GamePlayer> others) {
return getData().getRelationshipTracker().isAtWarWithAnyOfThesePlayers(this, others);
}

public final boolean isAlliedWithAnyOfThesePlayers(Collection<GamePlayer> others) {
return getData().getRelationshipTracker().isAlliedWithAnyOfThesePlayers(this, others);
}

/** A player type (e.g. human, AI). */
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@EqualsAndHashCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private static Tuple<ResourceCollection, Set<Unit>> getFuelCostsAndUnitsChargedF
units,
Matches.unitIsOwnedBy(player)
.negate()
.and(Matches.isUnitAllied(player, data.getRelationshipTracker()))
.and(Matches.isUnitAllied(player))
.and(Matches.unitCanLandOnCarrier()));
canLandOnCarrierUnits.addAll(
CollectionUtils.getMatches(
Expand All @@ -432,7 +432,7 @@ private static Tuple<ResourceCollection, Set<Unit>> getFuelCostsAndUnitsChargedF
CollectionUtils.getMatches(
units,
Matches.unitIsBeingTransportedByOrIsDependentOfSomeUnitInThisList(
units, player, data.getRelationshipTracker(), true)));
units, player, true)));

// Find fuel cost and whether to charge flat fuel cost
final ResourceCollection movementCharge = new ResourceCollection(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public double getValue(final GamePlayer player, final GameData data, final MapDa
final int production =
data.getMap().getTerritories().stream()
.filter(Matches.isTerritoryOwnedBy(player))
.filter(
Matches.territoryCanCollectIncomeFrom(
player, data.getProperties(), data.getRelationshipTracker()))
.filter(Matches.territoryCanCollectIncomeFrom(player, data.getProperties()))
.mapToInt(TerritoryAttachment::getProduction)
.sum();
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ public boolean acceptAction(
return true;
}
// politics from ally? accept
if (Matches.isAllied(this.getGamePlayer(), getGameData().getRelationshipTracker())
.test(playerSendingProposal)) {
if (Matches.isAllied(this.getGamePlayer()).test(playerSendingProposal)) {
return true;
}
// would we normally be allies?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ private void prioritizeAttackOptions(
final Set<Territory> nearbyTerritories =
data.getMap().getNeighbors(t, ProMatches.territoryCanMoveLandUnits(data, player, true));
final List<Territory> nearbyEnemyTerritories =
CollectionUtils.getMatches(
nearbyTerritories, Matches.isTerritoryEnemy(player, data.getRelationshipTracker()));
CollectionUtils.getMatches(nearbyTerritories, Matches.isTerritoryEnemy(player));
final List<Territory> nearbyTerritoriesWithOwnedUnits =
CollectionUtils.getMatches(nearbyTerritories, Matches.territoryHasUnitsOwnedBy(player));
for (final Territory nearbyEnemyTerritory : nearbyEnemyTerritories) {
Expand Down Expand Up @@ -554,7 +553,7 @@ private void removeTerritoriesThatArentWorthAttacking(
if (!patd.isCanHold()
&& enemyAttackOptions.getMax(t) != null
&& t.isWater()
&& !t.anyUnitsMatch(Matches.enemyUnit(player, data.getRelationshipTracker()))) {
&& !t.anyUnitsMatch(Matches.enemyUnit(player))) {
ProLogger.debug(
"Removing convoy zone that can't be held: "
+ t.getName()
Expand Down Expand Up @@ -669,7 +668,6 @@ private List<Unit> moveOneDefenderToLandTerritoriesBorderingEnemy(
.getNeighbors(
t,
Matches.territoryIsEnemyNonNeutralAndHasEnemyUnitMatching(
data.getRelationshipTracker(),
player,
Matches.unitIsLand()
.and(Matches.unitIsNotInfrastructure())
Expand Down Expand Up @@ -1617,8 +1615,7 @@ private Map<Unit, Set<Territory>> tryToAttackTerritories(
final List<Unit> defenders =
territoryToMoveTransport
.getUnitCollection()
.getMatches(
Matches.isUnitAllied(player, data.getRelationshipTracker()));
.getMatches(Matches.isUnitAllied(player));
defenders.add(transport);
final double strengthDifference =
ProBattleUtils.estimateStrengthDifference(
Expand Down Expand Up @@ -1787,9 +1784,7 @@ private void removeAttacksUntilCapitalCanBeHeld(
final Set<Territory> territoriesAdjacentToCapital =
data.getMap().getNeighbors(myCapital, Matches.territoryIsLand());
final List<Unit> defenders =
myCapital
.getUnitCollection()
.getMatches(Matches.isUnitAllied(player, data.getRelationshipTracker()));
myCapital.getUnitCollection().getMatches(Matches.isUnitAllied(player));
defenders.addAll(placeUnits);
for (final Territory t : territoriesAdjacentToCapital) {
defenders.addAll(
Expand Down Expand Up @@ -1866,7 +1861,7 @@ private void checkContestedSeaTerritories() {

for (final Territory t : proData.getMyUnitTerritories()) {
if (t.isWater()
&& Matches.territoryHasEnemyUnits(player, data.getRelationshipTracker()).test(t)
&& Matches.territoryHasEnemyUnits(player).test(t)
&& (attackMap.get(t) == null || attackMap.get(t).getUnits().isEmpty())) {

// Move into random adjacent safe sea territory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ private List<ProTerritory> prioritizeDefendOptions(
data.getMap().getNeighbors(t, Matches.territoryIsLand());
for (final Territory neighbor : landNeighbors) {
double neighborProduction = TerritoryAttachment.getProduction(neighbor);
if (Matches.isTerritoryAllied(player, data.getRelationshipTracker()).test(neighbor)) {
if (Matches.isTerritoryAllied(player).test(neighbor)) {
neighborProduction = 0.1 * neighborProduction;
}
neighborValue += neighborProduction;
Expand Down Expand Up @@ -847,8 +847,7 @@ private void moveUnitsToDefendTerritories(
if (Matches.unitIsCarrier().test(unit)) {
final Territory unitTerritory = unitTerritoryMap.get(unit);
final Map<Unit, Collection<Unit>> carrierMustMoveWith =
MoveValidator.carrierMustMoveWith(
unitTerritory.getUnits(), unitTerritory, data.getRelationshipTracker(), player);
MoveValidator.carrierMustMoveWith(unitTerritory.getUnits(), unitTerritory, player);
if (carrierMustMoveWith.containsKey(unit)) {
moveMap.get(maxWinTerritory).getTempUnits().addAll(carrierMustMoveWith.get(unit));
}
Expand Down Expand Up @@ -1746,10 +1745,7 @@ private void moveUnitsToBestTerritories() {
final Territory unitTerritory = unitTerritoryMap.get(u);
final Map<Unit, Collection<Unit>> carrierMustMoveWith =
MoveValidator.carrierMustMoveWith(
unitTerritory.getUnits(),
unitTerritory,
data.getRelationshipTracker(),
player);
unitTerritory.getUnits(), unitTerritory, player);
if (carrierMustMoveWith.containsKey(u)) {
proTerritory.getTempUnits().addAll(carrierMustMoveWith.get(u));
}
Expand Down Expand Up @@ -1852,10 +1848,7 @@ private void moveUnitsToBestTerritories() {
final Territory unitTerritory = unitTerritoryMap.get(u);
final Map<Unit, Collection<Unit>> carrierMustMoveWith =
MoveValidator.carrierMustMoveWith(
unitTerritory.getUnits(),
unitTerritory,
data.getRelationshipTracker(),
player);
unitTerritory.getUnits(), unitTerritory, player);
if (carrierMustMoveWith.containsKey(u)) {
moveMap.get(maxValueTerritory).getTempUnits().addAll(carrierMustMoveWith.get(u));
}
Expand Down Expand Up @@ -1899,10 +1892,7 @@ private void moveUnitsToBestTerritories() {
final Territory unitTerritory = unitTerritoryMap.get(u);
final Map<Unit, Collection<Unit>> carrierMustMoveWith =
MoveValidator.carrierMustMoveWith(
unitTerritory.getUnits(),
unitTerritory,
data.getRelationshipTracker(),
player);
unitTerritory.getUnits(), unitTerritory, player);
if (carrierMustMoveWith.containsKey(u)) {
moveMap.get(minTerritory).getTempUnits().addAll(carrierMustMoveWith.get(u));
}
Expand Down Expand Up @@ -2274,7 +2264,7 @@ private void moveUnitsToBestTerritories() {
final int numSeaAttackTerritories =
CollectionUtils.countMatches(
possibleAttackTerritories,
Matches.territoryHasEnemySeaUnits(player, data.getRelationshipTracker())
Matches.territoryHasEnemySeaUnits(player)
.and(
Matches.territoryHasUnitsThatMatch(
Matches.unitHasSubBattleAbilities().negate())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,7 @@ private void findDefendersInPlaceTerritories(
for (final ProPurchaseTerritory ppt : purchaseTerritories.values()) {
for (final ProPlaceTerritory placeTerritory : ppt.getCanPlaceTerritories()) {
final Territory t = placeTerritory.getTerritory();
final List<Unit> units =
t.getUnitCollection()
.getMatches(Matches.isUnitAllied(player, data.getRelationshipTracker()));
final List<Unit> units = t.getUnitCollection().getMatches(Matches.isUnitAllied(player));
placeTerritory.setDefendingUnits(units);
ProLogger.debug(t + " has numDefenders=" + units.size());
}
Expand Down Expand Up @@ -1223,9 +1221,7 @@ private void purchaseFactory(
} else {

// Find current battle result
final List<Unit> defenders =
t.getUnitCollection()
.getMatches(Matches.isUnitAllied(player, data.getRelationshipTracker()));
final List<Unit> defenders = t.getUnitCollection().getMatches(Matches.isUnitAllied(player));
final Set<Unit> enemyAttackingUnits =
new HashSet<>(enemyAttackOptions.getMax(t).getMaxUnits());
enemyAttackingUnits.addAll(enemyAttackOptions.getMax(t).getMaxAmphibUnits());
Expand Down Expand Up @@ -1298,9 +1294,7 @@ private void purchaseFactory(
data.getMap()
.getNeighbors(t, 9, ProMatches.territoryCanMoveLandUnits(data, player, false));
final int numNearbyEnemyTerritories =
CollectionUtils.countMatches(
nearbyLandTerritories,
Matches.isTerritoryEnemy(player, data.getRelationshipTracker()));
CollectionUtils.countMatches(nearbyLandTerritories, Matches.isTerritoryEnemy(player));
ProLogger.trace(
t
+ ", strategic value="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ Territory retreatQuery(
final double strength =
ProBattleUtils.estimateStrength(
t,
t.getUnitCollection()
.getMatches(Matches.isUnitAllied(player, data.getRelationshipTracker())),
t.getUnitCollection().getMatches(Matches.isUnitAllied(player)),
new ArrayList<>(),
false);
if (strength > maxStrength) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ private static float getStrengthOfPotentialAttackers(
enemyPlayer,
data,
enemyShip,
Matches.territoryIsBlockedSea(
enemyPlayer, data.getProperties(), data.getRelationshipTracker()),
Matches.territoryIsBlockedSea(enemyPlayer, data.getProperties()),
r,
true);
secondStrength = strength(ships, true, true, transportsFirst);
Expand Down Expand Up @@ -237,9 +236,7 @@ private static float getStrengthOfPotentialAttackers(
availOther += other;
}
final Set<Territory> transNeighbors =
data.getMap()
.getNeighbors(
t4, Matches.isTerritoryAllied(enemyPlayer, data.getRelationshipTracker()));
data.getMap().getNeighbors(t4, Matches.isTerritoryAllied(enemyPlayer));
for (final Territory transNeighbor : transNeighbors) {
final List<Unit> transUnits =
transNeighbor.getUnitCollection().getMatches(enemyTransportable);
Expand Down Expand Up @@ -388,7 +385,7 @@ private static float determineEnemyBlitzStrength(
final Predicate<Unit> blitzUnit =
Matches.unitIsOwnedBy(enemyPlayer).and(Matches.unitCanBlitz()).and(Matches.unitCanMove());
final Predicate<Territory> validBlitzRoute =
Matches.territoryHasNoEnemyUnits(enemyPlayer, data.getRelationshipTracker())
Matches.territoryHasNoEnemyUnits(enemyPlayer)
.and(Matches.territoryIsNotImpassableToLandUnits(enemyPlayer, data.getProperties()));
final List<Route> routes = new ArrayList<>();
final List<Unit> blitzUnits =
Expand Down Expand Up @@ -499,7 +496,7 @@ private static List<Unit> findPlaneAttackersThatCanLand(
q.add(neighbor);
distance.put(neighbor, distance.getInt(current) + 1);
if (lz == null
&& Matches.isTerritoryAllied(player, data.getRelationshipTracker()).test(neighbor)
&& Matches.isTerritoryAllied(player).test(neighbor)
&& !neighbor.isWater()) {
lz = neighbor;
}
Expand Down Expand Up @@ -559,7 +556,7 @@ private static Route getMaxSeaRoute(
Matches.unitIsTransport().negate().and(Matches.unitIsLand().negate());
final Predicate<Unit> unitCond =
PredicateBuilder.of(Matches.unitIsInfrastructure().negate())
.and(Matches.alliedUnit(player, data.getRelationshipTracker()).negate())
.and(Matches.alliedUnit(player).negate())
.and(Matches.unitCanBeMovedThroughByEnemies().negate())
.andIf(Properties.getIgnoreTransportInMovement(data.getProperties()), transport)
.build();
Expand Down Expand Up @@ -588,7 +585,7 @@ private static List<Territory> getNeighboringLandTerritories(
final List<Territory> territories = new ArrayList<>();
final List<Territory> checkList = getExactNeighbors(check, data);
for (final Territory t : checkList) {
if (Matches.isTerritoryAllied(player, data.getRelationshipTracker()).test(t)
if (Matches.isTerritoryAllied(player).test(t)
&& Matches.territoryIsNotImpassableToLandUnits(player, data.getProperties()).test(t)) {
territories.add(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ProPurchaseTerritory(
for (final Territory t : data.getMap().getNeighbors(territory, Matches.territoryIsWater())) {
if (Properties.getWW2V2(data.getProperties())
|| Properties.getUnitPlacementInEnemySeas(data.getProperties())
|| !t.anyUnitsMatch(Matches.enemyUnit(player, data.getRelationshipTracker()))) {
|| !t.anyUnitsMatch(Matches.enemyUnit(player))) {
canPlaceTerritories.add(new ProPlaceTerritory(t));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ public List<Unit> getMaxDefenders() {

public List<Unit> getMaxEnemyDefenders(final GamePlayer player, final GameState data) {
final List<Unit> defenders =
territory
.getUnitCollection()
.getMatches(Matches.enemyUnit(player, data.getRelationshipTracker()));
territory.getUnitCollection().getMatches(Matches.enemyUnit(player));
defenders.addAll(maxScrambleUnits);
return defenders;
}
Expand Down
Loading

0 comments on commit b55dce2

Please sign in to comment.