outpost/scripts/generate-achievement-art.mjs

251 lines
10 KiB
JavaScript

import { execFileSync } from "node:child_process";
import { chromium } from "playwright";
import { readFileSync, writeFileSync, mkdirSync } from "node:fs";
import { createHash } from "node:crypto";
const root = new URL("../", import.meta.url);
const out = new URL("assets/achievements/", root);
mkdirSync(out, { recursive: true });
const hash = (data) => createHash("sha256").update(data).digest("hex");
const ink = "#101e2b",
pale = "#dceef1",
cyan = "#72cde0",
gold = "#edbc62",
green = "#8cd09a";
const path = (d, f = pale, s = ink, w = 3) =>
`<path d="${d}" fill="${f}" stroke="${s}" stroke-width="${w}" stroke-linejoin="round" stroke-linecap="round"/>`;
const rect = (x, y, w, h, f = pale) =>
`<rect x="${x}" y="${y}" width="${w}" height="${h}" rx="2" fill="${f}" stroke="${ink}" stroke-width="3"/>`;
const circle = (x, y, r, f = pale) =>
`<circle cx="${x}" cy="${y}" r="${r}" fill="${f}" stroke="${ink}" stroke-width="3"/>`;
const group = (x, y, s, body) => `<g transform="translate(${x} ${y}) scale(${s})">${body}</g>`;
const line = (d) => path(d, "none", cyan, 3);
const dome = path("M-24 12V0a24 24 0 0 1 48 0v12Z", cyan) + line("M0-24V12M-24 0H24") + rect(-9, 1, 18, 12, ink);
const habitat = dome + rect(23, -2, 23, 15) + path("M23-2 34-12 46-2", gold) + rect(-29, 12, 79, 6, pale);
const flag = path("M0 22V-12", "none", gold, 4) + path("M2-12H25L18-3 25 5H2", gold);
const robot =
rect(-13, -24, 26, 17) +
rect(-16, -3, 32, 23, cyan) +
line("M0-24V-31") +
path("M-16 1-25 9-25 19M16 1 25 9 25 19M-9 20-12 30M9 20 12 30", "none", pale, 5) +
rect(-8, -19, 16, 5, ink);
const ship =
path("M-36 5 14-17 36-6 8 9-17 15Z", pale) +
path("M-8-7 0-23 15-27 14-17M8 9 23 20 36 14 27-1", cyan) +
path("M-29 11-43 19M-21 17-34 25", "none", gold, 4);
const station =
rect(-40, -12, 22, 25, cyan) +
rect(18, -12, 22, 25, cyan) +
line("M-30-12V13M30-12V13M-18 0H18M0-27V27") +
rect(-11, -10, 22, 20) +
`<ellipse cx="0" cy="0" rx="19" ry="29" fill="none" stroke="${gold}" stroke-width="3"/>`;
const leaf = path("M0 16V-12M0 3Q-21 0-17-13Q0-15 0 3M0-3Q19-7 16-22Q0-20 0-3", green, ink, 2);
const person = circle(0, -13, 7, gold) + path("M-11 15V0Q0-8 11 0V15M-5 15V25M5 15V25", "none", pale, 6);
const symbols = {
ore: path("M-12 9-3-10 3-1 9-6 15 9Z", "#a8b4c0"),
water: path("M0-15C8-5 12 0 12 6a12 12 0 1 1-24 0C-12 0-8-5 0-15Z", "#5aa5e0"),
titanium: path("M0-16 14-8V8L0 16-14 8V-8Z", "#d9a53a") + path("M0-16V16M-14-8 14 8M14-8-14 8", "none", ink, 2),
research:
circle(0, 0, 3, "#9a86e8") +
[0, 60, 120]
.map((a) => `<ellipse rx="17" ry="6" transform="rotate(${a})" fill="none" stroke="#b9a6ff" stroke-width="3"/>`)
.join(""),
newChemicals: path("M-5-15H5M-3-15V-5L-13 12Q-15 17-9 17H9Q15 17 13 12L3-5V-15", "#e082c2"),
};
const tank = (x, y) =>
group(
x,
y,
1,
rect(-12, -17, 24, 34, "#5aa5e0") +
`<ellipse cy="-17" rx="12" ry="5" fill="${pale}" stroke="${ink}" stroke-width="3"/>` +
line("M-6-6V8")
);
const factory = (x, y, s, color) =>
group(
x,
y,
s,
path("M-20 18V-9L-7-18V-9L7-18V-9H20V18Z", color) +
rect(9, -29, 8, 20) +
rect(-13, 3, 9, 15, ink) +
rect(4, 3, 9, 9, gold)
);
const scenes = {};
scenes["colony-victor"] = group(56, 75, 1, habitat) + group(76, 35, 0.85, flag);
scenes["head-to-head"] =
group(35, 79, 0.68, habitat) +
group(88, 63, 0.68, habitat) +
group(91, 29, 0.6, flag) +
path("M46 91 80 91", "none", gold, 4);
scenes["crowded-frontier"] =
[
[-32, 0],
[-22, -24],
[0, -34],
[23, -24],
[34, 0],
]
.map(([x, y]) => group(64 + x, 59 + y, 0.3, habitat))
.join("") +
group(53, 84, 0.75, habitat) +
group(88, 60, 0.55, flag);
scenes["kicker-victor"] =
path("M25 98H103M91 94V25H102V98M91 38H102M91 54H102M91 70H102", "none", pale, 5) +
group(61, 65, 0.83, ship) +
group(43, 29, 0.7, flag);
scenes["sealed-bid-victor"] =
rect(29, 53, 65, 40, cyan) +
path("M29 53 61 77 94 53", "none", ink, 3) +
circle(61, 75, 7, gold) +
group(83, 30, 0.65, flag) +
path("M40 29 66 44M34 23 45 17 55 31 44 37Z", gold, pale, 3);
scenes["seventy-five-points"] =
path("M25 96V79H40V61H55V43H72V61H88V79H103V96Z", cyan) +
path("M64 19 68 29 80 30 71 38 74 49 64 43 54 49 57 38 48 30 60 29Z", gold);
scenes["water-megaplex"] =
line("M40 47H88V86H40Z") +
[
[40, 47],
[88, 47],
[40, 86],
[88, 86],
]
.map(([x, y]) => tank(x, y))
.join("") +
group(64, 65, 0.9, symbols.water);
scenes["titanium-megaplex"] =
[
[37, 46],
[89, 46],
[37, 86],
[89, 86],
]
.map(([x, y]) => factory(x, y, 0.65, "#d9a53a"))
.join("") + group(64, 64, 1.1, symbols.titanium);
scenes["chemical-megaplex"] =
path("M54 22H74M58 22V44L29 94Q27 101 36 101H92Q101 101 98 94L70 44V22", "none", "#e082c2", 5) +
[
[51, 57],
[77, 57],
[43, 84],
[85, 84],
]
.map(([x, y]) => group(x, y, 0.62, symbols.newChemicals))
.join("") +
line("M51 57H77M43 84H85M64 57V84");
scenes["diversified-industry"] =
line("M64 35 94 57 84 92H43L30 57Z") +
Object.entries(symbols)
.map(([, s], i) => {
const a = ((i * 72 - 90) * Math.PI) / 180,
x = 64 + 35 * Math.cos(a),
y = 66 + 35 * Math.sin(a);
return factory(x, y, 0.55, ["#a8b4c0", "#5aa5e0", "#d9a53a", "#9a86e8", "#e082c2"][i]) + group(x, y - 4, 0.42, s);
})
.join("");
scenes["research-campus"] =
group(61, 44, 1.1, symbols.research) +
factory(79, 83, 0.9, "#9a86e8") +
group(37, 83, 0.75, person) +
path("M28 83 37 90 45 83", pale);
scenes["automated-colony"] = factory(89, 76, 0.6, cyan) + group(49, 66, 1.1, robot) + line("M76 78 87 72");
scenes["established-outpost"] =
group(54, 80, 1, habitat) + path("M85 62V28M74 27 97 37Q84 51 74 27Z", gold, pale, 3) + line("M32 59V43M25 49H39");
scenes["orbital-network"] =
`<ellipse cx="65" cy="65" rx="39" ry="33" fill="none" stroke="${cyan}" stroke-width="3"/>` +
group(44, 39, 0.52, station) +
group(91, 65, 0.5, ship) +
group(58, 94, 0.48, habitat);
scenes["lunar-colony"] =
path("M23 81Q61 108 105 75Q99 111 64 114Q31 111 23 81Z", "#a8b4c0") +
circle(45, 96, 5, ink) +
circle(80, 101, 4, ink) +
group(55, 73, 0.86, habitat) +
path("M85 54V34M78 33 94 40Q84 49 78 33Z", gold, pale, 3);
scenes["self-sustaining-colony"] =
path("M32 41Q62 15 96 45L96 32M96 45H83M97 90Q64 116 31 86L31 99M31 86H45", "none", green, 4) +
group(45, 67, 0.75, dome) +
group(45, 63, 0.65, leaf) +
group(87, 77, 0.43, habitat);
scenes["biosphere-colony"] =
circle(64, 65, 42, cyan) +
path("M24 73H103M31 88H97", "none", ink, 3) +
`<ellipse cx="64" cy="65" rx="23" ry="42" fill="none" stroke="${pale}" stroke-width="3"/>` +
group(50, 64, 1, leaf) +
group(77, 84, 0.42, habitat);
scenes["twenty-workforce"] =
[36, 64, 92].map((x) => group(x, 48, 0.52, person)).join("") +
group(39, 81, 0.55, robot) +
circle(74, 70, 4, gold) +
path("M74 78V88H86V98", "none", pale, 5) +
path("M82 78V91H97M80 90H92V100", "none", cyan, 4);
scenes["trading-network"] =
group(53, 43, 0.8, ship) +
path("M52 84V64L69 52H97L109 67V91H52Z", gold) +
rect(61, 71, 15, 20, ink) +
rect(83, 71, 16, 20, ink) +
path("M27 59V87H42L35 80M42 87 35 94M85 36H103V52L96 45M103 52 110 45", "none", cyan, 4);
const captions = { "crowded-frontier": "5+", "seventy-five-points": "75", "twenty-workforce": "20" };
const baseline = readFileSync(new URL("packages/engine/src/achievements.ts", root), "utf8");
const ids = [...baseline.matchAll(/id: "([\w-]+)"/g)].map((m) => m[1]);
if (JSON.stringify(ids) !== JSON.stringify(Object.keys(scenes))) {
throw Error("Catalog mismatch");
}
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage({ viewport: { width: 256, height: 256 }, deviceScaleFactor: 1 });
const assets = [];
for (const id of ids) {
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 128 128"><title>${id}</title><circle cx="64" cy="64" r="61" fill="${ink}" stroke="#587586" stroke-width="3"/><path d="M13 85Q64 61 115 85V94Q64 127 13 94Z" fill="#263d4c"/><circle cx="25" cy="33" r="2" fill="${cyan}"/><circle cx="103" cy="30" r="1.5" fill="${pale}"/>${scenes[id]}${captions[id] ? `<rect x="46" y="96" width="36" height="21" rx="7" fill="${ink}"/><text x="64" y="112" text-anchor="middle" font-family="DejaVu Sans" font-weight="bold" font-size="18" fill="${pale}">${captions[id]}</text>` : ""}</svg>`;
const key = `achievement-${id}`;
writeFileSync(new URL(`${key}.svg`, out), svg + "\n");
await page.setContent(`<style>body{margin:0;background:transparent}</style>${svg}`);
const png = await page.screenshot({ omitBackground: true });
writeFileSync(new URL(`${key}.png`, out), png);
assets.push({
id,
key,
svg: `${key}.svg`,
png: `${key}.png`,
svgSha256: hash(svg + "\n"),
pngSha256: hash(png),
width: 256,
height: 256,
license: "AGPL-3.0",
provenance:
"Original composition; adapted code-defined colony geometry and resource vocabulary. See PROVENANCE.md.",
});
}
for (const theme of ["light", "dark"]) {
const bg = theme === "light" ? "#f4f1ea" : "#10151e",
fg = theme === "light" ? "#182532" : "#e7eff4";
await page.setViewportSize({ width: 1000, height: 1150 });
await page.setContent(
`<style>body{margin:0;padding:20px;background:${bg};color:${fg};font:13px sans-serif}main{display:grid;grid-template-columns:repeat(4,1fr);gap:12px}section{height:250px}img{border-radius:50%;vertical-align:middle;margin:3px}h3{font-size:13px}</style><h1>Outpost • ${theme} • 128 / 32 / 48 / 64 px</h1><main>${assets.map((a) => `<section><img width="128" src="data:image/png;base64,${readFileSync(new URL(a.png, out)).toString("base64")}"><h3>${a.id}</h3>${[32, 48, 64].map((n) => `<img width="${n}" src="data:image/png;base64,${readFileSync(new URL(a.png, out)).toString("base64")}">`).join("")}</section>`).join("")}</main>`
);
await page.screenshot({ path: new URL(`contact-${theme}.png`, out).pathname, fullPage: true });
}
await browser.close();
const sources = [
"scripts/generate-achievement-art.mjs",
"packages/viewer/src/lib/ColonyArt.svelte",
"packages/viewer/src/lib/ResourceIcon.svelte",
"packages/viewer/src/lib/theme.css",
].map((path) => ({ path, sha256: hash(readFileSync(new URL(path, root))) }));
writeFileSync(
new URL("manifest.json", out),
JSON.stringify(
{
game: "outpost",
engine: "0.10.33",
revision: 3,
renderer: "Playwright 1.62.1 Chromium; DejaVu Sans",
sources,
assets,
},
null,
2
) + "\n"
);
console.log(`Generated ${assets.length} SVG/PNG pairs and light/dark contact sheets.`);
execFileSync("pnpm", ["exec", "oxfmt", "assets/achievements/manifest.json"], { cwd: root });