diff --git a/MekHQ/src/mekhq/campaign/stratcon/StratconContractInitializer.java b/MekHQ/src/mekhq/campaign/stratcon/StratconContractInitializer.java index 53da4fce77..64d15a232b 100644 --- a/MekHQ/src/mekhq/campaign/stratcon/StratconContractInitializer.java +++ b/MekHQ/src/mekhq/campaign/stratcon/StratconContractInitializer.java @@ -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) { @@ -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() @@ -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;