lazily create chunks to improve memory usage

This commit is contained in:
dfsek
2021-12-22 16:23:03 -07:00
parent 17e2bdc6f7
commit 4000899d6d
2 changed files with 20 additions and 11 deletions

View File

@@ -24,7 +24,7 @@ public final class TerraCLI {
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();
@@ -32,7 +32,6 @@ public final class TerraCLI {
Vector2Int pos = mcaFile.getLeft();
String name = MCAUtil.createNameFromRegionLocation(pos.getX(), pos.getZ());
LOGGER.info("Writing region ({}, {}) to {}", pos.getX(), pos.getZ(), name);
mcaFile.getRight().cleanupPalettesAndBlockStates();
try {
MCAUtil.write(mcaFile.getRight(), name);

View File

@@ -9,31 +9,41 @@ import com.dfsek.terra.cli.world.chunk.CLIChunk;
public class Region implements NBTSerializable<MCAFile> {
private final CLIChunk[] chunks;
private final int x, z;
private final CLIWorld world;
public Region(CLIWorld world, int x, int z) {
this.x = x;
this.z = z;
CLIChunk[] chunks = new CLIChunk[32 * 32];
for(int cx = 0; cx < 32; cx++) {
for(int cz = 0; cz < 32; cz++) {
chunks[cx + cz * 32] = new CLIChunk(cx, cz, world);
}
}
this.chunks = chunks;
this.world = world;
this.chunks = new CLIChunk[32 * 32];;
}
public CLIChunk get(int x, int z) {
return chunks[x + z*32];
int key = x + z*32;
CLIChunk chunk = chunks[key];
if(chunk == null) {
chunk = new CLIChunk(x, z, world);
chunks[key] = chunk;
}
return chunk;
}
@Override
public MCAFile serialize() {
MCAFile mcaFile = new MCAFile(x, z);
int count = 0;
for(int cx = 0; cx < 32; cx++) {
for(int cz = 0; cz < 32; cz++) {
mcaFile.setChunk(cx + cz * 32, chunks[cx + cz * 32].serialize());
CLIChunk chunk = chunks[cx + cz * 32];
if(chunk != null) {
count++;
mcaFile.setChunk(cx + cz * 32, chunk.serialize());
}
}
}
if(count > 0) {
mcaFile.cleanupPalettesAndBlockStates();
}
return mcaFile;
}