fix region serialization

This commit is contained in:
dfsek
2021-12-22 01:15:08 -07:00
parent 6b26cfc964
commit fb13ab40ca
6 changed files with 52 additions and 32 deletions
@@ -24,11 +24,11 @@ public final class TerraCLI {
ConfigPack generate = platform.getConfigRegistry().get("OVERWORLD").orElseThrow(); // TODO: make this a cli argument ConfigPack generate = platform.getConfigRegistry().get("OVERWORLD").orElseThrow(); // TODO: make this a cli argument
CLIWorld world = new CLIWorld(1, 2, 384, -64, generate); CLIWorld world = new CLIWorld(2, 2, 384, -64, generate);
world.generate(); world.generate();
world.serialize().forEach(mcaFile -> { world.serialize().parallel().forEach(mcaFile -> {
Vector2Int pos = mcaFile.getLeft(); Vector2Int pos = mcaFile.getLeft();
String name = MCAUtil.createNameFromRegionLocation(pos.getX(), pos.getZ()); String name = MCAUtil.createNameFromRegionLocation(pos.getX(), pos.getZ());
LOGGER.info("Writing region ({}, {}) to {}", pos.getX(), pos.getZ(), name); LOGGER.info("Writing region ({}, {}) to {}", pos.getX(), pos.getZ(), name);
@@ -39,6 +39,9 @@ public final class TerraCLI {
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
LOGGER.info("Wrote region to file.");
}); });
LOGGER.info("Done.");
System.exit(0);
} }
} }
@@ -16,13 +16,26 @@ public class CLIBlockState implements BlockState {
public CLIBlockState(String value) { public CLIBlockState(String value) {
this.value = value; this.value = value;
if(value.contains("[")) { if(value.contains("[")) {
this.type = new CLIBlockType(value.substring(0, value.indexOf("[")));
} else { } else {
this.type = new CLIBlockType(value);
} }
this.isAir = value.startsWith("minecraft:air"); this.isAir = value.startsWith("minecraft:air");
this.nbt = new CompoundTag(); this.nbt = new CompoundTag();
this.nbt.putString("Name", value); if(value.contains("[")) {
this.type = new CLIBlockType(value.substring(0, value.indexOf("[")));
String properties = value.substring(value.indexOf('[') + 1, value.indexOf(']'));
String[] props = properties.split(",");
CompoundTag pTag = new CompoundTag();
for(String property : props) {
String name = property.substring(0, property.indexOf('='));
String val = property.substring(property.indexOf('=') + 1);
pTag.putString(name, val);
}
this.nbt.put("Properties", pTag);
} else this.type = new CLIBlockType(value);
this.nbt.putString("Name", type.getHandle());
} }
@Override @Override
@@ -14,13 +14,13 @@ public class CLIBlockType implements BlockType {
public CLIBlockType(String value) { public CLIBlockType(String value) {
if(value.contains("[")) throw new IllegalArgumentException("Block Type must not contain properties"); if(value.contains("[")) throw new IllegalArgumentException("Block Type must not contain properties");
this.value = value; this.value = value;
this.solid = value.equals("minecraft:air"); this.solid = !value.equals("minecraft:air");
this.water = value.equals("minecraft:water"); this.water = value.equals("minecraft:water");
this.defaultState = Lazy.lazy(() -> new CLIBlockState(value)); this.defaultState = Lazy.lazy(() -> new CLIBlockState(value));
} }
@Override @Override
public Object getHandle() { public String getHandle() {
return value; return value;
} }
@@ -31,6 +31,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -74,20 +75,28 @@ public class CLIWorld implements ServerWorld, NBTSerializable<Stream<Pair<Vector
public void generate() { public void generate() {
int sizeChunks = size * 32; int sizeChunks = size * 32;
List<Future<?>> futures = new ArrayList<>(); List<Future<?>> futures = new ArrayList<>();
final long start = System.nanoTime(); final AtomicLong start = new AtomicLong(System.nanoTime());
for(int x = -sizeChunks + 1; x < sizeChunks; x++) { for(int x = 0; x < sizeChunks; x++) {
for(int z = -sizeChunks + 1; z < sizeChunks; z++) { for(int z = 0; z < sizeChunks; z++) {
int finalX = x; int finalX = x;
int finalZ = z; int finalZ = z;
futures.add(executor.submit(() -> { futures.add(executor.submit(() -> {
int num = amount.getAndIncrement(); try {
long time = System.nanoTime(); int num = amount.getAndIncrement();
double cps = num / ((double) (time - start) / 1000000000); CLIChunk chunk = getChunkAt(finalX, finalZ);
LOGGER.info("Generating chunk at ({}, {}), generated {} chunks at {}cps", finalX, finalZ, num, cps); chunkGenerator.generateChunkData(chunk, this, finalX, finalZ);
CLIChunk chunk = getChunkAt(finalX, finalZ); CLIProtoWorld protoWorld = new CLIProtoWorld(this, finalX, finalZ);
chunkGenerator.generateChunkData(chunk, this, finalX, finalZ); pack.getStages().forEach(stage -> stage.populate(protoWorld));
CLIProtoWorld protoWorld = new CLIProtoWorld(this, finalX, finalZ); if(num % 240 == 239) {
pack.getStages().forEach(stage -> stage.populate(protoWorld)); long time = System.nanoTime();
double cps = num / ((double) (time - start.get()) / 1000000000);
LOGGER.info("Generating chunk at ({}, {}), generated {} chunks at {}cps", finalX, finalZ, num, cps);
amount.set(0);
start.set(System.nanoTime());
}
} catch(Exception e) {
e.printStackTrace();
}
})); }));
} }
} }
@@ -154,10 +163,10 @@ public class CLIWorld implements ServerWorld, NBTSerializable<Stream<Pair<Vector
@Override @Override
public CLIChunk getChunkAt(int x, int z) { public CLIChunk getChunkAt(int x, int z) {
x += (size + 1) * 16; int cx = x + (size + 1) * 16;
z += (size + 1) * 16; int cz = z + (size + 1) * 16;
return regions[FastMath.floorDiv(x, regionBlocks) + regionBlocks * FastMath.floorDiv(z, regionBlocks)] return regions[FastMath.floorDiv(cx, regionBlocks) + regionBlocks * FastMath.floorDiv(cz, regionBlocks)]
.get(FastMath.floorMod(FastMath.floorDiv(x, 16), 32), FastMath.floorMod(FastMath.floorDiv(z, 16), 32)); .get(FastMath.floorMod(x, 32), FastMath.floorMod(z, 32));
} }
@Override @Override
@@ -32,7 +32,7 @@ public class Region implements NBTSerializable<MCAFile> {
MCAFile mcaFile = new MCAFile(x, z); MCAFile mcaFile = new MCAFile(x, z);
for(int cx = 0; cx < 32; cx++) { for(int cx = 0; cx < 32; cx++) {
for(int cz = 0; cz < 32; cz++) { for(int cz = 0; cz < 32; cz++) {
mcaFile.setChunk(cx, cz, chunks[cx + cz * 32].serialize()); mcaFile.setChunk(cx + cz * 32, chunks[cx + cz * 32].serialize());
} }
} }
return mcaFile; return mcaFile;
@@ -1,22 +1,16 @@
package com.dfsek.terra.cli.world.chunk; package com.dfsek.terra.cli.world.chunk;
import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.world.ServerWorld; import com.dfsek.terra.api.world.ServerWorld;
import com.dfsek.terra.api.world.chunk.Chunk; import com.dfsek.terra.api.world.chunk.Chunk;
import com.dfsek.terra.api.world.chunk.generation.ProtoChunk; import com.dfsek.terra.api.world.chunk.generation.ProtoChunk;
import com.dfsek.terra.cli.NBTSerializable; import com.dfsek.terra.cli.NBTSerializable;
import com.dfsek.terra.cli.block.CLIBlockState; import com.dfsek.terra.cli.block.CLIBlockState;
import com.dfsek.terra.cli.handle.CLIWorldHandle; import com.dfsek.terra.cli.handle.CLIWorldHandle;
import com.dfsek.terra.cli.world.CLIWorld; import com.dfsek.terra.cli.world.CLIWorld;
import net.querz.mca.MCAFile;
import net.querz.nbt.tag.CompoundTag;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.dfsek.terra.cli.handle.CLIWorldHandle.getAIR; import static com.dfsek.terra.cli.handle.CLIWorldHandle.getAIR;
@@ -71,7 +65,7 @@ public class CLIChunk implements Chunk, ProtoChunk, NBTSerializable<net.querz.mc
@Override @Override
public net.querz.mca.Chunk serialize() { public net.querz.mca.Chunk serialize() {
net.querz.mca.Chunk chunk = net.querz.mca.Chunk.newChunk(); net.querz.mca.Chunk chunk = net.querz.mca.Chunk.newChunk(2230);
for(int x = 0; x < blocks.length; x++) { for(int x = 0; x < blocks.length; x++) {
for(int z = 0; z < blocks[x].length; z++) { for(int z = 0; z < blocks[x].length; z++) {
for(int y = 0; y < blocks[z][z].length; y++) { for(int y = 0; y < blocks[z][z].length; y++) {
@@ -85,6 +79,7 @@ public class CLIChunk implements Chunk, ProtoChunk, NBTSerializable<net.querz.mc
} }
} }
} }
chunk.setStatus("features");
return chunk; return chunk;
} }