Skip to content

Commit 339162e

Browse files
authored
Add densities to mapgen options (#3211)
* Add densities to mapgen options * Fix plurality
1 parent 98cf376 commit 339162e

File tree

11 files changed

+352
-196
lines changed

11 files changed

+352
-196
lines changed

src/main/java/com/faforever/client/game/GenerateMapController.java

Lines changed: 106 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.faforever.client.fx.JavaFxUtil;
44
import com.faforever.client.fx.NodeController;
5+
import com.faforever.client.fx.ToStringOnlyConverter;
56
import com.faforever.client.i18n.I18n;
67
import com.faforever.client.map.generator.GenerationType;
78
import com.faforever.client.map.generator.GeneratorOptions;
@@ -14,6 +15,7 @@
1415
import javafx.collections.FXCollections;
1516
import javafx.collections.ObservableList;
1617
import javafx.collections.transformation.FilteredList;
18+
import javafx.scene.Node;
1719
import javafx.scene.control.Button;
1820
import javafx.scene.control.CheckBox;
1921
import javafx.scene.control.ComboBox;
@@ -28,6 +30,7 @@
2830
import javafx.util.StringConverter;
2931
import lombok.RequiredArgsConstructor;
3032
import lombok.extern.slf4j.Slf4j;
33+
import org.controlsfx.control.RangeSlider;
3134
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3235
import org.springframework.context.annotation.Scope;
3336
import org.springframework.stereotype.Component;
@@ -63,55 +66,44 @@ public class GenerateMapController extends NodeController<Pane> {
6366
public Label commandLineLabel;
6467
public TextField commandLineArgsText;
6568
public ComboBox<GenerationType> generationTypeComboBox;
66-
public Label mapStyleLabel;
6769
public ComboBox<String> mapStyleComboBox;
68-
public Label biomeLabel;
6970
public ComboBox<String> biomeComboBox;
7071
public Spinner<Integer> spawnCountSpinner;
7172
public Spinner<Double> mapSizeSpinner;
72-
public Label symmetryLabel;
7373
public ComboBox<String> symmetryComboBox;
74-
public Label customStyleLabel;
7574
public CheckBox customStyleCheckBox;
76-
public Label fixedSeedLabel;
7775
public CheckBox fixedSeedCheckBox;
7876
public TextField seedTextField;
7977
public Button seedRerollButton;
80-
public Label terrainLabel;
8178
public ComboBox<String> terrainComboBox;
82-
public Label resourceLabel;
8379
public ComboBox<String> resourcesComboBox;
84-
public Label propLabel;
8580
public ComboBox<String> propsComboBox;
81+
public RangeSlider reclaimDensitySlider;
82+
public RangeSlider resourcesDensitySlider;
8683

8784
private Runnable onCloseButtonClickedListener;
88-
private final ObservableList<Integer> validTeamSizes = FXCollections.observableList(IntStream.range(0, 17)
89-
.filter(value -> value != 1)
90-
.boxed()
91-
.collect(Collectors.toList()));
85+
private final ObservableList<Integer> validTeamSizes = FXCollections.observableList(
86+
IntStream.range(0, 17).filter(value -> value != 1).boxed().collect(Collectors.toList()));
9287
private final FilteredList<Integer> selectableTeamSizes = new FilteredList<>(validTeamSizes);
93-
private final ObservableList<Integer> validSpawnCount = FXCollections.observableList(IntStream.range(2, 17)
94-
.boxed()
95-
.collect(Collectors.toList()));
88+
private final ObservableList<Integer> validSpawnCount = FXCollections.observableList(
89+
IntStream.range(2, 17).boxed().collect(Collectors.toList()));
9690
private final FilteredList<Integer> selectableSpawnCounts = new FilteredList<>(validSpawnCount);
9791
public Spinner<Integer> numTeamsSpinner;
9892

9993
@Override
10094
protected void onInitialize() {
101-
JavaFxUtil.bindManagedToVisible(commandLineLabel, commandLineArgsText, mapStyleComboBox, mapStyleLabel, biomeComboBox, biomeLabel);
95+
JavaFxUtil.bindManagedToVisible(commandLineLabel, commandLineArgsText);
10296
initCommandlineArgs();
10397
initGenerationTypeComboBox();
10498
initSymmetryComboBox();
10599
initMapStyleComboBox();
106-
initCheckBoxes();
100+
initCustomStyleOptions();
107101
initNumTeamsSpinner();
108102
initSpawnCountSpinner();
109103
initMapSizeSpinner();
110104
initSeedField();
111-
initGeneratorComboBox(terrainComboBox);
112-
initGeneratorComboBox(biomeComboBox);
113-
initGeneratorComboBox(resourcesComboBox);
114-
initGeneratorComboBox(propsComboBox);
105+
bindCustomStyleDisabledProperty(terrainComboBox, biomeComboBox, resourcesComboBox, propsComboBox,
106+
resourcesDensitySlider, reclaimDensitySlider);
115107
}
116108

117109
private StringConverter<GenerationType> getGenerationTypeConverter() {
@@ -164,8 +156,9 @@ private void initGenerationTypeComboBox() {
164156
generationTypeComboBox.setValue(generationType);
165157
generatorPrefs.generationTypeProperty().bind(generationTypeComboBox.valueProperty());
166158
generationTypeComboBox.disableProperty()
167-
.bind(previousMapName.textProperty().isNotEmpty()
168-
.or(commandLineArgsText.textProperty().isNotEmpty()));
159+
.bind(previousMapName.textProperty()
160+
.isNotEmpty()
161+
.or(commandLineArgsText.textProperty().isNotEmpty()));
169162
}
170163

171164
private void initNumTeamsSpinner() {
@@ -192,8 +185,8 @@ private void initNumTeamsSpinner() {
192185
});
193186
generatorPrefs.numTeamsProperty().bind(numTeamsSpinner.valueProperty());
194187
numTeamsSpinner.disableProperty()
195-
.bind(previousMapName.textProperty().isNotEmpty()
196-
.or(commandLineArgsText.textProperty().isNotEmpty()));
188+
.bind(
189+
previousMapName.textProperty().isNotEmpty().or(commandLineArgsText.textProperty().isNotEmpty()));
197190
int lastIndex = selectableTeamSizes.indexOf(numTeamsProperty);
198191
numTeamsSpinner.increment(lastIndex >= 0 ? lastIndex : 1);
199192
}
@@ -207,8 +200,9 @@ private void initSpawnCountSpinner() {
207200
spawnCountSpinner.setValueFactory(new ListSpinnerValueFactory<>(selectableSpawnCounts));
208201
generatorPrefs.spawnCountProperty().bind(spawnCountSpinner.valueProperty());
209202
spawnCountSpinner.disableProperty()
210-
.bind(previousMapName.textProperty().isNotEmpty()
211-
.or(commandLineArgsText.textProperty().isNotEmpty()));
203+
.bind(previousMapName.textProperty()
204+
.isNotEmpty()
205+
.or(commandLineArgsText.textProperty().isNotEmpty()));
212206
int lastIndex = selectableSpawnCounts.indexOf(spawnCountProperty);
213207
spawnCountSpinner.increment(Math.max(lastIndex, 0));
214208
}
@@ -219,61 +213,85 @@ private void initMapSizeSpinner() {
219213
mapSizeSpinner.getValueFactory().setConverter(getMapSizeConverter());
220214
generatorPrefs.mapSizeInKmProperty().bind(mapSizeSpinner.getValueFactory().valueProperty());
221215
mapSizeSpinner.disableProperty()
222-
.bind(previousMapName.textProperty().isNotEmpty()
223-
.or(commandLineArgsText.textProperty().isNotEmpty()));
216+
.bind(
217+
previousMapName.textProperty().isNotEmpty().or(commandLineArgsText.textProperty().isNotEmpty()));
224218
}
225219

226220
private void initSymmetryComboBox() {
227221
symmetryComboBox.disableProperty()
228-
.bind(previousMapName.textProperty().isNotEmpty()
229-
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
230-
.or(commandLineArgsText.textProperty().isNotEmpty()));
222+
.bind(previousMapName.textProperty()
223+
.isNotEmpty()
224+
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
225+
.or(commandLineArgsText.textProperty().isNotEmpty()));
231226
}
232227

233228
private void initMapStyleComboBox() {
234229
mapStyleComboBox.disableProperty()
235-
.bind(previousMapName.textProperty().isNotEmpty()
236-
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
237-
.or(commandLineArgsText.textProperty().isNotEmpty())
238-
.or(customStyleCheckBox.selectedProperty()));
230+
.bind(previousMapName.textProperty()
231+
.isNotEmpty()
232+
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
233+
.or(commandLineArgsText.textProperty().isNotEmpty())
234+
.or(customStyleCheckBox.selectedProperty()));
239235
}
240236

241-
private void initCheckBoxes() {
237+
private void initCustomStyleOptions() {
242238
customStyleCheckBox.setSelected(generatorPrefs.getCustomStyle());
243239
generatorPrefs.customStyleProperty().bind(customStyleCheckBox.selectedProperty());
244240
customStyleCheckBox.disableProperty()
245-
.bind(previousMapName.textProperty().isNotEmpty()
246-
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
247-
.or(commandLineArgsText.textProperty().isNotEmpty()));
241+
.bind(previousMapName.textProperty()
242+
.isNotEmpty()
243+
.or(generationTypeComboBox.valueProperty()
244+
.isNotEqualTo(GenerationType.CASUAL))
245+
.or(commandLineArgsText.textProperty().isNotEmpty()));
248246
fixedSeedCheckBox.setSelected(generatorPrefs.getFixedSeed());
249247
generatorPrefs.fixedSeedProperty().bind(fixedSeedCheckBox.selectedProperty());
250248
fixedSeedCheckBox.disableProperty()
251-
.bind(previousMapName.textProperty().isNotEmpty()
252-
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
253-
.or(commandLineArgsText.textProperty().isNotEmpty()));
249+
.bind(previousMapName.textProperty()
250+
.isNotEmpty()
251+
.or(generationTypeComboBox.valueProperty()
252+
.isNotEqualTo(GenerationType.CASUAL))
253+
.or(commandLineArgsText.textProperty().isNotEmpty()));
254+
255+
reclaimDensitySlider.setLabelFormatter(
256+
new LessMoreStringConverter(reclaimDensitySlider.getMin(), reclaimDensitySlider.getMax()));
257+
resourcesDensitySlider.setLabelFormatter(
258+
new LessMoreStringConverter(resourcesDensitySlider.getMin(), resourcesDensitySlider.getMax()));
259+
reclaimDensitySlider.setHighValue(generatorPrefs.getReclaimDensityMax());
260+
generatorPrefs.reclaimDensityMaxProperty().bind(reclaimDensitySlider.highValueProperty());
261+
reclaimDensitySlider.setLowValue(generatorPrefs.getReclaimDensityMin());
262+
generatorPrefs.reclaimDensityMinProperty().bind(reclaimDensitySlider.lowValueProperty());
263+
resourcesDensitySlider.setHighValue(generatorPrefs.getResourceDensityMax());
264+
generatorPrefs.resourceDensityMaxProperty().bind(resourcesDensitySlider.highValueProperty());
265+
resourcesDensitySlider.setLowValue(generatorPrefs.getResourceDensityMin());
266+
generatorPrefs.resourceDensityMinProperty().bind(resourcesDensitySlider.lowValueProperty());
254267
}
255268

256269
private void initSeedField() {
257270
seedTextField.setText(String.valueOf(generatorPrefs.getSeed()));
258271
generatorPrefs.seedProperty().bind(seedTextField.textProperty());
259272
seedTextField.disableProperty()
260-
.bind(previousMapName.textProperty().isNotEmpty()
261-
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
262-
.or(commandLineArgsText.textProperty().isNotEmpty())
263-
.or(fixedSeedCheckBox.selectedProperty().not()));
273+
.bind(previousMapName.textProperty()
274+
.isNotEmpty()
275+
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
276+
.or(commandLineArgsText.textProperty().isNotEmpty())
277+
.or(fixedSeedCheckBox.selectedProperty().not()));
264278
seedRerollButton.disableProperty()
265-
.bind(previousMapName.textProperty().isNotEmpty()
266-
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
267-
.or(commandLineArgsText.textProperty().isNotEmpty())
268-
.or(fixedSeedCheckBox.selectedProperty().not()));
279+
.bind(previousMapName.textProperty()
280+
.isNotEmpty()
281+
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
282+
.or(commandLineArgsText.textProperty().isNotEmpty())
283+
.or(fixedSeedCheckBox.selectedProperty().not()));
269284
}
270285

271-
private void initGeneratorComboBox(ComboBox<String> comboBox) {
272-
comboBox.disableProperty()
273-
.bind(previousMapName.textProperty().isNotEmpty()
274-
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
275-
.or(commandLineArgsText.textProperty().isNotEmpty())
276-
.or(customStyleCheckBox.selectedProperty().not()));
286+
private void bindCustomStyleDisabledProperty(Node... nodes) {
287+
for (Node node : nodes) {
288+
node.disableProperty()
289+
.bind(previousMapName.textProperty()
290+
.isNotEmpty()
291+
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
292+
.or(commandLineArgsText.textProperty().isNotEmpty())
293+
.or(customStyleCheckBox.selectedProperty().not()));
294+
}
277295
}
278296

279297
private GeneratorOptions getGeneratorOptions() {
@@ -296,6 +314,21 @@ private GeneratorOptions getGeneratorOptions() {
296314
optionsBuilder.textureStyle(biomeComboBox.getValue());
297315
optionsBuilder.resourceStyle(resourcesComboBox.getValue());
298316
optionsBuilder.propStyle(propsComboBox.getValue());
317+
Random random = new Random();
318+
int reclaimLowValue = (int) reclaimDensitySlider.getLowValue();
319+
int reclaimHighValue = (int) reclaimDensitySlider.getHighValue();
320+
if (reclaimLowValue == reclaimHighValue) {
321+
optionsBuilder.reclaimDensity(reclaimLowValue / 127f);
322+
} else {
323+
optionsBuilder.reclaimDensity(random.nextInt(reclaimLowValue, reclaimHighValue) / 127f);
324+
}
325+
int resourcesLowValue = (int) resourcesDensitySlider.getLowValue();
326+
int resourcesHighValue = (int) resourcesDensitySlider.getHighValue();
327+
if (resourcesLowValue == resourcesHighValue) {
328+
optionsBuilder.resourceDensity(resourcesLowValue / 127f);
329+
} else {
330+
optionsBuilder.resourceDensity(random.nextInt(resourcesLowValue, resourcesHighValue) / 127f);
331+
}
299332
} else {
300333
optionsBuilder.style(mapStyleComboBox.getValue());
301334
}
@@ -458,4 +491,20 @@ void setOnCloseButtonClickedListener(Runnable onCloseButtonClickedListener) {
458491
public void onSeedRerollButtonClicked() {
459492
seedTextField.setText(String.valueOf(new Random().nextLong()));
460493
}
494+
495+
private class LessMoreStringConverter extends ToStringOnlyConverter<Number> {
496+
public LessMoreStringConverter(Number min, Number max) {
497+
super(number -> {
498+
if (number.equals(max)) {
499+
return i18n.get("more");
500+
}
501+
502+
if (number.equals(min)) {
503+
return i18n.get("less");
504+
}
505+
506+
return "";
507+
});
508+
}
509+
}
461510
}

src/main/java/com/faforever/client/map/generator/GenerateMapTask.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class GenerateMapTask extends CompletableTask<String> {
3737
private Path generatorExecutableFile;
3838
private ComparableVersion version;
3939
private GeneratorOptions generatorOptions;
40-
private String seed;
4140
private String mapName;
4241

4342
@Autowired
@@ -57,10 +56,12 @@ protected String call() throws Exception {
5756
updateTitle(i18n.get("game.mapGeneration.generateMap.title", version));
5857

5958
GeneratorCommand.GeneratorCommandBuilder generatorCommandBuilder = GeneratorCommand.builder()
60-
.version(version)
61-
.generatorExecutableFile(generatorExecutableFile)
62-
.javaExecutable(operatingSystem.getJavaExecutablePath())
63-
.mapName(mapName);
59+
.version(version)
60+
.generatorExecutableFile(
61+
generatorExecutableFile)
62+
.javaExecutable(
63+
operatingSystem.getJavaExecutablePath())
64+
.mapName(mapName);
6465

6566
if (generatorOptions != null) {
6667
generatorCommandBuilder.spawnCount(generatorOptions.spawnCount())
@@ -74,6 +75,8 @@ protected String call() throws Exception {
7475
.textureStyle(generatorOptions.textureStyle())
7576
.resourceStyle(generatorOptions.resourceStyle())
7677
.propStyle(generatorOptions.propStyle())
78+
.resourceDensity(generatorOptions.resourceDensity())
79+
.reclaimDensity(generatorOptions.reclaimDensity())
7780
.commandLineArgs(generatorOptions.commandLineArgs());
7881
}
7982

@@ -86,8 +89,8 @@ protected String call() throws Exception {
8689
processBuilder.directory(workingDirectory.toFile());
8790
processBuilder.command(command);
8891

89-
log.info("Starting map generator in directory: `{}` with command: `{}`",
90-
processBuilder.directory(), String.join(" ", processBuilder.command()));
92+
log.info("Starting map generator in directory: `{}` with command: `{}`", processBuilder.directory(),
93+
String.join(" ", processBuilder.command()));
9194

9295
Process process = processBuilder.start();
9396
OsUtils.gobbleLines(process.getInputStream(), msg -> {
@@ -106,7 +109,8 @@ protected String call() throws Exception {
106109
"--visualize")) {
107110
log.warn("Map generation timed out, killing process");
108111
process.destroyForcibly();
109-
notificationService.addImmediateErrorNotification(new RuntimeException("Map generation timed out"), "game.mapGeneration.failed.message");
112+
notificationService.addImmediateErrorNotification(new RuntimeException("Map generation timed out"),
113+
"game.mapGeneration.failed.message");
110114
}
111115
} catch (Exception e) {
112116
log.error("Could not start map generator", e);

src/main/java/com/faforever/client/map/generator/GeneratorCommand.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public record GeneratorCommand(
2525
String terrainStyle,
2626
String textureStyle,
2727
String resourceStyle,
28-
String propStyle,
28+
String propStyle, Float reclaimDensity, Float resourceDensity,
2929
String commandLineArgs
3030
) {
3131

@@ -93,6 +93,14 @@ public List<String> getCommand() {
9393
command.addAll(Arrays.asList("--prop-style", propStyle));
9494
}
9595

96+
if (resourceDensity != null) {
97+
command.addAll(Arrays.asList("--resource-density", String.valueOf(resourceDensity)));
98+
}
99+
100+
if (reclaimDensity != null) {
101+
command.addAll(Arrays.asList("--reclaim-density", String.valueOf(reclaimDensity)));
102+
}
103+
96104
return command;
97105
} else {
98106
return Arrays.asList(javaPath, "-jar", generatorExecutableFile.toAbsolutePath().toString(), ".",

src/main/java/com/faforever/client/map/generator/GeneratorOptions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public record GeneratorOptions(
1313
String style,
1414
String terrainStyle,
1515
String textureStyle,
16-
String resourceStyle,
17-
String propStyle,
16+
String resourceStyle, String propStyle, Float reclaimDensity, Float resourceDensity,
1817
String commandLineArgs
1918
) {}

src/main/java/com/faforever/client/map/generator/MapGeneratorService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ public Mono<String> generateMap(String mapName) {
122122

123123
GenerateMapTask generateMapTask = generateMapTaskFactory.getObject();
124124
generateMapTask.setVersion(version);
125-
generateMapTask.setSeed(seed);
126125
generateMapTask.setMapName(mapName);
127126
generateMapTask.setGeneratorExecutableFile(generatorExecutablePath);
128127

0 commit comments

Comments
 (0)