#include "remorse/entity_dispatch_entry.h" #include #include #include #include int main() { remorse::EntityDispatchEntryBase 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); assert(entry.bytes[0x05] == 0x12); remorse::EntityDispatchEntry::setEventTypeChecked(entry, 0x00f2); assert(entry.bytes[0x06] == 0xf2); assert(entry.bytes[0x07] == 0x00); remorse::EntityDispatchEntry::setEventTypeChecked(entry, 0x0fff); assert(entry.bytes[0x06] == 0xff && entry.bytes[0x07] == 0x0f); bool invalidEventType = false; try { remorse::EntityDispatchEntry::setEventTypeChecked(entry, 0x1000); } catch (const std::invalid_argument&) { invalidEventType = true; } assert(invalidEventType && entry.bytes[0x06] == 0 && entry.bytes[0x07] == 0x10); 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::ProcessKernel incrementKernel; incrementKernel.setActiveGroup(4); remorse::EntityDispatchEntry::incrementGroupId(entry, incrementKernel, groupCounts); assert(entry.bytes[0x08] == 5 && groupCounts[4] == 0 && groupCounts[5] == 1); assert(incrementKernel.activeGroup() == 5 && incrementKernel.groupSelectionPending()); bool wrongActiveGroup = false; try { remorse::EntityDispatchEntry::incrementGroupId(entry, incrementKernel, groupCounts); } catch (const std::invalid_argument&) { wrongActiveGroup = true; } assert(!wrongActiveGroup && entry.bytes[0x08] == 6 && incrementKernel.activeGroup() == 6); incrementKernel.setActiveGroup(7); try { remorse::EntityDispatchEntry::incrementGroupId(entry, incrementKernel, groupCounts); } catch (const std::invalid_argument&) { wrongActiveGroup = true; } assert(wrongActiveGroup && entry.bytes[0x08] == 6 && groupCounts[6] == 1); remorse::EntityDispatchEntry::setGroupId(entry, 29, groupCounts); incrementKernel.setActiveGroup(29); bool groupLimitReached = false; try { remorse::EntityDispatchEntry::incrementGroupId(entry, incrementKernel, groupCounts); } catch (const std::invalid_argument&) { groupLimitReached = true; } assert(groupLimitReached && entry.bytes[0x08] == 29 && groupCounts[29] == 1); 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 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 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 groupKernel; remorse::u16 groupTable[32]{}; for (int index = 0; index < 3; ++index) { groupKernel.createProcess(1, groupTable); } groupKernel.createProcess(2, groupTable); groupKernel.createProcess(3, groupTable); groupKernel.createProcess(30, groupTable); groupKernel.createProcess(31, groupTable); groupKernel.refreshActiveGroup(groupTable); assert(groupKernel.activeGroup() == 1 && !groupKernel.groupSelectionPending()); groupKernel.setActiveGroup(4); assert(groupKernel.activeGroup() == 4 && groupKernel.groupSelectionPending()); groupKernel.refreshActiveGroup(groupTable); assert(groupKernel.activeGroup() == 3 && !groupKernel.groupSelectionPending()); groupKernel.terminateProcess(4, groupTable); groupKernel.clearTerminatedProcesses(); groupKernel.refreshActiveGroup(groupTable); assert(groupKernel.activeGroup() == 2 && !groupKernel.groupSelectionPending()); for (remorse::u8 invalidGroup : {remorse::u8{0}, remorse::u8{32}}) { bool rejected = false; try { groupKernel.setActiveGroup(invalidGroup); } catch (const std::invalid_argument&) { rejected = true; } assert(rejected && groupKernel.activeGroup() == 2 && !groupKernel.groupSelectionPending()); } groupKernel.setActiveGroup(30); groupKernel.refreshActiveGroup(groupTable); assert(groupKernel.activeGroup() == 30 && groupKernel.groupSelectionPending()); remorse::ProcessKernel scheduler; remorse::u16 scheduledGroups[32]{}; assert(scheduler.activeGroup() == 1 && !scheduler.groupSelectionPending()); scheduler.setActiveGroup(1); assert(!scheduler.groupSelectionPending()); 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 ran; auto recordRun = [](remorse::EntityDispatchEntryBase& process, void* context) { auto& ids = *static_cast*>(context); ids.push_back(static_cast(process.bytes[0x02] | process.bytes[0x03] << 8)); }; scheduler.scanRunnableProcesses(2, recordRun, &ran); assert((ran == std::vector{1, 3})); assert(scheduler.activeGroup() == 2 && scheduler.groupSelectionPending()); assert(syncingProcess->bytes[0x16] == 0 && otherGroup->bytes[0x16] == 0); ran.clear(); matching->bytes[0x16] = 1; scheduler.scanRunnableProcesses(2, recordRun, &ran); assert((ran == std::vector{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(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(context); state.kernel->terminateProcess(static_cast(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{0})); assert(sleeper->bytes[0x19] == 0); for (remorse::u16 offset = 0x0a; offset < 0x12; ++offset) { assert(sleeper->bytes[offset] == 0); } remorse::ProcessKernel timedPassKernel; remorse::u16 timedPassGroups[32]{}; auto* timedPassSleeper = timedPassKernel.createProcess(1, timedPassGroups); timedPassKernel.createSystemTimerProcess(1, timedPassGroups); timedPassKernel.deliverSystemTimerEvent(); remorse::EntityDispatchEntry::sleep(*timedPassSleeper, 1, 1); std::vector timedPassRuns; const auto advanceTimer = [](void* context) { static_cast(context)->deliverSystemTimerEvent(); }; assert(timedPassKernel.runTimedSchedulerPass(1, recordRun, &timedPassRuns, advanceTimer, &timedPassKernel) == 1); assert(timedPassRuns.empty() && timedPassKernel.systemTimerTick() == 2); assert(timedPassKernel.runTimedSchedulerPass(1, recordRun, &timedPassRuns, advanceTimer, &timedPassKernel) == 1); assert(timedPassRuns.empty() && timedPassKernel.systemTimerTick() == 3); assert(timedPassKernel.runTimedSchedulerPass(1, recordRun, &timedPassRuns, advanceTimer, &timedPassKernel) == 1); assert((timedPassRuns == std::vector{0})); remorse::ProcessKernel standKernel; remorse::u16 standGroups[32]{}; standKernel.createProcess(2, standGroups); assert(!standKernel.itemHasProcess(0x1234) && !standKernel.itemHasProcess(0x1235)); bool oversizedStandRejected = false; try { standKernel.createStandProcess(2, 0x2400, standGroups); } catch (const std::out_of_range&) { oversizedStandRejected = true; } assert(oversizedStandRejected && standGroups[2] == 1 && standKernel.processCount() == 1); 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)); remorse::ProcessKernel cachedItemKernel; remorse::u16 cachedItemGroups[32]{}; for (int index = 0; index < 3; ++index) { cachedItemKernel.createProcess(1, cachedItemGroups); } auto* itemProcess = cachedItemKernel.createItemProcess(1, 0x100, cachedItemGroups); assert(cachedItemKernel.processAt(3) == itemProcess && cachedItemKernel.allocationSizeAt(3) == 0x32); assert(std::strcmp(cachedItemKernel.processNameAt(3), "ItemProcess") == 0); assert(itemProcess->bytes[0] == 0x2f && itemProcess->bytes[1] == 0x10); assert(itemProcess->bytes[4] == 0 && itemProcess->bytes[5] == 1); assert(itemProcess->bytes[6] == 0xf1 && itemProcess->bytes[7] == 0); assert(cachedItemKernel.itemHasProcess(0x100) && cachedItemKernel.itemProcessTypes(0x100) == 2); assert(cachedItemGroups[1] == 4); auto* sharedItemProcess = cachedItemKernel.createItemProcess(1, 0x100, cachedItemGroups); assert(cachedItemKernel.itemProcessTypes(0x100) == 2); cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0x101); assert(cachedItemKernel.itemHasProcess(0x100) && cachedItemKernel.itemHasProcess(0x101)); assert(cachedItemKernel.itemProcessTypes(0x100) == 2 && cachedItemKernel.itemProcessTypes(0x101) == 2); cachedItemKernel.setProcessEventTypeChecked(*sharedItemProcess, 0xf2); assert(cachedItemKernel.itemProcessTypes(0x101) == 4); cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0); assert(!cachedItemKernel.itemHasProcess(0x101) && cachedItemKernel.itemProcessTypes(0x101) == 0); cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0x102); assert(cachedItemKernel.itemHasProcess(0x102) && cachedItemKernel.itemProcessTypes(0x102) == 4); cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0x23ff); assert(cachedItemKernel.itemHasProcess(0x23ff) && cachedItemKernel.itemProcessTypes(0x23ff) == 4); bool invalidItemNumber = false; try { cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0x2400); } catch (const std::out_of_range&) { invalidItemNumber = true; } assert(invalidItemNumber && cachedItemKernel.itemHasProcess(0x23ff)); assert(!cachedItemKernel.itemHasProcess(0x2400) && cachedItemKernel.itemProcessTypes(0x2400) == 0); cachedItemKernel.setProcessSourceType(*sharedItemProcess, 0x102); cachedItemKernel.terminateProcess(4, cachedItemGroups); cachedItemKernel.clearTerminatedProcesses(); assert(!cachedItemKernel.itemHasProcess(0x102) && cachedItemKernel.itemProcessTypes(0x102) == 0); bool missingReleaseRejected = false; try { remorse::ItemProcess::run(*itemProcess, cachedItemKernel, cachedItemGroups, nullptr, nullptr); } catch (const std::invalid_argument&) { missingReleaseRejected = true; } assert(missingReleaseRejected && cachedItemKernel.itemHasProcess(0x100) && cachedItemGroups[1] == 4); remorse::u16 releasedItem = 0; struct ItemRunContext { remorse::ProcessKernel* kernel; remorse::u16 (*groups)[32]; remorse::u16* released; } itemRunContext{&cachedItemKernel, &cachedItemGroups, &releasedItem}; cachedItemKernel.scanRunnableProcesses(1, [](remorse::EntityDispatchEntryBase& process, void* context) { auto& state = *static_cast(context); if (process.bytes[0] == 0x2f && process.bytes[1] == 0x10) { remorse::ItemProcess::run(process, *state.kernel, *state.groups, [](remorse::u16 itemNumber, remorse::u32 eventMask, remorse::u16 eventId, void* result) { assert(eventMask == 0x200 && eventId == 9); *static_cast(result) = itemNumber; }, state.released); } }, &itemRunContext); assert(releasedItem == 0x100 && !cachedItemKernel.itemHasProcess(0x100)); assert(cachedItemKernel.itemProcessTypes(0x100) == 0); assert(cachedItemKernel.processAt(3) == nullptr && cachedItemGroups[1] == 3); 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(context)->visible; }, [](remorse::u16 npcNumber, void* context) { assert(npcNumber == 0x1234); return static_cast(context)->activity; }, [](remorse::u16 npcNumber, remorse::u16 activity, void* context) { assert(npcNumber == 0x1234 && activity == 0x71); return static_cast(context)->currentActivity; }, [](remorse::u16 npcNumber, void* context) { assert(npcNumber == 0x1234); return static_cast(context)->canSee; }, [](remorse::u16 npcNumber, remorse::u16 activity, void* context) { auto& state = *static_cast(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(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) == ®isteredTimer->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(~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{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; }