WIP chunk biomes

This commit is contained in:
Felix Cravic 2020-05-08 20:50:05 +02:00
parent 5dd08cf19b
commit a0a72cb6c7
7 changed files with 48 additions and 21 deletions

View File

@ -7,6 +7,7 @@ import net.minestom.server.instance.ChunkPopulator;
import net.minestom.server.instance.batch.ChunkBatch;
import net.minestom.server.instance.block.Block;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
@ -29,8 +30,8 @@ public class ChunkGeneratorDemo extends ChunkGenerator {
}
@Override
public Biome getBiome(int chunkX, int chunkZ) {
return Biome.PLAINS;
public void fillBiomes(Biome[] biomes, int chunkX, int chunkZ) {
Arrays.fill(biomes, Biome.PLAINS);
}
@Override

View File

@ -34,8 +34,11 @@ public class NoiseTestGenerator extends ChunkGenerator {
}
@Override
public Biome getBiome(int chunkX, int chunkZ) {
return Biome.PLAINS;
public void fillBiomes(Biome[] biomes, int chunkX, int chunkZ) {
for (int i = 0; i < biomes.length; i++) {
boolean pair = i % 2 == 0;
biomes[i] = pair ? Biome.PLAINS : Biome.BEACH;
}
}
@Override

View File

@ -36,7 +36,9 @@ public class Chunk implements Viewable {
public static final int CHUNK_SIZE_Z = 16;
public static final int CHUNK_SECTION_SIZE = 16;
private Biome biome;
public static final int BIOME_COUNT = 1024; // 4x4x4 blocks
private Biome[] biomes;
private int chunkX, chunkZ;
// blocks id based on coord, see Chunk#getBlockIndex
@ -61,8 +63,8 @@ public class Chunk implements Viewable {
private Set<Player> viewers = new CopyOnWriteArraySet<>();
private ByteBuf fullDataPacket;
public Chunk(Biome biome, int chunkX, int chunkZ) {
this.biome = biome;
public Chunk(Biome[] biomes, int chunkX, int chunkZ) {
this.biomes = biomes;
this.chunkX = chunkX;
this.chunkZ = chunkZ;
}
@ -249,8 +251,8 @@ public class Chunk implements Viewable {
}
}
public Biome getBiome() {
return biome;
public Biome[] getBiomes() {
return biomes;
}
public int getChunkX() {
@ -282,7 +284,10 @@ public class Chunk implements Viewable {
protected byte[] getSerializedData() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(output);
dos.writeByte(biome.getId());
for (int i = 0; i < BIOME_COUNT; i++) {
dos.writeByte(biomes[i].getId());
}
for (byte x = 0; x < CHUNK_SIZE_X; x++) {
for (short y = 0; y < CHUNK_SIZE_Y; y++) {
@ -341,7 +346,7 @@ public class Chunk implements Viewable {
setFullDataPacket(buffer);
}
// Write the pakcet in the writer thread pools
// Write the packet in the writer thread pools
public void refreshDataPacket(Runnable runnable) {
PacketWriterUtils.writeCallbackPacket(getFreshFullDataPacket(), buf -> {
setFullDataPacket(buf);

View File

@ -8,7 +8,12 @@ public abstract class ChunkGenerator {
public abstract void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ);
public abstract Biome getBiome(int chunkX, int chunkZ);
/**
* @param biomes the array to fill
* @param chunkX
* @param chunkZ
*/
public abstract void fillBiomes(Biome[] biomes, int chunkX, int chunkZ);
public abstract List<ChunkPopulator> getPopulators();

View File

@ -83,7 +83,7 @@ public class InstanceContainer extends Instance {
BlockPosition blockPosition = new BlockPosition(x, y, z);
if(isAlreadyChanged(blockPosition, blockId)) { // do NOT change the block again.
if (isAlreadyChanged(blockPosition, blockId)) { // do NOT change the block again.
// Avoids StackOverflowExceptions when onDestroy tries to destroy the block itself
// This can happen with nether portals which break the entire frame when a portal block is broken
return;
@ -122,13 +122,14 @@ public class InstanceContainer extends Instance {
/**
* Has this block already changed since last update? Prevents StackOverflow with blocks trying to modify their position in onDestroy or onPlace
*
* @param blockPosition
* @param blockId
* @return
*/
private boolean isAlreadyChanged(BlockPosition blockPosition, short blockId) {
Block changedBlock = currentlyChangingBlocks.get(blockPosition);
if(changedBlock == null)
if (changedBlock == null)
return false;
return changedBlock.getBlockId() == blockId;
}
@ -341,8 +342,14 @@ public class InstanceContainer extends Instance {
@Override
public void createChunk(int chunkX, int chunkZ, Consumer<Chunk> callback) {
Biome biome = chunkGenerator != null ? chunkGenerator.getBiome(chunkX, chunkZ) : Biome.VOID;
Chunk chunk = new Chunk(biome, chunkX, chunkZ);
Biome[] biomes = new Biome[Chunk.BIOME_COUNT];
if (chunkGenerator == null) {
Arrays.fill(biomes, Biome.VOID);
} else {
chunkGenerator.fillBiomes(biomes, chunkX, chunkZ);
}
Chunk chunk = new Chunk(biomes, chunkX, chunkZ);
cacheChunk(chunk);
if (chunkGenerator != null) {
ChunkBatch chunkBatch = createChunkBatch(chunk);

View File

@ -1,5 +1,6 @@
package net.minestom.server.network.packet.server.play;
import net.minestom.server.instance.Biome;
import net.minestom.server.instance.Chunk;
import net.minestom.server.network.packet.PacketWriter;
import net.minestom.server.network.packet.server.ServerPacket;
@ -78,9 +79,9 @@ public class ChunkDataPacket implements ServerPacket {
// Biome data
if (fullChunk) {
for (int z = 0; z < 1024; z++) {
// TODO proper chunk section biome
writer.writeInt(chunk.getBiome().getId());
Biome[] biomes = chunk.getBiomes();
for (int i = 0; i < biomes.length; i++) {
writer.writeInt(biomes[i].getId());
}
}

View File

@ -20,8 +20,13 @@ public class ChunkReader {
ChunkBatch chunkBatch = null;
try {
Biome biome = Biome.fromId(stream.readByte());
Chunk chunk = new Chunk(biome, chunkX, chunkZ);
Biome[] biomes = new Biome[Chunk.BIOME_COUNT];
for (int i = 0; i < biomes.length; i++) {
biomes[i] = Biome.fromId(stream.readByte());
}
Chunk chunk = new Chunk(biomes, chunkX, chunkZ);
chunkBatch = instance.createChunkBatch(chunk);