Refactor usecode task management and enhance teleportation logic
- Improved the queueUsecodeTask function to streamline process termination and context management. - Added robust error handling for event context creation and memory writing. - Enhanced the loadSceneAsync function to better handle avatar teleportation, including a new teleporter process. - Expanded entity dispatch tests to cover movement and teleporter processes, ensuring correct behavior under various conditions. - Introduced new tests for usecode runtime memory allocation and context creation, validating edge cases and error handling. - Updated flex archive tests to ensure proper loading and unloading of usecode slot chunk caches.
This commit is contained in:
parent
8c23bb02c9
commit
0771c86043
12 changed files with 1091 additions and 253 deletions
|
|
@ -1050,5 +1050,101 @@ int main()
|
|||
assert(animationKernel.processAt(6) == nullptr && animationKernel.allocationSizeAt(6) == 0);
|
||||
assert(animationGroups[1] == 6 && animationKernel.itemHasProcess(1));
|
||||
|
||||
remorse::ProcessKernel movementKernel;
|
||||
remorse::u16 movementGroups[32]{};
|
||||
for (int reservedSlot = 0; reservedSlot < 3; ++reservedSlot) {
|
||||
movementKernel.createProcess(1, movementGroups);
|
||||
}
|
||||
assert(movementKernel.createSystemTimerProcess(1, movementGroups));
|
||||
assert(movementKernel.createKeyboardProcess(1, movementGroups));
|
||||
assert(movementKernel.createControllerProcess(1, movementGroups));
|
||||
remorse::KeyEvent movementKey{};
|
||||
movementKey.scanCode = 0x48;
|
||||
movementKey.field8 = 1;
|
||||
assert(movementKernel.queuePendingKeyEvent(movementKey));
|
||||
remorse::ControllerMovementState movementController;
|
||||
movementKernel.dispatchPendingKeyEvents([](remorse::EntityDispatchEntryBase& process,
|
||||
remorse::u16 selector, remorse::u16 filter, remorse::KeyEvent& event, void* context) {
|
||||
if (process.bytes[0] == 0xa6 && process.bytes[1] == 0x3a) {
|
||||
assert(selector == 0x10 && filter == 0);
|
||||
static_cast<remorse::ControllerMovementState*>(context)->handleDirectionalKey(
|
||||
event.scanCode, event.modifiers, event.field8 != 0);
|
||||
}
|
||||
}, &movementController);
|
||||
const auto movementRequest = movementController.request();
|
||||
assert(movementRequest.animation == 1 && movementRequest.direction == 14);
|
||||
|
||||
remorse::AnimationClip controllerWalk{};
|
||||
controllerWalk.header = {17, 4, 0, 0};
|
||||
controllerWalk.frameCount = 17;
|
||||
controllerWalk.frames.resize(17 * 8);
|
||||
auto& controllerStep = controllerWalk.frames[7 * 17 + 1];
|
||||
controllerStep.bytes[4] = 1;
|
||||
controllerStep.bytes[5] = 2;
|
||||
remorse::LiveFixedMapItems movementItems({});
|
||||
assert(movementItems.initializeAvatar());
|
||||
remorse::StagedFixedMapItem movementSpawn{};
|
||||
movementSpawn.data.x = 512;
|
||||
movementSpawn.data.y = 512;
|
||||
assert(movementItems.teleportAvatar(movementSpawn, 1));
|
||||
std::vector<remorse::ShapeTypeInfo> movementShapes(2);
|
||||
movementShapes[1].x = movementShapes[1].y = movementShapes[1].z = 2;
|
||||
auto* controllerAnimation = movementKernel.createAnimPrimitiveProcess(1, 1,
|
||||
movementRequest.animation, movementRequest.direction, controllerWalk, movementGroups);
|
||||
assert(controllerAnimation);
|
||||
const remorse::u16 controllerAnimationId = static_cast<remorse::u16>(
|
||||
controllerAnimation->base.bytes[2] | (static_cast<remorse::u16>(controllerAnimation->base.bytes[3]) << 8));
|
||||
assert(movementKernel.startAnimPrimitiveProcess(controllerAnimationId, movementItems, controllerWalk));
|
||||
struct ControllerTickContext {
|
||||
remorse::ProcessKernel* kernel;
|
||||
remorse::LiveFixedMapItems* items;
|
||||
const remorse::AnimationClip* clip;
|
||||
const std::vector<remorse::ShapeTypeInfo>* shapes;
|
||||
remorse::FastAreaRange fastArea;
|
||||
remorse::u16 processId;
|
||||
std::optional<bool> advanced;
|
||||
} controllerTick{&movementKernel, &movementItems, &controllerWalk, &movementShapes,
|
||||
remorse::calculateFastAreaRange({512, 512, 0}), controllerAnimationId};
|
||||
movementKernel.runTimedSchedulerPass(1, [](remorse::EntityDispatchEntryBase& process, void* context) {
|
||||
auto& tick = *static_cast<ControllerTickContext*>(context);
|
||||
if (&process == tick.kernel->processAt(tick.processId)) {
|
||||
tick.advanced = tick.kernel->advanceGroundedAnimPrimitive(tick.processId,
|
||||
*tick.items, *tick.clip, *tick.shapes, tick.fastArea);
|
||||
}
|
||||
}, &controllerTick);
|
||||
assert(controllerTick.advanced == true && movementKernel.itemHasProcess(1));
|
||||
assert(movementItems.findItem(1)->data.x == 510 && movementItems.findItem(1)->data.y == 510 &&
|
||||
movementItems.findNpcAnimation(1)->frame == 1);
|
||||
movementKey.field8 = 0;
|
||||
assert(movementKernel.queuePendingKeyEvent(movementKey));
|
||||
movementKernel.dispatchPendingKeyEvents([](remorse::EntityDispatchEntryBase& process,
|
||||
remorse::u16 selector, remorse::u16 filter, remorse::KeyEvent& event, void* context) {
|
||||
if (process.bytes[0] == 0xa6 && process.bytes[1] == 0x3a) {
|
||||
assert(selector == 0x10 && filter == 0);
|
||||
static_cast<remorse::ControllerMovementState*>(context)->handleDirectionalKey(
|
||||
event.scanCode, event.modifiers, event.field8 != 0);
|
||||
}
|
||||
}, &movementController);
|
||||
assert(movementController.request().animation == 0 && movementController.request().direction == 14);
|
||||
|
||||
remorse::ProcessKernel teleporterKernel;
|
||||
remorse::u16 teleporterGroups[32]{};
|
||||
for (int reservedSlot = 0; reservedSlot < 3; ++reservedSlot) {
|
||||
teleporterKernel.createProcess(1, teleporterGroups);
|
||||
}
|
||||
auto* teleporter = teleporterKernel.createTeleporterProcess(1, 1, 0x1e, teleporterGroups);
|
||||
const remorse::u16 teleporterProcessId = static_cast<remorse::u16>(
|
||||
teleporter->base.bytes[2] | (static_cast<remorse::u16>(teleporter->base.bytes[3]) << 8));
|
||||
assert(teleporterKernel.processAt(teleporterProcessId) == &teleporter->base &&
|
||||
teleporterKernel.allocationSizeAt(teleporterProcessId) == 0x36 &&
|
||||
std::strcmp(teleporterKernel.processNameAt(teleporterProcessId), "Teleporter") == 0);
|
||||
assert(teleporter->base.bytes[0] == 0x3a && teleporter->base.bytes[1] == 0x0f &&
|
||||
teleporter->base.bytes[0x32] == 1 && teleporter->base.bytes[0x33] == 0 &&
|
||||
teleporter->base.bytes[0x34] == 0x1e && teleporter->base.bytes[0x35] == 0);
|
||||
teleporter->base.bytes[0x18] |= 4;
|
||||
teleporterKernel.terminateProcess(teleporterProcessId, teleporterGroups);
|
||||
assert(teleporterKernel.processAt(teleporterProcessId) == nullptr &&
|
||||
teleporterKernel.allocationSizeAt(teleporterProcessId) == 0 && teleporterGroups[1] == 3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,26 @@ int main()
|
|||
std::equal(prefix.begin(), prefix.end(), body.begin()) &&
|
||||
body[remorse::usecodeEventBytecodeOffset] == 0x0b;
|
||||
};
|
||||
const auto readDescriptorPointer = [](const std::vector<remorse::u8>& descriptors,
|
||||
std::size_t chunkIndex) {
|
||||
const std::size_t offset = chunkIndex * 6 + 2;
|
||||
return static_cast<remorse::u32>(descriptors[offset]) |
|
||||
(static_cast<remorse::u32>(descriptors[offset + 1]) << 8) |
|
||||
(static_cast<remorse::u32>(descriptors[offset + 2]) << 16) |
|
||||
(static_cast<remorse::u32>(descriptors[offset + 3]) << 24);
|
||||
};
|
||||
|
||||
remorse::UsecodeRuntimeMemory boundedMemory;
|
||||
assert(!boundedMemory.allocate(0));
|
||||
assert(!boundedMemory.allocate(0x10001));
|
||||
const auto fullSegment = boundedMemory.allocate(0x10000, 0x5a);
|
||||
assert(fullSegment);
|
||||
const auto finalSegmentByte = boundedMemory.read({0xffff, fullSegment->segment}, 1);
|
||||
assert(finalSegmentByte && finalSegmentByte->size() == 1 && finalSegmentByte->front() == 0x5a);
|
||||
assert(!boundedMemory.read({0xffff, fullSegment->segment}, 2));
|
||||
assert(!boundedMemory.read({0, 0xffff}, 1));
|
||||
assert(boundedMemory.release(*fullSegment) && boundedMemory.allocationCount() == 0);
|
||||
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
assert(root.has_value());
|
||||
|
||||
|
|
@ -66,6 +86,36 @@ int main()
|
|||
[](remorse::u16 state) { return state == 0xffff; }));
|
||||
assert(std::equal(npcTriggerOwnerBuffers->descriptors.begin(), npcTriggerOwnerBuffers->descriptors.end(),
|
||||
npcTriggerClass->bytes.begin() + 20));
|
||||
remorse::UsecodeSlotChunkCache npcTriggerChunkCache(*npcTriggerClass);
|
||||
const auto originalNpcTriggerDescriptors = npcTriggerChunkCache.ownerBuffers().descriptors;
|
||||
std::optional<remorse::u16> npcTriggerChunkIndex;
|
||||
for (remorse::u16 chunkIndex = 0; chunkIndex < npcTriggerChunkCache.chunkCount(); ++chunkIndex) {
|
||||
if (npcTriggerChunkCache.ensureLoaded(chunkIndex)) {
|
||||
npcTriggerChunkIndex = chunkIndex;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert(npcTriggerChunkIndex);
|
||||
const auto npcTriggerSourcePointer = readDescriptorPointer(
|
||||
originalNpcTriggerDescriptors, *npcTriggerChunkIndex);
|
||||
const auto* loadedNpcTriggerProcess = npcTriggerChunkCache.loadedProcess(*npcTriggerChunkIndex);
|
||||
assert(loadedNpcTriggerProcess && loadedNpcTriggerProcess->layout.slotIndex == 0x363 &&
|
||||
loadedNpcTriggerProcess->layout.chunkIndex == *npcTriggerChunkIndex &&
|
||||
loadedNpcTriggerProcess->layout.savedSource.offset == static_cast<remorse::u16>(npcTriggerSourcePointer) &&
|
||||
loadedNpcTriggerProcess->layout.savedSource.segment == static_cast<remorse::u16>(npcTriggerSourcePointer >> 16));
|
||||
const auto loadedNpcTriggerBytes = npcTriggerChunkCache.runtimeMemory().read(
|
||||
loadedNpcTriggerProcess->layout.chunkBuffer, loadedNpcTriggerProcess->body.length);
|
||||
assert(loadedNpcTriggerBytes && *loadedNpcTriggerBytes == loadedNpcTriggerProcess->body.bytes &&
|
||||
readDescriptorPointer(npcTriggerChunkCache.ownerBuffers().descriptors, *npcTriggerChunkIndex) ==
|
||||
(static_cast<remorse::u32>(loadedNpcTriggerProcess->layout.chunkBuffer.offset) |
|
||||
(static_cast<remorse::u32>(loadedNpcTriggerProcess->layout.chunkBuffer.segment) << 16)));
|
||||
assert(npcTriggerChunkCache.release(*npcTriggerChunkIndex));
|
||||
assert(npcTriggerChunkCache.tryUnload(*npcTriggerChunkIndex));
|
||||
assert(npcTriggerChunkCache.loadedProcessCount() == 0 &&
|
||||
npcTriggerChunkCache.runtimeMemory().allocationCount() == 0 &&
|
||||
readDescriptorPointer(npcTriggerChunkCache.ownerBuffers().descriptors, *npcTriggerChunkIndex) ==
|
||||
npcTriggerSourcePointer);
|
||||
|
||||
auto inconsistentOwnerHeader = *eventClass;
|
||||
inconsistentOwnerHeader.extraSlotCount = 1;
|
||||
assert(!remorse::initializeUsecodeSlotOwnerBuffers(inconsistentOwnerHeader));
|
||||
|
|
@ -74,9 +124,24 @@ int main()
|
|||
assert(eventBody->bytes.size() == 0x1fd6 && eventBody->bytes.front() == eventClass->bytes[0xd4]);
|
||||
remorse::UsecodeSlotChunkCache eventChunkCache(*eventClass);
|
||||
assert(eventChunkCache.chunkCount() == 32);
|
||||
const auto sourceEventPointer = readDescriptorPointer(eventChunkCache.ownerBuffers().descriptors, 0x0a);
|
||||
const auto* loadedEventChunk = eventChunkCache.ensureLoaded(0x0a);
|
||||
assert(loadedEventChunk && loadedEventChunk->bytes == eventBody->bytes &&
|
||||
eventChunkCache.inUseCount(0x0a) == 1);
|
||||
const auto* loadedEventProcess = eventChunkCache.loadedProcess(0x0a);
|
||||
assert(loadedEventProcess && loadedEventProcess->layout.vtableOffset == 0x673c &&
|
||||
loadedEventProcess->layout.slotIndex == 0x361 && loadedEventProcess->layout.chunkIndex == 0x0a &&
|
||||
loadedEventProcess->layout.savedSource.offset == static_cast<remorse::u16>(sourceEventPointer) &&
|
||||
loadedEventProcess->layout.savedSource.segment == static_cast<remorse::u16>(sourceEventPointer >> 16));
|
||||
const auto loadedEventBytes = eventChunkCache.runtimeMemory().read(
|
||||
loadedEventProcess->layout.chunkBuffer, loadedEventProcess->body.length);
|
||||
assert(loadedEventBytes && *loadedEventBytes == loadedEventProcess->body.bytes &&
|
||||
eventChunkCache.loadedListHead().offset == loadedEventProcess->processAddress.offset &&
|
||||
eventChunkCache.loadedListHead().segment == loadedEventProcess->processAddress.segment &&
|
||||
eventChunkCache.loadedProcessCount() == 1 && eventChunkCache.runtimeMemory().allocationCount() == 2);
|
||||
assert(readDescriptorPointer(eventChunkCache.ownerBuffers().descriptors, 0x0a) ==
|
||||
(static_cast<remorse::u32>(loadedEventProcess->layout.chunkBuffer.offset) |
|
||||
(static_cast<remorse::u32>(loadedEventProcess->layout.chunkBuffer.segment) << 16)));
|
||||
const auto* reusedEventChunk = eventChunkCache.ensureLoaded(0x0a);
|
||||
assert(reusedEventChunk == loadedEventChunk && eventChunkCache.inUseCount(0x0a) == 2);
|
||||
assert(eventChunkCache.release(0x0a) && eventChunkCache.inUseCount(0x0a) == 1);
|
||||
|
|
@ -85,6 +150,9 @@ int main()
|
|||
assert(!eventChunkCache.release(0x0a));
|
||||
assert(eventChunkCache.tryUnload(0x0a));
|
||||
assert(eventChunkCache.ownerBuffers().chunkStates[0x0a] == 0xffff);
|
||||
assert(eventChunkCache.loadedProcessCount() == 0 && eventChunkCache.runtimeMemory().allocationCount() == 0 &&
|
||||
eventChunkCache.loadedListHead().offset == 0 && eventChunkCache.loadedListHead().segment == 0 &&
|
||||
readDescriptorPointer(eventChunkCache.ownerBuffers().descriptors, 0x0a) == sourceEventPointer);
|
||||
const auto* reloadedEventChunk = eventChunkCache.ensureLoaded(0x0a);
|
||||
assert(reloadedEventChunk && reloadedEventChunk->bytes == eventBody->bytes &&
|
||||
eventChunkCache.inUseCount(0x0a) == 1);
|
||||
|
|
@ -95,6 +163,49 @@ int main()
|
|||
assert(eventChunkCache.inUseCount(0x0a) == 29999);
|
||||
assert(eventChunkCache.ensureLoaded(0x0a) == nullptr && eventChunkCache.inUseCount(0x0a) == 29999);
|
||||
assert(eventChunkCache.ensureLoaded(0x20) == nullptr && eventChunkCache.inUseCount(0x20) == 0);
|
||||
|
||||
remorse::UsecodeClass syntheticOwnerClass;
|
||||
syntheticOwnerClass.classId = 0x123;
|
||||
syntheticOwnerClass.name = "SYNTHETIC";
|
||||
syntheticOwnerClass.codeBase = 0xd4;
|
||||
syntheticOwnerClass.bytes.resize(syntheticOwnerClass.codeBase + 2);
|
||||
syntheticOwnerClass.bytes[20] = 1;
|
||||
syntheticOwnerClass.bytes[22] = 1;
|
||||
syntheticOwnerClass.bytes[26] = 1;
|
||||
syntheticOwnerClass.bytes[28] = 2;
|
||||
syntheticOwnerClass.bytes[syntheticOwnerClass.codeBase] = 0xaa;
|
||||
syntheticOwnerClass.bytes[syntheticOwnerClass.codeBase + 1] = 0xbb;
|
||||
remorse::UsecodeSlotChunkCache syntheticChunkCache(std::move(syntheticOwnerClass));
|
||||
const auto syntheticSourceDescriptors = syntheticChunkCache.ownerBuffers().descriptors;
|
||||
const auto* firstSyntheticBody = syntheticChunkCache.ensureLoaded(0);
|
||||
const auto* secondSyntheticBody = syntheticChunkCache.ensureLoaded(1);
|
||||
assert(firstSyntheticBody && firstSyntheticBody->bytes == std::vector<remorse::u8>{0xaa} &&
|
||||
secondSyntheticBody && secondSyntheticBody->bytes == std::vector<remorse::u8>{0xbb});
|
||||
const auto* firstSyntheticProcess = syntheticChunkCache.loadedProcess(0);
|
||||
const auto* secondSyntheticProcess = syntheticChunkCache.loadedProcess(1);
|
||||
assert(firstSyntheticProcess && secondSyntheticProcess &&
|
||||
secondSyntheticProcess->layout.previous.offset == firstSyntheticProcess->processAddress.offset &&
|
||||
secondSyntheticProcess->layout.previous.segment == firstSyntheticProcess->processAddress.segment &&
|
||||
firstSyntheticProcess->layout.next.offset == secondSyntheticProcess->processAddress.offset &&
|
||||
firstSyntheticProcess->layout.next.segment == secondSyntheticProcess->processAddress.segment &&
|
||||
syntheticChunkCache.loadedListHead().offset == secondSyntheticProcess->processAddress.offset &&
|
||||
syntheticChunkCache.loadedListHead().segment == secondSyntheticProcess->processAddress.segment);
|
||||
const auto firstSyntheticSourcePointer = readDescriptorPointer(
|
||||
syntheticSourceDescriptors, 0);
|
||||
const auto secondSyntheticSourcePointer = readDescriptorPointer(
|
||||
syntheticSourceDescriptors, 1);
|
||||
assert(syntheticChunkCache.release(0) && syntheticChunkCache.tryUnload(0));
|
||||
assert(syntheticChunkCache.loadedProcessCount() == 1 &&
|
||||
syntheticChunkCache.loadedListHead().offset == secondSyntheticProcess->processAddress.offset &&
|
||||
syntheticChunkCache.loadedListHead().segment == secondSyntheticProcess->processAddress.segment &&
|
||||
syntheticChunkCache.loadedProcess(1)->layout.previous.offset == 0 &&
|
||||
syntheticChunkCache.loadedProcess(1)->layout.previous.segment == 0 &&
|
||||
readDescriptorPointer(syntheticChunkCache.ownerBuffers().descriptors, 0) == firstSyntheticSourcePointer);
|
||||
assert(syntheticChunkCache.release(1) && syntheticChunkCache.tryUnload(1));
|
||||
assert(syntheticChunkCache.loadedProcessCount() == 0 && syntheticChunkCache.runtimeMemory().allocationCount() == 0 &&
|
||||
syntheticChunkCache.loadedListHead().offset == 0 && syntheticChunkCache.loadedListHead().segment == 0 &&
|
||||
readDescriptorPointer(syntheticChunkCache.ownerBuffers().descriptors, 1) == secondSyntheticSourcePointer);
|
||||
|
||||
assert(!remorse::readUsecodeEventBody(*eventClass, 0x20));
|
||||
assert(!remorse::loadUsecodeClass(*usecode, usecode->entryCount));
|
||||
auto truncated = *eventClass;
|
||||
|
|
@ -132,6 +243,48 @@ int main()
|
|||
assert(stagedItems && !stagedItems->empty());
|
||||
const auto spawnEgg = remorse::findTeleportEgg(*stagedItems, *typeFlags, 0x1e);
|
||||
assert(spawnEgg);
|
||||
remorse::LiveFixedMapItems spawnSearchWorld(*stagedItems);
|
||||
assert(spawnSearchWorld.initializeAvatar());
|
||||
remorse::ProcessKernel teleporterKernel;
|
||||
remorse::u16 teleporterGroups[32]{};
|
||||
for (int reservedSlot = 0; reservedSlot < 3; ++reservedSlot) {
|
||||
teleporterKernel.createProcess(1, teleporterGroups);
|
||||
}
|
||||
auto* teleporterProcess = teleporterKernel.createTeleporterProcess(1, 1, 0x1e, teleporterGroups);
|
||||
const remorse::u16 teleporterProcessId = static_cast<remorse::u16>(
|
||||
teleporterProcess->base.bytes[2] | (static_cast<remorse::u16>(teleporterProcess->base.bytes[3]) << 8));
|
||||
assert(!remorse::TeleporterProcess::run(*teleporterProcess, spawnSearchWorld, 2, *typeFlags));
|
||||
assert(spawnSearchWorld.findItem(1)->data.x == 0 && spawnSearchWorld.findItem(1)->data.y == 0);
|
||||
assert(remorse::TeleporterProcess::run(*teleporterProcess, spawnSearchWorld, 1, *typeFlags));
|
||||
teleporterProcess->base.bytes[0x18] |= 4;
|
||||
teleporterKernel.terminateProcess(teleporterProcessId, teleporterGroups);
|
||||
assert(teleporterKernel.processAt(teleporterProcessId) == nullptr &&
|
||||
teleporterKernel.allocationSizeAt(teleporterProcessId) == 0 && teleporterGroups[1] == 3);
|
||||
assert(spawnEgg->recordIndex < fixedMap->entries.size());
|
||||
const auto& startingEggRecord = fixedMap->entries[spawnEgg->recordIndex];
|
||||
const auto* spawnedAvatar = spawnSearchWorld.findItem(1);
|
||||
assert(spawnEgg->data.x == startingEggRecord.x / 2 &&
|
||||
spawnEgg->data.y == startingEggRecord.y / 2 && spawnEgg->data.z == startingEggRecord.z);
|
||||
assert(spawnedAvatar && spawnedAvatar->itemNumber == 1 &&
|
||||
spawnedAvatar->data.x == spawnEgg->data.x && spawnedAvatar->data.y == spawnEgg->data.y &&
|
||||
spawnedAvatar->data.z == spawnEgg->data.z && spawnedAvatar->data.mapNumber == 1);
|
||||
const auto spawnUseCandidates = remorse::itemGrabSearchCandidates(
|
||||
*spawnedAvatar, spawnSearchWorld.mapItems(), *typeFlags);
|
||||
assert(spawnUseCandidates);
|
||||
unsigned eligibleSpawnUses = 0;
|
||||
for (const auto& candidate : *spawnUseCandidates) {
|
||||
const auto* item = spawnSearchWorld.findItem(candidate.itemNumber);
|
||||
if (!item || item->data.shape >= typeFlags->size()) continue;
|
||||
const auto& shape = (*typeFlags)[item->data.shape];
|
||||
const remorse::UsecodeItemIdentity spawnIdentity{item->itemNumber, item->data.shape,
|
||||
item->data.flags, item->data.quality, shape.family, false};
|
||||
const auto eligible = remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
||||
spawnIdentity, remorse::UsecodeClassBases{0, 0x800, 0x900}, 0x02, 1);
|
||||
if (eligible) {
|
||||
++eligibleSpawnUses;
|
||||
}
|
||||
}
|
||||
assert(eligibleSpawnUses == 0);
|
||||
unsigned eligibleMapUseItems = 0;
|
||||
unsigned keycardUseItems = 0;
|
||||
std::optional<remorse::StagedFixedMapItem> boxItem;
|
||||
|
|
@ -448,9 +601,46 @@ int main()
|
|||
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& boxShape = (*typeFlags)[boxItem->data.shape];
|
||||
const remorse::u8 boxHeight = boxShape.z == 0 && (boxShape.flags & 1) == 0
|
||||
? 1 : static_cast<remorse::u8>(boxShape.z * 8);
|
||||
assert(static_cast<unsigned>(boxItem->data.z) + boxHeight <= 0xff);
|
||||
remorse::StagedFixedMapItem boxSearchOrigin;
|
||||
boxSearchOrigin.itemNumber = 1;
|
||||
boxSearchOrigin.data.shape = 1;
|
||||
boxSearchOrigin.data.x = boxItem->data.x;
|
||||
boxSearchOrigin.data.y = boxItem->data.y;
|
||||
boxSearchOrigin.data.z = static_cast<remorse::u8>(boxItem->data.z + boxHeight);
|
||||
const auto boxCandidates = remorse::itemGrabSearchCandidates(boxSearchOrigin,
|
||||
boxWorld.mapItems(), *typeFlags);
|
||||
assert(boxCandidates);
|
||||
const auto boxCandidate = std::find_if(boxCandidates->begin(), boxCandidates->end(),
|
||||
[boxItem](const auto& candidate) { return candidate.itemNumber == boxItem->itemNumber; });
|
||||
assert(boxCandidate != boxCandidates->end());
|
||||
const auto* selectedBoxItem = boxWorld.findItem(boxCandidate->itemNumber);
|
||||
assert(selectedBoxItem && selectedBoxItem->data.shape < typeFlags->size());
|
||||
const remorse::UsecodeItemIdentity boxIdentity{selectedBoxItem->itemNumber, selectedBoxItem->data.shape,
|
||||
selectedBoxItem->data.flags, selectedBoxItem->data.quality,
|
||||
(*typeFlags)[selectedBoxItem->data.shape].family, false};
|
||||
const auto selectedBoxEvent = remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
||||
boxIdentity, retailBases, 0x02, 1);
|
||||
assert(selectedBoxEvent && selectedBoxEvent->classId == 0x0081 &&
|
||||
selectedBoxEvent->className == "BOX_NS" && selectedBoxEvent->body.bytes == boxUse->bytes);
|
||||
remorse::UsecodeTaskRegistry boxTasks;
|
||||
auto boxContext = remorse::createUsecodeEventContext(boxTasks.runtimeMemory(), *retailOwnerRows,
|
||||
selectedBoxEvent->classId, 1, boxItem->itemNumber, selectedBoxEvent->body.bytes, 2);
|
||||
assert(boxContext && boxContext->context.interpreter.callFrameCount == 1 &&
|
||||
boxContext->ownedRegions.size() == 3 && boxTasks.runtimeMemory().allocationCount() == 5);
|
||||
const std::vector<remorse::u8> boxProcessIdBytes{0x40, 0x03};
|
||||
assert(boxTasks.runtimeMemory().write(boxContext->processIdOutput, boxProcessIdBytes));
|
||||
auto boxTaskValue = remorse::UsecodeProcessTask::create(0x0340, selectedBoxEvent->classId, 1,
|
||||
boxItem->itemNumber, selectedBoxEvent->body.bytes, 2);
|
||||
assert(boxTaskValue && boxTasks.addWithContext(std::move(*boxTaskValue), std::move(*boxContext)));
|
||||
auto* boxTask = boxTasks.find(0x0340);
|
||||
const auto* registeredBoxContext = boxTasks.context(0x0340);
|
||||
assert(boxTask && registeredBoxContext && boxTask->sourceItem == boxItem->itemNumber &&
|
||||
boxTask->interpreter.callFrameCount == 1 && registeredBoxContext->context.processBytes[
|
||||
remorse::usecodeContextSlotIndexOffset] == 0x81);
|
||||
const auto freeClass = remorse::loadUsecodeClass(*usecode, 0x0a0c);
|
||||
const auto freeTimerBody = freeClass ? remorse::readUsecodeEventBody(*freeClass, 0x32) : std::nullopt;
|
||||
auto boxRenderer = remorse::loadMapSceneRenderer(*root, 1);
|
||||
|
|
@ -540,6 +730,12 @@ int main()
|
|||
[](const auto& left, const auto& right) {
|
||||
return left.red == right.red && left.green == right.green && left.blue == right.blue;
|
||||
}));
|
||||
const auto boxProcessIdOutput = boxTasks.runtimeMemory().read(registeredBoxContext->processIdOutput,
|
||||
sizeof(remorse::u16));
|
||||
assert(boxProcessIdOutput && (*boxProcessIdOutput)[0] == 0x40 &&
|
||||
(*boxProcessIdOutput)[1] == 0x03 && boxTasks.runtimeMemory().allocationCount() == 5);
|
||||
assert(boxTasks.remove(0x0340) && boxTasks.size() == 0 && boxTasks.context(0x0340) == nullptr &&
|
||||
boxTasks.runtimeMemory().allocationCount() == 0);
|
||||
assert(!remorse::selectEligibleUsecodeEvent(*usecode, *retailOwnerRows,
|
||||
rechargeItem, retailBases, 0, 9));
|
||||
std::vector<remorse::u8> ownerRows(26);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "remorse/usecode_vm.h"
|
||||
#include "remorse/usecode_archive.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
|
@ -66,6 +67,96 @@ int main()
|
|||
assert(!remorse::writeUsecodeContextFactoryFields(processContext.data(),
|
||||
processContext.size() - 1, factoryFields));
|
||||
|
||||
remorse::UsecodeRuntimeMemory contextMemory;
|
||||
const auto contextOwnerSource = contextMemory.allocate(8, 0x11);
|
||||
const auto contextCodeBytes = contextMemory.allocate(16, 0x22);
|
||||
const auto contextOwnerRows = contextMemory.allocate(13, 0x33);
|
||||
assert(contextOwnerSource && contextCodeBytes && contextOwnerRows &&
|
||||
contextMemory.contains({1, contextOwnerSource->segment}, 1) &&
|
||||
!contextMemory.contains({8, contextOwnerSource->segment}, 1));
|
||||
remorse::UsecodeContextCreateRequest contextRequest;
|
||||
contextRequest.ownerSource = remorse::FarPtr16{1, contextOwnerSource->segment};
|
||||
contextRequest.codeSource = remorse::FarPtr16{2, contextCodeBytes->segment};
|
||||
contextRequest.ownerRows = remorse::FarPtr16{4, contextOwnerRows->segment};
|
||||
contextRequest.itemParameter = std::vector<remorse::u8>{0x34, 0x12};
|
||||
contextRequest.contextParameter = contextParameter;
|
||||
contextRequest.modeFlags = 2;
|
||||
contextRequest.slotIndex = 0x000a;
|
||||
contextRequest.valueAddOffset = 0x0020;
|
||||
const auto contextInstance = remorse::createUsecodeContext(contextMemory, contextRequest);
|
||||
assert(contextInstance && contextMemory.allocationCount() == 5 &&
|
||||
contextInstance->interpreter.callFrameCount == 1 &&
|
||||
contextInstance->interpreter.callFrames[0].offset == 0x000a &&
|
||||
contextInstance->interpreter.callFrames[0].segment == 0x0020);
|
||||
const auto contextMemoryBytes = contextMemory.read(contextInstance->processAddress,
|
||||
remorse::usecodeContextProcessSize);
|
||||
assert(contextMemoryBytes && std::equal(contextMemoryBytes->begin(), contextMemoryBytes->end(),
|
||||
contextInstance->processBytes.begin()));
|
||||
const auto contextCallStack = contextMemory.read(contextInstance->callStackAddress,
|
||||
remorse::usecodeInterpreterCallStackSize);
|
||||
assert(contextCallStack && (*contextCallStack)[0] == 0x0a && (*contextCallStack)[1] == 0 &&
|
||||
(*contextCallStack)[2] == 0x20 && (*contextCallStack)[3] == 0 &&
|
||||
(*contextCallStack)[0x80] == 1 && (*contextCallStack)[0x81] == 0);
|
||||
assert(contextInstance->processBytes[remorse::usecodeContextInterpreterOffset] ==
|
||||
static_cast<remorse::u8>(contextInstance->callStackAddress.offset) &&
|
||||
contextInstance->processBytes[remorse::usecodeContextInterpreterOffset + 2] ==
|
||||
static_cast<remorse::u8>(contextInstance->callStackAddress.segment));
|
||||
assert(contextInstance->processBytes[remorse::usecodeContextCodeSourceOffset] == 2 &&
|
||||
contextInstance->processBytes[remorse::usecodeContextCodeSourceOffset + 1] == 0 &&
|
||||
contextInstance->processBytes[remorse::usecodeContextCodeSourceOffset + 2] ==
|
||||
static_cast<remorse::u8>(contextCodeBytes->segment) &&
|
||||
contextInstance->processBytes[remorse::usecodeContextCodeSourceOffset + 3] == 0);
|
||||
assert(contextInstance->processBytes[remorse::usecodeContextSelfPointerOffset] ==
|
||||
static_cast<remorse::u8>(contextInstance->processAddress.offset) &&
|
||||
contextInstance->processBytes[remorse::usecodeContextSelfPointerOffset + 2] ==
|
||||
static_cast<remorse::u8>(contextInstance->processAddress.segment));
|
||||
assert(contextInstance->processBytes[remorse::usecodeContextOwnerRowsOffset] == 4 &&
|
||||
contextInstance->processBytes[remorse::usecodeContextOwnerRowsOffset + 1] == 0 &&
|
||||
contextInstance->processBytes[remorse::usecodeContextOwnerRowsOffset + 2] ==
|
||||
static_cast<remorse::u8>(contextOwnerRows->segment) &&
|
||||
contextInstance->processBytes[remorse::usecodeContextOwnerRowsOffset + 3] == 0);
|
||||
assert(contextInstance->processBytes[remorse::usecodeContextOwnerSourceOffset] == 1 &&
|
||||
contextInstance->processBytes[remorse::usecodeContextOwnerSourceOffset + 2] ==
|
||||
static_cast<remorse::u8>(contextOwnerSource->segment) &&
|
||||
contextInstance->processBytes[remorse::usecodeContextParameterOffset] == 190 &&
|
||||
contextInstance->processBytes[remorse::usecodeContextModeFlagsOffset] == 2 &&
|
||||
contextInstance->processBytes[remorse::usecodeContextActiveFlagOffset] == 0);
|
||||
const std::size_t savedPointerLocation = remorse::usecodeContextInterpreterOffset +
|
||||
contextInstance->interpreter.stackTop;
|
||||
assert(contextInstance->processBytes[savedPointerLocation] == 0x00 &&
|
||||
contextInstance->processBytes[savedPointerLocation + 1] == 0x01 &&
|
||||
contextInstance->processBytes[savedPointerLocation + 2] ==
|
||||
static_cast<remorse::u8>(contextInstance->processAddress.segment) &&
|
||||
contextInstance->processBytes[savedPointerLocation + 3] == 0 &&
|
||||
contextInstance->processBytes[0x100] == 0x34 && contextInstance->processBytes[0x101] == 0x12 &&
|
||||
contextInstance->processBytes[0xfc] == 0xa1 && contextInstance->processBytes[0xfd] == 0xb2 &&
|
||||
contextInstance->processBytes[0xfe] == 0xc3 && contextInstance->processBytes[0xff] == 0xd4);
|
||||
assert(contextInstance->processBytes[remorse::usecodeContextStackPointerOffset] == 0xf8 &&
|
||||
contextInstance->processBytes[remorse::usecodeContextStackPointerOffset + 2] ==
|
||||
static_cast<remorse::u8>(contextInstance->processAddress.segment) &&
|
||||
contextInstance->processBytes[remorse::usecodeContextStackBaseOffset] == 0x02 &&
|
||||
contextInstance->processBytes[remorse::usecodeContextStackBaseOffset + 1] == 0x01 &&
|
||||
contextInstance->processBytes[remorse::usecodeContextStackLimitOffset] == 199);
|
||||
assert(remorse::destroyUsecodeContext(contextMemory, *contextInstance) &&
|
||||
contextMemory.allocationCount() == 3);
|
||||
|
||||
remorse::UsecodeContextCreateRequest emptyContextRequest = contextRequest;
|
||||
emptyContextRequest.contextParameter = std::vector<remorse::u8>{};
|
||||
const auto emptyContextInstance = remorse::createUsecodeContext(contextMemory, emptyContextRequest);
|
||||
assert(emptyContextInstance && emptyContextInstance->interpreter.contextParameterOffset == 194 &&
|
||||
emptyContextInstance->processBytes[remorse::usecodeContextParameterOffset] == 194);
|
||||
assert(remorse::destroyUsecodeContext(contextMemory, *emptyContextInstance));
|
||||
remorse::UsecodeContextCreateRequest invalidContextRequest = contextRequest;
|
||||
invalidContextRequest.itemParameter = std::vector<remorse::u8>{0x34};
|
||||
assert(!remorse::createUsecodeContext(contextMemory, invalidContextRequest) &&
|
||||
contextMemory.allocationCount() == 3);
|
||||
invalidContextRequest = contextRequest;
|
||||
invalidContextRequest.codeSource = remorse::FarPtr16{0xffff, 0xfffe};
|
||||
assert(!remorse::createUsecodeContext(contextMemory, invalidContextRequest) &&
|
||||
contextMemory.allocationCount() == 3);
|
||||
assert(contextMemory.release(*contextOwnerSource) && contextMemory.release(*contextCodeBytes) &&
|
||||
contextMemory.release(*contextOwnerRows) && contextMemory.allocationCount() == 0);
|
||||
|
||||
std::vector<remorse::u8> code(remorse::usecodeEventBytecodeOffset, 0);
|
||||
code.insert(code.end(), {
|
||||
0x0a, 0x07,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue