70 lines
2.3 KiB
Java
70 lines
2.3 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.AbstractMazeInputStream;
|
|
import lombok.NonNull;
|
|
|
|
public class MazeInputStreamV2 extends AbstractMazeInputStream {
|
|
|
|
public MazeInputStreamV2(@NonNull final byte[] buf) {
|
|
super(buf);
|
|
}
|
|
|
|
@Override
|
|
public void checkHeader() {
|
|
// 00 0x1a magic
|
|
// 01 0xb1 magic
|
|
// 02 0x02 version
|
|
final byte magic1 = this.readByte();
|
|
if (magic1 != SerializerDeserializerV2.MAGIC_BYTE_1) {
|
|
throw new IllegalArgumentException("Invalid maze data.");
|
|
}
|
|
final byte magic2 = this.readByte();
|
|
if (magic2 != SerializerDeserializerV2.MAGIC_BYTE_2) {
|
|
throw new IllegalArgumentException("Invalid maze data.");
|
|
}
|
|
final int version = this.readByte();
|
|
if (version != SerializerDeserializerV2.VERSION_BYTE) {
|
|
throw new IllegalArgumentException("Unknown maze data version: " + version);
|
|
}
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public Maze readMazeData() {
|
|
// 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 int width = this.readInt();
|
|
final int height = this.readInt();
|
|
final int startX = this.readInt();
|
|
final int startY = this.readInt();
|
|
final int endX = this.readInt();
|
|
final int endY = this.readInt();
|
|
final long randomSeed = this.readLong();
|
|
|
|
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] = SerializerDeserializerV2.getTileForBitmask(bitmask);
|
|
}
|
|
}
|
|
|
|
final Position start = new Position(startX, startY);
|
|
final Position end = new Position(endX, endY);
|
|
return SerializerDeserializerV2.createMaze(tiles, width, height, start, end, randomSeed);
|
|
}
|
|
}
|