From 00d705dcce348847a0058f83153ff14f9f0f6b5f Mon Sep 17 00:00:00 2001 From: Aaron Gullickson Date: Fri, 3 May 2024 14:58:19 -0700 Subject: [PATCH 1/3] Allow game thread to use mapgen parameters to generate maps --- MekHQ/src/mekhq/GameThread.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/MekHQ/src/mekhq/GameThread.java b/MekHQ/src/mekhq/GameThread.java index ab9159ea18..25e74432c6 100644 --- a/MekHQ/src/mekhq/GameThread.java +++ b/MekHQ/src/mekhq/GameThread.java @@ -132,30 +132,31 @@ public void run() { MapSettings mapSettings = MapSettings.getInstance(); // check that we have valid conditions for setting the mapSettings - if ((scenario.getMapSizeX() > 1) && (scenario.getMapSizeY() > 1) && (null != scenario.getMap())) { + if ((scenario.getMapSizeX() > 1) && (scenario.getMapSizeY() > 1)) { mapSettings.setBoardSize(scenario.getMapSizeX(), scenario.getMapSizeY()); mapSettings.setMapSize(1, 1); - mapSettings.getBoardsSelectedVector().clear(); // if the scenario is taking place in space, do space settings instead if (scenario.getBoardType() == Scenario.T_SPACE) { mapSettings.setMedium(MapSettings.MEDIUM_SPACE); + mapSettings.getBoardsSelectedVector().clear(); mapSettings.getBoardsSelectedVector().add(MapSettings.BOARD_GENERATED); - } else if (scenario.isUsingFixedMap()) { + } else if (scenario.isUsingFixedMap() && scenario.getMap() != null) { String board = scenario.getMap().replace(".board", ""); // TODO : remove inline file type board = board.replace("\\", "/"); + mapSettings.getBoardsSelectedVector().clear(); mapSettings.getBoardsSelectedVector().add(board); if (scenario.getBoardType() == Scenario.T_ATMOSPHERE) { mapSettings.setMedium(MapSettings.MEDIUM_ATMOSPHERE); } - } else { - File mapgenFile = new File("data/mapgen/" + scenario.getMap() + ".xml"); // TODO : remove inline file path + } else if (scenario.getTerrainType() != null){ + File mapgenFile = new File("data/mapgen/" + scenario.getTerrainType() + ".xml"); // TODO : remove inline file path try (InputStream is = new FileInputStream(mapgenFile)) { mapSettings = MapSettings.getInstance(is); } catch (FileNotFoundException ex) { - LogManager.getLogger().error("Could not load map file data/mapgen/" + scenario.getMap() + ".xml", ex); // TODO : remove inline file path + LogManager.getLogger().error("Could not load map file data/mapgen/" + scenario.getTerrainType() + ".xml", ex); // TODO : remove inline file path } if (scenario.getBoardType() == Scenario.T_ATMOSPHERE) { @@ -165,7 +166,10 @@ public void run() { // duplicate code, but getting a new instance of map settings resets the size parameters mapSettings.setBoardSize(scenario.getMapSizeX(), scenario.getMapSizeY()); mapSettings.setMapSize(1, 1); + mapSettings.getBoardsSelectedVector().clear(); mapSettings.getBoardsSelectedVector().add(MapSettings.BOARD_GENERATED); + } else { + LogManager.getLogger().error("invalid map settings provided for scenario " + scenario.getName()); } } else { LogManager.getLogger().error("invalid map settings provided for scenario " + scenario.getName()); From d2d1ef8a85455a1dee1ed168ae088b33b025f80f Mon Sep 17 00:00:00 2001 From: Aaron Gullickson Date: Fri, 3 May 2024 16:58:09 -0700 Subject: [PATCH 2/3] crate mapGenerator variable in Scenario and refactor terrainType to AtBScenario --- MekHQ/src/mekhq/GameThread.java | 6 ++--- .../mekhq/campaign/mission/AtBScenario.java | 13 ++++++++++ .../src/mekhq/campaign/mission/Scenario.java | 24 +++++++++---------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/MekHQ/src/mekhq/GameThread.java b/MekHQ/src/mekhq/GameThread.java index 25e74432c6..9f43aca038 100644 --- a/MekHQ/src/mekhq/GameThread.java +++ b/MekHQ/src/mekhq/GameThread.java @@ -151,12 +151,12 @@ public void run() { if (scenario.getBoardType() == Scenario.T_ATMOSPHERE) { mapSettings.setMedium(MapSettings.MEDIUM_ATMOSPHERE); } - } else if (scenario.getTerrainType() != null){ - File mapgenFile = new File("data/mapgen/" + scenario.getTerrainType() + ".xml"); // TODO : remove inline file path + } else if (scenario.getMapGenerator() != null){ + File mapgenFile = new File("data/mapgen/" + scenario.getMapGenerator() + ".xml"); // TODO : remove inline file path try (InputStream is = new FileInputStream(mapgenFile)) { mapSettings = MapSettings.getInstance(is); } catch (FileNotFoundException ex) { - LogManager.getLogger().error("Could not load map file data/mapgen/" + scenario.getTerrainType() + ".xml", ex); // TODO : remove inline file path + LogManager.getLogger().error("Could not load map file data/mapgen/" + scenario.getMapGenerator() + ".xml", ex); // TODO : remove inline file path } if (scenario.getBoardType() == Scenario.T_ATMOSPHERE) { diff --git a/MekHQ/src/mekhq/campaign/mission/AtBScenario.java b/MekHQ/src/mekhq/campaign/mission/AtBScenario.java index 0fd005b641..c75f481e9a 100644 --- a/MekHQ/src/mekhq/campaign/mission/AtBScenario.java +++ b/MekHQ/src/mekhq/campaign/mission/AtBScenario.java @@ -178,6 +178,8 @@ public abstract class AtBScenario extends Scenario implements IAtBScenario { private Map numPlayerMinefields; + private String terrainType; + protected final transient ResourceBundle defaultResourceBundle = ResourceBundle.getBundle("mekhq.resources.AtBScenarioBuiltIn", MekHQ.getMHQOptions().getLocale()); @@ -252,6 +254,14 @@ public String getDesc() { return getScenarioTypeDescription() + (isStandardScenario() ? (isAttacker() ? " (Attacker)" : " (Defender)") : ""); } + public String getTerrainType() { + return terrainType; + } + + public void setTerrainType(String terrainType) { + this.terrainType = terrainType; + } + @Override public boolean isStandardScenario() { return !isSpecialScenario() && !isBigBattle(); @@ -1505,6 +1515,7 @@ protected void writeToXMLEnd(final PrintWriter pw, int indent) { MHQXMLUtility.writeSimpleXMLTag(pw, indent, "deploymentDelay", deploymentDelay); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "lanceCount", lanceCount); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "rerollsRemaining", rerollsRemaining); + MHQXMLUtility.writeSimpleXMLTag(pw, indent, "terrainType", terrainType); if (null != bigBattleAllies && !bigBattleAllies.isEmpty()) { MHQXMLUtility.writeSimpleXMLOpenTag(pw, indent++, "bigBattleAllies"); @@ -1603,6 +1614,8 @@ protected void loadFieldsFromXmlNode(final Node wn, final Version version, final lanceCount = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("rerollsRemaining")) { rerollsRemaining = Integer.parseInt(wn2.getTextContent()); + } else if (wn2.getNodeName().equalsIgnoreCase("terrainType")) { + terrainType = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("alliesPlayer")) { NodeList nl2 = wn2.getChildNodes(); for (int i = 0; i < nl2.getLength(); i++) { diff --git a/MekHQ/src/mekhq/campaign/mission/Scenario.java b/MekHQ/src/mekhq/campaign/mission/Scenario.java index fd163fdbf6..9e78cc5804 100644 --- a/MekHQ/src/mekhq/campaign/mission/Scenario.java +++ b/MekHQ/src/mekhq/campaign/mission/Scenario.java @@ -91,7 +91,7 @@ public class Scenario { private Map externalIDLookup; /** map generation variables **/ - private String terrainType; + private String mapGenerator; private int mapSizeX; private int mapSizeY; private String map; @@ -235,10 +235,6 @@ public void setCloaked(boolean cloaked) { this.cloaked = cloaked; } - public String getTerrainType() { - return terrainType; - } - public int getStartingPos() { return startingPos; } @@ -295,10 +291,6 @@ public void setStartingAnySEy(int startingAnySEy) { this.startingAnySEy = startingAnySEy; } - public void setTerrainType(String terrainType) { - this.terrainType = terrainType; - } - public void setBoardType(int boardType) { this.boardType = boardType; } @@ -331,6 +323,14 @@ public void setMap(String map) { this.map = map; } + public String getMapGenerator() { + return mapGenerator; + } + + public void setMapGenerator(String s) { + mapGenerator = s; + } + public String getMapForDisplay() { if (!isUsingFixedMap()) { return getMap(); @@ -876,7 +876,7 @@ protected int writeToXMLBegin(final PrintWriter pw, int indent) { MHQXMLUtility.writeSimpleXMLTag(pw, indent, "date", date); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "cloaked", isCloaked()); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "boardType", boardType); - MHQXMLUtility.writeSimpleXMLTag(pw, indent, "terrainType", terrainType); + MHQXMLUtility.writeSimpleXMLTag(pw, indent, "mapGenerator", mapGenerator); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "hasTrack", hasTrack); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "usingFixedMap", isUsingFixedMap()); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "mapSize", mapSizeX, mapSizeY); @@ -1014,8 +1014,8 @@ public static Scenario generateInstanceFromXML(Node wn, Campaign c, Version vers retVal.setUsingFixedMap(Boolean.parseBoolean(wn2.getTextContent().trim())); } else if (wn2.getNodeName().equalsIgnoreCase("boardType")) { retVal.boardType = Integer.parseInt(wn2.getTextContent()); - } else if (wn2.getNodeName().equalsIgnoreCase("terrainType")) { - retVal.terrainType = wn2.getTextContent(); + } else if (wn2.getNodeName().equalsIgnoreCase("mapGenerator")) { + retVal.mapGenerator = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("hasTrack")) { retVal.hasTrack = Boolean.parseBoolean(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("mapSize")) { From 45cb1453d18ff0a184e44f96acd2f8b6bc7671d1 Mon Sep 17 00:00:00 2001 From: Aaron Gullickson Date: Fri, 3 May 2024 22:04:13 -0700 Subject: [PATCH 3/3] Remove mapGenerator variable; use map --- MekHQ/src/mekhq/GameThread.java | 16 ++++++---------- MekHQ/src/mekhq/campaign/mission/Scenario.java | 13 +------------ 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/MekHQ/src/mekhq/GameThread.java b/MekHQ/src/mekhq/GameThread.java index 9f43aca038..ab9159ea18 100644 --- a/MekHQ/src/mekhq/GameThread.java +++ b/MekHQ/src/mekhq/GameThread.java @@ -132,31 +132,30 @@ public void run() { MapSettings mapSettings = MapSettings.getInstance(); // check that we have valid conditions for setting the mapSettings - if ((scenario.getMapSizeX() > 1) && (scenario.getMapSizeY() > 1)) { + if ((scenario.getMapSizeX() > 1) && (scenario.getMapSizeY() > 1) && (null != scenario.getMap())) { mapSettings.setBoardSize(scenario.getMapSizeX(), scenario.getMapSizeY()); mapSettings.setMapSize(1, 1); + mapSettings.getBoardsSelectedVector().clear(); // if the scenario is taking place in space, do space settings instead if (scenario.getBoardType() == Scenario.T_SPACE) { mapSettings.setMedium(MapSettings.MEDIUM_SPACE); - mapSettings.getBoardsSelectedVector().clear(); mapSettings.getBoardsSelectedVector().add(MapSettings.BOARD_GENERATED); - } else if (scenario.isUsingFixedMap() && scenario.getMap() != null) { + } else if (scenario.isUsingFixedMap()) { String board = scenario.getMap().replace(".board", ""); // TODO : remove inline file type board = board.replace("\\", "/"); - mapSettings.getBoardsSelectedVector().clear(); mapSettings.getBoardsSelectedVector().add(board); if (scenario.getBoardType() == Scenario.T_ATMOSPHERE) { mapSettings.setMedium(MapSettings.MEDIUM_ATMOSPHERE); } - } else if (scenario.getMapGenerator() != null){ - File mapgenFile = new File("data/mapgen/" + scenario.getMapGenerator() + ".xml"); // TODO : remove inline file path + } else { + File mapgenFile = new File("data/mapgen/" + scenario.getMap() + ".xml"); // TODO : remove inline file path try (InputStream is = new FileInputStream(mapgenFile)) { mapSettings = MapSettings.getInstance(is); } catch (FileNotFoundException ex) { - LogManager.getLogger().error("Could not load map file data/mapgen/" + scenario.getMapGenerator() + ".xml", ex); // TODO : remove inline file path + LogManager.getLogger().error("Could not load map file data/mapgen/" + scenario.getMap() + ".xml", ex); // TODO : remove inline file path } if (scenario.getBoardType() == Scenario.T_ATMOSPHERE) { @@ -166,10 +165,7 @@ public void run() { // duplicate code, but getting a new instance of map settings resets the size parameters mapSettings.setBoardSize(scenario.getMapSizeX(), scenario.getMapSizeY()); mapSettings.setMapSize(1, 1); - mapSettings.getBoardsSelectedVector().clear(); mapSettings.getBoardsSelectedVector().add(MapSettings.BOARD_GENERATED); - } else { - LogManager.getLogger().error("invalid map settings provided for scenario " + scenario.getName()); } } else { LogManager.getLogger().error("invalid map settings provided for scenario " + scenario.getName()); diff --git a/MekHQ/src/mekhq/campaign/mission/Scenario.java b/MekHQ/src/mekhq/campaign/mission/Scenario.java index 9e78cc5804..0190ae293f 100644 --- a/MekHQ/src/mekhq/campaign/mission/Scenario.java +++ b/MekHQ/src/mekhq/campaign/mission/Scenario.java @@ -91,9 +91,9 @@ public class Scenario { private Map externalIDLookup; /** map generation variables **/ - private String mapGenerator; private int mapSizeX; private int mapSizeY; + // map can be used to represent both fixed and random maps private String map; private boolean usingFixedMap; @@ -323,14 +323,6 @@ public void setMap(String map) { this.map = map; } - public String getMapGenerator() { - return mapGenerator; - } - - public void setMapGenerator(String s) { - mapGenerator = s; - } - public String getMapForDisplay() { if (!isUsingFixedMap()) { return getMap(); @@ -876,7 +868,6 @@ protected int writeToXMLBegin(final PrintWriter pw, int indent) { MHQXMLUtility.writeSimpleXMLTag(pw, indent, "date", date); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "cloaked", isCloaked()); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "boardType", boardType); - MHQXMLUtility.writeSimpleXMLTag(pw, indent, "mapGenerator", mapGenerator); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "hasTrack", hasTrack); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "usingFixedMap", isUsingFixedMap()); MHQXMLUtility.writeSimpleXMLTag(pw, indent, "mapSize", mapSizeX, mapSizeY); @@ -1014,8 +1005,6 @@ public static Scenario generateInstanceFromXML(Node wn, Campaign c, Version vers retVal.setUsingFixedMap(Boolean.parseBoolean(wn2.getTextContent().trim())); } else if (wn2.getNodeName().equalsIgnoreCase("boardType")) { retVal.boardType = Integer.parseInt(wn2.getTextContent()); - } else if (wn2.getNodeName().equalsIgnoreCase("mapGenerator")) { - retVal.mapGenerator = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("hasTrack")) { retVal.hasTrack = Boolean.parseBoolean(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("mapSize")) {