@adatechnology/game-interactables-household 0.10.0 → 0.11.0

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/dist/index.js CHANGED
@@ -3099,7 +3099,8 @@ function washDishes(c, basin, rack) {
3099
3099
  label: "use.wash_dishes",
3100
3100
  needs: [...DISH_NEEDS],
3101
3101
  dose: { need: 0 },
3102
- dishes: { ids: [...DISHES], basin: basin.id, rack }
3102
+ dishes: { ids: [...DISHES], basin: basin.id, rack },
3103
+ rinse: { clip: "rinse_dish", seconds: scrubSecondsOf("rinse_dish") }
3103
3104
  });
3104
3105
  }
3105
3106
  function tap2(c, x, y, z, id, partId, basin) {
@@ -5896,15 +5897,706 @@ function createRecorder() {
5896
5897
  };
5897
5898
  return { api, primitives, parts };
5898
5899
  }
5900
+
5901
+ // src/assembly/assemble.ts
5902
+ import { castWithMember } from "@adatechnology/game-interactables";
5903
+
5904
+ // src/assembly/animations.ts
5905
+ import { poseOf } from "@adatechnology/game-interactables";
5906
+
5907
+ // src/assembly/geometry.ts
5908
+ var round = (value, digits = 4) => Math.round(value * 10 ** digits) / 10 ** digits;
5909
+ var sanitize = (id) => String(id).trim().toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "");
5910
+ var rotateX = (v, angle) => [v[0], v[1] * Math.cos(angle) - v[2] * Math.sin(angle), v[1] * Math.sin(angle) + v[2] * Math.cos(angle)];
5911
+ var rotateY = (v, angle) => [v[0] * Math.cos(angle) + v[2] * Math.sin(angle), v[1], -v[0] * Math.sin(angle) + v[2] * Math.cos(angle)];
5912
+ var rotateZ = (v, angle) => [v[0] * Math.cos(angle) - v[1] * Math.sin(angle), v[0] * Math.sin(angle) + v[1] * Math.cos(angle), v[2]];
5913
+ function aabbOfBox(center, size, turn = {}) {
5914
+ const { ry = 0, rx = 0, rz = 0 } = turn;
5915
+ const lo = [Infinity, Infinity, Infinity];
5916
+ const hi = [-Infinity, -Infinity, -Infinity];
5917
+ for (const sx of [-0.5, 0.5])
5918
+ for (const sy of [-0.5, 0.5])
5919
+ for (const sz of [-0.5, 0.5]) {
5920
+ let v = [sx * size[0], sy * size[1], sz * size[2]];
5921
+ if (rz) v = rotateZ(v, rz);
5922
+ if (rx) v = rotateX(v, rx);
5923
+ if (ry) v = rotateY(v, ry);
5924
+ for (let k = 0; k < 3; k++) {
5925
+ lo[k] = Math.min(lo[k], center[k] + v[k]);
5926
+ hi[k] = Math.max(hi[k], center[k] + v[k]);
5927
+ }
5928
+ }
5929
+ return [...lo, ...hi];
5930
+ }
5931
+ function latheShape(args) {
5932
+ const [, material, x, y, z, points] = args;
5933
+ const radius = Math.max(...points.map((q) => q[0]));
5934
+ const heights = points.map((q) => q[1]);
5935
+ const lo = Math.min(...heights);
5936
+ const hi = Math.max(...heights);
5937
+ let moment = 0;
5938
+ for (let i = 0; i < points.length; i++) {
5939
+ const [r0, y0] = points[i];
5940
+ const [r1, y1] = points[(i + 1) % points.length];
5941
+ moment += (r0 * y1 - r1 * y0) * (r0 + r1) / 6;
5942
+ }
5943
+ return { key: material, aabb: [x - radius, y + lo, z - radius, x + radius, y + hi, z + radius], vol: Math.max(1e-6, 2 * Math.PI * Math.abs(moment)) };
5944
+ }
5945
+ function describePrimitive(fn, args) {
5946
+ if (fn === "box") {
5947
+ const [, material, x2, y2, z2, w2, h2, d, , ry2 = 0, rx = 0, rz = 0] = args;
5948
+ return { key: material, aabb: aabbOfBox([x2, y2 + h2 / 2, z2], [w2, h2, d], { ry: ry2, rx, rz }), vol: w2 * h2 * d };
5949
+ }
5950
+ if (fn === "cylinder") {
5951
+ const [, material, x2, y2, z2, r, h2, , rx = 0, rz = 0, shape] = args;
5952
+ const tilted = rx || rz;
5953
+ const share = shape === "cone" ? 1 / 3 : shape === "sphere" ? 2 / 3 : 1;
5954
+ return { key: material, aabb: aabbOfBox([x2, tilted ? y2 : y2 + h2 / 2, z2], [2 * r, h2, 2 * r], { rx, rz }), vol: Math.PI * r * r * h2 * share };
5955
+ }
5956
+ if (fn === "plane") {
5957
+ const [, material, x2, y2, z2, w2, h2, , ry2 = 0, rx = 0, rz = 0] = args;
5958
+ return { key: material, aabb: aabbOfBox([x2, y2, z2], [w2, h2, 0.012], { ry: ry2, rx, rz }), vol: w2 * h2 * 0.012 };
5959
+ }
5960
+ if (fn === "lathe") return latheShape(args);
5961
+ const [, x, y, z, w, h, , ry = 0] = args;
5962
+ return { key: "plant", aabb: aabbOfBox([x, y + h / 2, z], [w, h, w * 0.2], { ry }), vol: w * h * w * 0.1 };
5963
+ }
5964
+ var unionBox = (a, b) => [Math.min(a[0], b[0]), Math.min(a[1], b[1]), Math.min(a[2], b[2]), Math.max(a[3], b[3]), Math.max(a[4], b[4]), Math.max(a[5], b[5])];
5965
+ var thinnestOf = (b) => Math.min(b[3] - b[0], b[4] - b[1], b[5] - b[2]);
5966
+ var boxVolume = (b) => Math.max(1e-9, (b[3] - b[0]) * (b[4] - b[1]) * (b[5] - b[2]));
5967
+ var centre = (b) => [(b[0] + b[3]) / 2, (b[1] + b[4]) / 2, (b[2] + b[5]) / 2];
5968
+ function coverOf(skin, under) {
5969
+ const sizes = [skin[3] - skin[0], skin[4] - skin[1], skin[5] - skin[2]];
5970
+ const thin = sizes.indexOf(Math.min(...sizes));
5971
+ let share = 1;
5972
+ for (let k = 0; k < 3; k++) {
5973
+ if (k === thin) continue;
5974
+ const overlap = Math.max(0, Math.min(skin[k + 3], under[k + 3]) - Math.max(skin[k], under[k]));
5975
+ share *= overlap / Math.max(sizes[k], 1e-9);
5976
+ }
5977
+ return share;
5978
+ }
5979
+ function boxGap(a, b) {
5980
+ const dx = Math.max(0, a[0] - b[3], b[0] - a[3]);
5981
+ const dy = Math.max(0, a[1] - b[4], b[1] - a[4]);
5982
+ const dz = Math.max(0, a[2] - b[5], b[2] - a[5]);
5983
+ return Math.hypot(dx, dy, dz);
5984
+ }
5985
+ var SOLID_MIN = 2e-3;
5986
+ function solidsOf(prims, box) {
5987
+ if (prims.length < 2) return void 0;
5988
+ const c = centre(box);
5989
+ return prims.map((p) => {
5990
+ const at = centre(p.aabb);
5991
+ return {
5992
+ center: [round(at[0] - c[0]), round(at[1] - c[1]), round(at[2] - c[2])],
5993
+ size: [0, 1, 2].map((k) => round(Math.max(SOLID_MIN, p.aabb[k + 3] - p.aabb[k])))
5994
+ };
5995
+ });
5996
+ }
5997
+ function primitiveBoxInObject(mover, prim) {
5998
+ let box;
5999
+ for (const sx of [prim.aabb[0], prim.aabb[3]])
6000
+ for (const sy of [prim.aabb[1], prim.aabb[4]])
6001
+ for (const sz of [prim.aabb[2], prim.aabb[5]]) {
6002
+ const v = mover.ry ? rotateY([sx, sy, sz], mover.ry) : [sx, sy, sz];
6003
+ const w = [v[0] + mover.pivot[0], v[1] + mover.pivot[1], v[2] + mover.pivot[2]];
6004
+ box = box ? unionBox(box, [...w, ...w]) : [...w, ...w];
6005
+ }
6006
+ return box;
6007
+ }
6008
+ function moverBox(mover) {
6009
+ let box;
6010
+ for (const p of mover.prims) {
6011
+ const b = primitiveBoxInObject(mover, p);
6012
+ box = box ? unionBox(box, b) : b;
6013
+ }
6014
+ return box;
6015
+ }
6016
+
6017
+ // src/assembly/animations.ts
6018
+ var SOUND = { door: "door", drawer: "drawer", lid: "lid", flap: "flap", knob: "knob", button: "click", rocker: "click", breaker: "click" };
6019
+ var OWN_CLIPS_MAX = 8;
6020
+ function quaternionOf(motion) {
6021
+ const pose = poseOf(motion, 1);
6022
+ const [x, y, z] = pose.rotation.axis;
6023
+ const length = Math.hypot(x, y, z) || 1;
6024
+ const s = Math.sin(pose.rotation.angle / 2);
6025
+ return { position: [...pose.position], quaternion: [x / length * s, y / length * s, z / length * s, Math.cos(pose.rotation.angle / 2)] };
6026
+ }
6027
+ function eulerOf([x, y, z, w]) {
6028
+ const m13 = 2 * (x * z + w * y);
6029
+ const ey = Math.asin(Math.max(-1, Math.min(1, m13)));
6030
+ const ex = Math.abs(m13) < 0.9999999 ? Math.atan2(-2 * (y * z - w * x), 1 - 2 * (x * x + y * y)) : Math.atan2(2 * (y * z + w * x), 1 - 2 * (x * x + z * z));
6031
+ const ez = Math.abs(m13) < 0.9999999 ? Math.atan2(-2 * (x * y - w * z), 1 - 2 * (y * y + z * z)) : 0;
6032
+ return [round(ex), round(ey), round(ez)];
6033
+ }
6034
+ function partMotionPose(motion, ry) {
6035
+ const { position: p, quaternion } = quaternionOf(motion);
6036
+ const [x, y, z, w] = quaternion;
6037
+ const px = p[0] * Math.cos(ry) + p[2] * Math.sin(ry);
6038
+ const pz = -p[0] * Math.sin(ry) + p[2] * Math.cos(ry);
6039
+ const position = [round(px), round(p[1]), round(pz)];
6040
+ const along = [x, y, z].map((c) => Math.abs(c) > 1e-9);
6041
+ if (along.filter(Boolean).length <= 1) {
6042
+ const turn = [x, y, z].map((c) => Math.abs(c) > 1e-9 ? 2 * Math.atan2(c, w) : 0);
6043
+ return { position, rotation: [round(turn[0]), round(turn[1]), round(turn[2])] };
6044
+ }
6045
+ return { position, rotation: eulerOf(quaternion) };
6046
+ }
6047
+ function staged(list) {
6048
+ const bySource = new Map(list.map((p) => [p.source, p]));
6049
+ const start = (p) => {
6050
+ const before = p.requires ? bySource.get(p.requires) : void 0;
6051
+ return before ? start(before) + 0.5 : 0;
6052
+ };
6053
+ return list.map((p) => ({ ...p, start: start(p), end: start(p) + p.seconds }));
6054
+ }
6055
+ function clipOf({ id, list, closing, sound }) {
6056
+ const parts = staged(list);
6057
+ const duration = Math.max(...parts.map((p) => p.end));
6058
+ const times = [.../* @__PURE__ */ new Set([0, duration, ...parts.flatMap((p) => [p.start, p.end])])].sort((a, b) => a - b);
6059
+ const keys = times.map((t) => ({
6060
+ t: round(t, 3),
6061
+ parts: Object.fromEntries(
6062
+ parts.map((p) => {
6063
+ const share = Math.max(0, Math.min(1, (t - p.start) / (p.end - p.start)));
6064
+ const f = closing ? 1 - share : share;
6065
+ return [p.id, { position: p.pose.position.map((v) => round(v * f)), rotation: p.pose.rotation.map((v) => round(v * f)) }];
6066
+ })
6067
+ )
6068
+ }));
6069
+ return { id, duration: round(duration, 3), keys, events: [{ t: 0, type: "sound", data: { key: `${sound}_${closing ? "close" : "open"}` } }] };
6070
+ }
6071
+ function buildAnimations({ spec, ids, present }) {
6072
+ const movers = (spec.parts ?? []).filter((p) => p.motion.kind !== "scale" && present.has(p.id));
6073
+ if (movers.length === 0) return [];
6074
+ const info = movers.map((p) => {
6075
+ const kind = p.motion.kind;
6076
+ const on = kind === "knob" || kind === "rocker";
6077
+ const piece2 = p.destruction?.piece ?? (kind === "slide" ? "drawer" : "door");
6078
+ return {
6079
+ source: p.id,
6080
+ id: ids.get(p.id) ?? p.id,
6081
+ on,
6082
+ pose: partMotionPose(p.motion, p.ry ?? 0),
6083
+ seconds: on ? 0.4 : kind === "slide" ? 0.8 : 1.1,
6084
+ sound: SOUND[piece2] ?? "door",
6085
+ requires: p.requires
6086
+ };
6087
+ });
6088
+ const clips = [];
6089
+ const doors = info.filter((p) => !p.on);
6090
+ const knobs = info.filter((p) => p.on);
6091
+ if (doors.length) clips.push(clipOf({ id: "open_all", list: doors, closing: false, sound: "door" }), clipOf({ id: "close_all", list: doors, closing: true, sound: "door" }));
6092
+ if (knobs.length) clips.push(clipOf({ id: "on_all", list: knobs, closing: false, sound: "switch" }), clipOf({ id: "off_all", list: knobs, closing: true, sound: "switch" }));
6093
+ if (info.length > OWN_CLIPS_MAX) return clips;
6094
+ for (const p of info) {
6095
+ clips.push(
6096
+ clipOf({ id: `${p.on ? "on" : "open"}_${p.id}`, list: [p], closing: false, sound: p.sound }),
6097
+ clipOf({ id: `${p.on ? "off" : "close"}_${p.id}`, list: [p], closing: true, sound: p.sound })
6098
+ );
6099
+ }
6100
+ return clips;
6101
+ }
6102
+
6103
+ // src/assembly/cluster.ts
6104
+ var MAX_PIECES = {
6105
+ service_entrance: 32,
6106
+ service_mast: 24,
6107
+ bbq: 30,
6108
+ clothesline: 24,
6109
+ dog_house: 24,
6110
+ gas_bottle: 6,
6111
+ microwave: 10,
6112
+ washing_machine: 12,
6113
+ tv: 8,
6114
+ fridge_compact: 12,
6115
+ utility_sink: 12,
6116
+ wall_socket: 3,
6117
+ wall_switch: 3
6118
+ };
6119
+ var DEFAULT_MAX_PIECES = 18;
6120
+ var STRETCH_DIAMETERS = 2.5;
6121
+ var SKIN_MAX = 4e-3;
6122
+ var SKIN_GAP = 1e-3;
6123
+ var SKIN_COVER = 0.9;
6124
+ var SPECK = 0.02;
6125
+ function layInStretches(statics) {
6126
+ let member = 0;
6127
+ for (let i = statics.length - 1; i >= 0; i--) {
6128
+ const prim = statics[i];
6129
+ if (prim.fn !== "cylinder") continue;
6130
+ const [, , , y, , r, h, , rx = 0, rz = 0, shape] = prim.args;
6131
+ if (rx || rz || shape !== void 0 && shape !== "cyl" && shape !== "cyl6" || h < 2 * STRETCH_DIAMETERS * 2 * r) continue;
6132
+ const count = Math.floor(h / (STRETCH_DIAMETERS * 2 * r));
6133
+ const id = member++;
6134
+ const stretches = Array.from({ length: count }, (_, k) => {
6135
+ const args = prim.args.slice();
6136
+ args[3] = y + h * k / count;
6137
+ args[6] = h / count;
6138
+ return { fn: prim.fn, args, member: id, ...describePrimitive(prim.fn, args) };
6139
+ });
6140
+ statics.splice(i, 1, ...stretches);
6141
+ }
6142
+ }
6143
+ var glassy = (p) => p.keys.has("glass") || p.keys.has("emissive");
6144
+ var sizeOf = (p) => Math.max(p.box[3] - p.box[0], p.box[4] - p.box[1], p.box[5] - p.box[2]);
6145
+ function merge(into, from) {
6146
+ into.indices.push(...from.indices);
6147
+ into.box = unionBox(into.box, from.box);
6148
+ into.vol += from.vol;
6149
+ for (const k of from.keys) into.keys.add(k);
6150
+ for (const [k, v] of Object.entries(from.area)) into.area[k] = (into.area[k] ?? 0) + v;
6151
+ }
6152
+ function hostOf(pieces, small) {
6153
+ let best;
6154
+ let bestScore = Infinity;
6155
+ for (const q of pieces) {
6156
+ if (q === small || glassy(q) !== glassy(small)) continue;
6157
+ const score = boxGap(small.box, q.box) * 1e3 - Math.log(boxVolume(q.box));
6158
+ if (score < bestScore) {
6159
+ bestScore = score;
6160
+ best = q;
6161
+ }
6162
+ }
6163
+ return best;
6164
+ }
6165
+ function clusterStatics({ type, statics, castWith }) {
6166
+ const max = MAX_PIECES[type] ?? DEFAULT_MAX_PIECES;
6167
+ let pieces = statics.map((p, i) => ({ indices: [i], box: p.aabb.slice(), vol: p.vol, keys: /* @__PURE__ */ new Set([p.key]), area: { [p.key]: p.vol } }));
6168
+ const join = (host, piece2) => {
6169
+ merge(host, piece2);
6170
+ pieces = pieces.filter((q) => q !== piece2);
6171
+ };
6172
+ const absorb = (small) => {
6173
+ const host = hostOf(pieces, small);
6174
+ if (!host) return false;
6175
+ join(host, small);
6176
+ return true;
6177
+ };
6178
+ for (const p of pieces.slice()) {
6179
+ if (glassy(p) || pieces.length < 2) continue;
6180
+ const thin = thinnestOf(p.box);
6181
+ if (thin > SKIN_MAX) continue;
6182
+ const host = pieces.find((q) => q !== p && !glassy(q) && thinnestOf(q.box) > thin && boxGap(p.box, q.box) <= SKIN_GAP && coverOf(p.box, q.box) >= SKIN_COVER);
6183
+ if (host) join(host, p);
6184
+ }
6185
+ for (const p of pieces.slice()) {
6186
+ if (p.indices.length !== 1 || statics[p.indices[0]].member !== void 0) continue;
6187
+ const host = pieces.find((q) => q !== p && q.indices.some((i) => statics[i].member !== void 0) && castWith?.(q.box, p.box));
6188
+ if (host) join(host, p);
6189
+ }
6190
+ for (const p of pieces.slice()) if (sizeOf(p) < SPECK && pieces.length > 1 && !glassy(p)) absorb(p);
6191
+ while (pieces.length > max) {
6192
+ const candidates = pieces.filter((p) => !glassy(p) || pieces.filter(glassy).length > 3);
6193
+ const small = (candidates.length ? candidates : pieces).reduce((m, p) => boxVolume(p.box) < boxVolume(m.box) ? p : m);
6194
+ if (!absorb(small)) break;
6195
+ }
6196
+ return pieces.sort((a, b) => boxVolume(b.box) - boxVolume(a.box));
6197
+ }
6198
+
6199
+ // src/assembly/draw-recorder.ts
6200
+ var KINDS = ["box", "cylinder", "plane", "leaf", "lathe"];
6201
+ var NOOP_API = { part: (_id, _pivot, _ry, draw) => draw(), box() {
6202
+ }, cylinder() {
6203
+ }, plane() {
6204
+ }, leaf() {
6205
+ }, light() {
6206
+ }, lathe() {
6207
+ } };
6208
+ var callOf = (api, kind) => api[kind];
6209
+ function createDrawRecorder(inner) {
6210
+ const statics = [];
6211
+ const movers = /* @__PURE__ */ new Map();
6212
+ const stack = [];
6213
+ const api = {
6214
+ light: (...args) => inner.light?.(...args),
6215
+ part(id, pivot, ry, draw) {
6216
+ stack.push(id);
6217
+ movers.set(id, { id, pivot: [...pivot], ry: ry || 0, prims: [] });
6218
+ inner.part(id, pivot, ry, draw);
6219
+ stack.pop();
6220
+ }
6221
+ };
6222
+ for (const fn of KINDS) {
6223
+ ;
6224
+ api[fn] = (...args) => {
6225
+ const prim = { fn, args, ...describePrimitive(fn, args) };
6226
+ const open = stack[stack.length - 1];
6227
+ if (open === void 0) {
6228
+ statics.push(prim);
6229
+ return;
6230
+ }
6231
+ movers.get(open)?.prims.push(prim);
6232
+ callOf(inner, fn)(...args);
6233
+ };
6234
+ }
6235
+ const flush = (pieces, laid2 = statics) => {
6236
+ for (const piece2 of pieces) {
6237
+ inner.part(piece2.id, [0, 0, 0], 0, () => {
6238
+ for (const i of piece2.indices) callOf(inner, laid2[i].fn)(...laid2[i].args);
6239
+ });
6240
+ }
6241
+ };
6242
+ return { api, statics, movers, flush };
6243
+ }
6244
+
6245
+ // src/assembly/fluid.ts
6246
+ var BOWL_FILL_MAX = 0.7;
6247
+ var WATER_TEMPERATURE = 18;
6248
+ var FLUIDS = {
6249
+ sink_kitchen: { liters: 4, capacity: 14, size: [0.5, 0.16, 0.36], opening: 0.2 },
6250
+ sink_bathroom: { liters: 2, capacity: 6, size: [0.34, 0.12, 0.28], opening: 0.15 },
6251
+ utility_sink: { liters: 8, capacity: 28, size: [0.5, 0.25, 0.4], opening: 0.22 },
6252
+ toilet: { liters: 3, capacity: 6, size: [0.3, 0.18, 0.3], opening: 0.14 }
6253
+ };
6254
+ function fluidOf({ type, basin, parts, basinPiece }) {
6255
+ const fixture = FLUIDS[type];
6256
+ if (!fixture) return void 0;
6257
+ if (basin) return bowlFluid(fixture, basin, parts);
6258
+ const part = parts.find((p) => p.id === basinPiece);
6259
+ if (!part) return void 0;
6260
+ const top = part.position[1] + part.shape.size[1] / 2;
6261
+ return {
6262
+ container: { capacity: fixture.capacity, liquid: "water", liters: fixture.liters, temperature: WATER_TEMPERATURE, shape: { kind: "box", size: [...fixture.size] }, opening: fixture.opening },
6263
+ part: part.id,
6264
+ uses: ["pour"],
6265
+ offset: [part.position[0], round(top - fixture.size[1] - 0.01), part.position[2]]
6266
+ };
6267
+ }
6268
+ var isInside = (offset, part) => [0, 1, 2].every((k) => Math.abs(offset[k] + (k === 1 ? 5e-3 : 0) - part.position[k]) <= part.shape.size[k] / 2 + 0.01);
6269
+ function bowlFluid(fixture, bowl, parts) {
6270
+ const size = [0, 1, 2].map((k) => round(bowl.to[k] - bowl.from[k]));
6271
+ const offset = [round((bowl.from[0] + bowl.to[0]) / 2), round(bowl.from[1]), round((bowl.from[2] + bowl.to[2]) / 2)];
6272
+ const radius = round(Math.min(size[0], size[2]) / 2);
6273
+ const isRound = bowl.shape === "round";
6274
+ const capacity = round((isRound ? Math.PI * radius * radius * size[1] : size[0] * size[1] * size[2]) * 1e3, 2);
6275
+ const part = parts.find((p) => isInside(offset, p)) ?? parts[0];
6276
+ return {
6277
+ container: {
6278
+ capacity,
6279
+ liquid: "water",
6280
+ liters: Math.min(fixture.liters, round(capacity * BOWL_FILL_MAX, 2)),
6281
+ temperature: WATER_TEMPERATURE,
6282
+ shape: isRound ? { kind: "cylinder", radius, height: size[1] } : { kind: "box", size },
6283
+ opening: Math.min(fixture.opening, radius)
6284
+ },
6285
+ ...part ? { part: part.id } : {},
6286
+ uses: ["pour"],
6287
+ offset
6288
+ };
6289
+ }
6290
+
6291
+ // src/assembly/joints.ts
6292
+ var TOUCH = 0.01;
6293
+ var DEGREE_MAX = 5;
6294
+ var STRETCH_JOINT = { reinforced: "rebar", plain: "weld" };
6295
+ function createGroups(ids) {
6296
+ const parent = new Map(ids.map((id) => [id, id]));
6297
+ const find = (id) => {
6298
+ const up = parent.get(id) ?? id;
6299
+ if (up === id) return id;
6300
+ const root = find(up);
6301
+ parent.set(id, root);
6302
+ return root;
6303
+ };
6304
+ return { find, join: (a, b) => void parent.set(find(a), find(b)) };
6305
+ }
6306
+ function jointsOf({ spec, pieces, movers, statics }) {
6307
+ const body = spec.destruction ?? {};
6308
+ const cage = body.reinforcement;
6309
+ const bodyKind = body.joint ?? "screw";
6310
+ const memberOf = (piece2) => piece2.indices.map((i) => statics[i].member).find((member) => member !== void 0);
6311
+ const kindOf = (a, b) => {
6312
+ const member = memberOf(a);
6313
+ if (member !== void 0 && member === memberOf(b)) return cage ? STRETCH_JOINT.reinforced : STRETCH_JOINT.plain;
6314
+ if (a.material.includes("glass") || b.material.includes("glass")) return "clip";
6315
+ if (a.material === "foam" !== (b.material === "foam")) return "tie";
6316
+ return cage && (a.material !== body.material || b.material !== body.material) ? "bolt" : bodyKind;
6317
+ };
6318
+ const joints = [];
6319
+ const degree = /* @__PURE__ */ new Map();
6320
+ const groups = createGroups(pieces.map((p) => p.id));
6321
+ const edges = [];
6322
+ for (let i = 0; i < pieces.length; i++)
6323
+ for (let j = i + 1; j < pieces.length; j++) {
6324
+ const gap = boxGap(pieces[i].box, pieces[j].box);
6325
+ if (gap <= TOUCH) edges.push({ a: pieces[i], b: pieces[j], gap });
6326
+ }
6327
+ edges.sort((x, y) => x.gap - y.gap || Math.min(boxVolume(y.a.box), boxVolume(y.b.box)) - Math.min(boxVolume(x.a.box), boxVolume(x.b.box)));
6328
+ for (const e of edges) {
6329
+ if ((degree.get(e.a.id) ?? 0) >= DEGREE_MAX || (degree.get(e.b.id) ?? 0) >= DEGREE_MAX) continue;
6330
+ degree.set(e.a.id, (degree.get(e.a.id) ?? 0) + 1);
6331
+ degree.set(e.b.id, (degree.get(e.b.id) ?? 0) + 1);
6332
+ groups.join(e.a.id, e.b.id);
6333
+ joints.push({ a: e.a.id, b: e.b.id, kind: kindOf(e.a, e.b) });
6334
+ }
6335
+ joints.push(...bridgesOf(pieces, groups, kindOf));
6336
+ for (const m of movers) {
6337
+ const host = hostOf2(pieces, m);
6338
+ if (host) joints.push({ a: m.id, b: host.id, kind: m.tag.joint ?? "hinge" });
6339
+ }
6340
+ return joints;
6341
+ }
6342
+ function bridgesOf(pieces, groups, kindOf) {
6343
+ const bridges = [];
6344
+ while (new Set(pieces.map((p) => groups.find(p.id))).size > 1) {
6345
+ let best;
6346
+ for (const a of pieces)
6347
+ for (const b of pieces) {
6348
+ if (groups.find(a.id) === groups.find(b.id)) continue;
6349
+ const gap = boxGap(a.box, b.box);
6350
+ if (!best || gap < best.gap) best = { a, b, gap };
6351
+ }
6352
+ if (!best) break;
6353
+ groups.join(best.a.id, best.b.id);
6354
+ bridges.push({ a: best.a.id, b: best.b.id, kind: kindOf(best.a, best.b) });
6355
+ }
6356
+ return bridges;
6357
+ }
6358
+ function hostOf2(pieces, mover) {
6359
+ let best = pieces[0];
6360
+ let bestScore = Infinity;
6361
+ for (const h of pieces) {
6362
+ const score = boxGap(mover.box, h.box) * 1e3 - Math.log(boxVolume(h.box));
6363
+ if (score < bestScore) {
6364
+ bestScore = score;
6365
+ best = h;
6366
+ }
6367
+ }
6368
+ return best;
6369
+ }
6370
+
6371
+ // src/assembly/materials.ts
6372
+ var WOOD_FAMILY = /* @__PURE__ */ new Set(["wood_soft", "wood_hard", "mdf", "plywood"]);
6373
+ var METAL_FAMILY = /* @__PURE__ */ new Set(["steel", "aluminium", "cast_iron", "copper"]);
6374
+ var KEY_OVERRIDES = {
6375
+ shower: { plain: "ceramic", metal: "aluminium", glass: "tempered_glass" },
6376
+ sink_kitchen: { wood: "mdf" },
6377
+ sink_bathroom: { wood: "mdf" },
6378
+ mailbox: { wood: "steel" },
6379
+ sofa: { metal: "spring_steel" },
6380
+ armchair: { metal: "spring_steel" },
6381
+ bed_double: { metal: "spring_steel" },
6382
+ bed_single: { metal: "spring_steel" },
6383
+ box_bed: { metal: "spring_steel" }
6384
+ };
6385
+ var DENSITY = {
6386
+ steel: 7850,
6387
+ aluminium: 2700,
6388
+ cast_iron: 7200,
6389
+ copper: 8900,
6390
+ wood_soft: 550,
6391
+ wood_hard: 750,
6392
+ mdf: 700,
6393
+ plywood: 600,
6394
+ glass: 2500,
6395
+ tempered_glass: 2500,
6396
+ ceramic: 2300,
6397
+ plastic: 1e3,
6398
+ rubber: 1100,
6399
+ fabric: 250,
6400
+ foam: 40,
6401
+ brick: 1800,
6402
+ concrete: 2400,
6403
+ foliage: 100
6404
+ };
6405
+ var DEFAULT_DENSITY = 800;
6406
+ var THICKNESS = { steel: 12e-4, aluminium: 1e-3, copper: 2e-3, plastic: 3e-3, ceramic: 0.012, glass: 4e-3, tempered_glass: 6e-3 };
6407
+ function materialFor({ type, spec, key }) {
6408
+ const body = spec.destruction?.material ?? "plastic";
6409
+ const over = KEY_OVERRIDES[type]?.[key];
6410
+ if (over) return over;
6411
+ if (key === "glass" || key === "emissive") return body === "tempered_glass" ? "tempered_glass" : "glass";
6412
+ if (key === "metal") return METAL_FAMILY.has(body) ? body : "steel";
6413
+ if (key === "wood") return WOOD_FAMILY.has(body) ? body : "wood_soft";
6414
+ if (key === "foam") return "foam";
6415
+ if (key === "fabric") return "fabric";
6416
+ if (key === "plant") return "foliage";
6417
+ return body;
6418
+ }
6419
+
6420
+ // src/assembly/parts.ts
6421
+ var GROUND_TOUCH = 0.02;
6422
+ var CAGE_UNIT = 0.1;
6423
+ var CAGE_MIN = 0.1;
6424
+ var UPHOLSTERY_COVER = 2e-3;
6425
+ var GLASS_HP = { glass: 20, tempered_glass: 40 };
6426
+ function weightOf(piece2) {
6427
+ const [dx, dy, dz] = [piece2.box[3] - piece2.box[0], piece2.box[4] - piece2.box[1], piece2.box[5] - piece2.box[2]];
6428
+ const thin = THICKNESS[piece2.material];
6429
+ return (thin ? Math.min(piece2.vol, 2 * (dx * dy + dy * dz + dz * dx) * thin) : piece2.vol) * (DENSITY[piece2.material] ?? DEFAULT_DENSITY);
6430
+ }
6431
+ function shapeOf(type, piece2) {
6432
+ const size = [0, 1, 2].map((k) => Math.max(4e-3, piece2.box[k + 3] - piece2.box[k]));
6433
+ const fill = Math.max(0.03, Math.min(1, piece2.vol / (size[0] * size[1] * size[2])));
6434
+ return {
6435
+ id: piece2.id,
6436
+ material: piece2.material,
6437
+ shape: { kind: "box", size: size.map((v) => round(v)) },
6438
+ position: centre(piece2.box).map((v) => round(v)),
6439
+ fill: round(piece2.material === "foam" || piece2.material === "fabric" ? fill * 0.5 : fill, 3),
6440
+ tags: [type, ...piece2.mover && piece2.tag.piece ? [piece2.tag.piece] : []],
6441
+ ...piece2.solids ? { solids: piece2.solids } : {}
6442
+ };
6443
+ }
6444
+ function bodyBuildOf(piece2, body) {
6445
+ if (piece2.mover || piece2.material !== body.material) return {};
6446
+ const side = Math.min(piece2.box[3] - piece2.box[0], piece2.box[4] - piece2.box[1], piece2.box[5] - piece2.box[2]);
6447
+ const cage = body.reinforcement;
6448
+ if (cage && side >= CAGE_MIN) {
6449
+ return {
6450
+ reinforcement: { material: cage.material, spacing: cage.spacing, diameter: cage.diameter },
6451
+ units: { size: [CAGE_UNIT, CAGE_UNIT, round(side)], bond: "stack", mortar: piece2.material },
6452
+ structure: { role: "column" }
6453
+ };
6454
+ }
6455
+ const laid2 = body.units;
6456
+ return laid2 ? { units: { size: [laid2.size[0], laid2.size[1], laid2.size[2]], bond: laid2.bond, mortar: laid2.mortar } } : {};
6457
+ }
6458
+ function partsOf({ type, spec, pieces, all, massKg, planted }) {
6459
+ const body = spec.destruction ?? {};
6460
+ const totalWeight = all.reduce((sum, piece2) => sum + weightOf(piece2), 0) || 1;
6461
+ const parts = all.map((piece2) => {
6462
+ const extra = {};
6463
+ if (planted && !piece2.mover && piece2.box[1] <= GROUND_TOUCH) extra.anchored = true;
6464
+ Object.assign(extra, bodyBuildOf(piece2, body));
6465
+ const own = piece2.mover ? piece2.tag : void 0;
6466
+ if (own?.explodes) extra.explodes = { ...own.explodes };
6467
+ if (own?.contains) extra.contains = { ...own.contains };
6468
+ if (massKg) extra.mass = round(Math.max(0.01, massKg * weightOf(piece2) / totalWeight), 3);
6469
+ if (!piece2.mover && piece2.key === "foam") extra.layers = [{ material: "fabric", thickness: UPHOLSTERY_COVER, faces: "all" }];
6470
+ const hp = GLASS_HP[piece2.material];
6471
+ if (hp) extra.hp = hp;
6472
+ const thin = THICKNESS[piece2.material];
6473
+ if (thin) extra.thickness = Math.min(thin, Math.max(5e-4, Math.min(piece2.box[3] - piece2.box[0], piece2.box[4] - piece2.box[1], piece2.box[5] - piece2.box[2])));
6474
+ return Object.assign(shapeOf(type, piece2), extra);
6475
+ });
6476
+ const shell2 = pieces.length ? parts.find((p) => p.id === pieces[0].id) : void 0;
6477
+ if (shell2 && body.explodes) shell2.explodes = { ...body.explodes };
6478
+ if (shell2 && body.contains) shell2.contains = { ...body.contains };
6479
+ return parts;
6480
+ }
6481
+
6482
+ // src/assembly/pieces.ts
6483
+ function createIds() {
6484
+ const used = /* @__PURE__ */ new Set();
6485
+ return (base) => {
6486
+ let id = base;
6487
+ for (let n = 2; used.has(id); n++) id = `${base}_${n}`;
6488
+ used.add(id);
6489
+ return id;
6490
+ };
6491
+ }
6492
+ function bodyPiecesOf({ type, spec, clusters, statics, unique }) {
6493
+ const pieces = clusters.map((c) => {
6494
+ const dominant = Object.entries(c.area).sort((a, b) => b[1] - a[1])[0][0];
6495
+ const material = materialFor({ type, spec, key: dominant });
6496
+ const solids = solidsOf(c.indices.map((i) => statics[i]), c.box);
6497
+ return { id: unique(dominant === "emissive" ? "screen" : material), indices: c.indices, box: c.box, vol: c.vol, material, key: dominant, mover: false, ...solids ? { solids } : {} };
6498
+ });
6499
+ return pieces.length ? pieces : wirePiecesOf({ spec, unique });
6500
+ }
6501
+ function wirePiecesOf({ spec, unique }) {
6502
+ const pieces = [];
6503
+ for (const w of spec.wires ?? []) {
6504
+ if (!w.points || w.points.length < 2) continue;
6505
+ const r = w.radius ?? 0.01;
6506
+ const lo = [Infinity, Infinity, Infinity];
6507
+ const hi = [-Infinity, -Infinity, -Infinity];
6508
+ let length = 0;
6509
+ w.points.forEach((q, i) => {
6510
+ for (let k = 0; k < 3; k++) {
6511
+ lo[k] = Math.min(lo[k], q[k] - r);
6512
+ hi[k] = Math.max(hi[k], q[k] + r);
6513
+ }
6514
+ if (i) length += Math.hypot(q[0] - w.points[i - 1][0], q[1] - w.points[i - 1][1], q[2] - w.points[i - 1][2]);
6515
+ });
6516
+ const material = spec.destruction?.material ?? "plastic";
6517
+ pieces.push({ id: unique(w.kind === "conduit" ? "conduit" : w.kind), indices: [], box: [...lo, ...hi], vol: Math.PI * r * r * length, material, key: "plain", mover: false });
6518
+ }
6519
+ return pieces;
6520
+ }
6521
+ function moverPiecesOf({ spec, recorded, partIds }) {
6522
+ const movers = [];
6523
+ for (const p of spec.parts ?? []) {
6524
+ const r = recorded.get(p.id);
6525
+ if (!r || r.prims.length === 0) continue;
6526
+ const box = p.bounds ?? moverBox(r);
6527
+ if (!box) continue;
6528
+ const vol = r.prims.reduce((sum, q) => sum + q.vol, 0);
6529
+ const tag = p.destruction ?? {};
6530
+ const solids = solidsOf(r.prims.map((q) => ({ aabb: primitiveBoxInObject(r, q) })), box);
6531
+ const id = partIds.get(p.id) ?? p.id;
6532
+ movers.push({ id, source: p, indices: [], box, vol, material: tag.material ?? spec.destruction?.material ?? "plastic", mover: true, tag, ...solids ? { solids } : {} });
6533
+ }
6534
+ return movers;
6535
+ }
6536
+ function partIdsOf(spec, unique) {
6537
+ const ids = /* @__PURE__ */ new Map();
6538
+ for (const p of spec.parts ?? []) ids.set(p.id, unique(sanitize(p.id)));
6539
+ return ids;
6540
+ }
6541
+
6542
+ // src/assembly/assemble.ts
6543
+ var ASSEMBLY_SEED = 1;
6544
+ function assembleHouseholdType({ household, type, castWith = castWithMember }) {
6545
+ const recorder = createDrawRecorder(NOOP_API);
6546
+ const spec = household.build(type, { seed: ASSEMBLY_SEED }, recorder.api);
6547
+ const def = household.types.find((t) => t.id === type);
6548
+ const planted = def?.fixed === true && def.y <= 0;
6549
+ if (planted) layInStretches(recorder.statics);
6550
+ const clusters = clusterStatics({ type, statics: recorder.statics, castWith });
6551
+ const unique = createIds();
6552
+ const partIds = partIdsOf(spec, unique);
6553
+ const pieces = bodyPiecesOf({ type, spec, clusters, statics: recorder.statics, unique });
6554
+ const movers = moverPiecesOf({ spec, recorded: recorder.movers, partIds });
6555
+ const parts = partsOf({ type, spec, pieces, all: [...pieces, ...movers], massKg: def?.massKg, planted });
6556
+ const joints = jointsOf({ spec, pieces, movers, statics: recorder.statics });
6557
+ const destructible = { name: type, parts, joints, tags: ["furniture", type], ...planted ? { fixed: true } : {} };
6558
+ const animations = buildAnimations({ spec, ids: partIds, present: new Set(movers.map((m) => m.source.id)) });
6559
+ const basinPiece = (pieces.find((p) => p.material === "ceramic") ?? pieces[0])?.id;
6560
+ const fluid = fluidOf({ type, basin: spec.basins?.[0], parts, basinPiece });
6561
+ return { type, spec, pieces, movers, partIds, destructible, animations, fluid, statics: recorder.statics };
6562
+ }
6563
+ function drawHouseholdAssembly(params) {
6564
+ const { household, assembly, api } = params;
6565
+ const recorder = createDrawRecorder(api);
6566
+ const item = { seed: ASSEMBLY_SEED };
6567
+ const spec = household.build(assembly.type, item, recorder.api);
6568
+ recorder.flush(assembly.pieces, assembly.statics);
6569
+ return spec;
6570
+ }
6571
+ var REGION_OF = { en: "us", "pt-BR": "br" };
6572
+ function createHouseholdAssemblies(options = {}) {
6573
+ const households = {};
6574
+ const assemblies = /* @__PURE__ */ new Map();
6575
+ const household = (locale) => households[locale] ?? (households[locale] = createHousehold({ locale, region: REGION_OF[locale] }));
6576
+ const assemble = (locale, type) => {
6577
+ const key = `${locale}:${type}`;
6578
+ let assembly = assemblies.get(key);
6579
+ if (!assembly) assemblies.set(key, assembly = assembleHouseholdType({ household: household(locale), type, ...options.castWith ? { castWith: options.castWith } : {} }));
6580
+ return assembly;
6581
+ };
6582
+ return {
6583
+ household,
6584
+ assemble,
6585
+ draw: (locale, type, api) => drawHouseholdAssembly({ household: household(locale), assembly: assemble(locale, type), api })
6586
+ };
6587
+ }
5899
6588
  export {
5900
6589
  HOUSEHOLD_ITEM_IDS,
5901
6590
  HOUSEHOLD_REGIONS,
5902
6591
  HOUSEHOLD_TYPES,
5903
6592
  STOVE_WALL_GAP,
5904
6593
  WALL_PLATE,
6594
+ assembleHouseholdType,
5905
6595
  cornersOf,
5906
6596
  createHousehold,
6597
+ createHouseholdAssemblies,
5907
6598
  createRecorder,
6599
+ drawHouseholdAssembly,
5908
6600
  motionPoint,
5909
6601
  sweepOf
5910
6602
  };