Moving level loading into the package
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
import 'package:wolf_3d_data/wolf_3d_data.dart';
|
||||
|
||||
class WolfLevel {
|
||||
final String name;
|
||||
final Sprite wallGrid;
|
||||
final Sprite objectGrid;
|
||||
|
||||
WolfLevel({
|
||||
required this.name,
|
||||
required this.wallGrid,
|
||||
required this.objectGrid,
|
||||
});
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:wolf_3d_data/wolf_3d_data.dart';
|
||||
import 'package:wolf_dart/features/map/wolf_level.dart';
|
||||
import 'package:wolf_dart/features/map/wolf_map_parser.dart';
|
||||
|
||||
class WolfMap {
|
||||
/// The fully parsed and decompressed levels from the game files.
|
||||
@@ -24,7 +22,7 @@ class WolfMap {
|
||||
final vswap = await rootBundle.load("assets/VSWAP.WL1");
|
||||
|
||||
// 2. Parse the data using the parser we just built
|
||||
final parsedLevels = WolfMapParser.parseMaps(
|
||||
final parsedLevels = WLParser.parseMaps(
|
||||
mapHead,
|
||||
gameMaps,
|
||||
isShareware: true,
|
||||
@@ -48,7 +46,7 @@ class WolfMap {
|
||||
final vswap = await rootBundle.load("assets/VSWAP.WL6");
|
||||
|
||||
// 2. Parse the data using the parser we just built
|
||||
final parsedLevels = WolfMapParser.parseMaps(mapHead, gameMaps);
|
||||
final parsedLevels = WLParser.parseMaps(mapHead, gameMaps);
|
||||
final parsedTextures = WLParser.parseWalls(vswap);
|
||||
final parsedSprites = WLParser.parseSprites(vswap);
|
||||
|
||||
|
||||
@@ -1,187 +0,0 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:wolf_dart/features/entities/map_objects.dart';
|
||||
import 'package:wolf_dart/features/map/wolf_level.dart';
|
||||
|
||||
abstract class WolfMapParser {
|
||||
/// Parses MAPHEAD and GAMEMAPS to extract the raw level data.
|
||||
static List<WolfLevel> parseMaps(
|
||||
ByteData mapHead,
|
||||
ByteData gameMaps, {
|
||||
bool isShareware = true,
|
||||
}) {
|
||||
List<WolfLevel> levels = [];
|
||||
|
||||
// 1. READ MAPHEAD
|
||||
// The very first 16-bit word in MAPHEAD is the RLEW tag (usually 0xABCD)
|
||||
// We will need this later for decompression!
|
||||
int rlewTag = mapHead.getUint16(0, Endian.little);
|
||||
|
||||
// MAPHEAD contains up to 100 levels.
|
||||
// Starting at byte 2, there are 100 32-bit integers representing
|
||||
// the byte offset of each level's header inside GAMEMAPS.
|
||||
for (int i = 0; i < 100; i++) {
|
||||
int mapOffset = mapHead.getUint32(2 + (i * 4), Endian.little);
|
||||
|
||||
// An offset of 0 means the level doesn't exist (end of the list)
|
||||
if (mapOffset == 0) continue;
|
||||
|
||||
// 2. READ GAMEMAPS HEADER
|
||||
// Jump to the offset in GAMEMAPS to read the 38-byte Level Header
|
||||
|
||||
// Pointers to the compressed data for the 3 planes (Walls, Objects, Extra)
|
||||
int plane0Offset = gameMaps.getUint32(mapOffset + 0, Endian.little);
|
||||
int plane1Offset = gameMaps.getUint32(mapOffset + 4, Endian.little);
|
||||
// Plane 2 (offset + 8) is usually unused in standard Wolf3D
|
||||
|
||||
// Lengths of the compressed data for each plane
|
||||
int plane0Length = gameMaps.getUint16(mapOffset + 12, Endian.little);
|
||||
int plane1Length = gameMaps.getUint16(mapOffset + 14, Endian.little);
|
||||
|
||||
// Map Name (16 bytes of ASCII text)
|
||||
List<int> nameBytes = [];
|
||||
for (int n = 0; n < 16; n++) {
|
||||
int charCode = gameMaps.getUint8(mapOffset + 22 + n);
|
||||
if (charCode == 0) break; // Null terminator
|
||||
nameBytes.add(charCode);
|
||||
}
|
||||
String name = ascii.decode(nameBytes);
|
||||
|
||||
// 3. EXTRACT AND DECOMPRESS BOTH PLANES
|
||||
|
||||
// --- PLANE 0: WALLS ---
|
||||
final compressedWallData = gameMaps.buffer.asUint8List(
|
||||
plane0Offset,
|
||||
plane0Length,
|
||||
);
|
||||
Uint16List carmackExpandedWalls = _expandCarmack(compressedWallData);
|
||||
List<int> flatWallGrid = _expandRlew(carmackExpandedWalls, rlewTag);
|
||||
|
||||
// --- PLANE 1: OBJECTS (NEW) ---
|
||||
final compressedObjectData = gameMaps.buffer.asUint8List(
|
||||
plane1Offset,
|
||||
plane1Length,
|
||||
);
|
||||
Uint16List carmackExpandedObjects = _expandCarmack(compressedObjectData);
|
||||
List<int> flatObjectGrid = _expandRlew(carmackExpandedObjects, rlewTag);
|
||||
|
||||
for (int i = 0; i < flatObjectGrid.length; i++) {
|
||||
int id = flatObjectGrid[i];
|
||||
|
||||
// Handle the 'secret' pushwalls (Logic check)
|
||||
if (id == MapObject.pushwallTrigger) {
|
||||
// In Wolf3D, ID 98 means the wall at this same index in Plane 0 is pushable.
|
||||
// You might want to mark this in your engine state.
|
||||
}
|
||||
|
||||
// Filter out invalid IDs for Shareware to prevent crashes
|
||||
if (isShareware && !MapObject.isSharewareCompatible(id)) {
|
||||
flatObjectGrid[i] = 0; // Turn unknown objects into empty space
|
||||
}
|
||||
}
|
||||
|
||||
List<List<int>> wallGrid = [];
|
||||
List<List<int>> objectGrid = [];
|
||||
|
||||
for (int y = 0; y < 64; y++) {
|
||||
List<int> wallRow = [];
|
||||
List<int> objectRow = [];
|
||||
for (int x = 0; x < 64; x++) {
|
||||
wallRow.add(flatWallGrid[y * 64 + x]);
|
||||
objectRow.add(flatObjectGrid[y * 64 + x]);
|
||||
}
|
||||
wallGrid.add(wallRow);
|
||||
objectGrid.add(objectRow);
|
||||
}
|
||||
|
||||
levels.add(
|
||||
WolfLevel(
|
||||
name: name,
|
||||
wallGrid: wallGrid,
|
||||
objectGrid: objectGrid,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return levels;
|
||||
}
|
||||
|
||||
// --- ALGORITHM 1: CARMACK EXPANSION ---
|
||||
static Uint16List _expandCarmack(Uint8List compressed) {
|
||||
ByteData data = ByteData.sublistView(compressed);
|
||||
|
||||
// The first 16-bit word is the total length of the expanded data in BYTES.
|
||||
int expandedLengthBytes = data.getUint16(0, Endian.little);
|
||||
int expandedLengthWords = expandedLengthBytes ~/ 2;
|
||||
Uint16List expanded = Uint16List(expandedLengthWords);
|
||||
|
||||
int inIdx = 2; // Skip the length word we just read
|
||||
int outIdx = 0;
|
||||
|
||||
while (outIdx < expandedLengthWords && inIdx < compressed.length) {
|
||||
int word = data.getUint16(inIdx, Endian.little);
|
||||
inIdx += 2;
|
||||
|
||||
int highByte = word >> 8;
|
||||
int lowByte = word & 0xFF;
|
||||
|
||||
// 0xA7 and 0xA8 are the Carmack Pointer Tags
|
||||
if (highByte == 0xA7 || highByte == 0xA8) {
|
||||
if (lowByte == 0) {
|
||||
// Exception Rule: If the length (lowByte) is 0, it's not a pointer.
|
||||
// It's literally just the tag byte followed by another byte.
|
||||
int nextByte = data.getUint8(inIdx++);
|
||||
expanded[outIdx++] = (nextByte << 8) | highByte;
|
||||
} else if (highByte == 0xA7) {
|
||||
// 0xA7 = Near Pointer (look back a few spaces)
|
||||
int offset = data.getUint8(inIdx++);
|
||||
int copyFrom = outIdx - offset;
|
||||
for (int i = 0; i < lowByte; i++) {
|
||||
expanded[outIdx++] = expanded[copyFrom++];
|
||||
}
|
||||
} else if (highByte == 0xA8) {
|
||||
// 0xA8 = Far Pointer (absolute offset from the very beginning)
|
||||
int offset = data.getUint16(inIdx, Endian.little);
|
||||
inIdx += 2;
|
||||
for (int i = 0; i < lowByte; i++) {
|
||||
expanded[outIdx++] = expanded[offset++];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Normal, uncompressed word
|
||||
expanded[outIdx++] = word;
|
||||
}
|
||||
}
|
||||
return expanded;
|
||||
}
|
||||
|
||||
// --- ALGORITHM 2: RLEW EXPANSION ---
|
||||
static List<int> _expandRlew(Uint16List carmackExpanded, int rlewTag) {
|
||||
// The first word is the expanded length in BYTES
|
||||
int expandedLengthBytes = carmackExpanded[0];
|
||||
int expandedLengthWords = expandedLengthBytes ~/ 2;
|
||||
List<int> rlewExpanded = List<int>.filled(expandedLengthWords, 0);
|
||||
|
||||
int inIdx = 1; // Skip the length word
|
||||
int outIdx = 0;
|
||||
|
||||
while (outIdx < expandedLengthWords && inIdx < carmackExpanded.length) {
|
||||
int word = carmackExpanded[inIdx++];
|
||||
|
||||
if (word == rlewTag) {
|
||||
// We found an RLEW tag!
|
||||
// The next word is the count, the word after that is the value.
|
||||
int count = carmackExpanded[inIdx++];
|
||||
int value = carmackExpanded[inIdx++];
|
||||
for (int i = 0; i < count; i++) {
|
||||
rlewExpanded[outIdx++] = value;
|
||||
}
|
||||
} else {
|
||||
// Normal word
|
||||
rlewExpanded[outIdx++] = word;
|
||||
}
|
||||
}
|
||||
return rlewExpanded;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user