@clockgrove/assetgrove-tiles 0.0.0-stage → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +260 -2
- package/dist/animation.d.ts +8 -0
- package/dist/animation.js +46 -0
- package/dist/corner16.d.ts +55 -0
- package/dist/corner16.js +122 -0
- package/dist/extension.d.ts +3 -0
- package/dist/extension.js +10 -0
- package/dist/imported.d.ts +7 -0
- package/dist/imported.js +171 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/kind.d.ts +38 -0
- package/dist/kind.js +320 -0
- package/dist/labels.d.ts +25 -0
- package/dist/labels.js +67 -0
- package/dist/objects.d.ts +34 -0
- package/dist/objects.js +192 -0
- package/dist/sheet.d.ts +123 -0
- package/dist/sheet.js +279 -0
- package/package.json +55 -3
- package/skills/assetgrove-tiles/SKILL.md +144 -0
package/dist/imported.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { animationCheck } from "./animation.js";
|
|
2
|
+
import { sheetGridCheck } from "./kind.js";
|
|
3
|
+
import { LABELS_OPERATION, drawnCells, gridStatement, labelsPrompt, parseLabels, sheetReference, } from "./labels.js";
|
|
4
|
+
import { LAYOUTS, NO_LAYOUT, autotileOf, emptyCells, gridFor, gridProblems, layoutNamed, layoutsFitting, parseCellAddress, readGrid, } from "./sheet.js";
|
|
5
|
+
export const SHEET_KIND = "tiles:sheet";
|
|
6
|
+
function invalidInput(message) {
|
|
7
|
+
return Object.assign(new Error(message), { code: "invalid_input" });
|
|
8
|
+
}
|
|
9
|
+
const NOT_GENERATED = `a ${SHEET_KIND} is imported, not generated: assetgrove import --spec <spec.json> --sheet <file>`;
|
|
10
|
+
export function tilesSheetKind(context) {
|
|
11
|
+
const { z, raster } = context;
|
|
12
|
+
const specSchema = z.looseObject({
|
|
13
|
+
kind: z.string(),
|
|
14
|
+
assetId: z.string().min(1),
|
|
15
|
+
fields: z
|
|
16
|
+
.looseObject({
|
|
17
|
+
layout: z.string().optional(),
|
|
18
|
+
terrains: z.strictObject({ lower: z.string().min(1), upper: z.string().min(1) }).optional(),
|
|
19
|
+
grid: z
|
|
20
|
+
.strictObject({
|
|
21
|
+
cellWidth: z.number().int().min(1),
|
|
22
|
+
cellHeight: z.number().int().min(1),
|
|
23
|
+
margin: z.number().int().min(0).default(0),
|
|
24
|
+
spacing: z.number().int().min(0).default(0),
|
|
25
|
+
})
|
|
26
|
+
.optional(),
|
|
27
|
+
animations: z
|
|
28
|
+
.array(z.strictObject({
|
|
29
|
+
frames: z.array(z.string()).min(2),
|
|
30
|
+
durationMs: z.number().positive(),
|
|
31
|
+
}))
|
|
32
|
+
.optional(),
|
|
33
|
+
cellLabels: z
|
|
34
|
+
.strictObject({
|
|
35
|
+
grid: z.strictObject({
|
|
36
|
+
cellWidth: z.number().int().min(1),
|
|
37
|
+
cellHeight: z.number().int().min(1),
|
|
38
|
+
margin: z.number().int().min(0),
|
|
39
|
+
spacing: z.number().int().min(0),
|
|
40
|
+
}),
|
|
41
|
+
cells: z.record(z.string(), z.strictObject({
|
|
42
|
+
kind: z.string(),
|
|
43
|
+
walkable: z.boolean(),
|
|
44
|
+
draw: z.enum(["above", "below"]),
|
|
45
|
+
})),
|
|
46
|
+
})
|
|
47
|
+
.optional(),
|
|
48
|
+
})
|
|
49
|
+
.optional(),
|
|
50
|
+
});
|
|
51
|
+
const sheetGridCheckOfOutput = sheetGridCheck(raster, ({ output }) => output.fields);
|
|
52
|
+
const animationCheckOfOutput = animationCheck(context, ({ output }) => output.fields);
|
|
53
|
+
return {
|
|
54
|
+
kind: SHEET_KIND,
|
|
55
|
+
operations: { [LABELS_OPERATION]: "vision" },
|
|
56
|
+
profileSchema: z.strictObject({}),
|
|
57
|
+
specSchema,
|
|
58
|
+
checks: [sheetGridCheckOfOutput, animationCheckOfOutput],
|
|
59
|
+
planCandidate() {
|
|
60
|
+
throw invalidInput(NOT_GENERATED);
|
|
61
|
+
},
|
|
62
|
+
async generate() {
|
|
63
|
+
throw invalidInput(NOT_GENERATED);
|
|
64
|
+
},
|
|
65
|
+
planProposal(_spec, _profile, routes) {
|
|
66
|
+
const route = routes[LABELS_OPERATION];
|
|
67
|
+
if (route === undefined)
|
|
68
|
+
throw invalidInput(`no route for the ${LABELS_OPERATION} operation`);
|
|
69
|
+
return [
|
|
70
|
+
{
|
|
71
|
+
operation: LABELS_OPERATION,
|
|
72
|
+
model: route.model,
|
|
73
|
+
estimatedCostUsd: route.estimatedCostUsd,
|
|
74
|
+
},
|
|
75
|
+
];
|
|
76
|
+
},
|
|
77
|
+
async proposeImport({ output, tileKinds: projectKinds }, call) {
|
|
78
|
+
const tileKinds = projectKinds();
|
|
79
|
+
const { grid, emptyCells: empty, animations, } = output.fields;
|
|
80
|
+
// An animated tile's later frames are no tiles of their own, so only its first is labelled.
|
|
81
|
+
const later = (animations ?? []).flatMap((a) => a.frames.slice(1));
|
|
82
|
+
const drawn = drawnCells(grid, [...empty, ...later]);
|
|
83
|
+
const reply = await call.vision(LABELS_OPERATION, labelsPrompt(grid, drawn, tileKinds), [
|
|
84
|
+
sheetReference(output.bytes, output.mediaType, output.extension),
|
|
85
|
+
]);
|
|
86
|
+
const cells = parseLabels(reply, drawn, tileKinds);
|
|
87
|
+
const proposed = Object.keys(cells).length;
|
|
88
|
+
const labels = { grid: gridStatement(grid), cells };
|
|
89
|
+
return {
|
|
90
|
+
read: { cellLabels: labels },
|
|
91
|
+
reason: `the model proposed labels for ${proposed} of ${drawn.length} drawn cells; confirm them by stating read.cellLabels as fields.cellLabels in the spec, then import again`,
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
importMedia({ spec, profile, bytes, mediaType, extension, tileKinds: projectKinds }) {
|
|
95
|
+
const parsed = specSchema.safeParse(spec);
|
|
96
|
+
if (!parsed.success)
|
|
97
|
+
throw invalidInput(`a ${SHEET_KIND} spec is invalid: ${parsed.error.issues
|
|
98
|
+
.map((i) => `${i.path.join(".") || "(spec)"} ${i.message}`)
|
|
99
|
+
.join("; ")}`);
|
|
100
|
+
let image;
|
|
101
|
+
try {
|
|
102
|
+
image = raster.decode(bytes);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
throw invalidInput(`the sheet is not a readable image: ${error instanceof Error ? error.message : String(error)}`);
|
|
106
|
+
}
|
|
107
|
+
const declared = parsed.data.fields?.layout;
|
|
108
|
+
const layout = declared ?? NO_LAYOUT;
|
|
109
|
+
const stated = parsed.data.fields?.grid;
|
|
110
|
+
if (declared !== undefined && layoutNamed(declared) === undefined)
|
|
111
|
+
throw invalidInput(`unknown layout "${declared}"; the layouts are ${LAYOUTS.map((l) => l.name).join(", ")}`);
|
|
112
|
+
const read = stated === undefined ? readGrid(image) : undefined;
|
|
113
|
+
if (stated === undefined && read === undefined)
|
|
114
|
+
throw invalidInput("the grid cannot be read from the sheet (no gutters between its cells): state fields.grid { cellWidth, cellHeight, margin, spacing } in the spec");
|
|
115
|
+
const grid = gridFor(image, (stated ?? read));
|
|
116
|
+
const fitting = declared === undefined ? layoutsFitting(grid.columns, grid.rows) : [];
|
|
117
|
+
if (fitting.length > 0 || read !== undefined) {
|
|
118
|
+
const shape = `${grid.columns} × ${grid.rows} cells of ${grid.cellWidth} × ${grid.cellHeight} px`;
|
|
119
|
+
return {
|
|
120
|
+
read: {
|
|
121
|
+
...(read !== undefined ? { grid: read } : {}),
|
|
122
|
+
...(fitting.length > 0 ? { layouts: fitting } : {}),
|
|
123
|
+
},
|
|
124
|
+
reason: `${read !== undefined ? `the grid was read from the sheet's gutters: ${shape}, margin ${grid.margin}, spacing ${grid.spacing}; confirm it by stating it as fields.grid in the spec` : `the sheet's grid is ${shape}`}${fitting.length > 0 ? `; it fits the layouts ${fitting.join(", ")}: confirm one by stating fields.layout in the spec (or none for no layout)` : ""}, then import again`,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const problems = gridProblems({ layout, grid });
|
|
128
|
+
if (problems.length > 0)
|
|
129
|
+
throw invalidInput(`the grid is not sound: ${problems.join("; ")}`);
|
|
130
|
+
const { terrains } = parsed.data.fields ?? {};
|
|
131
|
+
const autotile = autotileOf(layout);
|
|
132
|
+
const labels = parsed.data.fields?.cellLabels;
|
|
133
|
+
if (labels !== undefined) {
|
|
134
|
+
const held = gridStatement(grid);
|
|
135
|
+
const moved = Object.keys(held).filter((field) => labels.grid[field] !== held[field]);
|
|
136
|
+
if (moved.length > 0)
|
|
137
|
+
throw invalidInput(`fields.cellLabels were confirmed on another grid (${moved.join(", ")} differ): the cells moved, so propose the labels again with --propose-labels`);
|
|
138
|
+
const tileKinds = projectKinds();
|
|
139
|
+
const later = (parsed.data.fields?.animations ?? []).flatMap((a) => a.frames.slice(1));
|
|
140
|
+
for (const [address, { kind }] of Object.entries(labels.cells)) {
|
|
141
|
+
if (parseCellAddress(address, grid) === undefined)
|
|
142
|
+
throw invalidInput(`fields.cellLabels names ${address}, which is not a cell of the grid`);
|
|
143
|
+
if (later.includes(address))
|
|
144
|
+
throw invalidInput(`fields.cellLabels names ${address}, a later frame of an animated tile: label its first frame`);
|
|
145
|
+
if (!tileKinds.includes(kind))
|
|
146
|
+
throw invalidInput(`fields.cellLabels gives ${address} the kind "${kind}", which is not a tile kind of the project (${tileKinds.join(", ")})`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const { threshold } = raster.resolveStyle(profile.style, []).alpha;
|
|
150
|
+
const { animations } = parsed.data.fields ?? {};
|
|
151
|
+
const animated = animations === undefined ? {} : { animations };
|
|
152
|
+
return {
|
|
153
|
+
output: {
|
|
154
|
+
mediaType,
|
|
155
|
+
extension,
|
|
156
|
+
bytes,
|
|
157
|
+
fields: {
|
|
158
|
+
layout,
|
|
159
|
+
grid,
|
|
160
|
+
emptyCells: emptyCells(image, grid, threshold),
|
|
161
|
+
...(terrains !== undefined && { terrains }),
|
|
162
|
+
...(autotile !== undefined && { autotile }),
|
|
163
|
+
...animated,
|
|
164
|
+
...(labels !== undefined && { cellLabels: labels }),
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
parameters: { layout, grid, ...animated },
|
|
168
|
+
};
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/kind.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { AssetKind, ExtensionContext, KindCheck, KindCheckInput } from "@clockgrove/assetgrove/extension";
|
|
2
|
+
import type { Pixels, Sheet } from "./sheet.ts";
|
|
3
|
+
export declare const TILES_KIND = "tiles:terrain";
|
|
4
|
+
export declare const ATLAS_OPERATION = "tiles:atlas";
|
|
5
|
+
export declare const SEAMS_OPERATION = "tiles:seams";
|
|
6
|
+
export declare const GRID_CHECK = "tiles:grid";
|
|
7
|
+
export declare const CLASS_UNIFORMITY_CHECK = "tiles:class-uniformity";
|
|
8
|
+
export declare const SEAM_QUALITY_CHECK = "tiles:seam-quality";
|
|
9
|
+
export declare const PALETTE_CHECK = "tiles:palette";
|
|
10
|
+
/** The profile's `kinds["tiles:terrain"]` block, without `values` and `layout`. */
|
|
11
|
+
export interface TilesProfile {
|
|
12
|
+
tileSize: number;
|
|
13
|
+
}
|
|
14
|
+
/** The two terrains a set joins, from the spec's `fields.terrains`. */
|
|
15
|
+
export interface Terrains {
|
|
16
|
+
lower: string;
|
|
17
|
+
upper: string;
|
|
18
|
+
}
|
|
19
|
+
/** The flat greys the template reference draws the lower and the upper terrain in. */
|
|
20
|
+
export declare const TEMPLATE_LOWER: readonly [64, 64, 64, 255];
|
|
21
|
+
export declare const TEMPLATE_UPPER: readonly [192, 192, 192, 255];
|
|
22
|
+
/** A `size` square texture of one colour. */
|
|
23
|
+
export declare function flatTexture(size: number, rgba: readonly number[]): Pixels;
|
|
24
|
+
/**
|
|
25
|
+
* The `tiles:grid` check: the sheet is the size its grid says, and no cell is empty where the
|
|
26
|
+
* layout's autotile table needs one (an empty cell elsewhere, an optional one included, is kept, and
|
|
27
|
+
* said so). `sheetOf` gives the sheet the output is held to.
|
|
28
|
+
*/
|
|
29
|
+
export declare function sheetGridCheck(raster: ExtensionContext["raster"], sheetOf: (input: KindCheckInput) => Sheet,
|
|
30
|
+
/** True where every cell must be drawn whatever the layout's table says (`tiles:objects`). */
|
|
31
|
+
everyCellDrawn?: (layout: string) => boolean): KindCheck;
|
|
32
|
+
/**
|
|
33
|
+
* The `tiles:palette` check: under a palette colour rule, every pixel at or above the alpha
|
|
34
|
+
* threshold is one of the palette's colours (the profile's, else the bible's). Under the full
|
|
35
|
+
* colour rule, or with no palette to hold it to, it passes as `skipped:`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function sheetPaletteCheck(raster: ExtensionContext["raster"]): KindCheck;
|
|
38
|
+
export declare function tilesTerrainKind(context: ExtensionContext): AssetKind;
|
package/dist/kind.js
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `tiles:terrain` kind: a corner-16 terrain tile set between two named terrains, made as one
|
|
3
|
+
* sheet image (a 4 × 4 grid of `kinds["tiles:terrain"].tileSize` cells, `output.fields.layout`
|
|
4
|
+
* `corner-16`), checked exactly for its grid and its seam classes.
|
|
5
|
+
*/
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { CORNER16_TABLE, classMismatches, corner16Grid, templateAtlas } from "./corner16.js";
|
|
8
|
+
import { CORNER16_LAYOUT, autotileOf, cellBox, emptyCells, sheetSize } from "./sheet.js";
|
|
9
|
+
export const TILES_KIND = "tiles:terrain";
|
|
10
|
+
export const ATLAS_OPERATION = "tiles:atlas";
|
|
11
|
+
export const SEAMS_OPERATION = "tiles:seams";
|
|
12
|
+
export const GRID_CHECK = "tiles:grid";
|
|
13
|
+
export const CLASS_UNIFORMITY_CHECK = "tiles:class-uniformity";
|
|
14
|
+
export const SEAM_QUALITY_CHECK = "tiles:seam-quality";
|
|
15
|
+
export const PALETTE_CHECK = "tiles:palette";
|
|
16
|
+
/** The flat greys the template reference draws the lower and the upper terrain in. */
|
|
17
|
+
export const TEMPLATE_LOWER = [0x40, 0x40, 0x40, 255];
|
|
18
|
+
export const TEMPLATE_UPPER = [0xc0, 0xc0, 0xc0, 255];
|
|
19
|
+
/** An error core maps to exit 2 `invalid_input` by its own code. */
|
|
20
|
+
function invalidInput(message) {
|
|
21
|
+
return Object.assign(new Error(message), { code: "invalid_input" });
|
|
22
|
+
}
|
|
23
|
+
/** A `size` square texture of one colour. */
|
|
24
|
+
export function flatTexture(size, rgba) {
|
|
25
|
+
const data = new Uint8Array(size * size * 4);
|
|
26
|
+
for (let i = 0; i < data.length; i += 4)
|
|
27
|
+
data.set(rgba, i);
|
|
28
|
+
return { width: size, height: size, data };
|
|
29
|
+
}
|
|
30
|
+
function reference(role, path, bytes) {
|
|
31
|
+
return {
|
|
32
|
+
role,
|
|
33
|
+
path,
|
|
34
|
+
mediaType: "image/png",
|
|
35
|
+
bytes,
|
|
36
|
+
sha256: createHash("sha256").update(bytes).digest("hex"),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The `tiles:grid` check: the sheet is the size its grid says, and no cell is empty where the
|
|
41
|
+
* layout's autotile table needs one (an empty cell elsewhere, an optional one included, is kept, and
|
|
42
|
+
* said so). `sheetOf` gives the sheet the output is held to.
|
|
43
|
+
*/
|
|
44
|
+
export function sheetGridCheck(raster, sheetOf,
|
|
45
|
+
/** True where every cell must be drawn whatever the layout's table says (`tiles:objects`). */
|
|
46
|
+
everyCellDrawn = () => false) {
|
|
47
|
+
return {
|
|
48
|
+
name: GRID_CHECK,
|
|
49
|
+
mode: "exact",
|
|
50
|
+
source: "deterministic",
|
|
51
|
+
run(input) {
|
|
52
|
+
const { profile, bible, output } = input;
|
|
53
|
+
const { layout, grid } = sheetOf(input);
|
|
54
|
+
const { threshold } = raster.resolveStyle(profile.style, bible.palette).alpha;
|
|
55
|
+
const image = raster.decode(output.bytes);
|
|
56
|
+
const base = { check: GRID_CHECK, source: "deterministic" };
|
|
57
|
+
const { width, height } = sheetSize(grid);
|
|
58
|
+
const shape = `${grid.columns} × ${grid.rows} cells of ${grid.cellWidth} × ${grid.cellHeight} px`;
|
|
59
|
+
if (image.width !== width || image.height !== height)
|
|
60
|
+
return [
|
|
61
|
+
{
|
|
62
|
+
...base,
|
|
63
|
+
passed: false,
|
|
64
|
+
reason: `the sheet is ${image.width} × ${image.height}, not ${width} × ${height} (${shape})`,
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
const empty = emptyCells(image, grid, threshold);
|
|
68
|
+
if (empty.length === 0)
|
|
69
|
+
return [
|
|
70
|
+
{
|
|
71
|
+
...base,
|
|
72
|
+
passed: true,
|
|
73
|
+
reason: `the sheet is ${shape} (${width} × ${height}), no cell empty`,
|
|
74
|
+
},
|
|
75
|
+
];
|
|
76
|
+
const needed = new Set(autotileOf(layout)
|
|
77
|
+
?.tiles.filter((tile) => tile.optional !== true)
|
|
78
|
+
.map((tile) => tile.cell));
|
|
79
|
+
const missing = everyCellDrawn(layout)
|
|
80
|
+
? empty
|
|
81
|
+
: empty.filter((address) => needed.has(address));
|
|
82
|
+
if (missing.length === 0)
|
|
83
|
+
return [
|
|
84
|
+
{
|
|
85
|
+
...base,
|
|
86
|
+
passed: true,
|
|
87
|
+
reason: `the sheet is ${shape} (${width} × ${height}); cells ${empty.join(", ")} are empty and kept as empty`,
|
|
88
|
+
},
|
|
89
|
+
];
|
|
90
|
+
return missing.map((address) => ({
|
|
91
|
+
...base,
|
|
92
|
+
passed: false,
|
|
93
|
+
reason: `cell ${address} is empty: no pixel at alpha ${threshold} or above`,
|
|
94
|
+
}));
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The `tiles:palette` check: under a palette colour rule, every pixel at or above the alpha
|
|
100
|
+
* threshold is one of the palette's colours (the profile's, else the bible's). Under the full
|
|
101
|
+
* colour rule, or with no palette to hold it to, it passes as `skipped:`.
|
|
102
|
+
*/
|
|
103
|
+
export function sheetPaletteCheck(raster) {
|
|
104
|
+
return {
|
|
105
|
+
name: PALETTE_CHECK,
|
|
106
|
+
mode: "exact",
|
|
107
|
+
source: "deterministic",
|
|
108
|
+
run({ profile, bible, output }) {
|
|
109
|
+
const base = { check: PALETTE_CHECK, source: "deterministic" };
|
|
110
|
+
const rule = profile.style?.color?.rule ?? "full";
|
|
111
|
+
const palette = profile.style?.color?.palette ?? bible.palette;
|
|
112
|
+
if (rule !== "palette" || palette.length === 0)
|
|
113
|
+
return [
|
|
114
|
+
{
|
|
115
|
+
...base,
|
|
116
|
+
passed: true,
|
|
117
|
+
reason: rule !== "palette"
|
|
118
|
+
? `skipped: the profile's colour rule is "${rule}", not palette`
|
|
119
|
+
: "skipped: neither the profile nor the bible gives a palette",
|
|
120
|
+
},
|
|
121
|
+
];
|
|
122
|
+
const { threshold } = raster.resolveStyle(profile.style, palette).alpha;
|
|
123
|
+
const allowed = new Set(palette.map((c) => c.trim().toLowerCase()));
|
|
124
|
+
const image = raster.decode(output.bytes);
|
|
125
|
+
const off = new Map();
|
|
126
|
+
for (let i = 0; i < image.data.length; i += 4) {
|
|
127
|
+
if ((image.data[i + 3] ?? 0) < threshold)
|
|
128
|
+
continue;
|
|
129
|
+
const hex = raster.toHex([image.data[i], image.data[i + 1], image.data[i + 2]]);
|
|
130
|
+
if (!allowed.has(hex))
|
|
131
|
+
off.set(hex, (off.get(hex) ?? 0) + 1);
|
|
132
|
+
}
|
|
133
|
+
if (off.size === 0)
|
|
134
|
+
return [
|
|
135
|
+
{
|
|
136
|
+
...base,
|
|
137
|
+
passed: true,
|
|
138
|
+
reason: `every pixel at alpha ${threshold} or above is one of the ${allowed.size} palette colours`,
|
|
139
|
+
},
|
|
140
|
+
];
|
|
141
|
+
const pixels = [...off.values()].reduce((a, b) => a + b, 0);
|
|
142
|
+
return [
|
|
143
|
+
{
|
|
144
|
+
...base,
|
|
145
|
+
passed: false,
|
|
146
|
+
reason: `${pixels} px in ${off.size} colours are off the palette, for example ${[...off.keys()].slice(0, 3).join(", ")}`,
|
|
147
|
+
},
|
|
148
|
+
];
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
export function tilesTerrainKind(context) {
|
|
153
|
+
const { z, raster } = context;
|
|
154
|
+
const profileSchema = z.strictObject({ tileSize: z.number().int().positive() });
|
|
155
|
+
const specSchema = z.looseObject({
|
|
156
|
+
kind: z.string(),
|
|
157
|
+
fields: z.looseObject({
|
|
158
|
+
terrains: z.strictObject({ lower: z.string().min(1), upper: z.string().min(1) }),
|
|
159
|
+
}),
|
|
160
|
+
});
|
|
161
|
+
/** The kind's block of the profile, validated; its `values` and `layout` are core's. */
|
|
162
|
+
function tilesProfileOf(profile) {
|
|
163
|
+
const block = profile.kinds?.[TILES_KIND];
|
|
164
|
+
if (block === undefined)
|
|
165
|
+
throw invalidInput(`the target profile has no kinds["${TILES_KIND}"] block (tileSize)`);
|
|
166
|
+
const own = Object.fromEntries(Object.entries(block).filter(([field]) => field !== "values" && field !== "layout"));
|
|
167
|
+
const parsed = profileSchema.safeParse(own);
|
|
168
|
+
if (!parsed.success)
|
|
169
|
+
throw invalidInput(`kinds["${TILES_KIND}"] is invalid: ${parsed.error.issues
|
|
170
|
+
.map((i) => `${i.path.join(".") || "(block)"} ${i.message}`)
|
|
171
|
+
.join("; ")}`);
|
|
172
|
+
return parsed.data;
|
|
173
|
+
}
|
|
174
|
+
function terrainsOf(spec) {
|
|
175
|
+
const parsed = specSchema.safeParse(spec);
|
|
176
|
+
if (!parsed.success)
|
|
177
|
+
throw invalidInput(`a ${TILES_KIND} spec names its two terrains as fields.terrains { lower, upper }`);
|
|
178
|
+
return parsed.data.fields.terrains;
|
|
179
|
+
}
|
|
180
|
+
/** The sheet the kind makes at the profile's tile size. */
|
|
181
|
+
const sheetOf = (tileSize) => ({
|
|
182
|
+
layout: CORNER16_LAYOUT,
|
|
183
|
+
grid: corner16Grid(tileSize),
|
|
184
|
+
});
|
|
185
|
+
const styleOf = (profile, bible) => raster.resolveStyle(profile.style, bible.palette);
|
|
186
|
+
const gridCheck = sheetGridCheck(raster, ({ profile }) => sheetOf(tilesProfileOf(profile).tileSize));
|
|
187
|
+
const classUniformityCheck = {
|
|
188
|
+
name: CLASS_UNIFORMITY_CHECK,
|
|
189
|
+
mode: "exact",
|
|
190
|
+
source: "deterministic",
|
|
191
|
+
run({ profile, bible, output }) {
|
|
192
|
+
const { grid } = sheetOf(tilesProfileOf(profile).tileSize);
|
|
193
|
+
const base = { check: CLASS_UNIFORMITY_CHECK, source: "deterministic" };
|
|
194
|
+
const { rule } = styleOf(profile, bible).color;
|
|
195
|
+
if (rule !== "palette")
|
|
196
|
+
return [
|
|
197
|
+
{
|
|
198
|
+
...base,
|
|
199
|
+
passed: true,
|
|
200
|
+
reason: `skipped: the profile's colour rule is "${rule}", not palette-indexed, so edge strips are not compared exactly`,
|
|
201
|
+
},
|
|
202
|
+
];
|
|
203
|
+
const image = raster.decode(output.bytes);
|
|
204
|
+
const { width, height } = sheetSize(grid);
|
|
205
|
+
if (image.width !== width || image.height !== height)
|
|
206
|
+
return [
|
|
207
|
+
{
|
|
208
|
+
...base,
|
|
209
|
+
passed: true,
|
|
210
|
+
reason: `skipped: the sheet is not ${width} × ${height} (${grid.columns} × ${grid.rows} cells of ${grid.cellWidth} × ${grid.cellHeight} px), which ${GRID_CHECK} reports`,
|
|
211
|
+
},
|
|
212
|
+
];
|
|
213
|
+
const mismatches = classMismatches(image, grid);
|
|
214
|
+
if (mismatches.length === 0)
|
|
215
|
+
return [
|
|
216
|
+
{
|
|
217
|
+
...base,
|
|
218
|
+
passed: true,
|
|
219
|
+
reason: "every tile's edge strip matches its (side, corner-label pair) class",
|
|
220
|
+
},
|
|
221
|
+
];
|
|
222
|
+
return mismatches.map(({ index, address, side, pair, pixels }) => ({
|
|
223
|
+
...base,
|
|
224
|
+
passed: false,
|
|
225
|
+
reason: `cell ${address} (tile ${index}): its ${side} strip differs from its class (${side}, ${pair}) in ${pixels} px`,
|
|
226
|
+
}));
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
const seamQualityCheck = {
|
|
230
|
+
name: SEAM_QUALITY_CHECK,
|
|
231
|
+
mode: "model",
|
|
232
|
+
operation: SEAMS_OPERATION,
|
|
233
|
+
version: "1",
|
|
234
|
+
source: "decision",
|
|
235
|
+
async run({ spec, profile, output }, call) {
|
|
236
|
+
const { tileSize } = tilesProfileOf(profile);
|
|
237
|
+
const { grid } = sheetOf(tileSize);
|
|
238
|
+
const { lower, upper } = terrainsOf(spec);
|
|
239
|
+
const atlas = raster.decode(output.bytes);
|
|
240
|
+
// Each pure tile (0, all lower; 15, all upper) repeated 2 × 2, side by side.
|
|
241
|
+
const wrap = raster.create(4 * tileSize, 2 * tileSize);
|
|
242
|
+
[0, 15].forEach((index, n) => {
|
|
243
|
+
const tile = raster.crop(atlas, cellBox(grid, CORNER16_TABLE[index]));
|
|
244
|
+
for (let y = 0; y < 2 * tileSize; y++)
|
|
245
|
+
for (let x = 0; x < 2 * tileSize; x++)
|
|
246
|
+
raster.setPixel(wrap, n * 2 * tileSize + x, y, raster.getPixel(tile, x % tileSize, y % tileSize));
|
|
247
|
+
});
|
|
248
|
+
const { result } = await call.decide(SEAMS_OPERATION, `The first image is a corner-16 terrain tile set between ${lower} (lower) and ${upper} (upper): 16 tiles of ${tileSize}px in a 4 × 4 grid. The second shows the pure ${lower} tile and the pure ${upper} tile, each repeated 2 × 2.`, [
|
|
249
|
+
reference("atlas", "atlas.png", output.bytes),
|
|
250
|
+
reference("wrap", "wrap.png", raster.encodePng(wrap)),
|
|
251
|
+
], {
|
|
252
|
+
type: "boolean",
|
|
253
|
+
instructions: `Placed by their corner labels, does each seam between neighbouring tiles look continuous, and does each pure tile wrap with itself without a visible edge?`,
|
|
254
|
+
whenTrue: "the seams look continuous and each pure tile wraps",
|
|
255
|
+
whenFalse: "a seam or a pure tile's wrap shows a visible break",
|
|
256
|
+
});
|
|
257
|
+
const answer = result.answer;
|
|
258
|
+
const passed = answer.type === "boolean" && answer.probability >= 0.5;
|
|
259
|
+
return [
|
|
260
|
+
{
|
|
261
|
+
check: SEAM_QUALITY_CHECK,
|
|
262
|
+
passed,
|
|
263
|
+
reason: passed
|
|
264
|
+
? "the seams look continuous and each pure tile wraps with itself"
|
|
265
|
+
: "a seam or a pure tile's wrap shows a visible break",
|
|
266
|
+
source: "decision",
|
|
267
|
+
},
|
|
268
|
+
];
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
return {
|
|
272
|
+
kind: TILES_KIND,
|
|
273
|
+
operations: { [ATLAS_OPERATION]: "image", [SEAMS_OPERATION]: "decisions" },
|
|
274
|
+
profileSchema,
|
|
275
|
+
specSchema,
|
|
276
|
+
checks: [gridCheck, classUniformityCheck, seamQualityCheck],
|
|
277
|
+
planCandidate(spec, profile, routes) {
|
|
278
|
+
tilesProfileOf(profile);
|
|
279
|
+
terrainsOf(spec);
|
|
280
|
+
const plan = (operation) => {
|
|
281
|
+
const route = routes[operation];
|
|
282
|
+
if (route === undefined)
|
|
283
|
+
throw invalidInput(`no route for the ${operation} operation`);
|
|
284
|
+
return { operation, model: route.model, estimatedCostUsd: route.estimatedCostUsd };
|
|
285
|
+
};
|
|
286
|
+
return [plan(ATLAS_OPERATION), plan(SEAMS_OPERATION)];
|
|
287
|
+
},
|
|
288
|
+
async generate(ctx) {
|
|
289
|
+
const { tileSize } = tilesProfileOf(ctx.profile);
|
|
290
|
+
const terrains = terrainsOf(ctx.spec);
|
|
291
|
+
const style = styleOf(ctx.profile, ctx.bible);
|
|
292
|
+
const sheet = sheetOf(tileSize);
|
|
293
|
+
const { width, height } = sheetSize(sheet.grid);
|
|
294
|
+
const template = templateAtlas(flatTexture(tileSize, TEMPLATE_LOWER), flatTexture(tileSize, TEMPLATE_UPPER));
|
|
295
|
+
const prompt = context.prompt.withNote([
|
|
296
|
+
`A corner-16 terrain tile set between ${terrains.lower} (the lower terrain) and ${terrains.upper} (the upper terrain), as one ${width} × ${height} image: 16 square tiles of ${tileSize}px in a 4 × 4 grid, no gaps, no borders.`,
|
|
297
|
+
`The reference image is the layout: dark grey marks ${terrains.lower}, light grey marks ${terrains.upper}. Each tile's four corners are ${terrains.lower} or ${terrains.upper} as the reference shows, so tiles placed by their corners join seamlessly.`,
|
|
298
|
+
`Tile 0 (top left) is all ${terrains.lower} and tile 15 (bottom right) all ${terrains.upper}; each must tile seamlessly with itself.`,
|
|
299
|
+
`Palette, use only these colours: ${ctx.bible.palette.join(", ")}.`,
|
|
300
|
+
...context.prompt.styleNotes(ctx.spec, ctx.bible).map((n) => `Style note: ${n}`),
|
|
301
|
+
].join("\n"), ctx.note);
|
|
302
|
+
const image = await ctx.call.image(ATLAS_OPERATION, prompt, [reference("layout", "corner-16-template.png", raster.encodePng(template))], ctx.seed);
|
|
303
|
+
const source = raster.decode(image.bytes);
|
|
304
|
+
const scale = Math.min(width / source.width, height / source.height);
|
|
305
|
+
const fitted = raster.scale(source, scale, { x: (width - source.width * scale) / 2, y: (height - source.height * scale) / 2 }, { width, height }, style.edges);
|
|
306
|
+
const atlas = raster.applyStyle(fitted, style);
|
|
307
|
+
return {
|
|
308
|
+
output: {
|
|
309
|
+
mediaType: "image/png",
|
|
310
|
+
extension: "png",
|
|
311
|
+
bytes: raster.encodePng(atlas),
|
|
312
|
+
fields: { ...sheet, terrains, autotile: autotileOf(CORNER16_LAYOUT) },
|
|
313
|
+
},
|
|
314
|
+
qa: { passed: true, reasons: [] },
|
|
315
|
+
prompt,
|
|
316
|
+
parameters: { ...sheet, terrains },
|
|
317
|
+
};
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
}
|
package/dist/labels.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { LoadedReference } from "@clockgrove/assetgrove/extension";
|
|
2
|
+
import type { SheetGrid } from "./sheet.ts";
|
|
3
|
+
export declare const LABELS_OPERATION = "tiles:labels";
|
|
4
|
+
export interface CellLabel {
|
|
5
|
+
kind: string;
|
|
6
|
+
walkable: boolean;
|
|
7
|
+
draw: "above" | "below";
|
|
8
|
+
}
|
|
9
|
+
export interface CellLabels {
|
|
10
|
+
grid: {
|
|
11
|
+
cellWidth: number;
|
|
12
|
+
cellHeight: number;
|
|
13
|
+
margin: number;
|
|
14
|
+
spacing: number;
|
|
15
|
+
};
|
|
16
|
+
cells: Record<string, CellLabel>;
|
|
17
|
+
}
|
|
18
|
+
/** The grid statement a set of labels is held to. */
|
|
19
|
+
export declare function gridStatement(grid: SheetGrid): CellLabels["grid"];
|
|
20
|
+
export declare function sheetReference(bytes: Uint8Array, mediaType: string, extension: string): LoadedReference;
|
|
21
|
+
/** The addresses of the drawn cells, row by row. */
|
|
22
|
+
export declare function drawnCells(grid: SheetGrid, empty: readonly string[]): string[];
|
|
23
|
+
export declare function labelsPrompt(grid: SheetGrid, drawn: readonly string[], tileKinds: readonly string[]): string;
|
|
24
|
+
/** The labels of a reply for the drawn cells with an allowed kind; every other entry is dropped. */
|
|
25
|
+
export declare function parseLabels(reply: string, drawn: readonly string[], tileKinds: readonly string[]): Record<string, CellLabel>;
|
package/dist/labels.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cell labels of a `tiles:sheet`: the kind of a cell, whether it is walkable, and whether it draws
|
|
3
|
+
* above or below characters. The model proposes them from the image; a person confirms them.
|
|
4
|
+
*/
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
import { sheetCells } from "./sheet.js";
|
|
7
|
+
export const LABELS_OPERATION = "tiles:labels";
|
|
8
|
+
/** The grid statement a set of labels is held to. */
|
|
9
|
+
export function gridStatement(grid) {
|
|
10
|
+
const { cellWidth, cellHeight, margin, spacing } = grid;
|
|
11
|
+
return { cellWidth, cellHeight, margin, spacing };
|
|
12
|
+
}
|
|
13
|
+
export function sheetReference(bytes, mediaType, extension) {
|
|
14
|
+
return {
|
|
15
|
+
role: "sheet",
|
|
16
|
+
path: `sheet.${extension}`,
|
|
17
|
+
mediaType,
|
|
18
|
+
bytes,
|
|
19
|
+
sha256: createHash("sha256").update(bytes).digest("hex"),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** The addresses of the drawn cells, row by row. */
|
|
23
|
+
export function drawnCells(grid, empty) {
|
|
24
|
+
return sheetCells(grid)
|
|
25
|
+
.map((cell) => cell.address)
|
|
26
|
+
.filter((address) => !empty.includes(address));
|
|
27
|
+
}
|
|
28
|
+
export function labelsPrompt(grid, drawn, tileKinds) {
|
|
29
|
+
return [
|
|
30
|
+
"The image is a tile sheet cut into a grid of cells.",
|
|
31
|
+
`Grid: ${grid.columns} columns × ${grid.rows} rows of ${grid.cellWidth} × ${grid.cellHeight} px, margin ${grid.margin} px, spacing ${grid.spacing} px.`,
|
|
32
|
+
"A cell's address is r<row>c<column>, zero-based from the top left: r0c0 is the top left cell, r1c2 is row 1, column 2.",
|
|
33
|
+
`Label these drawn cells: ${drawn.join(", ")}.`,
|
|
34
|
+
`The allowed kinds (JSON): ${JSON.stringify(tileKinds)}.`,
|
|
35
|
+
'For each cell give its kind, whether a character can walk on it (walkable), and whether it draws above or below characters (draw: "above" or "below").',
|
|
36
|
+
'Reply with ONLY JSON, no other text: {"cells":{"r0c0":{"kind":"...","walkable":true,"draw":"below"}}}',
|
|
37
|
+
].join("\n");
|
|
38
|
+
}
|
|
39
|
+
/** The JSON object a reply holds, from its first `{` to its last `}`; undefined when none parses. */
|
|
40
|
+
function replyObject(text) {
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(text.slice(text.indexOf("{"), text.lastIndexOf("}") + 1));
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** The labels of a reply for the drawn cells with an allowed kind; every other entry is dropped. */
|
|
49
|
+
export function parseLabels(reply, drawn, tileKinds) {
|
|
50
|
+
const object = replyObject(reply);
|
|
51
|
+
const cells = object?.cells;
|
|
52
|
+
const kept = {};
|
|
53
|
+
if (typeof cells !== "object" || cells === null)
|
|
54
|
+
return kept;
|
|
55
|
+
for (const address of drawn) {
|
|
56
|
+
const entry = cells[address];
|
|
57
|
+
if (typeof entry !== "object" || entry === null)
|
|
58
|
+
continue;
|
|
59
|
+
const { kind, walkable, draw } = entry;
|
|
60
|
+
if (typeof kind !== "string" || !tileKinds.includes(kind))
|
|
61
|
+
continue;
|
|
62
|
+
if (typeof walkable !== "boolean" || (draw !== "above" && draw !== "below"))
|
|
63
|
+
continue;
|
|
64
|
+
kept[address] = { kind, walkable, draw };
|
|
65
|
+
}
|
|
66
|
+
return kept;
|
|
67
|
+
}
|