Skip to content

Commit

Permalink
Remove getCanTheseUnitsMoveWithoutViolatingStackingLimit().
Browse files Browse the repository at this point in the history
  • Loading branch information
asvitkine committed Jul 27, 2023
1 parent de54f63 commit 05e8434
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@
import games.strategy.engine.data.GameState;
import games.strategy.engine.data.MutableProperty;
import games.strategy.engine.data.Resource;
import games.strategy.engine.data.Territory;
import games.strategy.engine.data.Unit;
import games.strategy.engine.data.UnitType;
import games.strategy.engine.data.gameparser.GameParseException;
import games.strategy.triplea.delegate.Matches;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import org.triplea.java.collections.CollectionUtils;
import org.triplea.java.collections.IntegerMap;
import org.triplea.util.Triple;

Expand Down Expand Up @@ -165,64 +158,6 @@ private Triple<Integer, String, Set<UnitType>> parseUnitLimit(
return Triple.of(max, s[1].intern(), types);
}

/**
* Returns {@code true} if the specified units can move into the specified territory without
* violating the specified stacking limit (movement, attack, or placement).
*/
public static boolean getCanTheseUnitsMoveWithoutViolatingStackingLimit(
final String limitType,
final Collection<Unit> unitsMoving,
final Territory toMoveInto,
final GamePlayer owner) {
final PlayerAttachment pa = PlayerAttachment.get(owner);
if (pa == null) {
return true;
}
final Set<Triple<Integer, String, Set<UnitType>>> stackingLimits;
switch (limitType) {
case "movementLimit":
stackingLimits = pa.getMovementLimit();
break;
case "attackingLimit":
stackingLimits = pa.getAttackingLimit();
break;
case "placementLimit":
stackingLimits = pa.getPlacementLimit();
break;
default:
throw new IllegalStateException("Invalid limitType: " + limitType);
}
if (stackingLimits.isEmpty()) {
return true;
}
final Predicate<Unit> notOwned = Matches.unitIsOwnedBy(owner).negate();
final Predicate<Unit> notAllied = Matches.alliedUnit(owner).negate();
for (final Triple<Integer, String, Set<UnitType>> currentLimit : stackingLimits) {
// first make a copy of unitsMoving
final Collection<Unit> copyUnitsMoving = new ArrayList<>(unitsMoving);
final Collection<Unit> currentInTerritory = new ArrayList<>(toMoveInto.getUnits());
final String type = currentLimit.getSecond();
// first remove units that do not apply to our current type
if (type.equals("owned")) {
currentInTerritory.removeAll(CollectionUtils.getMatches(currentInTerritory, notOwned));
copyUnitsMoving.removeAll(CollectionUtils.getMatches(copyUnitsMoving, notOwned));
} else if (type.equals("allied")) {
currentInTerritory.removeAll(CollectionUtils.getMatches(currentInTerritory, notAllied));
copyUnitsMoving.removeAll(CollectionUtils.getMatches(copyUnitsMoving, notAllied));
}
// now remove units that are not part of our list
final Predicate<Unit> matchesUnits = Matches.unitIsOfTypes(currentLimit.getThird());
currentInTerritory.retainAll(CollectionUtils.getMatches(currentInTerritory, matchesUnits));
copyUnitsMoving.retainAll(CollectionUtils.getMatches(copyUnitsMoving, matchesUnits));
// now test
final Integer max = currentLimit.getFirst();
if (max < (currentInTerritory.size() + copyUnitsMoving.size())) {
return false;
}
}
return true;
}

private void setSuicideAttackTargets(final String value) throws GameParseException {
suicideAttackTargets = parseUnitTypes("suicideAttackTargets", value, suicideAttackTargets);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2597,7 +2597,7 @@ public static int getMaximumNumberOfThisUnitTypeToReachStackingLimit(
if (type.equals("owned")) {
stackingMatch = stackingMatch.and(Matches.unitIsOwnedBy(owner));
} else if (type.equals("allied")) {
stackingMatch = stackingMatch.and(Matches.isUnitAllied(owner));
stackingMatch = stackingMatch.and(Matches.alliedUnit(owner));
}
final int totalInTerritory = CollectionUtils.countMatches(t.getUnits(), stackingMatch);
final Integer limitMax = currentLimit.getFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,11 +860,6 @@ public String canUnitsBePlaced(
return "UnitType " + ut.getName() + " is over stacking limit of " + maxForThisType;
}
}
if (!PlayerAttachment.getCanTheseUnitsMoveWithoutViolatingStackingLimit(
"placementLimit", units, to, player)) {
System.err.println("Err1");
return "Units Cannot Go Over Stacking Limit";
}
// now return null (valid placement) if we have placement restrictions disabled in game options
if (!Properties.getUnitPlacementRestrictions(getData().getProperties())) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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 @@ -824,10 +823,6 @@ private MoveValidationResult validateBasic(
"UnitType " + ut.getName() + " has reached stacking limit", unit);
}
}
if (!PlayerAttachment.getCanTheseUnitsMoveWithoutViolatingStackingLimit(
"attackingLimit", units, t, player)) {
return result.setErrorReturnResult("Units Cannot Go Over Stacking Limit");
}
} else {
for (final Unit unit : unitsWithStackingLimits) {
final UnitType ut = unit.getType();
Expand All @@ -842,10 +837,6 @@ private MoveValidationResult validateBasic(
"UnitType " + ut.getName() + " has reached stacking limit", unit);
}
}
if (!PlayerAttachment.getCanTheseUnitsMoveWithoutViolatingStackingLimit(
"movementLimit", units, t, player)) {
return result.setErrorReturnResult("Units Cannot Go Over Stacking Limit");
}
}
}

Expand Down

0 comments on commit 05e8434

Please sign in to comment.