Refactor USECODE tooling documentation and enhance map-viewer decompiler

- Updated the USECODE Tool Improvement Plan to focus on the map-viewer decompiler/editor, emphasizing its role as the primary development target for USECODE editing and recompilation.
- Revised the tooling comparison document to include the newer map-viewer decompiler, clarifying its purpose and distinguishing it from the Python parser.
- Introduced a new ClearAvatarInStasisProcessEntry structure in the entity dispatch entry header, along with related process creation and execution logic in the ProcessKernel.
- Implemented the setItemShape method in LiveFixedMapItems to allow dynamic shape updates for items.
- Enhanced the teleporter process to handle avatar stasis and integrated it into the game main loop, ensuring proper process management and state handling.
- Added tests for the new functionality, including shape setting and teleporter process behavior, to ensure correctness and stability.
This commit is contained in:
Maddo 2026-10-06 00:01:20 +02:00
commit 6d4972933f
17 changed files with 1868 additions and 114 deletions

View file

@ -195,6 +195,44 @@ TeleporterProcessEntry* ProcessKernel::createTeleporterProcess(u8 activeGroup, u
return result;
}
TeleporterProcessEntry* ProcessKernel::createTeleporterProcess(u8 activeGroup, u16 mapNumber,
u16 teleportId, u16 (&groupCounts)[32], bool doStasis, bool avatarTeleporting,
bool& avatarInStasis)
{
auto* teleporter = createTeleporterProcess(activeGroup, mapNumber, teleportId, groupCounts);
if (doStasis && !avatarTeleporting) {
auto* clearStasis = createClearAvatarInStasisProcess(activeGroup, avatarInStasis, groupCounts);
processWaits_.emplace_back(readLe16(teleporter->base, 0x02),
readLe16(clearStasis->base, 0x02));
}
return teleporter;
}
ClearAvatarInStasisProcessEntry* ProcessKernel::createClearAvatarInStasisProcess(u8 activeGroup,
bool& avatarInStasis, 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);
}
ownedClearAvatarInStasisProcesses_.reserve(ownedClearAvatarInStasisProcesses_.size() + 1);
auto process = std::make_unique<ClearAvatarInStasisProcessEntry>();
EntityDispatchEntry::initBase(&process->base, activeGroup, groupCounts);
writeLe16(process->base, 0x00, ClearAvatarInStasisProcess::vtableOffset);
process->extra[0] = avatarInStasis ? 1 : 0;
avatarInStasis = true;
setLastAllocationSize(sizeof(ClearAvatarInStasisProcessEntry));
const u16 id = addProcess(process->base);
processNames_[id] = "ClearAvatarInStasis";
ClearAvatarInStasisProcessEntry* result = process.get();
ownedClearAvatarInStasisProcesses_.push_back(std::move(process));
return result;
}
bool TeleporterProcess::run(const TeleporterProcessEntry& process, LiveFixedMapItems& items,
u16 currentMapNumber, const std::vector<ShapeTypeInfo>& shapes)
{
@ -211,6 +249,27 @@ bool TeleporterProcess::run(const TeleporterProcessEntry& process, LiveFixedMapI
return destination && items.teleportAvatar(*destination, static_cast<u8>(targetMap));
}
bool ProcessKernel::runClearAvatarInStasisProcess(u16 processId, bool& avatarInStasis,
u16 (&groupCounts)[32])
{
EntityDispatchEntryBase* entry = processAt(processId);
const auto ownedProcess = std::find_if(ownedClearAvatarInStasisProcesses_.begin(),
ownedClearAvatarInStasisProcesses_.end(), [entry](const auto& process) {
return &process->base == entry;
});
if (entry == nullptr || readLe16(*entry, 0x00) != ClearAvatarInStasisProcess::vtableOffset ||
ownedProcess == ownedClearAvatarInStasisProcesses_.end()) {
return false;
}
const auto* process = reinterpret_cast<const ClearAvatarInStasisProcessEntry*>(entry);
if (process->extra[0] == 0) {
avatarInStasis = false;
}
entry->bytes[0x18] |= 0x04;
terminateProcess(processId, groupCounts);
return true;
}
EntityVmContextProcessEntry* ProcessKernel::createUsecodeProcess(u8 activeGroup,
u16 (&groupCounts)[32])
{
@ -690,6 +749,9 @@ void ProcessKernel::clearProcessSlot(u16 id)
if (id >= processes_.size()) {
throw std::out_of_range("process ID outside table");
}
processWaits_.erase(std::remove_if(processWaits_.begin(), processWaits_.end(), [id](const auto& wait) {
return wait.first == id || wait.second == id;
}), processWaits_.end());
processes_[id] = nullptr;
allocationSizes_[id] = 0;
processNames_[id] = nullptr;
@ -723,6 +785,13 @@ void ProcessKernel::releaseOwnedProcess(u16 id)
ownedTeleporterProcesses_.erase(teleporter);
return;
}
const auto clearAvatarInStasis = std::find_if(ownedClearAvatarInStasisProcesses_.begin(),
ownedClearAvatarInStasisProcesses_.end(),
[entry](const auto& process) { return &process->base == entry; });
if (clearAvatarInStasis != ownedClearAvatarInStasisProcesses_.end()) {
ownedClearAvatarInStasisProcesses_.erase(clearAvatarInStasis);
return;
}
const auto usecode = std::find_if(ownedUsecodeProcesses_.begin(), ownedUsecodeProcesses_.end(),
[entry](const auto& process) { return &process->base == entry; });
if (usecode != ownedUsecodeProcesses_.end()) {
@ -771,6 +840,9 @@ void ProcessKernel::terminateProcess(u16 id, u16 (&groupCounts)[32])
const bool ownedTeleporterProcess = std::any_of(ownedTeleporterProcesses_.begin(),
ownedTeleporterProcesses_.end(),
[entry](const auto& process) { return &process->base == entry; });
const bool ownedClearAvatarInStasisProcess = std::any_of(ownedClearAvatarInStasisProcesses_.begin(),
ownedClearAvatarInStasisProcesses_.end(),
[entry](const auto& process) { return &process->base == entry; });
if (itemNumber != 0 && (stand == ownedStandProcesses_.end() && !ownedItemProcess &&
!ownedGravityProcess && !ownedAnimationProcess && !ownedUsecodeProcess || id < 3 ||
readLe16(*entry, 0x24) != 0 || readLe16(*entry, 0x26) != 0 ||
@ -778,7 +850,7 @@ void ProcessKernel::terminateProcess(u16 id, u16 (&groupCounts)[32])
throw std::logic_error("item process child cleanup is not ported");
}
if (stand == ownedStandProcesses_.end() && !ownedGravityProcess && !ownedAnimationProcess &&
!ownedUsecodeProcess && !ownedTeleporterProcess &&
!ownedUsecodeProcess && !ownedTeleporterProcess && !ownedClearAvatarInStasisProcess &&
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");
@ -923,7 +995,21 @@ void ProcessKernel::scanRunnableProcesses(u8 activeGroup, void (*run)(EntityDisp
try {
for (std::size_t id = 0; id < processes_.size(); ++id) {
EntityDispatchEntryBase* entry = processes_[id];
if (entry != nullptr && (readLe16(*entry, 0x16) & 0x08) == 0) {
bool waitingForProcess = false;
if (entry != nullptr) {
const u16 processId = readLe16(*entry, 0x02);
const auto wait = std::find_if(processWaits_.begin(), processWaits_.end(),
[processId](const auto& candidate) { return candidate.first == processId; });
if (wait != processWaits_.end()) {
const EntityDispatchEntryBase* target = processAt(wait->second);
waitingForProcess = target != nullptr &&
(readLe16(*target, 0x16) & 0x08) == 0;
if (!waitingForProcess) {
processWaits_.erase(wait);
}
}
}
if (entry != nullptr && !waitingForProcess && (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);

View file

@ -854,6 +854,18 @@ bool LiveFixedMapItems::isGlobHatched(u16 eggItemNumber) const
return eggItemNumber < hatchedGlobIds_.size() && hatchedGlobIds_[eggItemNumber];
}
bool LiveFixedMapItems::setItemShape(u16 itemNumber, u16 shape)
{
const auto item = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& candidate) {
return candidate.itemNumber == itemNumber;
});
if (item == items_.end()) {
return false;
}
item->data.shape = shape;
return true;
}
bool LiveFixedMapItems::setItemFrame(u16 itemNumber, u8 frame)
{
const auto item = std::find_if(items_.begin(), items_.end(), [itemNumber](const auto& candidate) {

View file

@ -299,8 +299,16 @@ void runUsecodeTasks(AppState& state)
const auto* item = app.liveItems ? app.liveItems->findItem(*itemNumber) : nullptr;
return item ? std::optional<remorse::u16>{item->data.mapNumber} : std::nullopt;
}
if (ordinal == 0x24 && argumentBytes == 6) {
const remorse::u16 shape = static_cast<remorse::u16>(arguments[4] |
(static_cast<remorse::u16>(arguments[5]) << 8));
if (!app.liveItems || !app.liveItems->setItemShape(*itemNumber, shape)) {
return std::nullopt;
}
app.usecodeWorldMutationPending = true;
return remorse::u16{0};
}
if (ordinal == 0x59 && argumentBytes == 6) {
std::lock_guard<std::mutex> lock(app.sceneMutex);
const remorse::u8 frame = static_cast<remorse::u8>(arguments[4] |
(static_cast<remorse::u16>(arguments[5]) << 8));
if (!app.liveItems || !app.liveItems->setItemFrame(*itemNumber, frame)) {
@ -526,15 +534,51 @@ void loadSceneAsync(HWND window, AppState& state)
for (int reservedSlot = 0; reservedSlot < 3; ++reservedSlot) {
teleporterKernel.createProcess(1, teleporterGroupCounts);
}
bool avatarInStasis = false;
auto* teleporter = teleporterKernel.createTeleporterProcess(1,
static_cast<remorse::u16>(state.mapIndex), 0x1e, teleporterGroupCounts);
static_cast<remorse::u16>(state.mapIndex), 0x1e, teleporterGroupCounts,
true, false, avatarInStasis);
const remorse::u16 processId = static_cast<remorse::u16>(teleporter->base.bytes[2] |
(static_cast<remorse::u16>(teleporter->base.bytes[3]) << 8));
const bool teleported = remorse::TeleporterProcess::run(*teleporter, liveItems,
static_cast<remorse::u16>(state.mapIndex), *shapeTypes);
teleporter->base.bytes[0x18] |= 4;
teleporterKernel.terminateProcess(processId, teleporterGroupCounts);
if (!teleported) {
struct StartupContext {
remorse::ProcessKernel& kernel;
remorse::LiveFixedMapItems& items;
const std::vector<remorse::ShapeTypeInfo>& shapes;
remorse::u16 (&groupCounts)[32];
bool& avatarInStasis;
remorse::u16 mapNumber;
bool teleported = false;
bool valid = true;
} startup{teleporterKernel, liveItems, *shapeTypes, teleporterGroupCounts,
avatarInStasis, static_cast<remorse::u16>(state.mapIndex)};
const auto runStartupProcess = [](remorse::EntityDispatchEntryBase& entry,
void* opaque) {
auto& context = *static_cast<StartupContext*>(opaque);
const remorse::u16 processId = static_cast<remorse::u16>(entry.bytes[2] |
(static_cast<remorse::u16>(entry.bytes[3]) << 8));
if (processId < 3) return;
const remorse::u16 vtable = static_cast<remorse::u16>(entry.bytes[0] |
(static_cast<remorse::u16>(entry.bytes[1]) << 8));
if (vtable == remorse::ClearAvatarInStasisProcess::vtableOffset) {
context.valid = context.kernel.runClearAvatarInStasisProcess(processId,
context.avatarInStasis, context.groupCounts);
} else if (vtable == remorse::TeleporterProcess::vtableOffset) {
const auto& teleporterProcess =
*reinterpret_cast<remorse::TeleporterProcessEntry*>(&entry);
context.teleported = remorse::TeleporterProcess::run(teleporterProcess,
context.items, context.mapNumber, context.shapes);
entry.bytes[0x18] |= 4;
context.kernel.terminateProcess(processId, context.groupCounts);
} else {
context.valid = false;
}
};
for (unsigned pass = 0; pass < 2 && teleporterKernel.processAt(processId) != nullptr;
++pass) {
teleporterKernel.scanRunnableProcesses(1, runStartupProcess, &startup);
}
if (!startup.valid || !startup.teleported || avatarInStasis ||
teleporterKernel.processAt(processId) != nullptr) {
setLoadFailure(state, L"The teleporter process could not resolve its map and egg destination.");
PostMessageW(window, sceneReadyMessage, 0, 0);
return;
@ -968,6 +1012,7 @@ void updatePlayer(HWND window, AppState& state)
runUsecodeTasks(state);
const auto movement = state.controllerMovement.request();
state.diagnosticKernel.deliverSystemTimerEvent();
bool gravityMoved = false;
if (state.diagnostics) {
const bool wasSleeping = (state.diagnosticStand->base.bytes[0x19] & 0x10) != 0;
const remorse::u32 previousSleepStart = standSleepStart(*state.diagnosticStand);
@ -1016,8 +1061,13 @@ void updatePlayer(HWND window, AppState& state)
if (wasSleeping && standSleepStart(*state.diagnosticStand) != previousSleepStart) {
++state.standWakeCount;
}
} else if (state.liveItems) {
gravityMoved = state.diagnosticKernel.scanResolvedGravity(
state.diagnosticKernel.activeGroup(), *state.liveItems, state.shapeTypes,
remorse::calculateFastAreaRange(avatarWorld(state)), 1,
state.diagnosticKernel.systemTimerTick(), state.diagnosticGroupCounts) != 0;
}
changed = changed || state.diagnostics;
changed = changed || state.diagnostics || gravityMoved;
if (movement.animation == 0) {
if (state.animationProcessId != 0) {
auto* process = state.diagnosticKernel.processAt(state.animationProcessId);
@ -1028,6 +1078,13 @@ void updatePlayer(HWND window, AppState& state)
changed = changed || state.playerAnimation != 0 || state.playerFrame != 0;
state.playerAnimation = 0;
state.playerFrame = 0;
const bool usecodeMutation = state.usecodeWorldMutationPending;
state.usecodeWorldMutationPending = false;
changed = changed || usecodeMutation;
if (state.renderThread.joinable() && (gravityMoved || usecodeMutation)) {
state.renderRequested = true;
state.renderWake.notify_one();
}
continue;
}
@ -1078,7 +1135,7 @@ void updatePlayer(HWND window, AppState& state)
*state.liveItems, *state.renderer, remorse::calculateFastAreaRange(avatarWorld(state)));
const bool usecodeMutation = state.usecodeWorldMutationPending;
state.usecodeWorldMutationPending = false;
if (state.renderThread.joinable() && (moved || hatched || usecodeMutation)) {
if (state.renderThread.joinable() && (moved || hatched || usecodeMutation || gravityMoved)) {
state.renderRequested = true;
state.renderWake.notify_one();
}