Skip to content

Commit

Permalink
fix: only chunks that are in tick radius should be ticked
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Dec 17, 2024
1 parent 59ffa84 commit 100db5a
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
@RequiredArgsConstructor
public final class AllayChunkService implements ChunkService {

private static final int TICK_RADIUS_SQUARED = (int) Math.pow(Server.SETTINGS.worldSettings().tickRadius(), 2);

private final Map<Long, Chunk> loadedChunks = new Long2ObjectNonBlockingMap<>();
private final Map<Long, CompletableFuture<Chunk>> loadingChunks = new Long2ObjectNonBlockingMap<>();
private final Map<ChunkLoader, ChunkLoaderManager> chunkLoaderManagers = new Object2ObjectOpenHashMap<>();
Expand All @@ -69,6 +71,10 @@ public void tick(long currentTick) {

private void tickChunks(long currentTick) {
for (Chunk chunk : loadedChunks.values()) {
if (!shouldTickChunk(chunk)) {
continue;
}

try {
((AllayChunk) chunk).tick(currentTick, dimension, worldStorage);
} catch (Throwable t) {
Expand All @@ -77,6 +83,21 @@ private void tickChunks(long currentTick) {
}
}

private boolean shouldTickChunk(Chunk chunk) {
var cx = chunk.getX();
var cz = chunk.getZ();
var shouldTick = false;
for (var chunkLoader : chunk.getChunkLoaders()) {
var lcx = ((int) Math.floor(chunkLoader.getLocation().x())) >> 4;
var lcz = ((int) Math.floor(chunkLoader.getLocation().z())) >> 4;
if (Math.pow(lcx - cx, 2) + Math.pow(lcz - cz, 2) <= TICK_RADIUS_SQUARED) {
shouldTick = true;
break;
}
}
return shouldTick;
}

private void sendChunkPackets() {
loadedChunks.values().forEach(Chunk::sendChunkPackets);
}
Expand Down

0 comments on commit 100db5a

Please sign in to comment.