Fix some inconsistencies between getting and setting LOD chunks

also improve variable naming in a few places
This commit is contained in:
James Seibel
2021-01-22 08:47:09 -06:00
parent 8e0700bbf9
commit b044ebc520
3 changed files with 20 additions and 25 deletions
@@ -167,6 +167,7 @@ public class LoadedRegions
if (xIndex < 0 || xIndex >= width || zIndex < 0 || zIndex >= width)
// out of range
// TODO, should this throw an exception?
return;
regions[xIndex][zIndex] = newRegion;
@@ -179,29 +180,28 @@ public class LoadedRegions
public void addLod(LodChunk lod)
{
int x = lod.x / 16;
int z = lod.z / 16;
int regionX = (lod.x + centerX) / LodRegion.SIZE;
int regionZ = (lod.z + centerZ) / LodRegion.SIZE;
// prevent issues if X/Z is negative and less than 16
if (lod.x < 0)
{
x = (Math.abs(x) * -1) - 1;
regionX = (Math.abs(regionX) * -1) - 1;
}
if (lod.z < 0)
{
z = (Math.abs(z) * -1) - 1;
regionZ = (Math.abs(regionZ) * -1) - 1;
}
LodRegion region = getRegion(x, z);
LodRegion region = getRegion(regionX, regionZ);
if (region == null)
{
// if no region exists, create it
region = new LodRegion(x, z);
region = new LodRegion(regionX, regionZ);
setRegion(region);
}
// TODO check what should be happening here
region.addLod(lod);
}
@@ -209,27 +209,23 @@ public class LoadedRegions
/**
* Returns null if the LodChunk isn't loaded
*/
public LodChunk getChunkFromCoordinates(int chunkX, int chunkZ)
public LodChunk getLodFromCoordinates(int chunkX, int chunkZ)
{
// (chunkX + centerX) % width
int xIndex = (chunkX + centerX) / LodRegion.SIZE;
int zIndex = (chunkZ + centerZ) / LodRegion.SIZE;
int regionX = (chunkX + centerX) / LodRegion.SIZE;
int regionZ = (chunkZ + centerZ) / LodRegion.SIZE;
// prevent issues if chunkX/Z is negative and less than width
if (chunkX < 0)
{
xIndex = (Math.abs(xIndex) * -1) - 1;
regionX = (Math.abs(regionX) * -1) - 1;
}
if (chunkZ < 0)
{
zIndex = (Math.abs(zIndex) * -1) - 1;
regionZ = (Math.abs(regionZ) * -1) - 1;
}
LodRegion region = getRegion(xIndex, zIndex);
// TODO should abs be used here?
//if(chunkX < 0 || chunkZ < 0)
// return null;
LodRegion region = getRegion(regionX, regionZ);
if(region == null)
return null;
@@ -37,8 +37,8 @@ public class LodRegion
// the region will negative first, therefore we don't have to
// store the LOD chunks at negative indexes since we search
// LOD the region first
int xIndex = Math.abs(lod.x % LodChunk.WIDTH);
int zIndex = Math.abs(lod.z % LodChunk.WIDTH);
int xIndex = Math.abs(lod.x % SIZE);
int zIndex = Math.abs(lod.z % SIZE);
chunks[xIndex][zIndex] = lod;
}
@@ -47,8 +47,8 @@ public class LodRegion
public LodChunk getLod(int x, int z)
{
// since we add LOD's with ABS, we get them the same way
x = Math.abs(x);
z = Math.abs(z);
x = Math.abs(x % SIZE);
z = Math.abs(z % SIZE);
if(x >= SIZE || z >= SIZE)
return null;
@@ -149,10 +149,10 @@ public class LodRenderer
startX; // offset so the center LOD block is centered underneath the player
double zOffset = -cameraZ + (LOD_WIDTH * j) + startZ;
int chunkX = ((LOD_WIDTH * i) + startX) / MINECRAFT_CHUNK_WIDTH;
int chunkZ = ((LOD_WIDTH * j) + startZ) / MINECRAFT_CHUNK_WIDTH;
int chunkX = i + (startX / MINECRAFT_CHUNK_WIDTH);
int chunkZ = j + (startZ / MINECRAFT_CHUNK_WIDTH);
LodChunk lod = regions.getChunkFromCoordinates(chunkX, chunkZ);
LodChunk lod = regions.getLodFromCoordinates(chunkX, chunkZ);
if (lod == null)
{
@@ -165,7 +165,6 @@ public class LodRenderer
double yOffset = -cameraY;
// if debugging draw the squares as a black and white checker board
if (debugging)
{