290 lines
11 KiB
JavaScript
290 lines
11 KiB
JavaScript
import { encodePng } from './bundles.js';
|
|
|
|
const DEFAULT_BACKGROUND = { red: 18, green: 18, blue: 18, alpha: 255 };
|
|
|
|
const GLYPHS = {
|
|
'0': ['111', '101', '101', '101', '111'],
|
|
'1': ['010', '110', '010', '010', '111'],
|
|
'2': ['111', '001', '111', '100', '111'],
|
|
'3': ['111', '001', '111', '001', '111'],
|
|
'4': ['101', '101', '111', '001', '001'],
|
|
'5': ['111', '100', '111', '001', '111'],
|
|
'6': ['111', '100', '111', '101', '111'],
|
|
'7': ['111', '001', '001', '001', '001'],
|
|
'8': ['111', '101', '111', '101', '111'],
|
|
'9': ['111', '101', '111', '001', '111'],
|
|
'a': ['010', '101', '111', '101', '101'],
|
|
'b': ['110', '101', '110', '101', '110'],
|
|
'c': ['111', '100', '100', '100', '111'],
|
|
'd': ['110', '101', '101', '101', '110'],
|
|
'e': ['111', '100', '110', '100', '111'],
|
|
'f': ['111', '100', '110', '100', '100'],
|
|
'p': ['110', '101', '110', '100', '100'],
|
|
't': ['111', '010', '010', '010', '010'],
|
|
'#': ['101', '111', '101', '111', '101'],
|
|
':': ['000', '010', '000', '010', '000'],
|
|
'/': ['001', '001', '010', '100', '100'],
|
|
'-': ['000', '000', '111', '000', '000'],
|
|
};
|
|
|
|
function clearCanvas(width, height, background = null) {
|
|
const canvas = Buffer.alloc(width * height * 4, 0);
|
|
if (!background) {
|
|
return canvas;
|
|
}
|
|
|
|
fillRect(
|
|
canvas,
|
|
width,
|
|
height,
|
|
0,
|
|
0,
|
|
width,
|
|
height,
|
|
background.red ?? 0,
|
|
background.green ?? 0,
|
|
background.blue ?? 0,
|
|
background.alpha ?? 255,
|
|
);
|
|
return canvas;
|
|
}
|
|
|
|
function fillRect(canvas, canvasWidth, canvasHeight, x, y, width, height, red, green, blue, alpha) {
|
|
const startX = Math.max(0, x);
|
|
const startY = Math.max(0, y);
|
|
const endX = Math.min(canvasWidth, x + width);
|
|
const endY = Math.min(canvasHeight, y + height);
|
|
|
|
for (let drawY = startY; drawY < endY; drawY += 1) {
|
|
for (let drawX = startX; drawX < endX; drawX += 1) {
|
|
const target = ((drawY * canvasWidth) + drawX) * 4;
|
|
canvas[target + 0] = red;
|
|
canvas[target + 1] = green;
|
|
canvas[target + 2] = blue;
|
|
canvas[target + 3] = alpha;
|
|
}
|
|
}
|
|
}
|
|
|
|
function drawGlyph(canvas, canvasWidth, canvasHeight, glyph, x, y, red, green, blue, alpha) {
|
|
const rows = GLYPHS[glyph];
|
|
if (!rows) {
|
|
return;
|
|
}
|
|
|
|
for (let rowIndex = 0; rowIndex < rows.length; rowIndex += 1) {
|
|
const row = rows[rowIndex];
|
|
for (let columnIndex = 0; columnIndex < row.length; columnIndex += 1) {
|
|
if (row[columnIndex] !== '1') {
|
|
continue;
|
|
}
|
|
fillRect(canvas, canvasWidth, canvasHeight, x + columnIndex, y + rowIndex, 1, 1, red, green, blue, alpha);
|
|
}
|
|
}
|
|
}
|
|
|
|
function drawLabel(canvas, canvasWidth, canvasHeight, text, x, y) {
|
|
const label = String(text);
|
|
const glyphWidth = 3;
|
|
const glyphHeight = 5;
|
|
const spacing = 1;
|
|
const boxWidth = (label.length * (glyphWidth + spacing)) - spacing + 2;
|
|
const boxHeight = glyphHeight + 2;
|
|
fillRect(canvas, canvasWidth, canvasHeight, x, y, boxWidth, boxHeight, 0, 0, 0, 220);
|
|
|
|
let cursorX = x + 1;
|
|
for (const glyph of label) {
|
|
drawGlyph(canvas, canvasWidth, canvasHeight, glyph, cursorX, y + 1, 255, 255, 0, 255);
|
|
cursorX += glyphWidth + spacing;
|
|
}
|
|
}
|
|
|
|
function drawBoundingBox(canvas, canvasWidth, canvasHeight, x, y, width, height, alpha = 220) {
|
|
if (width <= 0 || height <= 0) {
|
|
return;
|
|
}
|
|
// Top + bottom edges
|
|
fillRect(canvas, canvasWidth, canvasHeight, x, y, width, 1, 255, 255, 255, alpha);
|
|
fillRect(canvas, canvasWidth, canvasHeight, x, y + height - 1, width, 1, 255, 255, 255, alpha);
|
|
// Left + right edges
|
|
fillRect(canvas, canvasWidth, canvasHeight, x, y, 1, height, 255, 255, 255, alpha);
|
|
fillRect(canvas, canvasWidth, canvasHeight, x + width - 1, y, 1, height, 255, 255, 255, alpha);
|
|
}
|
|
|
|
function blitRgba(canvas, canvasWidth, canvasHeight, sprite, dstX, dstY, flipped = false) {
|
|
for (let y = 0; y < sprite.height; y += 1) {
|
|
const canvasY = dstY + y;
|
|
if (canvasY < 0 || canvasY >= canvasHeight) {
|
|
continue;
|
|
}
|
|
for (let x = 0; x < sprite.width; x += 1) {
|
|
const canvasX = dstX + x;
|
|
if (canvasX < 0 || canvasX >= canvasWidth) {
|
|
continue;
|
|
}
|
|
|
|
const sourceX = flipped ? (sprite.width - 1 - x) : x;
|
|
const source = ((y * sprite.width) + sourceX) * 4;
|
|
const alpha = sprite.rgba[source + 3];
|
|
if (alpha === 0) {
|
|
continue;
|
|
}
|
|
|
|
const target = ((canvasY * canvasWidth) + canvasX) * 4;
|
|
canvas[target + 0] = sprite.rgba[source + 0];
|
|
canvas[target + 1] = sprite.rgba[source + 1];
|
|
canvas[target + 2] = sprite.rgba[source + 2];
|
|
canvas[target + 3] = alpha;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Magenta diamond marker for placeholder (non-renderable) entities such as
|
|
// spawners, triggers, NPCs, and UI panels. Drawn in place of a sprite blit so
|
|
// the viewer still surfaces the entity for inspection without guessing wrong
|
|
// art. The diamond is 12x12 by default (caller passes the placeholder width).
|
|
function drawPlaceholderMarker(canvas, canvasWidth, canvasHeight, dstX, dstY, w, h) {
|
|
const half = Math.max(2, Math.floor(Math.min(w, h) / 2));
|
|
const cx = Math.round(dstX + w / 2);
|
|
const cy = Math.round(dstY + h / 2);
|
|
// Filled diamond by scanline.
|
|
for (let dy = -half; dy <= half; dy += 1) {
|
|
const span = half - Math.abs(dy);
|
|
const y = cy + dy;
|
|
if (y < 0 || y >= canvasHeight) continue;
|
|
for (let dx = -span; dx <= span; dx += 1) {
|
|
const x = cx + dx;
|
|
if (x < 0 || x >= canvasWidth) continue;
|
|
// Magenta interior with darker outline on the diamond edge.
|
|
const onEdge = Math.abs(dx) + Math.abs(dy) === half;
|
|
const target = ((y * canvasWidth) + x) * 4;
|
|
canvas[target + 0] = onEdge ? 90 : 220;
|
|
canvas[target + 1] = onEdge ? 0 : 60;
|
|
canvas[target + 2] = onEdge ? 110 : 200;
|
|
canvas[target + 3] = 255;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function renderMap(items, options = {}) {
|
|
if (items.length === 0) {
|
|
throw new Error('No renderable scene items were produced.');
|
|
}
|
|
|
|
// Guard against a single bad item (e.g. corrupt sprite origin) inflating the
|
|
// canvas to multiple gigabytes. We compute the median centroid of all items
|
|
// and drop anything farther than MAX_EXTENT px from it before computing
|
|
// bounds. This keeps render output sane even if the parser produces a few
|
|
// outliers, while preserving every real authored item (the playfield is
|
|
// never larger than a few thousand pixels in either axis).
|
|
const MAX_EXTENT = 4096;
|
|
const centroids = items.map((it) => ({ x: it.drawX + (it.width ?? 0) / 2, y: it.drawY + (it.height ?? 0) / 2 }));
|
|
const sortedX = centroids.map((c) => c.x).sort((a, b) => a - b);
|
|
const sortedY = centroids.map((c) => c.y).sort((a, b) => a - b);
|
|
const medianX = sortedX[sortedX.length >> 1];
|
|
const medianY = sortedY[sortedY.length >> 1];
|
|
const droppedOutliers = [];
|
|
const visibleItems = items.filter((it, idx) => {
|
|
const dx = Math.abs(centroids[idx].x - medianX);
|
|
const dy = Math.abs(centroids[idx].y - medianY);
|
|
if (dx > MAX_EXTENT || dy > MAX_EXTENT) {
|
|
droppedOutliers.push({ recordIndex: it.recordIndex, typeWord: it.typeWord, drawX: it.drawX, drawY: it.drawY, dx, dy });
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
if (droppedOutliers.length > 0 && process.env.PSX_TRACE) {
|
|
console.error('[renderMap] dropped ' + droppedOutliers.length + ' outlier item(s) beyond ' + MAX_EXTENT + 'px from median; first: ' + JSON.stringify(droppedOutliers[0]));
|
|
}
|
|
if (visibleItems.length === 0) {
|
|
throw new Error('All scene items were classified as outliers; nothing to render.');
|
|
}
|
|
|
|
const bounds = visibleItems.reduce(
|
|
(state, item) => ({
|
|
minX: Math.min(state.minX, item.drawX),
|
|
minY: Math.min(state.minY, item.drawY),
|
|
maxX: Math.max(state.maxX, item.drawX + item.width),
|
|
maxY: Math.max(state.maxY, item.drawY + item.height),
|
|
}),
|
|
{ minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity }
|
|
);
|
|
|
|
const padding = 16;
|
|
const width = Math.max(1, (bounds.maxX - bounds.minX) + (padding * 2));
|
|
const height = Math.max(1, (bounds.maxY - bounds.minY) + (padding * 2));
|
|
if (process.env.PSX_TRACE) {
|
|
console.error('[renderMap] items=' + visibleItems.length + ' bounds=' + JSON.stringify(bounds) + ' size=' + width + 'x' + height);
|
|
}
|
|
const canvas = clearCanvas(width, height, options.background ?? DEFAULT_BACKGROUND);
|
|
|
|
for (const item of visibleItems) {
|
|
if (item.placeholder || !item.sprite) {
|
|
// Draw a small magenta diamond marker for non-renderable entities
|
|
// (spawners, triggers, NPCs). Keeps them visible and inspectable in
|
|
// the viewer overlay without guessing wrong art.
|
|
drawPlaceholderMarker(
|
|
canvas,
|
|
width,
|
|
height,
|
|
item.drawX - bounds.minX + padding,
|
|
item.drawY - bounds.minY + padding,
|
|
item.width,
|
|
item.height
|
|
);
|
|
continue;
|
|
}
|
|
blitRgba(
|
|
canvas,
|
|
width,
|
|
height,
|
|
item.sprite,
|
|
item.drawX - bounds.minX + padding,
|
|
item.drawY - bounds.minY + padding,
|
|
Boolean(item.flipped)
|
|
);
|
|
}
|
|
|
|
if (options.drawLabels) {
|
|
// Cap labels at 10 instances per (typeWord, bundleAbsoluteOffset) pair so
|
|
// dense root layers (e.g. floor tiles) do not bury the labels we actually
|
|
// need to read. The bounding box is also suppressed once the label budget
|
|
// is exhausted so duplicate-instance clusters do not redraw white outlines
|
|
// on every tile and obscure surrounding art.
|
|
const labelBudgetPerBundle = 10;
|
|
const labelTally = new Map();
|
|
for (const item of visibleItems) {
|
|
const tallyKey = `${item.typeWord ?? '?'}|${item.bundleAbsoluteOffset ?? '?'}`;
|
|
const used = labelTally.get(tallyKey) ?? 0;
|
|
if (used >= labelBudgetPerBundle) {
|
|
continue;
|
|
}
|
|
labelTally.set(tallyKey, used + 1);
|
|
const boxX = item.drawX - bounds.minX + padding;
|
|
const boxY = item.drawY - bounds.minY + padding;
|
|
const boxW = item.width ?? item.sprite?.width ?? 0;
|
|
const boxH = item.height ?? item.sprite?.height ?? 0;
|
|
drawBoundingBox(canvas, width, height, boxX, boxY, boxW, boxH);
|
|
// Compose a useful label: typeWord (hex) + bundle absolute offset (last 4 hex)
|
|
const labelParts = [];
|
|
if (Number.isInteger(item.typeWord)) {
|
|
labelParts.push('t' + item.typeWord.toString(16));
|
|
}
|
|
if (Number.isInteger(item.bundleAbsoluteOffset)) {
|
|
labelParts.push('b' + (item.bundleAbsoluteOffset & 0xffff).toString(16).padStart(4, '0'));
|
|
}
|
|
if (labelParts.length === 0 && (item.labelId ?? item.id) !== undefined) {
|
|
labelParts.push('#' + (item.labelId ?? item.id));
|
|
}
|
|
drawLabel(canvas, width, height, labelParts.join(':'), boxX, boxY);
|
|
}
|
|
}
|
|
|
|
return {
|
|
width,
|
|
height,
|
|
bounds,
|
|
droppedOutlierCount: droppedOutliers.length,
|
|
png: encodePng(canvas, width, height),
|
|
};
|
|
}
|