- Implemented tests for ProcessKernel in entity_dispatch_entry_tests.cpp, covering process creation, termination, and scanning of runnable processes. - Enhanced fixed_map_tests.cpp with tests for item allocation and release, including recycling of items and process ownership. - Expanded flex_archive_tests.cpp to validate the interaction between usecode events and item processes, ensuring correct behavior of spawned tasks and rendering. - Introduced usecode_vm_tests.cpp to thoroughly test the UsecodeInterpreterState and UsecodeProcessTask functionalities, including stack operations and opcode services.
512 lines
29 KiB
C++
512 lines
29 KiB
C++
#include "remorse/collision.h"
|
|
#include "remorse/entity_dispatch_entry.h"
|
|
#include "remorse/file_system.h"
|
|
#include "remorse/flex_archive.h"
|
|
#include "remorse/fixed_map.h"
|
|
#include "remorse/glob_archive.h"
|
|
#include "remorse/map_renderer.h"
|
|
#include "remorse/type_flags.h"
|
|
#include "remorse/usecode_archive.h"
|
|
#include "remorse/usecode_vm.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
|
|
int main()
|
|
{
|
|
const auto hasRetailEventEntry = [](const std::vector<remorse::u8>& body,
|
|
const std::array<remorse::u8, remorse::usecodeEventBytecodeOffset>& prefix) {
|
|
return body.size() > remorse::usecodeEventBytecodeOffset &&
|
|
std::equal(prefix.begin(), prefix.end(), body.begin()) &&
|
|
body[remorse::usecodeEventBytecodeOffset] == 0x0b;
|
|
};
|
|
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
|
assert(root.has_value());
|
|
|
|
const auto combatPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "COMBAT.DAT");
|
|
assert(combatPath.has_value());
|
|
|
|
const auto archive = remorse::loadFlexArchive(*combatPath);
|
|
assert(archive.has_value());
|
|
assert(archive->populatedEntries.size() == 14);
|
|
|
|
const auto firstEntry = remorse::readFlexEntry(*archive, 0);
|
|
assert(firstEntry.has_value());
|
|
assert(firstEntry->size() == 0x4f);
|
|
assert((*firstEntry)[0] == 'D');
|
|
assert((*firstEntry)[1] == 'u');
|
|
assert((*firstEntry)[2] == 'm');
|
|
assert((*firstEntry)[3] == 'b');
|
|
const auto usecodePath = remorse::resolveGameDataPath(*root, std::filesystem::path("USECODE") / "EUSECODE.FLX");
|
|
assert(usecodePath);
|
|
const auto usecode = remorse::loadFlexArchive(*usecodePath);
|
|
assert(usecode);
|
|
const auto retailOwnerRows = remorse::loadUsecodeOwnerRows(*usecode);
|
|
assert(retailOwnerRows && retailOwnerRows->size() % 13 == 0);
|
|
const auto eventClass = remorse::loadUsecodeClass(*usecode, 0x361);
|
|
assert(eventClass && eventClass->name == "EVENT" && eventClass->codeBase == 0xd4);
|
|
const auto eventBody = remorse::readUsecodeEventBody(*eventClass, 0x0a);
|
|
assert(eventBody && eventBody->length == 0x1fd6 && eventBody->relativeOffset == 1);
|
|
assert(eventBody->bytes.size() == 0x1fd6 && eventBody->bytes.front() == eventClass->bytes[0xd4]);
|
|
assert(!remorse::readUsecodeEventBody(*eventClass, 0x20));
|
|
assert(!remorse::loadUsecodeClass(*usecode, usecode->entryCount));
|
|
auto truncated = *eventClass;
|
|
truncated.bytes.resize(truncated.codeBase + 1);
|
|
assert(!remorse::readUsecodeEventBody(truncated, 0x0a));
|
|
auto invalidOffset = *eventClass;
|
|
invalidOffset.bytes[20 + 0x0a * 6 + 2] = 0;
|
|
assert(!remorse::readUsecodeEventBody(invalidOffset, 0x0a));
|
|
const auto rechargeClass = remorse::loadUsecodeClass(*usecode, 0x138);
|
|
assert(rechargeClass && rechargeClass->name == "RECHFLNS");
|
|
const auto releaseBody = remorse::readUsecodeEventBody(*rechargeClass, 9);
|
|
assert(releaseBody && releaseBody->length == 102 && releaseBody->relativeOffset == 0x256);
|
|
const std::array<remorse::u8, remorse::usecodeEventBytecodeOffset> releasePrefix{
|
|
0x5a, 0x02, 0x5c, 0x49, 0x00, 'R', 'E', 'C', 'H', 'F', 'L', 'N', 'S', 0x00};
|
|
assert(hasRetailEventEntry(releaseBody->bytes, releasePrefix));
|
|
assert(remorse::ownerRowAllowsEvent(*retailOwnerRows, 0x138, 0x200));
|
|
const remorse::UsecodeClassBases bases{0x200, 0x400, 0x600};
|
|
remorse::UsecodeItemIdentity identity{7, 0x45, 0, 0x1278, 4, true};
|
|
assert(remorse::selectUsecodeClassId(identity, bases) == 0x407);
|
|
identity.status = 2;
|
|
assert(remorse::selectUsecodeClassId(identity, bases) == 0x678);
|
|
identity.family = 0;
|
|
assert(remorse::selectUsecodeClassId(identity, bases) == 0x245);
|
|
identity.isNpc = false;
|
|
identity.status = 0;
|
|
assert(remorse::selectUsecodeClassId(identity, bases) == 0x245);
|
|
const auto fixedPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "FIXED.DAT");
|
|
const auto flagsPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
|
|
assert(fixedPath && flagsPath);
|
|
const auto fixedArchive = remorse::loadFixedMapArchive(*fixedPath);
|
|
const auto fixedMap = fixedArchive ? remorse::loadFixedMap(*fixedArchive, 1) : std::nullopt;
|
|
const auto typeFlags = remorse::loadTypeFlags(*flagsPath);
|
|
assert(fixedMap && typeFlags);
|
|
const auto stagedItems = remorse::stageFixedMapItems(*fixedMap);
|
|
assert(stagedItems && !stagedItems->empty());
|
|
const auto spawnEgg = remorse::findTeleportEgg(*stagedItems, *typeFlags, 0x1e);
|
|
assert(spawnEgg);
|
|
unsigned eligibleMapUseItems = 0;
|
|
unsigned keycardUseItems = 0;
|
|
std::optional<remorse::StagedFixedMapItem> boxItem;
|
|
for (const auto& item : *stagedItems) {
|
|
if (item.data.shape >= typeFlags->size()) continue;
|
|
const auto& shape = (*typeFlags)[item.data.shape];
|
|
const remorse::UsecodeItemIdentity mapItemIdentity{item.itemNumber, item.data.shape,
|
|
item.data.flags, item.data.quality, shape.family, false};
|
|
const auto eligible = remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
|
mapItemIdentity, remorse::UsecodeClassBases{0, 0x800, 0x900}, 0x02, 1);
|
|
if (eligible) {
|
|
++eligibleMapUseItems;
|
|
if (eligible->classId == 0x031d || eligible->classId == 0x031e) {
|
|
++keycardUseItems;
|
|
}
|
|
if (eligible->className == "BOX_NS" && item.data.frame == 0 && !boxItem) {
|
|
boxItem = item;
|
|
}
|
|
}
|
|
}
|
|
assert(eligibleMapUseItems == 218 && keycardUseItems == 10 && boxItem);
|
|
const auto globPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "GLOB.FLX");
|
|
const auto globs = globPath ? remorse::loadGlobObjects(*globPath) : std::nullopt;
|
|
assert(globs);
|
|
remorse::LiveFixedMapItems spawnedItems(*stagedItems);
|
|
const auto fastArea = remorse::calculateFastAreaRange({
|
|
spawnEgg->data.x * 2, spawnEgg->data.y * 2, spawnEgg->data.z});
|
|
unsigned hatched = 0;
|
|
for (const auto& egg : *stagedItems) {
|
|
if (egg.data.shape >= typeFlags->size() || (*typeFlags)[egg.data.shape].family != 3 ||
|
|
!remorse::isWorldPointInFastArea(egg.data.x * 2, egg.data.y * 2, fastArea)) continue;
|
|
for (const auto& glob : *globs) {
|
|
if (glob.index == egg.data.quality) {
|
|
const auto children = spawnedItems.hatchGlobEgg(egg.itemNumber, glob, *typeFlags);
|
|
if (children) hatched += static_cast<unsigned>(children->size());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
unsigned scriptChildren = 0;
|
|
for (const auto& item : spawnedItems.mapItems()) {
|
|
if (item.recordIndex != static_cast<std::size_t>(-1) || item.data.shape >= typeFlags->size()) continue;
|
|
const remorse::UsecodeItemIdentity childIdentity{item.itemNumber, item.data.shape,
|
|
item.data.flags, item.data.quality, (*typeFlags)[item.data.shape].family, false};
|
|
const auto use = remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows, childIdentity,
|
|
remorse::UsecodeClassBases{0, 0x800, 0x900}, 2, 1);
|
|
if (use) {
|
|
++scriptChildren;
|
|
}
|
|
}
|
|
assert(hatched == 4 && scriptChildren == 0);
|
|
const auto& staged = stagedItems->front();
|
|
assert(staged.data.shape < typeFlags->size());
|
|
const remorse::UsecodeItemIdentity stagedIdentity{staged.itemNumber, staged.data.shape,
|
|
staged.data.flags, staged.data.quality, (*typeFlags)[staged.data.shape].family, false};
|
|
const remorse::UsecodeClassBases retailBases{0, 0x800, 0x900};
|
|
assert(remorse::selectUsecodeClassId(stagedIdentity, retailBases) == staged.data.shape);
|
|
const auto stagedClass = remorse::loadUsecodeClass(*usecode,
|
|
remorse::selectUsecodeClassId(stagedIdentity, retailBases));
|
|
assert(!stagedClass);
|
|
assert(!remorse::ownerRowAllowsEvent(*retailOwnerRows, staged.data.shape, 0x200));
|
|
assert(!remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
|
stagedIdentity, retailBases, 0x200, 9));
|
|
bool foundStagedRelease = false;
|
|
for (const auto& item : *stagedItems) {
|
|
if (item.data.shape >= typeFlags->size()) {
|
|
continue;
|
|
}
|
|
const remorse::UsecodeItemIdentity mapItemIdentity{item.itemNumber, item.data.shape,
|
|
item.data.flags, item.data.quality, (*typeFlags)[item.data.shape].family, false};
|
|
if (remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
|
mapItemIdentity, retailBases, 0x200, 9)) {
|
|
foundStagedRelease = true;
|
|
break;
|
|
}
|
|
}
|
|
assert(foundStagedRelease);
|
|
const remorse::UsecodeItemIdentity rechargeItem{0x100, 0x138, 0, 0, 0, false};
|
|
const auto eligibleRelease = remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
|
rechargeItem, retailBases, 0x200, 9);
|
|
assert(eligibleRelease && eligibleRelease->classId == 0x138 &&
|
|
eligibleRelease->className == "RECHFLNS" && eligibleRelease->body.bytes.size() == 102);
|
|
auto interpreter = remorse::UsecodeInterpreterState::initialize({0x00, 0x01});
|
|
assert(interpreter && interpreter->stackTop == 0xca && interpreter->stackLimit == 199);
|
|
assert(interpreter->valueStack[0xca] == 0x00 && interpreter->valueStack[0xcb] == 0x01);
|
|
assert(interpreter->pushCodeAddress(eligibleRelease->classId, 9));
|
|
assert(interpreter->callFrameCount == 1 && interpreter->callFrames[0].offset == 0x138 &&
|
|
interpreter->callFrames[0].segment == 9);
|
|
for (unsigned index = 1; index < 0x1f; ++index) {
|
|
assert(interpreter->pushCodeAddress(static_cast<remorse::u16>(index), 0));
|
|
}
|
|
assert(!interpreter->pushCodeAddress(0xffff, 0));
|
|
assert(interpreter->callFrameCount == 0x1f && interpreter->callFrames[31].offset == 0);
|
|
assert(!remorse::UsecodeInterpreterState::initialize(std::vector<remorse::u8>(0xcd)));
|
|
const auto cardClass = remorse::loadUsecodeClass(*usecode, 0x31d);
|
|
const auto cardUse = cardClass ? remorse::readUsecodeEventBody(*cardClass, 1) : std::nullopt;
|
|
assert(cardClass && cardClass->name == "CARD_NS" && cardUse && cardUse->bytes.size() == 56);
|
|
const std::array<remorse::u8, remorse::usecodeEventBytecodeOffset> cardPrefix{
|
|
0x5a, 0x00, 0x5c, 0x24, 0x00, 'C', 'A', 'R', 'D', '_', 'N', 'S', 0x00, 0x00};
|
|
assert(hasRetailEventEntry(cardUse->bytes, cardPrefix));
|
|
std::vector<remorse::u8> frame(0x80);
|
|
frame[remorse::usecodeFrameBaseOffset + 6] = 0x00;
|
|
frame[remorse::usecodeFrameBaseOffset + 7] = 0x01;
|
|
std::vector<remorse::u8> itemMemory(0x102);
|
|
itemMemory[0x100] = 0x34;
|
|
itemMemory[0x101] = 0x12;
|
|
remorse::ProcessKernel vmKernel;
|
|
remorse::u16 vmGroupCounts[32]{};
|
|
for (int index = 0; index < 3; ++index) {
|
|
vmKernel.createProcess(1, vmGroupCounts);
|
|
}
|
|
auto* vmProcess = vmKernel.createUsecodeProcess(1, vmGroupCounts);
|
|
const remorse::u16 vmProcessId = static_cast<remorse::u16>(
|
|
vmProcess->base.bytes[2] | (static_cast<remorse::u16>(vmProcess->base.bytes[3]) << 8));
|
|
auto cardTask = remorse::UsecodeProcessTask::create(vmProcessId, 0x31d, 1, 0x1234,
|
|
cardUse->bytes);
|
|
assert(cardTask && cardTask->programCounter == remorse::usecodeEventBytecodeOffset &&
|
|
cardTask->frame.size() == 0x80 &&
|
|
cardTask->frame[remorse::usecodeFrameBaseOffset + 7] == 1 &&
|
|
cardTask->itemMemory[0x100] == 0x34 &&
|
|
cardTask->itemMemory[0x101] == 0x12);
|
|
remorse::UsecodeTaskRegistry taskRegistry;
|
|
assert(taskRegistry.add(std::move(*cardTask)) && taskRegistry.size() == 1);
|
|
cardTask.reset();
|
|
auto* registeredCardTask = taskRegistry.find(vmProcessId);
|
|
assert(registeredCardTask && registeredCardTask->classId == 0x31d &&
|
|
registeredCardTask->eventSlot == 1 && registeredCardTask->sourceItem == 0x1234);
|
|
assert(!taskRegistry.add(*registeredCardTask));
|
|
const auto switchClass = remorse::loadUsecodeClass(*usecode, 0x0a16);
|
|
const auto switchUse = switchClass ? remorse::readUsecodeEventBody(*switchClass, 0x21) : std::nullopt;
|
|
assert(switchClass && switchClass->name == "SWITCH" && switchUse);
|
|
remorse::UsecodeOpcodeServices opcodeServices;
|
|
opcodeServices.readFarWord = [registeredCardTask](remorse::u16 offset, remorse::u16 segment)
|
|
-> std::optional<remorse::u16> {
|
|
return registeredCardTask->readMappedItemWord(remorse::FarPtr16{offset, segment});
|
|
};
|
|
opcodeServices.setInfo = [&vmKernel, vmProcessId](remorse::u16 eventType,
|
|
remorse::u16 sourceItem) {
|
|
return vmKernel.setProcessInfo(vmProcessId, eventType, sourceItem);
|
|
};
|
|
std::optional<std::array<remorse::u16, 3>> spawnedUsecode;
|
|
remorse::u16 spawnedProcessId = 0;
|
|
opcodeServices.processCount = [&vmKernel]() { return vmKernel.processCount(); };
|
|
opcodeServices.spawn = [&spawnedUsecode, &spawnedProcessId, &vmKernel, &vmGroupCounts,
|
|
&taskRegistry, &switchUse](remorse::u16 classId, remorse::u16 eventSlot,
|
|
remorse::u8 extraArgumentBytes, remorse::u8 itemArgumentSize,
|
|
remorse::u16 itemArgument, const remorse::u8*) -> std::optional<remorse::u16> {
|
|
assert(extraArgumentBytes == 0 && itemArgumentSize == 2);
|
|
spawnedUsecode = std::array<remorse::u16, 3>{classId, eventSlot, itemArgument};
|
|
auto* child = vmKernel.createUsecodeProcess(1, vmGroupCounts);
|
|
spawnedProcessId = static_cast<remorse::u16>(
|
|
child->base.bytes[2] | (static_cast<remorse::u16>(child->base.bytes[3]) << 8));
|
|
auto childTask = remorse::UsecodeProcessTask::create(spawnedProcessId, classId,
|
|
eventSlot, itemArgument, switchUse->bytes);
|
|
if (!childTask || !taskRegistry.add(std::move(*childTask))) {
|
|
vmKernel.terminateProcess(spawnedProcessId, vmGroupCounts);
|
|
vmKernel.clearTerminatedProcesses();
|
|
return std::nullopt;
|
|
}
|
|
return spawnedProcessId;
|
|
};
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Advanced &&
|
|
registeredCardTask->programCounter == 17);
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Advanced &&
|
|
registeredCardTask->programCounter == 19);
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Advanced &&
|
|
registeredCardTask->programCounter == 21);
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::SetInfo &&
|
|
registeredCardTask->programCounter == 22);
|
|
assert(registeredCardTask->interpreter.lastSetInfoOperands &&
|
|
(*registeredCardTask->interpreter.lastSetInfoOperands)[0] == 0x1234 &&
|
|
(*registeredCardTask->interpreter.lastSetInfoOperands)[1] == 0x0207);
|
|
assert(vmKernel.processAt(vmProcessId)->bytes[0x04] == 0x34 &&
|
|
vmKernel.processAt(vmProcessId)->bytes[0x05] == 0x12 &&
|
|
vmKernel.processAt(vmProcessId)->bytes[0x06] == 0x07 &&
|
|
vmKernel.processAt(vmProcessId)->bytes[0x07] == 0x02);
|
|
opcodeServices.setInfo = [](remorse::u16, remorse::u16) { return false; };
|
|
const auto stackTopBeforeRejectedInfo = registeredCardTask->interpreter.stackTop;
|
|
assert(registeredCardTask->interpreter.pushWord(0x1234) &&
|
|
registeredCardTask->interpreter.pushWord(0x0207));
|
|
const auto stackTopWithRejectedInfo = registeredCardTask->interpreter.stackTop;
|
|
registeredCardTask->programCounter = 21;
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::ServiceRejected &&
|
|
registeredCardTask->programCounter == 22);
|
|
assert(registeredCardTask->interpreter.stackTop == stackTopWithRejectedInfo &&
|
|
registeredCardTask->interpreter.popWord() == 0x0207 &&
|
|
registeredCardTask->interpreter.popWord() == 0x1234);
|
|
registeredCardTask->interpreter.stackTop = stackTopBeforeRejectedInfo;
|
|
registeredCardTask->programCounter = 22;
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Advanced &&
|
|
registeredCardTask->programCounter == 23 && registeredCardTask->interpreter.processExcluded);
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Advanced &&
|
|
registeredCardTask->programCounter == 26);
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Advanced &&
|
|
registeredCardTask->programCounter == 28);
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Spawned &&
|
|
registeredCardTask->programCounter == 35);
|
|
assert(spawnedUsecode && (*spawnedUsecode)[0] == 0x0a16 &&
|
|
(*spawnedUsecode)[1] == 0x21 && (*spawnedUsecode)[2] == 0x1234);
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Advanced &&
|
|
registeredCardTask->programCounter == 36);
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Advanced &&
|
|
registeredCardTask->programCounter == 37 &&
|
|
registeredCardTask->interpreter.temporaryWord == spawnedProcessId);
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Advanced &&
|
|
registeredCardTask->programCounter == 40);
|
|
assert(registeredCardTask->step(opcodeServices) == remorse::UsecodeStepResult::Returned &&
|
|
registeredCardTask->programCounter == 41);
|
|
auto* registeredSwitchTask = taskRegistry.find(spawnedProcessId);
|
|
assert(registeredSwitchTask && registeredSwitchTask->classId == 0x0a16 &&
|
|
registeredSwitchTask->eventSlot == 0x21 && registeredSwitchTask->sourceItem == 0x1234 &&
|
|
vmKernel.processAt(spawnedProcessId) != nullptr);
|
|
vmKernel.terminateProcess(vmProcessId, vmGroupCounts);
|
|
vmKernel.clearTerminatedProcesses();
|
|
assert(taskRegistry.remove(vmProcessId) && taskRegistry.size() == 1 &&
|
|
!taskRegistry.find(vmProcessId) && taskRegistry.find(spawnedProcessId) == registeredSwitchTask);
|
|
struct SwitchScheduleContext {
|
|
remorse::ProcessKernel* kernel;
|
|
remorse::UsecodeTaskRegistry* tasks;
|
|
remorse::u16 processId;
|
|
unsigned runs = 0;
|
|
} switchSchedule{&vmKernel, &taskRegistry, spawnedProcessId};
|
|
vmKernel.scanRunnableProcesses(1, [](remorse::EntityDispatchEntryBase& process, void* context) {
|
|
auto& schedule = *static_cast<SwitchScheduleContext*>(context);
|
|
const remorse::u16 processId = static_cast<remorse::u16>(
|
|
process.bytes[2] | (static_cast<remorse::u16>(process.bytes[3]) << 8));
|
|
if (processId != schedule.processId) {
|
|
return;
|
|
}
|
|
++schedule.runs;
|
|
auto* task = schedule.tasks->find(processId);
|
|
assert(task);
|
|
remorse::UsecodeOpcodeServices services;
|
|
services.readFarWord = [task](remorse::u16 offset, remorse::u16 segment)
|
|
-> std::optional<remorse::u16> {
|
|
return task->readMappedItemWord(remorse::FarPtr16{offset, segment});
|
|
};
|
|
services.setInfo = [schedule, processId](remorse::u16 eventType, remorse::u16 sourceItem) {
|
|
return schedule.kernel->setProcessInfo(processId, eventType, sourceItem);
|
|
};
|
|
assert(task->step(services) == remorse::UsecodeStepResult::Advanced);
|
|
assert(task->step(services) == remorse::UsecodeStepResult::Advanced);
|
|
assert(task->step(services) == remorse::UsecodeStepResult::Advanced);
|
|
assert(task->step(services) == remorse::UsecodeStepResult::SetInfo);
|
|
}, &switchSchedule);
|
|
assert(switchSchedule.runs == 1 && registeredSwitchTask->programCounter == 22 &&
|
|
vmKernel.processAt(spawnedProcessId)->bytes[0x04] == 0x34 &&
|
|
vmKernel.processAt(spawnedProcessId)->bytes[0x06] == 0x07);
|
|
remorse::StagedFixedMapItem switchItem;
|
|
switchItem.itemNumber = 0x1234;
|
|
switchItem.data.shape = 0x216;
|
|
switchItem.data.quality = 3;
|
|
remorse::LiveFixedMapItems switchWorld({switchItem});
|
|
auto switchTask = remorse::UsecodeProcessTask::create(spawnedProcessId, 0x0a16, 0x21,
|
|
switchItem.itemNumber, switchUse->bytes);
|
|
assert(switchTask);
|
|
const std::uint64_t heldKeycards = std::uint64_t{1} << switchItem.data.quality;
|
|
remorse::UsecodeOpcodeServices switchServices;
|
|
switchServices.readFarWord = [&switchTask](remorse::u16 offset, remorse::u16 segment)
|
|
-> std::optional<remorse::u16> {
|
|
return switchTask->readMappedItemWord(remorse::FarPtr16{offset, segment});
|
|
};
|
|
switchServices.setInfo = [&vmKernel, spawnedProcessId](remorse::u16 eventType,
|
|
remorse::u16 sourceItem) {
|
|
return vmKernel.setProcessInfo(spawnedProcessId, eventType, sourceItem);
|
|
};
|
|
switchServices.callIntrinsic = [&switchTask, &switchWorld, heldKeycards](remorse::u16 ordinal,
|
|
remorse::u8 argumentBytes, const remorse::u8* arguments) -> std::optional<remorse::u16> {
|
|
const auto itemNumber = [&]() -> std::optional<remorse::u16> {
|
|
if (argumentBytes < 4) return std::nullopt;
|
|
const remorse::u16 offset = static_cast<remorse::u16>(arguments[0] |
|
|
(static_cast<remorse::u16>(arguments[1]) << 8));
|
|
const remorse::u16 segment = static_cast<remorse::u16>(arguments[2] |
|
|
(static_cast<remorse::u16>(arguments[3]) << 8));
|
|
return switchTask->readMappedItemWord(remorse::FarPtr16{offset, segment});
|
|
};
|
|
if (ordinal == 0x00 && argumentBytes == 0) return remorse::u16{0};
|
|
if (ordinal == 0xfd && argumentBytes == 2) {
|
|
const remorse::u16 keycard = static_cast<remorse::u16>(arguments[0] |
|
|
(static_cast<remorse::u16>(arguments[1]) << 8));
|
|
return static_cast<remorse::u16>((heldKeycards & (std::uint64_t{1} << (keycard & 0x1f))) != 0);
|
|
}
|
|
if (ordinal == 0x18 && argumentBytes == 4) return remorse::u16{0};
|
|
if (ordinal == 0x15 && argumentBytes == 6) return remorse::u16{0};
|
|
const auto source = itemNumber();
|
|
if (!source) return std::nullopt;
|
|
const auto* item = switchWorld.findItem(*source);
|
|
if (ordinal == 0xea && argumentBytes == 4 && item) {
|
|
return static_cast<remorse::u16>(item->data.quality & 0xff);
|
|
}
|
|
if (ordinal == 0x01 && argumentBytes == 4 && item) {
|
|
return static_cast<remorse::u16>(item->data.frame);
|
|
}
|
|
if (ordinal == 0x59 && argumentBytes == 6 && arguments[4] == 4 && item) {
|
|
return switchWorld.setItemFrame(*source, arguments[4])
|
|
? std::optional<remorse::u16>{remorse::u16{0}} : std::nullopt;
|
|
}
|
|
return std::nullopt;
|
|
};
|
|
bool switchFrameChanged = false;
|
|
for (unsigned instruction = 0; instruction < 100 && !switchFrameChanged; ++instruction) {
|
|
const auto result = switchTask->step(switchServices);
|
|
const auto* item = switchWorld.findItem(switchItem.itemNumber);
|
|
switchFrameChanged = item && item->data.frame == 4;
|
|
assert(result == remorse::UsecodeStepResult::Advanced ||
|
|
result == remorse::UsecodeStepResult::SetInfo);
|
|
}
|
|
assert(switchFrameChanged && switchTask->programCounter < switchUse->bytes.size() &&
|
|
switchTask->sourceItem == switchItem.itemNumber);
|
|
const auto boxClass = remorse::loadUsecodeClass(*usecode, 0x0081);
|
|
const auto boxUse = boxClass ? remorse::readUsecodeEventBody(*boxClass, 1) : std::nullopt;
|
|
assert(boxClass && boxClass->name == "BOX_NS" && boxUse && boxItem);
|
|
const std::array<remorse::u8, remorse::usecodeEventBytecodeOffset> boxPrefix{
|
|
0x5a, 0x02, 0x5c, 0xd6, 0x01, 'B', 'O', 'X', '_', 'N', 'S', 0x00, 0x00, 0x00};
|
|
assert(hasRetailEventEntry(boxUse->bytes, boxPrefix));
|
|
remorse::LiveFixedMapItems boxWorld(*stagedItems);
|
|
auto boxTask = remorse::UsecodeProcessTask::create(0x0340, 0x0081, 1,
|
|
boxItem->itemNumber, boxUse->bytes);
|
|
assert(boxTask);
|
|
const auto freeClass = remorse::loadUsecodeClass(*usecode, 0x0a0c);
|
|
const auto freeTimerBody = freeClass ? remorse::readUsecodeEventBody(*freeClass, 0x32) : std::nullopt;
|
|
auto boxRenderer = remorse::loadMapSceneRenderer(*root, 1);
|
|
assert(freeClass && freeTimerBody && boxRenderer);
|
|
remorse::MapRenderOptions boxRenderOptions;
|
|
boxRenderOptions.mapIndex = 1;
|
|
boxRenderOptions.maxWidth = 320;
|
|
boxRenderOptions.maxHeight = 200;
|
|
boxRenderOptions.viewCenter = remorse::WorldPoint{
|
|
boxItem->data.x * 2, boxItem->data.y * 2, boxItem->data.z};
|
|
const auto boxBefore = boxRenderer->render(boxRenderOptions, &boxWorld);
|
|
assert(boxBefore);
|
|
remorse::UsecodeOpcodeServices boxServices;
|
|
boxServices.readFarWord = [&boxTask](remorse::u16 offset, remorse::u16 segment)
|
|
-> std::optional<remorse::u16> {
|
|
return boxTask->readMappedItemWord(remorse::FarPtr16{offset, segment});
|
|
};
|
|
boxServices.setInfo = [](remorse::u16, remorse::u16) { return true; };
|
|
boxServices.processCount = []() { return remorse::u16{2}; };
|
|
boxServices.currentProcessId = []() { return remorse::u16{0x0340}; };
|
|
boxServices.callIntrinsic = [&boxTask, &boxWorld](remorse::u16 ordinal,
|
|
remorse::u8 argumentBytes, const remorse::u8* arguments) -> std::optional<remorse::u16> {
|
|
if ((ordinal == 0x15 || ordinal == 0x3a) && argumentBytes == 6) return remorse::u16{0};
|
|
if (argumentBytes < 4) return std::nullopt;
|
|
const remorse::u16 offset = static_cast<remorse::u16>(arguments[0] |
|
|
(static_cast<remorse::u16>(arguments[1]) << 8));
|
|
const remorse::u16 segment = static_cast<remorse::u16>(arguments[2] |
|
|
(static_cast<remorse::u16>(arguments[3]) << 8));
|
|
const auto itemNumber = boxTask->readMappedItemWord(remorse::FarPtr16{offset, segment});
|
|
if (!itemNumber) return std::nullopt;
|
|
const auto* item = boxWorld.findItem(*itemNumber);
|
|
if (ordinal == 0x01 && argumentBytes == 4 && item) return item->data.frame;
|
|
if (ordinal == 0x02 && argumentBytes == 6 && item) {
|
|
const remorse::u8 frame = static_cast<remorse::u8>(arguments[4] |
|
|
(static_cast<remorse::u16>(arguments[5]) << 8));
|
|
return boxWorld.setItemFrame(*itemNumber, frame)
|
|
? std::optional<remorse::u16>{remorse::u16{0}} : std::nullopt;
|
|
}
|
|
if (ordinal == 0x03 && argumentBytes == 4 && item) return item->data.mapNumber;
|
|
return std::nullopt;
|
|
};
|
|
remorse::u16 nextTimerProcessId = 0x0350;
|
|
remorse::u32 timerTick = 100;
|
|
remorse::u16 lastTimerDelay = 0;
|
|
std::optional<remorse::UsecodeProcessTask> timerTask;
|
|
boxServices.spawn = [&nextTimerProcessId, &timerTick, &lastTimerDelay, &timerTask,
|
|
&freeTimerBody](remorse::u16 classId,
|
|
remorse::u16 eventSlot, remorse::u8 extraArgumentBytes, remorse::u8 itemArgumentSize,
|
|
remorse::u16 itemArgument, const remorse::u8* extraArguments) -> std::optional<remorse::u16> {
|
|
if (classId != 0x0a0c || eventSlot != 0x32 || extraArgumentBytes != 2 ||
|
|
itemArgumentSize != 0 || itemArgument != 0) return std::nullopt;
|
|
lastTimerDelay = static_cast<remorse::u16>(extraArguments[0] |
|
|
(static_cast<remorse::u16>(extraArguments[1]) << 8));
|
|
std::vector<remorse::u8> timerArguments(extraArguments, extraArguments + extraArgumentBytes);
|
|
const remorse::u16 processId = nextTimerProcessId++;
|
|
timerTask = remorse::UsecodeProcessTask::create(processId, classId, eventSlot,
|
|
itemArgument, freeTimerBody->bytes, itemArgumentSize, std::move(timerArguments));
|
|
if (!timerTask) return std::nullopt;
|
|
timerTask->timerDelayProcess = true;
|
|
timerTask->wakeTick = timerTick + lastTimerDelay;
|
|
return processId;
|
|
};
|
|
for (remorse::u8 expectedFrame = 0; expectedFrame < 6; ++expectedFrame) {
|
|
bool yielded = false;
|
|
for (unsigned instruction = 0; instruction < 500 && !yielded; ++instruction) {
|
|
const auto result = boxTask->step(boxServices);
|
|
if (result == remorse::UsecodeStepResult::Yielded) {
|
|
yielded = true;
|
|
break;
|
|
}
|
|
assert(result == remorse::UsecodeStepResult::Advanced ||
|
|
result == remorse::UsecodeStepResult::SetInfo ||
|
|
result == remorse::UsecodeStepResult::Spawned);
|
|
}
|
|
assert(yielded && lastTimerDelay == 5 && timerTask &&
|
|
boxTask->interpreter.impliedProcessId == timerTask->processId);
|
|
assert(boxWorld.findItem(boxItem->itemNumber)->data.frame == expectedFrame);
|
|
assert(timerTask->frame[remorse::usecodeFrameBaseOffset + 6] == 5 &&
|
|
timerTask->timerDelayProcess && timerTask->wakeTick == timerTick + 5);
|
|
timerTick = timerTask->wakeTick;
|
|
timerTask.reset();
|
|
boxTask->interpreter.impliedProcessId = 0;
|
|
}
|
|
const auto boxAfter = boxRenderer->render(boxRenderOptions, &boxWorld);
|
|
assert(boxAfter && boxBefore->pixels.size() == boxAfter->pixels.size());
|
|
assert(!std::equal(boxBefore->pixels.begin(), boxBefore->pixels.end(), boxAfter->pixels.begin(),
|
|
[](const auto& left, const auto& right) {
|
|
return left.red == right.red && left.green == right.green && left.blue == right.blue;
|
|
}));
|
|
assert(!remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
|
rechargeItem, retailBases, 0, 9));
|
|
std::vector<remorse::u8> ownerRows(26);
|
|
ownerRows[13] = 0x00;
|
|
ownerRows[14] = 0x02;
|
|
ownerRows[16] = 0x01;
|
|
assert(remorse::ownerRowAllowsEvent(ownerRows, 1, 0x200));
|
|
assert(remorse::ownerRowAllowsEvent(ownerRows, 1, 0x1000000));
|
|
assert(!remorse::ownerRowAllowsEvent(ownerRows, 1, 0x400));
|
|
assert(!remorse::ownerRowAllowsEvent(ownerRows, 0, 0x200));
|
|
assert(!remorse::ownerRowAllowsEvent(ownerRows, 2, 0x200));
|
|
assert(!remorse::ownerRowAllowsEvent(ownerRows, 1, 0));
|
|
return 0;
|
|
}
|