From d91018d502412b6614bc29e38422852dc69e1969 Mon Sep 17 00:00:00 2001 From: dags- Date: Sun, 14 Jun 2020 12:25:09 +0100 Subject: [PATCH] added biome weight & performance configs --- Engine | 2 +- .../java/com/terraforged/TerraForgedMod.java | 2 + .../com/terraforged/biome/map/set/Check.java | 11 --- .../biome/provider/BiomeWeights.java | 28 ++++++ .../com/terraforged/chunk/TerraContext.java | 16 +++- .../com/terraforged/config/ConfigManager.java | 95 +++++++++++++++++++ .../com/terraforged/config/ConfigRef.java | 30 ++++++ .../com/terraforged/config/PerfDefaults.java | 36 +++++++ 8 files changed, 203 insertions(+), 17 deletions(-) delete mode 100644 src/main/java/com/terraforged/biome/map/set/Check.java create mode 100644 src/main/java/com/terraforged/config/ConfigManager.java create mode 100644 src/main/java/com/terraforged/config/ConfigRef.java create mode 100644 src/main/java/com/terraforged/config/PerfDefaults.java diff --git a/Engine b/Engine index 148a59d..7a8cf40 160000 --- a/Engine +++ b/Engine @@ -1 +1 @@ -Subproject commit 148a59d8ec0f555e61b6e13526aedd3bc193d427 +Subproject commit 7a8cf4023c45668b7622aee8b372e119c998a6a7 diff --git a/src/main/java/com/terraforged/TerraForgedMod.java b/src/main/java/com/terraforged/TerraForgedMod.java index 634afa8..3c74eed 100644 --- a/src/main/java/com/terraforged/TerraForgedMod.java +++ b/src/main/java/com/terraforged/TerraForgedMod.java @@ -27,6 +27,7 @@ package com.terraforged; import com.terraforged.api.material.WGTags; import com.terraforged.command.TerraCommand; +import com.terraforged.config.ConfigManager; import com.terraforged.data.DataGen; import com.terraforged.feature.context.ContextSelectorFeature; import com.terraforged.feature.decorator.poisson.PoissonAtSurface; @@ -63,6 +64,7 @@ public class TerraForgedMod { WGTags.init(); TerraWorld.init(); TerraCommand.init(); + ConfigManager.init(); SettingsHelper.init(); // temp fix diff --git a/src/main/java/com/terraforged/biome/map/set/Check.java b/src/main/java/com/terraforged/biome/map/set/Check.java deleted file mode 100644 index 1ee9415..0000000 --- a/src/main/java/com/terraforged/biome/map/set/Check.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.terraforged.biome.map.set; - -import java.util.stream.Stream; - -public class Check { - - public static void main(String[] args) { - String[] s = {null}; - Stream.of(null).forEach(System.out::println); - } -} diff --git a/src/main/java/com/terraforged/biome/provider/BiomeWeights.java b/src/main/java/com/terraforged/biome/provider/BiomeWeights.java index 344a719..356bf53 100644 --- a/src/main/java/com/terraforged/biome/provider/BiomeWeights.java +++ b/src/main/java/com/terraforged/biome/provider/BiomeWeights.java @@ -1,9 +1,13 @@ package com.terraforged.biome.provider; +import com.electronwill.nightconfig.core.CommentedConfig; +import com.terraforged.Log; +import com.terraforged.config.ConfigManager; import net.minecraft.util.ResourceLocation; import net.minecraft.world.biome.Biome; import net.minecraftforge.common.BiomeDictionary; import net.minecraftforge.common.BiomeManager; +import net.minecraftforge.registries.ForgeRegistries; import java.util.HashMap; import java.util.List; @@ -24,6 +28,8 @@ public class BiomeWeights { this.standardWeight = standard; this.forestWeight = forest; this.rareWeight = rare; + + // Get biome weights from Forge for (BiomeManager.BiomeType type : BiomeManager.BiomeType.values()) { List entries = BiomeManager.getBiomes(type); if (entries == null) { @@ -33,6 +39,9 @@ public class BiomeWeights { biomes.put(entry.biome.getRegistryName(), entry.itemWeight); } } + + // TF config gets final say + readWeights(); } public int getWeight(Biome biome) { @@ -48,4 +57,23 @@ public class BiomeWeights { } return standardWeight; } + + private void readWeights() { + CommentedConfig config = ConfigManager.BIOME_WEIGHTS.get(); + + for (String key : config.valueMap().keySet()) { + int weight = config.getInt(key); + if (weight < 0) { + continue; + } + + ResourceLocation name = new ResourceLocation(key); + if (!ForgeRegistries.BIOMES.containsKey(name)) { + continue; + } + + biomes.put(name, weight); + Log.debug("Loaded custom biome weight: %s=%s", name, weight); + } + } } diff --git a/src/main/java/com/terraforged/chunk/TerraContext.java b/src/main/java/com/terraforged/chunk/TerraContext.java index 91ead09..cf78013 100644 --- a/src/main/java/com/terraforged/chunk/TerraContext.java +++ b/src/main/java/com/terraforged/chunk/TerraContext.java @@ -25,9 +25,11 @@ package com.terraforged.chunk; +import com.electronwill.nightconfig.core.CommentedConfig; import com.terraforged.api.chunk.column.DecoratorContext; import com.terraforged.api.chunk.surface.ChunkSurfaceBuffer; import com.terraforged.api.chunk.surface.SurfaceContext; +import com.terraforged.config.PerfDefaults; import com.terraforged.core.concurrent.thread.ThreadPools; import com.terraforged.core.region.gen.RegionCache; import com.terraforged.core.region.gen.RegionGenerator; @@ -73,12 +75,16 @@ public class TerraContext extends GeneratorContext { } public static RegionCache createCache(WorldGeneratorFactory factory) { + CommentedConfig config = PerfDefaults.getAndPrintPerfSettings(); + boolean batching = config.getOrElse("batching",false); + int tileSize = Math.min(PerfDefaults.MAX_TILE_SIZE, Math.max(2, config.getInt("tile_size"))); + int batchSize = Math.min(PerfDefaults.MAX_BATCH_SIZE, Math.max(1, config.getInt("batch_size"))); + int threadCount = Math.min(PerfDefaults.MAX_THREAD_COUNT, Math.max(1, config.getInt("thread_count"))); return RegionGenerator.builder() + .pool(ThreadPools.create(threadCount, batching)) + .size(tileSize, 2) + .batch(batchSize) .factory(factory) - .size(3, 2) - .pool(ThreadPools.getPool()) - .batch(6) - .build() - .toCache(); + .build().toCache(); } } diff --git a/src/main/java/com/terraforged/config/ConfigManager.java b/src/main/java/com/terraforged/config/ConfigManager.java new file mode 100644 index 0000000..feea59e --- /dev/null +++ b/src/main/java/com/terraforged/config/ConfigManager.java @@ -0,0 +1,95 @@ +package com.terraforged.config; + +import com.electronwill.nightconfig.core.CommentedConfig; +import com.electronwill.nightconfig.core.Config; +import com.electronwill.nightconfig.core.file.CommentedFileConfig; +import com.electronwill.nightconfig.toml.TomlFormat; +import com.terraforged.Log; +import joptsimple.internal.Strings; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.function.Consumer; + +public class ConfigManager { + + private static final Path COMMON_DIR = Paths.get("config", "terraforged"); + + public static final ConfigRef BIOME_WEIGHTS = new ConfigRef(() -> create("biome_weights", cfg -> { + set( + cfg, + "terraforged:example_biome", + 10, + "Configure biome weights by entering their id and an integer value for their weight (default weight is 10)", + "This config will override the weights configured or provided by other mods for TerraForged worlds only." + ); + })); + + public static final ConfigRef PERFORMANCE = new ConfigRef(() -> create("performance", cfg -> { + set ( + cfg, + "thread_count", + PerfDefaults.THREAD_COUNT, + "Controls the total number of threads that will be used to generate heightmap tiles.", + "Allowing the generator to use more threads can help speed up generation but increases overall", + "load on your CPU which may adversely affect performance in other areas of the game engine." + ); + + set( + cfg, + "tile_size", + PerfDefaults.TILE_SIZE, + "Controls the size of heightmap tiles.", + "Smaller tiles are faster to generate but less memory efficient." + ); + + set( + cfg, + "batching", + true, + "Batching breaks heightmap tiles up into smaller pieces that can be generated concurrently.", + "This can help improve generation speed by utilizing more CPU cores.", + "It is better suited to higher core-count machines (6+ cores)" + ); + + set( + cfg, + "batch_size", + PerfDefaults.BATCH_SIZE, + "Controls the number of pieces a heightmap tile is divided up into.", + "Higher core count CPUs may benefit from higher batch sizes." + ); + })); + + public static void init() { + Config.setInsertionOrderPreserved(true); + BIOME_WEIGHTS.get(); + PERFORMANCE.get(); + PerfDefaults.getAndPrintPerfSettings(); + } + + private static CommentedFileConfig create(String name, Consumer defaulter) { + Path path = COMMON_DIR.resolve(name + ".conf"); + if (!Files.exists(path)) { + Log.info("Creating default config: {}", name); + try { + Files.createDirectories(path.getParent()); + } catch (IOException e) { + e.printStackTrace(); + } + CommentedFileConfig config = CommentedFileConfig.of(path, TomlFormat.instance()); + defaulter.accept(config); + config.save(); + return config; + } else { + return CommentedFileConfig.of(path, TomlFormat.instance()); + } + } + + private static void set(CommentedConfig config, String path, T value, String... lines) { + config.setComment(path, Strings.join(lines, "\n")); + config.set(path, value); + } +} diff --git a/src/main/java/com/terraforged/config/ConfigRef.java b/src/main/java/com/terraforged/config/ConfigRef.java new file mode 100644 index 0000000..8fa8337 --- /dev/null +++ b/src/main/java/com/terraforged/config/ConfigRef.java @@ -0,0 +1,30 @@ +package com.terraforged.config; + +import com.electronwill.nightconfig.core.file.CommentedFileConfig; +import com.terraforged.Log; + +import java.util.function.Supplier; + +public class ConfigRef implements Supplier { + + private final Object lock = new Object(); + private final Supplier factory; + + private CommentedFileConfig ref; + + public ConfigRef(Supplier factory) { + this.factory = factory; + } + + @Override + public CommentedFileConfig get() { + synchronized (lock) { + if (ref != null) { + Log.info("Loading config: %s", ref.getFile().getName()); + ref.load(); + return ref; + } + return ref = factory.get(); + } + } +} diff --git a/src/main/java/com/terraforged/config/PerfDefaults.java b/src/main/java/com/terraforged/config/PerfDefaults.java new file mode 100644 index 0000000..4c906de --- /dev/null +++ b/src/main/java/com/terraforged/config/PerfDefaults.java @@ -0,0 +1,36 @@ +package com.terraforged.config; + +import com.electronwill.nightconfig.core.CommentedConfig; +import com.terraforged.Log; +import com.terraforged.core.concurrent.thread.ThreadPools; + +public class PerfDefaults { + + public static final int TILE_SIZE = 3; + public static final int BATCH_SIZE = 6; + public static final int THREAD_COUNT = ThreadPools.defaultPoolSize(); + + public static final int MAX_TILE_SIZE = 8; + public static final int MAX_BATCH_SIZE = 20; + public static final int MAX_THREAD_COUNT = Runtime.getRuntime().availableProcessors(); + + private static boolean isUsingDefaultPerfSettings(CommentedConfig config) { + boolean yes = true; + yes &= config.getOrElse("batching", true); + yes &= config.getInt("thread_count") == THREAD_COUNT; + yes &= config.getInt("batch_size") == BATCH_SIZE; + yes &= config.getInt("tile_size") == TILE_SIZE; + return yes; + } + + public static CommentedConfig getAndPrintPerfSettings() { + CommentedConfig config = ConfigManager.PERFORMANCE.get(); + boolean defaults = isUsingDefaultPerfSettings(config); + Log.info("Performance Settings [default={}]", defaults); + Log.info(" - Thread Count: {}", config.getInt("thread_count")); + Log.info(" - Tile Size: {}", config.getInt("tile_size")); + Log.info(" - Batching: {}", config.getOrElse("batching", true)); + Log.info(" - Batch Size: {}", config.getInt("batch_size")); + return config; + } +}