Skip to content

Commit

Permalink
Handled StratCon Scenario Placement Failure when All Coordinates Occu…
Browse files Browse the repository at this point in the history
…pied

Added checks to handle scenarios where facilities or objectives could not be placed because all coordinates were occupied. Issued warnings and errors, and updated the `getUnoccupiedCoords` method to return null when placement is not possible.
  • Loading branch information
IllianiCBT committed Oct 28, 2024
1 parent f0099f1 commit c4b5af8
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions MekHQ/src/mekhq/campaign/stratcon/StratconContractInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ private static void initializeTrackFacilities(StratconTrackState trackState, int

StratconCoords coords = getUnoccupiedCoords(trackState);

if (coords == null) {
logger.warn(String.format("Unable to place facility on track %s," +
" as all coords were occupied. Aborting.",
trackState.getDisplayableName()));
return;
}

trackState.addFacility(coords, sf);

if (strategicObjective) {
Expand Down Expand Up @@ -336,6 +343,13 @@ private static void initializeObjectiveScenarios(Campaign campaign, AtBContract

StratconCoords coords = getUnoccupiedCoords(trackState);

if (coords == null) {
logger.error(String.format("Unable to place objective scenario on track %s," +
" as all coords were occupied. Aborting.",
trackState.getDisplayableName()));
return;
}

// facility
if (template.isFacilityScenario()) {
StratconFacility facility = template.isHostileFacility()
Expand Down Expand Up @@ -376,18 +390,24 @@ private static void initializeObjectiveScenarios(Campaign campaign, AtBContract
* Utility function that, given a track state, picks a random set of unoccupied
* coordinates.
*/
private static StratconCoords getUnoccupiedCoords(StratconTrackState trackState) {
// plonk
public static StratconCoords getUnoccupiedCoords(StratconTrackState trackState) {
// Maximum number of attempts
int maxAttempts = trackState.getWidth() * trackState.getHeight();
int attempts = 0;

int x = Compute.randomInt(trackState.getWidth());
int y = Compute.randomInt(trackState.getHeight());
StratconCoords coords = new StratconCoords(x, y);

// make sure we don't put the facility down on top of anything else
while ((trackState.getFacility(coords) != null) ||
(trackState.getScenario(coords) != null)) {
while ((trackState.getFacility(coords) != null || trackState.getScenario(coords) != null) && attempts < maxAttempts) {
x = Compute.randomInt(trackState.getWidth());
y = Compute.randomInt(trackState.getHeight());
coords = new StratconCoords(x, y);
attempts++;
}

if (attempts == maxAttempts) {
return null;
}

return coords;
Expand Down

0 comments on commit c4b5af8

Please sign in to comment.