Skip to content

Commit

Permalink
Speed up canal validation code.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvitkine committed Aug 27, 2023
1 parent 2282e30 commit b0c1c0c
Showing 1 changed file with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private MoveValidationResult validateCanal(
*/
public @Nullable String validateCanal(
final Route route, @Nullable final Collection<Unit> units, final GamePlayer player) {
return validateCanal(route, units, new HashMap<>(), player);
return validateCanal(route, units, Map.of(), player);
}

@Nullable
Expand All @@ -300,32 +300,36 @@ String validateCanal(
@Nullable final Collection<Unit> units,
final Map<Unit, Collection<Unit>> airTransportDependents,
final GamePlayer player) {
List<CanalAttachment> canals =
route.getAllTerritories().stream()
.map(t -> CanalAttachment.get(t))
.flatMap(Collection::stream)
// Only check canals that are on the route
.filter(c -> CanalAttachment.isCanalOnRoute(c.getCanalName(), route))
.collect(Collectors.toList());
if (canals.isEmpty()) {
return null;
}
final boolean mustControlAllCanals =
Properties.getControlAllCanalsBetweenTerritoriesToPass(data.getProperties());
// Check each unit 1 by 1 to see if they can move through necessary canals on route
String result = null;
final Set<Unit> unitsThatFailCanal = new HashSet<>();
final Set<Unit> setWithNull = new HashSet<>();
setWithNull.add(null);
final Collection<Unit> unitsWithoutDependents =
(units == null) ? setWithNull : findNonDependentUnits(units, route, airTransportDependents);
(units == null)
? Set.of((Unit) null)
: findNonDependentUnits(units, route, airTransportDependents);
for (final Unit unit : unitsWithoutDependents) {
for (final Territory t : route.getAllTerritories()) {
Optional<String> failureMessage = Optional.empty();
for (final CanalAttachment canalAttachment : CanalAttachment.get(t)) {
if (!CanalAttachment.isCanalOnRoute(canalAttachment.getCanalName(), route)) {
continue; // Only check canals that are on the route
}
failureMessage = canPassThroughCanal(canalAttachment, unit, player);
final boolean canPass = failureMessage.isEmpty();
final boolean mustControlAllCanals =
Properties.getControlAllCanalsBetweenTerritoriesToPass(data.getProperties());
if (mustControlAllCanals != canPass) {
// If need to control any canal and can pass OR need to control all and can't pass.
break;
for (CanalAttachment canalAttachment : canals) {
Optional<String> failureMessage = canPassThroughCanal(canalAttachment, unit, player);
final boolean canPass = failureMessage.isEmpty();
if (mustControlAllCanals != canPass) {
// If need to control any canal and can pass OR need to control all and can't pass.
if (!canPass) {
result = failureMessage.get();
unitsThatFailCanal.add(unit);
}
}
if (failureMessage.isPresent()) {
result = failureMessage.get();
unitsThatFailCanal.add(unit);
break;
}
}
}
Expand Down

0 comments on commit b0c1c0c

Please sign in to comment.