Skip to content

Commit

Permalink
Merge pull request #6606 from psikomonkie/improve-towing-logic
Browse files Browse the repository at this point in the history
Enhancement - Improve Deployment-Phase Towing Logic
  • Loading branch information
HammerGS authored Feb 25, 2025
2 parents 3730d1e + af1ab22 commit 6706585
Show file tree
Hide file tree
Showing 3 changed files with 538 additions and 162 deletions.
61 changes: 7 additions & 54 deletions megamek/src/megamek/client/bot/princess/Princess.java
Original file line number Diff line number Diff line change
Expand Up @@ -731,60 +731,13 @@ protected Coords rankDeploymentCoords(Entity deployedUnit, List<Coords> possible
sb.append("Ranking deployment hexes...");
}

// If we're a trailer, get out of here and let the tractor find the better position.
if (deployedUnit.getTowedBy() != Entity.NONE) {
Entity tractor = game.getEntity(deployedUnit.getTowedBy());
if (tractor != null) {
if (tractor.isDeployed()) {
List<Coords> trailerDeployCoords = TowLinkWarning.getCoordsForTrailerGivenTractor(game, deployedUnit, tractor);
// Would any of our possible deploy coords let us be towed by our tractor? Filter to that.
// Else use the default list, guess we can't tow right now.
if (possibleDeployCoords.stream().anyMatch(trailerDeployCoords::contains)) {
possibleDeployCoords = possibleDeployCoords.stream().filter(trailerDeployCoords::contains).toList();
}
} else {
// If the tractor isn't deployed let's find the best place to deploy it instead and base our location on that
final List<Coords> startingCoords = getStartingCoordsArray(tractor);
if (startingCoords.isEmpty()) {
logger.error("No valid locations to deploy " + tractor.getDisplayName());
}

Coords bestTractorCoords = getFirstValidCoords(tractor, startingCoords);
List<Coords> filteredDeployCoords = new ArrayList<>();
if (deployedUnit instanceof LargeSupportTank) {
filteredDeployCoords = possibleDeployCoords.stream().filter(c -> bestTractorCoords.allAdjacent().contains(c)).toList();
if (!filteredDeployCoords.isEmpty()) {
possibleDeployCoords = filteredDeployCoords;
}
} else if (possibleDeployCoords.contains(bestTractorCoords)){
filteredDeployCoords.add(bestTractorCoords);
possibleDeployCoords = filteredDeployCoords;
}
}
}
}

// If we're a tractor, let's make sure we can deploy with our trailer
if (deployedUnit.getTowing() != Entity.NONE) {
Entity trailer = game.getEntity(deployedUnit.getTowing());
if (trailer != null) {
if (trailer.isDeployed()) {
List<Coords> tractorDeployCoords = TowLinkWarning.getCoordsForTractorGivenTrailer(game, deployedUnit, trailer);
// Would any of our possible deploy coords let us be tow our trailer? Filter to that.
// Else use the default list, guess we can't tow right now.
if (possibleDeployCoords.stream().anyMatch(tractorDeployCoords::contains)) {
possibleDeployCoords = possibleDeployCoords.stream().filter(tractorDeployCoords::contains).toList();
}
} else {
List<Coords> towLinkIssues = TowLinkWarning.findTowLinkIssues(game, deployedUnit, game.getBoard());
// If there are any coords we can deploy to without tow link issues, filter our list to just that
if (possibleDeployCoords.stream().anyMatch(c -> !towLinkIssues.contains(c))) {
List<Coords> filteredDeployCoords = possibleDeployCoords.stream().filter(c -> !towLinkIssues.contains(c)).toList();
if (!filteredDeployCoords.isEmpty()) {
possibleDeployCoords = filteredDeployCoords;
}
}
}
if ((deployedUnit.getTowing() != Entity.NONE && game.getEntity(deployedUnit.getTowing()) != null)
|| (deployedUnit.getTowedBy() != Entity.NONE) && game.getEntity(deployedUnit.getTowedBy()) != null) {
List<Coords> filteredCoords = TowLinkWarning.findValidDeployCoordsForTractorTrailer(game, deployedUnit, getBoard());
if (filteredCoords != null && !filteredCoords.isEmpty()) {
possibleDeployCoords = possibleDeployCoords.stream().filter(filteredCoords::contains).toList();
} else {
logger.error("No valid locations to tractor/trailer deploy " + deployedUnit.getDisplayName());
}
}

Expand Down
Loading

0 comments on commit 6706585

Please sign in to comment.