@adatechnology/game-floorplan 0.3.12 → 0.4.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
@@ -16,6 +16,7 @@ var createRegistries = () => ({
16
16
  SEED_KEYS: { rooms: {}, items: {} },
17
17
  stateDistribution: { weights: null, seedKey: "state" },
18
18
  RESULT_EXTENSIONS: [],
19
+ OPTION_RESOLVERS: [],
19
20
  FINDS_PROVIDERS: [],
20
21
  VIEWER_LAYERS: [],
21
22
  LOCALES: {},
@@ -102,6 +103,12 @@ function resolveStyleId(id) {
102
103
  function styleOf(id) {
103
104
  return registries().STYLES[id] || registries().STYLES.budget || Object.values(registries().STYLES)[0];
104
105
  }
106
+ function registerOptionResolver(name, resolve) {
107
+ const list = registries().OPTION_RESOLVERS;
108
+ const i = list.findIndex((x) => x.name === name);
109
+ if (i >= 0) list[i] = { name, resolve };
110
+ else list.push({ name, resolve });
111
+ }
105
112
  function registerResultExtension(name, run, options = {}) {
106
113
  const entry = { name, run, after: options.after || [], seedKey: options.seedKey ?? name };
107
114
  const i = registries().RESULT_EXTENSIONS.findIndex((x) => x.name === name);
@@ -244,11 +251,28 @@ var WINDOWLESS_ROOM_TYPES = /* @__PURE__ */ new Set([
244
251
  ]);
245
252
  var CAR_CLEARANCE = { driver: 0.8, side: 0.6, end: 0.6 };
246
253
 
254
+ // src/wall-thickness.ts
255
+ var active = WALL_THICKNESS;
256
+ var wallThickness = () => active;
257
+ var wallMargin = () => active / 2;
258
+ var wallThicknessFor = (tpl, op) => op.wallThickness ?? tpl.wallThickness ?? WALL_THICKNESS;
259
+ function withWallThickness(thickness, fn) {
260
+ const previous = active;
261
+ active = thickness;
262
+ try {
263
+ return fn();
264
+ } finally {
265
+ active = previous;
266
+ }
267
+ }
268
+ var roomMargin = (room) => room.outdoor ? WALL_THICKNESS / 2 : wallMargin();
269
+
247
270
  // src/geometry.ts
248
271
  var r2 = (x) => Math.round(x * 100) / 100;
249
272
  var clamp = (x, a, b) => Math.max(a, Math.min(b, x));
250
273
  var overlaps = (a, b, e = EPS) => a[0] < b[2] - e && a[2] > b[0] + e && a[1] < b[3] - e && a[3] > b[1] + e;
251
- var inner = (p) => [p[0] + MARGIN, p[1] + MARGIN, p[2] - MARGIN, p[3] - MARGIN];
274
+ var inner = (p) => insetBy(p, wallMargin());
275
+ var insetBy = (p, margin) => [p[0] + margin, p[1] + margin, p[2] - margin, p[3] - margin];
252
276
  var eq = (a, b) => Math.abs(a - b) < TOL;
253
277
  var inflate = (r, e) => [r[0] - e, r[1] - e, r[2] + e, r[3] + e];
254
278
  var areaOf = (r) => (r[2] - r[0]) * (r[3] - r[1]);
@@ -321,7 +345,7 @@ function openingZones(u, v, axis, width, depth) {
321
345
  const h = width / 2;
322
346
  return axis === "u" ? [[u - h, v - depth, u + h, v], [u - h, v, u + h, v + depth]] : [[u - depth, v - h, u, v + h], [u, v - h, u + depth, v + h]];
323
347
  }
324
- var openingDepth = (width, leaf) => Math.max(0.9, leaf) + MARGIN;
348
+ var openingDepth = (width, leaf) => Math.max(0.9, leaf) + wallMargin();
325
349
  function entryPoints(ab) {
326
350
  return ab.axis === "u" ? [[ab.u, ab.v - 0.55], [ab.u, ab.v + 0.55]] : [[ab.u - 0.55, ab.v], [ab.u + 0.55, ab.v]];
327
351
  }
@@ -345,7 +369,7 @@ function normalizeRoom(c, floorIndex) {
345
369
  };
346
370
  }
347
371
  function usableParts(room, extra) {
348
- const m = MARGIN + (extra || 0);
372
+ const m = roomMargin(room) + (extra || 0);
349
373
  const out = [];
350
374
  for (const p of room.parts) {
351
375
  out.push([p[0] + m, p[1] + m, p[2] - m, p[3] - m]);
@@ -443,7 +467,7 @@ function buildWalls(rooms, doors, openPairs, floorIndex) {
443
467
  v0: g.axis === "u" ? g.pos : x,
444
468
  u1: g.axis === "u" ? y : g.pos,
445
469
  v1: g.axis === "u" ? g.pos : y,
446
- thickness: WALL_THICKNESS,
470
+ thickness: wallThickness(),
447
471
  sides: [s.A, s.B],
448
472
  exterior: s.A === "outside" || s.B === "outside",
449
473
  type: railing ? "railing" : "wall",
@@ -517,8 +541,9 @@ function windowZone(j, outside) {
517
541
  if (outside) n = [-n[0], -n[1]];
518
542
  const u = j.u;
519
543
  const v = j.v;
520
- if (j.axis === "u") return n[1] > 0 ? [u - h, v - MARGIN, u + h, v + depth] : [u - h, v - depth, u + h, v + MARGIN];
521
- return n[0] > 0 ? [u - MARGIN, v - h, u + depth, v + h] : [u - depth, v - h, u + MARGIN, v + h];
544
+ const MARGIN2 = wallMargin();
545
+ if (j.axis === "u") return n[1] > 0 ? [u - h, v - MARGIN2, u + h, v + depth] : [u - h, v - depth, u + h, v + MARGIN2];
546
+ return n[0] > 0 ? [u - MARGIN2, v - h, u + depth, v + h] : [u - depth, v - h, u + MARGIN2, v + h];
522
547
  }
523
548
  function blocksWindow(it, win, overheadOff) {
524
549
  const height = Math.max(it.h, it.windowHeight || 0, it.overhead && !overheadOff ? OVERHEAD_TOP : 0);
@@ -552,14 +577,15 @@ function wallBodies(plan, floor) {
552
577
  const cache = plan.wallBodies = plan.wallBodies || {};
553
578
  if (cache[floor]) return cache[floor];
554
579
  const doors = plan.doors.filter((p) => p.floor === floor);
580
+ const MARGIN2 = plan.wallThickness / 2;
555
581
  return cache[floor] = plan.walls.filter((p) => p.floor === floor && (p.type === "wall" || p.type === "railing")).map((p) => {
556
582
  const a0 = p.axis === "u" ? p.u0 : p.v0;
557
583
  const a1 = p.axis === "u" ? p.u1 : p.v1;
558
584
  const pos = p.axis === "u" ? p.v0 : p.u0;
559
585
  const atDoor = (x) => doors.some((q) => q.axis === p.axis && eq(q.pos, pos) && (eq(q.at - q.width / 2, x) || eq(q.at + q.width / 2, x)));
560
- const e0 = atDoor(a0) ? 0 : MARGIN;
561
- const e1 = atDoor(a1) ? 0 : MARGIN;
562
- return p.axis === "u" ? [a0 - e0, pos - MARGIN, a1 + e1, pos + MARGIN] : [pos - MARGIN, a0 - e0, pos + MARGIN, a1 + e1];
586
+ const e0 = atDoor(a0) ? 0 : MARGIN2;
587
+ const e1 = atDoor(a1) ? 0 : MARGIN2;
588
+ return p.axis === "u" ? [a0 - e0, pos - MARGIN2, a1 + e1, pos + MARGIN2] : [pos - MARGIN2, a0 - e0, pos + MARGIN2, a1 + e1];
563
589
  });
564
590
  }
565
591
  function createGrid(plan, room, ignored, radius = plan.playerRadius) {
@@ -901,7 +927,9 @@ var STREAM = {
901
927
 
902
928
  // src/context.ts
903
929
  var placementKit = {
904
- MARGIN,
930
+ get MARGIN() {
931
+ return wallMargin();
932
+ },
905
933
  EPS,
906
934
  r2,
907
935
  clamp,
@@ -1350,6 +1378,7 @@ function buildPlan(tpl, wIn, dIn, opIn, bc = {}) {
1350
1378
  const FLOOR_DEFS = floorsOf(tpl, op);
1351
1379
  const style = styleOf(styleId);
1352
1380
  const plan = {
1381
+ wallThickness: wallThickness(),
1353
1382
  w,
1354
1383
  d,
1355
1384
  tpl,
@@ -1826,7 +1855,7 @@ function slotDefsOf(plan, room) {
1826
1855
  const key = registries().LIBRARY_ALIAS[name] || name;
1827
1856
  const library = room.slots ? room.slots : registries().LIBRARIES[key];
1828
1857
  if (!library) return [];
1829
- const R = inner(room.parts[0]);
1858
+ const R = insetBy(room.parts[0], roomMargin(room));
1830
1859
  let defs = typeof library === "function" ? library({ w: R[2] - R[0], d: R[3] - R[1] }) : library.slice();
1831
1860
  defs = defs.map((x) => Object.assign({}, x));
1832
1861
  for (const id of room.without || []) defs = defs.filter((x) => x.id !== id);
@@ -1892,7 +1921,7 @@ function resolveSlot(plan, room, def, byId, zones) {
1892
1921
  }
1893
1922
  const size = def.size;
1894
1923
  const roomCenter = (() => {
1895
- const R0 = inner(room.parts[0]);
1924
+ const R0 = insetBy(room.parts[0], roomMargin(room));
1896
1925
  return [(R0[0] + R0[2]) / 2, (R0[1] + R0[3]) / 2];
1897
1926
  })();
1898
1927
  const openings = openingsOfFloor(plan, room.floor);
@@ -1918,7 +1947,7 @@ function resolveSlot(plan, room, def, byId, zones) {
1918
1947
  };
1919
1948
  const free = (rect) => !!def.floorFree || !zones.some((z) => overlaps(rect, z, room.type === "stairs" ? -0.011 : EPS));
1920
1949
  const rule = def.facing || "center";
1921
- let R = inner(room.parts[Math.min(def.part || 0, room.parts.length - 1)]);
1950
+ let R = insetBy(room.parts[Math.min(def.part || 0, room.parts.length - 1)], roomMargin(room));
1922
1951
  if (def.anchor === "wall" || def.anchor === "window") {
1923
1952
  let side = def.side;
1924
1953
  let a = null;
@@ -1930,7 +1959,7 @@ function resolveSlot(plan, room, def, byId, zones) {
1930
1959
  const found2 = win;
1931
1960
  side = room.outdoor ? SIDE_OPPOSITE[win.side] : WINDOW_SIDE[win.side];
1932
1961
  const cand = room.parts.map(inner).find(
1933
- (r) => (found2.axis === "u" ? found2.u >= r[0] - TOL && found2.u <= r[2] + TOL : found2.v >= r[1] - TOL && found2.v <= r[3] + TOL) && (side === "left" && eq(r[0], found2.pos + MARGIN) || side === "right" && eq(r[2], found2.pos - MARGIN) || side === "front" && eq(r[1], found2.pos + MARGIN) || side === "back" && eq(r[3], found2.pos - MARGIN))
1962
+ (r) => (found2.axis === "u" ? found2.u >= r[0] - TOL && found2.u <= r[2] + TOL : found2.v >= r[1] - TOL && found2.v <= r[3] + TOL) && (side === "left" && eq(r[0], found2.pos + roomMargin(room)) || side === "right" && eq(r[2], found2.pos - roomMargin(room)) || side === "front" && eq(r[1], found2.pos + roomMargin(room)) || side === "back" && eq(r[3], found2.pos - roomMargin(room)))
1934
1963
  );
1935
1964
  if (!cand) return invalid("window_outside_room");
1936
1965
  R = cand;
@@ -1947,7 +1976,7 @@ function resolveSlot(plan, room, def, byId, zones) {
1947
1976
  const hw = Math.min(size[0], len) / 2;
1948
1977
  const cell = (ac) => side === "left" ? [R[0], ac - hw, R[0] + depth, ac + hw] : side === "right" ? [R[2] - depth, ac - hw, R[2], ac + hw] : side === "front" ? [ac - hw, R[1], ac + hw, R[1] + depth] : [ac - hw, R[3] - depth, ac + hw, R[3]];
1949
1978
  const nominal = a != null ? a : a0 + hw + (def.pos == null ? 0.5 : def.pos) * Math.max(0, len - 2 * hw) + (def.shift || 0);
1950
- const wp = side === "left" ? R[0] - MARGIN : side === "right" ? R[2] + MARGIN : side === "front" ? R[1] - MARGIN : R[3] + MARGIN;
1979
+ const wp = side === "left" ? R[0] - roomMargin(room) : side === "right" ? R[2] + roomMargin(room) : side === "front" ? R[1] - roomMargin(room) : R[3] + roomMargin(room);
1951
1980
  const pa = clamp(nominal, a0 + hw, a1 - hw);
1952
1981
  let found = null;
1953
1982
  const steps = [0];
@@ -1986,7 +2015,7 @@ function resolveSlot(plan, room, def, byId, zones) {
1986
2015
  if (extentU > R[2] - R[0] + EPS || extentV > R[3] - R[1] + EPS) return invalid("larger_than_room");
1987
2016
  const rect = [cornerU === "left" ? R[0] : R[2] - extentU, cornerV === "front" ? R[1] : R[3] - extentV, cornerU === "left" ? R[0] + extentU : R[2], cornerV === "front" ? R[1] + extentV : R[3]];
1988
2017
  if (!free(rect)) return invalid("blocked_by_door");
1989
- for (const sp of [[kx - normalU[0] * (MARGIN + 0.1), (rect[1] + rect[3]) / 2], [(rect[0] + rect[2]) / 2, kz - normalV[1] * (MARGIN + 0.1)]]) if (isOpenSide(plan, room, sp[0], sp[1])) return invalid("open_wall");
2018
+ for (const sp of [[kx - normalU[0] * (roomMargin(room) + 0.1), (rect[1] + rect[3]) / 2], [(rect[0] + rect[2]) / 2, kz - normalV[1] * (roomMargin(room) + 0.1)]]) if (isOpenSide(plan, room, sp[0], sp[1])) return invalid("open_wall");
1990
2019
  return Object.assign(base, { x: r2((rect[0] + rect[2]) / 2), z: r2((rect[1] + rect[3]) / 2), frontU: f[0], frontV: f[1], ry: r2(Math.atan2(f[0], f[1])), w: size[0], d: size[1], aabb: rect.map(r2), _cell: { rect, f, enclosed: [cornerU, cornerV], R, size } });
1991
2020
  }
1992
2021
  if (def.anchor === "center") {
@@ -2028,7 +2057,10 @@ var cleanSlot = (s) => {
2028
2057
  };
2029
2058
  function slots(template, options) {
2030
2059
  const tpl = templateOf(template);
2031
- const o = options || {};
2060
+ const o = resolveOptions(tpl, options || {});
2061
+ return withWallThickness(wallThicknessFor(tpl, o), () => slotsWith(tpl, o));
2062
+ }
2063
+ function slotsWith(tpl, o) {
2032
2064
  const plan = buildPlan(tpl, o.w, o.d, o);
2033
2065
  const locale = o.locale || "en";
2034
2066
  return resolveSlots(plan).map(cleanSlot).map((s) => s.reason ? Object.assign(s, { reason: translate("reasons", s.reason, locale) }) : s);
@@ -2071,7 +2103,11 @@ function applyState(plan, items, ctx, seed, state) {
2071
2103
  var loc = (key, locale, ns = "labels", vars) => key ? translate(ns, key, locale, vars) : key;
2072
2104
  function generate(template, options) {
2073
2105
  const tpl = templateOf(template);
2074
- const o = options || {};
2106
+ const o = resolveOptions(tpl, options || {});
2107
+ return withWallThickness(wallThicknessFor(tpl, o), () => generateWith(tpl, o));
2108
+ }
2109
+ var resolveOptions = (tpl, options) => registries().OPTION_RESOLVERS.reduce((op, r) => r.resolve(tpl, op), options);
2110
+ function generateWith(tpl, o) {
2075
2111
  const seed = o.seed == null ? 1 : o.seed;
2076
2112
  const locale = o.locale || "en";
2077
2113
  let op = Object.assign({}, o, { seed });
@@ -2483,6 +2519,7 @@ function buildEngine(scope) {
2483
2519
  registerState: scoped(registerState),
2484
2520
  setStateDistribution: scoped(setStateDistribution),
2485
2521
  registerLocale: scoped(registerLocale),
2522
+ registerOptionResolver: scoped(registerOptionResolver),
2486
2523
  registerResultExtension: scoped(registerResultExtension),
2487
2524
  registerFindsProvider: scoped(registerFindsProvider),
2488
2525
  findsFor: scoped(findsFor),
@@ -2791,6 +2828,7 @@ export {
2791
2828
  registerLibrary,
2792
2829
  registerLibraryAlias,
2793
2830
  registerLocale,
2831
+ registerOptionResolver,
2794
2832
  registerPaletteStyle,
2795
2833
  registerPass,
2796
2834
  registerPermeableGround,