Skip to content

Commit

Permalink
Fix validation of move/attack stacking limits from player attachment. (
Browse files Browse the repository at this point in the history
…#11831)

* Fix validation of move/attack stacking limits from player attachment.

Fixes issue in my last PR where these were not being applied due to incorrect filtering of the unit list.
  • Loading branch information
asvitkine authored Jul 29, 2023
1 parent 00d298a commit 9d0f075
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,6 @@ static Predicate<Unit> unitIsRocket() {
return obj -> unitTypeIsRocket().test(obj.getType());
}

public static Predicate<Unit> unitHasMovementLimit() {
return u -> u.getUnitAttachment().getMovementLimit() != null;
}

public static Predicate<Unit> unitHasAttackingLimit() {
return u -> u.getUnitAttachment().getAttackingLimit() != null;
}

public static Predicate<UnitType> unitTypeCanNotMoveDuringCombatMove() {
return u -> u.getUnitAttachment().getCanNotMoveDuringCombatMove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import games.strategy.triplea.Constants;
import games.strategy.triplea.Properties;
import games.strategy.triplea.attachments.CanalAttachment;
import games.strategy.triplea.attachments.PlayerAttachment;
import games.strategy.triplea.attachments.RulesAttachment;
import games.strategy.triplea.attachments.TechAbilityAttachment;
import games.strategy.triplea.attachments.UnitAttachment;
Expand Down Expand Up @@ -53,6 +54,7 @@
import org.triplea.java.PredicateBuilder;
import org.triplea.java.collections.CollectionUtils;
import org.triplea.java.collections.IntegerMap;
import org.triplea.util.Triple;

/** Responsible for validating unit movement. */
@AllArgsConstructor
Expand Down Expand Up @@ -803,9 +805,31 @@ private MoveValidationResult validateBasic(
}
}
// test for stack limits per unit
final PlayerAttachment pa = PlayerAttachment.get(player);
final Set<Triple<Integer, String, Set<UnitType>>> playerMovementLimit =
(pa != null ? pa.getMovementLimit() : Set.of());
final Set<Triple<Integer, String, Set<UnitType>>> playerAttackingLimit =
(pa != null ? pa.getAttackingLimit() : Set.of());
final Predicate<Unit> hasMovementOrAttackingLimit =
unit -> {
final var ua = unit.getUnitAttachment();
if (ua.getMovementLimit() != null || ua.getAttackingLimit() != null) {
return true;
}
for (final var limit : playerMovementLimit) {
if (limit.getThird().contains(unit.getType())) {
return true;
}
}
for (final var limit : playerAttackingLimit) {
if (limit.getThird().contains(unit.getType())) {
return true;
}
}
return false;
};
final Collection<Unit> unitsWithStackingLimits =
CollectionUtils.getMatches(
units, Matches.unitHasMovementLimit().or(Matches.unitHasAttackingLimit()));
CollectionUtils.getMatches(units, hasMovementOrAttackingLimit);
for (final Territory t : route.getSteps()) {
final String limitType;
if (Matches.isTerritoryEnemyAndNotUnownedWater(player).test(t)
Expand Down

0 comments on commit 9d0f075

Please sign in to comment.