Skip to content

Commit

Permalink
compressed mazes to regions
Browse files Browse the repository at this point in the history
  • Loading branch information
LudoCrypt committed Dec 27, 2023
1 parent 3753449 commit 1342465
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
org.gradle.jvmargs = -Xmx1G
org.gradle.parallel = true

version = b12.0.1
version = b12.0.2
maven_group = net.ludocrypt
archives_base_name = limlib
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,18 @@ public Vec2i add(int x, int y) {
return new Vec2i(this.x + x, this.y + y);
}

public Vec2i add(Vec2i vec) {
return add(vec.x, vec.y);
}

public Vec2i mul(int x, int y) {
return new Vec2i(this.x * x, this.y * y);
}

public Vec2i mul(int x) {
return mul(x, x);
}

public Vec2i up() {
return up(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Queue;

import org.apache.commons.io.FilenameUtils;
Expand All @@ -17,6 +18,7 @@
import net.ludocrypt.limlib.api.world.maze.MazeGenerator;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtIo;
import net.minecraft.util.math.MathHelper;

public class MazeStorage {

Expand All @@ -34,6 +36,7 @@ public MazeStorage(Map<String, MazeGenerator> generators, File dir) {
dir.mkdirs();

for (Entry<String, MazeGenerator> entry : generators.entrySet()) {
new File(dir, entry.getKey()).mkdirs();
entry.getValue().connect((pos, maze) -> dirt.add(() -> serialize(entry.getKey(), pos, maze)));
}

Expand All @@ -47,12 +50,24 @@ public void read() {
for (File data : mazeDir.listFiles((file) -> FilenameUtils.getExtension(file.getAbsolutePath()).equals("nbt"))) {

try {
NbtCompound readMaze = NbtIo.read(data);
NbtCompound region = NbtIo.readCompressed(data);

String[] sizeRaw = FilenameUtils.getBaseName(data.getAbsolutePath()).substring(2).split("\\.");
Vec2i size = new Vec2i(Integer.valueOf(sizeRaw[0]), Integer.valueOf(sizeRaw[1]));
String[] regionPosRaw = FilenameUtils.getBaseName(data.getAbsolutePath()).substring(2).split("\\.");
Vec2i regionPos = new Vec2i(Integer.valueOf(regionPosRaw[0]), Integer.valueOf(regionPosRaw[1])).mul(16);

for (String mid : region.getKeys()) {
String[] posRaw = mid.split("\\.");
Vec2i pos = new Vec2i(Integer.valueOf(posRaw[0]), Integer.valueOf(posRaw[1]));

NbtCompound mazeRaw = region.getCompound(mid);

this.generators
.get(id)
.getMazes()
.put(regionPos.add(pos), new MazeComponent(mazeRaw.getInt("width"), mazeRaw.getInt("height"))
.read(mazeRaw.getCompound("maze")));
}

this.generators.get(id).getMazes().put(size, new MazeComponent(size.getX(), size.getY()).read(readMaze));
} catch (IOException | NullPointerException e) {
LOGGER.error("Could not read data {}", this, e);
}
Expand All @@ -75,9 +90,22 @@ public void save() {
public void serialize(String mazeId, Vec2i pos, MazeComponent maze) {

try {
NbtIo
.writeCompressed(maze.write(new NbtCompound()),
new File(new File(dir, mazeId), "m." + pos.getX() + "." + pos.getY() + ".nbt"));
File regionFile = new File(new File(dir, mazeId), "m." + ((pos.getX() - MathHelper
.floorMod(pos.getX(), 16)) / 16) + "." + ((pos.getY() - MathHelper.floorMod(pos.getY(), 16)) / 16) + ".nbt");

NbtCompound region = Optional.ofNullable(NbtIo.readCompressed(regionFile)).orElse(new NbtCompound());

NbtCompound compound = new NbtCompound();
NbtCompound mazeCompound = maze.write(new NbtCompound());

compound.put("maze", mazeCompound);
compound.putInt("width", maze.width);
compound.putInt("height", maze.height);

region.put(MathHelper.floorMod(pos.getX(), 16) + "." + MathHelper.floorMod(pos.getY(), 16), compound);

NbtIo.writeCompressed(region, regionFile);

} catch (IOException e) {
LOGGER.error("Could not save data {}", this, e);
}
Expand Down

0 comments on commit 1342465

Please sign in to comment.