Add ChunkUtils#getChunkCount

This commit is contained in:
themode 2022-02-13 10:27:24 +01:00
parent 4459bbeebd
commit 84d154b8db
4 changed files with 44 additions and 23 deletions

View File

@ -142,9 +142,11 @@ final class EntityTrackerImpl implements EntityTracker {
var rangeRef = entry.references.computeIfAbsent(range, integer -> Long2ObjectSyncMap.hashmap()); var rangeRef = entry.references.computeIfAbsent(range, integer -> Long2ObjectSyncMap.hashmap());
return rangeRef.computeIfAbsent(ChunkUtils.getChunkIndex(chunkX, chunkZ), return rangeRef.computeIfAbsent(ChunkUtils.getChunkIndex(chunkX, chunkZ),
chunkIndex -> { chunkIndex -> {
List<List<T>> entities = new ArrayList<>(); final int count = ChunkUtils.getChunkCount(range);
List<List<T>> entities = new ArrayList<>(count);
ChunkUtils.forChunksInRange(ChunkUtils.getChunkCoordX(chunkIndex), ChunkUtils.getChunkCoordZ(chunkIndex), range, ChunkUtils.forChunksInRange(ChunkUtils.getChunkCoordX(chunkIndex), ChunkUtils.getChunkCoordZ(chunkIndex), range,
(x, z) -> entities.add(entry.chunkEntities.computeIfAbsent(getChunkIndex(x, z), i -> (List<T>) LIST_SUPPLIER.get()))); (x, z) -> entities.add(entry.chunkEntities.computeIfAbsent(getChunkIndex(x, z), i -> (List<T>) LIST_SUPPLIER.get())));
assert entities.size() == count : "Expected " + count + " chunks, got " + entities.size();
return List.copyOf(entities); return List.copyOf(entities);
}); });
} }

View File

@ -143,6 +143,14 @@ public final class ChunkUtils {
return (int) index; return (int) index;
} }
public static int getChunkCount(int range) {
if (range < 0) {
throw new IllegalArgumentException("Range cannot be negative");
}
final int square = range * 2 + 1;
return square * square;
}
public static void forDifferingChunksInRange(int newChunkX, int newChunkZ, public static void forDifferingChunksInRange(int newChunkX, int newChunkZ,
int oldChunkX, int oldChunkZ, int oldChunkX, int oldChunkZ,
int range, @NotNull IntegerBiConsumer callback) { int range, @NotNull IntegerBiConsumer callback) {

View File

@ -1,39 +1,50 @@
package net.minestom.server.coordinate; package net.minestom.server.coordinate;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.chunk.ChunkUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static net.minestom.server.utils.chunk.ChunkUtils.*;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class CoordinateTest { public class CoordinateTest {
@Test @Test
public void chunkIndex() { public void chunkIndex() {
var index = ChunkUtils.getChunkIndex(2, 5); var index = getChunkIndex(2, 5);
assertEquals(2, ChunkUtils.getChunkCoordX(index)); assertEquals(2, getChunkCoordX(index));
assertEquals(5, ChunkUtils.getChunkCoordZ(index)); assertEquals(5, getChunkCoordZ(index));
index = ChunkUtils.getChunkIndex(-5, 25); index = getChunkIndex(-5, 25);
assertEquals(-5, ChunkUtils.getChunkCoordX(index)); assertEquals(-5, getChunkCoordX(index));
assertEquals(25, ChunkUtils.getChunkCoordZ(index)); assertEquals(25, getChunkCoordZ(index));
index = ChunkUtils.getChunkIndex(Integer.MAX_VALUE, Integer.MIN_VALUE); index = getChunkIndex(Integer.MAX_VALUE, Integer.MIN_VALUE);
assertEquals(Integer.MAX_VALUE, ChunkUtils.getChunkCoordX(index)); assertEquals(Integer.MAX_VALUE, getChunkCoordX(index));
assertEquals(Integer.MIN_VALUE, ChunkUtils.getChunkCoordZ(index)); assertEquals(Integer.MIN_VALUE, getChunkCoordZ(index));
} }
@Test @Test
public void chunkCoordinate() { public void chunkCoordinate() {
assertEquals(1, ChunkUtils.getChunkCoordinate(16)); assertEquals(1, getChunkCoordinate(16));
assertEquals(-1, ChunkUtils.getChunkCoordinate(-16)); assertEquals(-1, getChunkCoordinate(-16));
assertEquals(3, ChunkUtils.getChunkCoordinate(48)); assertEquals(3, getChunkCoordinate(48));
assertEquals(4, ChunkUtils.getChunkCoordinate(65)); assertEquals(4, getChunkCoordinate(65));
assertEquals(4, ChunkUtils.getChunkCoordinate(64)); assertEquals(4, getChunkCoordinate(64));
assertEquals(3, ChunkUtils.getChunkCoordinate(63)); assertEquals(3, getChunkCoordinate(63));
assertEquals(-2, ChunkUtils.getChunkCoordinate(-25)); assertEquals(-2, getChunkCoordinate(-25));
assertEquals(23, ChunkUtils.getChunkCoordinate(380)); assertEquals(23, getChunkCoordinate(380));
}
@Test
public void chunkCount() {
assertEquals(289, getChunkCount(8));
assertEquals(169, getChunkCount(6));
assertEquals(121, getChunkCount(5));
assertEquals(9, getChunkCount(1));
assertEquals(1, getChunkCount(0));
assertThrows(IllegalArgumentException.class, () -> getChunkCount(-1));
} }
@Test @Test

View File

@ -5,6 +5,7 @@ import net.minestom.server.api.Env;
import net.minestom.server.api.EnvTest; import net.minestom.server.api.EnvTest;
import net.minestom.server.coordinate.Pos; import net.minestom.server.coordinate.Pos;
import net.minestom.server.network.packet.server.play.ChunkDataPacket; import net.minestom.server.network.packet.server.play.ChunkDataPacket;
import net.minestom.server.utils.chunk.ChunkUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
@ -36,7 +37,7 @@ public class ChunkViewerIntegrationTest {
@Test @Test
public void renderDistance(Env env) { public void renderDistance(Env env) {
final int viewRadius = MinecraftServer.getChunkViewDistance(); final int viewRadius = MinecraftServer.getChunkViewDistance();
final int viewLength = 1 + viewRadius * 2; final int count = ChunkUtils.getChunkCount(viewRadius);
var instance = env.createFlatInstance(); var instance = env.createFlatInstance();
var connection = env.createConnection(); var connection = env.createConnection();
// Check initial load // Check initial load
@ -45,8 +46,7 @@ public class ChunkViewerIntegrationTest {
var player = connection.connect(instance, new Pos(0, 40, 0)).join(); var player = connection.connect(instance, new Pos(0, 40, 0)).join();
assertEquals(instance, player.getInstance()); assertEquals(instance, player.getInstance());
assertEquals(new Pos(0, 40, 0), player.getPosition()); assertEquals(new Pos(0, 40, 0), player.getPosition());
assertEquals(count, tracker.collect().size());
assertEquals(viewLength * viewLength, tracker.collect().size());
} }
// Check chunk#sendChunk // Check chunk#sendChunk
{ {
@ -56,7 +56,7 @@ public class ChunkViewerIntegrationTest {
instance.getChunk(x, z).sendChunk(); instance.getChunk(x, z).sendChunk();
} }
} }
assertEquals(viewLength * viewLength, tracker.collect().size()); assertEquals(count, tracker.collect().size());
} }
} }
} }