mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 14:38:26 +01:00
Simplify/Improve getChunksInRange
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
e522ba7e81
commit
4a260b98c2
@ -1,10 +1,10 @@
|
|||||||
package net.minestom.server.utils.chunk;
|
package net.minestom.server.utils.chunk;
|
||||||
|
|
||||||
|
import it.unimi.dsi.fastutil.longs.LongArrayList;
|
||||||
import net.minestom.server.coordinate.Point;
|
import net.minestom.server.coordinate.Point;
|
||||||
import net.minestom.server.coordinate.Vec;
|
import net.minestom.server.coordinate.Vec;
|
||||||
import net.minestom.server.instance.Chunk;
|
import net.minestom.server.instance.Chunk;
|
||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.utils.MathUtils;
|
|
||||||
import net.minestom.server.utils.callback.OptionalCallback;
|
import net.minestom.server.utils.callback.OptionalCallback;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -161,44 +161,16 @@ public final class ChunkUtils {
|
|||||||
* @return an array containing chunks index
|
* @return an array containing chunks index
|
||||||
*/
|
*/
|
||||||
public static long @NotNull [] getChunksInRange(@NotNull Point point, int range) {
|
public static long @NotNull [] getChunksInRange(@NotNull Point point, int range) {
|
||||||
long[] visibleChunks = new long[MathUtils.square(range * 2 + 1)];
|
final int chunkX = point.chunkX();
|
||||||
int xDistance = 0;
|
final int chunkZ = point.chunkZ();
|
||||||
int xDirection = 1;
|
LongArrayList list = new LongArrayList(); // TODO use array
|
||||||
int zDistance = 0;
|
for (int z = -range; z <= range; ++z) {
|
||||||
int zDirection = -1;
|
for (int x = -range; x <= range; ++x) {
|
||||||
int len = 1;
|
final long index = getChunkIndex(chunkX + x, chunkZ + z);
|
||||||
int corner = 0;
|
list.add(index);
|
||||||
|
|
||||||
for (int i = 0; i < visibleChunks.length; i++) {
|
|
||||||
final int chunkX = getChunkCoordinate(xDistance * Chunk.CHUNK_SIZE_X + point.x());
|
|
||||||
final int chunkZ = getChunkCoordinate(zDistance * Chunk.CHUNK_SIZE_Z + point.z());
|
|
||||||
visibleChunks[i] = getChunkIndex(chunkX, chunkZ);
|
|
||||||
|
|
||||||
if (corner % 2 == 0) {
|
|
||||||
// step on X axis
|
|
||||||
xDistance += xDirection;
|
|
||||||
|
|
||||||
if (Math.abs(xDistance) == len) {
|
|
||||||
// hit corner
|
|
||||||
corner++;
|
|
||||||
xDirection = -xDirection;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// step on Z axis
|
|
||||||
zDistance += zDirection;
|
|
||||||
|
|
||||||
if (Math.abs(zDistance) == len) {
|
|
||||||
// hit corner
|
|
||||||
corner++;
|
|
||||||
zDirection = -zDirection;
|
|
||||||
|
|
||||||
if (corner % 4 == 0) {
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return visibleChunks;
|
return list.elements();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user