Skip to content

Commit

Permalink
Add densities to mapgen options (#3211)
Browse files Browse the repository at this point in the history
* Add densities to mapgen options

* Fix plurality
  • Loading branch information
Sheikah45 authored Jun 19, 2024
1 parent 98cf376 commit 339162e
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 196 deletions.
163 changes: 106 additions & 57 deletions src/main/java/com/faforever/client/game/GenerateMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -63,55 +66,44 @@ public class GenerateMapController extends NodeController<Pane> {
public Label commandLineLabel;
public TextField commandLineArgsText;
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 Label symmetryLabel;
public ComboBox<String> symmetryComboBox;
public Label customStyleLabel;
public CheckBox customStyleCheckBox;
public Label fixedSeedLabel;
public CheckBox fixedSeedCheckBox;
public TextField seedTextField;
public Button seedRerollButton;
public Label terrainLabel;
public ComboBox<String> terrainComboBox;
public Label resourceLabel;
public ComboBox<String> resourcesComboBox;
public Label propLabel;
public ComboBox<String> propsComboBox;
public RangeSlider reclaimDensitySlider;
public RangeSlider resourcesDensitySlider;

private Runnable onCloseButtonClickedListener;
private final ObservableList<Integer> validTeamSizes = FXCollections.observableList(IntStream.range(0, 17)
.filter(value -> value != 1)
.boxed()
.collect(Collectors.toList()));
private final ObservableList<Integer> validTeamSizes = FXCollections.observableList(
IntStream.range(0, 17).filter(value -> value != 1).boxed().collect(Collectors.toList()));
private final FilteredList<Integer> selectableTeamSizes = new FilteredList<>(validTeamSizes);
private final ObservableList<Integer> validSpawnCount = FXCollections.observableList(IntStream.range(2, 17)
.boxed()
.collect(Collectors.toList()));
private final ObservableList<Integer> validSpawnCount = FXCollections.observableList(
IntStream.range(2, 17).boxed().collect(Collectors.toList()));
private final FilteredList<Integer> selectableSpawnCounts = new FilteredList<>(validSpawnCount);
public Spinner<Integer> 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<GenerationType> getGenerationTypeConverter() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
}
Expand All @@ -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));
}
Expand All @@ -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<String> 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() {
Expand All @@ -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());
}
Expand Down Expand Up @@ -458,4 +491,20 @@ void setOnCloseButtonClickedListener(Runnable onCloseButtonClickedListener) {
public void onSeedRerollButtonClicked() {
seedTextField.setText(String.valueOf(new Random().nextLong()));
}

private class LessMoreStringConverter extends ToStringOnlyConverter<Number> {
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 "";
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class GenerateMapTask extends CompletableTask<String> {
private Path generatorExecutableFile;
private ComparableVersion version;
private GeneratorOptions generatorOptions;
private String seed;
private String mapName;

@Autowired
Expand All @@ -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())
Expand All @@ -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());
}

Expand All @@ -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 -> {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public record GeneratorCommand(
String terrainStyle,
String textureStyle,
String resourceStyle,
String propStyle,
String propStyle, Float reclaimDensity, Float resourceDensity,
String commandLineArgs
) {

Expand Down Expand Up @@ -93,6 +93,14 @@ public List<String> 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(), ".",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public Mono<String> generateMap(String mapName) {

GenerateMapTask generateMapTask = generateMapTaskFactory.getObject();
generateMapTask.setVersion(version);
generateMapTask.setSeed(seed);
generateMapTask.setMapName(mapName);
generateMapTask.setGeneratorExecutableFile(generatorExecutablePath);

Expand Down
Loading

0 comments on commit 339162e

Please sign in to comment.