some refactor

This commit is contained in:
mlbv 2025-06-11 10:18:21 +08:00
parent f2991e4518
commit 79dcb69ed2
3 changed files with 27 additions and 40 deletions

View File

@ -20,8 +20,11 @@ import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.DoubleAdder;
import static com.hbm.util.SubChunkSnapshot.getSnapshot;
/**
* Threaded DDA raytracer for the nuke explosion.
*
* @author mlbv
*/
public class ExplosionNukeRayParallelized implements IExplosionRay {
private static final int WORLD_HEIGHT = 256;
@ -114,10 +117,7 @@ public class ExplosionNukeRayParallelized implements IExplosionRay {
while (System.nanoTime() < deadline) {
ChunkKey ck = cacheQueue.poll();
if (ck == null) break;
snapshots.computeIfAbsent(ck, key -> {
SubChunkSnapshot snap = getSnapshot(this.world, key.pos, key.subY);
return snap == null ? SubChunkSnapshot.EMPTY : snap;
});
snapshots.computeIfAbsent(ck, k -> SubChunkSnapshot.getSnapshot(world, k, BombConfig.chunkloading));
}
}
@ -188,7 +188,7 @@ public class ExplosionNukeRayParallelized implements IExplosionRay {
if (bs.isEmpty()) {
destructionMap.remove(cp);
for (int sy = 0; sy < (WORLD_HEIGHT >> 4); sy++) {
snapshots.remove(new ChunkKey(cp.chunkXPos, cp.chunkZPos, sy));
snapshots.remove(new ChunkKey(cp, sy));
}
it.remove();
}
@ -244,7 +244,7 @@ public class ExplosionNukeRayParallelized implements IExplosionRay {
private List<Vec3> generateSphereRays(int count) {
List<Vec3> list = new ArrayList<>(count);
if (count <= 0) return list;
if (count == 0) return list;
if (count == 1) {
list.add(Vec3.createVectorHelper(1, 0, 0).normalize());
return list;
@ -293,7 +293,7 @@ public class ExplosionNukeRayParallelized implements IExplosionRay {
continue;
}
ChunkKey snapshotKey = new ChunkKey(cp.chunkXPos, cp.chunkZPos, subY);
ChunkKey snapshotKey = new ChunkKey(cp, subY);
SubChunkSnapshot snap = snapshots.get(snapshotKey);
Block originalBlock;

View File

@ -6,7 +6,7 @@ import java.util.Objects;
/**
* Unique identifier for sub-chunks.
* @Author mlbv
* @author mlbv
*/
public class ChunkKey {
public final ChunkCoordIntPair pos;
@ -17,6 +17,11 @@ public class ChunkKey {
this.subY = sy;
}
public ChunkKey(ChunkCoordIntPair pos, int sy) {
this.pos = pos;
this.subY = sy;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -2,7 +2,6 @@ package com.hbm.util;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
@ -14,7 +13,7 @@ import java.util.Map;
/**
* A snapshot of a 16×16×16 sub-chunk.
* @Author mlbv
* @author mlbv
*/
public class SubChunkSnapshot {
/**
@ -29,42 +28,25 @@ public class SubChunkSnapshot {
this.data = d;
}
/**
* Creates a SubChunkSnapshot from a loaded chunk.
*
* @param world
* The World instance from which to retrieve the chunk.
* @param cpos
* The ChunkCoordIntPair identifying the chunk coordinates (x, z).
* @param subY
* The vertical sub-chunk index (015) within the chunk.
* @return
* A SubChunkSnapshot containing the palette and block data for the sub-chunk,
* or SubChunkSnapshot.EMPTY if the region is unloaded or contains only air.
*/
public static SubChunkSnapshot getSnapshot(World world, ChunkCoordIntPair cpos, int subY) {
if (!world.getChunkProvider().chunkExists(cpos.chunkXPos, cpos.chunkZPos)) {
return SubChunkSnapshot.EMPTY;
}
return getOrLoadSnapshot(world, cpos, subY);
}
/**
* Creates a SubChunkSnapshot.
*
* @param world
* The World instance from which to retrieve the chunk.
* @param cpos
* The ChunkCoordIntPair identifying the chunk coordinates (x, z).
* @param subY
* The vertical sub-chunk index (015) within the chunk.
* @param key
* The ChunkKey identifying the sub-chunk.
* @param allowGeneration
* Whether to generate chunks. If false, attempting to retrieve a snapshot of a chunk that does not exist will return {@link SubChunkSnapshot#EMPTY}.
* @return
* A SubChunkSnapshot containing the palette and block data for the sub-chunk,
* or SubChunkSnapshot.EMPTY if the region contains only air.
* or {@link SubChunkSnapshot#EMPTY} if the region contains only air.
*/
public static SubChunkSnapshot getOrLoadSnapshot(World world, ChunkCoordIntPair cpos, int subY){
Chunk chunk = world.getChunkFromChunkCoords(cpos.chunkXPos, cpos.chunkZPos);
ExtendedBlockStorage ebs = chunk.getBlockStorageArray()[subY];
public static SubChunkSnapshot getSnapshot(World world, ChunkKey key, boolean allowGeneration){
if (!world.getChunkProvider().chunkExists(key.pos.chunkXPos, key.pos.chunkZPos) && !allowGeneration) {
return SubChunkSnapshot.EMPTY;
}
Chunk chunk = world.getChunkProvider().provideChunk(key.pos.chunkXPos, key.pos.chunkZPos);
ExtendedBlockStorage ebs = chunk.getBlockStorageArray()[key.subY];
if (ebs == null || ebs.isEmpty()) return SubChunkSnapshot.EMPTY;
short[] data = new short[16 * 16 * 16];