52 lines
1.7 KiB
Java
52 lines
1.7 KiB
Java
package ch.fritteli.maze.generator.serialization.v1;
|
|
|
|
import ch.fritteli.maze.generator.model.Maze;
|
|
import ch.fritteli.maze.generator.model.Tile;
|
|
import ch.fritteli.maze.generator.serialization.AbstractMazeInputStream;
|
|
import lombok.NonNull;
|
|
|
|
public class MazeInputStreamV1 extends AbstractMazeInputStream {
|
|
|
|
public MazeInputStreamV1(@NonNull final byte[] buf) {
|
|
super(buf);
|
|
}
|
|
|
|
@Override
|
|
public void checkHeader() {
|
|
final byte magic1 = this.readByte();
|
|
if (magic1 != SerializerDeserializerV1.MAGIC_BYTE_1) {
|
|
throw new IllegalArgumentException("Invalid maze data.");
|
|
}
|
|
final byte magic2 = this.readByte();
|
|
if (magic2 != SerializerDeserializerV1.MAGIC_BYTE_2) {
|
|
throw new IllegalArgumentException("Invalid maze data.");
|
|
}
|
|
final int version = this.readByte();
|
|
if (version != SerializerDeserializerV1.VERSION_BYTE) {
|
|
throw new IllegalArgumentException("Unknown maze data version: " + version);
|
|
}
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public Maze readMazeData() {
|
|
final long randomSeed = this.readLong();
|
|
final int width = this.readInt();
|
|
final int height = this.readInt();
|
|
|
|
final Tile[][] tiles = new Tile[width][height];
|
|
for (int x = 0; x < width; x++) {
|
|
tiles[x] = new Tile[height];
|
|
}
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
for (int x = 0; x < width; x++) {
|
|
final byte bitmask = this.readByte();
|
|
tiles[x][y] = SerializerDeserializerV1.getTileForBitmask(bitmask);
|
|
}
|
|
}
|
|
|
|
return SerializerDeserializerV1.createMaze(tiles, width, height, randomSeed);
|
|
}
|
|
}
|