Skip to content

Commit

Permalink
Update infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 committed Mar 25, 2024
1 parent 3327962 commit c9bc633
Show file tree
Hide file tree
Showing 118 changed files with 2,240 additions and 1,709 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.faforever.neroxis.generator.cli.VisibilityOptions;
import com.faforever.neroxis.generator.style.BasicStyleGenerator;
import com.faforever.neroxis.generator.style.StyleGenerator;
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 All @@ -30,7 +29,6 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -48,7 +46,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.faforever.neroxis.map.SCMap.PBR_SHADER_NAME;
import static picocli.CommandLine.Command;
import static picocli.CommandLine.Option;
import static picocli.CommandLine.Spec;
Expand Down Expand Up @@ -125,7 +122,8 @@ private void printStyles() {
@Command(name = "biomes", aliases = {
"--biomes"}, description = "Prints the biomes available", versionProvider = VersionProvider.class, usageHelpAutoWidth = true)
private void printBiomes() {
System.out.println(Arrays.stream(BiomeName.values()).map(BiomeName::toString).collect(Collectors.joining("\n")));
System.out.println(
Arrays.stream(BiomeName.values()).map(BiomeName::toString).collect(Collectors.joining("\n")));
}

@Override
Expand Down Expand Up @@ -176,37 +174,30 @@ void populateGeneratorParametersAndName() {
encodeMapName();
}

@SuppressWarnings("unchecked")
private void setStyleAndParameters(GeneratorParameters.GeneratorParametersBuilder generatorParametersBuilder) {
if (tuningOptions.getMapStyle() == null) {
overwriteOptionalGeneratorParametersFromOptions(generatorParametersBuilder);
generatorParameters = generatorParametersBuilder.build();
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();
WeightedOption<StyleGenerator>[] generatorOptions = Arrays.stream(MapStyle.values()).map(mapStyle -> {
StyleGenerator styleGenerator = mapStyle.getGeneratorSupplier().get();
return new WeightedOption<>(styleGenerator, mapStyle.getWeight());
}).toArray(WeightedOption[]::new);
Map<Class<? extends StyleGenerator>, MapStyle> styleMap = Arrays.stream(MapStyle.values())
.collect(Collectors.toMap(
MapStyle::getGeneratorClass,
Function.identity()));
WeightedConstrainedOptions<StyleGenerator> styleGeneratorOptions = new WeightedConstrainedOptions<>(
WeightedOptionsWithFallback<StyleGenerator> styleGeneratorOptions = WeightedOptionsWithFallback.of(
new BasicStyleGenerator(), generatorOptions);
styleGenerator = ConstrainedSelector.selectRandomMatchingOption(random, styleGeneratorOptions,
generatorParameters);
styleGenerator = styleGeneratorOptions.select(random,
styleGenerator -> styleGenerator.getParameterConstraints()
.matches(
generatorParameters));
tuningOptions.setMapStyle(styleMap.get(styleGenerator.getClass()));
} else {
try {
styleGenerator = tuningOptions.getMapStyle().getGeneratorClass().getConstructor().newInstance();
generatorParameters = styleGenerator.getParameterConstraints()
.initParameters(random, generatorParametersBuilder);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException e) {
throw new RuntimeException(e);
}
styleGenerator = tuningOptions.getMapStyle().getGeneratorSupplier().get();
generatorParameters = styleGenerator.getParameterConstraints()
.initParameters(random, generatorParametersBuilder);
}
}

Expand Down Expand Up @@ -469,8 +460,8 @@ private void save() {
System.out.printf("Debug export done: %d ms\n", System.currentTimeMillis() - startTime);
}
} catch (IOException e) {
e.printStackTrace();
System.err.println("Error while saving the map.");
e.printStackTrace();
}
}

Expand Down Expand Up @@ -517,24 +508,11 @@ private void generate() {
map.changeMapSize(mapSize, compatibleMapSize, boundOffset);

map.addBlank(new Marker(mapName, new Vector2(0, 0)));
map.addDecalGroup(new DecalGroup(mapName, new int[0]));
map.addDecalGroup(new DecalGroup(mapName, List.of()));
map.setName(mapName);
map.setFolderName(mapName);
map.setFilePrefix(mapName);

if (map.getTerrainShaderPath().equals(PBR_SHADER_NAME)) {
map.getBiome().terrainMaterials().getNormalPaths()[8] = Path.of("/maps", map.getFolderName(), "env",
"layers", "heightRoughness.dds")
.toString()
.replace("\\", "/");
map.getBiome().terrainMaterials().getTexturePaths()[9] = Path.of("/maps", map.getFolderName(), "env",
"layers", "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;
}

ScriptGenerator.generateScript(map);

System.out.printf("Map generation done: %d ms\n", System.currentTimeMillis() - startTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.function.Supplier;

@Getter
@AllArgsConstructor
public enum MapStyle {
BASIC(BasicStyleGenerator.class, 1),
BIG_ISLANDS(BigIslandsStyleGenerator.class, 4),
CENTER_LAKE(CenterLakeStyleGenerator.class, 1),
DROP_PLATEAU(DropPlateauStyleGenerator.class, .5f),
FLOODED(FloodedStyleGenerator.class, 0),
HIGH_RECLAIM(HighReclaimStyleGenerator.class, .25f),
LAND_BRIDGE(LandBridgeStyleGenerator.class, 2),
LITTLE_MOUNTAIN(LittleMountainStyleGenerator.class, 1),
LOW_MEX(LowMexStyleGenerator.class, .5f),
MOUNTAIN_RANGE(MountainRangeStyleGenerator.class, 1),
ONE_ISLAND(OneIslandStyleGenerator.class, 1),
SMALL_ISLANDS(SmallIslandsStyleGenerator.class, 4),
VALLEY(ValleyStyleGenerator.class, 1);
BASIC(BasicStyleGenerator.class, BasicStyleGenerator::new, 1),
BIG_ISLANDS(BigIslandsStyleGenerator.class, BigIslandsStyleGenerator::new, 4),
CENTER_LAKE(CenterLakeStyleGenerator.class, CenterLakeStyleGenerator::new, 1),
DROP_PLATEAU(DropPlateauStyleGenerator.class, DropPlateauStyleGenerator::new, .5f),
FLOODED(FloodedStyleGenerator.class, FloodedStyleGenerator::new, .01f),
HIGH_RECLAIM(HighReclaimStyleGenerator.class, HighReclaimStyleGenerator::new, .25f),
LAND_BRIDGE(LandBridgeStyleGenerator.class, LandBridgeStyleGenerator::new, 2),
LITTLE_MOUNTAIN(LittleMountainStyleGenerator.class, LittleMountainStyleGenerator::new, 1),
LOW_MEX(LowMexStyleGenerator.class, LowMexStyleGenerator::new, .5f),
MOUNTAIN_RANGE(MountainRangeStyleGenerator.class, MountainRangeStyleGenerator::new, 1),
ONE_ISLAND(OneIslandStyleGenerator.class, OneIslandStyleGenerator::new, 1),
SMALL_ISLANDS(SmallIslandsStyleGenerator.class, SmallIslandsStyleGenerator::new, 4),
VALLEY(ValleyStyleGenerator.class, ValleyStyleGenerator::new, 1);

private final Class<? extends StyleGenerator> generatorClass;
private final Supplier<StyleGenerator> generatorSupplier;
private final float weight;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.random.RandomGenerator;

@SuppressWarnings({"unused"})
public record ParameterConstraints(Range landDensityRange,
Expand Down Expand Up @@ -41,7 +41,7 @@ public boolean matches(int mapSize, int numTeams, int spawnCount) {
spawnCount);
}

public GeneratorParameters randomizeParameters(Random random, GeneratorParameters generatorParameters) {
public GeneratorParameters randomizeParameters(RandomGenerator random, GeneratorParameters generatorParameters) {
return GeneratorParameters.builder()
.spawnCount(generatorParameters.spawnCount())
.landDensity(landDensityRange.getRandomFloat(random))
Expand All @@ -58,7 +58,7 @@ public GeneratorParameters randomizeParameters(Random random, GeneratorParameter
.build();
}

public GeneratorParameters mapToLevel(float level, GeneratorParameters generatorParameters, Random random) {
public GeneratorParameters mapToLevel(float level, GeneratorParameters generatorParameters, RandomGenerator random) {
return GeneratorParameters.builder()
.spawnCount(generatorParameters.spawnCount())
.landDensity(landDensityRange.map(level))
Expand All @@ -75,7 +75,7 @@ public GeneratorParameters mapToLevel(float level, GeneratorParameters generator
.build();
}

public GeneratorParameters initParameters(Random random,
public GeneratorParameters initParameters(RandomGenerator random,
GeneratorParameters.GeneratorParametersBuilder generatorParametersBuilder) {
return generatorParametersBuilder.landDensity(landDensityRange.getRandomFloat(random))
.plateauDensity(plateauDensityRange.getRandomFloat(random))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
package com.faforever.neroxis.generator;

public record WeightedOption<T>(T option, float weight) {}
import java.util.Objects;

public record WeightedOption<T>(
T option,
float weight
) {
public WeightedOption {
Objects.requireNonNull(option, "Option cannot be null");
if (weight <= 0) {
throw new IllegalArgumentException("Weight must be greater than 0");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.faforever.neroxis.generator;

import com.faforever.neroxis.generator.util.HasParameterConstraints;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.function.Predicate;

public sealed interface WeightedOptionsWithFallback<T> {

@SafeVarargs
static <T extends HasParameterConstraints> WeightedOptionsWithFallback<T> of(T fallback, WeightedOption<T>... options) {
if (options.length == 0) {
return new WeightedOptionsWithFallback.Single<>(fallback);
}

return new WeightedOptionsWithFallback.Multi<>(fallback, List.of(options));
}

default T select(Random random) {
return select(random, option -> true);
}

default T select(Random random, Predicate<? super T> filter) {
return switch (this) {
case Single(T option) -> option;
case Multi(T fallbackOption, List<WeightedOption<T>> options) -> {
List<WeightedOption<T>> matchingOptions = options.stream()
.filter(weightedOption -> filter.test(
weightedOption.option()))
.toList();
if (matchingOptions.isEmpty()) {
yield fallbackOption;
}

double[] weights = matchingOptions.stream().mapToDouble(WeightedOption::weight).toArray();
double[] cumulativeWeights = new double[weights.length];
double sum = 0;
for (int i = 0; i < weights.length; i++) {
sum += weights[i];
cumulativeWeights[i] = sum;
}
double value = random.nextDouble(sum);
int index = Arrays.binarySearch(cumulativeWeights, value);
if (index < 0) {
index = (index + 1) * -1;
}
yield matchingOptions.get(index).option();
}
};
}

record Multi<T>(
T fallbackOption,
List<WeightedOption<T>> options
) implements WeightedOptionsWithFallback<T> {

public Multi {
Objects.requireNonNull(fallbackOption, "Fallback cannot be null");
if (options.size() < 2) {
throw new IllegalArgumentException("At least two options must be provided");
}
options = List.copyOf(options);
}

}

record Single<T>(T option) implements WeightedOptionsWithFallback<T> {
public Single {
Objects.requireNonNull(option, "Option cannot be null");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ public void placeDecals() {
Pipeline.await(fieldDecal, slopeDecal);
DebugUtil.timedRun("com.faforever.neroxis.map.generator", "placeDecals", () -> {
decalPlacer.placeDecals(fieldDecal.getFinalMask(),
generatorParameters.biome().decalMaterials().getFieldNormals(), 32, 32, 24,
generatorParameters.biome().decalMaterials().fieldNormals(), 32, 32, 24,
32);
decalPlacer.placeDecals(fieldDecal.getFinalMask(),
generatorParameters.biome().decalMaterials().getFieldAlbedos(), 64, 128, 24,
generatorParameters.biome().decalMaterials().fieldAlbedos(), 64, 128, 24,
32);
decalPlacer.placeDecals(slopeDecal.getFinalMask(),
generatorParameters.biome().decalMaterials().getSlopeNormals(), 16, 32, 16,
generatorParameters.biome().decalMaterials().slopeNormals(), 16, 32, 16,
32);
decalPlacer.placeDecals(slopeDecal.getFinalMask(),
generatorParameters.biome().decalMaterials().getSlopeAlbedos(), 64, 128, 32,
generatorParameters.biome().decalMaterials().slopeAlbedos(), 64, 128, 32,
48);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public void placePropsWithExclusion() {
Pipeline.await(treeMask, cliffRockMask, fieldStoneMask);
DebugUtil.timedRun("com.faforever.neroxis.map.generator", "placeProps", () -> {
Biome biome = generatorParameters.biome();
propPlacer.placeProps(treeMask.getFinalMask().subtract(noProps), biome.propMaterials().getTreeGroups(),
propPlacer.placeProps(treeMask.getFinalMask().subtract(noProps), biome.propMaterials().treeGroups(),
3f, 7f);
propPlacer.placeProps(cliffRockMask.getFinalMask(), biome.propMaterials().getRocks(), .5f, 2.5f);
propPlacer.placeProps(cliffRockMask.getFinalMask(), biome.propMaterials().rocks(), .5f, 2.5f);
propPlacer.placeProps(fieldStoneMask.getFinalMask().subtract(noProps),
biome.propMaterials().getBoulders(), 30f);
biome.propMaterials().boulders(), 30f);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.faforever.neroxis.util.Pipeline;

import java.io.IOException;
import java.util.ArrayList;

public class EnemyCivPropGenerator extends BasicPropGenerator {
protected BooleanMask baseMask;
Expand Down Expand Up @@ -43,8 +42,8 @@ public void placeUnits() {
generateUnitExclusionMasks();
Pipeline.await(baseMask);
DebugUtil.timedRun("com.faforever.neroxis.map.generator", "placeBases", () -> {
Army army17 = new Army("ARMY_17", new ArrayList<>());
Group army17Initial = new Group("INITIAL", new ArrayList<>());
Army army17 = new Army("ARMY_17");
Group army17Initial = new Group("INITIAL");
army17.addGroup(army17Initial);
map.addArmy(army17);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import com.faforever.neroxis.util.DebugUtil;
import com.faforever.neroxis.util.Pipeline;

import static com.faforever.neroxis.biomes.BiomeName.*;
import static com.faforever.neroxis.biomes.BiomeName.DESERT;
import static com.faforever.neroxis.biomes.BiomeName.FRITHEN;
import static com.faforever.neroxis.biomes.BiomeName.MOONLIGHT;
import static com.faforever.neroxis.biomes.BiomeName.SUNSET;
import static com.faforever.neroxis.biomes.BiomeName.WONDER;

public class HighReclaimPropGenerator extends BasicPropGenerator {

Expand All @@ -25,11 +29,11 @@ public void placePropsWithExclusion() {
Pipeline.await(treeMask, cliffRockMask, fieldStoneMask);
DebugUtil.timedRun("com.faforever.neroxis.map.generator", "placeProps", () -> {
Biome biome = generatorParameters.biome();
propPlacer.placeProps(treeMask.getFinalMask().subtract(noProps), biome.propMaterials().getTreeGroups(),
propPlacer.placeProps(treeMask.getFinalMask().subtract(noProps), biome.propMaterials().treeGroups(),
3f, 7f);
propPlacer.placeProps(cliffRockMask.getFinalMask(), biome.propMaterials().getBoulders(), 3f, 8f);
propPlacer.placeProps(cliffRockMask.getFinalMask(), biome.propMaterials().boulders(), 3f, 8f);
propPlacer.placeProps(fieldStoneMask.getFinalMask().subtract(noProps),
biome.propMaterials().getBoulders(), 30f);
biome.propMaterials().boulders(), 30f);
});
}

Expand Down
Loading

0 comments on commit c9bc633

Please sign in to comment.