- Enhanced isometric_controls_tests.cpp to include assertions for controller fire attempt suppression under various conditions. - Added tests for the Usecode interpreter in usecode_vm_tests.cpp, verifying script execution and stack manipulation. - Introduced a new PowerShell script, clean-old-build-artifacts.ps1, to facilitate the cleanup of old build artifacts in specified directories.
503 lines
19 KiB
C++
503 lines
19 KiB
C++
#include "remorse/map_renderer.h"
|
|
|
|
#include "remorse/fixed_map.h"
|
|
#include "remorse/glob_archive.h"
|
|
#include "remorse/detail/map_sorter.h"
|
|
#include "remorse/shape_archive.h"
|
|
#include "remorse/type_flags.h"
|
|
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
#include <map>
|
|
#include <unordered_map>
|
|
|
|
namespace remorse {
|
|
namespace {
|
|
|
|
constexpr u16 flagInvisible = 0x0010;
|
|
constexpr u16 flagFlipped = 0x0020;
|
|
constexpr u16 flagEthereal = liveItemEtherealFlag;
|
|
constexpr u32 siSolid = 0x0002;
|
|
constexpr u32 siLand = 0x0008;
|
|
constexpr u32 siOccl = 0x0010;
|
|
constexpr u32 siDraw = 0x0100;
|
|
constexpr u32 siRoof = 0x0400;
|
|
constexpr u32 siTranslucent = 0x0800;
|
|
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;
|
|
const DecodedShapeFrame* decodedOverride = nullptr;
|
|
const Palette256* paletteOverride = nullptr;
|
|
};
|
|
|
|
struct RenderNode {
|
|
SceneItem item;
|
|
const DecodedShapeFrame* decoded = nullptr;
|
|
const Palette256* paletteOverride = nullptr;
|
|
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<WorldRect>& 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<u8>((static_cast<u32>(color.red) * 255) / 63);
|
|
scaled.green = static_cast<u8>((static_cast<u32>(color.green) * 255) / 63);
|
|
scaled.blue = static_cast<u8>((static_cast<u32>(color.blue) * 255) / 63);
|
|
return scaled;
|
|
}
|
|
|
|
std::vector<SceneItem> collectSceneItems(
|
|
const FixedMap& fixedMap,
|
|
const std::vector<ShapeTypeInfo>& typeFlags,
|
|
const std::vector<GlobObject>& globs,
|
|
const MapRenderOptions& options,
|
|
const LiveFixedMapItems* liveItems)
|
|
{
|
|
std::unordered_map<u32, const GlobObject*> globsByIndex;
|
|
globsByIndex.reserve(globs.size());
|
|
for (const GlobObject& glob : globs) {
|
|
globsByIndex.try_emplace(glob.index, &glob);
|
|
}
|
|
|
|
std::vector<SceneItem> pending;
|
|
pending.reserve(fixedMap.entries.size());
|
|
std::vector<const StagedFixedMapItem*> liveByRecord;
|
|
if (liveItems != nullptr) {
|
|
liveByRecord.resize(fixedMap.entries.size(), nullptr);
|
|
for (const auto& item : liveItems->mapItems()) {
|
|
if (item.recordIndex < liveByRecord.size()) {
|
|
liveByRecord[item.recordIndex] = &item;
|
|
}
|
|
}
|
|
}
|
|
for (std::size_t recordIndex = 0; recordIndex < fixedMap.entries.size(); ++recordIndex) {
|
|
const FixedMapEntry& entry = liveByRecord.empty() || liveByRecord[recordIndex] == nullptr
|
|
? fixedMap.entries[recordIndex] : liveByRecord[recordIndex]->data;
|
|
const s32 scale = liveByRecord.empty() || liveByRecord[recordIndex] == nullptr ? 1 : 2;
|
|
pending.push_back(SceneItem{
|
|
static_cast<s32>(entry.x) * scale,
|
|
static_cast<s32>(entry.y) * scale,
|
|
static_cast<s32>(entry.z),
|
|
entry.shape,
|
|
entry.frame,
|
|
entry.flags,
|
|
entry.quality,
|
|
entry.mapNumber,
|
|
true});
|
|
}
|
|
if (liveItems != nullptr) {
|
|
for (const auto& item : liveItems->mapItems()) {
|
|
if (item.recordIndex == static_cast<std::size_t>(-1)) {
|
|
pending.push_back(SceneItem{static_cast<s32>(item.data.x) * 2,
|
|
static_cast<s32>(item.data.y) * 2, item.data.z, item.data.shape,
|
|
item.data.frame, item.data.flags, item.data.quality, item.data.mapNumber,
|
|
false});
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<SceneItem> 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 ((item.flags & flagEthereal) != 0) {
|
|
continue;
|
|
}
|
|
if ((info.flags & siRoof) != 0 && !options.includeRoofs) {
|
|
continue;
|
|
}
|
|
if (options.expandGlobs && info.family == 3 && item.fromFixed) {
|
|
const auto* stagedEgg = liveByRecord.empty() || index >= liveByRecord.size()
|
|
? nullptr : liveByRecord[index];
|
|
const auto glob = stagedEgg && liveItems->isGlobHatched(stagedEgg->itemNumber)
|
|
? globsByIndex.end() : globsByIndex.find(item.quality);
|
|
if (glob != globsByIndex.end()) {
|
|
for (const GlobObjectEntry& entry : glob->second->entries) {
|
|
pending.push_back(SceneItem{
|
|
(item.x & globCoordMask) + (static_cast<s32>(entry.x) << globCoordShift) + globCoordOffset,
|
|
(item.y & globCoordMask) + (static_cast<s32>(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 projectToScreen(WorldPoint{item.x, item.y, item.z}).x;
|
|
}
|
|
|
|
s32 screenY(const SceneItem& item)
|
|
{
|
|
return projectToScreen(WorldPoint{item.x, item.y, item.z}).y;
|
|
}
|
|
|
|
const DecodedShapeFrame* cachedDecode(
|
|
const ShapeArchive& archive,
|
|
std::map<u32, std::vector<ShapeFrame>>& parsedFrames,
|
|
std::map<std::pair<u32, u32>, 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;
|
|
}
|
|
auto parsed = parsedFrames.find(shape);
|
|
if (parsed == parsedFrames.end()) {
|
|
auto frames = loadShapeFrames(archive, shape);
|
|
if (!frames) {
|
|
return nullptr;
|
|
}
|
|
parsed = parsedFrames.emplace(shape, std::move(*frames)).first;
|
|
}
|
|
if (frame >= parsed->second.size()) {
|
|
return nullptr;
|
|
}
|
|
const auto decoded = decodeShapeFrame(parsed->second[frame]);
|
|
if (!decoded) {
|
|
return nullptr;
|
|
}
|
|
return &cache.emplace(key, std::move(*decoded)).first->second;
|
|
}
|
|
|
|
std::vector<RenderNode> prepareRenderNodes(
|
|
const std::vector<SceneItem>& items,
|
|
const ShapeArchive& shapeArchive,
|
|
std::map<u32, std::vector<ShapeFrame>>& parsedFrames,
|
|
const std::vector<ShapeTypeInfo>& typeFlags,
|
|
std::map<std::pair<u32, u32>, DecodedShapeFrame>& cache)
|
|
{
|
|
std::vector<RenderNode> nodes;
|
|
std::vector<detail::RenderSortItem> sortItems;
|
|
nodes.reserve(items.size());
|
|
sortItems.reserve(items.size());
|
|
for (const SceneItem& item : items) {
|
|
const auto* decoded = item.decodedOverride != nullptr
|
|
? item.decodedOverride : cachedDecode(
|
|
shapeArchive, parsedFrames, 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.paletteOverride = item.paletteOverride;
|
|
node.left = flipped
|
|
? bottomX + decoded->frame.xOffset - static_cast<s32>(decoded->frame.width)
|
|
: bottomX - decoded->frame.xOffset;
|
|
node.top = bottomY - decoded->frame.yOffset;
|
|
node.right = node.left + static_cast<s32>(decoded->frame.width);
|
|
node.bottom = node.top + static_cast<s32>(decoded->frame.height);
|
|
|
|
const ShapeTypeInfo& info = typeFlags[item.shape];
|
|
const s32 xDimension = static_cast<s32>(flipped ? info.y : info.x) * 32;
|
|
const s32 yDimension = static_cast<s32>(flipped ? info.x : info.y) * 32;
|
|
const s32 zDimension = static_cast<s32>(info.z) * 8;
|
|
detail::RenderSortItem sortItem;
|
|
sortItem.left = node.left;
|
|
sortItem.top = node.top;
|
|
sortItem.right = node.right;
|
|
sortItem.bottom = node.bottom;
|
|
sortItem.x = item.x;
|
|
sortItem.xLeft = item.x - xDimension;
|
|
sortItem.y = item.y;
|
|
sortItem.yFar = item.y - yDimension;
|
|
sortItem.z = item.z;
|
|
sortItem.zTop = item.z + zDimension;
|
|
sortItem.sxLeft = sortItem.xLeft / 4 - item.y / 4;
|
|
sortItem.sxRight = item.x / 4 - sortItem.yFar / 4;
|
|
sortItem.sxTop = sortItem.xLeft / 4 - sortItem.yFar / 4;
|
|
sortItem.syTop = sortItem.xLeft / 8 + sortItem.yFar / 8 - sortItem.zTop;
|
|
sortItem.sxBottom = bottomX;
|
|
sortItem.syBottom = bottomY;
|
|
sortItem.shape = item.shape;
|
|
sortItem.frame = item.frame;
|
|
sortItem.bigFlatSquare = xDimension == yDimension && xDimension >= 128;
|
|
sortItem.flat = zDimension == 0;
|
|
sortItem.translucent = (info.flags & siTranslucent) != 0;
|
|
sortItem.animated = info.animationType != 0;
|
|
sortItem.occludes = (info.flags & siOccl) != 0 && !sortItem.translucent &&
|
|
(item.flags & flagInvisible) == 0;
|
|
sortItem.solid = (info.flags & siSolid) != 0;
|
|
sortItem.draw = (info.flags & siDraw) != 0;
|
|
sortItem.roof = (info.flags & siRoof) != 0;
|
|
sortItem.land = (info.flags & siLand) != 0;
|
|
sortItem.inventoryItem = info.family == 13;
|
|
nodes.push_back(node);
|
|
sortItems.push_back(sortItem);
|
|
}
|
|
|
|
const std::vector<std::size_t> order = detail::resolveRenderOrder(sortItems);
|
|
std::vector<RenderNode> orderedNodes;
|
|
orderedNodes.reserve(order.size());
|
|
for (const std::size_t index : order) {
|
|
orderedNodes.push_back(std::move(nodes[index]));
|
|
}
|
|
return orderedNodes;
|
|
}
|
|
|
|
void paintNode(RenderedMapImage& image, const RenderNode& node, const Palette256& palette)
|
|
{
|
|
const Palette256& nodePalette = node.paletteOverride != nullptr ? *node.paletteOverride : 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<s32>(sourceY);
|
|
if (destY < 0 || destY >= static_cast<s32>(image.height)) {
|
|
continue;
|
|
}
|
|
const std::size_t sourceRow = static_cast<std::size_t>(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<s32>(sourceX);
|
|
if (destX < 0 || destX >= static_cast<s32>(image.width)) {
|
|
continue;
|
|
}
|
|
const std::size_t pixelIndex = static_cast<std::size_t>(destY) * image.width + destX;
|
|
image.pixels[pixelIndex] = scalePaletteColor(nodePalette.colors[colorIndex]);
|
|
image.coverage[pixelIndex] = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
void writeLe16(std::ofstream& output, u16 value)
|
|
{
|
|
output.put(static_cast<char>(value & 0xff));
|
|
output.put(static_cast<char>((value >> 8) & 0xff));
|
|
}
|
|
|
|
void writeLe32(std::ofstream& output, u32 value)
|
|
{
|
|
output.put(static_cast<char>(value & 0xff));
|
|
output.put(static_cast<char>((value >> 8) & 0xff));
|
|
output.put(static_cast<char>((value >> 16) & 0xff));
|
|
output.put(static_cast<char>((value >> 24) & 0xff));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
ScreenPoint projectToScreen(const WorldPoint& point)
|
|
{
|
|
return ScreenPoint{(point.x - point.y) / 4, (point.x + point.y) / 8 - point.z};
|
|
}
|
|
|
|
std::optional<MapSceneRenderer> loadMapSceneRenderer(const GameDataRoot& root, u32 mapIndex)
|
|
{
|
|
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, 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;
|
|
}
|
|
return MapSceneRenderer{std::move(*fixedMap), std::move(*globs), std::move(*shapeArchive),
|
|
*palette, std::move(*typeFlags), {}};
|
|
}
|
|
|
|
std::optional<RenderedMapImage> MapSceneRenderer::render(const MapRenderOptions& options,
|
|
const LiveFixedMapItems* liveItems, const MapRenderSprite* sprite)
|
|
{
|
|
auto items = collectSceneItems(map, typeFlags, globs, options, liveItems);
|
|
if (sprite != nullptr && sprite->decoded != nullptr && sprite->palette != nullptr &&
|
|
sprite->shape < typeFlags.size()) {
|
|
SceneItem sceneSprite;
|
|
sceneSprite.x = sprite->position.x;
|
|
sceneSprite.y = sprite->position.y;
|
|
sceneSprite.z = sprite->position.z;
|
|
sceneSprite.shape = sprite->shape;
|
|
sceneSprite.frame = sprite->frame;
|
|
sceneSprite.flags = sprite->flipped ? flagFlipped : 0;
|
|
sceneSprite.mapNumber = static_cast<u8>(options.mapIndex);
|
|
sceneSprite.fromFixed = false;
|
|
sceneSprite.decodedOverride = sprite->decoded;
|
|
sceneSprite.paletteOverride = sprite->palette;
|
|
if (inWorldRect(sceneSprite, options.worldRect)) {
|
|
items.push_back(sceneSprite);
|
|
}
|
|
}
|
|
std::map<u32, std::vector<ShapeFrame>> parsedFrames;
|
|
auto nodes = prepareRenderNodes(items, shapes, parsedFrames, typeFlags, decodedFrames);
|
|
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<u32>(map.entries.size());
|
|
image.renderItemCount = static_cast<u32>(items.size());
|
|
image.decodedItemCount = static_cast<u32>(nodes.size());
|
|
image.fullWidth = maxRight - minLeft;
|
|
image.fullHeight = maxBottom - minTop;
|
|
image.width = options.viewCenter
|
|
? options.maxWidth
|
|
: std::min(options.maxWidth, static_cast<u32>(std::max<s32>(1, image.fullWidth)));
|
|
image.height = options.viewCenter
|
|
? options.maxHeight
|
|
: std::min(options.maxHeight, static_cast<u32>(std::max<s32>(1, image.fullHeight)));
|
|
if (options.viewCenter) {
|
|
const ScreenPoint center = projectToScreen(*options.viewCenter);
|
|
image.sourceLeft = center.x - static_cast<s32>(image.width / 2);
|
|
image.sourceTop = center.y - static_cast<s32>(image.height / 2);
|
|
} else {
|
|
image.sourceLeft = nodes[0].left;
|
|
image.sourceTop = nodes[0].top;
|
|
}
|
|
image.pixels.assign(
|
|
static_cast<std::size_t>(image.width) * image.height,
|
|
RgbColor{static_cast<u8>(10), static_cast<u8>(12), static_cast<u8>(18)});
|
|
image.coverage.assign(image.pixels.size(), 0);
|
|
|
|
for (const RenderNode& node : nodes) {
|
|
if (node.right < image.sourceLeft || node.bottom < image.sourceTop ||
|
|
node.left >= image.sourceLeft + static_cast<s32>(image.width) ||
|
|
node.top >= image.sourceTop + static_cast<s32>(image.height)) {
|
|
continue;
|
|
}
|
|
paintNode(image, node, palette);
|
|
++image.paintedItemCount;
|
|
}
|
|
return image;
|
|
}
|
|
|
|
std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const MapRenderOptions& options)
|
|
{
|
|
auto renderer = loadMapSceneRenderer(root, options.mapIndex);
|
|
return renderer ? renderer->render(options) : std::nullopt;
|
|
}
|
|
|
|
std::optional<RenderedMapImage> renderLiveMapPreview(const GameDataRoot& root,
|
|
const MapRenderOptions& options, const LiveFixedMapItems& items)
|
|
{
|
|
auto renderer = loadMapSceneRenderer(root, options.mapIndex);
|
|
return renderer ? renderer->render(options, &items) : std::nullopt;
|
|
}
|
|
|
|
bool writeBmp24(const std::filesystem::path& path, const RenderedMapImage& image)
|
|
{
|
|
if (image.width == 0 || image.height == 0 || image.pixels.size() != static_cast<std::size_t>(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<u8> row(rowStride, 0);
|
|
for (s32 y = static_cast<s32>(image.height) - 1; y >= 0; --y) {
|
|
std::fill(row.begin(), row.end(), static_cast<u8>(0));
|
|
for (u32 x = 0; x < image.width; ++x) {
|
|
const RgbColor& pixel = image.pixels[static_cast<std::size_t>(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<const char*>(row.data()), row.size());
|
|
}
|
|
return static_cast<bool>(output);
|
|
}
|
|
|
|
} // namespace remorse
|