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

@ -1,5 +1,9 @@
#include "remorse/entity_dispatch_entry.h"
#include <algorithm>
#include <cstring>
#include <stdexcept>
namespace remorse {
namespace {
@ -12,23 +16,437 @@ void writeLe16(EntityDispatchEntryBase& entry, u16 offset, u16 value)
entry.bytes[offset] = static_cast<u8>(value & 0xff);
entry.bytes[offset + 1] = static_cast<u8>(value >> 8);
}
u16 readLe16(const EntityDispatchEntryBase& entry, u16 offset)
{
return static_cast<u16>(entry.bytes[offset] | (static_cast<u16>(entry.bytes[offset + 1]) << 8));
}
EntityDispatchEntryBase* EntityDispatchEntry::initBase(EntityDispatchEntryBase* entry)
void clearBytes(EntityDispatchEntryBase& entry, u16 begin, u16 end)
{
for (u16 offset = begin; offset < end; ++offset) {
entry.bytes[offset] = 0;
}
}
}
EntityDispatchEntryBase* ProcessKernel::createProcess(u8 activeGroup, u16 (&groupCounts)[32])
{
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
throw std::overflow_error("process table exhausted");
}
if (std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
processes_.reserve(processes_.size() + 30);
allocationSizes_.reserve(allocationSizes_.size() + 30);
processNames_.reserve(processNames_.size() + 30);
}
ownedProcesses_.reserve(ownedProcesses_.size() + 1);
auto entry = std::make_unique<EntityDispatchEntryBase>();
EntityDispatchEntry::initBase(entry.get(), activeGroup, groupCounts);
setLastAllocationSize(sizeof(EntityDispatchEntryBase));
const u16 id = addProcess(*entry);
if (syncEnabled_) {
EntityDispatchEntryBase* current = currentProcessId_ == 0 ? nullptr : processAt(currentProcessId_);
if (current != nullptr && (current->bytes[0x16] & 0x08) == 0 && (current->bytes[0x18] & 0x02) != 0) {
current->bytes[0x18] |= 0x04;
}
if (id > currentProcessId_) {
entry->bytes[0x16] |= 0x40;
}
}
EntityDispatchEntryBase* result = entry.get();
ownedProcesses_.push_back(std::move(entry));
return result;
}
StandProcessEntry* ProcessKernel::createStandProcess(u8 activeGroup, u16 npcNumber, u16 (&groupCounts)[32])
{
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
throw std::overflow_error("process table exhausted");
}
if (std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
processes_.reserve(processes_.size() + 30);
allocationSizes_.reserve(allocationSizes_.size() + 30);
processNames_.reserve(processNames_.size() + 30);
}
ownedStandProcesses_.reserve(ownedStandProcesses_.size() + 1);
auto process = std::make_unique<StandProcessEntry>();
EntityDispatchEntry::initBase(&process->base, activeGroup, groupCounts);
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x300));
writeLe16(process->base, 0x04, npcNumber);
writeLe16(process->base, 0x06, 0x258);
writeLe16(process->base, 0x00, StandProcess::vtableOffset);
process->extra[0x00] |= 0x01;
process->extra[0x01] |= 0x01;
process->extra[0x0e] = static_cast<u8>(npcNumber);
process->extra[0x0f] = static_cast<u8>(npcNumber >> 8);
setLastAllocationSize(sizeof(StandProcessEntry));
const u16 id = addProcess(process->base);
processNames_[id] = "Stand";
if (syncEnabled_) {
EntityDispatchEntryBase* current = currentProcessId_ == 0 ? nullptr : processAt(currentProcessId_);
if (current != nullptr && (current->bytes[0x16] & 0x08) == 0 && (current->bytes[0x18] & 0x02) != 0) {
current->bytes[0x18] |= 0x04;
}
if (id > currentProcessId_) {
process->base.bytes[0x16] |= 0x40;
}
}
StandProcessEntry* result = process.get();
ownedStandProcesses_.push_back(std::move(process));
const std::size_t itemByte = npcNumber >> 3;
if (itemProcessPresence_.size() <= itemByte) {
itemProcessPresence_.resize(itemByte + 1, 0);
}
itemProcessPresence_[itemByte] |= static_cast<u8>(1u << (npcNumber & 7));
return result;
}
SystemTimerProcessEntry* ProcessKernel::createSystemTimerProcess(u8 activeGroup, u16 (&groupCounts)[32])
{
if (systemTimerProcess_ != nullptr) {
throw std::logic_error("system timer process already exists");
}
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
throw std::overflow_error("process table exhausted");
}
auto process = std::make_unique<SystemTimerProcessEntry>();
EntityDispatchEntry::initBase(&process->base, activeGroup, groupCounts);
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x300));
process->extra[0] = 0x02;
process->extra[4] = 0x3c;
setLastAllocationSize(sizeof(SystemTimerProcessEntry));
const u16 id = addProcess(process->base);
processNames_[id] = "SystemTimer";
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x2403));
systemTimerProcessId_ = id;
systemTimerProcess_ = std::move(process);
return systemTimerProcess_.get();
}
KeyboardProcessEntry* ProcessKernel::createKeyboardProcess(u8 activeGroup, u16 (&groupCounts)[32])
{
if (keyboardProcess_ != nullptr) {
throw std::logic_error("keyboard process already exists");
}
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
throw std::overflow_error("process table exhausted");
}
auto process = std::make_unique<KeyboardProcessEntry>();
EntityDispatchEntry::initBase(&process->base, activeGroup, groupCounts);
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x103));
setLastAllocationSize(sizeof(KeyboardProcessEntry));
const u16 id = addProcess(process->base);
processNames_[id] = "KeyboardProcess";
writeLe16(process->base, 0x18, static_cast<u16>(readLe16(process->base, 0x18) | 0x180));
keyboardProcess_ = std::move(process);
return keyboardProcess_.get();
}
KeyDaemonProcessEntry* ProcessKernel::createKeyDaemonProcess(u8 activeGroup, u16 (&groupCounts)[32])
{
if (keyDaemonProcess_ != nullptr) {
throw std::logic_error("key daemon process already exists");
}
if (processes_.size() == 1020 && std::find(processes_.begin(), processes_.end(), nullptr) == processes_.end()) {
throw std::overflow_error("process table exhausted");
}
auto process = std::make_unique<KeyDaemonProcessEntry>();
EntityDispatchEntry::initBase(&process->base, activeGroup, groupCounts);
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x300));
process->extra[0] = 0x02;
process->extra[4] = 0x2d;
setLastAllocationSize(sizeof(KeyDaemonProcessEntry));
const u16 id = addProcess(process->base);
processNames_[id] = "KeyDaemon";
writeLe16(process->base, 0x16, static_cast<u16>(readLe16(process->base, 0x16) | 0x2403));
keyDaemonProcess_ = std::move(process);
return keyDaemonProcess_.get();
}
void ProcessKernel::deliverSystemTimerEvent()
{
if (systemTimerProcess_ == nullptr) {
throw std::logic_error("system timer process is not registered");
}
if (processAt(systemTimerProcessId_) != &systemTimerProcess_->base ||
(readLe16(systemTimerProcess_->base, 0x16) & 0x400) == 0) {
throw std::logic_error("system timer event registration is invalid");
}
SystemTimerProcess::increment(*systemTimerProcess_);
}
u32 ProcessKernel::systemTimerTick() const
{
if (systemTimerProcess_ == nullptr) {
throw std::logic_error("system timer process is not registered");
}
return SystemTimerProcess::tick(*systemTimerProcess_);
}
void ProcessKernel::setSyncContext(bool enabled, u16 currentProcessId)
{
syncEnabled_ = enabled;
currentProcessId_ = currentProcessId;
}
void ProcessKernel::setMarkNewProcesses(bool enabled)
{
markNewProcesses_ = enabled;
}
void ProcessKernel::setLastAllocationSize(u16 size)
{
lastAllocationSize_ = size;
}
u16 ProcessKernel::addProcess(EntityDispatchEntryBase& entry)
{
auto freeSlot = std::find(processes_.begin(), processes_.end(), nullptr);
if (freeSlot == processes_.end()) {
if (processes_.size() + 30 > 1023) {
throw std::overflow_error("process table exhausted");
}
const std::size_t oldCapacity = processes_.size();
processes_.resize(oldCapacity + 30, nullptr);
allocationSizes_.resize(oldCapacity + 30, 0);
processNames_.resize(oldCapacity + 30, nullptr);
freeSlot = processes_.begin() + oldCapacity;
}
const u16 id = static_cast<u16>(freeSlot - processes_.begin());
*freeSlot = &entry;
entry.bytes[0x02] = static_cast<u8>(id & 0xff);
entry.bytes[0x03] = static_cast<u8>(id >> 8);
allocationSizes_[id] = lastAllocationSize_;
lastAllocationSize_ = 0;
++processCount_;
if (markNewProcesses_) {
++newlyCreatedCount_;
entry.bytes[0x19] |= 0x02;
}
processNames_[id] = "Default";
return id;
}
void ProcessKernel::clearProcessSlot(u16 id)
{
if (id >= processes_.size()) {
throw std::out_of_range("process ID outside table");
}
processes_[id] = nullptr;
allocationSizes_[id] = 0;
processNames_[id] = nullptr;
}
void ProcessKernel::releaseOwnedProcess(u16 id)
{
EntityDispatchEntryBase* entry = processes_[id];
clearProcessSlot(id);
const auto stand = std::find_if(ownedStandProcesses_.begin(), ownedStandProcesses_.end(),
[entry](const auto& process) { return &process->base == entry; });
if (stand != ownedStandProcesses_.end()) {
ownedStandProcesses_.erase(stand);
return;
}
const auto owned = std::find_if(ownedProcesses_.begin(), ownedProcesses_.end(),
[entry](const auto& process) { return process.get() == entry; });
ownedProcesses_.erase(owned);
}
void ProcessKernel::terminateProcess(u16 id, u16 (&groupCounts)[32])
{
EntityDispatchEntryBase* entry = processAt(id);
if (id == 0 || entry == nullptr) {
throw std::out_of_range("process ID is not addressable");
}
const u16 itemNumber = readLe16(*entry, 0x04);
if (systemTimerProcess_ != nullptr && entry == &systemTimerProcess_->base) {
throw std::logic_error("system timer teardown is not ported");
}
if (keyboardProcess_ != nullptr && entry == &keyboardProcess_->base) {
throw std::logic_error("keyboard process teardown is not ported");
}
if (keyDaemonProcess_ != nullptr && entry == &keyDaemonProcess_->base) {
throw std::logic_error("key daemon process teardown is not ported");
}
const auto stand = std::find_if(ownedStandProcesses_.begin(), ownedStandProcesses_.end(),
[entry](const auto& process) { return &process->base == entry; });
if (itemNumber != 0 && (stand == ownedStandProcesses_.end() || id < 3 ||
readLe16(*entry, 0x24) != 0 || readLe16(*entry, 0x26) != 0 ||
readLe16(*entry, 0x2e) != 0 || readLe16(*entry, 0x30) != 0)) {
throw std::logic_error("item process child cleanup is not ported");
}
if (stand == ownedStandProcesses_.end() && std::none_of(ownedProcesses_.begin(), ownedProcesses_.end(),
[entry](const auto& process) { return process.get() == entry; })) {
throw std::logic_error("external process ownership is not ported");
}
const bool immediate = (entry->bytes[0x18] & 0x04) != 0;
if (!immediate && (entry->bytes[0x16] & 0x08) != 0) {
throw std::logic_error("process is already terminated");
}
--groupCounts[entry->bytes[0x08] & 0x1f];
if (immediate) {
releaseOwnedProcess(id);
} else {
entry->bytes[0x16] |= 0x08;
++terminatedProcessCount_;
}
--processCount_;
if (itemNumber != 0) {
const bool stillPresent = std::any_of(processes_.begin() + 3, processes_.end(),
[itemNumber](const EntityDispatchEntryBase* process) {
return process != nullptr && readLe16(*process, 0x04) == itemNumber &&
(readLe16(*process, 0x16) & 0x08) == 0;
});
if (!stillPresent) {
itemProcessPresence_[itemNumber >> 3] &= static_cast<u8>(~(1u << (itemNumber & 7)));
}
}
}
void ProcessKernel::clearTerminatedProcesses()
{
for (std::size_t id = 3; id < processes_.size() && terminatedProcessCount_ != 0; ++id) {
EntityDispatchEntryBase* entry = processes_[id];
if (entry == nullptr || (entry->bytes[0x16] & 0x08) == 0) {
continue;
}
if (entry->bytes[0x1a] != 0 || entry->bytes[0x1b] != 0) {
throw std::logic_error("terminated process has linked state");
}
releaseOwnedProcess(static_cast<u16>(id));
--terminatedProcessCount_;
}
}
void ProcessKernel::scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDispatchEntryBase&, void*), void* context, u32 timerTick)
{
if (run == nullptr) {
throw std::invalid_argument("process run slot is required");
}
const bool priorSyncEnabled = syncEnabled_;
const u16 priorProcessId = currentProcessId_;
syncEnabled_ = true;
try {
for (std::size_t id = 0; id < processes_.size(); ++id) {
EntityDispatchEntryBase* entry = processes_[id];
if (entry != nullptr && (readLe16(*entry, 0x16) & 0x08) == 0) {
if ((readLe16(*entry, 0x18) & 0x1000) != 0) {
const u32 sleepStart = readLe16(*entry, 0x0a) | (static_cast<u32>(readLe16(*entry, 0x0c)) << 16);
const u32 sleepLength = readLe16(*entry, 0x0e) | (static_cast<u32>(readLe16(*entry, 0x10)) << 16);
if (sleepStart + sleepLength < timerTick) {
if ((readLe16(*entry, 0x16) & 0x100) != 0) {
const auto stand = std::find_if(ownedStandProcesses_.begin(), ownedStandProcesses_.end(),
[entry](const auto& process) { return &process->base == entry; });
if (stand == ownedStandProcesses_.end() ||
readLe16(*entry, 0x00) != StandProcess::vtableOffset) {
throw std::logic_error("sleep completion dispatch is not ported");
}
}
EntityDispatchEntry::unlinkSleep(*entry);
}
}
if ((readLe16(*entry, 0x16) & 0x40) != 0) {
writeLe16(*entry, 0x16, static_cast<u16>(readLe16(*entry, 0x16) & ~0x40));
} else {
const u8 groupId = entry->bytes[0x08] & 0x1f;
const u16 flags = readLe16(*entry, 0x16);
if ((readLe16(*entry, 0x18) & 0x1000) == 0 && readLe16(*entry, 0x24) == 0 &&
readLe16(*entry, 0x26) == 0 && (flags & 1) == 0 &&
((flags & 2) != 0 || groupId == activeGroup || groupId == 30 || groupId == 31)) {
currentProcessId_ = static_cast<u16>(id);
run(*entry, context);
currentProcessId_ = 0;
}
}
}
clearTerminatedProcesses();
}
} catch (...) {
syncEnabled_ = priorSyncEnabled;
currentProcessId_ = priorProcessId;
throw;
}
syncEnabled_ = priorSyncEnabled;
currentProcessId_ = priorProcessId;
}
EntityDispatchEntryBase* ProcessKernel::processAt(u16 id) const
{
return id < processes_.size() ? processes_[id] : nullptr;
}
u16 ProcessKernel::allocationSizeAt(u16 id) const
{
return id < allocationSizes_.size() ? allocationSizes_[id] : 0;
}
const char* ProcessKernel::processNameAt(u16 id) const
{
return id < processNames_.size() ? processNames_[id] : nullptr;
}
bool ProcessKernel::itemHasProcess(u16 itemNumber) const
{
const std::size_t itemByte = itemNumber >> 3;
return itemByte < itemProcessPresence_.size() &&
(itemProcessPresence_[itemByte] & (1u << (itemNumber & 7))) != 0;
}
u16 ProcessKernel::processCount() const
{
return processCount_;
}
u16 ProcessKernel::terminatedProcessCount() const
{
return terminatedProcessCount_;
}
u16 ProcessKernel::newlyCreatedCount() const
{
return newlyCreatedCount_;
}
std::size_t ProcessKernel::capacity() const
{
return processes_.size();
}
EntityDispatchEntryBase* EntityDispatchEntry::initBase(EntityDispatchEntryBase* entry, u8 activeGroup, u16 (&groupCounts)[32])
{
if (entry == nullptr) {
return nullptr;
}
for (u8& byte : entry->bytes) {
byte = 0;
}
writeLe16(*entry, 0x00, 0x3b06);
writeLe16(*entry, 0x1e, 0x2d10);
writeLe16(*entry, 0x28, 0x3afe);
clearBytes(*entry, 0x02, 0x06);
writeLe16(*entry, 0x06, 1);
entry->bytes[0x08] = 0;
clearBytes(*entry, 0x0a, 0x1e);
writeLe16(*entry, 0x18, 1);
clearBytes(*entry, 0x20, 0x28);
clearBytes(*entry, 0x2a, 0x32);
setGroupId(*entry, activeGroup, groupCounts);
return entry;
}
void EntityDispatchEntry::setGroupId(EntityDispatchEntryBase& entry, u8 groupId)
void EntityDispatchEntry::setGroupId(EntityDispatchEntryBase& entry, u8 groupId, u16 (&groupCounts)[32])
{
entry.bytes[groupIdOffset] = static_cast<u8>((entry.bytes[groupIdOffset] & 0xe0) | (groupId & 0x1f));
if (groupId == 0 || groupId > 31) {
throw std::invalid_argument("process group ID must be in 1..31");
}
const u8 oldGroupId = entry.bytes[groupIdOffset] & 0x1f;
if (oldGroupId == groupId) {
return;
}
if (oldGroupId != 0) {
--groupCounts[oldGroupId];
}
entry.bytes[groupIdOffset] = groupId;
++groupCounts[groupId];
}
void EntityDispatchEntry::incrementGroupId(EntityDispatchEntryBase& entry)
@ -46,4 +464,77 @@ void EntityDispatchEntry::setEventTypeChecked(EntityDispatchEntryBase& entry, u1
writeLe16(entry, eventTypeOffset, eventType);
}
void EntityDispatchEntry::sleep(EntityDispatchEntryBase& entry, u32 timerTick, u32 duration)
{
writeLe16(entry, 0x18, static_cast<u16>(readLe16(entry, 0x18) | 0x1000));
writeLe16(entry, 0x0a, static_cast<u16>(timerTick));
writeLe16(entry, 0x0c, static_cast<u16>(timerTick >> 16));
writeLe16(entry, 0x0e, static_cast<u16>(duration));
writeLe16(entry, 0x10, static_cast<u16>(duration >> 16));
}
void EntityDispatchEntry::unlinkSleep(EntityDispatchEntryBase& entry)
{
writeLe16(entry, 0x18, static_cast<u16>(readLe16(entry, 0x18) & ~0x1000));
clearBytes(entry, 0x0a, 0x12);
}
void StandProcess::run(EntityDispatchEntryBase& entry,
bool (*startBehavior)(const EntityDispatchEntryBase&, void*), void* context, u32 timerTick)
{
if (startBehavior == nullptr) {
throw std::invalid_argument("NPC behavior boundary is required");
}
if (!startBehavior(entry, context)) {
EntityDispatchEntry::sleep(entry, timerTick, 0x3c);
}
}
void StandProcess::run(StandProcessEntry& entry, const NpcBehaviorServices& services, u32 timerTick)
{
if (services.isEntirelyOnScreen == nullptr || services.getActivity == nullptr ||
services.isCurrentActivitySpecifiedBy == nullptr || services.canSeeControlledNPC == nullptr ||
services.startNextBehavior == nullptr) {
throw std::invalid_argument("NPC behavior services are required");
}
const u16 npcNumber = static_cast<u16>(entry.extra[0x0e] | (static_cast<u16>(entry.extra[0x0f]) << 8));
bool started = false;
if (services.isEntirelyOnScreen(npcNumber, services.context) &&
services.getActivity(npcNumber, services.context) != 0 &&
!services.isCurrentActivitySpecifiedBy(npcNumber, 0x71, services.context) &&
services.canSeeControlledNPC(npcNumber, services.context)) {
services.startNextBehavior(npcNumber, 0x71, services.context);
started = true;
}
run(entry.base, [](const EntityDispatchEntryBase&, void* context) {
return *static_cast<bool*>(context);
}, &started, timerTick);
}
u32 SystemTimerProcess::tick(const SystemTimerProcessEntry& entry)
{
return static_cast<u32>(entry.extra[0x0e] | (static_cast<u16>(entry.extra[0x0f]) << 8)) |
(static_cast<u32>(entry.extra[0x10] | (static_cast<u16>(entry.extra[0x11]) << 8)) << 16);
}
void SystemTimerProcess::increment(SystemTimerProcessEntry& entry)
{
const u32 nextTick = tick(entry) + 1;
entry.extra[0x0e] = static_cast<u8>(nextTick);
entry.extra[0x0f] = static_cast<u8>(nextTick >> 8);
entry.extra[0x10] = static_cast<u8>(nextTick >> 16);
entry.extra[0x11] = static_cast<u8>(nextTick >> 24);
}
bool KeyboardProcess::handleKeyEvent(KeyboardProcessEntry& process, u16 selector, u16 filter,
KeyEvent& event, const KeyEvent& lastEvent)
{
if (selector != 0x10 || filter != 0 || event.character == 0 || event.field8 == 0) {
return false;
}
std::memcpy(&event, &lastEvent, sizeof(event));
process.extra[0] &= static_cast<u8>(~0x10);
return true;
}
} // namespace remorse