Skip to content

Commit

Permalink
Simplify sub element generator selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 committed Nov 5, 2023
1 parent a81fe11 commit 684baa1
Show file tree
Hide file tree
Showing 38 changed files with 642 additions and 611 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,41 @@
import lombok.Builder;

@Builder(toBuilder = true)
public record GeneratorParameters(
int spawnCount,
float landDensity,
float plateauDensity,
float mountainDensity,
float rampDensity,
float reclaimDensity,
float mexDensity,
int mapSize,
int numTeams,
Visibility visibility,
Symmetry terrainSymmetry,
Biome biome
) {
public record GeneratorParameters(int spawnCount,
float landDensity,
float plateauDensity,
float mountainDensity,
float rampDensity,
float reclaimDensity,
float mexDensity,
int mapSize,
int numTeams,
Visibility visibility,
Symmetry terrainSymmetry,
Biome biome) {

public String toString() {
if (visibility == null) {
return "Spawns: "
+ spawnCount
+ "\nMap Size: "
+ mapSize
+ "\nNum Teams: "
+ numTeams
+ "\nBiome: "
+ biome.name()
+ "\nLand Density: "
+ landDensity
+ "\nPlateau Density: "
+ plateauDensity
+ "\nMountain Density: "
+ mountainDensity
+ "\nRamp Density: "
+ rampDensity
+ "\nReclaim Density: "
+ reclaimDensity
+ "\nMex Density: "
+ mexDensity
+ "\nTerrain Symmetry: "
+ terrainSymmetry;
return """
Spawns: %d
Map Size: %d
Num Teams: %d
Biome: %s
Land Density: %s
Plateau Density: %s
Mountain Density: %s
Ramp Density: %s
Reclaim Density: %s
Mex Density: %s
Terrain Symmetry: %s
""".formatted(spawnCount, mapSize, numTeams, biome.name(), landDensity, plateauDensity,
mountainDensity, rampDensity, reclaimDensity, mexDensity, terrainSymmetry);
} else {
return "Spawns: " + spawnCount + "\nMap Size: " + mapSize + "\nNum Teams: " + numTeams;
return """
Spawns: %d
Map Size: %d
Num Teams: %d
""".formatted(spawnCount, mapSize, numTeams);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,8 @@
import com.faforever.neroxis.generator.cli.TuningOptions;
import com.faforever.neroxis.generator.cli.VisibilityOptions;
import com.faforever.neroxis.generator.style.BasicStyleGenerator;
import com.faforever.neroxis.generator.style.BigIslandsStyleGenerator;
import com.faforever.neroxis.generator.style.CenterLakeStyleGenerator;
import com.faforever.neroxis.generator.style.DropPlateauStyleGenerator;
import com.faforever.neroxis.generator.style.FloodedStyleGenerator;
import com.faforever.neroxis.generator.style.HighReclaimStyleGenerator;
import com.faforever.neroxis.generator.style.LandBridgeStyleGenerator;
import com.faforever.neroxis.generator.style.LittleMountainStyleGenerator;
import com.faforever.neroxis.generator.style.LowMexStyleGenerator;
import com.faforever.neroxis.generator.style.MountainRangeStyleGenerator;
import com.faforever.neroxis.generator.style.OneIslandStyleGenerator;
import com.faforever.neroxis.generator.style.SmallIslandsStyleGenerator;
import com.faforever.neroxis.generator.style.StyleGenerator;
import com.faforever.neroxis.generator.style.TestStyleGenerator;
import com.faforever.neroxis.generator.style.ValleyStyleGenerator;
import com.faforever.neroxis.generator.util.GeneratorSelector;
import com.faforever.neroxis.generator.util.ConstrainedSelector;
import com.faforever.neroxis.map.DecalGroup;
import com.faforever.neroxis.map.Marker;
import com.faforever.neroxis.map.SCMap;
Expand Down Expand Up @@ -57,6 +44,7 @@
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.faforever.neroxis.map.SCMap.MAP_WIDE_ASSETS_SHADER_NAME;
Expand All @@ -70,19 +58,7 @@
public class MapGenerator implements Callable<Integer> {
public static final int NUM_BINS = 127;
private static final String VERSION = new VersionProvider().getVersion()[0];
private final List<StyleGenerator> mapStyles = List.of(new BigIslandsStyleGenerator(),
new CenterLakeStyleGenerator(), new BasicStyleGenerator(),
new DropPlateauStyleGenerator(),
new LandBridgeStyleGenerator(),
new LittleMountainStyleGenerator(),
new MountainRangeStyleGenerator(),
new OneIslandStyleGenerator(),
new SmallIslandsStyleGenerator(), new ValleyStyleGenerator(),
new HighReclaimStyleGenerator(), new LowMexStyleGenerator(),
new FloodedStyleGenerator(), new TestStyleGenerator());
private final List<StyleGenerator> productionStyles = mapStyles.stream()
.filter(styleGenerator -> !(styleGenerator instanceof TestStyleGenerator))
.toList();

@Spec
private CommandLine.Model.CommandSpec spec;
// Set during generation
Expand Down Expand Up @@ -143,10 +119,7 @@ private void setPreviewFolder(Path previewFolder) throws IOException {
@Command(name = "styles", aliases = {
"--styles"}, description = "Prints the styles available", versionProvider = VersionProvider.class, usageHelpAutoWidth = true)
private void printStyles() {
System.out.println(Arrays.stream(MapStyle.values())
.filter(MapStyle::isProduction)
.map(MapStyle::toString)
.collect(Collectors.joining("\n")));
System.out.println(Arrays.stream(MapStyle.values()).map(MapStyle::toString).collect(Collectors.joining("\n")));
}

@Override
Expand All @@ -161,9 +134,7 @@ public Integer call() throws Exception {
generate();
save();

System.out.printf("Saving map to %s%n", outputFolderMixin.getOutputPath()
.resolve(mapName)
.toAbsolutePath());
System.out.printf("Saving map to %s%n", outputFolderMixin.getOutputPath().resolve(mapName).toAbsolutePath());

Visibility visibility = Optional.ofNullable(tuningOptions.getVisibilityOptions())
.map(VisibilityOptions::getVisibility)
Expand Down Expand Up @@ -203,27 +174,23 @@ private void setStyleAndParameters(GeneratorParameters.GeneratorParametersBuilde
if (tuningOptions.getMapStyle() == null) {
overwriteOptionalGeneratorParametersFromOptions(generatorParametersBuilder);
generatorParameters = generatorParametersBuilder.build();
List<StyleGenerator> productionStyles = Arrays.stream(MapStyle.values())
.filter(MapStyle::isProduction)
.map(MapStyle::getGeneratorClass)
.map(clazz -> {
try {
return clazz.getConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException |
InvocationTargetException |
NoSuchMethodException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList());
List<WeightedOption<StyleGenerator>> generatorOptions = Arrays.stream(MapStyle.values()).map(mapStyle -> {
try {
StyleGenerator styleGenerator = mapStyle.getGeneratorClass().getConstructor().newInstance();
return new WeightedOption<>(styleGenerator, mapStyle.getWeight());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException e) {
throw new RuntimeException(e);
}
}).toList();
Map<Class<? extends StyleGenerator>, MapStyle> styleMap = Arrays.stream(MapStyle.values())
.collect(Collectors.toMap(
MapStyle::getGeneratorClass,
style -> style));
GeneratorOptions<StyleGenerator> styleGeneratorOptions = new GeneratorOptions<>(new BasicStyleGenerator(),
productionStyles);
styleGenerator = GeneratorSelector.selectRandomMatchingGenerator(random, styleGeneratorOptions,
generatorParameters);
Function.identity()));
WeightedConstrainedOptions<StyleGenerator> styleGeneratorOptions = new WeightedConstrainedOptions<>(
new BasicStyleGenerator(), generatorOptions);
styleGenerator = ConstrainedSelector.selectRandomMatchingOption(random, styleGeneratorOptions,
generatorParameters);
tuningOptions.setMapStyle(styleMap.get(styleGenerator.getClass()));
} else {
try {
Expand Down Expand Up @@ -297,8 +264,7 @@ private void checkParameters() {

if (parameterOptions != null &&
parameterOptions.getTerrainSymmetry() != null &&
numTeams % parameterOptions.getTerrainSymmetry()
.getNumSymPoints() != 0) {
numTeams % parameterOptions.getTerrainSymmetry().getNumSymPoints() != 0) {
throw new CommandLine.ParameterException(spec.commandLine(), String.format(
"Terrain symmetry `%s` not compatible with Num Teams `%d`", parameterOptions.getTerrainSymmetry(),
numTeams));
Expand Down Expand Up @@ -455,13 +421,11 @@ private void encodeMapName() {
(byte) MathUtil.binPercentage(generatorParameters.rampDensity(), NUM_BINS),
(byte) MathUtil.binPercentage(generatorParameters.reclaimDensity(), NUM_BINS),
(byte) MathUtil.binPercentage(generatorParameters.mexDensity(), NUM_BINS),
(byte) generatorParameters.terrainSymmetry()
.ordinal()};
(byte) generatorParameters.terrainSymmetry().ordinal()};
} else if (tuningOptions.getVisibilityOptions() != null) {
optionArray = new byte[]{(byte) generatorParameters.spawnCount(),
(byte) (generatorParameters.mapSize() / 64),
(byte) generatorParameters.numTeams(),
(byte) visibility.ordinal()};
(byte) generatorParameters.numTeams(), (byte) visibility.ordinal()};
} else if (parseResult.hasMatchedOption("--style")) {
optionArray = new byte[]{(byte) generatorParameters.spawnCount(),
(byte) (generatorParameters.mapSize() / 64),
Expand All @@ -474,9 +438,8 @@ private void encodeMapName() {
}
String optionString = GeneratedMapNameEncoder.encode(optionArray);
if (visibility != null) {
String timeString = GeneratedMapNameEncoder.encode(ByteBuffer.allocate(8)
.putLong(generationTime)
.array());
String timeString = GeneratedMapNameEncoder.encode(
ByteBuffer.allocate(8).putLong(generationTime).array());
optionString += "_" + timeString;
}
mapName = String.format(mapNameFormat, VERSION, seedString, optionString).toLowerCase();
Expand Down Expand Up @@ -558,19 +521,22 @@ private void generate() {
map.setFilePrefix(mapName);

if (map.getTerrainShaderPath().equals(PBR_SHADER_NAME)) {
map.getBiome().terrainMaterials().getTexturePaths()[8] =
Path.of("/maps", map.getFolderName(), "env", "texture", "heightRoughness.dds")
.toString()
.replace("\\", "/");
map.getBiome().terrainMaterials().getNormalPaths()[8] =
map.getBiome().terrainMaterials().getCubeMaps().get(0).getPath();
map.getBiome().terrainMaterials().getTexturePaths()[8] = Path.of("/maps", map.getFolderName(), "env",
"texture", "heightRoughness.dds")
.toString()
.replace("\\", "/");
map.getBiome().terrainMaterials().getNormalPaths()[8] = map.getBiome()
.terrainMaterials()
.getCubeMaps()
.get(0)
.getPath();
}
if (map.getTerrainShaderPath().equals(PBR_SHADER_NAME) ||
map.getTerrainShaderPath().equals(MAP_WIDE_ASSETS_SHADER_NAME)) {
map.getBiome().terrainMaterials().getTexturePaths()[9] =
Path.of("/maps", map.getFolderName(), "env", "texture", "mapwide.dds")
.toString()
.replace("\\", "/");
map.getBiome().terrainMaterials().getTexturePaths()[9] = Path.of("/maps", map.getFolderName(), "env",
"texture", "mapwide.dds")
.toString()
.replace("\\", "/");
// This needs to be higher than the map size in ogrids to trigger all aspects of the terrain shader.
map.getBiome().terrainMaterials().getTextureScales()[9] = map.getSize() + 1;
}
Expand All @@ -586,14 +552,13 @@ private void toFile(Path path) throws IOException {
boolean status = outFile.createNewFile();
if (status) {
FileOutputStream out = new FileOutputStream(outFile);
String summaryString = "Seed: "
+ seed
+ "\n"
+ generatorParameters.toString()
+ "\nStyle: "
+ tuningOptions.getMapStyle()
+ "\n"
+ styleGenerator.generatorsToString();
String summaryString = """
Seed: %d
%s
Style: %s
%s"
""".formatted(seed, generatorParameters.toString(), tuningOptions.getMapStyle(),
styleGenerator.generatorsToString());
out.write(summaryString.getBytes());
out.flush();
out.close();
Expand Down
Loading

0 comments on commit 684baa1

Please sign in to comment.