From 339162e2737db00bda57c9a3e8e6db7dc9121958 Mon Sep 17 00:00:00 2001 From: Sheikah45 <66929319+Sheikah45@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:12:01 -0400 Subject: [PATCH] Add densities to mapgen options (#3211) * Add densities to mapgen options * Fix plurality --- .../client/game/GenerateMapController.java | 163 ++++++++++++------ .../client/map/generator/GenerateMapTask.java | 20 ++- .../map/generator/GeneratorCommand.java | 10 +- .../map/generator/GeneratorOptions.java | 3 +- .../map/generator/MapGeneratorService.java | 1 - .../client/preferences/GeneratorPrefs.java | 60 +++++++ src/main/resources/i18n/messages.properties | 3 + .../resources/theme/play/generate_map.fxml | 142 ++++++++------- .../game/GenerateMapControllerTest.java | 117 ++++++------- .../map/generator/GeneratorCommandTest.java | 27 ++- .../generator/MapGeneratorServiceTest.java | 2 - 11 files changed, 352 insertions(+), 196 deletions(-) diff --git a/src/main/java/com/faforever/client/game/GenerateMapController.java b/src/main/java/com/faforever/client/game/GenerateMapController.java index a9cf420ea0..3ebd1979a2 100644 --- a/src/main/java/com/faforever/client/game/GenerateMapController.java +++ b/src/main/java/com/faforever/client/game/GenerateMapController.java @@ -2,6 +2,7 @@ import com.faforever.client.fx.JavaFxUtil; import com.faforever.client.fx.NodeController; +import com.faforever.client.fx.ToStringOnlyConverter; import com.faforever.client.i18n.I18n; import com.faforever.client.map.generator.GenerationType; import com.faforever.client.map.generator.GeneratorOptions; @@ -14,6 +15,7 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; +import javafx.scene.Node; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; @@ -28,6 +30,7 @@ import javafx.util.StringConverter; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.controlsfx.control.RangeSlider; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @@ -63,55 +66,44 @@ public class GenerateMapController extends NodeController { public Label commandLineLabel; public TextField commandLineArgsText; public ComboBox generationTypeComboBox; - public Label mapStyleLabel; public ComboBox mapStyleComboBox; - public Label biomeLabel; public ComboBox biomeComboBox; public Spinner spawnCountSpinner; public Spinner mapSizeSpinner; - public Label symmetryLabel; public ComboBox symmetryComboBox; - public Label customStyleLabel; public CheckBox customStyleCheckBox; - public Label fixedSeedLabel; public CheckBox fixedSeedCheckBox; public TextField seedTextField; public Button seedRerollButton; - public Label terrainLabel; public ComboBox terrainComboBox; - public Label resourceLabel; public ComboBox resourcesComboBox; - public Label propLabel; public ComboBox propsComboBox; + public RangeSlider reclaimDensitySlider; + public RangeSlider resourcesDensitySlider; private Runnable onCloseButtonClickedListener; - private final ObservableList validTeamSizes = FXCollections.observableList(IntStream.range(0, 17) - .filter(value -> value != 1) - .boxed() - .collect(Collectors.toList())); + private final ObservableList validTeamSizes = FXCollections.observableList( + IntStream.range(0, 17).filter(value -> value != 1).boxed().collect(Collectors.toList())); private final FilteredList selectableTeamSizes = new FilteredList<>(validTeamSizes); - private final ObservableList validSpawnCount = FXCollections.observableList(IntStream.range(2, 17) - .boxed() - .collect(Collectors.toList())); + private final ObservableList validSpawnCount = FXCollections.observableList( + IntStream.range(2, 17).boxed().collect(Collectors.toList())); private final FilteredList selectableSpawnCounts = new FilteredList<>(validSpawnCount); public Spinner numTeamsSpinner; @Override protected void onInitialize() { - JavaFxUtil.bindManagedToVisible(commandLineLabel, commandLineArgsText, mapStyleComboBox, mapStyleLabel, biomeComboBox, biomeLabel); + JavaFxUtil.bindManagedToVisible(commandLineLabel, commandLineArgsText); initCommandlineArgs(); initGenerationTypeComboBox(); initSymmetryComboBox(); initMapStyleComboBox(); - initCheckBoxes(); + initCustomStyleOptions(); initNumTeamsSpinner(); initSpawnCountSpinner(); initMapSizeSpinner(); initSeedField(); - initGeneratorComboBox(terrainComboBox); - initGeneratorComboBox(biomeComboBox); - initGeneratorComboBox(resourcesComboBox); - initGeneratorComboBox(propsComboBox); + bindCustomStyleDisabledProperty(terrainComboBox, biomeComboBox, resourcesComboBox, propsComboBox, + resourcesDensitySlider, reclaimDensitySlider); } private StringConverter getGenerationTypeConverter() { @@ -164,8 +156,9 @@ private void initGenerationTypeComboBox() { generationTypeComboBox.setValue(generationType); generatorPrefs.generationTypeProperty().bind(generationTypeComboBox.valueProperty()); generationTypeComboBox.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(commandLineArgsText.textProperty().isNotEmpty())); + .bind(previousMapName.textProperty() + .isNotEmpty() + .or(commandLineArgsText.textProperty().isNotEmpty())); } private void initNumTeamsSpinner() { @@ -192,8 +185,8 @@ private void initNumTeamsSpinner() { }); generatorPrefs.numTeamsProperty().bind(numTeamsSpinner.valueProperty()); numTeamsSpinner.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(commandLineArgsText.textProperty().isNotEmpty())); + .bind( + previousMapName.textProperty().isNotEmpty().or(commandLineArgsText.textProperty().isNotEmpty())); int lastIndex = selectableTeamSizes.indexOf(numTeamsProperty); numTeamsSpinner.increment(lastIndex >= 0 ? lastIndex : 1); } @@ -207,8 +200,9 @@ private void initSpawnCountSpinner() { spawnCountSpinner.setValueFactory(new ListSpinnerValueFactory<>(selectableSpawnCounts)); generatorPrefs.spawnCountProperty().bind(spawnCountSpinner.valueProperty()); spawnCountSpinner.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(commandLineArgsText.textProperty().isNotEmpty())); + .bind(previousMapName.textProperty() + .isNotEmpty() + .or(commandLineArgsText.textProperty().isNotEmpty())); int lastIndex = selectableSpawnCounts.indexOf(spawnCountProperty); spawnCountSpinner.increment(Math.max(lastIndex, 0)); } @@ -219,61 +213,85 @@ private void initMapSizeSpinner() { mapSizeSpinner.getValueFactory().setConverter(getMapSizeConverter()); generatorPrefs.mapSizeInKmProperty().bind(mapSizeSpinner.getValueFactory().valueProperty()); mapSizeSpinner.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(commandLineArgsText.textProperty().isNotEmpty())); + .bind( + previousMapName.textProperty().isNotEmpty().or(commandLineArgsText.textProperty().isNotEmpty())); } private void initSymmetryComboBox() { symmetryComboBox.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty())); + .bind(previousMapName.textProperty() + .isNotEmpty() + .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) + .or(commandLineArgsText.textProperty().isNotEmpty())); } private void initMapStyleComboBox() { mapStyleComboBox.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty()) - .or(customStyleCheckBox.selectedProperty())); + .bind(previousMapName.textProperty() + .isNotEmpty() + .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) + .or(commandLineArgsText.textProperty().isNotEmpty()) + .or(customStyleCheckBox.selectedProperty())); } - private void initCheckBoxes() { + private void initCustomStyleOptions() { customStyleCheckBox.setSelected(generatorPrefs.getCustomStyle()); generatorPrefs.customStyleProperty().bind(customStyleCheckBox.selectedProperty()); customStyleCheckBox.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty())); + .bind(previousMapName.textProperty() + .isNotEmpty() + .or(generationTypeComboBox.valueProperty() + .isNotEqualTo(GenerationType.CASUAL)) + .or(commandLineArgsText.textProperty().isNotEmpty())); fixedSeedCheckBox.setSelected(generatorPrefs.getFixedSeed()); generatorPrefs.fixedSeedProperty().bind(fixedSeedCheckBox.selectedProperty()); fixedSeedCheckBox.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty())); + .bind(previousMapName.textProperty() + .isNotEmpty() + .or(generationTypeComboBox.valueProperty() + .isNotEqualTo(GenerationType.CASUAL)) + .or(commandLineArgsText.textProperty().isNotEmpty())); + + reclaimDensitySlider.setLabelFormatter( + new LessMoreStringConverter(reclaimDensitySlider.getMin(), reclaimDensitySlider.getMax())); + resourcesDensitySlider.setLabelFormatter( + new LessMoreStringConverter(resourcesDensitySlider.getMin(), resourcesDensitySlider.getMax())); + reclaimDensitySlider.setHighValue(generatorPrefs.getReclaimDensityMax()); + generatorPrefs.reclaimDensityMaxProperty().bind(reclaimDensitySlider.highValueProperty()); + reclaimDensitySlider.setLowValue(generatorPrefs.getReclaimDensityMin()); + generatorPrefs.reclaimDensityMinProperty().bind(reclaimDensitySlider.lowValueProperty()); + resourcesDensitySlider.setHighValue(generatorPrefs.getResourceDensityMax()); + generatorPrefs.resourceDensityMaxProperty().bind(resourcesDensitySlider.highValueProperty()); + resourcesDensitySlider.setLowValue(generatorPrefs.getResourceDensityMin()); + generatorPrefs.resourceDensityMinProperty().bind(resourcesDensitySlider.lowValueProperty()); } private void initSeedField() { seedTextField.setText(String.valueOf(generatorPrefs.getSeed())); generatorPrefs.seedProperty().bind(seedTextField.textProperty()); seedTextField.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty()) - .or(fixedSeedCheckBox.selectedProperty().not())); + .bind(previousMapName.textProperty() + .isNotEmpty() + .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) + .or(commandLineArgsText.textProperty().isNotEmpty()) + .or(fixedSeedCheckBox.selectedProperty().not())); seedRerollButton.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty()) - .or(fixedSeedCheckBox.selectedProperty().not())); + .bind(previousMapName.textProperty() + .isNotEmpty() + .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) + .or(commandLineArgsText.textProperty().isNotEmpty()) + .or(fixedSeedCheckBox.selectedProperty().not())); } - private void initGeneratorComboBox(ComboBox comboBox) { - comboBox.disableProperty() - .bind(previousMapName.textProperty().isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty()) - .or(customStyleCheckBox.selectedProperty().not())); + private void bindCustomStyleDisabledProperty(Node... nodes) { + for (Node node : nodes) { + node.disableProperty() + .bind(previousMapName.textProperty() + .isNotEmpty() + .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) + .or(commandLineArgsText.textProperty().isNotEmpty()) + .or(customStyleCheckBox.selectedProperty().not())); + } } private GeneratorOptions getGeneratorOptions() { @@ -296,6 +314,21 @@ private GeneratorOptions getGeneratorOptions() { optionsBuilder.textureStyle(biomeComboBox.getValue()); optionsBuilder.resourceStyle(resourcesComboBox.getValue()); optionsBuilder.propStyle(propsComboBox.getValue()); + Random random = new Random(); + int reclaimLowValue = (int) reclaimDensitySlider.getLowValue(); + int reclaimHighValue = (int) reclaimDensitySlider.getHighValue(); + if (reclaimLowValue == reclaimHighValue) { + optionsBuilder.reclaimDensity(reclaimLowValue / 127f); + } else { + optionsBuilder.reclaimDensity(random.nextInt(reclaimLowValue, reclaimHighValue) / 127f); + } + int resourcesLowValue = (int) resourcesDensitySlider.getLowValue(); + int resourcesHighValue = (int) resourcesDensitySlider.getHighValue(); + if (resourcesLowValue == resourcesHighValue) { + optionsBuilder.resourceDensity(resourcesLowValue / 127f); + } else { + optionsBuilder.resourceDensity(random.nextInt(resourcesLowValue, resourcesHighValue) / 127f); + } } else { optionsBuilder.style(mapStyleComboBox.getValue()); } @@ -458,4 +491,20 @@ void setOnCloseButtonClickedListener(Runnable onCloseButtonClickedListener) { public void onSeedRerollButtonClicked() { seedTextField.setText(String.valueOf(new Random().nextLong())); } + + private class LessMoreStringConverter extends ToStringOnlyConverter { + public LessMoreStringConverter(Number min, Number max) { + super(number -> { + if (number.equals(max)) { + return i18n.get("more"); + } + + if (number.equals(min)) { + return i18n.get("less"); + } + + return ""; + }); + } + } } diff --git a/src/main/java/com/faforever/client/map/generator/GenerateMapTask.java b/src/main/java/com/faforever/client/map/generator/GenerateMapTask.java index d6b3948848..25f116a35e 100644 --- a/src/main/java/com/faforever/client/map/generator/GenerateMapTask.java +++ b/src/main/java/com/faforever/client/map/generator/GenerateMapTask.java @@ -37,7 +37,6 @@ public class GenerateMapTask extends CompletableTask { private Path generatorExecutableFile; private ComparableVersion version; private GeneratorOptions generatorOptions; - private String seed; private String mapName; @Autowired @@ -57,10 +56,12 @@ protected String call() throws Exception { updateTitle(i18n.get("game.mapGeneration.generateMap.title", version)); GeneratorCommand.GeneratorCommandBuilder generatorCommandBuilder = GeneratorCommand.builder() - .version(version) - .generatorExecutableFile(generatorExecutableFile) - .javaExecutable(operatingSystem.getJavaExecutablePath()) - .mapName(mapName); + .version(version) + .generatorExecutableFile( + generatorExecutableFile) + .javaExecutable( + operatingSystem.getJavaExecutablePath()) + .mapName(mapName); if (generatorOptions != null) { generatorCommandBuilder.spawnCount(generatorOptions.spawnCount()) @@ -74,6 +75,8 @@ protected String call() throws Exception { .textureStyle(generatorOptions.textureStyle()) .resourceStyle(generatorOptions.resourceStyle()) .propStyle(generatorOptions.propStyle()) + .resourceDensity(generatorOptions.resourceDensity()) + .reclaimDensity(generatorOptions.reclaimDensity()) .commandLineArgs(generatorOptions.commandLineArgs()); } @@ -86,8 +89,8 @@ protected String call() throws Exception { processBuilder.directory(workingDirectory.toFile()); processBuilder.command(command); - log.info("Starting map generator in directory: `{}` with command: `{}`", - processBuilder.directory(), String.join(" ", processBuilder.command())); + log.info("Starting map generator in directory: `{}` with command: `{}`", processBuilder.directory(), + String.join(" ", processBuilder.command())); Process process = processBuilder.start(); OsUtils.gobbleLines(process.getInputStream(), msg -> { @@ -106,7 +109,8 @@ protected String call() throws Exception { "--visualize")) { log.warn("Map generation timed out, killing process"); process.destroyForcibly(); - notificationService.addImmediateErrorNotification(new RuntimeException("Map generation timed out"), "game.mapGeneration.failed.message"); + notificationService.addImmediateErrorNotification(new RuntimeException("Map generation timed out"), + "game.mapGeneration.failed.message"); } } catch (Exception e) { log.error("Could not start map generator", e); diff --git a/src/main/java/com/faforever/client/map/generator/GeneratorCommand.java b/src/main/java/com/faforever/client/map/generator/GeneratorCommand.java index 51e2f95db7..de48376fbd 100644 --- a/src/main/java/com/faforever/client/map/generator/GeneratorCommand.java +++ b/src/main/java/com/faforever/client/map/generator/GeneratorCommand.java @@ -25,7 +25,7 @@ public record GeneratorCommand( String terrainStyle, String textureStyle, String resourceStyle, - String propStyle, + String propStyle, Float reclaimDensity, Float resourceDensity, String commandLineArgs ) { @@ -93,6 +93,14 @@ public List getCommand() { command.addAll(Arrays.asList("--prop-style", propStyle)); } + if (resourceDensity != null) { + command.addAll(Arrays.asList("--resource-density", String.valueOf(resourceDensity))); + } + + if (reclaimDensity != null) { + command.addAll(Arrays.asList("--reclaim-density", String.valueOf(reclaimDensity))); + } + return command; } else { return Arrays.asList(javaPath, "-jar", generatorExecutableFile.toAbsolutePath().toString(), ".", diff --git a/src/main/java/com/faforever/client/map/generator/GeneratorOptions.java b/src/main/java/com/faforever/client/map/generator/GeneratorOptions.java index bf81ccbe1f..26342a5746 100644 --- a/src/main/java/com/faforever/client/map/generator/GeneratorOptions.java +++ b/src/main/java/com/faforever/client/map/generator/GeneratorOptions.java @@ -13,7 +13,6 @@ public record GeneratorOptions( String style, String terrainStyle, String textureStyle, - String resourceStyle, - String propStyle, + String resourceStyle, String propStyle, Float reclaimDensity, Float resourceDensity, String commandLineArgs ) {} diff --git a/src/main/java/com/faforever/client/map/generator/MapGeneratorService.java b/src/main/java/com/faforever/client/map/generator/MapGeneratorService.java index b101a82fa0..c793ae6ddd 100644 --- a/src/main/java/com/faforever/client/map/generator/MapGeneratorService.java +++ b/src/main/java/com/faforever/client/map/generator/MapGeneratorService.java @@ -122,7 +122,6 @@ public Mono generateMap(String mapName) { GenerateMapTask generateMapTask = generateMapTaskFactory.getObject(); generateMapTask.setVersion(version); - generateMapTask.setSeed(seed); generateMapTask.setMapName(mapName); generateMapTask.setGeneratorExecutableFile(generatorExecutablePath); diff --git a/src/main/java/com/faforever/client/preferences/GeneratorPrefs.java b/src/main/java/com/faforever/client/preferences/GeneratorPrefs.java index 6336d01238..d1860bae81 100644 --- a/src/main/java/com/faforever/client/preferences/GeneratorPrefs.java +++ b/src/main/java/com/faforever/client/preferences/GeneratorPrefs.java @@ -28,6 +28,10 @@ public class GeneratorPrefs { private final StringProperty textureStyle = new SimpleStringProperty(""); private final StringProperty resourceStyle = new SimpleStringProperty(""); private final StringProperty propStyle = new SimpleStringProperty(""); + private final IntegerProperty reclaimDensityMin = new SimpleIntegerProperty(0); + private final IntegerProperty reclaimDensityMax = new SimpleIntegerProperty(127); + private final IntegerProperty resourceDensityMin = new SimpleIntegerProperty(0); + private final IntegerProperty resourceDensityMax = new SimpleIntegerProperty(127); public GenerationType getGenerationType() { return generationType.get(); @@ -196,4 +200,60 @@ public void setPropStyle(String propStyle) { public StringProperty propStyleProperty() { return propStyle; } + + public boolean isFixedSeed() { + return fixedSeed.get(); + } + + public boolean isCustomStyle() { + return customStyle.get(); + } + + public int getReclaimDensityMin() { + return reclaimDensityMin.get(); + } + + public IntegerProperty reclaimDensityMinProperty() { + return reclaimDensityMin; + } + + public void setReclaimDensityMin(int min) { + this.reclaimDensityMin.set(min); + } + + public int getReclaimDensityMax() { + return reclaimDensityMax.get(); + } + + public IntegerProperty reclaimDensityMaxProperty() { + return reclaimDensityMax; + } + + public void setReclaimDensityMax(int max) { + this.reclaimDensityMax.set(max); + } + + public int getResourceDensityMin() { + return resourceDensityMin.get(); + } + + public IntegerProperty resourceDensityMinProperty() { + return resourceDensityMin; + } + + public void setResourceDensityMin(int min) { + this.resourceDensityMin.set(min); + } + + public int getResourceDensityMax() { + return resourceDensityMax.get(); + } + + public IntegerProperty resourceDensityMaxProperty() { + return resourceDensityMax; + } + + public void setResourceDensityMax(int max) { + this.resourceDensityMax.set(max); + } } diff --git a/src/main/resources/i18n/messages.properties b/src/main/resources/i18n/messages.properties index 97952097d3..a106dea2a3 100644 --- a/src/main/resources/i18n/messages.properties +++ b/src/main/resources/i18n/messages.properties @@ -10,6 +10,7 @@ errorTitle = Uh oh\! bytesProgress = {0} of {1} messageFromServer = Message from server more = More +less=Less searchResult = Search result search = Search back = Back @@ -828,6 +829,8 @@ game.generateMap.seed = Use a fixed seed game.generateMap.terrain = Terrain Generator game.generateMap.resources = Resource Generator game.generateMap.props = Prop Generator +game.generateMap.resource.density=Resource Density +game.generateMap.reclaim.density=Reclaim Density game.generateMap.dialog = Map Generator Options game.generateMap.nameCaution = Enter the name of a generated map you want to regenerate. Map name must be of the form neroxis_map_generator_(version)_(seed)_(options). game.generateMap.random = Random diff --git a/src/main/resources/theme/play/generate_map.fxml b/src/main/resources/theme/play/generate_map.fxml index febdcec904..438abc571b 100644 --- a/src/main/resources/theme/play/generate_map.fxml +++ b/src/main/resources/theme/play/generate_map.fxml @@ -13,93 +13,115 @@ - + + - - + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - + - diff --git a/src/test/java/com/faforever/client/game/GenerateMapControllerTest.java b/src/test/java/com/faforever/client/game/GenerateMapControllerTest.java index ebac79c47e..6c4b4c536a 100644 --- a/src/test/java/com/faforever/client/game/GenerateMapControllerTest.java +++ b/src/test/java/com/faforever/client/game/GenerateMapControllerTest.java @@ -14,7 +14,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; -import org.testfx.util.WaitForAsyncUtils; import reactor.core.publisher.Mono; import java.util.ArrayList; @@ -61,6 +60,10 @@ public void unbindProperties() { generatorPrefs.resourceStyleProperty().unbind(); generatorPrefs.propStyleProperty().unbind(); generatorPrefs.commandLineArgsProperty().unbind(); + generatorPrefs.reclaimDensityMinProperty().unbind(); + generatorPrefs.reclaimDensityMaxProperty().unbind(); + generatorPrefs.resourceDensityMinProperty().unbind(); + generatorPrefs.resourceDensityMaxProperty().unbind(); } @BeforeEach @@ -74,8 +77,7 @@ public void setUp() throws Exception { @Test public void testBadMapNameFails() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.previousMapName.setText("Bad"); instance.onGenerateMap(); @@ -84,8 +86,7 @@ public void testBadMapNameFails() { @Test public void testSetLastSpawnCount() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); assertEquals(10, instance.spawnCountSpinner.getValue().intValue()); } @@ -94,8 +95,7 @@ public void testSetLastSpawnCount() { public void testSetLastNumTeams() { generatorPrefs.setNumTeams(5); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); assertEquals(instance.numTeamsSpinner.getValue().intValue(), 5); } @@ -103,8 +103,7 @@ public void testSetLastNumTeams() { @Test public void testSetLastMapSize() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); assertEquals(instance.mapSizeSpinner.getValue(), 10.0); assertEquals((int) instance.spawnCountSpinner.getValue(), 10); @@ -114,8 +113,7 @@ public void testSetLastMapSize() { public void testSetLastSymmetry() { generatorPrefs.setSymmetry("Test"); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.setSymmetries(new ArrayList<>(List.of("Test"))); @@ -126,8 +124,7 @@ public void testSetLastSymmetry() { public void testSetLastFixedSeed() { generatorPrefs.setFixedSeed(true); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); assertTrue(instance.fixedSeedCheckBox.isSelected()); } @@ -136,8 +133,7 @@ public void testSetLastFixedSeed() { public void testSetLastSeed() { generatorPrefs.setSeed("100"); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); assertEquals(instance.seedTextField.getText(), "100"); } @@ -146,8 +142,7 @@ public void testSetLastSeed() { public void testSetLastMapStyle() { generatorPrefs.setMapStyle("TEST"); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.setStyles(new ArrayList<>(List.of("TEST"))); @@ -158,8 +153,7 @@ public void testSetLastMapStyle() { public void testSetLastCustomStyle() { generatorPrefs.setCustomStyle(true); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); assertTrue(instance.customStyleCheckBox.isSelected()); } @@ -168,8 +162,7 @@ public void testSetLastCustomStyle() { public void testSetLastTerrainStyle() { generatorPrefs.setTerrainStyle("Test"); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.setTerrainStyles(new ArrayList<>(List.of("Test"))); @@ -180,8 +173,7 @@ public void testSetLastTerrainStyle() { public void testSetLastTextureStyle() { generatorPrefs.setTextureStyle("Test"); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.setTextureStyles(new ArrayList<>(List.of("Test"))); @@ -192,8 +184,7 @@ public void testSetLastTextureStyle() { public void testSetLastResourceStyle() { generatorPrefs.setResourceStyle("Test"); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.setResourceStyles(new ArrayList<>(List.of("Test"))); @@ -204,8 +195,7 @@ public void testSetLastResourceStyle() { public void testSetLastPropStyle() { generatorPrefs.setPropStyle("Test"); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.setPropStyles(new ArrayList<>(List.of("Test"))); @@ -216,8 +206,7 @@ public void testSetLastPropStyle() { public void testSetLastCommandLineArgs() { generatorPrefs.setCommandLineArgs("--help"); - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); assertEquals("--help", instance.commandLineArgsText.getText()); assertTrue(instance.commandLineArgsText.isVisible()); @@ -226,8 +215,7 @@ public void testSetLastCommandLineArgs() { @Test public void testCommandLineArgsNotVisibleWhenNotSetInitially() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); assertFalse(instance.commandLineArgsText.isVisible()); assertFalse(instance.commandLineLabel.isVisible()); @@ -235,14 +223,12 @@ public void testCommandLineArgsNotVisibleWhenNotSetInitially() { @Test public void testToggleCommandLineArgs() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); assertFalse(instance.commandLineArgsText.isVisible()); assertFalse(instance.commandLineLabel.isVisible()); - WaitForAsyncUtils.asyncFx(() -> instance.toggleCommandlineInput()); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> instance.toggleCommandlineInput()); assertTrue(instance.commandLineArgsText.isVisible()); assertTrue(instance.commandLineLabel.isVisible()); @@ -250,8 +236,7 @@ public void testToggleCommandLineArgs() { @Test public void testOptionsNotDisabledWithoutMapName() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.previousMapName.setText("neroxis_map_generator"); instance.previousMapName.setText(""); @@ -266,9 +251,9 @@ public void testOptionsNotDisabledWithoutMapName() { @Test public void testOptionsDisabledWithMapName() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.previousMapName.setText("neroxis_map_generator"); + instance.customStyleCheckBox.setSelected(true); assertTrue(instance.commandLineArgsText.isDisabled()); assertTrue(instance.generationTypeComboBox.isDisabled()); @@ -284,14 +269,15 @@ public void testOptionsDisabledWithMapName() { assertTrue(instance.terrainComboBox.isDisabled()); assertTrue(instance.biomeComboBox.isDisabled()); assertTrue(instance.resourcesComboBox.isDisabled()); - assertTrue(instance.propsComboBox.isDisabled()); + assertTrue(instance.resourcesDensitySlider.isDisabled()); + assertTrue(instance.reclaimDensitySlider.isDisabled()); } @Test public void testOptionsDisabledWithCommandLine() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.commandLineArgsText.setText("--help"); + instance.customStyleCheckBox.setSelected(true); assertTrue(instance.generationTypeComboBox.isDisabled()); assertTrue(instance.spawnCountSpinner.isDisabled()); @@ -307,12 +293,13 @@ public void testOptionsDisabledWithCommandLine() { assertTrue(instance.biomeComboBox.isDisabled()); assertTrue(instance.resourcesComboBox.isDisabled()); assertTrue(instance.propsComboBox.isDisabled()); + assertTrue(instance.resourcesDensitySlider.isDisabled()); + assertTrue(instance.reclaimDensitySlider.isDisabled()); } @Test public void testOptionsDisabledWithoutCustomStyle() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.customStyleCheckBox.setSelected(false); assertFalse(instance.mapStyleComboBox.isDisabled()); @@ -320,12 +307,13 @@ public void testOptionsDisabledWithoutCustomStyle() { assertTrue(instance.biomeComboBox.isDisabled()); assertTrue(instance.resourcesComboBox.isDisabled()); assertTrue(instance.propsComboBox.isDisabled()); + assertTrue(instance.resourcesDensitySlider.isDisabled()); + assertTrue(instance.reclaimDensitySlider.isDisabled()); } @Test public void testOptionsNotDisabledWithCustomStyle() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.customStyleCheckBox.setSelected(true); assertTrue(instance.mapStyleComboBox.isDisabled()); @@ -333,12 +321,13 @@ public void testOptionsNotDisabledWithCustomStyle() { assertFalse(instance.biomeComboBox.isDisabled()); assertFalse(instance.resourcesComboBox.isDisabled()); assertFalse(instance.propsComboBox.isDisabled()); + assertFalse(instance.resourcesDensitySlider.isDisabled()); + assertFalse(instance.reclaimDensitySlider.isDisabled()); } @Test public void testSeedDisabledWithoutFixedSeed() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.fixedSeedCheckBox.setSelected(false); assertTrue(instance.seedTextField.isDisabled()); @@ -347,8 +336,7 @@ public void testSeedDisabledWithoutFixedSeed() { @Test public void testSeedNotDisabledWithFixedSeed() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.fixedSeedCheckBox.setSelected(true); assertFalse(instance.seedTextField.isDisabled()); @@ -357,8 +345,7 @@ public void testSeedNotDisabledWithFixedSeed() { @Test public void testOptionsNotDisabledWithCasual() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.generationTypeComboBox.setValue(GenerationType.TOURNAMENT); instance.generationTypeComboBox.setValue(GenerationType.CASUAL); @@ -373,9 +360,9 @@ public void testOptionsNotDisabledWithCasual() { @Test public void testOptionsDisabledWithTournament() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.generationTypeComboBox.setValue(GenerationType.TOURNAMENT); + instance.customStyleCheckBox.setSelected(true); assertFalse(instance.generationTypeComboBox.isDisabled()); assertFalse(instance.spawnCountSpinner.isDisabled()); @@ -391,13 +378,15 @@ public void testOptionsDisabledWithTournament() { assertTrue(instance.biomeComboBox.isDisabled()); assertTrue(instance.resourcesComboBox.isDisabled()); assertTrue(instance.propsComboBox.isDisabled()); + assertTrue(instance.resourcesDensitySlider.isDisabled()); + assertTrue(instance.reclaimDensitySlider.isDisabled()); } @Test public void testOptionsDisabledWithBlind() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.generationTypeComboBox.setValue(GenerationType.BLIND); + instance.customStyleCheckBox.setSelected(true); assertFalse(instance.generationTypeComboBox.isDisabled()); assertFalse(instance.spawnCountSpinner.isDisabled()); @@ -413,13 +402,15 @@ public void testOptionsDisabledWithBlind() { assertTrue(instance.biomeComboBox.isDisabled()); assertTrue(instance.resourcesComboBox.isDisabled()); assertTrue(instance.propsComboBox.isDisabled()); + assertTrue(instance.resourcesDensitySlider.isDisabled()); + assertTrue(instance.reclaimDensitySlider.isDisabled()); } @Test public void testOptionsDisabledWithUnexplored() { - WaitForAsyncUtils.asyncFx(() -> reinitialize(instance)); - WaitForAsyncUtils.waitForFxEvents(); + runOnFxThreadAndWait(() -> reinitialize(instance)); instance.generationTypeComboBox.setValue(GenerationType.UNEXPLORED); + instance.customStyleCheckBox.setSelected(true); assertFalse(instance.generationTypeComboBox.isDisabled()); assertFalse(instance.spawnCountSpinner.isDisabled()); @@ -435,6 +426,8 @@ public void testOptionsDisabledWithUnexplored() { assertTrue(instance.biomeComboBox.isDisabled()); assertTrue(instance.resourcesComboBox.isDisabled()); assertTrue(instance.propsComboBox.isDisabled()); + assertTrue(instance.resourcesDensitySlider.isDisabled()); + assertTrue(instance.reclaimDensitySlider.isDisabled()); } @Test @@ -460,6 +453,10 @@ public void testGetGenerateMapNoNameCustomStyle() { generatorPrefs.setNumTeams(2); generatorPrefs.setMapSizeInKm(10.0); generatorPrefs.setGenerationType(GenerationType.CASUAL); + generatorPrefs.setResourceDensityMin(5); + generatorPrefs.setResourceDensityMax(5); + generatorPrefs.setReclaimDensityMin(10); + generatorPrefs.setReclaimDensityMax(10); instance.symmetryComboBox.setItems(FXCollections.observableList(List.of("SYMMETRY"))); instance.symmetryComboBox.getSelectionModel().selectFirst(); @@ -493,6 +490,8 @@ public void testGetGenerateMapNoNameCustomStyle() { assertEquals("BIOME", result.textureStyle()); assertEquals("MEXES", result.resourceStyle()); assertEquals("PROPS", result.propStyle()); + assertEquals(10 / 127f, result.reclaimDensity()); + assertEquals(5 / 127f, result.resourceDensity()); } @Test @@ -530,6 +529,8 @@ public void testGetGenerateMapNoNameMapStyle() { assertNull(result.textureStyle()); assertNull(result.resourceStyle()); assertNull(result.propStyle()); + assertNull(result.resourceDensity()); + assertNull(result.reclaimDensity()); } } diff --git a/src/test/java/com/faforever/client/map/generator/GeneratorCommandTest.java b/src/test/java/com/faforever/client/map/generator/GeneratorCommandTest.java index 18dbfbbf80..8339fb2d28 100644 --- a/src/test/java/com/faforever/client/map/generator/GeneratorCommandTest.java +++ b/src/test/java/com/faforever/client/map/generator/GeneratorCommandTest.java @@ -10,6 +10,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInRelativeOrder; import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -83,37 +84,49 @@ public void testMapNameNoException() { @Test public void testSeedSet() { List command = defaultBuilder().seed("100").build().getCommand(); - assertTrue(command.containsAll(List.of("--seed", "100"))); + assertThat(command, containsInRelativeOrder("--seed", "100")); } @Test public void testSymmetrySet() { List command = defaultBuilder().symmetry("XZ").build().getCommand(); - assertTrue(command.containsAll(List.of("--terrain-symmetry", "XZ"))); + assertThat(command, containsInRelativeOrder("--terrain-symmetry", "XZ")); } @Test public void testTerrainStyleSet() { List command = defaultBuilder().terrainStyle("TERRAIN").build().getCommand(); - assertTrue(command.containsAll(List.of("--terrain-style", "TERRAIN"))); + assertThat(command, containsInRelativeOrder("--terrain-style", "TERRAIN")); } @Test public void testTextureStyleSet() { List command = defaultBuilder().textureStyle("BIOME").build().getCommand(); - assertTrue(command.containsAll(List.of("--texture-style", "BIOME"))); + assertThat(command, containsInRelativeOrder("--texture-style", "BIOME")); } @Test public void testResourceStyleSet() { List command = defaultBuilder().resourceStyle("RESOURCE").build().getCommand(); - assertTrue(command.containsAll(List.of("--resource-style", "RESOURCE"))); + assertThat(command, containsInRelativeOrder("--resource-style", "RESOURCE")); } @Test public void testPropStyleSet() { List command = defaultBuilder().propStyle("PROPS").build().getCommand(); - assertTrue(command.containsAll(List.of("--prop-style", "PROPS"))); + assertThat(command, containsInRelativeOrder("--prop-style", "PROPS")); + } + + @Test + public void testResourceDensitySet() { + List command = defaultBuilder().resourceDensity(.5f).build().getCommand(); + assertThat(command, containsInRelativeOrder("--resource-density", "0.5")); + } + + @Test + public void testReclaimDensitySet() { + List command = defaultBuilder().reclaimDensity(.5f).build().getCommand(); + assertThat(command, containsInRelativeOrder("--reclaim-density", "0.5")); } @Test @@ -143,7 +156,7 @@ public void testUnexploredType() { @Test public void testStyleSet() { List command = defaultBuilder().style("TEST").build().getCommand(); - assertTrue(command.containsAll(List.of("--style", "TEST"))); + assertThat(command, containsInRelativeOrder("--style", "TEST")); } @Test diff --git a/src/test/java/com/faforever/client/map/generator/MapGeneratorServiceTest.java b/src/test/java/com/faforever/client/map/generator/MapGeneratorServiceTest.java index 9328dd38cc..8f098a768c 100644 --- a/src/test/java/com/faforever/client/map/generator/MapGeneratorServiceTest.java +++ b/src/test/java/com/faforever/client/map/generator/MapGeneratorServiceTest.java @@ -125,7 +125,6 @@ public void testGenerateMapGeneratorPresent() throws Exception { verify(taskService).submitTask(generateMapTask); verify(generateMapTask).setVersion(versionGeneratorPresent); - verify(generateMapTask).setSeed(seed); verify(generateMapTask).setMapName(testMapNameGenerator); String generatorExecutableName = String.format(MapGeneratorService.GENERATOR_EXECUTABLE_FILENAME, versionGeneratorPresent); @@ -160,7 +159,6 @@ public void testGenerateMapWithGeneratorOptions() { verify(generateMapTask).setVersion(versionGeneratorPresent); verify(generateMapTask).setGeneratorOptions(generatorOptions); verify(generateMapTask, never()).setMapName(anyString()); - verify(generateMapTask, never()).setSeed(anyString()); } @Test