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

View File

@ -34,8 +34,11 @@ public class NoiseTestGenerator extends ChunkGenerator {
} }
@Override @Override
public Biome getBiome(int chunkX, int chunkZ) { public void fillBiomes(Biome[] biomes, int chunkX, int chunkZ) {
return Biome.PLAINS; for (int i = 0; i < biomes.length; i++) {
boolean pair = i % 2 == 0;
biomes[i] = pair ? Biome.PLAINS : Biome.BEACH;
}
} }
@Override @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_SIZE_Z = 16;
public static final int CHUNK_SECTION_SIZE = 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; private int chunkX, chunkZ;
// blocks id based on coord, see Chunk#getBlockIndex // blocks id based on coord, see Chunk#getBlockIndex
@ -61,8 +63,8 @@ public class Chunk implements Viewable {
private Set<Player> viewers = new CopyOnWriteArraySet<>(); private Set<Player> viewers = new CopyOnWriteArraySet<>();
private ByteBuf fullDataPacket; private ByteBuf fullDataPacket;
public Chunk(Biome biome, int chunkX, int chunkZ) { public Chunk(Biome[] biomes, int chunkX, int chunkZ) {
this.biome = biome; this.biomes = biomes;
this.chunkX = chunkX; this.chunkX = chunkX;
this.chunkZ = chunkZ; this.chunkZ = chunkZ;
} }
@ -249,8 +251,8 @@ public class Chunk implements Viewable {
} }
} }
public Biome getBiome() { public Biome[] getBiomes() {
return biome; return biomes;
} }
public int getChunkX() { public int getChunkX() {
@ -282,7 +284,10 @@ public class Chunk implements Viewable {
protected byte[] getSerializedData() throws IOException { protected byte[] getSerializedData() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(output); 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 (byte x = 0; x < CHUNK_SIZE_X; x++) {
for (short y = 0; y < CHUNK_SIZE_Y; y++) { for (short y = 0; y < CHUNK_SIZE_Y; y++) {
@ -341,7 +346,7 @@ public class Chunk implements Viewable {
setFullDataPacket(buffer); setFullDataPacket(buffer);
} }
// Write the pakcet in the writer thread pools // Write the packet in the writer thread pools
public void refreshDataPacket(Runnable runnable) { public void refreshDataPacket(Runnable runnable) {
PacketWriterUtils.writeCallbackPacket(getFreshFullDataPacket(), buf -> { PacketWriterUtils.writeCallbackPacket(getFreshFullDataPacket(), buf -> {
setFullDataPacket(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 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(); public abstract List<ChunkPopulator> getPopulators();

View File

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

View File

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

View File

@ -20,8 +20,13 @@ public class ChunkReader {
ChunkBatch chunkBatch = null; ChunkBatch chunkBatch = null;
try { 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); chunkBatch = instance.createChunkBatch(chunk);