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

View File

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

View File

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