Less magic values

This commit is contained in:
Felix Cravic 2020-04-26 19:17:04 +02:00
parent 161e229985
commit d4cf29c7a7
9 changed files with 43 additions and 39 deletions

View File

@ -1,6 +1,7 @@
package fr.themode.demo.generator;
import net.minestom.server.instance.Biome;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.ChunkGenerator;
import net.minestom.server.instance.batch.ChunkBatch;
@ -12,8 +13,8 @@ public class ChunkGeneratorDemo extends ChunkGenerator {
@Override
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
for (byte x = 0; x < 16; x++)
for (byte z = 0; z < 16; z++) {
for (byte x = 0; x < Chunk.CHUNK_SIZE_X; x++)
for (byte z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
for (byte y = 0; y < 65; y++) {
if (random.nextInt(100) > 10) {
batch.setCustomBlock(x, y, z, "custom_block");

View File

@ -1,6 +1,7 @@
package fr.themode.demo.generator;
import net.minestom.server.instance.Biome;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.ChunkGenerator;
import net.minestom.server.instance.batch.ChunkBatch;
import net.minestom.server.utils.noise.FastNoise;
@ -19,9 +20,9 @@ public class NoiseTestGenerator extends ChunkGenerator {
@Override
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
for (byte x = 0; x < 16; x++)
for (byte z = 0; z < 16; z++) {
float height = fastNoise.GetSimplex(x + 16 * chunkX, z + 16 * chunkZ) * 135;
for (byte x = 0; x < Chunk.CHUNK_SIZE_X; x++)
for (byte z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
float height = fastNoise.GetSimplex(x + Chunk.CHUNK_SIZE_X * chunkX, z + Chunk.CHUNK_SIZE_Z * chunkZ) * 135;
height = Math.max(height, 70);
for (byte y = 0; y < height; y++) {
if (random.nextInt(100) > 10) {

View File

@ -33,6 +33,7 @@ public class Chunk implements Viewable {
public static final int CHUNK_SIZE_X = 16;
public static final int CHUNK_SIZE_Y = 256;
public static final int CHUNK_SIZE_Z = 16;
public static final int CHUNK_SECTION_SIZE = 16;
private Biome biome;
private int chunkX, chunkZ;
@ -91,7 +92,7 @@ public class Chunk implements Viewable {
}
private void setBlock(int x, int y, int z, short blockId, short customId, Data data, UpdateConsumer updateConsumer) {
int index = SerializerUtils.chunkCoordToIndex(x, y, z);
int index = SerializerUtils.coordToChunkIndex(x, y, z);
if (blockId != 0
|| (blockId == 0 && customId != 0 && updateConsumer != null)) { // Allow custom air block for update purpose, refused if no update consumer has been found
refreshBlockValue(x, y, z, blockId, customId);
@ -138,7 +139,7 @@ public class Chunk implements Viewable {
}
public void setBlockData(int x, int y, int z, Data data) {
int index = SerializerUtils.chunkCoordToIndex(x, y, z);
int index = SerializerUtils.coordToChunkIndex(x, y, z);
if (data != null) {
this.blocksData.put(index, data);
} else {
@ -178,7 +179,7 @@ public class Chunk implements Viewable {
}
public Data getData(byte x, byte y, byte z) {
int index = SerializerUtils.chunkCoordToIndex(x, y, z);
int index = SerializerUtils.coordToChunkIndex(x, y, z);
return getData(index);
}
@ -256,7 +257,7 @@ public class Chunk implements Viewable {
for (byte x = 0; x < CHUNK_SIZE_X; x++) {
for (short y = 0; y < CHUNK_SIZE_Y; y++) {
for (byte z = 0; z < CHUNK_SIZE_Z; z++) {
int index = SerializerUtils.chunkCoordToIndex(x, y, z);
int index = SerializerUtils.coordToChunkIndex(x, y, z);
short blockId = getBlockId(x, y, z);
short customBlockId = getCustomBlockId(x, y, z);
@ -342,8 +343,8 @@ public class Chunk implements Viewable {
}
private int getBlockIndex(int x, int y, int z) {
x = x % 16;
z = z % 16;
x = x % Chunk.CHUNK_SIZE_X;
z = z % Chunk.CHUNK_SIZE_Z;
x = x < 0 ? Chunk.CHUNK_SIZE_X + x : x;
z = z < 0 ? Chunk.CHUNK_SIZE_Z + z : z;

View File

@ -48,11 +48,7 @@ public class InstanceContainer extends Instance {
Chunk chunk = getChunkAt(x, z);
synchronized (chunk) {
int chunkX = x % 16;
int chunkY = y;
int chunkZ = z % 16;
int index = SerializerUtils.chunkCoordToIndex(chunkX, chunkY, chunkZ);
int index = SerializerUtils.coordToChunkIndex(x, y, z);
callBlockDestroy(chunk, index, x, y, z);
@ -73,11 +69,7 @@ public class InstanceContainer extends Instance {
Chunk chunk = getChunkAt(x, z);
synchronized (chunk) {
int chunkX = x % 16;
int chunkY = y;
int chunkZ = z % 16;
int index = SerializerUtils.chunkCoordToIndex(chunkX, chunkY, chunkZ);
int index = SerializerUtils.coordToChunkIndex(x, y, z);
callBlockDestroy(chunk, index, x, y, z);
@ -85,7 +77,7 @@ public class InstanceContainer extends Instance {
blockId = executeBlockPlacementRule(blockId, blockPosition);
chunk.UNSAFE_setCustomBlock(chunkX, chunkY, chunkZ, blockId, data);
chunk.UNSAFE_setCustomBlock(x, y, z, blockId, data);
executeNeighboursBlockPlacementRule(blockPosition);

View File

@ -41,9 +41,9 @@ public class BlockBatch implements InstanceBatch {
blocksData = new ArrayList<>();
BlockData blockData = new BlockData();
blockData.x = x % 16;
blockData.x = x;
blockData.y = y;
blockData.z = z % 16;
blockData.z = z;
blockData.isCustomBlock = customBlock;
blockData.blockId = blockId;
blockData.data = data;

View File

@ -110,12 +110,12 @@ public class ChunkDataPacket implements ServerPacket {
}
private short[] getSection(Chunk chunk, byte section) {
short[] blocks = new short[16 * 16 * 16];
short[] blocks = new short[Chunk.CHUNK_SIZE_X * Chunk.CHUNK_SECTION_SIZE * Chunk.CHUNK_SIZE_Z];
boolean empty = true;
for (byte y = 0; y < 16; y++) {
for (byte x = 0; x < 16; x++) {
for (byte z = 0; z < 16; z++) {
short blockId = chunk.getBlockId(x, (y + 16 * section), z);
for (byte y = 0; y < Chunk.CHUNK_SECTION_SIZE; y++) {
for (byte x = 0; x < Chunk.CHUNK_SIZE_X; x++) {
for (byte z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
short blockId = chunk.getBlockId(x, (y + Chunk.CHUNK_SECTION_SIZE * section), z);
if (blockId != 0)
empty = false;

View File

@ -1,15 +1,19 @@
package net.minestom.server.utils;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
public class ChunkUtils {
public static boolean isChunkUnloaded(Instance instance, float x, float z) {
return instance.getChunk((int) Math.floor(x / 16), (int) Math.floor(z / 16)) == null;
int chunkX = getChunkCoordinate((int) x);
int chunkZ = getChunkCoordinate((int) z);
return instance.getChunk(chunkX, chunkZ) == null;
}
public static int getChunkCoordinate(int xz) {
return Math.floorDiv(xz, 16);
// Assume Chunk.CHUNK_SIZE_X == Chunk.CHUNK_SIZE_Z
return Math.floorDiv(xz, Chunk.CHUNK_SIZE_X);
}
public static long getChunkIndex(int chunkX, int chunkZ) {
@ -23,7 +27,7 @@ public class ChunkUtils {
}
public static int getSectionAt(int y) {
return y / 16;
return y / Chunk.CHUNK_SECTION_SIZE;
}
public static long[] getChunksInRange(final Position position, int range) {
@ -33,8 +37,8 @@ public class ChunkUtils {
int counter = 0;
for (int x = startLoop; x < endLoop; x++) {
for (int z = startLoop; z < endLoop; z++) {
int chunkX = getChunkCoordinate((int) (position.getX() + 16 * x));
int chunkZ = getChunkCoordinate((int) (position.getZ() + 16 * z));
int chunkX = getChunkCoordinate((int) (position.getX() + Chunk.CHUNK_SIZE_X * x));
int chunkZ = getChunkCoordinate((int) (position.getZ() + Chunk.CHUNK_SIZE_Z * z));
visibleChunks[counter] = getChunkIndex(chunkX, chunkZ);
counter++;
}

View File

@ -1,5 +1,7 @@
package net.minestom.server.utils;
import net.minestom.server.instance.Chunk;
public class SerializerUtils {
public static byte[] intToBytes(int value) {
@ -18,7 +20,9 @@ public class SerializerUtils {
((value[3] & 0xFF) << 0);
}
public static int chunkCoordToIndex(int x, int y, int z) {
public static int coordToChunkIndex(int x, int y, int z) {
x = x % Chunk.CHUNK_SIZE_X;
z = z % Chunk.CHUNK_SIZE_Z;
short index = (short) (x & 0x000F);
index |= (y << 4) & 0x0FF0;
index |= (z << 12) & 0xF000;

View File

@ -2,6 +2,7 @@ package net.minestom.server.utils;
import io.netty.buffer.ByteBuf;
import net.minestom.server.chat.Chat;
import net.minestom.server.instance.Chunk;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.PacketReader;
import net.minestom.server.network.packet.PacketWriter;
@ -174,10 +175,10 @@ public class Utils {
buffer.putShort(count);
buffer.putByte((byte) bitsPerEntry);
int[] blocksData = new int[16 * 16 * 16];
for (int y = 0; y < 16; y++) {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
int[] blocksData = new int[Chunk.CHUNK_SIZE_X * Chunk.CHUNK_SECTION_SIZE * Chunk.CHUNK_SIZE_Z];
for (int y = 0; y < Chunk.CHUNK_SECTION_SIZE; y++) {
for (int x = 0; x < Chunk.CHUNK_SIZE_X; x++) {
for (int z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
int sectionIndex = (((y * 16) + x) * 16) + z;
int index = y << 8 | z << 4 | x;
blocksData[index] = blocksId[sectionIndex];