Skip to content

Commit

Permalink
chore: simplify trim logic
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreSchwang committed Sep 1, 2024
1 parent c132b95 commit d4ca4f9
Showing 1 changed file with 28 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,45 +65,38 @@ default boolean trimY(IChunkSet set, int minY, int maxY, final boolean keepInsid
int minLayer = minY >> 4;
int maxLayer = maxY >> 4;
if (keepInsideRange) {
for (int layer = set.getMinSectionPosition(); layer <= minLayer; layer++) {
if (set.hasSection(layer)) {
if (layer == minLayer) {
char[] arr = set.loadIfPresent(layer);
if (arr != null) {
int index = (minY & 15) << 8;
for (int i = 0; i < index; i++) {
arr[i] = BlockTypesCache.ReservedIDs.__RESERVED__;
}
set.setBlocks(layer, arr);
}
} else {
set.setBlocks(layer, null);
for (int layer = set.getMinSectionPosition(); layer <= set.getMaxSectionPosition(); layer++) {
if (!set.hasSection(layer)) {
continue;
}
// wipe all data from chunk layers above or below the max / min layer
if (layer < minLayer || layer > maxLayer) {
set.setBlocks(layer, null);
continue;
}
// if chunk layer / section is fully enclosed by minY to maxY, keep as is
if (layer > minLayer && layer < maxLayer) {
continue;
}
char[] blocks = set.loadIfPresent(layer);
if (blocks == null) {
continue;
}
// When on the minimum layer (as defined by minY), remove blocks up to minY (exclusive)
if (layer == minLayer) {
int index = (minY & 15) << 8;
for (int i = 0; i < index; i++) {
blocks[i] = BlockTypesCache.ReservedIDs.__RESERVED__;
}
}
}
for (int layer = maxLayer; layer <= set.getMaxSectionPosition(); layer++) {
if (set.hasSection(layer)) {
if (layer == maxLayer) {
char[] arr = set.loadIfPresent(layer);
if (arr != null) {
/*
If maxY is the last coordinate in a chunk section (`2^n-1`), it bleeds into
the next higher section and represents the 0th block on the y-axis on that section. This results
in the index being 0 (so the whole section is wiped) - even though only the last slice should be
overwritten.
If maxY is any other coordinate (0 - 14) `((maxY + 1) & 15) << 8` is evaluated, resulting in
getting the index for the next (upper) slice of this section (as this one should be overwritten)
*/
int index = (maxY + 1) % 16 == 0 ? arr.length : ((maxY + 1) & 15) << 8;
for (int i = index; i < arr.length; i++) {
arr[i] = BlockTypesCache.ReservedIDs.__RESERVED__;
}
set.setBlocks(layer, arr);
}
} else {
set.setBlocks(layer, null);
// When on the maximum layer (as defined by maxY), remove blocks above maxY (exclusive)
if (layer == maxLayer) {
int index = ((maxY & 15) + 1) << 8;
for (int i = index; i < blocks.length; i++) {
blocks[i] = BlockTypesCache.ReservedIDs.__RESERVED__;
}
}
set.setBlocks(layer, blocks);
}
try {
int layer = (minY - 15) >> 4;
Expand Down

0 comments on commit d4ca4f9

Please sign in to comment.