reformat chunk generator code

This commit is contained in:
James Seibel
2022-11-25 15:58:53 -06:00
parent eb0bb8a67d
commit 007e993148
4 changed files with 672 additions and 514 deletions
@@ -3,7 +3,7 @@
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2021 Tom Lee (TomTheFurry) & James Seibel (Original code)
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
@@ -25,6 +25,7 @@ import com.seibel.lod.api.enums.config.EDistanceGenerationMode;
import com.seibel.lod.core.dependencyInjection.SingletonInjector;
import com.seibel.lod.core.logging.DhLoggerBuilder;
import com.seibel.lod.core.pos.DhChunkPos;
import com.seibel.lod.core.util.BitShiftUtil;
import com.seibel.lod.core.wrapperInterfaces.IWrapperFactory;
import com.seibel.lod.core.wrapperInterfaces.chunk.IChunkWrapper;
import com.seibel.lod.core.wrapperInterfaces.minecraft.IMinecraftClientWrapper;
@@ -36,38 +37,51 @@ import java.lang.invoke.MethodHandles;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
/**
*
* @version 2022-11-25
*/
public class BatchGenerator implements IChunkGenerator
{
public static final boolean ENABLE_GENERATOR_STATS_LOGGING = false;
private static final IMinecraftClientWrapper MC = SingletonInjector.INSTANCE.get(IMinecraftClientWrapper.class);
private static final IWrapperFactory FACTORY = SingletonInjector.INSTANCE.get(IWrapperFactory.class);
public AbstractBatchGenerationEnvionmentWrapper generationGroup;
public IDhLevel targetLodLevel;
public static final int generationGroupSize = 4;
private static final Logger LOGGER = DhLoggerBuilder.getLogger(MethodHandles.lookup().lookupClass().getSimpleName());
public BatchGenerator(IDhLevel targetLodLevel) {
public BatchGenerator(IDhLevel targetLodLevel)
{
this.targetLodLevel = targetLodLevel;
generationGroup = FACTORY.createBatchGenerator(targetLodLevel);
this.generationGroup = FACTORY.createBatchGenerator(targetLodLevel);
LOGGER.info("Batch Chunk Generator initialized");
}
public void stop(boolean blocking) {
LOGGER.info("1.18 Experimental Chunk Generator shutting down...");
generationGroup.stop(blocking);
public void stop(boolean blocking)
{
LOGGER.info("Batch Chunk Generator shutting down...");
this.generationGroup.stop(blocking);
}
@Override
public boolean isBusy() {
return generationGroup.getEventCount() > Math.max(Config.Client.Advanced.Threading.numberOfWorldGenerationThreads.get().intValue(), 1) *1.5;
public boolean isBusy()
{
return this.generationGroup.getEventCount() > Math.max(Config.Client.Advanced.Threading.numberOfWorldGenerationThreads.get().intValue(), 1) * 1.5;
}
@Override
public CompletableFuture<Void> generateChunks(DhChunkPos chunkPosMin, byte granularity, byte targetDataDetail, Consumer<IChunkWrapper> resultConsumer) {
public CompletableFuture<Void> generateChunks(DhChunkPos chunkPosMin, byte granularity, byte targetDataDetail, Consumer<IChunkWrapper> resultConsumer)
{
EDistanceGenerationMode mode = Config.Client.WorldGenerator.distanceGenerationMode.get();
Steps targetStep = null;
switch (mode) {
switch (mode)
{
case NONE:
targetStep = Steps.Empty; // NOTE: Only load in existing chunks. No new chunk generation
break;
@@ -84,47 +98,36 @@ public class BatchGenerator implements IChunkGenerator
case FULL:
targetStep = Steps.Features;
break;
};
}
;
int chunkXMin = chunkPosMin.x;
int chunkZMin = chunkPosMin.z;
int genChunkSize = 1 << (granularity - 4); // minus 4 for chunk size as its equal to div by 16
double runTimeRatio = Config.Client.Advanced.Threading.numberOfWorldGenerationThreads.get()>1 ? 1.0
: Config.Client.Advanced.Threading.numberOfWorldGenerationThreads.get();
return generationGroup.generateChunks(chunkXMin, chunkZMin, genChunkSize, targetStep, runTimeRatio, resultConsumer);
int genChunkSize = BitShiftUtil.powerOfTwo(granularity - 4); // minus 4 for chunk size as its equal to dividing by 16
double runTimeRatio = Config.Client.Advanced.Threading.numberOfWorldGenerationThreads.get() > 1 ?
1.0 :
Config.Client.Advanced.Threading.numberOfWorldGenerationThreads.get();
return this.generationGroup.generateChunks(chunkXMin, chunkZMin, genChunkSize, targetStep, runTimeRatio, resultConsumer);
}
@Override
public byte getMinDataDetail() {
return 0;
}
public byte getMinDataDetail() { return 0; }
@Override
public byte getMaxDataDetail() {
return 0;
}
public byte getMaxDataDetail() { return 0; }
@Override
public int getPriority() {
return 0;
}
public int getPriority() { return 0; }
@Override
public byte getMinGenerationGranularity() {
return 4;
}
public byte getMinGenerationGranularity() { return 4; }
@Override
public byte getMaxGenerationGranularity() {
return 6;
}
public byte getMaxGenerationGranularity() { return 6; }
@Override
public void close() {
stop(true);
}
public void update() {
generationGroup.updateAllFutures();
}
public void close() { this.stop(true); }
public void update() { this.generationGroup.updateAllFutures(); }
}
File diff suppressed because it is too large Load Diff
@@ -8,14 +8,24 @@ import com.seibel.lod.core.wrapperInterfaces.chunk.IChunkWrapper;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
public interface IChunkGenerator extends IGenerator {
CompletableFuture<Void> generateChunks(DhChunkPos chunkPosMin, byte granularity, byte targetDataDetail, Consumer<IChunkWrapper> resultConsumer);
@Override
default CompletableFuture<Void> generate(DhChunkPos chunkPosMin, byte granularity, byte targetDataDetail, Consumer<ChunkSizedData> resultConsumer) {
return generateChunks(chunkPosMin, granularity, targetDataDetail, (chunk) -> {
resultConsumer.accept(LodDataBuilder.createChunkData(chunk));
});
}
/**
* @version 2022-11-25
*/
public interface IChunkGenerator extends IGenerator
{
CompletableFuture<Void> generateChunks(DhChunkPos chunkPosMin,
byte granularity, byte targetDataDetail,
Consumer<IChunkWrapper> resultConsumer);
@Override
default CompletableFuture<Void> generate(DhChunkPos chunkPosMin,
byte granularity, byte targetDataDetail,
Consumer<ChunkSizedData> resultConsumer)
{
return this.generateChunks(chunkPosMin, granularity, targetDataDetail, (chunk) ->
{
resultConsumer.accept(LodDataBuilder.createChunkData(chunk));
});
}
}
@@ -6,29 +6,43 @@ import com.seibel.lod.core.pos.DhChunkPos;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
public interface IGenerator extends AutoCloseable {
// What is the detail / resolution of the data? (This will offset the generation granularity)
// (minimum detail is 0, maximum detail is 255) (though that high isn't really... realistic)
// (0 = 1x1 block per data, 1 = 2x2 block per data, 2 = 4x4 block per data... etc.)
// TODO: System currently only supports 1x1 block per data.
/**
* @version 2022-11-25
*/
public interface IGenerator extends AutoCloseable
{
/**
* What is the detail/resolution of the data? (This will offset the generation granularity)
* (minimum detail is 0, maximum detail is 255) (though that high isn't really... realistic)
* (0 = 1x1 block per data, 1 = 2x2 block per data, 2 = 4x4 block per data... etc.)
* TODO: System currently only supports 1x1 block per data.
*/
byte getMinDataDetail();
byte getMaxDataDetail();
int getPriority();
// What is the min batch size of a single generation?
// (minimum return value is 4 since that's the MC chunk size)
// (4 -> 16x16 data per call, 5 -> 32x32 data per call, 6 -> 64x64 data per call... etc.)
/**
* What is the min batch size of a single generation?
* (minimum return value is 4 since that's the MC chunk size)
* (4 -> 16x16 data per call, 5 -> 32x32 data per call, 6 -> 64x64 data per call... etc.)
*/
byte getMinGenerationGranularity();
// What is the max batch size of a single generation? The system will try to group tasks to the max batch size if possible
// (minimum return value is 4 since that's the MC chunk size)
// (4 -> 16x16 data per call, 5 -> 32x32 data per call, 6 -> 64x64 data per call... etc.)
/**
* What is the max batch size of a single generation? The system will try to group tasks to the max batch size if possible
* (minimum return value is 4 since that's the MC chunk size)
* (4 -> 16x16 data per call, 5 -> 32x32 data per call, 6 -> 64x64 data per call... etc.)
*/
byte getMaxGenerationGranularity();
// Start a generation event
// (Note that the chunkPos is always aligned to the granularity)
// (For example, if the granularity is 4, data detail is 0, the chunkPos will be aligned to 16x16 blocks)
/**
* Start a generation event
* (Note that the chunkPos is always aligned to the granularity)
* (For example, if the granularity is 4, data detail is 0, the chunkPos will be aligned to 16x16 blocks)
*/
CompletableFuture<Void> generate(DhChunkPos chunkPosMin, byte granularity, byte targetDataDetail, Consumer<ChunkSizedData> resultConsumer);
// Return whether the generator is currently busy and cannot accept new generation requests.
/** Returns whether the generator is unable to accept new generation requests. */
boolean isBusy();
}