Add core functionality for loading and processing game assets
- Implemented palette loading from binary files in `palette.cpp`.
- Added runtime utilities for reading text and JSON files in `runtime.cpp`.
- Created shape archive handling in `shape_archive.cpp`, including frame parsing and pixel decoding.
- Introduced type flag loading functionality in `type_flags.cpp`.
- Developed tests for combat tactics, entity dispatch entries, file system operations, fixed maps, flex archives, glob archives, map rendering, palette loading, shape archives, and type flags.
- Added a Python script for importing Ghidra-exported function slices with reporting capabilities.
2026-04-27 21:15:01 +02:00
|
|
|
#include "remorse/fixed_map.h"
|
|
|
|
|
|
|
|
|
|
#include "remorse/file_system.h"
|
Add comprehensive tests for entity dispatch, file system, fixed map, flex archive, isometric controls, and crash setup
- Enhance `entity_dispatch_entry_tests.cpp` with additional assertions for event type checks, group ID increments, and error handling.
- Refactor `file_system_tests.cpp` to streamline probe execution and enhance output validation.
- Extend `fixed_map_tests.cpp` to cover retail map loading, item classification, and process creation scenarios.
- Introduce new checks in `flex_archive_tests.cpp` for usecode loading and event handling, ensuring robust validation of game data.
- Add tests in `isometric_controls_tests.cpp` for controller direction mapping based on scan codes.
- Implement `test_crash_setup.cpp` to initialize crash diagnostics for improved error handling during tests.
2026-09-26 15:36:11 +02:00
|
|
|
#include "remorse/type_flags.h"
|
Add core functionality for loading and processing game assets
- Implemented palette loading from binary files in `palette.cpp`.
- Added runtime utilities for reading text and JSON files in `runtime.cpp`.
- Created shape archive handling in `shape_archive.cpp`, including frame parsing and pixel decoding.
- Introduced type flag loading functionality in `type_flags.cpp`.
- Developed tests for combat tactics, entity dispatch entries, file system operations, fixed maps, flex archives, glob archives, map rendering, palette loading, shape archives, and type flags.
- Added a Python script for importing Ghidra-exported function slices with reporting capabilities.
2026-04-27 21:15:01 +02:00
|
|
|
|
|
|
|
|
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<u16> readU16Le(const std::vector<u8>& bytes, u32 offset)
|
|
|
|
|
{
|
|
|
|
|
if (offset + 2 > bytes.size()) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<u32> readU32Le(const std::vector<u8>& bytes, u32 offset)
|
|
|
|
|
{
|
|
|
|
|
if (offset + 4 > bytes.size()) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
return static_cast<u32>(bytes[offset]) |
|
|
|
|
|
(static_cast<u32>(bytes[offset + 1]) << 8) |
|
|
|
|
|
(static_cast<u32>(bytes[offset + 2]) << 16) |
|
|
|
|
|
(static_cast<u32>(bytes[offset + 3]) << 24);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<FixedMapEntry> readFixedMapEntry(const std::vector<u8>& 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<u16>(*diskX * crusaderCoordinateScale);
|
|
|
|
|
entry.y = static_cast<u16>(*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<FixedMapArchive> 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<u32>(*mapCount) * fixedMapTableRowSize);
|
|
|
|
|
if (tableEnd > bytes->size()) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FixedMapArchive archive;
|
|
|
|
|
archive.mapCount = *mapCount;
|
|
|
|
|
archive.bytes = *bytes;
|
|
|
|
|
return archive;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<FixedMap> 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;
|
|
|
|
|
}
|
|
|
|
|
|
Add comprehensive tests for entity dispatch, file system, fixed map, flex archive, isometric controls, and crash setup
- Enhance `entity_dispatch_entry_tests.cpp` with additional assertions for event type checks, group ID increments, and error handling.
- Refactor `file_system_tests.cpp` to streamline probe execution and enhance output validation.
- Extend `fixed_map_tests.cpp` to cover retail map loading, item classification, and process creation scenarios.
- Introduce new checks in `flex_archive_tests.cpp` for usecode loading and event handling, ensuring robust validation of game data.
- Add tests in `isometric_controls_tests.cpp` for controller direction mapping based on scan codes.
- Implement `test_crash_setup.cpp` to initialize crash diagnostics for improved error handling during tests.
2026-09-26 15:36:11 +02:00
|
|
|
std::optional<std::vector<StagedFixedMapItem>> stageFixedMapItems(const FixedMap& map)
|
|
|
|
|
{
|
|
|
|
|
constexpr std::size_t firstFreeItemNumber = 0x100;
|
|
|
|
|
constexpr std::size_t freeItemCount = 0x2700;
|
|
|
|
|
std::vector<StagedFixedMapItem> items;
|
|
|
|
|
items.reserve(map.entries.size());
|
|
|
|
|
std::size_t allocated = 0;
|
|
|
|
|
for (std::size_t index = 0; index < map.entries.size(); ++index) {
|
|
|
|
|
if (map.entries[index].shape == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (allocated == freeItemCount) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
const u16 itemNumber = static_cast<u16>(firstFreeItemNumber + allocated++);
|
|
|
|
|
if ((map.entries[index].flags & 8) != 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
StagedFixedMapItem item;
|
|
|
|
|
item.itemNumber = itemNumber;
|
|
|
|
|
item.recordIndex = index;
|
|
|
|
|
item.data = map.entries[index];
|
|
|
|
|
item.data.x = static_cast<u16>(item.data.x / crusaderCoordinateScale);
|
|
|
|
|
item.data.y = static_cast<u16>(item.data.y / crusaderCoordinateScale);
|
|
|
|
|
item.data.flags &= static_cast<u16>(~1u);
|
|
|
|
|
items.push_back(item);
|
|
|
|
|
}
|
|
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<StagedFixedMapItem> nearestStagedFixedMapItem(
|
|
|
|
|
const std::vector<StagedFixedMapItem>& items, s32 worldX, s32 worldY, s32 maxDistance)
|
|
|
|
|
{
|
|
|
|
|
if (maxDistance < 0) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
const auto limit = static_cast<std::int64_t>(maxDistance) * maxDistance;
|
|
|
|
|
auto bestDistance = limit + 1;
|
|
|
|
|
const StagedFixedMapItem* best = nullptr;
|
|
|
|
|
for (const auto& item : items) {
|
|
|
|
|
const auto deltaX = static_cast<std::int64_t>(item.data.x) * crusaderCoordinateScale - worldX;
|
|
|
|
|
const auto deltaY = static_cast<std::int64_t>(item.data.y) * crusaderCoordinateScale - worldY;
|
|
|
|
|
const auto distance = deltaX * deltaX + deltaY * deltaY;
|
|
|
|
|
if (distance <= limit && distance < bestDistance) {
|
|
|
|
|
bestDistance = distance;
|
|
|
|
|
best = &item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return best == nullptr ? std::nullopt : std::optional<StagedFixedMapItem>(*best);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool itemGrabCanCreateProcess(const StagedFixedMapItem& item)
|
|
|
|
|
{
|
|
|
|
|
return (item.data.flags & 0x02) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u8 classifyItemFindZMatch(const StagedFixedMapItem& origin, const ShapeTypeInfo& originShape,
|
|
|
|
|
const StagedFixedMapItem& candidate, const ShapeTypeInfo& candidateShape, u8 zMatchFlags)
|
|
|
|
|
{
|
|
|
|
|
if (candidate.itemNumber == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
const auto height = [](const ShapeTypeInfo& shape) {
|
|
|
|
|
return shape.z == 0 && (shape.flags & 1) == 0 ? 1 : static_cast<int>(shape.z) * 8;
|
|
|
|
|
};
|
|
|
|
|
const int minimumX = static_cast<int>(candidate.data.x) - static_cast<int>(candidateShape.x) * 16 + 1;
|
|
|
|
|
const int minimumY = static_cast<int>(candidate.data.y) - static_cast<int>(candidateShape.y) * 16 + 1;
|
|
|
|
|
if (minimumX > origin.data.x || minimumY > origin.data.y) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
const u8 originTop = static_cast<u8>(static_cast<int>(origin.data.z) + height(originShape));
|
|
|
|
|
if ((zMatchFlags & 1) != 0 && candidate.data.z == originTop) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if ((zMatchFlags & 2) != 0 && origin.data.z == static_cast<int>(candidate.data.z) + height(candidateShape)) {
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ItemFindArea itemFindAreaAroundItem(const StagedFixedMapItem& origin, const ShapeTypeInfo& originShape)
|
|
|
|
|
{
|
|
|
|
|
const auto normalize = [](u16 value, u16 negativeValue) {
|
|
|
|
|
return static_cast<s16>(value) < 0 ? negativeValue : value;
|
|
|
|
|
};
|
|
|
|
|
const u16 minX = static_cast<u16>(static_cast<int>(origin.data.x) - originShape.x * 16 + 1);
|
|
|
|
|
const u16 minY = static_cast<u16>(static_cast<int>(origin.data.y) - originShape.y * 16 + 1);
|
|
|
|
|
const u16 maxX = static_cast<u16>(origin.data.x + 0x100);
|
|
|
|
|
const u16 maxY = static_cast<u16>(origin.data.y + 0x100);
|
|
|
|
|
return {normalize(minX, 0), normalize(minY, 0),
|
|
|
|
|
normalize(maxX, 0x7fff), normalize(maxY, 0x7fff)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<u16> itemFindBlockOrder(const ItemFindArea& area)
|
|
|
|
|
{
|
|
|
|
|
std::vector<u16> blocks;
|
|
|
|
|
if (area.minX > area.maxX || area.minY > area.maxY) {
|
|
|
|
|
return blocks;
|
|
|
|
|
}
|
|
|
|
|
for (u16 blockY = area.minY >> 9; blockY <= (area.maxY >> 9); ++blockY) {
|
|
|
|
|
for (u16 blockX = area.minX >> 9; blockX <= (area.maxX >> 9); ++blockX) {
|
|
|
|
|
blocks.push_back(static_cast<u16>(blockX * 0x40 + blockY));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return blocks;
|
|
|
|
|
}
|
|
|
|
|
|
Add core functionality for loading and processing game assets
- Implemented palette loading from binary files in `palette.cpp`.
- Added runtime utilities for reading text and JSON files in `runtime.cpp`.
- Created shape archive handling in `shape_archive.cpp`, including frame parsing and pixel decoding.
- Introduced type flag loading functionality in `type_flags.cpp`.
- Developed tests for combat tactics, entity dispatch entries, file system operations, fixed maps, flex archives, glob archives, map rendering, palette loading, shape archives, and type flags.
- Added a Python script for importing Ghidra-exported function slices with reporting capabilities.
2026-04-27 21:15:01 +02:00
|
|
|
} // namespace remorse
|