maze-generator/src/main/java/ch/fritteli/maze/generator/serialization/v2/MazeOutputStreamV2.java

54 lines
1.8 KiB
Java

package ch.fritteli.maze.generator.serialization.v2;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Position;
import ch.fritteli.maze.generator.model.Tile;
import ch.fritteli.maze.generator.serialization.AbstractMazeOutputStream;
import lombok.NonNull;
public class MazeOutputStreamV2 extends AbstractMazeOutputStream {
@Override
public void writeHeader() {
// 00 0x1a magic
// 01 0xb1 magic
// 02 0x02 version
this.writeByte(SerializerDeserializerV2.MAGIC_BYTE_1);
this.writeByte(SerializerDeserializerV2.MAGIC_BYTE_2);
this.writeByte(SerializerDeserializerV2.VERSION_BYTE);
}
@Override
public void writeMazeData(@NonNull final Maze maze) {
// 03..06 width (int)
// 07..10 height (int)
// 11..14 start-x (int)
// 15..18 start-y (int)
// 19..22 end-x (int)
// 23..26 end-y (int)
// 27..34 random seed number (long)
// 35.. tiles
final long randomSeed = maze.getRandomSeed();
final int width = maze.getWidth();
final int height = maze.getHeight();
final Position start = maze.getStart();
final Position end = maze.getEnd();
this.writeInt(width);
this.writeInt(height);
this.writeInt(start.getX());
this.writeInt(start.getY());
this.writeInt(end.getX());
this.writeInt(end.getY());
this.writeLong(randomSeed);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// We .get() it, because we want to crash hard if it is not available.
final Tile tile = maze.getTileAt(x, y).get();
final byte bitmask = SerializerDeserializerV2.getBitmaskForTile(tile);
this.writeByte(bitmask);
}
}
}
}