Skip to content
Source
Sign inSign up
samwillis/neonbricks
Open History to see the last commit

design-repair.ts

72 lines3.8 KB

1import { compileBlueprint, type CompiledBlueprint } from "./blueprint";
2import { expandDesign, type Design } from "./design";
3import { bounds } from "./geometry";
4​
5// Count every issue for comparison, even when the bounded diagnostic list is full.
6export function fitScore(compiled: CompiledBlueprint) {
7 return Object.entries(compiled.issueCounts).reduce(
8 (score, [code, count]) =>
9 score + count * (code === "unattached" ? 1 : code === "overlap" ? 4 : 8),
10 0,
11 );
13​
14export function fitRepairFeedback(
15 compiled: CompiledBlueprint,
16 design?: Design,
17 retainedPrevious = false,
18) {
19 // Report coordinates before presentation rotation, matching the source rows.
20 const parts = design
21 ? compileBlueprint({ ...design, front: "+z" }).parts
22 : compiled.parts;
23 const byId = new Map(parts.map((p) => [p.id, p]));
24 const owner = new Map<string, string>();
25 if (design)
26 for (const row of design.pieces)
27 for (const part of expandDesign({ ...design, pieces: [row] }).parts)
28 owner.set(part.id, row.id);
29 const extent = (id: string) => {
30 const part = byId.get(id);
31 if (!part) return { id, row: owner.get(id) || id };
32 const b = bounds(part);
33 return {
34 id,
35 row: owner.get(id) || id,
36 x: [b.minx / 6, b.maxx / 6],
37 y: [b.miny / 2, b.maxy / 2],
38 z: [b.minz / 6, b.maxz / 6],
39 };
40 };
41 const diagnostics = compiled.issues.map((issue) => ({
42 ...issue,
43 pieces: issue.parts.map(extent),
44 }));
45 return `\nFIT REPAIR: correct the remaining geometry in this draft.
46${retainedPrevious ? "The last patch made the fit worse and was discarded. Work from the retained design below, not the discarded patch.\n" : ""}Previous design: ${JSON.stringify(design || compiled.blueprint)}
47Full issue counts: ${JSON.stringify(compiled.issueCounts)}
48Validation errors and actual extents (x/z studs; y plates): ${JSON.stringify(diagnostics)}
49Resolve overlaps first. Compare the named pieces' x, y and z intervals: touching endpoints are fine, occupied volume in common is not. Rotated dimensions are already reflected in the extents. Fix the named SOURCE ROWS; a repeated piece or assembly may expand into several named components.
50For wall corners, shorten or split the competing courses so only one wall owns the corner. For windows and arches, leave a real opening; split repeat rows that run through it. For stair landings, stop the stairs at the deck edge or leave a matching opening in the deck/floor. Do not move an entire wall into the roof or other valid pieces to hide one collision.
51Then connect floating components to supported surfaces or ground them. Ground-level scenery outside the base must start at y=0; use y=1 only when a plate actually supports it. A plate stair tread is only one plate tall: do not stack the next tread across a gap without a supported riser.
52Keep the requested features, palette, silhouette and unaffected rows. Check every changed row against its neighbours; do not trade one fit error for another. ${design ? "Return only a patch; keep unaffected rows." : "Return a complete corrected version 2 design."}`;
54​
55export function fitFailureMessage(compiled?: CompiledBlueprint) {
56 if (!compiled)
57 return "AI returned an incomplete design after three passes. Your model is unchanged. Please try building again.";
58 const overlap = compiled.issueCounts.overlap || 0;
59 const unsupported = compiled.issueCounts.unattached || 0;
60 const problems: string[] = [];
61 if (overlap)
62 problems.push(
63 `${overlap} overlapping piece ${overlap === 1 ? "pair" : "pairs"}`,
64 );
65 if (unsupported)
66 problems.push(
67 `${unsupported} ${unsupported === 1 ? "piece" : "pieces"} without support`,
68 );
69 if (!problems.length)
70 problems.push("invalid piece placements or connections");
71 return `The draft still has ${problems.join(" and ")} after three design passes. Your model is unchanged. Please try building again.`;