Clean up SectionPos wording for chunk/block pos converters

This commit is contained in:
James Seibel
2024-07-21 07:35:35 -05:00
parent de7d22766a
commit 589340f2db
5 changed files with 42 additions and 82 deletions
@@ -62,6 +62,8 @@ public class FullDataSourceV2 implements IDataSource<IDhLevel>
/** measured in data columns */
public static final int WIDTH = 64;
/** how many chunks wide this datasource is. */
public static final int NUMB_OF_CHUNKS_WIDE = WIDTH / LodUtil.CHUNK_WIDTH;
public static final byte DATA_FORMAT_VERSION = 1;
@@ -46,8 +46,6 @@ public class LodDataBuilder
{
private static final Logger LOGGER = DhLoggerBuilder.getLogger();
private static final IBlockStateWrapper AIR = SingletonInjector.INSTANCE.get(IWrapperFactory.class).getAirBlockStateWrapper();
/** how many chunks wide the {@link FullDataSourceV2} is. */
private static final int NUMB_OF_CHUNKS_WIDE = FullDataSourceV2.WIDTH / LodUtil.CHUNK_WIDTH;
private static boolean getTopErrorLogged = false;
@@ -93,30 +91,30 @@ public class LodDataBuilder
// -3 -> 1
// -4 -> 0 ---
// -5 -> 3
chunkOffsetX = ((chunkOffsetX) % NUMB_OF_CHUNKS_WIDE);
chunkOffsetX = ((chunkOffsetX) % FullDataSourceV2.NUMB_OF_CHUNKS_WIDE);
if (chunkOffsetX != 0)
{
chunkOffsetX += NUMB_OF_CHUNKS_WIDE;
chunkOffsetX += FullDataSourceV2.NUMB_OF_CHUNKS_WIDE;
}
}
else
{
chunkOffsetX %= NUMB_OF_CHUNKS_WIDE;
chunkOffsetX %= FullDataSourceV2.NUMB_OF_CHUNKS_WIDE;
}
chunkOffsetX *= LodUtil.CHUNK_WIDTH;
int chunkOffsetZ = chunkWrapper.getChunkPos().z;
if (chunkWrapper.getChunkPos().z < 0)
{
chunkOffsetZ = ((chunkOffsetZ) % NUMB_OF_CHUNKS_WIDE);
chunkOffsetZ = ((chunkOffsetZ) % FullDataSourceV2.NUMB_OF_CHUNKS_WIDE);
if (chunkOffsetZ != 0)
{
chunkOffsetZ += NUMB_OF_CHUNKS_WIDE;
chunkOffsetZ += FullDataSourceV2.NUMB_OF_CHUNKS_WIDE;
}
}
else
{
chunkOffsetZ %= NUMB_OF_CHUNKS_WIDE;
chunkOffsetZ %= FullDataSourceV2.NUMB_OF_CHUNKS_WIDE;
}
chunkOffsetZ *= LodUtil.CHUNK_WIDTH;
@@ -366,7 +364,7 @@ public class LodDataBuilder
// get the section position
int sectionPos = chunkXOrZPos;
// negative positions start at -1 so the logic there is slightly different
sectionPos = (sectionPos < 0) ? ((sectionPos + 1) / NUMB_OF_CHUNKS_WIDE) - 1 : (sectionPos / NUMB_OF_CHUNKS_WIDE);
sectionPos = (sectionPos < 0) ? ((sectionPos + 1) / FullDataSourceV2.NUMB_OF_CHUNKS_WIDE) - 1 : (sectionPos / FullDataSourceV2.NUMB_OF_CHUNKS_WIDE);
return sectionPos;
}
@@ -190,7 +190,7 @@ public class SubDimensionLevelMatcher implements AutoCloseable
}
FullDataSourceV2 newChunkSizedFullDataView = FullDataSourceV2.createFromChunk(newlyLoadedChunk);
// convert to a data source for easier comparing
FullDataSourceV2 newDataSource = FullDataSourceV2.createEmpty(DhSectionPos.encodeLodPos(this.playerData.playerBlockPos));
FullDataSourceV2 newDataSource = FullDataSourceV2.createEmpty(DhSectionPos.encodeContainingPos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, this.playerData.playerBlockPos));
newDataSource.update(newChunkSizedFullDataView);
@@ -215,7 +215,7 @@ public class SubDimensionLevelMatcher implements AutoCloseable
// get the data source to compare against
try (IDhLevel tempLevel = new DhClientLevel(new ClientOnlySaveStructure(), this.currentClientLevel, testLevelFolder, false))
{
testFullDataSource = tempLevel.getFullDataProvider().getAsync(DhSectionPos.encodeLodPos(this.playerData.playerBlockPos)).join();
testFullDataSource = tempLevel.getFullDataProvider().getAsync(DhSectionPos.encodeContainingPos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, this.playerData.playerBlockPos)).join();
if (testFullDataSource == null)
{
continue;
@@ -19,6 +19,7 @@
package com.seibel.distanthorizons.core.pos;
import com.seibel.distanthorizons.core.dataObjects.fullData.sources.FullDataSourceV2;
import com.seibel.distanthorizons.core.enums.EDhDirection;
import com.seibel.distanthorizons.core.util.LodUtil;
import com.seibel.distanthorizons.coreapi.util.BitShiftUtil;
@@ -101,26 +102,33 @@ public class DhSectionPos
return data;
}
// TODO need changing
public static long encodeLodPos(DhBlockPos pos) { return encodeLodBlockPos(pos.x, pos.z); }
public static long encodeLodPos(DhBlockPos2D pos) { return encodeLodBlockPos(pos.x, pos.z); }
public static long encodeLodBlockPos(int blockX, int blockZ)
/** Returns the section pos at the requested detail level containing the given BlockPos */
public static long encodeContainingPos(byte outputSectionDetailLevel, DhBlockPos pos)
{
long pos = encodePos(LodUtil.BLOCK_DETAIL_LEVEL, blockX, blockZ);
pos = convertToDetailLevel(pos, DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL);
return pos;
int sectionPosX = getXOrZSectionPosFromChunkOrBlockPos(pos.x, false);
int sectionPosZ = getXOrZSectionPosFromChunkOrBlockPos(pos.z, false);
long blockPos = DhSectionPos.encodePos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, sectionPosX, sectionPosZ);
return convertToDetailLevel(blockPos, outputSectionDetailLevel);
}
public static long encodeLodPos(DhChunkPos pos) { return encodeLodChunkPos(pos.x, pos.z); }
public static long encodeLodChunkPos(int chunkX, int chunkZ) // TODO broken?
/** Returns the section pos at the requested detail level containing the given ChunkPos */
public static long encodeContainingPos(byte outputSectionDetailLevel, DhChunkPos pos)
{
long pos = encodePos(LodUtil.CHUNK_DETAIL_LEVEL, chunkX, chunkZ);
pos = convertToDetailLevel(pos, DhSectionPos.SECTION_CHUNK_DETAIL_LEVEL);
return pos;
int sectionPosX = getXOrZSectionPosFromChunkOrBlockPos(pos.x, true);
int sectionPosZ = getXOrZSectionPosFromChunkOrBlockPos(pos.z, true);
long blockPos = DhSectionPos.encodePos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, sectionPosX, sectionPosZ);
return convertToDetailLevel(blockPos, outputSectionDetailLevel);
}
private static int getXOrZSectionPosFromChunkOrBlockPos(int chunkXOrZPos, boolean isChunkPos)
{
int sectionPos = chunkXOrZPos;
int fullDataSourceWidth = isChunkPos ? FullDataSourceV2.NUMB_OF_CHUNKS_WIDE : (FullDataSourceV2.NUMB_OF_CHUNKS_WIDE * LodUtil.CHUNK_WIDTH);
// negative positions start at -1 so the logic there is slightly different
sectionPos = (sectionPos < 0)
? ((sectionPos + 1) / fullDataSourceWidth) - 1
: (sectionPos / fullDataSourceWidth);
return sectionPos;
}
public static long encodePos(DhChunkPos pos) { return encodeChunkPos(pos.x, pos.z); }
public static long encodeChunkPos(int chunkX, int chunkZ) { return encodePos(LodUtil.CHUNK_DETAIL_LEVEL, chunkX, chunkZ); }
+7 -55
View File
@@ -192,41 +192,19 @@ public class DhSectionPosTest
// origin pos //
DhBlockPos originBlockPos = new DhBlockPos(0, 0, 0);
long originsectionPos = DhSectionPos.encodeLodPos(originBlockPos);
assertSectionPosEqual(DhSectionPos.encodePos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, 0, 0), originsectionPos);
long originSectionPos = DhSectionPos.encodeContainingPos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, originBlockPos);
assertSectionPosEqual(DhSectionPos.encodePos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, 0, 0), originSectionPos);
// offset pos //
long offsetSectionPos;
DhBlockPos offsetBlockPos = new DhBlockPos(1000, 0, 42000);
offsetSectionPos = DhSectionPos.encodeLodPos(offsetBlockPos);
offsetSectionPos = DhSectionPos.encodeContainingPos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, offsetBlockPos);
assertSectionPosEqual(DhSectionPos.encodePos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, 15, 656), offsetSectionPos);
offsetBlockPos = new DhBlockPos(-987654, 0, 46);
offsetSectionPos = DhSectionPos.encodeLodPos(offsetBlockPos);
assertSectionPosEqual(DhSectionPos.encodePos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, -15433, 0), offsetSectionPos);
}
@Test
public void createFromBlockPos2D()
{
// origin pos //
DhBlockPos2D originBlockPos = new DhBlockPos2D(0, 0);
long originSectionPos = DhSectionPos.encodeLodPos(originBlockPos);
assertSectionPosEqual(DhSectionPos.encodePos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, 0, 0), originSectionPos);
// offset pos //
DhBlockPos2D offsetBlockPos = new DhBlockPos2D(1000, 42000);
long offsetSectionPos = DhSectionPos.encodeLodPos(offsetBlockPos);
assertSectionPosEqual(DhSectionPos.encodePos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, 15, 656), offsetSectionPos);
offsetBlockPos = new DhBlockPos2D(-987654, 46);
offsetSectionPos = DhSectionPos.encodeLodPos(offsetBlockPos);
offsetSectionPos = DhSectionPos.encodeContainingPos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, offsetBlockPos);
assertSectionPosEqual(DhSectionPos.encodePos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, -15433, 0), offsetSectionPos);
}
@@ -237,48 +215,22 @@ public class DhSectionPosTest
// origin pos //
DhChunkPos originChunkPos = new DhChunkPos(0,0);
long originSectionPos = DhSectionPos.encodeLodPos(originChunkPos);
long originSectionPos = DhSectionPos.encodeContainingPos(DhSectionPos.SECTION_CHUNK_DETAIL_LEVEL, originChunkPos);
assertSectionPosEqual(DhSectionPos.encodePos(DhSectionPos.SECTION_CHUNK_DETAIL_LEVEL, 0, 0), originSectionPos);
// offset pos //
DhChunkPos offsetChunkPos = new DhChunkPos(1000, 42000);
long offsetSectionPos = DhSectionPos.encodeLodPos(offsetChunkPos);
long offsetSectionPos = DhSectionPos.encodeContainingPos(DhSectionPos.SECTION_CHUNK_DETAIL_LEVEL, offsetChunkPos);
assertSectionPosEqual(DhSectionPos.encodePos(DhSectionPos.SECTION_CHUNK_DETAIL_LEVEL, 15, 656), offsetSectionPos);
offsetChunkPos = new DhChunkPos(-987654, 46);
offsetSectionPos = DhSectionPos.encodeLodPos(offsetChunkPos);
offsetSectionPos = DhSectionPos.encodeContainingPos(DhSectionPos.SECTION_CHUNK_DETAIL_LEVEL, offsetChunkPos);
assertSectionPosEqual(DhSectionPos.encodePos(DhSectionPos.SECTION_CHUNK_DETAIL_LEVEL, -15433, 0), offsetSectionPos);
}
@Test
public void createFromChunkPos_test()
{
// origin pos //
DhChunkPos originChunkPos = new DhChunkPos(0,0);
long originSectionPos = DhSectionPos.encodePos(originChunkPos);
assertSectionPosEqual(DhSectionPos.encodePos(LodUtil.CHUNK_DETAIL_LEVEL, 0, 0), originSectionPos);
// offset pos //
DhChunkPos offsetChunkPos = new DhChunkPos(1, 1);
long offsetSectionPos = DhSectionPos.encodePos(offsetChunkPos);
assertSectionPosEqual(DhSectionPos.encodePos(LodUtil.CHUNK_DETAIL_LEVEL, 1, 1), offsetSectionPos);
offsetChunkPos = new DhChunkPos(2, 2);
offsetSectionPos = DhSectionPos.encodePos(offsetChunkPos);
assertSectionPosEqual(DhSectionPos.encodePos(LodUtil.CHUNK_DETAIL_LEVEL, 2, 2), offsetSectionPos);
offsetChunkPos = new DhChunkPos(-3, -2);
offsetSectionPos = DhSectionPos.encodePos(offsetChunkPos);
assertSectionPosEqual(DhSectionPos.encodePos(LodUtil.CHUNK_DETAIL_LEVEL, -3, -2), offsetSectionPos);
}
@Test
public void convertToDetailLevel()
{