#include "remorse/fixed_map.h" #include "remorse/file_system.h" namespace remorse { namespace { constexpr u32 fixedMapCountOffset = 0x54; constexpr u32 fixedMapTableOffset = 0x80; constexpr u32 fixedMapTableRowSize = 8; constexpr u32 fixedMapRecordSize = 16; constexpr u16 crusaderCoordinateScale = 2; std::optional readU16Le(const std::vector& bytes, u32 offset) { if (offset + 2 > bytes.size()) { return std::nullopt; } return static_cast(bytes[offset] | (static_cast(bytes[offset + 1]) << 8)); } std::optional readU32Le(const std::vector& bytes, u32 offset) { if (offset + 4 > bytes.size()) { return std::nullopt; } return static_cast(bytes[offset]) | (static_cast(bytes[offset + 1]) << 8) | (static_cast(bytes[offset + 2]) << 16) | (static_cast(bytes[offset + 3]) << 24); } std::optional readFixedMapEntry(const std::vector& bytes, u32 offset) { if (offset + fixedMapRecordSize > bytes.size()) { return std::nullopt; } const auto diskX = readU16Le(bytes, offset); const auto diskY = readU16Le(bytes, offset + 2); const auto shape = readU16Le(bytes, offset + 5); const auto flags = readU16Le(bytes, offset + 8); const auto quality = readU16Le(bytes, offset + 10); const auto next = readU16Le(bytes, offset + 14); if (!diskX || !diskY || !shape || !flags || !quality || !next) { return std::nullopt; } FixedMapEntry entry; entry.x = static_cast(*diskX * crusaderCoordinateScale); entry.y = static_cast(*diskY * crusaderCoordinateScale); entry.z = bytes[offset + 4]; entry.shape = *shape; entry.frame = bytes[offset + 7]; entry.flags = *flags; entry.quality = *quality; entry.npcNumber = bytes[offset + 12]; entry.mapNumber = bytes[offset + 13]; entry.next = *next; return entry; } } // namespace std::optional loadFixedMapArchive(const std::filesystem::path& path) { auto bytes = readBinaryFile(path); if (!bytes) { return std::nullopt; } const auto mapCount = readU16Le(*bytes, fixedMapCountOffset); if (!mapCount) { return std::nullopt; } const u32 tableEnd = fixedMapTableOffset + (static_cast(*mapCount) * fixedMapTableRowSize); if (tableEnd > bytes->size()) { return std::nullopt; } FixedMapArchive archive; archive.mapCount = *mapCount; archive.bytes = *bytes; return archive; } std::optional loadFixedMap(const FixedMapArchive& archive, u32 mapIndex) { if (mapIndex >= archive.mapCount) { return std::nullopt; } const u32 tableOffset = fixedMapTableOffset + (mapIndex * fixedMapTableRowSize); const auto mapOffset = readU32Le(archive.bytes, tableOffset); const auto mapSize = readU32Le(archive.bytes, tableOffset + 4); if (!mapOffset || !mapSize) { return std::nullopt; } if (*mapOffset > archive.bytes.size() || *mapSize > archive.bytes.size() - *mapOffset) { return std::nullopt; } if ((*mapSize % fixedMapRecordSize) != 0) { return std::nullopt; } FixedMap map; map.index = mapIndex; for (u32 offset = *mapOffset; offset < *mapOffset + *mapSize; offset += fixedMapRecordSize) { const auto entry = readFixedMapEntry(archive.bytes, offset); if (!entry) { return std::nullopt; } map.entries.push_back(*entry); } return map; } } // namespace remorse