Enhance teleporter and map rendering tests

- Added initialization of startup processes in flex_archive_tests to ensure proper setup before teleporter operations.
- Updated assertions to verify the correct allocation and state of processes during teleporter execution.
- Introduced new tests in map_renderer_tests to validate shape rendering, including isolated shape rendering and player sprite integration.
- Enhanced map_sorter_tests with additional checks for occlusion handling and boundary conditions in render order resolution.
This commit is contained in:
Maddo 2026-10-06 21:07:35 +02:00
commit e00f9f3186
16 changed files with 954 additions and 398 deletions

View file

@ -18,10 +18,12 @@
#include "remorse/usecode_vm.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <condition_variable>
#include <cstdint>
#include <cstring>
#include <cwchar>
#include <iomanip>
#include <mutex>
#include <optional>
@ -37,15 +39,24 @@ constexpr UINT timerId = 1;
constexpr UINT sceneReadyMessage = WM_APP + 1;
constexpr remorse::u32 backingWidth = 2560;
constexpr remorse::u32 backingHeight = 1440;
constexpr remorse::u32 playerPatchWidth = 512;
constexpr remorse::u32 playerPatchHeight = 384;
constexpr int initialClientWidth = 1280;
constexpr int initialClientHeight = 760;
constexpr double simulationTickSeconds = 0.025;
constexpr remorse::s32 diagnosticItemRange = 48;
struct WarpStart {
remorse::u16 x = 0;
remorse::u16 y = 0;
remorse::u8 z = 0;
};
struct AppState {
remorse::GameDataRoot root;
remorse::u32 mapIndex = 1;
bool diagnostics = false;
std::optional<WarpStart> warpStart;
remorse::ProcessKernel diagnosticKernel;
remorse::u16 diagnosticGroupCounts[32]{};
remorse::UsecodeTaskRegistry usecodeTasks;
@ -66,11 +77,15 @@ struct AppState {
bool diagnosticItemCompleted = false;
remorse::u32 standWakeCount = 0;
remorse::u32 diagnosticKeyEvents = 0;
std::mutex kernelMutex;
std::mutex sceneMutex;
std::condition_variable renderWake;
bool renderRequested = false;
bool renderFullRequested = false;
std::uint64_t renderGeneration = 0;
bool renderStop = false;
std::optional<remorse::RenderedMapImage> scene;
std::optional<remorse::RenderedMapImage> playerPatch;
std::optional<remorse::MapSceneRenderer> renderer;
std::optional<remorse::AnimationClip> standAnimation;
std::optional<remorse::AnimationClip> walkAnimation;
@ -145,6 +160,50 @@ remorse::WorldPoint avatarWorld(const AppState& state)
: state.spawn;
}
remorse::MapRenderOptions playerPatchOptions(remorse::u32 mapIndex,
const remorse::WorldPoint& center)
{
remorse::MapRenderOptions options;
options.mapIndex = mapIndex;
options.maxWidth = playerPatchWidth;
options.maxHeight = playerPatchHeight;
options.includeRoofs = true;
options.viewCenter = center;
const remorse::s32 radius = static_cast<remorse::s32>(
playerPatchWidth + playerPatchHeight * 2 + 1280);
options.worldRect = remorse::WorldRect{
center.x - radius, center.y - radius, center.x + radius, center.y + radius};
return options;
}
std::optional<remorse::MapRenderSprite> playerRenderSprite(const AppState& state)
{
const remorse::AnimationClip* clip = nullptr;
if (state.playerAnimation == 1 && state.walkAnimation) {
clip = &*state.walkAnimation;
} else if (state.playerAnimation == 3 && state.runAnimation) {
clip = &*state.runAnimation;
} else if (state.standAnimation) {
clip = &*state.standAnimation;
}
if (!clip) {
return std::nullopt;
}
const remorse::u8 frameIndex = state.playerAnimation == 0 ? 0 : state.playerFrame;
const auto metadata = remorse::getAnimationFrame(*clip, state.playerDirection, frameIndex);
if (!metadata) {
return std::nullopt;
}
const remorse::u16 shapeFrame = remorse::getAnimationShapeFrame(*metadata);
if (shapeFrame >= state.playerFrames.size() || !state.playerFrames[shapeFrame]) {
return std::nullopt;
}
return remorse::MapRenderSprite{avatarWorld(state), 1, static_cast<remorse::u8>(shapeFrame),
remorse::isAnimationFrameFlipped(*metadata), &*state.playerFrames[shapeFrame],
&state.playerPalette};
}
bool hatchGlobsInFastArea(remorse::LiveFixedMapItems& items,
const remorse::MapSceneRenderer& renderer, const remorse::FastAreaRange& range);
@ -507,99 +566,98 @@ void loadSceneAsync(HWND window, AppState& state)
maxY = std::max(maxY, static_cast<remorse::s32>(entry.y));
}
const auto startingEgg = remorse::findTeleportEgg(*stagedItems, *shapeTypes, 0x1e);
if (state.mapIndex == 1 && !startingEgg) {
const auto startingEgg = state.warpStart
? std::optional<remorse::StagedFixedMapItem>{}
: remorse::findTeleportEgg(*stagedItems, *shapeTypes, 0x1e);
if (!state.warpStart && state.mapIndex == 1 && !startingEgg) {
setLoadFailure(state, L"The original map 1 starting teleporter could not be found uniquely.");
PostMessageW(window, sceneReadyMessage, 0, 0);
return;
}
if (state.mapIndex != 1 && !state.diagnostics) {
if (!state.warpStart && state.mapIndex != 1 && !state.diagnostics) {
setLoadFailure(state, L"Only map 1 has a source-backed start. Use --diagnostics for other maps.");
PostMessageW(window, sceneReadyMessage, 0, 0);
return;
}
const remorse::WorldPoint spawn = startingEgg
? remorse::WorldPoint{startingEgg->data.x * 2, startingEgg->data.y * 2, startingEgg->data.z}
: remorse::WorldPoint{(minX + maxX) / 2, (minY + maxY) / 2, 0};
remorse::WorldPoint spawn = state.warpStart
? remorse::WorldPoint{static_cast<remorse::s32>(state.warpStart->x) * 2,
static_cast<remorse::s32>(state.warpStart->y) * 2, state.warpStart->z}
: startingEgg
? remorse::WorldPoint{startingEgg->data.x * 2, startingEgg->data.y * 2,
startingEgg->data.z}
: remorse::WorldPoint{(minX + maxX) / 2, (minY + maxY) / 2, 0};
remorse::MapRenderOptions options;
options.mapIndex = state.mapIndex;
options.maxWidth = backingWidth;
options.maxHeight = backingHeight;
options.includeRoofs = true;
options.viewCenter = spawn;
remorse::LiveFixedMapItems liveItems(std::move(*stagedItems), std::move(containedItems));
liveItems.initializeAvatar();
if (startingEgg) {
remorse::ProcessKernel teleporterKernel;
remorse::u16 teleporterGroupCounts[32]{};
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,
true, false, avatarInStasis);
const remorse::u16 processId = static_cast<remorse::u16>(teleporter->base.bytes[2] |
(static_cast<remorse::u16>(teleporter->base.bytes[3]) << 8));
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) {
std::lock_guard<std::mutex> kernelLock(state.kernelMutex);
if (!remorse::runFreshGameTeleporterStartup(state.diagnosticKernel, liveItems,
static_cast<remorse::u16>(state.mapIndex), 0x1e, *shapeTypes,
state.diagnosticGroupCounts)) {
setLoadFailure(state, L"The teleporter process could not resolve its map and egg destination.");
PostMessageW(window, sceneReadyMessage, 0, 0);
return;
}
const auto* avatar = liveItems.findItem(1);
const auto stagingTeleporter = avatar ? remorse::findNearbyActiveTeleportEgg(
*avatar, liveItems.mapItems(), *shapeTypes, static_cast<remorse::u8>(state.mapIndex))
: std::nullopt;
if (!stagingTeleporter || !remorse::runFreshGameTeleporterStartup(
state.diagnosticKernel, liveItems, stagingTeleporter->data.mapNumber,
static_cast<remorse::u8>(stagingTeleporter->data.quality & 0xff), *shapeTypes,
state.diagnosticGroupCounts)) {
setLoadFailure(state, L"The staging teleporter could not resolve its level destination.");
PostMessageW(window, sceneReadyMessage, 0, 0);
return;
}
avatar = liveItems.findItem(1);
spawn = remorse::WorldPoint{avatar->data.x * 2, avatar->data.y * 2, avatar->data.z};
options.viewCenter = spawn;
} else {
remorse::StagedFixedMapItem diagnosticStart;
diagnosticStart.data.x = static_cast<remorse::u16>(spawn.x / 2);
diagnosticStart.data.y = static_cast<remorse::u16>(spawn.y / 2);
diagnosticStart.data.z = static_cast<remorse::u8>(spawn.z);
liveItems.teleportAvatar(diagnosticStart, static_cast<remorse::u8>(state.mapIndex));
remorse::StagedFixedMapItem directStart;
directStart.data.x = static_cast<remorse::u16>(spawn.x / 2);
directStart.data.y = static_cast<remorse::u16>(spawn.y / 2);
directStart.data.z = static_cast<remorse::u8>(spawn.z);
liveItems.teleportAvatar(directStart, static_cast<remorse::u8>(state.mapIndex));
}
auto renderer = remorse::loadMapSceneRenderer(state.root, state.mapIndex);
if (renderer) {
hatchGlobsInFastArea(liveItems, *renderer, remorse::calculateFastAreaRange(spawn));
}
const auto initialMetadata = remorse::getAnimationFrame(*standAnimation, 14, 0);
if (!initialMetadata) {
setLoadFailure(state, L"The player stand frame could not be resolved.");
PostMessageW(window, sceneReadyMessage, 0, 0);
return;
}
const remorse::u16 initialShapeFrame = remorse::getAnimationShapeFrame(*initialMetadata);
if (initialShapeFrame >= playerFrames.size() || !playerFrames[initialShapeFrame]) {
setLoadFailure(state, L"The player stand frame could not be decoded.");
PostMessageW(window, sceneReadyMessage, 0, 0);
return;
}
const remorse::MapRenderSprite initialPlayer{spawn, 1,
static_cast<remorse::u8>(initialShapeFrame),
remorse::isAnimationFrameFlipped(*initialMetadata), &*playerFrames[initialShapeFrame],
&*palette};
auto scene = renderer ? renderer->render(options, &liveItems) : std::nullopt;
if (!scene) {
setLoadFailure(state, L"Map graphics or palette data could not be loaded.");
PostMessageW(window, sceneReadyMessage, 0, 0);
return;
}
auto playerPatch = renderer ? renderer->render(
playerPatchOptions(state.mapIndex, spawn), &liveItems, &initialPlayer) : std::nullopt;
if (!playerPatch) {
setLoadFailure(state, L"The initial player render patch could not be built.");
PostMessageW(window, sceneReadyMessage, 0, 0);
return;
}
std::vector<std::uint8_t> dibPixels(scene->pixels.size() * 3);
for (std::size_t index = 0; index < scene->pixels.size(); ++index) {
@ -624,6 +682,7 @@ void loadSceneAsync(HWND window, AppState& state)
state.maxWorldX = maxX;
state.maxWorldY = maxY;
state.scene = std::move(scene);
state.playerPatch = std::move(playerPatch);
state.renderer = std::move(renderer);
state.dibPixels = std::move(dibPixels);
state.standAnimation = *standAnimation;
@ -672,7 +731,10 @@ void startLiveRenderer(HWND window, AppState& state)
state.renderThread = std::thread([window, &state]() {
for (;;) {
std::optional<remorse::LiveFixedMapItems> snapshot;
std::optional<remorse::MapRenderSprite> playerSprite;
remorse::WorldPoint center{};
bool fullRender = false;
std::uint64_t generation = 0;
{
std::unique_lock<std::mutex> lock(state.sceneMutex);
state.renderWake.wait(lock, [&state]() { return state.renderStop || state.renderRequested; });
@ -680,41 +742,70 @@ void startLiveRenderer(HWND window, AppState& state)
return;
}
state.renderRequested = false;
fullRender = state.renderFullRequested;
state.renderFullRequested = false;
generation = state.renderGeneration;
snapshot.emplace(*state.liveItems);
center = avatarWorld(state);
playerSprite = playerRenderSprite(state);
}
remorse::MapRenderOptions options;
options.mapIndex = state.mapIndex;
options.maxWidth = backingWidth;
options.maxHeight = backingHeight;
options.viewCenter = center;
const remorse::s32 radius = static_cast<remorse::s32>(
backingWidth + backingHeight * 2 + 512);
options.worldRect = remorse::WorldRect{
center.x - radius, center.y - radius, center.x + radius, center.y + radius};
auto scene = state.renderer->render(options, &*snapshot);
if (!scene) {
std::optional<remorse::RenderedMapImage> scene;
std::vector<std::uint8_t> pixels;
if (fullRender) {
remorse::MapRenderOptions options;
options.mapIndex = state.mapIndex;
options.maxWidth = backingWidth;
options.maxHeight = backingHeight;
options.includeRoofs = true;
options.viewCenter = center;
const remorse::s32 radius = static_cast<remorse::s32>(
backingWidth + backingHeight * 2 + 512);
options.worldRect = remorse::WorldRect{
center.x - radius, center.y - radius, center.x + radius, center.y + radius};
scene = state.renderer->render(options, &*snapshot);
if (!scene) {
continue;
}
pixels.resize(scene->pixels.size() * 3);
for (std::size_t index = 0; index < scene->pixels.size(); ++index) {
pixels[index * 3] = scene->pixels[index].blue;
pixels[index * 3 + 1] = scene->pixels[index].green;
pixels[index * 3 + 2] = scene->pixels[index].red;
}
}
auto playerPatch = state.renderer->render(playerPatchOptions(state.mapIndex, center),
&*snapshot, playerSprite ? &*playerSprite : nullptr);
if (!playerPatch) {
continue;
}
std::vector<std::uint8_t> pixels(scene->pixels.size() * 3);
for (std::size_t index = 0; index < scene->pixels.size(); ++index) {
pixels[index * 3] = scene->pixels[index].blue;
pixels[index * 3 + 1] = scene->pixels[index].green;
pixels[index * 3 + 2] = scene->pixels[index].red;
}
{
std::lock_guard<std::mutex> lock(state.sceneMutex);
if (state.renderStop) {
return;
}
state.scene = std::move(scene);
state.dibPixels = std::move(pixels);
if (state.renderGeneration != generation && state.renderFullRequested) {
state.renderRequested = true;
continue;
}
if (fullRender) {
state.scene = std::move(scene);
state.dibPixels = std::move(pixels);
}
state.playerPatch = std::move(playerPatch);
}
PostMessageW(window, sceneReadyMessage, 0, 0);
}
});
}
void requestLiveRender(AppState& state, bool fullRender)
{
state.renderRequested = true;
state.renderFullRequested = state.renderFullRequested || fullRender;
++state.renderGeneration;
state.renderWake.notify_one();
}
void drawCenteredText(HDC device, const RECT& bounds, const wchar_t* text, COLORREF color, HFONT font)
{
const int saved = SaveDC(device);
@ -725,76 +816,6 @@ void drawCenteredText(HDC device, const RECT& bounds, const wchar_t* text, COLOR
RestoreDC(device, saved);
}
void compositePlayer(HDC device, AppState& state, int viewWidth, int viewHeight,
int sourceLeft, int sourceTop, std::size_t viewStride)
{
const remorse::AnimationClip* clip = nullptr;
if (state.playerAnimation == 1 && state.walkAnimation) {
clip = &*state.walkAnimation;
} else if (state.playerAnimation == 3 && state.runAnimation) {
clip = &*state.runAnimation;
} else if (state.standAnimation) {
clip = &*state.standAnimation;
}
if (!clip || state.playerFrames.empty()) {
return;
}
const remorse::u8 frameIndex = state.playerAnimation == 0 ? 0 : state.playerFrame;
const auto metadata = remorse::getAnimationFrame(*clip, state.playerDirection, frameIndex);
if (!metadata) {
return;
}
const remorse::u16 shapeFrame = remorse::getAnimationShapeFrame(*metadata);
if (shapeFrame >= state.playerFrames.size() || !state.playerFrames[shapeFrame]) {
return;
}
const remorse::DecodedShapeFrame& sprite = *state.playerFrames[shapeFrame];
const auto avatarScreen = remorse::projectToScreen(avatarWorld(state));
const int actorX = avatarScreen.x - state.scene->sourceLeft - sourceLeft;
const int actorY = avatarScreen.y - state.scene->sourceTop - sourceTop;
const int spriteLeft = actorX + sprite.frame.xOffset;
const int spriteTop = actorY + sprite.frame.yOffset;
const bool flipped = remorse::isAnimationFrameFlipped(*metadata);
for (remorse::u32 y = 0; y < sprite.frame.height; ++y) {
const int destinationY = spriteTop + static_cast<int>(y);
if (destinationY < 0 || destinationY >= viewHeight) {
continue;
}
for (remorse::u32 x = 0; x < sprite.frame.width; ++x) {
const int destinationX = spriteLeft + static_cast<int>(x);
if (destinationX < 0 || destinationX >= viewWidth) {
continue;
}
const remorse::u32 sourceX = flipped ? sprite.frame.width - 1 - x : x;
const remorse::s16 paletteIndex = sprite.pixels[
static_cast<std::size_t>(y) * sprite.frame.width + sourceX];
if (paletteIndex < 0) {
continue;
}
const remorse::RgbColor& color = state.playerPalette.colors[
static_cast<remorse::u8>(paletteIndex)];
const std::size_t pixelOffset = static_cast<std::size_t>(destinationY) * viewStride +
static_cast<std::size_t>(destinationX) * 3;
state.viewPixels[pixelOffset] = static_cast<std::uint8_t>(color.blue * 255 / 63);
state.viewPixels[pixelOffset + 1] = static_cast<std::uint8_t>(color.green * 255 / 63);
state.viewPixels[pixelOffset + 2] = static_cast<std::uint8_t>(color.red * 255 / 63);
}
}
BITMAPINFO bitmap{};
bitmap.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmap.bmiHeader.biWidth = viewWidth;
bitmap.bmiHeader.biHeight = -viewHeight;
bitmap.bmiHeader.biPlanes = 1;
bitmap.bmiHeader.biBitCount = 24;
bitmap.bmiHeader.biCompression = BI_RGB;
StretchDIBits(device, 0, 0, viewWidth, viewHeight, 0, 0, viewWidth, viewHeight,
state.viewPixels.data(), &bitmap, DIB_RGB_COLORS, SRCCOPY);
}
void drawLoadingScreen(HDC device, const RECT& client, const AppState& state)
{
HBRUSH background = CreateSolidBrush(RGB(13, 23, 28));
@ -885,6 +906,9 @@ void paintWindow(HWND window, AppState& state)
const int centeredTop = avatarScreen.y - state.scene->sourceTop - viewHeight / 2;
const int sourceLeft = std::clamp(centeredLeft, 0, static_cast<int>(state.scene->width) - viewWidth);
const int sourceTop = std::clamp(centeredTop, 0, static_cast<int>(state.scene->height) - viewHeight);
if ((sourceLeft != centeredLeft || sourceTop != centeredTop) && state.renderThread.joinable()) {
requestLiveRender(state, true);
}
const std::size_t viewStride = (static_cast<std::size_t>(viewWidth) * 3 + 3) & ~std::size_t(3);
state.viewPixels.resize(viewStride * static_cast<std::size_t>(viewHeight));
@ -896,7 +920,43 @@ void paintWindow(HWND window, AppState& state)
std::memcpy(state.viewPixels.data() + viewOffset, state.dibPixels.data() + sceneOffset,
static_cast<std::size_t>(viewWidth) * 3);
}
compositePlayer(device, state, viewWidth, viewHeight, sourceLeft, sourceTop, viewStride);
if (state.playerPatch) {
const remorse::RenderedMapImage& patch = *state.playerPatch;
const int patchLeft = patch.sourceLeft - (state.scene->sourceLeft + sourceLeft);
const int patchTop = patch.sourceTop - (state.scene->sourceTop + sourceTop);
for (int patchY = 0; patchY < static_cast<int>(patch.height); ++patchY) {
const int destY = patchTop + patchY;
if (destY < 0 || destY >= viewHeight) {
continue;
}
for (int patchX = 0; patchX < static_cast<int>(patch.width); ++patchX) {
const int destX = patchLeft + patchX;
if (destX < 0 || destX >= viewWidth) {
continue;
}
const std::size_t sourceIndex = static_cast<std::size_t>(patchY) * patch.width + patchX;
if (patch.coverage[sourceIndex] == 0) {
continue;
}
const remorse::RgbColor& color = patch.pixels[sourceIndex];
const std::size_t destIndex = static_cast<std::size_t>(destY) * viewStride +
static_cast<std::size_t>(destX) * 3;
state.viewPixels[destIndex] = color.blue;
state.viewPixels[destIndex + 1] = color.green;
state.viewPixels[destIndex + 2] = color.red;
}
}
}
BITMAPINFO bitmapInfo{};
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biWidth = viewWidth;
bitmapInfo.bmiHeader.biHeight = -viewHeight;
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 24;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapInfo.bmiHeader.biSizeImage = static_cast<DWORD>(state.viewPixels.size());
StretchDIBits(device, 0, 0, viewWidth, viewHeight, 0, 0, viewWidth, viewHeight,
state.viewPixels.data(), &bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
if (!state.diagnostics) {
finishPaint();
return;
@ -1069,6 +1129,7 @@ void updatePlayer(HWND window, AppState& state)
}
changed = changed || state.diagnostics || gravityMoved;
if (movement.animation == 0) {
const bool playerSpriteChanged = state.playerAnimation != 0 || state.playerFrame != 0;
if (state.animationProcessId != 0) {
auto* process = state.diagnosticKernel.processAt(state.animationProcessId);
process->bytes[0x18] |= 4;
@ -1081,13 +1142,16 @@ void updatePlayer(HWND window, AppState& state)
const bool usecodeMutation = state.usecodeWorldMutationPending;
state.usecodeWorldMutationPending = false;
changed = changed || usecodeMutation;
if (state.renderThread.joinable() && (gravityMoved || usecodeMutation)) {
state.renderRequested = true;
state.renderWake.notify_one();
if (state.renderThread.joinable() &&
(gravityMoved || usecodeMutation || playerSpriteChanged)) {
requestLiveRender(state, gravityMoved || usecodeMutation);
}
continue;
}
const remorse::u8 previousPlayerAnimation = state.playerAnimation;
const remorse::u8 previousPlayerDirection = state.playerDirection;
const remorse::u8 previousPlayerFrame = state.playerFrame;
const remorse::u8 requestedAnimation = movement.animation;
const remorse::AnimationClip& clip = requestedAnimation == 1
? *state.walkAnimation : *state.runAnimation;
@ -1135,9 +1199,11 @@ 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 || gravityMoved)) {
state.renderRequested = true;
state.renderWake.notify_one();
const bool playerSpriteChanged = state.playerAnimation != previousPlayerAnimation ||
state.playerDirection != previousPlayerDirection || state.playerFrame != previousPlayerFrame;
if (state.renderThread.joinable() &&
(moved || hatched || usecodeMutation || gravityMoved || playerSpriteChanged)) {
requestLiveRender(state, hatched || usecodeMutation || gravityMoved);
}
changed = true;
}
@ -1172,7 +1238,10 @@ LRESULT CALLBACK windowProcedure(HWND window, UINT message, WPARAM wParam, LPARA
return 0;
case WM_KILLFOCUS:
state->controllerMovement.clear();
while (state->diagnosticKernel.popPendingKeyEvent()) {}
{
std::lock_guard<std::mutex> lock(state->kernelMutex);
while (state->diagnosticKernel.popPendingKeyEvent()) {}
}
return 0;
case WM_KEYDOWN:
case WM_KEYUP:
@ -1204,6 +1273,7 @@ LRESULT CALLBACK windowProcedure(HWND window, UINT message, WPARAM wParam, LPARA
if (GetKeyState(VK_LSHIFT) & 0x8000) event.modifiers |= 1;
if (GetKeyState(VK_RSHIFT) & 0x8000) event.modifiers |= 2;
event.field8 = message == WM_KEYDOWN ? 1 : 0;
std::lock_guard<std::mutex> lock(state->kernelMutex);
state->diagnosticKernel.queuePendingKeyEvent(event);
}
if (message == WM_KEYUP) return 0;
@ -1290,19 +1360,78 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR commandLine, int showCo
remorse::installCrashDiagnostics();
remorse::u32 mapIndex = 1;
bool diagnostics = false;
std::wistringstream arguments(commandLine ? commandLine : L"");
std::optional<WarpStart> warpStart;
constexpr std::array<remorse::u16, 17> missionBaseMaps{
0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 40};
std::wistringstream argumentStream(commandLine ? commandLine : L"");
std::vector<std::wstring> arguments;
std::wstring argument;
while (arguments >> argument) {
if (argument == L"--diagnostics") {
while (argumentStream >> argument) {
arguments.push_back(argument);
}
const auto parseInteger = [](const std::wstring& text, long minimum, long maximum, long& value) {
wchar_t* end = nullptr;
const long parsed = std::wcstol(text.c_str(), &end, 0);
if (end == text.c_str() || *end != L'\0' || parsed < minimum || parsed > maximum) {
return false;
}
value = parsed;
return true;
};
long warpMission = 0;
long mapOffset = 0;
bool warpRequested = false;
bool invalidArguments = false;
for (std::size_t index = 0; index < arguments.size(); ++index) {
const std::wstring& current = arguments[index];
if (current == L"--diagnostics") {
diagnostics = true;
} else if (current == L"-warp") {
long x = 0;
long y = 0;
long z = 0;
if (warpRequested || index + 4 >= arguments.size() ||
!parseInteger(arguments[index + 1], 0,
static_cast<long>(missionBaseMaps.size() - 1), warpMission) ||
!parseInteger(arguments[index + 2], 0, 0x7fff, x) ||
!parseInteger(arguments[index + 3], 0, 0x7fff, y) ||
!parseInteger(arguments[index + 4], 0, 0xff, z)) {
invalidArguments = true;
break;
}
warpStart = WarpStart{static_cast<remorse::u16>(x),
static_cast<remorse::u16>(y), static_cast<remorse::u8>(z)};
warpRequested = true;
index += 4;
} else if (current == L"-mapoff") {
if (index + 1 >= arguments.size() ||
!parseInteger(arguments[index + 1], -255, 255, mapOffset)) {
invalidArguments = true;
break;
}
++index;
} else {
wchar_t* end = nullptr;
const unsigned long parsed = std::wcstoul(argument.c_str(), &end, 0);
if (end != argument.c_str() && *end == L'\0' && parsed <= 255) {
long parsed = 0;
if (parseInteger(current, 0, 255, parsed)) {
mapIndex = static_cast<remorse::u32>(parsed);
}
}
}
if (warpRequested) {
const long targetMap = missionBaseMaps[static_cast<std::size_t>(warpMission)] + mapOffset;
if (targetMap < 0 || targetMap > 255) {
invalidArguments = true;
} else {
mapIndex = static_cast<remorse::u32>(targetMap);
}
}
if (invalidArguments) {
MessageBoxW(nullptr,
L"Usage: remorse_game.exe -warp <mission> <x> <y> <z> [-mapoff <delta>]\n"
L"Mission must be 0..16, map 0..255, X/Y 0..32767, and Z 0..255.",
L"Invalid warp arguments", MB_OK | MB_ICONERROR);
return 2;
}
wchar_t executablePath[MAX_PATH]{};
GetModuleFileNameW(nullptr, executablePath, MAX_PATH);
@ -1321,6 +1450,7 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR commandLine, int showCo
state.root = *root;
state.mapIndex = mapIndex;
state.diagnostics = diagnostics;
state.warpStart = warpStart;
for (int index = 0; index < 3; ++index) {
state.diagnosticKernel.createProcess(1, state.diagnosticGroupCounts);
}
@ -1348,8 +1478,16 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR commandLine, int showCo
RECT bounds{0, 0, initialClientWidth, initialClientHeight};
AdjustWindowRect(&bounds, WS_OVERLAPPEDWINDOW, FALSE);
HWND window = CreateWindowExW(0, windowClassName,
diagnostics ? L"Crusader: No Remorse | Diagnostics" : L"Crusader: No Remorse | Traversal Test",
std::wstring windowTitle = diagnostics
? L"Crusader: No Remorse | Diagnostics" : L"Crusader: No Remorse | Traversal Test";
if (state.warpStart) {
std::wostringstream title;
title << L"Crusader: No Remorse | Warp map " << state.mapIndex << L" ("
<< state.warpStart->x << L", " << state.warpStart->y << L", "
<< static_cast<unsigned>(state.warpStart->z) << L")";
windowTitle = title.str();
}
HWND window = CreateWindowExW(0, windowClassName, windowTitle.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, bounds.right - bounds.left, bounds.bottom - bounds.top,
nullptr, nullptr, instance, &state);