Crusader_Decomp/rebuild/remorse/tests/map_renderer_tests.cpp
Maddo e757bb33d0 Add extensive tests for animation, input handling, and map rendering
- Enhanced animation archive tests to validate frame metadata and start frame selection logic.
- Introduced new tests for entity dispatch entry handling, including key event queuing and processing.
- Added tests for fixed map item interactions, including glob egg hatching and item movement.
- Expanded flex archive tests to cover teleport egg spawning and usecode event selection.
- Implemented isometric controls tests to verify directional key handling and animation state changes.
- Improved map renderer tests to ensure correct rendering of map previews and collision detection.
2026-09-27 11:48:14 +02:00

154 lines
7.8 KiB
C++

#include "remorse/collision.h"
#include "remorse/file_system.h"
#include "remorse/fixed_map.h"
#include "remorse/glob_archive.h"
#include "remorse/map_renderer.h"
#include "remorse/type_flags.h"
#include <algorithm>
#include <cassert>
#include <filesystem>
#include <utility>
int main()
{
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
assert(root.has_value());
remorse::MapRenderOptions options;
options.mapIndex = 1;
options.maxWidth = 320;
options.maxHeight = 200;
const auto image = remorse::renderMapPreview(*root, options);
assert(image.has_value());
assert(image->width > 0);
assert(image->height > 0);
assert(!image->pixels.empty());
assert(image->decodedItemCount > 0);
assert(image->paintedItemCount > 0);
const auto fixedPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "FIXED.DAT");
assert(fixedPath.has_value());
const auto archive = remorse::loadFixedMapArchive(*fixedPath);
assert(archive.has_value());
const auto fixedMap = remorse::loadFixedMap(*archive, options.mapIndex);
assert(fixedMap.has_value());
remorse::s32 minX = fixedMap->entries.front().x;
remorse::s32 minY = fixedMap->entries.front().y;
remorse::s32 maxX = minX;
remorse::s32 maxY = minY;
for (const remorse::FixedMapEntry& entry : fixedMap->entries) {
minX = std::min(minX, static_cast<remorse::s32>(entry.x));
minY = std::min(minY, static_cast<remorse::s32>(entry.y));
maxX = std::max(maxX, static_cast<remorse::s32>(entry.x));
maxY = std::max(maxY, static_cast<remorse::s32>(entry.y));
}
options.maxWidth = 1280;
options.maxHeight = 760;
options.viewCenter = remorse::WorldPoint{(minX + maxX) / 2, (minY + maxY) / 2, 0};
const auto centeredImage = remorse::renderMapPreview(*root, options);
assert(centeredImage.has_value());
assert(centeredImage->paintedItemCount > 0);
assert(centeredImage->width == options.maxWidth);
assert(centeredImage->height == options.maxHeight);
const remorse::ScreenPoint center = remorse::projectToScreen(*options.viewCenter);
assert(centeredImage->sourceLeft == center.x - static_cast<remorse::s32>(options.maxWidth / 2));
assert(centeredImage->sourceTop == center.y - static_cast<remorse::s32>(options.maxHeight / 2));
const auto typesPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
const auto globPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "GLOB.FLX");
assert(typesPath && globPath);
const auto shapeTypes = remorse::loadTypeFlags(*typesPath);
const auto globs = remorse::loadGlobObjects(*globPath);
assert(shapeTypes && shapeTypes->size() > 1 && globs);
const auto stagedItems = remorse::stageFixedMapItems(*fixedMap);
assert(stagedItems);
const auto startingEgg = remorse::findTeleportEgg(*stagedItems, *shapeTypes, 0x1e);
assert(startingEgg && startingEgg->itemNumber >= 0x100);
assert((startingEgg->data.quality & 0xff) == 0x1e && startingEgg->data.frame == 1);
assert(startingEgg->data.x > 0 && startingEgg->data.y > 0);
assert(startingEgg->data.x * 2 == fixedMap->entries[startingEgg->recordIndex].x);
assert(startingEgg->data.y * 2 == fixedMap->entries[startingEgg->recordIndex].y);
const auto obstacles = remorse::collectMapCollisionBodies(*fixedMap, *shapeTypes, *globs);
const remorse::WorldPoint spawn{startingEgg->data.x * 2, startingEgg->data.y * 2, startingEgg->data.z};
options.maxWidth = 320;
options.maxHeight = 200;
options.viewCenter = spawn;
const auto startingScene = remorse::renderMapPreview(*root, options);
assert(startingScene && startingScene->paintedItemCount > 0);
remorse::LiveFixedMapItems liveSceneItems(*stagedItems);
auto cachedRenderer = remorse::loadMapSceneRenderer(*root, 1);
assert(cachedRenderer);
const auto liveBefore = cachedRenderer->render(options, &liveSceneItems);
const auto samePixels = [](const auto& left, const auto& right) {
return left.size() == right.size() && std::equal(left.begin(), left.end(), right.begin(),
[](const auto& lhs, const auto& rhs) {
return lhs.red == rhs.red && lhs.green == rhs.green && lhs.blue == rhs.blue;
});
};
assert(liveBefore && samePixels(liveBefore->pixels, startingScene->pixels));
const remorse::s32 redrawRadius = static_cast<remorse::s32>(
options.maxWidth + options.maxHeight * 2 + 512);
options.worldRect = remorse::WorldRect{spawn.x - redrawRadius, spawn.y - redrawRadius,
spawn.x + redrawRadius, spawn.y + redrawRadius};
const auto boundedScene = cachedRenderer->render(options, &liveSceneItems);
assert(boundedScene && samePixels(boundedScene->pixels, liveBefore->pixels));
options.worldRect.reset();
const auto nearEgg = std::find_if(stagedItems->begin(), stagedItems->end(), [&](const auto& item) {
return item.data.shape < shapeTypes->size() &&
(*shapeTypes)[item.data.shape].family == 3 && item.data.quality != 0 &&
std::any_of(globs->begin(), globs->end(), [&](const auto& object) {
return object.index == item.data.quality;
});
});
assert(nearEgg != stagedItems->end());
{
options.viewCenter = remorse::WorldPoint{
nearEgg->data.x * 2, nearEgg->data.y * 2, nearEgg->data.z};
const auto unhatchedScene = cachedRenderer->render(options, &liveSceneItems);
assert(unhatchedScene);
const auto glob = std::find_if(globs->begin(), globs->end(), [&](const auto& object) {
return object.index == nearEgg->data.quality;
});
assert(glob != globs->end());
const auto childIds = liveSceneItems.hatchGlobEgg(nearEgg->itemNumber, *glob, *shapeTypes);
assert(childIds && childIds->size() == glob->entries.size());
const auto hatchedScene = cachedRenderer->render(options, &liveSceneItems);
assert(hatchedScene && samePixels(hatchedScene->pixels, unhatchedScene->pixels));
for (const remorse::u16 childId : *childIds) {
const auto* child = liveSceneItems.findItem(childId);
assert(child && liveSceneItems.moveItem(childId,
static_cast<remorse::u16>(child->data.x + 512),
static_cast<remorse::u16>(child->data.y + 512), child->data.z));
}
const auto movedScene = cachedRenderer->render(options, &liveSceneItems);
assert(movedScene && !samePixels(movedScene->pixels, hatchedScene->pixels));
}
options.viewCenter = spawn;
auto visibleItem = std::find_if(stagedItems->begin(), stagedItems->end(), [&](const auto& item) {
if (item.data.shape >= shapeTypes->size() || (item.data.flags & 0x10) != 0 ||
((*shapeTypes)[item.data.shape].flags & 0x400) != 0) {
return false;
}
const auto family = (*shapeTypes)[item.data.shape].family;
if (family == 3 || family == 4 || family == 7 || family == 8) {
return false;
}
return true;
});
assert(visibleItem != stagedItems->end());
assert(liveSceneItems.moveItem(visibleItem->itemNumber, startingEgg->data.x,
startingEgg->data.y, 16));
const auto liveAfter = cachedRenderer->render(options, &liveSceneItems);
assert(liveAfter && !samePixels(liveAfter->pixels, liveBefore->pixels));
assert(!cachedRenderer->decodedFrames.empty());
const auto actor = remorse::makeCollisionBody(spawn, (*shapeTypes)[1]);
bool canLeaveSpawn = false;
for (const auto [deltaX, deltaY] : {std::pair{32, 0}, {0, 32}, {-32, 0}, {0, -32}}) {
canLeaveSpawn |= remorse::canMoveCollisionBody(actor,
{spawn.x + deltaX, spawn.y + deltaY, spawn.z}, obstacles);
}
assert(canLeaveSpawn);
return 0;
}