From 9e3f729c8f44498631ceb0f8111333571d312c09 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Mon, 2 Jan 2023 11:13:31 -0600 Subject: [PATCH] Replace LevelPosUtil.convert() with DhLodPos.convertToDetailLevel() --- .../column/render/CubicLodTemplate.java | 18 ++++++++----- .../datatype/full/FullDataDownSampler.java | 2 +- .../core/datatype/full/FullDataSource.java | 4 +-- .../core/datatype/full/SpottyDataSource.java | 6 ++--- .../core/file/datafile/DataFileHandler.java | 2 +- .../file/renderfile/RenderFileHandler.java | 2 +- .../core/generation/WorldGenerationQueue.java | 6 ++--- .../lod/core/level/DhClientServerLevel.java | 2 +- .../com/seibel/lod/core/pos/DhChunkPos.java | 12 --------- .../com/seibel/lod/core/pos/DhLodPos.java | 26 ++++++++++++------- .../com/seibel/lod/core/pos/DhSectionPos.java | 5 ++-- .../seibel/lod/core/util/LevelPosUtil.java | 20 -------------- .../chunk/IChunkWrapper.java | 10 +------ 13 files changed, 44 insertions(+), 71 deletions(-) diff --git a/core/src/main/java/com/seibel/lod/core/datatype/column/render/CubicLodTemplate.java b/core/src/main/java/com/seibel/lod/core/datatype/column/render/CubicLodTemplate.java index 307170139..7d0d20c3b 100644 --- a/core/src/main/java/com/seibel/lod/core/datatype/column/render/CubicLodTemplate.java +++ b/core/src/main/java/com/seibel/lod/core/datatype/column/render/CubicLodTemplate.java @@ -24,15 +24,16 @@ import com.seibel.lod.core.datatype.column.accessor.ColumnFormat; import com.seibel.lod.api.enums.rendering.EDebugMode; import com.seibel.lod.core.dependencyInjection.SingletonInjector; import com.seibel.lod.core.datatype.column.accessor.ColumnArrayView; +import com.seibel.lod.core.pos.DhLodPos; +import com.seibel.lod.core.util.BitShiftUtil; import com.seibel.lod.core.util.ColorUtil; -import com.seibel.lod.core.util.LevelPosUtil; import com.seibel.lod.core.util.LodUtil; import com.seibel.lod.core.wrapperInterfaces.config.ILodConfigWrapperSingleton; /** * Builds LODs as rectangular prisms. * @author James Seibel - * @version 3-19-2022 + * @version 2022-1-2 */ public class CubicLodTemplate { @@ -42,14 +43,19 @@ public class CubicLodTemplate public static void addLodToBuffer(long data, long topData, long botData, ColumnArrayView[][] adjData, byte detailLevel, int offsetPosX, int offsetOosZ, LodQuadBuilder quadBuilder, EDebugMode debugging, ColumnRenderSource.DebugSourceFlag debugSource) { - short width = (short) (1 << detailLevel); - short x = (short) LevelPosUtil.convert(detailLevel, offsetPosX, LodUtil.BLOCK_DETAIL_LEVEL); + DhLodPos blockOffsetPos = new DhLodPos(detailLevel, offsetPosX, offsetOosZ).convertToDetailLevel(LodUtil.BLOCK_DETAIL_LEVEL); + + short width = (short) BitShiftUtil.powerOfTwo(detailLevel); + short x = (short) blockOffsetPos.x; short y = ColumnFormat.getDepth(data); - short z = (short) LevelPosUtil.convert(detailLevel, offsetOosZ, LodUtil.BLOCK_DETAIL_LEVEL); + short z = (short) (short) blockOffsetPos.z; short dy = (short) (ColumnFormat.getHeight(data) - y); + if (dy == 0) + { return; - if (dy < 0) + } + else if (dy < 0) { throw new IllegalArgumentException("Negative y size for the data! Data: " + ColumnFormat.toString(data)); } diff --git a/core/src/main/java/com/seibel/lod/core/datatype/full/FullDataDownSampler.java b/core/src/main/java/com/seibel/lod/core/datatype/full/FullDataDownSampler.java index 4e693c21f..105552b2e 100644 --- a/core/src/main/java/com/seibel/lod/core/datatype/full/FullDataDownSampler.java +++ b/core/src/main/java/com/seibel/lod/core/datatype/full/FullDataDownSampler.java @@ -77,7 +77,7 @@ public class FullDataDownSampler { return; } DhLodPos trgOffset = trgPos.getCorner(target.getDataDetail()); - DhLodPos srcOffset = srcPos.getSectionBBoxPos().convertUpwardsTo(target.getDataDetail()); + DhLodPos srcOffset = srcPos.getSectionBBoxPos().convertToDetailLevel(target.getDataDetail()); int offsetX = trgOffset.x - srcOffset.x; int offsetZ = trgOffset.z - srcOffset.z; LodUtil.assertTrue(offsetX >= 0 && offsetX < FullDataSource.SECTION_SIZE diff --git a/core/src/main/java/com/seibel/lod/core/datatype/full/FullDataSource.java b/core/src/main/java/com/seibel/lod/core/datatype/full/FullDataSource.java index a43c87fc1..f8e50062e 100644 --- a/core/src/main/java/com/seibel/lod/core/datatype/full/FullDataSource.java +++ b/core/src/main/java/com/seibel/lod/core/datatype/full/FullDataSource.java @@ -106,7 +106,7 @@ public class FullDataSource extends FullArrayView implements ILodDataSource if (data.x % chunkPerFull != 0 || data.z % chunkPerFull != 0) return; DhLodPos baseOffset = this.sectionPos.getCorner(this.getDataDetail()); - DhLodPos dataOffset = data.getBBoxLodPos().convertUpwardsTo(this.getDataDetail()); + DhLodPos dataOffset = data.getBBoxLodPos().convertToDetailLevel(this.getDataDetail()); int offsetX = dataOffset.x - baseOffset.x; int offsetZ = dataOffset.z - baseOffset.z; LodUtil.assertTrue(offsetX >= 0 && offsetX < SECTION_SIZE && offsetZ >= 0 && offsetZ < SECTION_SIZE); @@ -273,7 +273,7 @@ public class FullDataSource extends FullArrayView implements ILodDataSource else { // Count == 1 - DhLodPos subDataPos = lowerSectPos.getSectionBBoxPos().convertUpwardsTo(targetDataDetail); + DhLodPos subDataPos = lowerSectPos.getSectionBBoxPos().convertToDetailLevel(targetDataDetail); int dataOffsetX = subDataPos.x - minDataPos.x; int dataOffsetZ = subDataPos.z - minDataPos.z; LodUtil.assertTrue(dataOffsetX >= 0 && dataOffsetX < SECTION_SIZE && dataOffsetZ >= 0 && dataOffsetZ < SECTION_SIZE); diff --git a/core/src/main/java/com/seibel/lod/core/datatype/full/SpottyDataSource.java b/core/src/main/java/com/seibel/lod/core/datatype/full/SpottyDataSource.java index 8089172cd..e3a4e8187 100644 --- a/core/src/main/java/com/seibel/lod/core/datatype/full/SpottyDataSource.java +++ b/core/src/main/java/com/seibel/lod/core/datatype/full/SpottyDataSource.java @@ -58,7 +58,7 @@ public class SpottyDataSource extends FullArrayView implements IIncompleteDataSo if (data.x % chunkPerFull != 0 || data.z % chunkPerFull != 0) return; DhLodPos baseOffset = this.sectionPos.getCorner(this.getDataDetail()); - DhLodPos dataOffset = data.getBBoxLodPos().convertUpwardsTo(this.getDataDetail()); + DhLodPos dataOffset = data.getBBoxLodPos().convertToDetailLevel(this.getDataDetail()); int offsetX = dataOffset.x - baseOffset.x; int offsetZ = dataOffset.z - baseOffset.z; LodUtil.assertTrue(offsetX >= 0 && offsetX < SECTION_SIZE && offsetZ >= 0 && offsetZ < SECTION_SIZE); @@ -269,7 +269,7 @@ public class SpottyDataSource extends FullArrayView implements IIncompleteDataSo if (dataPos.x % lowerSectionsPerData != 0 || dataPos.z % lowerSectionsPerData != 0) return; DhLodPos basePos = this.sectionPos.getCorner(this.getDataDetail()); - dataPos = dataPos.convertUpwardsTo(this.getDataDetail()); + dataPos = dataPos.convertToDetailLevel(this.getDataDetail()); int offsetX = dataPos.x - basePos.x; int offsetZ = dataPos.z - basePos.z; SingleFullArrayView column = sparseSource.tryGet(0, 0); @@ -307,7 +307,7 @@ public class SpottyDataSource extends FullArrayView implements IIncompleteDataSo int lowerSectionsPerData = this.sectionPos.getWidth(dataPos.detailLevel).numberOfLodSectionsWide; if (dataPos.x % lowerSectionsPerData != 0 || dataPos.z % lowerSectionsPerData != 0) return; DhLodPos basePos = this.sectionPos.getCorner(this.getDataDetail()); - dataPos = dataPos.convertUpwardsTo(this.getDataDetail()); + dataPos = dataPos.convertToDetailLevel(this.getDataDetail()); int offsetX = dataPos.x - basePos.x; int offsetZ = dataPos.z - basePos.z; this.isColumnNotEmpty.set(offsetX * SECTION_SIZE + offsetZ, true); diff --git a/core/src/main/java/com/seibel/lod/core/file/datafile/DataFileHandler.java b/core/src/main/java/com/seibel/lod/core/file/datafile/DataFileHandler.java index 090f9fa5f..585c0526a 100644 --- a/core/src/main/java/com/seibel/lod/core/file/datafile/DataFileHandler.java +++ b/core/src/main/java/com/seibel/lod/core/file/datafile/DataFileHandler.java @@ -236,7 +236,7 @@ public class DataFileHandler implements IDataSourceProvider { { DhLodPos chunkPos = new DhLodPos((byte) (chunkData.dataDetail+4), chunkData.x, chunkData.z); LodUtil.assertTrue(chunkPos.overlaps(sectionPos.getSectionBBoxPos()), "Chunk {} does not overlap section {}", chunkPos, sectionPos); - chunkPos = chunkPos.convertUpwardsTo((byte) this.minDetailLevel); // TODO: Handle if chunkData has higher detail than lowestDetail. + chunkPos = chunkPos.convertToDetailLevel((byte) this.minDetailLevel); this.recursiveWrite(new DhSectionPos(chunkPos.detailLevel, chunkPos.x, chunkPos.z), chunkData); } private void recursiveWrite(DhSectionPos sectionPos, ChunkSizedData chunkData) diff --git a/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderFileHandler.java b/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderFileHandler.java index 4a30163bf..57645618f 100644 --- a/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderFileHandler.java +++ b/core/src/main/java/com/seibel/lod/core/file/renderfile/RenderFileHandler.java @@ -191,7 +191,7 @@ public class RenderFileHandler implements IRenderSourceProvider public void write(DhSectionPos sectionPos, ChunkSizedData chunkData) { // can be used for debugging - if (chunkData.getBBoxLodPos().convertUpwardsTo((byte)6).equals(new DhLodPos((byte)6, 10, -11))) + if (chunkData.getBBoxLodPos().convertToDetailLevel((byte)6).equals(new DhLodPos((byte)6, 10, -11))) { int doNothing = 0; } diff --git a/core/src/main/java/com/seibel/lod/core/generation/WorldGenerationQueue.java b/core/src/main/java/com/seibel/lod/core/generation/WorldGenerationQueue.java index d6819eb8e..73a6316a9 100644 --- a/core/src/main/java/com/seibel/lod/core/generation/WorldGenerationQueue.java +++ b/core/src/main/java/com/seibel/lod/core/generation/WorldGenerationQueue.java @@ -149,7 +149,7 @@ public class WorldGenerationQueue implements Closeable { // Too small of a chunk. We'll just over-size the generation. byte parentDetail = (byte) (this.minGranularity + requiredDataDetail); - DhLodPos parentPos = pos.convertUpwardsTo(parentDetail); + DhLodPos parentPos = pos.convertToDetailLevel(parentDetail); CompletableFuture future = new CompletableFuture<>(); this.looseTasks.add(new WorldGenTask(parentPos, requiredDataDetail, tracker, future)); if (this.closer != null) @@ -199,7 +199,7 @@ public class WorldGenerationQueue implements Closeable { // Obviously, only do so if we aren't at the maxGranularity already // Check for merging and upping the granularity DhLodPos corePos = target.pos; - DhLodPos parentPos = corePos.convertUpwardsTo((byte) (corePos.detailLevel + 1)); + DhLodPos parentPos = corePos.convertToDetailLevel((byte) (corePos.detailLevel + 1)); int targetChildId = target.pos.getChildIndexOfParent(); boolean allPassed = true; for (int i = 0; i < 4; i++) @@ -302,7 +302,7 @@ public class WorldGenerationQueue implements Closeable boolean didAnything = false; while (++granularity <= this.maxGranularity) { - group = this.taskGroups.get(task.pos.convertUpwardsTo((byte) (taskDataDetail + granularity))); + group = this.taskGroups.get(task.pos.convertToDetailLevel((byte) (taskDataDetail + granularity))); if (group != null && group.dataDetail == taskDataDetail) { // We can just append to the higher granularity group one diff --git a/core/src/main/java/com/seibel/lod/core/level/DhClientServerLevel.java b/core/src/main/java/com/seibel/lod/core/level/DhClientServerLevel.java index adf36e468..0b76ad2bc 100644 --- a/core/src/main/java/com/seibel/lod/core/level/DhClientServerLevel.java +++ b/core/src/main/java/com/seibel/lod/core/level/DhClientServerLevel.java @@ -118,7 +118,7 @@ public class DhClientServerLevel implements IDhClientLevel, IDhServerLevel private void saveWrites(ChunkSizedData data) { RenderState rs = this.renderState.get(); - DhLodPos pos = data.getBBoxLodPos().convertUpwardsTo(FullDataSource.SECTION_SIZE_OFFSET); + DhLodPos pos = data.getBBoxLodPos().convertToDetailLevel(FullDataSource.SECTION_SIZE_OFFSET); if (rs != null) { rs.renderFileHandler.write(new DhSectionPos(pos.detailLevel, pos.x, pos.z), data); diff --git a/core/src/main/java/com/seibel/lod/core/pos/DhChunkPos.java b/core/src/main/java/com/seibel/lod/core/pos/DhChunkPos.java index 1fe736caa..1e619ac31 100644 --- a/core/src/main/java/com/seibel/lod/core/pos/DhChunkPos.java +++ b/core/src/main/java/com/seibel/lod/core/pos/DhChunkPos.java @@ -89,18 +89,6 @@ public class DhChunkPos { return new DhBlockPos2D(x<<4, z<<4); } - @Deprecated - public int getRegionX() - { - return LevelPosUtil.convert(LodUtil.CHUNK_DETAIL_LEVEL, x, LodUtil.REGION_DETAIL_LEVEL); - } - - @Deprecated - public int getRegionZ() - { - return LevelPosUtil.convert(LodUtil.CHUNK_DETAIL_LEVEL, z, LodUtil.REGION_DETAIL_LEVEL); - } - public long getLong() { return toLong(x, z); } diff --git a/core/src/main/java/com/seibel/lod/core/pos/DhLodPos.java b/core/src/main/java/com/seibel/lod/core/pos/DhLodPos.java index 4d47fb45a..62d68503b 100644 --- a/core/src/main/java/com/seibel/lod/core/pos/DhLodPos.java +++ b/core/src/main/java/com/seibel/lod/core/pos/DhLodPos.java @@ -137,7 +137,7 @@ public class DhLodPos implements Comparable } DhLodPos lodPos = new DhLodPos(this.detailLevel, this.x, this.z); - lodPos = lodPos.convertUpwardsTo(sectionDetailLevel); + lodPos = lodPos.convertToDetailLevel(sectionDetailLevel); return new DhSectionPos(lodPos.detailLevel, lodPos.x, lodPos.z); } @@ -147,13 +147,21 @@ public class DhLodPos implements Comparable // methods // //=========// - /** Only works for newDetailLevel's that are greater than or equal to this position's detailLevel */ - public DhLodPos convertUpwardsTo(byte newDetailLevel) + /** Returns a new DhLodPos with the given detail level. */ + public DhLodPos convertToDetailLevel(byte newDetailLevel) { - LodUtil.assertTrue(newDetailLevel >= this.detailLevel); - return new DhLodPos(newDetailLevel, - Math.floorDiv(this.x, BitShiftUtil.powerOfTwo(newDetailLevel - this.detailLevel)), - Math.floorDiv(this.z, BitShiftUtil.powerOfTwo(newDetailLevel - this.detailLevel))); + if (newDetailLevel >= this.detailLevel) + { + return new DhLodPos(newDetailLevel, + Math.floorDiv(this.x, BitShiftUtil.powerOfTwo(newDetailLevel - this.detailLevel)), + Math.floorDiv(this.z, BitShiftUtil.powerOfTwo(newDetailLevel - this.detailLevel))); + } + else + { + return new DhLodPos(newDetailLevel, + this.x * BitShiftUtil.powerOfTwo(this.detailLevel - newDetailLevel), + this.z * BitShiftUtil.powerOfTwo(this.detailLevel - newDetailLevel)); + } } public boolean overlaps(DhLodPos other) @@ -165,11 +173,11 @@ public class DhLodPos implements Comparable if (this.detailLevel > other.detailLevel) { - return this.equals(other.convertUpwardsTo(this.detailLevel)); + return this.equals(other.convertToDetailLevel(this.detailLevel)); } else { - return other.equals(this.convertUpwardsTo(other.detailLevel)); + return other.equals(this.convertToDetailLevel(other.detailLevel)); } } diff --git a/core/src/main/java/com/seibel/lod/core/pos/DhSectionPos.java b/core/src/main/java/com/seibel/lod/core/pos/DhSectionPos.java index e5f3b1f4a..db1510e04 100644 --- a/core/src/main/java/com/seibel/lod/core/pos/DhSectionPos.java +++ b/core/src/main/java/com/seibel/lod/core/pos/DhSectionPos.java @@ -3,7 +3,6 @@ package com.seibel.lod.core.pos; import com.seibel.lod.core.enums.ELodDirection; import com.seibel.lod.core.util.BitShiftUtil; import com.seibel.lod.core.util.LodUtil; -import com.seibel.lod.core.util.MathUtil; import java.util.function.Consumer; @@ -50,7 +49,7 @@ public class DhSectionPos public DhSectionPos(DhBlockPos blockPos) { DhLodPos lodPos = new DhLodPos(LodUtil.BLOCK_DETAIL_LEVEL, blockPos.x, blockPos.z); - lodPos = lodPos.convertUpwardsTo(SECTION_BLOCK_DETAIL_LEVEL); + lodPos = lodPos.convertToDetailLevel(SECTION_BLOCK_DETAIL_LEVEL); this.sectionDetail = SECTION_BLOCK_DETAIL_LEVEL; this.sectionX = lodPos.x; @@ -60,7 +59,7 @@ public class DhSectionPos public DhSectionPos(DhChunkPos chunkPos) { DhLodPos lodPos = new DhLodPos(LodUtil.CHUNK_DETAIL_LEVEL, chunkPos.x, chunkPos.z); - lodPos = lodPos.convertUpwardsTo(SECTION_CHUNK_DETAIL_LEVEL); + lodPos = lodPos.convertToDetailLevel(SECTION_CHUNK_DETAIL_LEVEL); this.sectionDetail = SECTION_CHUNK_DETAIL_LEVEL; this.sectionX = lodPos.x; diff --git a/core/src/main/java/com/seibel/lod/core/util/LevelPosUtil.java b/core/src/main/java/com/seibel/lod/core/util/LevelPosUtil.java index e7eb95c5a..1105f375f 100644 --- a/core/src/main/java/com/seibel/lod/core/util/LevelPosUtil.java +++ b/core/src/main/java/com/seibel/lod/core/util/LevelPosUtil.java @@ -58,21 +58,6 @@ public class LevelPosUtil return new int[] { detailLevel, posX, posZ }; } - public static int convert(byte detailLevel, int pos, byte newDetailLevel) - { - int width; - if (newDetailLevel >= detailLevel) - { - width = 1 << (newDetailLevel - detailLevel); - return Math.floorDiv(pos, width); - } - else - { - width = 1 << (detailLevel - newDetailLevel); - return pos * width; - } - } - public static int getRegion(byte detailLevel, int pos) { return Math.floorDiv(pos, 1 << (LodUtil.REGION_DETAIL_LEVEL - detailLevel)); @@ -145,11 +130,6 @@ public class LevelPosUtil return Math.floorDiv(getPosZ(levelPos), width); } - public static int getChunkPos(byte detailLevel, int pos) - { - return convert(detailLevel, pos, LodUtil.CHUNK_DETAIL_LEVEL); - } - public static double centerDistance(byte detailLevel, int posX, int posZ, int playerPosX, int playerPosZ) { int width = 1 << detailLevel; double cPosX = posX * width + width/2.; diff --git a/core/src/main/java/com/seibel/lod/core/wrapperInterfaces/chunk/IChunkWrapper.java b/core/src/main/java/com/seibel/lod/core/wrapperInterfaces/chunk/IChunkWrapper.java index 712880311..e1b57d76c 100644 --- a/core/src/main/java/com/seibel/lod/core/wrapperInterfaces/chunk/IChunkWrapper.java +++ b/core/src/main/java/com/seibel/lod/core/wrapperInterfaces/chunk/IChunkWrapper.java @@ -40,15 +40,7 @@ public interface IChunkWrapper extends IBindable // FIXME: getHeightMapValue & getMaxY is the same! Which one to keep? int getHeightMapValue(int xRel, int zRel); int getMaxY(int x, int z); - - @Deprecated - int getChunkPosX(); - @Deprecated - int getChunkPosZ(); - @Deprecated - int getRegionPosX(); - @Deprecated - int getRegionPosZ(); + int getMaxX(); int getMaxZ(); int getMinX();