Open History to see the last commit
1import { BY_KEY, type Definition } from "./catalogue";
2import {
3 UNIT,
4 WORLD_LIMIT,
5 HEIGHT_LIMIT,
6 BUCKET,
7 type Part,
8 type Position,
9} from "./types";
10export type Bounds = {
11 minx: number;
12 miny: number;
13 minz: number;
14 maxx: number;
15 maxy: number;
16 maxz: number;
17};
18export type Connector = {
19 id: string;
20 type:
21 "stud" | "socket" | "hole" | "shaft" | "gear" | "hingeBase" | "hingeLeaf";
22 at: Position;
23 axis: string;
24 exclusive: boolean;
25 direction?: Position;
26};
27const masks = new Map<string, Position[]>();
28export function dimensions(p: Pick<Part, "key" | "rotation">) {
29 const d = BY_KEY[p.key];
30 return {
31 x: (p.rotation % 2 ? d.d : d.w) * UNIT,
32 y: d.h * 2,
33 z: (p.rotation % 2 ? d.w : d.d) * UNIT,
34 };
35}
36export function bounds(p: Part): Bounds {
37 const d = dimensions(p);
38 return {
39 minx: p.position.x,
40 miny: p.position.y,
41 minz: p.position.z,
42 maxx: p.position.x + d.x,
43 maxy: p.position.y + d.y,
44 maxz: p.position.z + d.z,
45 };
46}
47export function validPart(p: Part) {
48 if (
49 !BY_KEY[p.key] ||
50 !/^#[\da-f]{6}$/i.test(p.color) ||
51 !Number.isInteger(p.rotation) ||
52 p.rotation < 0 ||
53 p.rotation > 3
54 )
55 return false;
56 if (!Object.values(p.position).every(Number.isSafeInteger)) return false;
57 const b = bounds(p);
58 return (
59 b.minx >= -WORLD_LIMIT &&
60 b.minz >= -WORLD_LIMIT &&
61 b.miny >= 0 &&
62 b.maxx <= WORLD_LIMIT &&
63 b.maxz <= WORLD_LIMIT &&
64 b.maxy <= HEIGHT_LIMIT
65 );
66}
67function solid(d: Definition, x: number, y: number, z: number) {
68 const h = d.h / 3,
69 cx = x - d.w / 2,
70 cy = y - h / 2;
71 switch (d.shape) {
72 case "crank":
73 return (
74 (z < 0.6 && Math.hypot(x - 0.5, y - 0.5) > 0.27) ||
75 (z > 0.55 && Math.hypot(x - 0.5, y - 1.5) < 0.18)
76 );
77 case "round":
78 return Math.hypot(x - 0.5, z - 0.5) < 0.48;
79 case "slope":
80 return y < 0.25 + (h - 0.25) * (1 - z / d.d);
81 case "wedge":
82 return x < d.w * (1 - (z / d.d) * 0.7);
83 case "arch":
84 return y > h - 0.5 || x < 0.6 || x > d.w - 0.6;
85 case "window":
86 return y < 0.25 || y > h - 0.25 || x < 0.25 || x > d.w - 0.25;
87 case "beam":
88 return Math.hypot(x - (Math.floor(x) + 0.5), y - 0.5) > 0.27;
89 case "bearing":
90 case "hub":
91 return Math.hypot(x - 0.5, y - 0.5) > 0.27;
92 case "axle":
93 return (
94 (Math.abs(x - 0.5) < 0.14 && Math.abs(y - 0.5) < 0.25) ||
95 (Math.abs(x - 0.5) < 0.25 && Math.abs(y - 0.5) < 0.14)
96 );
97 case "pin":
98 return Math.hypot(x - 0.5, y - 0.5) < 0.17;
99 case "wheel":
100 return Math.hypot(cx, cy) < 1.4 && Math.hypot(cx, cy) > 0.27;
101 case "gear": {
102 const a = Math.atan2(cy, cx),
103 r = Math.hypot(cx, cy);
104 return (
105 r > 0.27 && r < d.w / 2 - 0.13 + 0.1 * Math.cos(a * (d.detail || 12))
106 );
107 }
108 default:
109 return true;
110 }
111}
112function localMask(key: string) {
113 if (masks.has(key)) return masks.get(key)!;
114 const d = BY_KEY[key],
115 result: Position[] = [];
116 for (let x = 0; x < d.w * UNIT; x++)
117 for (let y = 0; y < d.h * 2; y++)
118 for (let z = 0; z < d.d * UNIT; z++)
119 if (solid(d, (x + 0.5) / UNIT, (y + 0.5) / UNIT, (z + 0.5) / UNIT))
120 result.push({ x, y, z });
121 masks.set(key, result);
122 return result;
123}
124export function rotateLocal(p: Part, v: Position, cell = false): Position {
125 const d = BY_KEY[p.key],
126 w = d.w * UNIT,
127 h = d.d * UNIT,
128 k = cell ? 1 : 0;
129 const r =
130 p.rotation === 0
131 ? { x: v.x, z: v.z }
132 : p.rotation === 1
133 ? { x: h - v.z - k, z: v.x }
134 : p.rotation === 2
135 ? { x: w - v.x - k, z: h - v.z - k }
136 : { x: v.z, z: w - v.x - k };
137 return {
138 x: r.x + p.position.x,
139 y: v.y + p.position.y,
140 z: r.z + p.position.z,
141 };
142}
143export function mask(p: Part) {
144 return localMask(p.key).map((v) => rotateLocal(p, v, true));
145}
146const cellKey = (v: Position) => `${v.x},${v.y},${v.z}`;
147export function intersects(a: Bounds, b: Bounds) {
148 return (
149 a.minx < b.maxx &&
150 a.maxx > b.minx &&
151 a.miny < b.maxy &&
152 a.maxy > b.miny &&
153 a.minz < b.maxz &&
154 a.maxz > b.minz
155 );
156}
157export function collision(a: Part, b: Part) {
158 if (!intersects(bounds(a), bounds(b))) return false;
159 const occupied = new Set(mask(a).map(cellKey));
160 return mask(b).some((v) => occupied.has(cellKey(v)));
161}
162export function connectorList(p: Part): Connector[] {
163 const d = BY_KEY[p.key],
164 list: Connector[] = [],
165 axis = p.rotation % 2 ? "x" : "z";
166 const add = (
167 id: string,
168 type: Connector["type"],
169 v: Position,
170 exclusive = true,
171 ) =>
172 list.push({
173 id,
174 type,
175 at: rotateLocal(p, v),
176 axis: type === "stud" || type === "socket" ? "y" : axis,
177 exclusive,
178 });
179 if (d.studs)
180 for (let x = 0; x < d.w; x++)
181 for (let z = 0; z < d.d; z++)
182 add(`s${x}:${z}`, "stud", { x: x * 6 + 3, y: d.h * 2, z: z * 6 + 3 });
183 if (
184 [
185 "brick",
186 "plate",
187 "tile",
188 "round",
189 "slope",
190 "wedge",
191 "arch",
192 "window",
193 "hinge",
194 ].includes(d.shape)
195 )
196 for (let x = 0; x < d.w; x++)
197 for (let z = 0; z < d.d; z++)
198 if (solid(d, x + 0.5, 0.05, z + 0.5))
199 add(`b${x}:${z}`, "socket", { x: x * 6 + 3, y: 0, z: z * 6 + 3 });
200 if (d.shape === "beam")
201 for (let x = 0; x < d.w; x++)
202 add(`h${x}`, "hole", { x: x * 6 + 3, y: 3, z: 3 });
203 if (["bearing", "hub", "wheel", "gear"].includes(d.shape))
204 add("h0", "hole", { x: d.w * 3, y: d.h, z: 3 });
205 if (d.shape === "crank") add("h0", "hole", { x: 3, y: 3, z: 3 });
206 if (d.shape === "pin" || d.shape === "axle")
207 for (let z = 0; z < d.d; z++)
208 add(`a${z}`, "shaft", { x: 3, y: 3, z: z * 6 + 3 }, false);
209 if (d.shape === "gear") {
210 const radius = d.w * 3 - 1;
211 for (const [id, dx, dy] of [
212 ["left", -1, 0],
213 ["right", 1, 0],
214 ["up", 0, 1],
215 ["down", 0, -1],
216 ] as const) {
217 const center = { x: d.w * 3, y: d.h, z: 3 },
218 local = { x: center.x + dx * radius, y: center.y + dy * radius, z: 3 };
219 const origin = rotateLocal(p, { x: 0, y: 0, z: 0 }),
220 vector = rotateLocal(p, { x: dx, y: dy, z: 0 });
221 list.push({
222 id: `g-${id}`,
223 type: "gear",
224 at: rotateLocal(p, local),
225 axis,
226 exclusive: true,
227 direction: { x: vector.x - origin.x, y: dy, z: vector.z - origin.z },
228 });
229 }
230 }
231 if (d.shape === "hinge")
232 list.push({
233 id: "pivot",
234 type: d.detail ? "hingeLeaf" : "hingeBase",
235 at: rotateLocal(p, { x: 6, y: 1, z: d.detail ? 0 : 12 }),
236 axis: p.rotation % 2 ? "z" : "x",
237 exclusive: true,
238 });
239 return list;
240}
241export function mating(a: Connector, b: Connector) {
242 if (
243 a.axis !== b.axis ||
244 a.at.x !== b.at.x ||
245 a.at.y !== b.at.y ||
246 a.at.z !== b.at.z
247 )
248 return false;
249 if (a.type === "gear" && b.type === "gear")
250 return (
251 !!a.direction &&
252 !!b.direction &&
253 a.direction.x === -b.direction.x &&
254 a.direction.y === -b.direction.y &&
255 a.direction.z === -b.direction.z
256 );
257 if (
258 (a.type === "hingeBase" && b.type === "hingeLeaf") ||
259 (b.type === "hingeBase" && a.type === "hingeLeaf")
260 )
261 return true;
262 return (
263 (a.type === "stud" && b.type === "socket") ||
264 (b.type === "stud" && a.type === "socket") ||
265 (a.type === "shaft" && b.type === "hole") ||
266 (b.type === "shaft" && a.type === "hole")
267 );
268}
269export function bucketKeys(parts: Part[]) {
270 const set = new Set<string>();
271 for (const p of parts) {
272 const b = bounds(p);
273 for (
274 let x = Math.floor((b.minx - 2) / BUCKET);
275 x <= Math.floor((b.maxx + 2) / BUCKET);
276 x++
277 )
278 for (
279 let y = Math.floor((b.miny - 2) / BUCKET);
280 y <= Math.floor((b.maxy + 2) / BUCKET);
281 y++
282 )
283 for (
284 let z = Math.floor((b.minz - 2) / BUCKET);
285 z <= Math.floor((b.maxz + 2) / BUCKET);
286 z++
287 )
288 set.add(`${x},${y},${z}`);
289 }
290 return [...set].sort((a, b) => {
291 const aa = a.split(",").map(Number),
292 bb = b.split(",").map(Number);
293 return aa[0] - bb[0] || aa[1] - bb[1] || aa[2] - bb[2];
294 });
295}
296export function enclosing(parts: Part[], pad = 2): Bounds {
297 const bs = parts.map(bounds);
298 return {
299 minx: Math.min(...bs.map((b) => b.minx)) - pad,
300 miny: Math.min(...bs.map((b) => b.miny)) - pad,
301 minz: Math.min(...bs.map((b) => b.minz)) - pad,
302 maxx: Math.max(...bs.map((b) => b.maxx)) + pad,
303 maxy: Math.max(...bs.map((b) => b.maxy)) + pad,
304 maxz: Math.max(...bs.map((b) => b.maxz)) + pad,
305 };
306}