added biome weight & performance configs

This commit is contained in:
dags- 2020-06-14 12:25:09 +01:00
parent d6df3dd168
commit d91018d502
8 changed files with 203 additions and 17 deletions

2
Engine

@ -1 +1 @@
Subproject commit 148a59d8ec0f555e61b6e13526aedd3bc193d427 Subproject commit 7a8cf4023c45668b7622aee8b372e119c998a6a7

View File

@ -27,6 +27,7 @@ package com.terraforged;
import com.terraforged.api.material.WGTags; import com.terraforged.api.material.WGTags;
import com.terraforged.command.TerraCommand; import com.terraforged.command.TerraCommand;
import com.terraforged.config.ConfigManager;
import com.terraforged.data.DataGen; import com.terraforged.data.DataGen;
import com.terraforged.feature.context.ContextSelectorFeature; import com.terraforged.feature.context.ContextSelectorFeature;
import com.terraforged.feature.decorator.poisson.PoissonAtSurface; import com.terraforged.feature.decorator.poisson.PoissonAtSurface;
@ -63,6 +64,7 @@ public class TerraForgedMod {
WGTags.init(); WGTags.init();
TerraWorld.init(); TerraWorld.init();
TerraCommand.init(); TerraCommand.init();
ConfigManager.init();
SettingsHelper.init(); SettingsHelper.init();
// temp fix // temp fix

View File

@ -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);
}
}

View File

@ -1,9 +1,13 @@
package com.terraforged.biome.provider; 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.util.ResourceLocation;
import net.minecraft.world.biome.Biome; import net.minecraft.world.biome.Biome;
import net.minecraftforge.common.BiomeDictionary; import net.minecraftforge.common.BiomeDictionary;
import net.minecraftforge.common.BiomeManager; import net.minecraftforge.common.BiomeManager;
import net.minecraftforge.registries.ForgeRegistries;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -24,6 +28,8 @@ public class BiomeWeights {
this.standardWeight = standard; this.standardWeight = standard;
this.forestWeight = forest; this.forestWeight = forest;
this.rareWeight = rare; this.rareWeight = rare;
// Get biome weights from Forge
for (BiomeManager.BiomeType type : BiomeManager.BiomeType.values()) { for (BiomeManager.BiomeType type : BiomeManager.BiomeType.values()) {
List<BiomeManager.BiomeEntry> entries = BiomeManager.getBiomes(type); List<BiomeManager.BiomeEntry> entries = BiomeManager.getBiomes(type);
if (entries == null) { if (entries == null) {
@ -33,6 +39,9 @@ public class BiomeWeights {
biomes.put(entry.biome.getRegistryName(), entry.itemWeight); biomes.put(entry.biome.getRegistryName(), entry.itemWeight);
} }
} }
// TF config gets final say
readWeights();
} }
public int getWeight(Biome biome) { public int getWeight(Biome biome) {
@ -48,4 +57,23 @@ public class BiomeWeights {
} }
return standardWeight; 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);
}
}
} }

View File

@ -25,9 +25,11 @@
package com.terraforged.chunk; package com.terraforged.chunk;
import com.electronwill.nightconfig.core.CommentedConfig;
import com.terraforged.api.chunk.column.DecoratorContext; import com.terraforged.api.chunk.column.DecoratorContext;
import com.terraforged.api.chunk.surface.ChunkSurfaceBuffer; import com.terraforged.api.chunk.surface.ChunkSurfaceBuffer;
import com.terraforged.api.chunk.surface.SurfaceContext; import com.terraforged.api.chunk.surface.SurfaceContext;
import com.terraforged.config.PerfDefaults;
import com.terraforged.core.concurrent.thread.ThreadPools; import com.terraforged.core.concurrent.thread.ThreadPools;
import com.terraforged.core.region.gen.RegionCache; import com.terraforged.core.region.gen.RegionCache;
import com.terraforged.core.region.gen.RegionGenerator; import com.terraforged.core.region.gen.RegionGenerator;
@ -73,12 +75,16 @@ public class TerraContext extends GeneratorContext {
} }
public static RegionCache createCache(WorldGeneratorFactory factory) { 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() return RegionGenerator.builder()
.pool(ThreadPools.create(threadCount, batching))
.size(tileSize, 2)
.batch(batchSize)
.factory(factory) .factory(factory)
.size(3, 2) .build().toCache();
.pool(ThreadPools.getPool())
.batch(6)
.build()
.toCache();
} }
} }

View File

@ -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<CommentedFileConfig> 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 <T> void set(CommentedConfig config, String path, T value, String... lines) {
config.setComment(path, Strings.join(lines, "\n"));
config.set(path, value);
}
}

View File

@ -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<CommentedFileConfig> {
private final Object lock = new Object();
private final Supplier<CommentedFileConfig> factory;
private CommentedFileConfig ref;
public ConfigRef(Supplier<CommentedFileConfig> 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();
}
}
}

View File

@ -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;
}
}