From fca926bc8f2f701be6951d93ad42c98cacada145 Mon Sep 17 00:00:00 2001 From: Leonardo Date: Tue, 5 Oct 2021 11:25:06 +0200 Subject: [PATCH] Added some comment, fixed the thread conflict problem --- .../worldGeneration/LodNodeGenWorker.java | 8 ++--- .../worldGeneration/LodWorldGenerator.java | 29 +++++++-------- .../com/seibel/lod/proxy/ClientProxy.java | 17 +++++++-- .../com/seibel/lod/util/DataPointUtil.java | 36 ++++++++++++++----- 4 files changed, 58 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/seibel/lod/builders/worldGeneration/LodNodeGenWorker.java b/src/main/java/com/seibel/lod/builders/worldGeneration/LodNodeGenWorker.java index 93c639b6a..18500fe7d 100644 --- a/src/main/java/com/seibel/lod/builders/worldGeneration/LodNodeGenWorker.java +++ b/src/main/java/com/seibel/lod/builders/worldGeneration/LodNodeGenWorker.java @@ -101,9 +101,9 @@ public class LodNodeGenWorker implements IWorker if (newServerWorld == null) throw new IllegalArgumentException("LodChunkGenThread requires a non-null ServerWorld"); - - - + + + thread = new LodChunkGenThread(newPos, newGenerationMode, newLodBuilder, newLodDimension, newServerWorld); @@ -615,7 +615,7 @@ public class LodNodeGenWorker implements IWorker { genThreads.shutdownNow(); } - genThreads = Executors.newFixedThreadPool(LodConfig.CLIENT.threading.numberOfWorldGenerationThreads.get(), new LodThreadFactory(LodNodeGenWorker.class.getSimpleName())); + genThreads = Executors.newFixedThreadPool(LodConfig.CLIENT.threading.numberOfWorldGenerationThreads.get(), new ThreadFactoryBuilder().setNameFormat("Gen-Worker-Thread-%d").build()); } diff --git a/src/main/java/com/seibel/lod/builders/worldGeneration/LodWorldGenerator.java b/src/main/java/com/seibel/lod/builders/worldGeneration/LodWorldGenerator.java index a81db12c0..4c7464c07 100644 --- a/src/main/java/com/seibel/lod/builders/worldGeneration/LodWorldGenerator.java +++ b/src/main/java/com/seibel/lod/builders/worldGeneration/LodWorldGenerator.java @@ -126,13 +126,11 @@ public class LodWorldGenerator nearIndex++; ChunkPos chunkPos = new ChunkPos(LevelPosUtil.getChunkPos(detailLevel, posX), LevelPosUtil.getChunkPos(detailLevel, posZ)); - if (numberOfChunksWaitingToGenerate.get() < maxChunkGenRequests) - { - // prevent generating the same chunk multiple times - if (positionsWaitingToBeGenerated.contains(chunkPos)) - continue; - } - + + // prevent generating the same chunk multiple times + if (positionsWaitingToBeGenerated.contains(chunkPos)) + continue; + // don't add more to the generation queue then allowed if (numberOfChunksWaitingToGenerate.get() >= maxChunkGenRequests) break; @@ -153,17 +151,16 @@ public class LodWorldGenerator farIndex++; ChunkPos chunkPos = new ChunkPos(LevelPosUtil.getChunkPos(detailLevel, posX), LevelPosUtil.getChunkPos(detailLevel, posZ)); - if (numberOfChunksWaitingToGenerate.get() < maxChunkGenRequests) - { - // prevent generating the same chunk multiple times - if (positionsWaitingToBeGenerated.contains(chunkPos)) - continue; - } - + // don't add more to the generation queue then allowed if (numberOfChunksWaitingToGenerate.get() >= maxChunkGenRequests) - break; - + continue; + //break; + + // prevent generating the same chunk multiple times + if (positionsWaitingToBeGenerated.contains(chunkPos)) + continue; + positionsWaitingToBeGenerated.add(chunkPos); numberOfChunksWaitingToGenerate.addAndGet(1); LodNodeGenWorker genWorker = new LodNodeGenWorker(chunkPos, DetailDistanceUtil.getDistanceGenerationMode(detailLevel), lodBuilder, lodDim, serverWorld); diff --git a/src/main/java/com/seibel/lod/proxy/ClientProxy.java b/src/main/java/com/seibel/lod/proxy/ClientProxy.java index 795f22c5f..aa410e3ef 100644 --- a/src/main/java/com/seibel/lod/proxy/ClientProxy.java +++ b/src/main/java/com/seibel/lod/proxy/ClientProxy.java @@ -231,7 +231,8 @@ public class ClientProxy public void worldLoadEvent(WorldEvent.Load event) { DataPointUtil.worldHeight = event.getWorld().getHeight(); - ThreadMapUtil.clearMaps(); + //LodNodeGenWorker.restartExecuterService(); + //ThreadMapUtil.clearMaps(); // the player just loaded a new world/dimension lodWorld.selectWorld(LodUtil.getWorldID(event.getWorld())); @@ -254,8 +255,8 @@ public class ClientProxy // if this isn't done unfinished tasks may be left in the queue // preventing new LodChunks form being generated - LodNodeGenWorker.restartExecuterService(); - ThreadMapUtil.clearMaps(); + //LodNodeGenWorker.restartExecuterService(); + //ThreadMapUtil.clearMaps(); LodWorldGenerator.INSTANCE.numberOfChunksWaitingToGenerate.set(0); lodWorld.deselectWorld(); @@ -366,6 +367,16 @@ public class ClientProxy firstTimeSetupComplete = true; } + + /** + * this method reset some of the static data everytime we change world + **/ + private void resetMod() + { + ThreadMapUtil.clearMaps(); + LodNodeGenWorker.restartExecuterService(); + + } //================// // public getters // diff --git a/src/main/java/com/seibel/lod/util/DataPointUtil.java b/src/main/java/com/seibel/lod/util/DataPointUtil.java index 45bff9c76..b38a97653 100644 --- a/src/main/java/com/seibel/lod/util/DataPointUtil.java +++ b/src/main/java/com/seibel/lod/util/DataPointUtil.java @@ -174,7 +174,13 @@ public class DataPointUtil { return (int) (((dataPoint >>> COLOR_SHIFT) & COLOR_MASK) | (((dataPoint >>> (ALPHA_SHIFT - ALPHA_DOWNSIZE_SHIFT)) | 0b1111) << 24)); } - + + /** + * This method apply the lightmap to the color to use + * @param dataPoint + * @param lightMap + * @return + */ public static int getLightColor(long dataPoint, NativeImage lightMap) { int lightBlock = getLightBlock(dataPoint); @@ -186,7 +192,12 @@ public class DataPointUtil return ColorUtil.multiplyRGBcolors(getColor(dataPoint), ColorUtil.rgbToInt(red, green, blue)); } - + + /** + * This is used to convert a dataPoint to string (usefull for the print function) + * @param dataPoint + * @return + */ public static String toString(long dataPoint) { StringBuilder s = new StringBuilder(); @@ -214,7 +225,12 @@ public class DataPointUtil s.append('\n'); return s.toString(); } - + + /** + * This method merge column of single data together + * @param dataToMerge + * @return + */ public static long mergeSingleData(long[] dataToMerge) { int numberOfChildren = 0; @@ -275,7 +291,14 @@ public class DataPointUtil return DataPointUtil.createDataPoint(tempAlpha, tempRed, tempGreen, tempBlue, tempHeight, tempDepth, tempLightSky, tempLightBlock, tempGenMode); } } - + + /** + * This method merge column of multiple data together + * @param dataToMerge + * @param inputVerticalData vertical size of an input data + * @param maxVerticalData max vertical size of the merged data + * @return + */ public static long[] mergeMultiData(long[] dataToMerge, int inputVerticalData, int maxVerticalData) { int size = dataToMerge.length / inputVerticalData; @@ -422,9 +445,4 @@ public class DataPointUtil } return dataPoint; } - - public static long[] compress(long[] data, byte detailLevel) - { - return null; - } } \ No newline at end of file