Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6061: Restore Altitude Behavior for Princess Deploying Aero #6071

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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