Add comprehensive tests for collision detection, entity dispatch, file system, isometric controls, map rendering, and sorting

- Implemented collision tests to verify overlap and movement logic for collision bodies.
- Enhanced entity dispatch entry tests to cover initialization, group ID management, and process handling.
- Expanded file system tests to include crash diagnostics and ensure proper game data root detection.
- Created isometric controls tests to validate movement requests and screen projection calculations.
- Added map renderer tests to check fixed map loading and rendering functionality.
- Introduced map sorter tests to confirm correct rendering order for various items.
This commit is contained in:
Maddo 2026-09-25 21:41:20 +02:00
commit 202679f857
27 changed files with 3530 additions and 81 deletions

View file

@ -0,0 +1,149 @@
#include "remorse/animation_archive.h"
#include "remorse/file_system.h"
#include "remorse/isometric_controls.h"
#include "remorse/shape_archive.h"
#include <cassert>
#include <filesystem>
namespace {
void writeU32Le(std::vector<remorse::u8>& bytes, std::size_t offset, remorse::u32 value)
{
for (std::size_t index = 0; index < 4; ++index) {
bytes[offset + index] = static_cast<remorse::u8>(value >> (index * 8));
}
}
remorse::AnimationArchive makeSyntheticArchive()
{
constexpr remorse::u32 shapeTable = 0x20;
constexpr remorse::u32 clipOffset = 0x440;
std::vector<remorse::u8> bytes(clipOffset + 4 + 2 * 16 * 8);
writeU32Le(bytes, 4, shapeTable);
writeU32Le(bytes, shapeTable + 4, clipOffset);
bytes[clipOffset] = 2;
bytes[clipOffset + 1] = 5;
bytes[clipOffset + 2] = 0x02;
bytes[clipOffset + 3] = 0x40;
const std::size_t frameOffset = clipOffset + 4 + (5 * 2 + 1) * 8;
bytes[frameOffset] = 0x34;
bytes[frameOffset + 1] = 0x81;
bytes[frameOffset + 2] = 0xfe;
bytes[frameOffset + 4] = 0xff;
bytes[frameOffset + 5] = 0x01;
return remorse::AnimationArchive{std::move(bytes)};
}
} // namespace
int main()
{
const auto synthetic = makeSyntheticArchive();
const auto sixteenDirectionClip = remorse::loadAnimationClip(synthetic, 1, 1);
assert(sixteenDirectionClip.has_value());
assert(sixteenDirectionClip->frameCount == 2);
assert(sixteenDirectionClip->directionCount == 16);
const auto selectedFrame = remorse::getAnimationFrame(*sixteenDirectionClip, 5, 1);
assert(selectedFrame.has_value());
assert(remorse::getAnimationShapeFrame(*selectedFrame) == 0x134);
assert(remorse::isAnimationFrameFlipped(*selectedFrame));
const auto rotatedStep = remorse::decodeAnimationDisplacement(*selectedFrame, 5, true);
assert(rotatedStep.has_value());
assert(rotatedStep->x == 2);
assert(rotatedStep->y == -4);
assert(rotatedStep->z == -2);
assert(!rotatedStep->onGround);
assert(!remorse::getAnimationFrame(*sixteenDirectionClip, 16, 0));
assert(!remorse::getAnimationFrame(*sixteenDirectionClip, 0, 2));
remorse::AnimationFrameMetadata negativeStride;
negativeStride.bytes[4] = 0xff;
negativeStride.bytes[5] = 0x01;
const auto northStep = remorse::decodeAnimationDisplacement(negativeStride, 0, false);
assert(northStep.has_value());
assert(northStep->x == 0);
assert(northStep->y == 2);
negativeStride.bytes[5] = 0x03;
const auto groundedStep = remorse::decodeAnimationDisplacement(negativeStride, 0, false);
assert(groundedStep.has_value() && groundedStep->onGround);
remorse::AnimationFrameMetadata positiveStride;
positiveStride.bytes[4] = 1;
const remorse::s32 expectedX[16] = {
0, 2, 2, 4, 2, 4, 2, 2, 0, -2, -2, -4, -2, -4, -2, -2};
const remorse::s32 expectedY[16] = {
-2, -4, -2, -2, 0, 2, 2, 4, 2, 4, 2, 2, 0, -2, -2, -4};
for (remorse::u8 direction = 0; direction < 16; ++direction) {
const auto step = remorse::decodeAnimationDisplacement(positiveStride, direction, false);
assert(step.has_value());
assert(step->x == expectedX[direction]);
assert(step->y == expectedY[direction]);
}
remorse::AnimationArchive malformed;
malformed.bytes.resize(8);
writeU32Le(malformed.bytes, 4, 0xfffffff0);
assert(!remorse::loadAnimationClip(malformed, 1, 0));
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
assert(root.has_value());
const auto animationPath = remorse::resolveGameDataPath(
*root, std::filesystem::path("STATIC") / "ANIM.DAT");
assert(animationPath.has_value());
const auto animationArchive = remorse::loadAnimationArchive(*animationPath);
assert(animationArchive.has_value());
const auto stand = remorse::loadAnimationClip(*animationArchive, 1, 0);
const auto walk = remorse::loadAnimationClip(*animationArchive, 1, 1);
const auto run = remorse::loadAnimationClip(*animationArchive, 1, 3);
assert(stand.has_value() && stand->frameCount > 0);
assert(walk.has_value() && walk->frameCount == 17);
assert(run.has_value() && run->frameCount == 12);
assert(remorse::getAnimationFrame(*walk, 0, 0).has_value());
assert(remorse::getAnimationFrame(*run, 15, 11).has_value());
for (remorse::u8 direction : {remorse::u8{6}, remorse::u8{7}, remorse::u8{10}, remorse::u8{11}}) {
for (remorse::u8 frame : {remorse::u8{4}, remorse::u8{12}}) {
const auto metadata = remorse::getAnimationFrame(*walk, direction, frame);
assert(metadata.has_value() && (metadata->bytes[5] & 1) != 0);
const auto authored = remorse::decodeAnimationDisplacement(*metadata, direction, false);
const auto host = remorse::hostMovementStep(*metadata, direction, false);
assert(authored && (authored->x < 0 || authored->y < 0));
assert(host && host->x == 0 && host->y == 0);
}
}
const auto runFrame = remorse::getAnimationFrame(*run, 14, 6);
assert(runFrame.has_value());
const auto hostRun = remorse::hostMovementStep(*runFrame, 14, false);
assert(hostRun && hostRun->x < 0 && hostRun->y < 0);
const auto shapesPath = remorse::resolveGameDataPath(
*root, std::filesystem::path("STATIC") / "SHAPES.FLX");
assert(shapesPath.has_value());
const auto shapeArchive = remorse::loadShapeArchive(*shapesPath);
assert(shapeArchive.has_value());
const auto playerFrames = remorse::loadShapeFrames(*shapeArchive, 1);
assert(playerFrames.has_value() && playerFrames->size() >= walk->frameCount);
std::vector<bool> referencedFrames(playerFrames->size(), false);
for (const remorse::AnimationClip* clip : {&*stand, &*walk, &*run}) {
for (remorse::u8 direction = 0; direction < 16; ++direction) {
for (remorse::u8 frame = 0; frame < clip->frameCount; ++frame) {
const auto metadata = remorse::getAnimationFrame(*clip, direction, frame);
assert(metadata.has_value());
const remorse::u16 shapeFrame = remorse::getAnimationShapeFrame(*metadata);
assert(shapeFrame < referencedFrames.size());
referencedFrames[shapeFrame] = true;
}
}
}
bool decodedReferencedFrame = false;
for (std::size_t frame = 0; frame < referencedFrames.size(); ++frame) {
if (referencedFrames[frame]) {
assert(remorse::decodeShapeFrame(*shapeArchive, 1, static_cast<remorse::u32>(frame)).has_value());
decodedReferencedFrame = true;
break;
}
}
assert(decodedReferencedFrame);
return 0;
}

View file

@ -0,0 +1,72 @@
#include "remorse/collision.h"
#include <cassert>
int main()
{
const remorse::CollisionBody actor{{100, 100, 0}, 96, 96, 80};
const remorse::CollisionBody wall{{300, 100, 0}, 64, 96, 128};
assert(!remorse::collisionBodiesOverlap(actor, wall));
assert(remorse::canMoveCollisionBody(actor, {200, 100, 0}, {wall}));
assert(!remorse::canMoveCollisionBody(actor, {260, 100, 0}, {wall}));
assert(!remorse::canMoveCollisionBody(actor, {400, 100, 0}, {wall}));
const remorse::CollisionBody floorBelow{{300, 100, -20}, 64, 96, 20};
const remorse::CollisionBody overhead{{300, 100, 80}, 64, 96, 32};
assert(!remorse::collisionBodiesOverlap(actor, floorBelow));
assert(!remorse::collisionBodiesOverlap(actor, overhead));
const remorse::CollisionBody initiallyOverlapping{{250, 100, 0}, 96, 96, 80};
assert(remorse::canMoveCollisionBody(initiallyOverlapping, {200, 100, 0}, {wall}));
assert(!remorse::canMoveCollisionBody(initiallyOverlapping, {400, 100, 0}, {wall}));
remorse::ShapeTypeInfo actorInfo;
actorInfo.x = 3;
actorInfo.y = 3;
actorInfo.z = 10;
actorInfo.flags = 0x2;
const remorse::CollisionBody authoredActor = remorse::makeCollisionBody({100, 100, 0}, actorInfo);
assert(authoredActor.extentX == 96);
assert(authoredActor.extentY == 96);
assert(authoredActor.extentZ == 80);
remorse::ShapeTypeInfo obstacleInfo;
obstacleInfo.x = 2;
obstacleInfo.y = 4;
obstacleInfo.z = 8;
const remorse::CollisionBody flipped = remorse::makeCollisionBody({300, 300, 0}, obstacleInfo, true);
assert(flipped.extentX == 128);
assert(flipped.extentY == 64);
remorse::FixedMap map;
remorse::FixedMapEntry solid;
solid.x = 500;
solid.y = 600;
solid.shape = 1;
map.entries.push_back(solid);
remorse::FixedMapEntry globParent;
globParent.x = 0x480;
globParent.y = 0x640;
globParent.shape = 2;
globParent.quality = 7;
map.entries.push_back(globParent);
std::vector<remorse::ShapeTypeInfo> shapeTypes(4);
shapeTypes[1] = obstacleInfo;
shapeTypes[1].flags = 0x2;
shapeTypes[2].family = 3;
shapeTypes[2].flags = 0x2;
shapeTypes[3] = obstacleInfo;
shapeTypes[3].flags = 0x2;
remorse::GlobObject glob;
glob.index = 7;
glob.entries.push_back(remorse::GlobObjectEntry{2, 3, 4, 3, 0});
const auto bodies = remorse::collectMapCollisionBodies(map, shapeTypes, {glob});
assert(bodies.size() == 2);
assert(bodies[0].location.x == 500 && bodies[0].location.y == 600);
assert(bodies[1].location.x == (0x480 & ~0x3ff) + 2 * 4 + 2);
assert(bodies[1].location.y == (0x640 & ~0x3ff) + 3 * 4 + 2);
assert(bodies[1].location.z == 4);
return 0;
}

View file

@ -1,11 +1,30 @@
#include "remorse/entity_dispatch_entry.h"
#include <cassert>
#include <cstring>
#include <stdexcept>
#include <vector>
int main()
{
remorse::EntityDispatchEntryBase entry{};
remorse::EntityDispatchEntry::initBase(&entry);
remorse::u16 groupCounts[32]{};
for (remorse::u8& byte : entry.bytes) {
byte = 0xa5;
}
assert(remorse::EntityDispatchEntry::initBase(&entry, 1, groupCounts) == &entry);
assert(entry.bytes[0x00] == 0x06 && entry.bytes[0x01] == 0x3b);
assert(entry.bytes[0x09] == 0xa5);
assert(entry.bytes[0x1e] == 0x10 && entry.bytes[0x1f] == 0x2d);
assert(entry.bytes[0x28] == 0xfe && entry.bytes[0x29] == 0x3a);
for (remorse::u16 offset : {remorse::u16{0x02}, remorse::u16{0x04}, remorse::u16{0x0a}, remorse::u16{0x12}, remorse::u16{0x16}, remorse::u16{0x1a}, remorse::u16{0x20}, remorse::u16{0x26}, remorse::u16{0x2a}, remorse::u16{0x30}}) {
assert(entry.bytes[offset] == 0 && entry.bytes[offset + 1] == 0);
}
assert(entry.bytes[0x06] == 1 && entry.bytes[0x07] == 0);
assert(entry.bytes[0x08] == 1 && groupCounts[1] == 1);
assert(entry.bytes[0x18] == 1 && entry.bytes[0x19] == 0);
assert(remorse::EntityDispatchEntry::initBase(nullptr, 1, groupCounts) == nullptr);
assert(groupCounts[1] == 1);
remorse::EntityDispatchEntry::setSourceType(entry, 0x1234);
assert(entry.bytes[0x04] == 0x34);
@ -15,12 +34,446 @@ int main()
assert(entry.bytes[0x06] == 0xf2);
assert(entry.bytes[0x07] == 0x00);
entry.bytes[0x08] = 0xe0;
remorse::EntityDispatchEntry::setGroupId(entry, 0x23);
assert(entry.bytes[0x08] == 0xe3);
remorse::EntityDispatchEntry::sleep(entry, 0x12345678, 0x0001003c);
assert(entry.bytes[0x18] == 0x01 && entry.bytes[0x19] == 0x10);
assert(entry.bytes[0x0a] == 0x78 && entry.bytes[0x0b] == 0x56);
assert(entry.bytes[0x0c] == 0x34 && entry.bytes[0x0d] == 0x12);
assert(entry.bytes[0x0e] == 0x3c && entry.bytes[0x0f] == 0x00);
assert(entry.bytes[0x10] == 0x01 && entry.bytes[0x11] == 0x00);
remorse::EntityDispatchEntry::setGroupId(entry, 3, groupCounts);
assert(groupCounts[1] == 0 && groupCounts[3] == 1);
entry.bytes[0x08] = 0xe3;
remorse::EntityDispatchEntry::setGroupId(entry, 3, groupCounts);
assert(entry.bytes[0x08] == 0xe3 && groupCounts[3] == 1);
remorse::EntityDispatchEntry::setGroupId(entry, 3, groupCounts);
assert(groupCounts[3] == 1);
remorse::EntityDispatchEntry::setGroupId(entry, 4, groupCounts);
assert(entry.bytes[0x08] == 4 && groupCounts[3] == 0 && groupCounts[4] == 1);
for (remorse::u8 invalidId : {remorse::u8{0}, remorse::u8{32}}) {
bool rejected = false;
try {
remorse::EntityDispatchEntry::setGroupId(entry, invalidId, groupCounts);
} catch (const std::invalid_argument&) {
rejected = true;
}
assert(rejected && entry.bytes[0x08] == 4 && groupCounts[4] == 1);
}
remorse::EntityDispatchEntry::incrementGroupId(entry);
assert(entry.bytes[0x08] == 0xe4);
assert(entry.bytes[0x08] == 5);
remorse::ProcessKernel kernel;
kernel.setLastAllocationSize(sizeof(entry));
assert(kernel.addProcess(entry) == 0);
assert(entry.bytes[0x02] == 0 && entry.bytes[0x03] == 0);
assert(kernel.processAt(0) == &entry);
assert(kernel.allocationSizeAt(0) == 0x32);
assert(kernel.processCount() == 1 && kernel.capacity() == 30);
assert(std::strcmp(kernel.processNameAt(0), "Default") == 0);
remorse::EntityDispatchEntryBase second{};
kernel.setMarkNewProcesses(true);
assert(kernel.addProcess(second) == 1);
assert(second.bytes[0x02] == 1 && second.bytes[0x03] == 0);
assert(kernel.allocationSizeAt(1) == 0);
assert(second.bytes[0x19] == 0x02 && kernel.newlyCreatedCount() == 1);
kernel.setMarkNewProcesses(false);
kernel.clearProcessSlot(1);
assert(kernel.processAt(1) == nullptr && kernel.allocationSizeAt(1) == 0);
assert(kernel.processNameAt(1) == nullptr && kernel.processCount() == 2);
remorse::EntityDispatchEntryBase reused{};
assert(kernel.addProcess(reused) == 1 && kernel.processAt(1) == &reused);
std::vector<remorse::EntityDispatchEntryBase> additional(29);
for (auto& process : additional) {
kernel.addProcess(process);
}
assert(kernel.processAt(30) == &additional.back());
assert(kernel.capacity() == 60 && kernel.processCount() == 32);
remorse::ProcessKernel createdKernel;
remorse::u16 createdGroups[32]{};
auto* created = createdKernel.createProcess(2, createdGroups);
assert(created != nullptr && createdKernel.processAt(0) == created);
assert(createdKernel.processCount() == 1 && createdKernel.allocationSizeAt(0) == 0x32);
assert(created->bytes[0x02] == 0 && created->bytes[0x03] == 0);
assert(created->bytes[0x08] == 2 && createdGroups[2] == 1);
createdKernel.setSyncContext(true, 1);
auto* next = createdKernel.createProcess(2, createdGroups);
assert(createdKernel.processAt(1) == next && createdGroups[2] == 2);
assert(next->bytes[0x02] == 1 && next->bytes[0x03] == 0);
assert(next->bytes[0x16] == 0);
createdKernel.setSyncContext(true, 0);
auto* syncing = createdKernel.createProcess(2, createdGroups);
assert(createdKernel.processAt(2) == syncing && createdGroups[2] == 3);
assert(syncing->bytes[0x16] == 0x40 && syncing->bytes[0x17] == 0);
createdKernel.setSyncContext(true, 1);
next->bytes[0x18] |= 0x02;
auto* retiring = createdKernel.createProcess(2, createdGroups);
assert(createdKernel.processAt(3) == retiring && createdKernel.processCount() == 4);
assert(next->bytes[0x18] == 0x07 && retiring->bytes[0x16] == 0x40);
createdKernel.terminateProcess(3, createdGroups);
assert(createdKernel.processAt(3) == retiring && (retiring->bytes[0x16] & 0x08) != 0);
assert(createdKernel.processCount() == 3 && createdKernel.terminatedProcessCount() == 1);
assert(createdGroups[2] == 3);
bool alreadyTerminated = false;
try {
createdKernel.terminateProcess(3, createdGroups);
} catch (const std::logic_error&) {
alreadyTerminated = true;
}
assert(alreadyTerminated && createdGroups[2] == 3 && createdKernel.terminatedProcessCount() == 1);
createdKernel.setSyncContext(true, 3);
auto* afterTerminated = createdKernel.createProcess(2, createdGroups);
assert((retiring->bytes[0x18] & 0x04) == 0 && (afterTerminated->bytes[0x16] & 0x40) != 0);
createdKernel.terminateProcess(4, createdGroups);
createdKernel.clearTerminatedProcesses();
assert(createdKernel.processAt(3) == nullptr && createdKernel.allocationSizeAt(3) == 0);
assert(createdKernel.processNameAt(3) == nullptr && createdKernel.terminatedProcessCount() == 0);
auto* recycled = createdKernel.createProcess(2, createdGroups);
assert(createdKernel.processAt(3) == recycled && recycled->bytes[0x02] == 3);
assert(createdKernel.processCount() == 4 && createdGroups[2] == 4);
auto* immediate = createdKernel.createProcess(2, createdGroups);
immediate->bytes[0x18] |= 0x04;
createdKernel.terminateProcess(4, createdGroups);
assert(createdKernel.processAt(4) == nullptr && createdKernel.processCount() == 4);
assert(createdKernel.terminatedProcessCount() == 0 && createdGroups[2] == 4);
remorse::ProcessKernel fullKernel;
std::vector<remorse::EntityDispatchEntryBase> fullTable(1020);
for (auto& process : fullTable) {
fullKernel.addProcess(process);
}
remorse::u16 fullGroups[32]{};
bool exhausted = false;
try {
fullKernel.createProcess(2, fullGroups);
} catch (const std::overflow_error&) {
exhausted = true;
}
assert(exhausted && fullKernel.processCount() == 1020 && fullGroups[2] == 0);
exhausted = false;
try {
fullKernel.createKeyboardProcess(2, fullGroups);
} catch (const std::overflow_error&) {
exhausted = true;
}
assert(exhausted && fullKernel.processCount() == 1020 && fullGroups[2] == 0);
exhausted = false;
try {
fullKernel.createSystemTimerProcess(2, fullGroups);
} catch (const std::overflow_error&) {
exhausted = true;
}
assert(exhausted && fullKernel.processCount() == 1020 && fullGroups[2] == 0);
remorse::ProcessKernel scheduler;
remorse::u16 scheduledGroups[32]{};
auto* otherGroup = scheduler.createProcess(1, scheduledGroups);
auto* matching = scheduler.createProcess(2, scheduledGroups);
auto* syncingProcess = scheduler.createProcess(2, scheduledGroups);
auto* specialGroup = scheduler.createProcess(30, scheduledGroups);
syncingProcess->bytes[0x16] = 0x40;
std::vector<remorse::u16> ran;
auto recordRun = [](remorse::EntityDispatchEntryBase& process, void* context) {
auto& ids = *static_cast<std::vector<remorse::u16>*>(context);
ids.push_back(static_cast<remorse::u16>(process.bytes[0x02] | process.bytes[0x03] << 8));
};
scheduler.scanRunnableProcesses(2, recordRun, &ran);
assert((ran == std::vector<remorse::u16>{1, 3}));
assert(syncingProcess->bytes[0x16] == 0 && otherGroup->bytes[0x16] == 0);
ran.clear();
matching->bytes[0x16] = 1;
scheduler.scanRunnableProcesses(2, recordRun, &ran);
assert((ran == std::vector<remorse::u16>{2, 3}));
assert(specialGroup->bytes[0x08] == 30);
remorse::ProcessKernel spawningKernel;
remorse::u16 spawningGroups[32]{};
spawningKernel.createProcess(1, spawningGroups);
auto* spawningProcess = spawningKernel.createProcess(2, spawningGroups);
spawningProcess->bytes[0x18] |= 0x02;
struct SpawnContext {
remorse::ProcessKernel* kernel;
remorse::u16 (*groups)[32];
remorse::EntityDispatchEntryBase* spawned = nullptr;
int runs = 0;
} spawnContext{&spawningKernel, &spawningGroups};
auto spawnRun = [](remorse::EntityDispatchEntryBase&, void* context) {
auto& state = *static_cast<SpawnContext*>(context);
++state.runs;
if (state.spawned == nullptr) {
state.spawned = state.kernel->createProcess(2, *state.groups);
}
};
spawningKernel.scanRunnableProcesses(2, spawnRun, &spawnContext);
assert(spawnContext.runs == 1 && spawnContext.spawned == spawningKernel.processAt(2));
assert(spawningProcess->bytes[0x18] == 0x07 && spawnContext.spawned->bytes[0x16] == 0);
remorse::ProcessKernel retiringKernel;
remorse::u16 retiringGroups[32]{};
for (int index = 0; index < 3; ++index) {
retiringKernel.createProcess(1, retiringGroups);
}
retiringKernel.createProcess(2, retiringGroups);
struct RetireContext {
remorse::ProcessKernel* kernel;
remorse::u16 (*groups)[32];
} retireContext{&retiringKernel, &retiringGroups};
auto retireRun = [](remorse::EntityDispatchEntryBase& process, void* context) {
auto& state = *static_cast<RetireContext*>(context);
state.kernel->terminateProcess(static_cast<remorse::u16>(process.bytes[0x02]), *state.groups);
};
retiringKernel.scanRunnableProcesses(2, retireRun, &retireContext);
assert(retiringKernel.processAt(3) == nullptr && retiringKernel.terminatedProcessCount() == 0);
assert(retiringKernel.processCount() == 3 && retiringGroups[2] == 0);
remorse::ProcessKernel sleepingKernel;
remorse::u16 sleepingGroups[32]{};
auto* sleeper = sleepingKernel.createProcess(2, sleepingGroups);
remorse::EntityDispatchEntry::sleep(*sleeper, 100, 60);
ran.clear();
sleepingKernel.scanRunnableProcesses(2, recordRun, &ran, 160);
assert(ran.empty() && sleeper->bytes[0x19] == 0x10);
sleepingKernel.scanRunnableProcesses(2, recordRun, &ran, 161);
assert((ran == std::vector<remorse::u16>{0}));
assert(sleeper->bytes[0x19] == 0);
for (remorse::u16 offset = 0x0a; offset < 0x12; ++offset) {
assert(sleeper->bytes[offset] == 0);
}
remorse::ProcessKernel standKernel;
remorse::u16 standGroups[32]{};
standKernel.createProcess(2, standGroups);
assert(!standKernel.itemHasProcess(0x1234) && !standKernel.itemHasProcess(0x1235));
auto* standEntry = standKernel.createStandProcess(2, 0x1234, standGroups);
auto* standing = &standEntry->base;
assert(standKernel.processAt(1) == standing && standKernel.allocationSizeAt(1) == 0x42);
assert(std::strcmp(standKernel.processNameAt(1), "Stand") == 0 && standGroups[2] == 2);
assert(standing->bytes[0] == 0x0f && standing->bytes[1] == 0x1f);
assert(standing->bytes[0x04] == 0x34 && standing->bytes[0x05] == 0x12);
assert(standing->bytes[0x06] == 0x58 && standing->bytes[0x07] == 0x02);
assert(standing->bytes[0x17] == 0x03 && standEntry->extra[0] == 0x01 && standEntry->extra[1] == 0x01);
assert(standEntry->extra[0x0e] == 0x34 && standEntry->extra[0x0f] == 0x12);
assert(standKernel.itemHasProcess(0x1234) && !standKernel.itemHasProcess(0x1235));
bool itemTeardownRejected = false;
try {
standKernel.terminateProcess(1, standGroups);
} catch (const std::logic_error&) {
itemTeardownRejected = true;
}
assert(itemTeardownRejected && standGroups[2] == 2 && standKernel.processAt(1) == standing);
struct StandContext {
remorse::u32 tick;
bool visible;
remorse::u16 activity = 0;
bool currentActivity = false;
bool canSee = false;
remorse::u16 startedNpc = 0;
remorse::u16 startedActivity = 0;
int starts = 0;
int runs = 0;
remorse::StandProcessEntry* entry = nullptr;
remorse::NpcBehaviorServices* services = nullptr;
} standContext{99, false};
remorse::NpcBehaviorServices standServices{
[](remorse::u16 npcNumber, void* context) {
assert(npcNumber == 0x1234);
return static_cast<StandContext*>(context)->visible;
},
[](remorse::u16 npcNumber, void* context) {
assert(npcNumber == 0x1234);
return static_cast<StandContext*>(context)->activity;
},
[](remorse::u16 npcNumber, remorse::u16 activity, void* context) {
assert(npcNumber == 0x1234 && activity == 0x71);
return static_cast<StandContext*>(context)->currentActivity;
},
[](remorse::u16 npcNumber, void* context) {
assert(npcNumber == 0x1234);
return static_cast<StandContext*>(context)->canSee;
},
[](remorse::u16 npcNumber, remorse::u16 activity, void* context) {
auto& state = *static_cast<StandContext*>(context);
state.startedNpc = npcNumber;
state.startedActivity = activity;
++state.starts;
},
&standContext
};
standContext.entry = standEntry;
standContext.services = &standServices;
auto standRun = [](remorse::EntityDispatchEntryBase& process, void* context) {
auto& state = *static_cast<StandContext*>(context);
if (process.bytes[0] != 0x0f || process.bytes[1] != 0x1f) {
return;
}
++state.runs;
remorse::StandProcess::run(*state.entry, *state.services, state.tick);
};
standKernel.scanRunnableProcesses(2, standRun, &standContext, standContext.tick);
assert(standContext.runs == 1 && standing->bytes[0x19] == 0x10);
assert(standing->bytes[0x0a] == 99 && standing->bytes[0x0e] == 60);
assert(standContext.starts == 0);
standContext.tick = 159;
standKernel.scanRunnableProcesses(2, standRun, &standContext, standContext.tick);
assert(standContext.runs == 1 && standing->bytes[0x19] == 0x10);
standContext.tick = 160;
standContext.visible = true;
standContext.activity = 0x71;
standContext.canSee = true;
standKernel.scanRunnableProcesses(2, standRun, &standContext, standContext.tick);
assert(standContext.runs == 2 && standing->bytes[0x19] == 0 && standing->bytes[0x0e] == 0);
assert(standContext.starts == 1 && standContext.startedNpc == 0x1234 && standContext.startedActivity == 0x71);
standContext.tick = 161;
standContext.currentActivity = true;
standKernel.scanRunnableProcesses(2, standRun, &standContext, standContext.tick);
assert(standContext.runs == 3 && standContext.starts == 1 && standing->bytes[0x0e] == 60);
remorse::ProcessKernel itemKernel;
remorse::u16 itemGroups[32]{};
for (int index = 0; index < 3; ++index) {
itemKernel.createProcess(1, itemGroups);
}
auto* firstStand = itemKernel.createStandProcess(2, 0x1234, itemGroups);
auto* secondStand = itemKernel.createStandProcess(2, 0x1234, itemGroups);
auto* otherStand = itemKernel.createStandProcess(2, 0x1235, itemGroups);
assert(itemKernel.itemHasProcess(0x1234) && itemKernel.itemHasProcess(0x1235));
itemKernel.terminateProcess(3, itemGroups);
assert(itemKernel.itemHasProcess(0x1234) && itemKernel.processAt(3) == &firstStand->base);
itemKernel.clearTerminatedProcesses();
assert(itemKernel.processAt(3) == nullptr && itemKernel.itemHasProcess(0x1234));
secondStand->base.bytes[0x18] |= 0x04;
itemKernel.terminateProcess(4, itemGroups);
assert(itemKernel.processAt(4) == nullptr && !itemKernel.itemHasProcess(0x1234));
assert(itemKernel.itemHasProcess(0x1235) && itemKernel.processAt(5) == &otherStand->base);
assert(itemGroups[2] == 1 && itemKernel.processCount() == 4);
remorse::SystemTimerProcessEntry systemTimer{};
systemTimer.extra[0x0e] = 0xff;
systemTimer.extra[0x0f] = 0xff;
assert(remorse::SystemTimerProcess::tick(systemTimer) == 0xffff);
remorse::SystemTimerProcess::increment(systemTimer);
assert(remorse::SystemTimerProcess::tick(systemTimer) == 0x10000);
assert(systemTimer.extra[0x0e] == 0 && systemTimer.extra[0x0f] == 0);
assert(systemTimer.extra[0x10] == 1 && systemTimer.extra[0x11] == 0);
remorse::ProcessKernel timerKernel;
remorse::u16 timerGroups[32]{};
bool missingTimerRejected = false;
try {
timerKernel.deliverSystemTimerEvent();
} catch (const std::logic_error&) {
missingTimerRejected = true;
}
assert(missingTimerRejected);
for (int index = 0; index < 3; ++index) {
timerKernel.createProcess(1, timerGroups);
}
auto* registeredTimer = timerKernel.createSystemTimerProcess(1, timerGroups);
assert(timerKernel.processAt(3) == &registeredTimer->base);
assert(timerKernel.allocationSizeAt(3) == 0x4a && timerGroups[1] == 4);
assert(std::strcmp(timerKernel.processNameAt(3), "SystemTimer") == 0);
assert(registeredTimer->base.bytes[0x16] == 3 && registeredTimer->base.bytes[0x17] == 0x27);
assert(registeredTimer->base.bytes[0x18] == 1 && registeredTimer->extra[0] == 2);
assert(registeredTimer->extra[4] == 0x3c && timerKernel.systemTimerTick() == 0);
registeredTimer->base.bytes[0x17] &= static_cast<remorse::u8>(~0x04);
bool registrationRejected = false;
try {
timerKernel.deliverSystemTimerEvent();
} catch (const std::logic_error&) {
registrationRejected = true;
}
assert(registrationRejected && timerKernel.systemTimerTick() == 0);
registeredTimer->base.bytes[0x17] |= 0x04;
bool duplicateTimerRejected = false;
try {
timerKernel.createSystemTimerProcess(1, timerGroups);
} catch (const std::logic_error&) {
duplicateTimerRejected = true;
}
assert(duplicateTimerRejected && timerGroups[1] == 4);
bool timerTeardownRejected = false;
try {
timerKernel.terminateProcess(3, timerGroups);
} catch (const std::logic_error&) {
timerTeardownRejected = true;
}
assert(timerTeardownRejected && timerGroups[1] == 4);
auto* timedStand = timerKernel.createStandProcess(1, 7, timerGroups);
remorse::EntityDispatchEntry::sleep(timedStand->base, timerKernel.systemTimerTick(), 60);
for (int tick = 0; tick < 60; ++tick) {
timerKernel.deliverSystemTimerEvent();
}
ran.clear();
timerKernel.scanRunnableProcesses(1, recordRun, &ran, timerKernel.systemTimerTick());
assert(ran.size() == 3 && timedStand->base.bytes[0x19] == 0x10);
timerKernel.deliverSystemTimerEvent();
ran.clear();
timerKernel.scanRunnableProcesses(1, recordRun, &ran, timerKernel.systemTimerTick());
assert((ran == std::vector<remorse::u16>{0, 1, 2, 4}));
assert(timedStand->base.bytes[0x19] == 0 && timerKernel.systemTimerTick() == 61);
auto* keyboard = timerKernel.createKeyboardProcess(1, timerGroups);
assert(timerKernel.processAt(5) == &keyboard->base && timerKernel.allocationSizeAt(5) == 0x36);
assert(std::strcmp(timerKernel.processNameAt(5), "KeyboardProcess") == 0);
assert(keyboard->base.bytes[0x16] == 3 && keyboard->base.bytes[0x17] == 1);
assert(keyboard->base.bytes[0x18] == 0x81 && keyboard->base.bytes[0x19] == 1);
assert(keyboard->extra[0] == 0 && keyboard->extra[3] == 0 && timerGroups[1] == 6);
bool duplicateKeyboardRejected = false;
try {
timerKernel.createKeyboardProcess(1, timerGroups);
} catch (const std::logic_error&) {
duplicateKeyboardRejected = true;
}
assert(duplicateKeyboardRejected && timerGroups[1] == 6);
bool keyboardTeardownRejected = false;
try {
timerKernel.terminateProcess(5, timerGroups);
} catch (const std::logic_error&) {
keyboardTeardownRejected = true;
}
assert(keyboardTeardownRejected && timerGroups[1] == 6);
auto* keyDaemon = timerKernel.createKeyDaemonProcess(1, timerGroups);
assert(timerKernel.processAt(6) == &keyDaemon->base && timerKernel.allocationSizeAt(6) == 0x40);
assert(std::strcmp(timerKernel.processNameAt(6), "KeyDaemon") == 0);
assert(keyDaemon->base.bytes[0x16] == 3 && keyDaemon->base.bytes[0x17] == 0x27);
assert(keyDaemon->extra[0] == 2 && keyDaemon->extra[4] == 0x2d && timerGroups[1] == 7);
bool duplicateDaemonRejected = false;
try {
timerKernel.createKeyDaemonProcess(1, timerGroups);
} catch (const std::logic_error&) {
duplicateDaemonRejected = true;
}
assert(duplicateDaemonRejected && timerGroups[1] == 7);
bool daemonTeardownRejected = false;
try {
timerKernel.terminateProcess(6, timerGroups);
} catch (const std::logic_error&) {
daemonTeardownRejected = true;
}
assert(daemonTeardownRejected && timerGroups[1] == 7);
remorse::KeyEvent keyboardEvent{0x41, 0x1e, 1, 2, 3, 1, 4, 5};
const remorse::KeyEvent lastEvent{0x42, 0x30, 6, 7, 8, 9, 10, 11};
keyboard->extra[0] = 0x31;
assert(!remorse::KeyboardProcess::handleKeyEvent(*keyboard, 0x11, 0, keyboardEvent, lastEvent));
assert(!remorse::KeyboardProcess::handleKeyEvent(*keyboard, 0x10, 1, keyboardEvent, lastEvent));
assert(keyboardEvent.character == 0x41 && keyboard->extra[0] == 0x31);
keyboardEvent.field8 = 0;
assert(!remorse::KeyboardProcess::handleKeyEvent(*keyboard, 0x10, 0, keyboardEvent, lastEvent));
keyboardEvent.field8 = 1;
keyboardEvent.character = 0;
assert(!remorse::KeyboardProcess::handleKeyEvent(*keyboard, 0x10, 0, keyboardEvent, lastEvent));
keyboardEvent.character = 0x41;
assert(remorse::KeyboardProcess::handleKeyEvent(*keyboard, 0x10, 0, keyboardEvent, lastEvent));
assert(std::memcmp(&keyboardEvent, &lastEvent, sizeof(keyboardEvent)) == 0);
assert(keyboard->extra[0] == 0x21);
return 0;
}

View file

@ -1,10 +1,98 @@
#include "remorse/file_system.h"
#include "remorse/runtime.h"
#include <cassert>
#include <cstdio>
#include <cstring>
#include <exception>
#include <filesystem>
#include <stdexcept>
#include <string>
int main()
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifdef _DEBUG
#include <crtdbg.h>
#endif
#endif
int main(int argc, char** argv)
{
#ifdef _WIN32
if (argc == 2 && std::strcmp(argv[1], "--crash-probe") == 0) {
remorse::installCrashDiagnostics();
try {
throw std::runtime_error("diagnostic probe");
} catch (...) {
std::terminate();
}
}
#ifdef _DEBUG
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_WNDW);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW);
#endif
remorse::installCrashDiagnostics();
assert((GetErrorMode() & SEM_NOGPFAULTERRORBOX) != 0);
#ifdef _DEBUG
assert((_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE) & _CRTDBG_MODE_WNDW) == 0);
assert((_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE) & _CRTDBG_MODE_DEBUG) != 0);
assert((_CrtSetReportMode(_CRT_ERROR, _CRTDBG_REPORT_MODE) & _CRTDBG_MODE_WNDW) == 0);
#endif
wchar_t executable[MAX_PATH]{};
if (GetModuleFileNameW(nullptr, executable, MAX_PATH) == 0) {
return 1;
}
std::wstring commandLine = L"\"" + std::wstring(executable) + L"\" --crash-probe";
SECURITY_ATTRIBUTES pipeSecurity{sizeof(SECURITY_ATTRIBUTES), nullptr, TRUE};
HANDLE readPipe = nullptr;
HANDLE writePipe = nullptr;
if (!CreatePipe(&readPipe, &writePipe, &pipeSecurity, 0)) {
return 1;
}
if (!SetHandleInformation(readPipe, HANDLE_FLAG_INHERIT, 0)) {
CloseHandle(readPipe);
CloseHandle(writePipe);
return 1;
}
STARTUPINFOW startup{};
startup.cb = sizeof(startup);
startup.dwFlags = STARTF_USESTDHANDLES;
startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
startup.hStdOutput = writePipe;
startup.hStdError = writePipe;
PROCESS_INFORMATION child{};
if (!CreateProcessW(nullptr, commandLine.data(), nullptr, nullptr, TRUE, CREATE_NO_WINDOW,
nullptr, nullptr, &startup, &child)) {
CloseHandle(readPipe);
CloseHandle(writePipe);
return 1;
}
CloseHandle(writePipe);
const DWORD waitResult = WaitForSingleObject(child.hProcess, 5000);
if (waitResult == WAIT_TIMEOUT) {
TerminateProcess(child.hProcess, 2);
WaitForSingleObject(child.hProcess, INFINITE);
}
char buffer[512];
DWORD count = 0;
std::string crashOutput;
while (ReadFile(readPipe, buffer, sizeof(buffer), &count, nullptr) && count != 0) {
crashOutput.append(buffer, count);
}
DWORD exitCode = 0;
const BOOL gotExitCode = GetExitCodeProcess(child.hProcess, &exitCode);
CloseHandle(readPipe);
CloseHandle(child.hThread);
CloseHandle(child.hProcess);
if (!gotExitCode || waitResult != WAIT_OBJECT_0 || exitCode != 1 ||
crashOutput.find("Unhandled C++ exception: diagnostic probe") == std::string::npos) {
std::fprintf(stderr, "Crash probe wait=%lu exit=0x%08lX output=%s\n",
waitResult, exitCode, crashOutput.c_str());
return 1;
}
#endif
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
assert(root.has_value());
assert(root->hasExecutable);

View file

@ -0,0 +1,54 @@
#include "remorse/isometric_controls.h"
#include "remorse/map_renderer.h"
#include <cassert>
int main()
{
assert(remorse::controllerWalkRequested(0));
assert(!remorse::controllerWalkRequested(0x01));
assert(!remorse::controllerWalkRequested(0x02));
assert(!remorse::controllerWalkRequested(0x40));
assert(remorse::controllerWalkRequested(0x41));
assert(remorse::controllerWalkRequested(0x42));
constexpr remorse::s32 originX = 1024;
constexpr remorse::s32 originY = 2048;
const remorse::ScreenPoint origin = remorse::projectToScreen({originX, originY, 0});
const auto east = remorse::worldPositionAtScreenOffset(originX, originY, 1.0, 0.0);
const remorse::ScreenPoint eastProjection = remorse::projectToScreen({
static_cast<remorse::s32>(east.x), static_cast<remorse::s32>(east.y), 0});
assert(eastProjection.x - origin.x == 1);
assert(eastProjection.y - origin.y == 0);
const auto north = remorse::worldPositionAtScreenOffset(originX, originY, 0.0, -1.0);
const remorse::ScreenPoint northProjection = remorse::projectToScreen({
static_cast<remorse::s32>(north.x), static_cast<remorse::s32>(north.y), 0});
assert(northProjection.x - origin.x == 0);
assert(northProjection.y - origin.y == -1);
const auto west = remorse::worldPositionAtScreenOffset(originX, originY, -1.0, 0.0);
const remorse::ScreenPoint westProjection = remorse::projectToScreen({
static_cast<remorse::s32>(west.x), static_cast<remorse::s32>(west.y), 0});
assert(westProjection.x - origin.x == -1);
assert(westProjection.y - origin.y == 0);
const auto south = remorse::worldPositionAtScreenOffset(originX, originY, 0.0, 1.0);
const remorse::ScreenPoint southProjection = remorse::projectToScreen({
static_cast<remorse::s32>(south.x), static_cast<remorse::s32>(south.y), 0});
assert(southProjection.x - origin.x == 0);
assert(southProjection.y - origin.y == 1);
remorse::AnimationFrameMetadata forwardFrame{};
forwardFrame.bytes[4] = 6;
const auto forward = remorse::hostMovementStep(forwardFrame, 6, false);
assert(forward && forward->x > 0 && forward->y > 0);
remorse::AnimationFrameMetadata reverseFrame{};
reverseFrame.bytes[4] = 0xfe;
reverseFrame.bytes[5] = 3;
const auto authoredReverse = remorse::decodeAnimationDisplacement(reverseFrame, 6, false);
const auto hostReverse = remorse::hostMovementStep(reverseFrame, 6, false);
assert(authoredReverse && authoredReverse->x < 0 && authoredReverse->y < 0);
assert(hostReverse && hostReverse->x == 0 && hostReverse->y == 0);
assert(hostReverse->z == authoredReverse->z && hostReverse->onGround == authoredReverse->onGround);
return 0;
}

View file

@ -1,6 +1,8 @@
#include "remorse/file_system.h"
#include "remorse/fixed_map.h"
#include "remorse/map_renderer.h"
#include <algorithm>
#include <cassert>
#include <filesystem>
@ -21,5 +23,33 @@ int main()
assert(!image->pixels.empty());
assert(image->decodedItemCount > 0);
assert(image->paintedItemCount > 0);
const auto fixedPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "FIXED.DAT");
assert(fixedPath.has_value());
const auto archive = remorse::loadFixedMapArchive(*fixedPath);
assert(archive.has_value());
const auto fixedMap = remorse::loadFixedMap(*archive, options.mapIndex);
assert(fixedMap.has_value());
remorse::s32 minX = fixedMap->entries.front().x;
remorse::s32 minY = fixedMap->entries.front().y;
remorse::s32 maxX = minX;
remorse::s32 maxY = minY;
for (const remorse::FixedMapEntry& entry : fixedMap->entries) {
minX = std::min(minX, static_cast<remorse::s32>(entry.x));
minY = std::min(minY, static_cast<remorse::s32>(entry.y));
maxX = std::max(maxX, static_cast<remorse::s32>(entry.x));
maxY = std::max(maxY, static_cast<remorse::s32>(entry.y));
}
options.maxWidth = 1280;
options.maxHeight = 760;
options.viewCenter = remorse::WorldPoint{(minX + maxX) / 2, (minY + maxY) / 2, 0};
const auto centeredImage = remorse::renderMapPreview(*root, options);
assert(centeredImage.has_value());
assert(centeredImage->paintedItemCount > 0);
assert(centeredImage->width == options.maxWidth);
assert(centeredImage->height == options.maxHeight);
const remorse::ScreenPoint center = remorse::projectToScreen(*options.viewCenter);
assert(centeredImage->sourceLeft == center.x - static_cast<remorse::s32>(options.maxWidth / 2));
assert(centeredImage->sourceTop == center.y - static_cast<remorse::s32>(options.maxHeight / 2));
return 0;
}

View file

@ -0,0 +1,70 @@
#include "remorse/detail/map_sorter.h"
#include <cassert>
#include <vector>
int main()
{
remorse::detail::RenderSortItem elevated;
elevated.left = 0;
elevated.top = 0;
elevated.right = 64;
elevated.bottom = 64;
elevated.x = 0;
elevated.xLeft = -32;
elevated.y = 0;
elevated.yFar = -32;
elevated.z = 32;
elevated.zTop = 48;
elevated.sxLeft = -100;
elevated.sxRight = 100;
elevated.sxTop = -100;
elevated.syTop = -100;
elevated.sxBottom = 100;
elevated.syBottom = 100;
elevated.shape = 2;
remorse::detail::RenderSortItem ground;
ground.left = 0;
ground.top = 0;
ground.right = 64;
ground.bottom = 64;
ground.x = 100;
ground.xLeft = 68;
ground.y = 100;
ground.yFar = 68;
ground.z = 0;
ground.zTop = 16;
ground.sxLeft = -100;
ground.sxRight = 100;
ground.sxTop = -100;
ground.syTop = -100;
ground.sxBottom = 100;
ground.syBottom = 100;
ground.shape = 1;
const std::vector<remorse::detail::RenderSortItem> items{elevated, ground};
const std::vector<std::size_t> order = remorse::detail::resolveRenderOrder(items);
assert(order.size() == 2);
assert(order[0] == 1);
assert(order[1] == 0);
remorse::detail::RenderSortItem animatedFlat = elevated;
animatedFlat.x = 64;
animatedFlat.xLeft = 64;
animatedFlat.y = 64;
animatedFlat.yFar = 64;
animatedFlat.z = 0;
animatedFlat.zTop = 0;
animatedFlat.flat = true;
animatedFlat.animated = true;
remorse::detail::RenderSortItem staticFlat = animatedFlat;
staticFlat.animated = false;
const std::vector<remorse::detail::RenderSortItem> flatItems{animatedFlat, staticFlat};
const std::vector<std::size_t> flatOrder = remorse::detail::resolveRenderOrder(flatItems);
assert(flatOrder.size() == 2);
assert(flatOrder[0] == 1);
assert(flatOrder[1] == 0);
return 0;
}