@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
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `tiles:objects` kind: a generated sheet of nine unrelated object tiles on a 3 × 3 grid of
|
|
3
|
+
* `kinds["tiles:objects"].tileSize` cells, layout `none`, every cell drawn. The model draws them on
|
|
4
|
+
* a flat chroma background, which is made transparent before the sheet is fitted to its grid and
|
|
5
|
+
* the profile's style is applied. The shape is the one the qualification run held to its grid
|
|
6
|
+
* (docs/decisions.md, "Generated tile sheets", #691).
|
|
7
|
+
*/
|
|
8
|
+
import type { AssetKind, ExtensionContext } from "@clockgrove/assetgrove/extension";
|
|
9
|
+
import type { Pixels, SheetGrid } from "./sheet.ts";
|
|
10
|
+
export declare const OBJECTS_KIND = "tiles:objects";
|
|
11
|
+
export declare const OBJECTS_OPERATION = "tiles:objects";
|
|
12
|
+
export declare const IN_CELL_CHECK = "tiles:in-cell";
|
|
13
|
+
/** The qualified shape: 9 objects, 3 columns by 3 rows. */
|
|
14
|
+
export declare const OBJECTS_COLUMNS = 3;
|
|
15
|
+
export declare const OBJECTS_ROWS = 3;
|
|
16
|
+
export declare const OBJECTS_COUNT: number;
|
|
17
|
+
/** The flat background the model is asked for; no palette colour should be near it. */
|
|
18
|
+
export declare const OBJECTS_BACKGROUND = "#ff00ff";
|
|
19
|
+
/** Pixels within this RGB distance of the sheet's border colour are background. */
|
|
20
|
+
export declare const BACKGROUND_TOLERANCE = 64;
|
|
21
|
+
/** The least share of its cell an object covers, so a speck is not an object. */
|
|
22
|
+
export declare const MIN_COVER = 0.05;
|
|
23
|
+
/** The 3 × 3 grid of `tileSize` square cells, no margin and no spacing. */
|
|
24
|
+
export declare const objectsGrid: (tileSize: number) => SheetGrid;
|
|
25
|
+
/** The prompt for `objects` in cell order, held to `palette`; `notes` are the bible's style notes. */
|
|
26
|
+
export declare function objectsPrompt(objects: readonly string[], palette: readonly string[], notes?: readonly string[]): string;
|
|
27
|
+
/** A copy of `image` whose pixels near its border's median colour are transparent. */
|
|
28
|
+
export declare function clearBackground(image: Pixels): Pixels;
|
|
29
|
+
/**
|
|
30
|
+
* What keeps each object from sitting inside its cell: a pixel at or above `threshold` on the
|
|
31
|
+
* cell's outermost ring (the grid cuts the object), or an object under `MIN_COVER` of the cell.
|
|
32
|
+
*/
|
|
33
|
+
export declare function inCellProblems(image: Pixels, grid: SheetGrid, threshold: number): string[];
|
|
34
|
+
export declare function tilesObjectsKind(context: ExtensionContext): AssetKind;
|
package/dist/objects.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { sheetGridCheck, sheetPaletteCheck } from "./kind.js";
|
|
2
|
+
import { NO_LAYOUT, cellBox, sheetCells, sheetSize } from "./sheet.js";
|
|
3
|
+
export const OBJECTS_KIND = "tiles:objects";
|
|
4
|
+
export const OBJECTS_OPERATION = "tiles:objects";
|
|
5
|
+
export const IN_CELL_CHECK = "tiles:in-cell";
|
|
6
|
+
/** The qualified shape: 9 objects, 3 columns by 3 rows. */
|
|
7
|
+
export const OBJECTS_COLUMNS = 3;
|
|
8
|
+
export const OBJECTS_ROWS = 3;
|
|
9
|
+
export const OBJECTS_COUNT = OBJECTS_COLUMNS * OBJECTS_ROWS;
|
|
10
|
+
/** The flat background the model is asked for; no palette colour should be near it. */
|
|
11
|
+
export const OBJECTS_BACKGROUND = "#ff00ff";
|
|
12
|
+
/** Pixels within this RGB distance of the sheet's border colour are background. */
|
|
13
|
+
export const BACKGROUND_TOLERANCE = 64;
|
|
14
|
+
/** The least share of its cell an object covers, so a speck is not an object. */
|
|
15
|
+
export const MIN_COVER = 0.05;
|
|
16
|
+
function invalidInput(message) {
|
|
17
|
+
return Object.assign(new Error(message), { code: "invalid_input" });
|
|
18
|
+
}
|
|
19
|
+
/** The 3 × 3 grid of `tileSize` square cells, no margin and no spacing. */
|
|
20
|
+
export const objectsGrid = (tileSize) => ({
|
|
21
|
+
cellWidth: tileSize,
|
|
22
|
+
cellHeight: tileSize,
|
|
23
|
+
columns: OBJECTS_COLUMNS,
|
|
24
|
+
rows: OBJECTS_ROWS,
|
|
25
|
+
margin: 0,
|
|
26
|
+
spacing: 0,
|
|
27
|
+
});
|
|
28
|
+
/** The prompt for `objects` in cell order, held to `palette`; `notes` are the bible's style notes. */
|
|
29
|
+
export function objectsPrompt(objects, palette, notes = []) {
|
|
30
|
+
return [
|
|
31
|
+
`A sprite sheet of ${objects.length} game objects for a top-down 2D game: ${objects.join(", ")}.`,
|
|
32
|
+
`Exactly ${OBJECTS_COLUMNS} columns and ${OBJECTS_ROWS} rows of equal square cells, one object centred in each cell, in that order from the top left. Each object sits wholly inside its cell with empty space around it; nothing crosses from one cell into another.`,
|
|
33
|
+
`A flat solid ${OBJECTS_BACKGROUND} background everywhere that is not an object. No grid lines, no borders, no labels, no text, no shadows on the background.`,
|
|
34
|
+
...(palette.length > 0 ? [`Palette, use only these colours: ${palette.join(", ")}.`] : []),
|
|
35
|
+
...notes.map((n) => `Style note: ${n}`),
|
|
36
|
+
].join("\n");
|
|
37
|
+
}
|
|
38
|
+
/** A copy of `image` whose pixels near its border's median colour are transparent. */
|
|
39
|
+
export function clearBackground(image) {
|
|
40
|
+
const { width, height, data } = image;
|
|
41
|
+
const border = [[], [], []];
|
|
42
|
+
const push = (x, y) => {
|
|
43
|
+
const at = (y * width + x) * 4;
|
|
44
|
+
for (let c = 0; c < 3; c++)
|
|
45
|
+
border[c].push(data[at + c]);
|
|
46
|
+
};
|
|
47
|
+
for (let x = 0; x < width; x++) {
|
|
48
|
+
push(x, 0);
|
|
49
|
+
push(x, height - 1);
|
|
50
|
+
}
|
|
51
|
+
for (let y = 1; y < height - 1; y++) {
|
|
52
|
+
push(0, y);
|
|
53
|
+
push(width - 1, y);
|
|
54
|
+
}
|
|
55
|
+
const bg = border.map((values) => values.sort((a, b) => a - b)[values.length >> 1]);
|
|
56
|
+
const out = new Uint8Array(data);
|
|
57
|
+
for (let i = 0; i < out.length; i += 4) {
|
|
58
|
+
const d = Math.hypot(out[i] - bg[0], out[i + 1] - bg[1], out[i + 2] - bg[2]);
|
|
59
|
+
if (d <= BACKGROUND_TOLERANCE)
|
|
60
|
+
out[i + 3] = 0;
|
|
61
|
+
}
|
|
62
|
+
return { width, height, data: out };
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* What keeps each object from sitting inside its cell: a pixel at or above `threshold` on the
|
|
66
|
+
* cell's outermost ring (the grid cuts the object), or an object under `MIN_COVER` of the cell.
|
|
67
|
+
*/
|
|
68
|
+
export function inCellProblems(image, grid, threshold) {
|
|
69
|
+
const problems = [];
|
|
70
|
+
for (const cell of sheetCells(grid)) {
|
|
71
|
+
const { x, y, width, height } = cellBox(grid, cell);
|
|
72
|
+
let ring = 0;
|
|
73
|
+
let area = 0;
|
|
74
|
+
for (let py = y; py < y + height; py++)
|
|
75
|
+
for (let px = x; px < x + width; px++) {
|
|
76
|
+
if ((image.data[(py * image.width + px) * 4 + 3] ?? 0) < threshold)
|
|
77
|
+
continue;
|
|
78
|
+
area++;
|
|
79
|
+
if (px === x || py === y || px === x + width - 1 || py === y + height - 1)
|
|
80
|
+
ring++;
|
|
81
|
+
}
|
|
82
|
+
if (ring > 0)
|
|
83
|
+
problems.push(`cell ${cell.address}: ${ring} px on the cell's edge, so the grid cuts its object`);
|
|
84
|
+
if (area < MIN_COVER * width * height)
|
|
85
|
+
problems.push(`cell ${cell.address}: its object covers ${area} px, under ${MIN_COVER * 100}% of the cell`);
|
|
86
|
+
}
|
|
87
|
+
return problems;
|
|
88
|
+
}
|
|
89
|
+
export function tilesObjectsKind(context) {
|
|
90
|
+
const { z, raster } = context;
|
|
91
|
+
const profileSchema = z.strictObject({ tileSize: z.number().int().positive() });
|
|
92
|
+
const specSchema = z.looseObject({
|
|
93
|
+
kind: z.string(),
|
|
94
|
+
fields: z.looseObject({
|
|
95
|
+
objects: z.array(z.string().trim().min(1)).length(OBJECTS_COUNT),
|
|
96
|
+
}),
|
|
97
|
+
});
|
|
98
|
+
function tileSizeOf(profile) {
|
|
99
|
+
const block = profile.kinds?.[OBJECTS_KIND];
|
|
100
|
+
if (block === undefined)
|
|
101
|
+
throw invalidInput(`the target profile has no kinds["${OBJECTS_KIND}"] block (tileSize)`);
|
|
102
|
+
const own = Object.fromEntries(Object.entries(block).filter(([field]) => field !== "values" && field !== "layout"));
|
|
103
|
+
const parsed = profileSchema.safeParse(own);
|
|
104
|
+
if (!parsed.success)
|
|
105
|
+
throw invalidInput(`kinds["${OBJECTS_KIND}"] is invalid: ${parsed.error.issues
|
|
106
|
+
.map((i) => `${i.path.join(".") || "(block)"} ${i.message}`)
|
|
107
|
+
.join("; ")}`);
|
|
108
|
+
return parsed.data.tileSize;
|
|
109
|
+
}
|
|
110
|
+
function objectsOf(spec) {
|
|
111
|
+
const parsed = specSchema.safeParse(spec);
|
|
112
|
+
if (!parsed.success)
|
|
113
|
+
throw invalidInput(`a ${OBJECTS_KIND} spec names its ${OBJECTS_COUNT} objects, in cell order, as fields.objects`);
|
|
114
|
+
return parsed.data.fields.objects;
|
|
115
|
+
}
|
|
116
|
+
const sheetOf = (profile) => ({
|
|
117
|
+
layout: NO_LAYOUT,
|
|
118
|
+
grid: objectsGrid(tileSizeOf(profile)),
|
|
119
|
+
});
|
|
120
|
+
const inCellCheck = {
|
|
121
|
+
name: IN_CELL_CHECK,
|
|
122
|
+
mode: "exact",
|
|
123
|
+
source: "deterministic",
|
|
124
|
+
run({ profile, bible, output }) {
|
|
125
|
+
const { grid } = sheetOf(profile);
|
|
126
|
+
const base = { check: IN_CELL_CHECK, source: "deterministic" };
|
|
127
|
+
const image = raster.decode(output.bytes);
|
|
128
|
+
const { width, height } = sheetSize(grid);
|
|
129
|
+
if (image.width !== width || image.height !== height)
|
|
130
|
+
return [
|
|
131
|
+
{
|
|
132
|
+
...base,
|
|
133
|
+
passed: true,
|
|
134
|
+
reason: `skipped: the sheet is not ${width} × ${height}, which tiles:grid reports`,
|
|
135
|
+
},
|
|
136
|
+
];
|
|
137
|
+
const { threshold } = raster.resolveStyle(profile.style, bible.palette).alpha;
|
|
138
|
+
const problems = inCellProblems(image, grid, threshold);
|
|
139
|
+
if (problems.length === 0)
|
|
140
|
+
return [{ ...base, passed: true, reason: "each object sits inside its own cell" }];
|
|
141
|
+
return problems.map((reason) => ({ ...base, passed: false, reason }));
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
return {
|
|
145
|
+
kind: OBJECTS_KIND,
|
|
146
|
+
operations: { [OBJECTS_OPERATION]: "image" },
|
|
147
|
+
profileSchema,
|
|
148
|
+
specSchema,
|
|
149
|
+
checks: [
|
|
150
|
+
sheetGridCheck(raster, ({ profile }) => sheetOf(profile), () => true),
|
|
151
|
+
sheetPaletteCheck(raster),
|
|
152
|
+
inCellCheck,
|
|
153
|
+
],
|
|
154
|
+
planCandidate(spec, profile, routes) {
|
|
155
|
+
tileSizeOf(profile);
|
|
156
|
+
objectsOf(spec);
|
|
157
|
+
const route = routes[OBJECTS_OPERATION];
|
|
158
|
+
if (route === undefined)
|
|
159
|
+
throw invalidInput(`no route for the ${OBJECTS_OPERATION} operation`);
|
|
160
|
+
return [
|
|
161
|
+
{
|
|
162
|
+
operation: OBJECTS_OPERATION,
|
|
163
|
+
model: route.model,
|
|
164
|
+
estimatedCostUsd: route.estimatedCostUsd,
|
|
165
|
+
},
|
|
166
|
+
];
|
|
167
|
+
},
|
|
168
|
+
async generate(ctx) {
|
|
169
|
+
const sheet = sheetOf(ctx.profile);
|
|
170
|
+
const objects = objectsOf(ctx.spec);
|
|
171
|
+
const style = raster.resolveStyle(ctx.profile.style, ctx.bible.palette);
|
|
172
|
+
const { width, height } = sheetSize(sheet.grid);
|
|
173
|
+
const prompt = context.prompt.withNote(objectsPrompt(objects, ctx.bible.palette, context.prompt.styleNotes(ctx.spec, ctx.bible)), ctx.note);
|
|
174
|
+
const image = await ctx.call.image(OBJECTS_OPERATION, prompt, [], ctx.seed);
|
|
175
|
+
const source = clearBackground(raster.decode(image.bytes));
|
|
176
|
+
const scale = Math.min(width / source.width, height / source.height);
|
|
177
|
+
const fitted = raster.scale(source, scale, { x: (width - source.width * scale) / 2, y: (height - source.height * scale) / 2 }, { width, height }, style.edges);
|
|
178
|
+
const fields = { ...sheet, objects };
|
|
179
|
+
return {
|
|
180
|
+
output: {
|
|
181
|
+
mediaType: "image/png",
|
|
182
|
+
extension: "png",
|
|
183
|
+
bytes: raster.encodePng(raster.applyStyle(fitted, style)),
|
|
184
|
+
fields,
|
|
185
|
+
},
|
|
186
|
+
qa: { passed: true, reasons: [] },
|
|
187
|
+
prompt,
|
|
188
|
+
parameters: fields,
|
|
189
|
+
};
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|
package/dist/sheet.d.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tile sheet: one image cut into a grid of cells, and the layout that gives the cells their
|
|
3
|
+
* meaning. Pure arithmetic over RGBA bytes, so it needs nothing from core.
|
|
4
|
+
*/
|
|
5
|
+
/** An 8-bit RGBA image, row-major (core's `Raster`). */
|
|
6
|
+
export interface Pixels {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
data: Uint8Array;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The grid, in whole pixels. `margin` applies on every side of the sheet and `spacing` between
|
|
13
|
+
* every pair of neighbouring cells.
|
|
14
|
+
*/
|
|
15
|
+
export interface SheetGrid {
|
|
16
|
+
cellWidth: number;
|
|
17
|
+
cellHeight: number;
|
|
18
|
+
columns: number;
|
|
19
|
+
rows: number;
|
|
20
|
+
margin: number;
|
|
21
|
+
spacing: number;
|
|
22
|
+
}
|
|
23
|
+
/** Which neighbours an autotile cell's peering bits name. */
|
|
24
|
+
export type AutotileMode = "corners" | "sides" | "corners-and-sides";
|
|
25
|
+
/** The neighbour order of each mode's peering bits. */
|
|
26
|
+
export declare const AUTOTILE_NEIGHBOURS: Readonly<Record<AutotileMode, readonly string[]>>;
|
|
27
|
+
/**
|
|
28
|
+
* A layout fixes the grid shape it names; one without `columns` and `rows` fixes none. `autotile`
|
|
29
|
+
* is its cell table: one string per grid row, one space-separated token per cell,
|
|
30
|
+
* `<centre>/<bits>` (0 the lower terrain, 1 the upper; bits in the mode's neighbour order). A
|
|
31
|
+
* leading `?` marks a cell the layout may leave blank, a spare the completeness check does not need.
|
|
32
|
+
*/
|
|
33
|
+
export interface TileLayout {
|
|
34
|
+
name: string;
|
|
35
|
+
columns?: number;
|
|
36
|
+
rows?: number;
|
|
37
|
+
autotile?: {
|
|
38
|
+
mode: AutotileMode;
|
|
39
|
+
rows: readonly string[];
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export declare const CORNER16_LAYOUT = "corner-16";
|
|
43
|
+
export declare const NO_LAYOUT = "none";
|
|
44
|
+
export declare const LAYOUTS: readonly TileLayout[];
|
|
45
|
+
export declare const BLOB47_LAYOUT: string;
|
|
46
|
+
export declare const SIDE16_LAYOUT: string;
|
|
47
|
+
export declare const CORNER16_2X2_LAYOUT: string;
|
|
48
|
+
export declare const layoutNamed: (name: string) => TileLayout | undefined;
|
|
49
|
+
/** One tile with frames: the cells that are its frames, base tile first, and each one's time. */
|
|
50
|
+
export interface TileAnimation {
|
|
51
|
+
frames: string[];
|
|
52
|
+
durationMs: number;
|
|
53
|
+
}
|
|
54
|
+
export interface Sheet {
|
|
55
|
+
layout: string;
|
|
56
|
+
grid: SheetGrid;
|
|
57
|
+
animations?: TileAnimation[];
|
|
58
|
+
}
|
|
59
|
+
export interface CellPosition {
|
|
60
|
+
column: number;
|
|
61
|
+
row: number;
|
|
62
|
+
}
|
|
63
|
+
export interface Box {
|
|
64
|
+
x: number;
|
|
65
|
+
y: number;
|
|
66
|
+
width: number;
|
|
67
|
+
height: number;
|
|
68
|
+
}
|
|
69
|
+
/** A cell's stable address, zero-based from the top left: `r1c1` is row 1, column 1. */
|
|
70
|
+
export declare const cellAddress: ({ column, row }: CellPosition) => string;
|
|
71
|
+
/**
|
|
72
|
+
* One autotile cell: its terrain at the centre and at each neighbour (0 lower, 1 upper), and
|
|
73
|
+
* `optional` when the layout may leave it blank.
|
|
74
|
+
*/
|
|
75
|
+
export interface AutotileTile {
|
|
76
|
+
cell: string;
|
|
77
|
+
terrain: 0 | 1;
|
|
78
|
+
peering: Record<string, 0 | 1>;
|
|
79
|
+
optional?: true;
|
|
80
|
+
}
|
|
81
|
+
/** The autotile table of a layout as records in row-major order; undefined without one. */
|
|
82
|
+
export declare function autotileOf(layoutName: string): {
|
|
83
|
+
mode: AutotileMode;
|
|
84
|
+
tiles: AutotileTile[];
|
|
85
|
+
} | undefined;
|
|
86
|
+
/** The names of the terrain layouts whose fixed grid shape is `columns` × `rows`, in list order. */
|
|
87
|
+
export declare const layoutsFitting: (columns: number, rows: number) => string[];
|
|
88
|
+
/** The cell an address names, or undefined when it is malformed or outside the grid. */
|
|
89
|
+
export declare function parseCellAddress(address: string, grid: SheetGrid): CellPosition | undefined;
|
|
90
|
+
export declare function cellBox(grid: SheetGrid, { column, row }: CellPosition): Box;
|
|
91
|
+
export declare function sheetSize(grid: SheetGrid): {
|
|
92
|
+
width: number;
|
|
93
|
+
height: number;
|
|
94
|
+
};
|
|
95
|
+
/** Every cell in row-major order. */
|
|
96
|
+
export declare function sheetCells(grid: SheetGrid): Array<CellPosition & {
|
|
97
|
+
address: string;
|
|
98
|
+
}>;
|
|
99
|
+
/** What is wrong with the sheet's grid and layout; empty when it is sound. */
|
|
100
|
+
export declare function gridProblems({ layout, grid }: Sheet): string[];
|
|
101
|
+
/**
|
|
102
|
+
* What is wrong with each declared animation's frames, in declaration order: an address that is
|
|
103
|
+
* malformed or off the grid, a cell used twice (within or across animations), or a layout whose
|
|
104
|
+
* autotile table gives every cell its own tile.
|
|
105
|
+
*/
|
|
106
|
+
export declare function animationProblems({ layout, grid, animations }: Sheet): string[][];
|
|
107
|
+
/** The addresses of cells with no pixel whose alpha is at least `threshold`. */
|
|
108
|
+
export declare function emptyCells(image: Pixels, grid: SheetGrid, threshold: number): string[];
|
|
109
|
+
/** The grid as stated: what a person gives, or `readGrid` reads. */
|
|
110
|
+
export interface GridStatement {
|
|
111
|
+
cellWidth: number;
|
|
112
|
+
cellHeight: number;
|
|
113
|
+
margin: number;
|
|
114
|
+
spacing: number;
|
|
115
|
+
}
|
|
116
|
+
/** The sheet's grid from the cell size, margin and spacing: as many whole cells as fit. */
|
|
117
|
+
export declare function gridFor(image: Pixels, stated: GridStatement): SheetGrid;
|
|
118
|
+
/**
|
|
119
|
+
* The grid a sheet's gutters show. A line is a gutter when every pixel is background: alpha 0 or
|
|
120
|
+
* the top-left pixel's exact colour. Undefined when the gutters are not consistent (one margin all
|
|
121
|
+
* round, one spacing, equal cells) or when there are none to read a cell size from.
|
|
122
|
+
*/
|
|
123
|
+
export declare function readGrid(image: Pixels): GridStatement | undefined;
|
package/dist/sheet.js
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tile sheet: one image cut into a grid of cells, and the layout that gives the cells their
|
|
3
|
+
* meaning. Pure arithmetic over RGBA bytes, so it needs nothing from core.
|
|
4
|
+
*/
|
|
5
|
+
/** The neighbour order of each mode's peering bits. */
|
|
6
|
+
export const AUTOTILE_NEIGHBOURS = {
|
|
7
|
+
corners: ["nw", "ne", "sw", "se"],
|
|
8
|
+
sides: ["n", "e", "s", "w"],
|
|
9
|
+
"corners-and-sides": ["n", "ne", "e", "se", "s", "sw", "w", "nw"],
|
|
10
|
+
};
|
|
11
|
+
export const CORNER16_LAYOUT = "corner-16";
|
|
12
|
+
export const NO_LAYOUT = "none";
|
|
13
|
+
const CORNER16 = {
|
|
14
|
+
name: CORNER16_LAYOUT,
|
|
15
|
+
columns: 4,
|
|
16
|
+
rows: 4,
|
|
17
|
+
autotile: {
|
|
18
|
+
mode: "corners",
|
|
19
|
+
rows: [
|
|
20
|
+
"0/0000 0/1000 0/0100 0/1100",
|
|
21
|
+
"0/0010 0/1010 0/0110 0/1110",
|
|
22
|
+
"0/0001 0/1001 0/0101 0/1101",
|
|
23
|
+
"0/0011 0/1011 0/0111 1/1111",
|
|
24
|
+
],
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
// The Godot 3 entries are Godot 3's autotile templates, the arrangements published Godot packs are
|
|
28
|
+
// drawn to. Their per-cell data is from TileBitTools' templates
|
|
29
|
+
// (https://github.com/dandeliondino/tile_bit_tools, MIT), with the drawn terrain as the upper.
|
|
30
|
+
const BLOB47 = {
|
|
31
|
+
name: "blob-47",
|
|
32
|
+
columns: 12,
|
|
33
|
+
rows: 4,
|
|
34
|
+
autotile: {
|
|
35
|
+
mode: "corners-and-sides",
|
|
36
|
+
rows: [
|
|
37
|
+
"1/00001000 1/00101000 1/00101010 1/00001010 1/10101011 1/00111010 1/00101110 1/11101010 1/00111000 1/10111110 1/00111110 1/00001110",
|
|
38
|
+
"1/10001000 1/10101000 1/10101010 1/10001010 1/10111000 1/11111110 1/10111111 1/10001110 1/11111000 1/11101110 ?0/00000000 1/10101111",
|
|
39
|
+
"1/10000000 1/10100000 1/10100010 1/10000010 1/11101000 1/11111011 1/11101111 1/10001011 1/11111010 1/11111111 1/10111011 1/10001111",
|
|
40
|
+
"1/00000000 1/00100000 1/00100010 1/00000010 1/10101110 1/11100010 1/10100011 1/10111010 1/11100000 1/11100011 1/11101011 1/10000011",
|
|
41
|
+
],
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
const SIDE16 = {
|
|
45
|
+
name: "side-16",
|
|
46
|
+
columns: 4,
|
|
47
|
+
rows: 4,
|
|
48
|
+
autotile: {
|
|
49
|
+
mode: "sides",
|
|
50
|
+
rows: [
|
|
51
|
+
"1/0010 1/0110 1/0111 1/0011",
|
|
52
|
+
"1/1010 1/1110 1/1111 1/1011",
|
|
53
|
+
"1/1000 1/1100 1/1101 1/1001",
|
|
54
|
+
"1/0000 1/0100 1/0101 1/0001",
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
const CORNER16_2X2 = {
|
|
59
|
+
name: "corner-16-2x2",
|
|
60
|
+
columns: 4,
|
|
61
|
+
rows: 4,
|
|
62
|
+
autotile: {
|
|
63
|
+
mode: "corners",
|
|
64
|
+
rows: [
|
|
65
|
+
"1/0010 1/0101 1/1011 1/0011",
|
|
66
|
+
"1/1001 1/0111 1/1111 1/1110",
|
|
67
|
+
"1/0100 1/1100 1/1101 1/1010",
|
|
68
|
+
"?0/0000 1/0001 1/0110 1/1000",
|
|
69
|
+
],
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
export const LAYOUTS = [
|
|
73
|
+
CORNER16,
|
|
74
|
+
BLOB47,
|
|
75
|
+
SIDE16,
|
|
76
|
+
CORNER16_2X2,
|
|
77
|
+
{ name: NO_LAYOUT },
|
|
78
|
+
];
|
|
79
|
+
export const BLOB47_LAYOUT = BLOB47.name;
|
|
80
|
+
export const SIDE16_LAYOUT = SIDE16.name;
|
|
81
|
+
export const CORNER16_2X2_LAYOUT = CORNER16_2X2.name;
|
|
82
|
+
export const layoutNamed = (name) => LAYOUTS.find((layout) => layout.name === name);
|
|
83
|
+
/** A cell's stable address, zero-based from the top left: `r1c1` is row 1, column 1. */
|
|
84
|
+
export const cellAddress = ({ column, row }) => `r${row}c${column}`;
|
|
85
|
+
/** The autotile table of a layout as records in row-major order; undefined without one. */
|
|
86
|
+
export function autotileOf(layoutName) {
|
|
87
|
+
const autotile = layoutNamed(layoutName)?.autotile;
|
|
88
|
+
if (autotile === undefined)
|
|
89
|
+
return undefined;
|
|
90
|
+
const names = AUTOTILE_NEIGHBOURS[autotile.mode];
|
|
91
|
+
const tiles = [];
|
|
92
|
+
autotile.rows.forEach((line, row) => line.split(" ").forEach((token, column) => {
|
|
93
|
+
const optional = token.startsWith("?");
|
|
94
|
+
const [centre = "", bits = ""] = token.slice(optional ? 1 : 0).split("/");
|
|
95
|
+
const bit = (c) => (c === "1" ? 1 : 0);
|
|
96
|
+
tiles.push({
|
|
97
|
+
cell: cellAddress({ column, row }),
|
|
98
|
+
terrain: bit(centre),
|
|
99
|
+
peering: Object.fromEntries(names.map((name, i) => [name, bit(bits[i])])),
|
|
100
|
+
...(optional && { optional }),
|
|
101
|
+
});
|
|
102
|
+
}));
|
|
103
|
+
return { mode: autotile.mode, tiles };
|
|
104
|
+
}
|
|
105
|
+
/** The names of the terrain layouts whose fixed grid shape is `columns` × `rows`, in list order. */
|
|
106
|
+
export const layoutsFitting = (columns, rows) => LAYOUTS.filter((layout) => layout.autotile !== undefined && layout.columns === columns && layout.rows === rows).map((layout) => layout.name);
|
|
107
|
+
/** The cell an address names, or undefined when it is malformed or outside the grid. */
|
|
108
|
+
export function parseCellAddress(address, grid) {
|
|
109
|
+
const match = /^r(0|[1-9]\d*)c(0|[1-9]\d*)$/.exec(address);
|
|
110
|
+
if (match === null)
|
|
111
|
+
return undefined;
|
|
112
|
+
const row = Number(match[1]);
|
|
113
|
+
const column = Number(match[2]);
|
|
114
|
+
return row < grid.rows && column < grid.columns ? { column, row } : undefined;
|
|
115
|
+
}
|
|
116
|
+
export function cellBox(grid, { column, row }) {
|
|
117
|
+
return {
|
|
118
|
+
x: grid.margin + column * (grid.cellWidth + grid.spacing),
|
|
119
|
+
y: grid.margin + row * (grid.cellHeight + grid.spacing),
|
|
120
|
+
width: grid.cellWidth,
|
|
121
|
+
height: grid.cellHeight,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
export function sheetSize(grid) {
|
|
125
|
+
return {
|
|
126
|
+
width: 2 * grid.margin + grid.columns * grid.cellWidth + (grid.columns - 1) * grid.spacing,
|
|
127
|
+
height: 2 * grid.margin + grid.rows * grid.cellHeight + (grid.rows - 1) * grid.spacing,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/** Every cell in row-major order. */
|
|
131
|
+
export function sheetCells(grid) {
|
|
132
|
+
const cells = [];
|
|
133
|
+
for (let row = 0; row < grid.rows; row++)
|
|
134
|
+
for (let column = 0; column < grid.columns; column++)
|
|
135
|
+
cells.push({ column, row, address: cellAddress({ column, row }) });
|
|
136
|
+
return cells;
|
|
137
|
+
}
|
|
138
|
+
/** What is wrong with the sheet's grid and layout; empty when it is sound. */
|
|
139
|
+
export function gridProblems({ layout, grid }) {
|
|
140
|
+
const problems = [];
|
|
141
|
+
const whole = (name, min) => {
|
|
142
|
+
const value = grid[name];
|
|
143
|
+
if (!Number.isInteger(value) || value < min)
|
|
144
|
+
problems.push(`${name} must be a whole number of at least ${min}, not ${value}`);
|
|
145
|
+
};
|
|
146
|
+
for (const name of ["cellWidth", "cellHeight", "columns", "rows"])
|
|
147
|
+
whole(name, 1);
|
|
148
|
+
for (const name of ["margin", "spacing"])
|
|
149
|
+
whole(name, 0);
|
|
150
|
+
const named = layoutNamed(layout);
|
|
151
|
+
if (named === undefined)
|
|
152
|
+
problems.push(`unknown layout "${layout}"; the layouts are ${LAYOUTS.map((l) => l.name).join(", ")}`);
|
|
153
|
+
else {
|
|
154
|
+
if (named.columns !== undefined && grid.columns !== named.columns)
|
|
155
|
+
problems.push(`layout ${layout} has ${named.columns} columns, not ${grid.columns}`);
|
|
156
|
+
if (named.rows !== undefined && grid.rows !== named.rows)
|
|
157
|
+
problems.push(`layout ${layout} has ${named.rows} rows, not ${grid.rows}`);
|
|
158
|
+
}
|
|
159
|
+
return problems;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* What is wrong with each declared animation's frames, in declaration order: an address that is
|
|
163
|
+
* malformed or off the grid, a cell used twice (within or across animations), or a layout whose
|
|
164
|
+
* autotile table gives every cell its own tile.
|
|
165
|
+
*/
|
|
166
|
+
export function animationProblems({ layout, grid, animations }) {
|
|
167
|
+
const owned = layoutNamed(layout)?.autotile !== undefined;
|
|
168
|
+
const seen = new Set();
|
|
169
|
+
return (animations ?? []).map(({ frames }) => {
|
|
170
|
+
const problems = [];
|
|
171
|
+
if (owned)
|
|
172
|
+
problems.push(`layout ${layout} gives every cell its own tile, so it cannot hold frames`);
|
|
173
|
+
for (const address of frames) {
|
|
174
|
+
if (parseCellAddress(address, grid) === undefined)
|
|
175
|
+
problems.push(`frame ${address} is not a cell of the ${grid.columns} × ${grid.rows} grid`);
|
|
176
|
+
else if (seen.has(address))
|
|
177
|
+
problems.push(`cell ${address} is used as a frame twice`);
|
|
178
|
+
seen.add(address);
|
|
179
|
+
}
|
|
180
|
+
return problems;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/** The addresses of cells with no pixel whose alpha is at least `threshold`. */
|
|
184
|
+
export function emptyCells(image, grid, threshold) {
|
|
185
|
+
return sheetCells(grid)
|
|
186
|
+
.filter((cell) => {
|
|
187
|
+
const { x, y, width, height } = cellBox(grid, cell);
|
|
188
|
+
for (let py = y; py < y + height; py++)
|
|
189
|
+
for (let px = x; px < x + width; px++)
|
|
190
|
+
if ((image.data[(py * image.width + px) * 4 + 3] ?? 0) >= threshold)
|
|
191
|
+
return false;
|
|
192
|
+
return true;
|
|
193
|
+
})
|
|
194
|
+
.map((cell) => cell.address);
|
|
195
|
+
}
|
|
196
|
+
/** The sheet's grid from the cell size, margin and spacing: as many whole cells as fit. */
|
|
197
|
+
export function gridFor(image, stated) {
|
|
198
|
+
const count = (extent, cell) => Math.max(0, Math.floor((extent - 2 * stated.margin + stated.spacing) / (cell + stated.spacing)));
|
|
199
|
+
return {
|
|
200
|
+
...stated,
|
|
201
|
+
columns: count(image.width, stated.cellWidth),
|
|
202
|
+
rows: count(image.height, stated.cellHeight),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
/** Splits an axis into its runs of gutter and content lines; undefined when nothing is content. */
|
|
206
|
+
function axisRuns(gutter) {
|
|
207
|
+
const runs = [];
|
|
208
|
+
for (const flag of gutter) {
|
|
209
|
+
const last = runs[runs.length - 1];
|
|
210
|
+
if (last !== undefined && last.gutter === flag)
|
|
211
|
+
last.length++;
|
|
212
|
+
else
|
|
213
|
+
runs.push({ gutter: flag, length: 1 });
|
|
214
|
+
}
|
|
215
|
+
const content = runs.filter((run) => !run.gutter).map((run) => run.length);
|
|
216
|
+
if (content.length === 0)
|
|
217
|
+
return undefined;
|
|
218
|
+
const lead = runs[0].gutter ? runs[0].length : 0;
|
|
219
|
+
const last = runs[runs.length - 1];
|
|
220
|
+
const trail = last.gutter ? last.length : 0;
|
|
221
|
+
const inner = runs
|
|
222
|
+
.slice(lead > 0 ? 1 : 0, trail > 0 ? -1 : undefined)
|
|
223
|
+
.filter((run) => run.gutter)
|
|
224
|
+
.map((run) => run.length);
|
|
225
|
+
return { lead, trail, inner, content };
|
|
226
|
+
}
|
|
227
|
+
const allEqual = (values) => values.every((value) => value === values[0]);
|
|
228
|
+
/**
|
|
229
|
+
* The grid a sheet's gutters show. A line is a gutter when every pixel is background: alpha 0 or
|
|
230
|
+
* the top-left pixel's exact colour. Undefined when the gutters are not consistent (one margin all
|
|
231
|
+
* round, one spacing, equal cells) or when there are none to read a cell size from.
|
|
232
|
+
*/
|
|
233
|
+
export function readGrid(image) {
|
|
234
|
+
const { width, height, data } = image;
|
|
235
|
+
if (width === 0 || height === 0)
|
|
236
|
+
return undefined;
|
|
237
|
+
const corner = data.subarray(0, 4);
|
|
238
|
+
const background = (x, y) => {
|
|
239
|
+
const at = (y * width + x) * 4;
|
|
240
|
+
return (data[at + 3] === 0 ||
|
|
241
|
+
(data[at] === corner[0] &&
|
|
242
|
+
data[at + 1] === corner[1] &&
|
|
243
|
+
data[at + 2] === corner[2] &&
|
|
244
|
+
data[at + 3] === corner[3]));
|
|
245
|
+
};
|
|
246
|
+
const columns = Array.from({ length: width }, (_, x) => {
|
|
247
|
+
for (let y = 0; y < height; y++)
|
|
248
|
+
if (!background(x, y))
|
|
249
|
+
return false;
|
|
250
|
+
return true;
|
|
251
|
+
});
|
|
252
|
+
const rows = Array.from({ length: height }, (_, y) => {
|
|
253
|
+
for (let x = 0; x < width; x++)
|
|
254
|
+
if (!background(x, y))
|
|
255
|
+
return false;
|
|
256
|
+
return true;
|
|
257
|
+
});
|
|
258
|
+
const across = axisRuns(columns);
|
|
259
|
+
const down = axisRuns(rows);
|
|
260
|
+
if (across === undefined || down === undefined)
|
|
261
|
+
return undefined;
|
|
262
|
+
const margins = [across.lead, across.trail, down.lead, down.trail];
|
|
263
|
+
if (!allEqual(margins))
|
|
264
|
+
return undefined;
|
|
265
|
+
const margin = margins[0];
|
|
266
|
+
const spacings = [...across.inner, ...down.inner];
|
|
267
|
+
if (!allEqual(spacings))
|
|
268
|
+
return undefined;
|
|
269
|
+
if (!allEqual(across.content) || !allEqual(down.content))
|
|
270
|
+
return undefined;
|
|
271
|
+
if (margin === 0 && across.content.length === 1 && down.content.length === 1)
|
|
272
|
+
return undefined;
|
|
273
|
+
return {
|
|
274
|
+
cellWidth: across.content[0],
|
|
275
|
+
cellHeight: down.content[0],
|
|
276
|
+
margin,
|
|
277
|
+
spacing: spacings[0] ?? 0,
|
|
278
|
+
};
|
|
279
|
+
}
|