Skip to content

Commit

Permalink
Add a biome selector to the map generation dialog (#3085)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackYps committed Jan 4, 2024
1 parent 2b15d9b commit 5067d5e
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 28 deletions.
Expand Up @@ -409,8 +409,9 @@ public void onRandomMapButtonClicked() {
public void onGenerateMapButtonClicked() {
GenerateMapController generateMapController = uiService.loadFxml("theme/play/generate_map.fxml");
mapGeneratorService.getNewestGenerator()
.thenCompose(aVoid -> mapGeneratorService.getGeneratorStyles())
.thenAccept(generateMapController::setStyles)
.thenCompose(aVoid -> CompletableFuture.allOf(
mapGeneratorService.getGeneratorStyles().thenAccept(generateMapController::setStyles),
mapGeneratorService.getGeneratorBiomes().thenAccept(generateMapController::setBiomes)))
.thenRunAsync(() -> {
Pane root = generateMapController.getRoot();
generateMapController.setCreateGameController(this);
Expand Down
40 changes: 36 additions & 4 deletions src/main/java/com/faforever/client/game/GenerateMapController.java
Expand Up @@ -39,6 +39,7 @@
import java.security.InvalidParameterException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -68,6 +69,8 @@ public class GenerateMapController extends NodeController<Pane> {
public ComboBox<GenerationType> generationTypeComboBox;
public Label mapStyleLabel;
public ComboBox<String> mapStyleComboBox;
public Label biomeLabel;
public ComboBox<String> biomeComboBox;
public Spinner<Integer> spawnCountSpinner;
public Spinner<Double> mapSizeSpinner;
public Slider waterSlider;
Expand Down Expand Up @@ -108,10 +111,11 @@ public class GenerateMapController extends NodeController<Pane> {

@Override
protected void onInitialize() {
JavaFxUtil.bindManagedToVisible(commandLineLabel, commandLineArgsText, mapStyleComboBox, mapStyleLabel);
JavaFxUtil.bindManagedToVisible(commandLineLabel, commandLineArgsText, mapStyleComboBox, mapStyleLabel, biomeComboBox, biomeLabel);
initCommandlineArgs();
initGenerationTypeComboBox();
initMapStyleComboBox();
initBiomeComboBox();
initNumTeamsSpinner();
initSpawnCountSpinner();
initMapSizeSpinner();
Expand Down Expand Up @@ -236,7 +240,18 @@ private void initMapStyleComboBox() {
mapStyleComboBox.disableProperty()
.bind(previousMapName.textProperty().isNotEmpty()
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
.or(commandLineArgsText.textProperty().isNotEmpty()));
.or(commandLineArgsText.textProperty().isNotEmpty())
.or(biomeComboBox.valueProperty().isNotNull()
.and(biomeComboBox.valueProperty().isNotEqualTo(MapGeneratorService.GENERATOR_RANDOM_BIOME))));
}

private void initBiomeComboBox() {
biomeComboBox.disableProperty()
.bind(previousMapName.textProperty().isNotEmpty()
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
.or(commandLineArgsText.textProperty().isNotEmpty())
.or(mapStyleComboBox.valueProperty().isNotNull()
.and(mapStyleComboBox.valueProperty().isNotEqualTo(MapGeneratorService.GENERATOR_RANDOM_STYLE))));
}

private void initOptionSlider(IntegerProperty valueProperty, BooleanProperty randomProperty, Slider slider,
Expand Down Expand Up @@ -278,6 +293,7 @@ private GeneratorOptions getGeneratorOptions() {
optionsBuilder.numTeams(numTeamsSpinner.getValue());
optionsBuilder.generationType(generationTypeComboBox.getValue());
optionsBuilder.style(mapStyleComboBox.getValue());
optionsBuilder.biome(biomeComboBox.getValue());
getSliderValue(waterSlider, waterRandom).ifPresent(value -> optionsBuilder.landDensity(1 - value));
getSliderValue(plateauSlider, plateauRandom).ifPresent(optionsBuilder::plateauDensity);
getSliderValue(mountainSlider, mountainRandom).ifPresent(optionsBuilder::mountainDensity);
Expand Down Expand Up @@ -345,8 +361,9 @@ protected void setCreateGameController(CreateGameController controller) {
}

protected void setStyles(List<String> styles) {
styles.add(0, MapGeneratorService.GENERATOR_RANDOM_STYLE);
mapStyleComboBox.setItems(FXCollections.observableList(styles));
ArrayList<String> styleList = new ArrayList<>(List.of(MapGeneratorService.GENERATOR_RANDOM_STYLE));
styleList.addAll(styles);
mapStyleComboBox.setItems(FXCollections.observableList(styleList));
String mapStyle = generatorPrefs.getMapStyle();
if (mapStyleComboBox.getItems().contains(mapStyle)) {
mapStyleComboBox.getSelectionModel().select(mapStyle);
Expand All @@ -358,6 +375,21 @@ protected void setStyles(List<String> styles) {
mapStyleLabel.setVisible(true);
}

protected void setBiomes(List<String> biomes) {
ArrayList<String> biomeList = new ArrayList<>(List.of(MapGeneratorService.GENERATOR_RANDOM_BIOME));
biomeList.addAll(biomes);
biomeComboBox.setItems(FXCollections.observableList(biomeList));
String biome = generatorPrefs.getBiome();
if (biomeComboBox.getItems().contains(biome)) {
biomeComboBox.getSelectionModel().select(biome);
} else {
biomeComboBox.getSelectionModel().select(MapGeneratorService.GENERATOR_RANDOM_BIOME);
}
generatorPrefs.biomeProperty().bind(biomeComboBox.valueProperty());
biomeComboBox.setVisible(true);
biomeLabel.setVisible(true);
}

public void onNewLabelClicked(MouseEvent mouseEvent) {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY) && mouseEvent.getClickCount() == 2) {
toggleCommandlineInput();
Expand Down
Expand Up @@ -48,6 +48,7 @@ public class MapGeneratorService implements DisposableBean {
public static final String GENERATOR_EXECUTABLE_SUB_DIRECTORY = "map_generator";
public static final int GENERATION_TIMEOUT_SECONDS = 60 * 3;
public static final String GENERATOR_RANDOM_STYLE = "RANDOM";
public static final String GENERATOR_RANDOM_BIOME = "RANDOM";
private static final Pattern VERSION_PATTERN = Pattern.compile("\\d\\d?\\d?\\.\\d\\d?\\d?\\.\\d\\d?\\d?");
protected static final Pattern GENERATED_MAP_PATTERN = Pattern.compile("neroxis_map_generator_(" + VERSION_PATTERN + ")_(.*)");

Expand Down Expand Up @@ -189,6 +190,16 @@ public CompletableFuture<List<String>> getGeneratorStyles() {
return taskService.submitTask(generatorOptionsTask).getFuture();
}

public CompletableFuture<List<String>> getGeneratorBiomes() {
Assert.checkNullIllegalState(defaultGeneratorVersion, "Generator version not set");
GeneratorOptionsTask generatorOptionsTask = generatorOptionsTaskFactory.getObject();
Path generatorExecutablePath = getGeneratorExecutablePath(defaultGeneratorVersion);
generatorOptionsTask.setVersion(defaultGeneratorVersion);
generatorOptionsTask.setQuery("--biomes");
generatorOptionsTask.setGeneratorExecutableFile(generatorExecutablePath);
return taskService.submitTask(generatorOptionsTask).getFuture();
}

@NotNull
public Path getGeneratorExecutablePath(ComparableVersion defaultGeneratorVersion) {
return dataPrefs.getMapGeneratorDirectory()
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/faforever/client/preferences/GeneratorPrefs.java
Expand Up @@ -20,6 +20,7 @@ public class GeneratorPrefs {
private final IntegerProperty numTeams = new SimpleIntegerProperty(2);
private final DoubleProperty mapSizeInKm = new SimpleDoubleProperty(10);
private final StringProperty mapStyle = new SimpleStringProperty("");
private final StringProperty biome = new SimpleStringProperty("");
private final IntegerProperty waterDensity = new SimpleIntegerProperty(0);
private final BooleanProperty waterRandom = new SimpleBooleanProperty(true);
private final IntegerProperty plateauDensity = new SimpleIntegerProperty(0);
Expand Down Expand Up @@ -93,6 +94,18 @@ public StringProperty mapStyleProperty() {
return mapStyle;
}

public String getBiome() {
return biome.get();
}

public void setBiome(String biome) {
this.biome.set(biome);
}

public StringProperty biomeProperty() {
return biome;
}

public GenerationType getGenerationType() {
return generationType.get();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/i18n/messages.properties
Expand Up @@ -1054,6 +1054,7 @@ management.maps.officialMaps = Official maps
management.maps.uninstall.error = cannot uninstall the map
game.generateMap.commandLine = Command Arguments
game.generateMap.style = Map Style
game.generateMap.biome = Biome
game.mapGeneration.options.title = Getting map options (Version\: {0})
game.replayNotAvailable = Replay file not available at this time
game.filter.numberOfPlayers = Number of players
Expand Down
44 changes: 24 additions & 20 deletions src/main/resources/theme/play/generate_map.fxml
Expand Up @@ -89,69 +89,73 @@
GridPane.rowIndex="9"/>
<ComboBox fx:id="mapStyleComboBox" maxHeight="Infinity" visible="false" GridPane.columnIndex="1"
GridPane.columnSpan="2" GridPane.rowIndex="9"/>
<Label text="%game.generateMap.waterSlider" GridPane.columnIndex="0" GridPane.rowIndex="10"/>
<HBox fx:id="waterRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="10">
<Label fx:id="biomeLabel" text="%game.generateMap.biome" visible="false" GridPane.columnIndex="0"
GridPane.rowIndex="10"/>
<ComboBox fx:id="biomeComboBox" maxHeight="Infinity" visible="false" GridPane.columnIndex="1"
GridPane.columnSpan="2" GridPane.rowIndex="10"/>
<Label text="%game.generateMap.waterSlider" GridPane.columnIndex="0" GridPane.rowIndex="11"/>
<HBox fx:id="waterRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="11">
<Label text="%game.generateMap.random"/>
<CheckBox fx:id="waterRandom"/>
</HBox>
<HBox fx:id="waterSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2" GridPane.rowIndex="10">
<HBox fx:id="waterSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2" GridPane.rowIndex="11">
<Label text="%game.generateMap.less"/>
<Slider fx:id="waterSlider" majorTickUnit="1" max="127" maxHeight="Infinity" snapToTicks="true"
HBox.hgrow="ALWAYS"/>
<Label text="%game.generateMap.more"/>
</HBox>
<Label text="%game.generateMap.plateauSlider" GridPane.columnIndex="0" GridPane.rowIndex="11"/>
<HBox fx:id="plateauRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="11">
<Label text="%game.generateMap.plateauSlider" GridPane.columnIndex="0" GridPane.rowIndex="12"/>
<HBox fx:id="plateauRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="12">
<Label text="%game.generateMap.random"/>
<CheckBox fx:id="plateauRandom"/>
</HBox>
<HBox fx:id="plateauSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2" GridPane.rowIndex="11">
<HBox fx:id="plateauSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2" GridPane.rowIndex="12">
<Label text="%game.generateMap.less"/>
<Slider fx:id="plateauSlider" majorTickUnit="1" max="127" maxHeight="Infinity" snapToTicks="true"
HBox.hgrow="ALWAYS"/>
<Label text="%game.generateMap.more"/>
</HBox>
<Label text="%game.generateMap.mountainSlider" GridPane.columnIndex="0" GridPane.rowIndex="12"/>
<Label text="%game.generateMap.mountainSlider" GridPane.columnIndex="0" GridPane.rowIndex="13"/>
<HBox fx:id="mountainRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1"
GridPane.rowIndex="12">
GridPane.rowIndex="13">
<Label text="%game.generateMap.random"/>
<CheckBox fx:id="mountainRandom"/>
</HBox>
<HBox fx:id="mountainSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2"
GridPane.rowIndex="12">
GridPane.rowIndex="13">
<Label text="%game.generateMap.less"/>
<Slider fx:id="mountainSlider" majorTickUnit="1" max="127" maxHeight="Infinity" snapToTicks="true"
HBox.hgrow="ALWAYS"/>
<Label text="%game.generateMap.more"/>
</HBox>
<Label text="%game.generateMap.rampSlider" GridPane.columnIndex="0" GridPane.rowIndex="13"/>
<HBox fx:id="rampRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="13">
<Label text="%game.generateMap.rampSlider" GridPane.columnIndex="0" GridPane.rowIndex="14"/>
<HBox fx:id="rampRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="14">
<Label text="%game.generateMap.random"/>
<CheckBox fx:id="rampRandom"/>
</HBox>
<HBox fx:id="rampSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2" GridPane.rowIndex="13">
<HBox fx:id="rampSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2" GridPane.rowIndex="14">
<Label text="%game.generateMap.less"/>
<Slider fx:id="rampSlider" majorTickUnit="1" max="127" maxHeight="Infinity" snapToTicks="true"
HBox.hgrow="ALWAYS"/>
<Label text="%game.generateMap.more"/>
</HBox>
<Label text="%game.generateMap.mexSlider" GridPane.columnIndex="0" GridPane.rowIndex="14"/>
<HBox fx:id="mexRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="14">
<Label text="%game.generateMap.mexSlider" GridPane.columnIndex="0" GridPane.rowIndex="15"/>
<HBox fx:id="mexRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="15">
<Label text="%game.generateMap.random"/>
<CheckBox fx:id="mexRandom"/>
</HBox>
<HBox fx:id="mexSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2" GridPane.rowIndex="14">
<HBox fx:id="mexSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2" GridPane.rowIndex="15">
<Label text="%game.generateMap.less"/>
<Slider fx:id="mexSlider" majorTickUnit="1" max="127" maxHeight="Infinity" snapToTicks="true"
HBox.hgrow="ALWAYS"/>
<Label text="%game.generateMap.more"/>
</HBox>
<Label text="%game.generateMap.reclaimSlider" GridPane.columnIndex="0" GridPane.rowIndex="15"/>
<HBox fx:id="reclaimRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="15">
<Label text="%game.generateMap.reclaimSlider" GridPane.columnIndex="0" GridPane.rowIndex="16"/>
<HBox fx:id="reclaimRandomBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="16">
<Label text="%game.generateMap.random"/>
<CheckBox fx:id="reclaimRandom"/>
</HBox>
<HBox fx:id="reclaimSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2" GridPane.rowIndex="15">
<HBox fx:id="reclaimSliderBox" alignment="CENTER_LEFT" spacing="10" GridPane.columnIndex="2" GridPane.rowIndex="16">
<Label text="%game.generateMap.less"/>
<Slider fx:id="reclaimSlider" majorTickUnit="1" max="127" maxHeight="Infinity" snapToTicks="true"
HBox.hgrow="ALWAYS"/>
Expand All @@ -161,11 +165,11 @@

<Button fx:id="generateMapButton" contentDisplay="RIGHT" defaultButton="true" mnemonicParsing="false"
onAction="#onGenerateMapButtonClicked" styleClass="game-generate-map" text="%game.generateMap"
GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.rowIndex="16">
GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.rowIndex="17">
</Button>

<!-- Cancel, Ok -->
<Button mnemonicParsing="false" onAction="#onCloseButtonClicked" text="%cancel" GridPane.columnIndex="0"
GridPane.halignment="LEFT" GridPane.rowIndex="16">
GridPane.halignment="LEFT" GridPane.rowIndex="17">
</Button>
</GridPane>
Expand Up @@ -524,14 +524,17 @@ public void testCreateGameTitleTextBorderColor() {

@Test
public void testOnGenerateMapClicked() {
when(mapGeneratorService.getNewestGenerator()).thenReturn(CompletableFuture.completedFuture(null));
when(mapGeneratorService.getGeneratorStyles()).thenReturn(CompletableFuture.completedFuture(List.of()));
when(mapGeneratorService.getNewestGenerator()).thenReturn(completedFuture(null));
when(mapGeneratorService.getGeneratorStyles()).thenReturn(completedFuture(List.of()));
when(mapGeneratorService.getGeneratorBiomes()).thenReturn(completedFuture(List.of()));

runOnFxThreadAndWait(() -> instance.onGenerateMapButtonClicked());

verify(mapGeneratorService).getNewestGenerator();
verify(mapGeneratorService).getGeneratorStyles();
verify(mapGeneratorService).getGeneratorBiomes();
verify(generateMapController).setStyles(any());
verify(generateMapController).setBiomes(any());
verify(generateMapController).setOnCloseButtonClickedListener(any());
}

Expand Down

0 comments on commit 5067d5e

Please sign in to comment.