@adatechnology/game-fluids 0.6.3 → 0.6.5

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.d.ts CHANGED
@@ -135,6 +135,11 @@ interface SurfaceSpec {
135
135
  /** 0 = sealed (tile, glass, metal), 1 = soaks fast (fabric, soil, wood). Soaking shrinks the stain area and darkens it. */
136
136
  porosity: number;
137
137
  }
138
+ /** A rigid move of a surface from where it was added: rotated by `rotation`, then moved by `position` (world space). */
139
+ interface SurfacePose {
140
+ position: Vec3;
141
+ rotation: Quat;
142
+ }
138
143
  interface StainState {
139
144
  id: string;
140
145
  surface: string;
@@ -282,6 +287,13 @@ interface FluidWorld {
282
287
  removeSurface(id: string): void;
283
288
  /** Moves a surface and the stains on it by `offset` (a pushed table carries what was spilled on its top). Unknown ids are ignored. */
284
289
  moveSurface(id: string, offset: Vec3): void;
290
+ /**
291
+ * Puts a surface where the object it belongs to is now: `pose` moves the surface as it was added (`x' = rotation · x + position`; the identity is where
292
+ * it was added, a `moveSurface` adds to `position`). The stains on it go with it, and once it turned from its rest past what the liquid holds on (6° for
293
+ * water, more for a thicker one; a top turned over holds nothing), the pool on it runs off its low rim and lands on what is under it; the part
294
+ * soaked in stays on the surface. Unknown ids are ignored.
295
+ */
296
+ setSurfacePose(id: string, pose: SurfacePose): void;
285
297
  /**
286
298
  * Add a tap: while `on` it runs a `tap` stream (cause `'tap'`) from `position` along `direction` at `rate` litres per second. The stream fills any
287
299
  * container whose opening it lands in (the same logic as a pour) and otherwise makes a stain on the surface below. Returns the tap id.
@@ -707,4 +719,4 @@ declare function createCpuSimulator(field: FilmField, layout: FilmLayout, profil
707
719
  declare function hashCell(x: number, y: number): number;
708
720
  declare function valueNoise(x: number, y: number): number;
709
721
 
710
- export { type ContainerState, FILM_PROFILES, type Film, type FilmAdvanceInput, type FilmBead, type FilmBounds, type FilmDrag, type FilmDrip, type FilmDropState, type FilmField, type FilmImpact, type FilmLayout, type FilmLcg, type FilmLook, type FilmOptions, type FilmProfile, type FilmRandom, type FilmRegion, type FilmRules, type FilmSimulator, type FilmSource, type FilmWeather, type FluidEvent, type FluidLocale, type FluidOptions, type FluidStrings, type FluidWorld, type FoldedLayoutParams, type HandTransform, ITEM_USES, type ItemUsePlayer, LIQUIDS, type LiquidDef, PADRAO_LAYOUT, SAMPLES as STAIN_SAMPLES, type SplashOptions, type StainState, type StainTint, type StreamCause, type StreamState, type SurfaceLayoutOptions, type SurfaceSpec, type TapSpec, type TapState, WORKSHOP_LAYOUT, ballistic, createCpuSimulator, createFilm, createFilmLcg, createFluids, createFoldedLayout, createItemUse, describeEvent, filmProfileOf, hashCell, layoutFromSurfaces, locales, resolveStrings, sampleStream, stainDirection, valueNoise };
722
+ export { type ContainerState, FILM_PROFILES, type Film, type FilmAdvanceInput, type FilmBead, type FilmBounds, type FilmDrag, type FilmDrip, type FilmDropState, type FilmField, type FilmImpact, type FilmLayout, type FilmLcg, type FilmLook, type FilmOptions, type FilmProfile, type FilmRandom, type FilmRegion, type FilmRules, type FilmSimulator, type FilmSource, type FilmWeather, type FluidEvent, type FluidLocale, type FluidOptions, type FluidStrings, type FluidWorld, type FoldedLayoutParams, type HandTransform, ITEM_USES, type ItemUsePlayer, LIQUIDS, type LiquidDef, PADRAO_LAYOUT, SAMPLES as STAIN_SAMPLES, type SplashOptions, type StainState, type StainTint, type StreamCause, type StreamState, type SurfaceLayoutOptions, type SurfacePose, type SurfaceSpec, type TapSpec, type TapState, WORKSHOP_LAYOUT, ballistic, createCpuSimulator, createFilm, createFilmLcg, createFluids, createFoldedLayout, createItemUse, describeEvent, filmProfileOf, hashCell, layoutFromSurfaces, locales, resolveStrings, sampleStream, stainDirection, valueNoise };
@@ -980,6 +980,37 @@ var GameFluids = (() => {
980
980
  };
981
981
  var stainDirection = (s, i) => sampleDir(makeBasis(s.normal ?? [0, 1, 0], s.tangent), i);
982
982
 
983
+ // src/surface-pose.ts
984
+ var IDENTITY_POSE = { position: [0, 0, 0], rotation: [0, 0, 0, 1] };
985
+ var RUNOFF_TILT = 6 * Math.PI / 180;
986
+ var RUNOFF_TILT_MAX = 60 * Math.PI / 180;
987
+ var RUNOFF_CLEARANCE = 0.02;
988
+ var RUNOFF_SPEED = 0.6;
989
+ var UPSIDE_DOWN_Y = -0.2;
990
+ var applyPose = (pose, point) => add(pose.position, quatRotate(pose.rotation, point));
991
+ var undoPose = (pose, point) => quatRotate(quatConjugate(pose.rotation), sub(point, pose.position));
992
+ function posedFrame(rest, pose) {
993
+ return { point: applyPose(pose, rest.point), normal: quatRotate(pose.rotation, rest.normal), tangent: quatRotate(pose.rotation, rest.tangent) };
994
+ }
995
+ function carryStain({ stain, from, to }) {
996
+ const turn = quatMultiply(to.rotation, quatConjugate(from.rotation));
997
+ stain.center = applyPose(to, undoPose(from, stain.center));
998
+ stain.normal = quatRotate(turn, stain.normal);
999
+ stain.tangent = quatRotate(turn, stain.tangent);
1000
+ if (stain.flow) stain.flow = quatRotate(turn, stain.flow);
1001
+ }
1002
+ var runoffTilt = (def) => clamp(RUNOFF_TILT * Math.sqrt(Math.max(1, def.viscosity)), RUNOFF_TILT, RUNOFF_TILT_MAX);
1003
+ var tiltOf = (rest, normal) => Math.acos(clamp(dot(rest.normal, normal), -1, 1));
1004
+ function runoffStart({ center, surface }) {
1005
+ const { dir, slope } = downhill(surface.normal);
1006
+ if (surface.normal[1] < UPSIDE_DOWN_Y || slope < 1e-3) return { origin: add(center, scale(surface.normal, RUNOFF_CLEARANCE)), velocity: [0, -RUNOFF_SPEED, 0] };
1007
+ const rel = sub(center, surface.point);
1008
+ const room = (half, at, along) => along > 1e-6 ? (half - at) / along : along < -1e-6 ? (-half - at) / along : Infinity;
1009
+ const reach = Math.max(0, Math.min(room(surface.half[0], dot(rel, surface.t1), dot(dir, surface.t1)), room(surface.half[1], dot(rel, surface.t2), dot(dir, surface.t2))));
1010
+ const rim = add(center, scale(dir, Number.isFinite(reach) ? reach + RUNOFF_CLEARANCE : RUNOFF_CLEARANCE));
1011
+ return { origin: add(rim, scale(surface.normal, RUNOFF_CLEARANCE)), velocity: scale(dir, RUNOFF_SPEED) };
1012
+ }
1013
+
983
1014
  // src/world.ts
984
1015
  var GROUND_POROSITY = 0.2;
985
1016
  var MAX_STEP = 0.1;
@@ -1030,11 +1061,29 @@ var GameFluids = (() => {
1030
1061
  c.d = levelFor(c.cols, c.u, c.spec.capacity > 0 ? c.liters / c.spec.capacity : 0);
1031
1062
  };
1032
1063
  const surfaceFor = (id) => id === GROUND_ID ? groundSurface : surfaces.get(id) ?? groundSurface;
1033
- const landingSurfaces = () => {
1064
+ const landingSurfaces = (except) => {
1034
1065
  const list = [];
1035
- for (const [id, s] of surfaces) list.push({ id, normal: s.normal, t1: s.t1, t2: s.t2, point: s.point, half: s.half });
1066
+ for (const [id, s] of surfaces) if (id !== except) list.push({ id, normal: s.normal, t1: s.t1, t2: s.t2, point: s.point, half: s.half });
1036
1067
  return list;
1037
1068
  };
1069
+ const poseSurface = (surface, pose, carry) => {
1070
+ const from = surface.pose;
1071
+ const to = { position: copy3(pose.position), rotation: [pose.rotation[0], pose.rotation[1], pose.rotation[2], pose.rotation[3]] };
1072
+ if (carry) {
1073
+ for (const st of stains.values()) if (st.surface === surface.id) carryStain({ stain: st, from, to });
1074
+ }
1075
+ const frame = posedFrame(surface.rest, to);
1076
+ Object.assign(surface, makeBasis(frame.normal, frame.tangent), { point: frame.point, normal: frame.normal, pose: to });
1077
+ };
1078
+ const runOff = (st, def) => {
1079
+ const surface = surfaces.get(st.surface);
1080
+ if (!surface || st.liters <= 1e-6 || tiltOf(surface.rest, surface.normal) <= runoffTilt(def)) return;
1081
+ const { origin, velocity } = runoffStart({ center: st.center, surface });
1082
+ const liters = st.liters;
1083
+ st.liters = 0;
1084
+ const hit = trace(landingSurfaces(surface.id), groundAt, origin, velocity, gravity);
1085
+ if (hit) deposit(st.liquid, hit, liters, hit.velocity, st.tint);
1086
+ };
1038
1087
  const stopStream = (id) => {
1039
1088
  const s = streams.get(id);
1040
1089
  if (!s) return;
@@ -1341,6 +1390,7 @@ var GameFluids = (() => {
1341
1390
  for (const st of [...stains.values()]) {
1342
1391
  const def = liquids.get(st.liquid);
1343
1392
  if (!def) continue;
1393
+ runOff(st, def);
1344
1394
  const status = stepStain(st, def, surfaceFor(st.surface), ambientC, dt);
1345
1395
  if (status === "dried_mark") emit({ type: "dried", stain: st.id, mark: true });
1346
1396
  else if (status === "dried_gone") {
@@ -1574,7 +1624,9 @@ var GameFluids = (() => {
1574
1624
  ...clone(spec),
1575
1625
  ...basis,
1576
1626
  point: copy3(spec.point),
1577
- half: [spec.halfSize[0], spec.halfSize[1]]
1627
+ half: [spec.halfSize[0], spec.halfSize[1]],
1628
+ rest: { point: copy3(spec.point), normal: copy3(basis.normal), tangent: copy3(basis.t1) },
1629
+ pose: IDENTITY_POSE
1578
1630
  });
1579
1631
  },
1580
1632
  removeSurface(id) {
@@ -1583,9 +1635,11 @@ var GameFluids = (() => {
1583
1635
  },
1584
1636
  moveSurface(id, offset) {
1585
1637
  const surface = surfaces.get(id);
1586
- if (!surface) return;
1587
- surface.point = add(surface.point, offset);
1588
- for (const s of stains.values()) if (s.surface === id) s.center = add(s.center, offset);
1638
+ if (surface) poseSurface(surface, { position: add(surface.pose.position, offset), rotation: surface.pose.rotation }, true);
1639
+ },
1640
+ setSurfacePose(id, pose) {
1641
+ const surface = surfaces.get(id);
1642
+ if (surface) poseSurface(surface, pose, true);
1589
1643
  },
1590
1644
  splash(liquid, point, liters, velocity, options2) {
1591
1645
  if (!liquids.has(liquid) || !(liters > 0)) return;
@@ -1643,7 +1697,10 @@ var GameFluids = (() => {
1643
1697
  const { cols: _cols, ...rest } = c;
1644
1698
  return clone(rest);
1645
1699
  }),
1646
- surfaces: [...surfaces.values()].map((s) => clone({ id: s.id, point: s.point, normal: s.normal, halfSize: s.half, tangent: s.t1, porosity: s.porosity })),
1700
+ surfaces: [...surfaces.values()].map((s) => {
1701
+ const moved = s.pose !== IDENTITY_POSE;
1702
+ return clone({ id: s.id, point: s.rest.point, normal: s.rest.normal, halfSize: s.half, tangent: s.rest.tangent, porosity: s.porosity, ...moved ? { pose: s.pose } : {} });
1703
+ }),
1647
1704
  streams: [...streams.values()].map((s) => clone(s)),
1648
1705
  taps: [...taps.values()].map((t) => clone(t)),
1649
1706
  stains: [...stains.values()].map((s) => clone(s))
@@ -1662,7 +1719,11 @@ var GameFluids = (() => {
1662
1719
  containers = /* @__PURE__ */ new Map();
1663
1720
  for (const raw of snap.containers) containers.set(raw.id, { ...raw, cols: makeColumns(raw.spec.shape) });
1664
1721
  surfaces = /* @__PURE__ */ new Map();
1665
- for (const s of snap.surfaces) world.addSurface(s);
1722
+ for (const { pose, ...spec } of snap.surfaces) {
1723
+ world.addSurface(spec);
1724
+ const surface = surfaces.get(spec.id);
1725
+ if (pose && surface) poseSurface(surface, pose, false);
1726
+ }
1666
1727
  counters.tap = snap.counters.tap ?? 0;
1667
1728
  streams = new Map(snap.streams.map((s) => [s.id, s]));
1668
1729
  taps = new Map((snap.taps ?? []).map((t) => [t.id, t]));