Skip to content

Commit

Permalink
Merge pull request #5002 from kuronekochomusuke/fixedMapIssueSpace
Browse files Browse the repository at this point in the history
correct issues with fixed map generation
  • Loading branch information
HammerGS authored Oct 8, 2024
2 parents 976204b + a80cee3 commit 31a2d6c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
34 changes: 1 addition & 33 deletions MekHQ/src/mekhq/campaign/mission/AtBDynamicScenarioFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static void finalizeScenario(AtBDynamicScenario scenario, AtBContract con
// approximate estimate, anyway.
scenario.setLanceCount(generatedLanceCount + (playerForceUnitCount / 4));
setScenarioMapSize(scenario);
setScenarioMap(scenario, campaign.getCampaignOptions().getFixedMapChance());
scenario.setScenarioMap(campaign.getCampaignOptions().getFixedMapChance());
setDeploymentZones(scenario);
setDestinationZones(scenario);

Expand Down Expand Up @@ -1387,38 +1387,6 @@ public static void setScenarioMapSize(AtBDynamicScenario scenario) {
scenario.setMapSizeY(mapSizeY);
}

/**
* If there are maps of the appropriate size available and we roll higher than
* the given threshold, replace the scenario's generated map with a fixed map
* from data/boards
*/
private static void setScenarioMap(AtBDynamicScenario scenario, int mapChance) {
if (scenario.getBoardType() != Scenario.T_SPACE
&& scenario.getTerrainType().equals("Space")
&& (scenario.getMapSizeX() > 0)
&& (scenario.getMapSizeY() > 0)
&& (Compute.randomInt(100) <= mapChance)) {
BoardClassifier bc = BoardClassifier.getInstance();
List<String> maps = bc.getMatchingBoards(scenario.getMapSizeX(), scenario.getMapSizeY(), 5, 5,
new ArrayList<>());

if (!maps.isEmpty()) {
String mapPath = ObjectUtility.getRandomItem(maps);
MegaMekFile mapFile = new MegaMekFile(mapPath);
BoardDimensions dimensions = Board.getSize(mapFile.getFile());

scenario.setMap(bc.getBoardPaths().get(mapPath));
scenario.setMapSizeX(dimensions.width());
scenario.setMapSizeY(dimensions.height());
scenario.setUsingFixedMap(true);
return;
}
}

scenario.setUsingFixedMap(false);
scenario.setMapFile();
}

/**
* Randomly generates the number of scenario modifiers for a scenario,
* for each random scenario in the count a random modifier is applied to the
Expand Down
43 changes: 35 additions & 8 deletions MekHQ/src/mekhq/campaign/mission/AtBScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,16 @@
import java.time.LocalDate;
import java.util.*;

import megamek.common.*;
import megamek.common.util.fileUtils.MegaMekFile;
import megamek.utilities.BoardClassifier;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import megamek.Version;
import megamek.client.generator.TeamLoadOutGenerator;
import megamek.codeUtilities.ObjectUtility;
import megamek.common.Board;
import megamek.common.Compute;
import megamek.common.Entity;
import megamek.common.EntityWeightClass;
import megamek.common.Infantry;
import megamek.common.TargetRoll;
import megamek.common.UnitType;
import megamek.common.WeatherRestriction;
import megamek.common.annotations.Nullable;
import megamek.common.enums.SkillLevel;
import megamek.common.icons.Camouflage;
Expand Down Expand Up @@ -492,6 +487,38 @@ public void setMapFile() {
setMapFile(getTerrainType());
}

/**
* If there are maps of the appropriate size available and we roll higher than
* the given threshold, replace the scenario's generated map with a fixed map
* from data/boards
*/
public void setScenarioMap(int mapChance) {
if (getBoardType() != Scenario.T_SPACE
&& !getTerrainType().equals("Space")
&& (getMapSizeX() > 0)
&& (getMapSizeY() > 0)
&& (Compute.randomInt(100) <= mapChance)) {
BoardClassifier bc = BoardClassifier.getInstance();
List<String> maps = bc.getMatchingBoards(getMapSizeX(), getMapSizeY(), 7, 7,
new ArrayList<>());

if (!maps.isEmpty()) {
String mapPath = ObjectUtility.getRandomItem(maps);
MegaMekFile mapFile = new MegaMekFile(mapPath);
BoardDimensions dimensions = Board.getSize(mapFile.getFile());

setMap(bc.getBoardPaths().get(mapPath));
setMapSizeX(dimensions.width());
setMapSizeY(dimensions.height());
setUsingFixedMap(true);
return;
}
}

setUsingFixedMap(false);
setMapFile();
}

public boolean canRerollTerrain() {
return canRerollMap();
}
Expand Down
12 changes: 8 additions & 4 deletions MekHQ/src/mekhq/gui/view/AtBScenarioViewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,22 +797,26 @@ private void countRerollBoxes() {
private void rerollBattleConditions() {
if (chkReroll[REROLL_TERRAIN] != null && chkReroll[REROLL_TERRAIN].isSelected()) {
scenario.setTerrain();
scenario.setMapFile();
scenario.setScenarioMap(campaign.getCampaignOptions().getFixedMapChance());
scenario.useReroll();
chkReroll[REROLL_TERRAIN].setSelected(false);
lblTerrainDesc.setText(scenario.getTerrainType());
lblMapDesc.setText(scenario.getMap());
lblMapDesc.setText(scenario.getMapForDisplay());
lblMapSizeDesc.setText(scenario.getMapSizeX() + "x" + scenario.getMapSizeY());
}
if (chkReroll[REROLL_MAP] != null && chkReroll[REROLL_MAP].isSelected()) {
scenario.setMapFile();
scenario.setScenarioMap(campaign.getCampaignOptions().getFixedMapChance());
scenario.useReroll();
chkReroll[REROLL_MAP].setSelected(false);
lblMapDesc.setText(scenario.getMap());
lblMapDesc.setText(scenario.getMapForDisplay());
lblMapSizeDesc.setText(scenario.getMapSizeX() + "x" + scenario.getMapSizeY());
}
if (chkReroll[REROLL_MAPSIZE] != null && chkReroll[REROLL_MAPSIZE].isSelected()) {
scenario.setMapSize();
scenario.setScenarioMap(campaign.getCampaignOptions().getFixedMapChance());
scenario.useReroll();
chkReroll[REROLL_MAPSIZE].setSelected(false);
lblMapDesc.setText(scenario.getMapForDisplay());
lblMapSizeDesc.setText(scenario.getMapSizeX() + "x" + scenario.getMapSizeY());
}
if (chkReroll[REROLL_LIGHT] != null && chkReroll[REROLL_LIGHT].isSelected()) {
Expand Down

0 comments on commit 31a2d6c

Please sign in to comment.