Skip to content

Commit

Permalink
restore altitude behavior for Princess deploying Aero
Browse files Browse the repository at this point in the history
  • Loading branch information
SJuliez committed Oct 6, 2024
1 parent e1d2728 commit 8fdd24a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
31 changes: 25 additions & 6 deletions megamek/src/megamek/client/bot/princess/Princess.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,32 @@ protected void calculateDeployment() {
decentFacing = deployCoords.direction(center);
}

final Entity deployEntity = game.getEntity(entityNum);
final Entity deployEntity = getEntity(entityNum);
final Hex deployHex = game.getBoard().getHex(deployCoords);

int deployElevation = getDeployElevation(deployEntity, deployHex);

// Compensate for hex elevation where != 0...
deployElevation -= deployHex.getLevel();
int deployElevation = deployEntity.getElevation();

if (deployEntity.isAero()) {
if (game.getBoard().onGround()) {
// keep the altitude set in the lobby, possibly starting grounded
deployElevation = deployEntity.getAltitude();
} else if (game.getBoard().inAtmosphere()) {
// try to keep the altitude set in the lobby, but stay above the terrain
var deploymentHelper = new AllowedDeploymentHelper(deployEntity, deployCoords, game.getBoard(), deployHex, game);
List<ElevationOption> allowedDeployment = deploymentHelper.findAllowedElevations(DeploymentElevationType.ALTITUDE);
if (allowedDeployment.isEmpty()) {
// that's bad, cannot deploy at all
logger.error("Cannot find viable altitude to deploy to");
sendDeleteEntity(entityNum);
return;
} else {
deployElevation = Math.max(deployEntity.getAltitude(), Collections.min(allowedDeployment).elevation());
}
}
} else {
deployElevation = getDeployElevation(deployEntity, deployHex);
// Compensate for hex elevation where != 0...
deployElevation -= deployHex.getLevel();
}
deploy(entityNum, deployCoords, decentFacing, deployElevation);
}

Expand Down
30 changes: 24 additions & 6 deletions megamek/src/megamek/common/AllowedDeploymentHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package megamek.common;

import megamek.common.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -27,16 +29,28 @@
public record AllowedDeploymentHelper(Entity entity, Coords coords, Board board, Hex hex, Game game) {

/**
* Returns a list of elevations/altitudes that the given entity can deploy to at the given coords. This
* can be anything from the seafloor, swimming, ice, water surface, ground, up to elevations and
* altitudes. For VTOLs, elevations up to 10 are always included individually if available. Above that
* and above all terrain features of the hex, only a single elevation is reported using the
* ELEVATIONS_ABOVE marker, meaning that any elevation above the reported value is also available.
* Altitudes are always reported individually (0 to 10).
* Returns a list of elevations/altitudes that the given entity can deploy to at the given coords. This can be anything from the
* seafloor, swimming, ice, water surface, ground, up to elevations and altitudes. For VTOLs, elevations up to 10 are always included
* individually if available. Above that and above all terrain features of the hex, only a single elevation is reported using the
* ELEVATIONS_ABOVE marker, meaning that any elevation above the reported value is also available. Altitudes are always reported
* individually (0 to 10).
*
* @return All legal deployment elevations/altitudes
*/
public List<ElevationOption> findAllowedElevations() {
return findAllowedElevations(null);
}

/**
* Returns a list of elevations/altitudes that the given entity can deploy to at the given coords that are of the given type. This can
* be anything from the seafloor, swimming, ice, water surface, ground, up to elevations and altitudes. For VTOLs, elevations up to 10
* are always included individually if available. Above that and above all terrain features of the hex, only a single elevation is
* reported using the ELEVATIONS_ABOVE marker, meaning that any elevation above the reported value is also available. Altitudes are
* always reported individually (0 to 10).
*
* @return All legal deployment elevations/altitudes of the given type
*/
public List<ElevationOption> findAllowedElevations(@Nullable DeploymentElevationType limitToType) {
if (board.inSpace()) {
throw new IllegalStateException("Cannot find allowed deployment elevations in space!");
}
Expand All @@ -54,6 +68,10 @@ public List<ElevationOption> findAllowedElevations() {
addAirborneWigeOptions(result);
}

if (limitToType != null) {
result.removeIf(o -> o.type() != limitToType);
}

Collections.sort(result);
return result;
}
Expand Down
8 changes: 4 additions & 4 deletions megamek/src/megamek/common/ElevationOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import java.util.Objects;

/**
* This is a data record for a possible deployment elevation together with a DeploymentElevationType that
* signals if the elevation is, e.g., on the ground, on a bridge or submerged. Note that two such records are
* equal when their elevation and type are equal.
* This is a data record for a possible deployment elevation together with a DeploymentElevationType that signals if the elevation is, e.g.,
* on the ground, on a bridge or submerged. Note that two such records are equal when their elevation and type are equal. ElevationOptions
* are comparable, with the natural ordering being by their elevation only.
*
* @param elevation The elevation or altitude in the hex
* @param type the DeploymentElevationType (on the ground, on a bridge, ...)
* @param type the DeploymentElevationType (on the ground, on a bridge, ...)
*/
public record ElevationOption(int elevation, DeploymentElevationType type) implements Comparable<ElevationOption> {

Expand Down

0 comments on commit 8fdd24a

Please sign in to comment.