#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/shape_archive.h" #include "remorse/type_flags.h" #include #include #include #include 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(entry.x)); minY = std::min(minY, static_cast(entry.y)); maxX = std::max(maxX, static_cast(entry.x)); maxY = std::max(maxY, static_cast(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(options.maxWidth / 2)); assert(centeredImage->sourceTop == center.y - static_cast(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 shape60Item = std::find_if(stagedItems->begin(), stagedItems->end(), [](const auto& item) { return item.data.shape == 0x003c; }); assert(shape60Item != stagedItems->end()); const auto shape60Frame = remorse::decodeShapeFrame(cachedRenderer->shapes, 0x003c, shape60Item->data.frame); assert(shape60Frame && std::any_of(shape60Frame->pixels.begin(), shape60Frame->pixels.end(), [](remorse::s16 color) { return color >= 0; })); assert(((*shapeTypes)[0x003c].flags & 0x400) != 0); remorse::MapSceneRenderer isolatedShape60 = *cachedRenderer; isolatedShape60.map.entries = {fixedMap->entries[shape60Item->recordIndex]}; remorse::MapRenderOptions shape60Options; shape60Options.mapIndex = 1; shape60Options.maxWidth = 320; shape60Options.maxHeight = 200; shape60Options.includeHiddenMarkers = true; shape60Options.viewCenter = remorse::WorldPoint{ static_cast(fixedMap->entries[shape60Item->recordIndex].x), static_cast(fixedMap->entries[shape60Item->recordIndex].y), fixedMap->entries[shape60Item->recordIndex].z}; assert(!isolatedShape60.render(shape60Options)); shape60Options.includeRoofs = true; const auto isolatedShape60Image = isolatedShape60.render(shape60Options); assert(isolatedShape60Image && isolatedShape60Image->renderItemCount == 1 && isolatedShape60Image->decodedItemCount == 1 && isolatedShape60Image->paintedItemCount == 1); 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 auto playerFrame = remorse::decodeShapeFrame(cachedRenderer->shapes, 1, 0); assert(playerFrame); const remorse::MapRenderSprite playerSprite{spawn, 1, 0, false, &*playerFrame, &cachedRenderer->palette}; const auto sceneWithPlayer = cachedRenderer->render(options, &liveSceneItems, &playerSprite); assert(sceneWithPlayer && sceneWithPlayer->renderItemCount == liveBefore->renderItemCount + 1 && !samePixels(sceneWithPlayer->pixels, liveBefore->pixels)); assert(sceneWithPlayer->coverage.size() == sceneWithPlayer->pixels.size() && std::any_of(sceneWithPlayer->coverage.begin(), sceneWithPlayer->coverage.end(), [](remorse::u8 covered) { return covered != 0; })); remorse::MapRenderOptions fullPlayerReferenceOptions = options; fullPlayerReferenceOptions.maxWidth = 512; fullPlayerReferenceOptions.maxHeight = 384; const auto fullPlayerReference = cachedRenderer->render( fullPlayerReferenceOptions, &liveSceneItems, &playerSprite); assert(fullPlayerReference); remorse::MapRenderOptions playerPatchOptions = fullPlayerReferenceOptions; playerPatchOptions.worldRect = remorse::WorldRect{ spawn.x - 2048, spawn.y - 2048, spawn.x + 2048, spawn.y + 2048}; const auto playerPatch = cachedRenderer->render(playerPatchOptions, &liveSceneItems, &playerSprite); assert(playerPatch && playerPatch->sourceLeft == fullPlayerReference->sourceLeft && playerPatch->sourceTop == fullPlayerReference->sourceTop); for (std::size_t index = 0; index < playerPatch->coverage.size(); ++index) { if (playerPatch->coverage[index] != 0) { assert(playerPatch->pixels[index].red == fullPlayerReference->pixels[index].red && playerPatch->pixels[index].green == fullPlayerReference->pixels[index].green && playerPatch->pixels[index].blue == fullPlayerReference->pixels[index].blue); } } const remorse::WorldPoint warpPosition{30463 * 2, 29951 * 2, 0}; const remorse::MapRenderSprite warpPlayer{warpPosition, 1, 0, false, &*playerFrame, &cachedRenderer->palette}; remorse::MapRenderOptions warpReferenceOptions = fullPlayerReferenceOptions; warpReferenceOptions.includeRoofs = true; warpReferenceOptions.viewCenter = warpPosition; const auto warpReference = cachedRenderer->render(warpReferenceOptions, nullptr, &warpPlayer); assert(warpReference); remorse::MapRenderOptions warpPatchOptions = warpReferenceOptions; warpPatchOptions.worldRect = remorse::WorldRect{ warpPosition.x - 2560, warpPosition.y - 2560, warpPosition.x + 2560, warpPosition.y + 2560}; const auto warpPatch = cachedRenderer->render(warpPatchOptions, nullptr, &warpPlayer); assert(warpPatch && warpPatch->sourceLeft == warpReference->sourceLeft && warpPatch->sourceTop == warpReference->sourceTop); for (std::size_t index = 0; index < warpPatch->coverage.size(); ++index) { if (warpPatch->coverage[index] != 0) { assert(warpPatch->pixels[index].red == warpReference->pixels[index].red && warpPatch->pixels[index].green == warpReference->pixels[index].green && warpPatch->pixels[index].blue == warpReference->pixels[index].blue); } } const remorse::s32 redrawRadius = static_cast( 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(child->data.x + 512), static_cast(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; }