#include "remorse/map_renderer.h" #include "remorse/fixed_map.h" #include "remorse/glob_archive.h" #include "remorse/shape_archive.h" #include "remorse/type_flags.h" #include #include #include namespace remorse { namespace { constexpr u16 flagInvisible = 0x0010; constexpr u16 flagFlipped = 0x0020; constexpr u32 siRoof = 0x0400; constexpr s32 globCoordMask = ~0x3ff; constexpr s32 globCoordShift = 2; constexpr s32 globCoordOffset = 2; struct SceneItem { s32 x = 0; s32 y = 0; s32 z = 0; u16 shape = 0; u8 frame = 0; u16 flags = 0; u16 quality = 0; u8 mapNumber = 0; bool fromFixed = true; }; struct RenderNode { SceneItem item; DecodedShapeFrame decoded; s32 left = 0; s32 top = 0; s32 right = 0; s32 bottom = 0; }; bool isEggFamily(u8 family) { return family == 3 || family == 4 || family == 7 || family == 8; } bool inWorldRect(const SceneItem& item, const std::optional& rect) { if (!rect) { return true; } return item.x >= rect->minX && item.x <= rect->maxX && item.y >= rect->minY && item.y <= rect->maxY; } RgbColor scalePaletteColor(const RgbColor& color) { RgbColor scaled; scaled.red = static_cast((static_cast(color.red) * 255) / 63); scaled.green = static_cast((static_cast(color.green) * 255) / 63); scaled.blue = static_cast((static_cast(color.blue) * 255) / 63); return scaled; } std::optional findGlobObject(const std::vector& globs, u32 index) { for (const GlobObject& object : globs) { if (object.index == index) { return object; } } return std::nullopt; } std::vector collectSceneItems( const FixedMap& fixedMap, const std::vector& typeFlags, const std::vector& globs, const MapRenderOptions& options) { std::vector pending; pending.reserve(fixedMap.entries.size()); for (const FixedMapEntry& entry : fixedMap.entries) { pending.push_back(SceneItem{ static_cast(entry.x), static_cast(entry.y), static_cast(entry.z), entry.shape, entry.frame, entry.flags, entry.quality, entry.mapNumber, true}); } std::vector items; for (std::size_t index = 0; index < pending.size(); ++index) { const SceneItem item = pending[index]; if (!inWorldRect(item, options.worldRect)) { continue; } if (item.shape >= typeFlags.size()) { continue; } const ShapeTypeInfo& info = typeFlags[item.shape]; if ((item.flags & flagInvisible) != 0 && !options.includeHiddenMarkers) { continue; } if ((info.flags & siRoof) != 0 && !options.includeRoofs) { continue; } if (options.expandGlobs && info.family == 3 && item.fromFixed) { const auto glob = findGlobObject(globs, item.quality); if (glob) { for (const GlobObjectEntry& entry : glob->entries) { pending.push_back(SceneItem{ (item.x & globCoordMask) + (static_cast(entry.x) << globCoordShift) + globCoordOffset, (item.y & globCoordMask) + (static_cast(entry.y) << globCoordShift) + globCoordOffset, item.z + entry.z, entry.shape, entry.frame, 0, 0, item.mapNumber, false}); } } if (!options.includeHiddenMarkers) { continue; } } if (isEggFamily(info.family) && !options.includeHiddenMarkers) { continue; } items.push_back(item); } return items; } s32 screenX(const SceneItem& item) { return (item.x - item.y) / 4; } s32 screenY(const SceneItem& item) { return (item.x + item.y) / 8 - item.z; } std::optional cachedDecode( const ShapeArchive& archive, std::map, DecodedShapeFrame>& cache, u32 shape, u32 frame) { const auto key = std::make_pair(shape, frame); const auto found = cache.find(key); if (found != cache.end()) { return found->second; } const auto decoded = decodeShapeFrame(archive, shape, frame); if (!decoded) { return std::nullopt; } cache.emplace(key, *decoded); return decoded; } std::vector prepareRenderNodes(const std::vector& items, const ShapeArchive& shapeArchive) { std::map, DecodedShapeFrame> cache; std::vector nodes; for (const SceneItem& item : items) { const auto decoded = cachedDecode(shapeArchive, cache, item.shape, item.frame); if (!decoded) { continue; } const s32 bottomX = screenX(item); const s32 bottomY = screenY(item); const bool flipped = (item.flags & flagFlipped) != 0; RenderNode node; node.item = item; node.decoded = *decoded; node.left = flipped ? bottomX + node.decoded.frame.xOffset - static_cast(node.decoded.frame.width) : bottomX - node.decoded.frame.xOffset; node.top = bottomY - node.decoded.frame.yOffset; node.right = node.left + static_cast(node.decoded.frame.width); node.bottom = node.top + static_cast(node.decoded.frame.height); nodes.push_back(node); } std::sort(nodes.begin(), nodes.end(), [](const RenderNode& left, const RenderNode& right) { const s32 leftKey = left.item.x + left.item.y; const s32 rightKey = right.item.x + right.item.y; if (leftKey != rightKey) { return leftKey < rightKey; } if (left.item.z != right.item.z) { return left.item.z < right.item.z; } if (left.item.y != right.item.y) { return left.item.y < right.item.y; } return left.item.x < right.item.x; }); return nodes; } void paintNode(RenderedMapImage& image, const RenderNode& node, const Palette256& palette) { const bool flipped = (node.item.flags & flagFlipped) != 0; for (u32 sourceY = 0; sourceY < node.decoded.frame.height; ++sourceY) { const s32 destY = node.top - image.sourceTop + static_cast(sourceY); if (destY < 0 || destY >= static_cast(image.height)) { continue; } const std::size_t sourceRow = static_cast(sourceY) * node.decoded.frame.width; for (u32 sourceX = 0; sourceX < node.decoded.frame.width; ++sourceX) { const u32 actualSourceX = flipped ? node.decoded.frame.width - 1 - sourceX : sourceX; const s16 colorIndex = node.decoded.pixels[sourceRow + actualSourceX]; if (colorIndex < 0 || colorIndex >= 256) { continue; } const s32 destX = node.left - image.sourceLeft + static_cast(sourceX); if (destX < 0 || destX >= static_cast(image.width)) { continue; } image.pixels[static_cast(destY) * image.width + destX] = scalePaletteColor(palette.colors[colorIndex]); } } } void writeLe16(std::ofstream& output, u16 value) { output.put(static_cast(value & 0xff)); output.put(static_cast((value >> 8) & 0xff)); } void writeLe32(std::ofstream& output, u32 value) { output.put(static_cast(value & 0xff)); output.put(static_cast((value >> 8) & 0xff)); output.put(static_cast((value >> 16) & 0xff)); output.put(static_cast((value >> 24) & 0xff)); } } // namespace std::optional renderMapPreview(const GameDataRoot& root, const MapRenderOptions& options) { const auto fixedPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "FIXED.DAT"); const auto globPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "GLOB.FLX"); const auto shapePath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "SHAPES.FLX"); const auto palettePath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "GAMEPAL.PAL"); const auto typeFlagPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "TYPEFLAG.DAT"); if (!fixedPath || !globPath || !shapePath || !palettePath || !typeFlagPath) { return std::nullopt; } const auto fixedArchive = loadFixedMapArchive(*fixedPath); const auto fixedMap = fixedArchive ? loadFixedMap(*fixedArchive, options.mapIndex) : std::nullopt; const auto globs = loadGlobObjects(*globPath); const auto shapeArchive = loadShapeArchive(*shapePath); const auto palette = loadPalette256(*palettePath); const auto typeFlags = loadTypeFlags(*typeFlagPath); if (!fixedMap || !globs || !shapeArchive || !palette || !typeFlags) { return std::nullopt; } const auto items = collectSceneItems(*fixedMap, *typeFlags, *globs, options); auto nodes = prepareRenderNodes(items, *shapeArchive); if (nodes.empty()) { return std::nullopt; } s32 minLeft = nodes[0].left; s32 minTop = nodes[0].top; s32 maxRight = nodes[0].right; s32 maxBottom = nodes[0].bottom; for (const RenderNode& node : nodes) { minLeft = std::min(minLeft, node.left); minTop = std::min(minTop, node.top); maxRight = std::max(maxRight, node.right); maxBottom = std::max(maxBottom, node.bottom); } RenderedMapImage image; image.rawItemCount = static_cast(fixedMap->entries.size()); image.renderItemCount = static_cast(items.size()); image.decodedItemCount = static_cast(nodes.size()); image.sourceLeft = nodes[0].left; image.sourceTop = nodes[0].top; image.fullWidth = maxRight - minLeft; image.fullHeight = maxBottom - minTop; image.width = std::min(options.maxWidth, static_cast(std::max(1, image.fullWidth))); image.height = std::min(options.maxHeight, static_cast(std::max(1, image.fullHeight))); image.pixels.assign( static_cast(image.width) * image.height, RgbColor{static_cast(10), static_cast(12), static_cast(18)}); for (const RenderNode& node : nodes) { if (node.right < image.sourceLeft || node.bottom < image.sourceTop || node.left >= image.sourceLeft + static_cast(image.width) || node.top >= image.sourceTop + static_cast(image.height)) { continue; } paintNode(image, node, *palette); ++image.paintedItemCount; } return image; } bool writeBmp24(const std::filesystem::path& path, const RenderedMapImage& image) { if (image.width == 0 || image.height == 0 || image.pixels.size() != static_cast(image.width) * image.height) { return false; } std::filesystem::create_directories(path.parent_path()); std::ofstream output(path, std::ios::binary); if (!output) { return false; } const u32 rowStride = ((image.width * 3 + 3) / 4) * 4; const u32 pixelBytes = rowStride * image.height; const u32 fileSize = 54 + pixelBytes; output.put('B'); output.put('M'); writeLe32(output, fileSize); writeLe16(output, 0); writeLe16(output, 0); writeLe32(output, 54); writeLe32(output, 40); writeLe32(output, image.width); writeLe32(output, image.height); writeLe16(output, 1); writeLe16(output, 24); writeLe32(output, 0); writeLe32(output, pixelBytes); writeLe32(output, 2835); writeLe32(output, 2835); writeLe32(output, 0); writeLe32(output, 0); std::vector row(rowStride, 0); for (s32 y = static_cast(image.height) - 1; y >= 0; --y) { std::fill(row.begin(), row.end(), static_cast(0)); for (u32 x = 0; x < image.width; ++x) { const RgbColor& pixel = image.pixels[static_cast(y) * image.width + x]; row[x * 3] = pixel.blue; row[x * 3 + 1] = pixel.green; row[x * 3 + 2] = pixel.red; } output.write(reinterpret_cast(row.data()), row.size()); } return static_cast(output); } } // namespace remorse