@adatechnology/game-fluids 0.0.0-stage → 0.1.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 ADDED
@@ -0,0 +1,2184 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+
5
+ // src/data/liquids.json
6
+ var liquids_default = [
7
+ {
8
+ id: "water",
9
+ color: 11064558,
10
+ opacity: 0.35,
11
+ stainColor: 7309206,
12
+ viscosity: 1,
13
+ density: 1e3,
14
+ dryTime: 600,
15
+ gloss: 0.9,
16
+ drink: {
17
+ thirst: 100,
18
+ effects: []
19
+ },
20
+ tags: [
21
+ "drink",
22
+ "clear"
23
+ ]
24
+ },
25
+ {
26
+ id: "coffee",
27
+ color: 3875604,
28
+ opacity: 0.95,
29
+ stainColor: 2824202,
30
+ viscosity: 1.5,
31
+ density: 1005,
32
+ dryTime: 900,
33
+ gloss: 0.8,
34
+ leavesMark: true,
35
+ steamC: 60,
36
+ drink: {
37
+ thirst: 80,
38
+ effects: [
39
+ "caffeine"
40
+ ]
41
+ },
42
+ tags: [
43
+ "drink",
44
+ "hot"
45
+ ]
46
+ },
47
+ {
48
+ id: "tea",
49
+ color: 9063188,
50
+ opacity: 0.8,
51
+ stainColor: 6961682,
52
+ viscosity: 1.4,
53
+ density: 1005,
54
+ dryTime: 800,
55
+ gloss: 0.8,
56
+ leavesMark: true,
57
+ steamC: 60,
58
+ drink: {
59
+ thirst: 90,
60
+ effects: []
61
+ },
62
+ tags: [
63
+ "drink",
64
+ "hot"
65
+ ]
66
+ },
67
+ {
68
+ id: "milk",
69
+ color: 16052712,
70
+ opacity: 0.98,
71
+ stainColor: 14473160,
72
+ viscosity: 2,
73
+ density: 1030,
74
+ dryTime: 900,
75
+ gloss: 0.5,
76
+ leavesMark: true,
77
+ drink: {
78
+ thirst: 70,
79
+ effects: []
80
+ },
81
+ tags: [
82
+ "drink"
83
+ ]
84
+ },
85
+ {
86
+ id: "soda",
87
+ color: 3808530,
88
+ opacity: 0.85,
89
+ stainColor: 2757901,
90
+ viscosity: 1.3,
91
+ density: 1040,
92
+ dryTime: 1200,
93
+ gloss: 0.85,
94
+ leavesMark: true,
95
+ drink: {
96
+ thirst: 60,
97
+ effects: []
98
+ },
99
+ tags: [
100
+ "drink",
101
+ "sticky"
102
+ ]
103
+ },
104
+ {
105
+ id: "juice",
106
+ color: 15239467,
107
+ opacity: 0.9,
108
+ stainColor: 12741916,
109
+ viscosity: 2,
110
+ density: 1045,
111
+ dryTime: 1100,
112
+ gloss: 0.8,
113
+ leavesMark: true,
114
+ drink: {
115
+ thirst: 75,
116
+ effects: []
117
+ },
118
+ tags: [
119
+ "drink",
120
+ "sticky"
121
+ ]
122
+ },
123
+ {
124
+ id: "beer",
125
+ color: 14259740,
126
+ opacity: 0.8,
127
+ stainColor: 11039764,
128
+ viscosity: 1.4,
129
+ density: 1010,
130
+ dryTime: 900,
131
+ gloss: 0.8,
132
+ drink: {
133
+ thirst: 50,
134
+ effects: [
135
+ "alcohol"
136
+ ]
137
+ },
138
+ tags: [
139
+ "drink",
140
+ "alcohol"
141
+ ]
142
+ },
143
+ {
144
+ id: "wine",
145
+ color: 7016223,
146
+ opacity: 0.9,
147
+ stainColor: 5048854,
148
+ viscosity: 1.6,
149
+ density: 990,
150
+ dryTime: 800,
151
+ gloss: 0.85,
152
+ leavesMark: true,
153
+ drink: {
154
+ thirst: 40,
155
+ effects: [
156
+ "alcohol"
157
+ ]
158
+ },
159
+ tags: [
160
+ "drink",
161
+ "alcohol"
162
+ ]
163
+ },
164
+ {
165
+ id: "spirits",
166
+ color: 13214042,
167
+ opacity: 0.6,
168
+ stainColor: 10320959,
169
+ viscosity: 1.8,
170
+ density: 940,
171
+ dryTime: 200,
172
+ gloss: 0.85,
173
+ flammable: true,
174
+ drink: {
175
+ thirst: 5,
176
+ effects: [
177
+ "alcohol"
178
+ ]
179
+ },
180
+ tags: [
181
+ "drink",
182
+ "alcohol"
183
+ ]
184
+ },
185
+ {
186
+ id: "fuel",
187
+ color: 14926682,
188
+ opacity: 0.6,
189
+ stainColor: 7232298,
190
+ viscosity: 0.6,
191
+ density: 740,
192
+ dryTime: 120,
193
+ gloss: 0.95,
194
+ leavesMark: true,
195
+ flammable: true,
196
+ drink: {
197
+ thirst: 0,
198
+ effects: [
199
+ "poison"
200
+ ]
201
+ },
202
+ tags: [
203
+ "fuel",
204
+ "gasoline"
205
+ ]
206
+ },
207
+ {
208
+ id: "diesel",
209
+ color: 12098094,
210
+ opacity: 0.8,
211
+ stainColor: 5917208,
212
+ viscosity: 3,
213
+ density: 830,
214
+ dryTime: 3600,
215
+ gloss: 0.95,
216
+ leavesMark: true,
217
+ flammable: true,
218
+ drink: {
219
+ thirst: 0,
220
+ effects: [
221
+ "poison"
222
+ ]
223
+ },
224
+ tags: [
225
+ "fuel"
226
+ ]
227
+ },
228
+ {
229
+ id: "motor_oil",
230
+ color: 3877388,
231
+ opacity: 0.95,
232
+ stainColor: 1840134,
233
+ viscosity: 100,
234
+ density: 880,
235
+ dryTime: 0,
236
+ gloss: 1,
237
+ leavesMark: true,
238
+ flammable: true,
239
+ drink: {
240
+ thirst: 0,
241
+ effects: [
242
+ "poison"
243
+ ]
244
+ },
245
+ tags: [
246
+ "oil"
247
+ ]
248
+ },
249
+ {
250
+ id: "cooking_oil",
251
+ color: 14267708,
252
+ opacity: 0.85,
253
+ stainColor: 11110698,
254
+ viscosity: 60,
255
+ density: 920,
256
+ dryTime: 0,
257
+ gloss: 1,
258
+ leavesMark: true,
259
+ flammable: true,
260
+ drink: {
261
+ thirst: 0,
262
+ effects: [
263
+ "poison"
264
+ ]
265
+ },
266
+ tags: [
267
+ "oil",
268
+ "food"
269
+ ]
270
+ },
271
+ {
272
+ id: "blood",
273
+ color: 7997964,
274
+ opacity: 0.92,
275
+ stainColor: 4850950,
276
+ viscosity: 4,
277
+ density: 1060,
278
+ dryTime: 1200,
279
+ gloss: 0.75,
280
+ leavesMark: true,
281
+ drink: {
282
+ thirst: 10,
283
+ effects: [
284
+ "disgust"
285
+ ]
286
+ },
287
+ tags: [
288
+ "organic"
289
+ ]
290
+ },
291
+ {
292
+ id: "sap",
293
+ color: 13146940,
294
+ opacity: 0.8,
295
+ stainColor: 10122282,
296
+ viscosity: 200,
297
+ density: 1100,
298
+ dryTime: 3600,
299
+ gloss: 0.8,
300
+ leavesMark: true,
301
+ drink: {
302
+ thirst: 20,
303
+ effects: []
304
+ },
305
+ tags: [
306
+ "organic",
307
+ "sticky"
308
+ ]
309
+ },
310
+ {
311
+ id: "acid",
312
+ color: 10146378,
313
+ opacity: 0.6,
314
+ stainColor: 7313970,
315
+ viscosity: 1.5,
316
+ density: 1200,
317
+ dryTime: 900,
318
+ gloss: 0.9,
319
+ leavesMark: true,
320
+ drink: {
321
+ thirst: 0,
322
+ effects: [
323
+ "poison"
324
+ ]
325
+ },
326
+ tags: [
327
+ "corrosive"
328
+ ]
329
+ },
330
+ {
331
+ id: "paint",
332
+ color: 11740715,
333
+ opacity: 0.98,
334
+ stainColor: 9050655,
335
+ viscosity: 300,
336
+ density: 1300,
337
+ dryTime: 1800,
338
+ gloss: 0.6,
339
+ leavesMark: true,
340
+ drink: {
341
+ thirst: 0,
342
+ effects: [
343
+ "poison"
344
+ ]
345
+ },
346
+ tags: [
347
+ "paint"
348
+ ]
349
+ },
350
+ {
351
+ id: "bleach",
352
+ color: 15134688,
353
+ opacity: 0.7,
354
+ stainColor: 13621446,
355
+ viscosity: 1.2,
356
+ density: 1100,
357
+ dryTime: 900,
358
+ gloss: 0.85,
359
+ leavesMark: true,
360
+ drink: {
361
+ thirst: 0,
362
+ effects: [
363
+ "poison"
364
+ ]
365
+ },
366
+ tags: [
367
+ "corrosive"
368
+ ]
369
+ },
370
+ {
371
+ id: "honey",
372
+ color: 14193162,
373
+ opacity: 0.9,
374
+ stainColor: 12089350,
375
+ viscosity: 3e3,
376
+ density: 1420,
377
+ dryTime: 14400,
378
+ gloss: 0.95,
379
+ leavesMark: true,
380
+ drink: {
381
+ thirst: 10,
382
+ effects: []
383
+ },
384
+ tags: [
385
+ "drink",
386
+ "sticky"
387
+ ]
388
+ }
389
+ ];
390
+
391
+ // src/liquids.ts
392
+ var LIQUIDS = liquids_default;
393
+
394
+ // src/math.ts
395
+ var GRAVITY = 9.81;
396
+ var clamp = (v, lo, hi) => v < lo ? lo : v > hi ? hi : v;
397
+ var lerp = (a, b, t) => a + (b - a) * t;
398
+ var add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
399
+ var sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
400
+ var scale = (a, s) => [a[0] * s, a[1] * s, a[2] * s];
401
+ var dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
402
+ var cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
403
+ var length = (a) => Math.hypot(a[0], a[1], a[2]);
404
+ var distance = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
405
+ var normalize = (a, fallback = [0, 1, 0]) => {
406
+ const l = length(a);
407
+ return l < 1e-9 ? [fallback[0], fallback[1], fallback[2]] : [a[0] / l, a[1] / l, a[2] / l];
408
+ };
409
+ var copy3 = (a) => [a[0], a[1], a[2]];
410
+ var lerp3 = (a, b, t) => [lerp(a[0], b[0], t), lerp(a[1], b[1], t), lerp(a[2], b[2], t)];
411
+ var quatRotate = (q, v) => {
412
+ const u = [q[0], q[1], q[2]];
413
+ const c1 = cross(u, v);
414
+ const c2 = cross(u, c1);
415
+ return [v[0] + 2 * (q[3] * c1[0] + c2[0]), v[1] + 2 * (q[3] * c1[1] + c2[1]), v[2] + 2 * (q[3] * c1[2] + c2[2])];
416
+ };
417
+ var quatConjugate = (q) => [-q[0], -q[1], -q[2], q[3]];
418
+ var quatMultiply = (a, b) => [
419
+ a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
420
+ a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
421
+ a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
422
+ a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2]
423
+ ];
424
+ var quatFromEuler = (x, y, z) => {
425
+ const c1 = Math.cos(x / 2);
426
+ const c2 = Math.cos(y / 2);
427
+ const c3 = Math.cos(z / 2);
428
+ const s1 = Math.sin(x / 2);
429
+ const s2 = Math.sin(y / 2);
430
+ const s3 = Math.sin(z / 2);
431
+ return [s1 * c2 * c3 + c1 * s2 * s3, c1 * s2 * c3 - s1 * c2 * s3, c1 * c2 * s3 + s1 * s2 * c3, c1 * c2 * c3 - s1 * s2 * s3];
432
+ };
433
+ var Rng = class {
434
+ constructor(seed) {
435
+ __publicField(this, "state");
436
+ this.state = Math.imul((seed | 0) ^ 2654435769, 2246822507) >>> 0 || 1;
437
+ }
438
+ next() {
439
+ this.state = this.state + 1831565813 >>> 0;
440
+ let t = this.state;
441
+ t = Math.imul(t ^ t >>> 15, t | 1);
442
+ t ^= t + Math.imul(t ^ t >>> 7, t | 61);
443
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
444
+ }
445
+ };
446
+
447
+ // src/landing.ts
448
+ var GROUND_ID = "ground";
449
+ var MAX_FLIGHT = 3;
450
+ var ballistic = (origin, velocity, gravity, t) => [
451
+ origin[0] + velocity[0] * t,
452
+ origin[1] + velocity[1] * t - 0.5 * gravity * t * t,
453
+ origin[2] + velocity[2] * t
454
+ ];
455
+ var sampleStream = (stream, gravity, t) => ballistic(stream.origin, stream.velocity, gravity, t);
456
+ var velocityAt = (velocity, gravity, t) => [velocity[0], velocity[1] - gravity * t, velocity[2]];
457
+ var trace = (surfaces, groundAt, origin, velocity, gravity) => {
458
+ let best = null;
459
+ for (const s of surfaces) {
460
+ const n = s.normal;
461
+ const c = dot(n, sub(origin, s.point));
462
+ if (c < -5e-3) continue;
463
+ const a = -0.5 * gravity * n[1];
464
+ const b = dot(n, velocity);
465
+ let t = Infinity;
466
+ if (Math.abs(a) < 1e-9) {
467
+ if (b < -1e-9) t = -c / b;
468
+ } else {
469
+ const disc = b * b - 4 * a * c;
470
+ if (disc >= 0) {
471
+ const sq = Math.sqrt(disc);
472
+ for (const root of [(-b - sq) / (2 * a), (-b + sq) / (2 * a)]) {
473
+ if (root >= -1e-6 && b + 2 * a * root < 0 && root < t) t = root;
474
+ }
475
+ }
476
+ }
477
+ if (!Number.isFinite(t) || t > MAX_FLIGHT) continue;
478
+ t = Math.max(0, t);
479
+ if (best && t >= best.t) continue;
480
+ const p = ballistic(origin, velocity, gravity, t);
481
+ const rel = sub(p, s.point);
482
+ if (Math.abs(dot(rel, s.t1)) > s.half[0] || Math.abs(dot(rel, s.t2)) > s.half[1]) continue;
483
+ best = { t, point: p, normal: n, surface: s.id, velocity: velocityAt(velocity, gravity, t) };
484
+ }
485
+ const step = 0.02;
486
+ let prevT = 0;
487
+ const startOver = origin[1] - groundAt(origin[0], origin[2]);
488
+ const limit = best ? best.t : MAX_FLIGHT;
489
+ if (startOver <= 0) {
490
+ return best && best.t <= 0 ? best : { t: 0, point: [origin[0], groundAt(origin[0], origin[2]), origin[2]], normal: [0, 1, 0], surface: GROUND_ID, velocity };
491
+ }
492
+ for (let t = step; t <= limit + step; t += step) {
493
+ const tt = Math.min(t, limit);
494
+ const p = ballistic(origin, velocity, gravity, tt);
495
+ const over = p[1] - groundAt(p[0], p[2]);
496
+ if (over <= 0) {
497
+ let lo = prevT;
498
+ let hi = tt;
499
+ for (let i = 0; i < 18; i++) {
500
+ const mid = (lo + hi) / 2;
501
+ const q2 = ballistic(origin, velocity, gravity, mid);
502
+ if (q2[1] - groundAt(q2[0], q2[2]) > 0) lo = mid;
503
+ else hi = mid;
504
+ }
505
+ if (best && hi >= best.t - 1e-3) return best;
506
+ const q = ballistic(origin, velocity, gravity, hi);
507
+ return { t: hi, point: [q[0], groundAt(q[0], q[2]), q[2]], normal: [0, 1, 0], surface: GROUND_ID, velocity: velocityAt(velocity, gravity, hi) };
508
+ }
509
+ prevT = tt;
510
+ if (tt >= limit) break;
511
+ }
512
+ return best;
513
+ };
514
+
515
+ // src/locale.ts
516
+ var locales = {
517
+ en: {
518
+ liquids: {
519
+ water: "water",
520
+ coffee: "coffee",
521
+ tea: "tea",
522
+ milk: "milk",
523
+ soda: "soda",
524
+ juice: "juice",
525
+ beer: "beer",
526
+ wine: "wine",
527
+ spirits: "spirits",
528
+ fuel: "gasoline",
529
+ diesel: "diesel",
530
+ motor_oil: "motor oil",
531
+ cooking_oil: "cooking oil",
532
+ blood: "blood",
533
+ sap: "sap",
534
+ acid: "acid",
535
+ paint: "paint",
536
+ bleach: "bleach",
537
+ honey: "honey"
538
+ },
539
+ events: {
540
+ fill: "filled",
541
+ pour: "poured",
542
+ spill: "spilled",
543
+ leak: "leaking",
544
+ drink: "drank",
545
+ empty: "emptied",
546
+ stream_end: "stream ended",
547
+ splash: "splash",
548
+ stain: "stain",
549
+ dried: "dried",
550
+ steam: "steam",
551
+ mix: "mixed",
552
+ tap: "tap"
553
+ },
554
+ on: "on",
555
+ off: "off"
556
+ },
557
+ "pt-BR": {
558
+ liquids: {
559
+ water: "\xE1gua",
560
+ coffee: "caf\xE9",
561
+ tea: "ch\xE1",
562
+ milk: "leite",
563
+ soda: "refrigerante",
564
+ juice: "suco",
565
+ beer: "cerveja",
566
+ wine: "vinho",
567
+ spirits: "destilado",
568
+ fuel: "gasolina",
569
+ diesel: "diesel",
570
+ motor_oil: "\xF3leo de motor",
571
+ cooking_oil: "\xF3leo de cozinha",
572
+ blood: "sangue",
573
+ sap: "seiva",
574
+ acid: "\xE1cido",
575
+ paint: "tinta",
576
+ bleach: "\xE1gua sanit\xE1ria",
577
+ honey: "mel"
578
+ },
579
+ events: {
580
+ fill: "encheu",
581
+ pour: "despejou",
582
+ spill: "derramou",
583
+ leak: "vazando",
584
+ drink: "bebeu",
585
+ empty: "esvaziou",
586
+ stream_end: "parou de escorrer",
587
+ splash: "respingo",
588
+ stain: "mancha",
589
+ dried: "secou",
590
+ steam: "vapor",
591
+ mix: "misturou",
592
+ tap: "torneira"
593
+ },
594
+ on: "ligado",
595
+ off: "desligado"
596
+ }
597
+ };
598
+ var resolveStrings = (locale) => locales[locale ?? "en"] ?? locales.en;
599
+ var fmt = (n) => `${(Math.round(n * 1e3) / 1e3).toString()} l`;
600
+ var pt = (p) => `(${p.map((v) => Math.round(v * 100) / 100).join(", ")})`;
601
+ var describeEvent = (strings, event) => {
602
+ const label = strings.events[event.type];
603
+ const name = (id) => strings.liquids[id] ?? id;
604
+ switch (event.type) {
605
+ case "fill":
606
+ return `${label}: ${event.container} ${name(event.liquid)} ${fmt(event.liters)}`;
607
+ case "pour":
608
+ case "spill":
609
+ return `${label}: ${event.container} ${name(event.liquid)} ${fmt(event.rate)}/s`;
610
+ case "leak":
611
+ return `${label}: ${event.container} ${name(event.liquid)} ${fmt(event.rate)}/s ${pt(event.point)}`;
612
+ case "drink":
613
+ return `${label}: ${event.container} ${name(event.liquid)} ${fmt(event.liters)}${event.effects.length ? ` [${event.effects.join(", ")}]` : ""}${event.burn ? " !" : ""}`;
614
+ case "empty":
615
+ return `${label}: ${event.container}`;
616
+ case "stream_end":
617
+ return `${label}: ${event.stream}`;
618
+ case "splash":
619
+ return `${label}: ${name(event.liquid)} ${pt(event.point)}`;
620
+ case "stain":
621
+ return `${label}: ${event.stain} ${name(event.liquid)} ${event.surface} ${pt(event.point)}`;
622
+ case "dried":
623
+ return `${label}: ${event.stain}${event.mark ? " (mark)" : ""}`;
624
+ case "steam":
625
+ return `${label}: ${event.container} ${event.on ? strings.on : strings.off}`;
626
+ case "mix":
627
+ return `${label}: ${event.container} ${name(event.from)} -> ${name(event.into)}`;
628
+ case "tap":
629
+ return `${label}: ${event.tap} ${event.on ? strings.on : strings.off}`;
630
+ }
631
+ };
632
+
633
+ // src/shapes.ts
634
+ var RINGS = 8;
635
+ var SEGMENTS = 16;
636
+ var GRID = 12;
637
+ var makeColumns = (shape) => {
638
+ if (shape.kind === "cylinder") {
639
+ const x2 = new Float64Array(RINGS * SEGMENTS);
640
+ const z2 = new Float64Array(RINGS * SEGMENTS);
641
+ let n2 = 0;
642
+ for (let k = 0; k < RINGS; k++) {
643
+ const rr = shape.radius * Math.sqrt((k + 0.5) / RINGS);
644
+ for (let m = 0; m < SEGMENTS; m++) {
645
+ const a = (m + k % 2 * 0.5) / SEGMENTS * Math.PI * 2;
646
+ x2[n2] = Math.cos(a) * rr;
647
+ z2[n2] = Math.sin(a) * rr;
648
+ n2++;
649
+ }
650
+ }
651
+ return { x: x2, z: z2, h: shape.height, kind: "cylinder", r: shape.radius, hx: shape.radius, hz: shape.radius };
652
+ }
653
+ const [sx, sy, sz] = shape.size;
654
+ const x = new Float64Array(GRID * GRID);
655
+ const z = new Float64Array(GRID * GRID);
656
+ let n = 0;
657
+ for (let i = 0; i < GRID; i++) {
658
+ for (let j = 0; j < GRID; j++) {
659
+ x[n] = ((i + 0.5) / GRID - 0.5) * sx;
660
+ z[n] = ((j + 0.5) / GRID - 0.5) * sz;
661
+ n++;
662
+ }
663
+ }
664
+ return { x, z, h: sy, kind: "box", r: Math.hypot(sx, sz) / 2, hx: sx / 2, hz: sz / 2 };
665
+ };
666
+ var planeRange = (c, u) => {
667
+ const horizontal = c.kind === "cylinder" ? c.r * Math.hypot(u[0], u[2]) : Math.abs(u[0]) * c.hx + Math.abs(u[2]) * c.hz;
668
+ const top = u[1] * c.h;
669
+ return [-horizontal + Math.min(0, top), horizontal + Math.max(0, top)];
670
+ };
671
+ var planeFraction = (c, u, d) => {
672
+ const uy = u[1];
673
+ let total = 0;
674
+ const n = c.x.length;
675
+ for (let i = 0; i < n; i++) {
676
+ const s0 = u[0] * c.x[i] + u[2] * c.z[i];
677
+ let len;
678
+ if (uy > 1e-6) len = clamp((d - s0) / uy, 0, c.h);
679
+ else if (uy < -1e-6) len = c.h - clamp((d - s0) / uy, 0, c.h);
680
+ else len = s0 <= d ? c.h : 0;
681
+ total += len;
682
+ }
683
+ return total / (n * c.h);
684
+ };
685
+ var levelFor = (c, u, f) => {
686
+ const [lo0, hi0] = planeRange(c, u);
687
+ if (f <= 0) return lo0;
688
+ if (f >= 1) return hi0;
689
+ if (Math.abs(u[0]) + Math.abs(u[2]) < 1e-9) {
690
+ return u[1] > 0 ? f * c.h * u[1] : u[1] * c.h - f * c.h * u[1];
691
+ }
692
+ let lo = lo0;
693
+ let hi = hi0;
694
+ for (let i = 0; i < 28; i++) {
695
+ const mid = (lo + hi) / 2;
696
+ if (planeFraction(c, u, mid) < f) lo = mid;
697
+ else hi = mid;
698
+ }
699
+ return (lo + hi) / 2;
700
+ };
701
+ var lipRadius = (shape, opening) => {
702
+ const limit = shape.kind === "cylinder" ? shape.radius : Math.min(shape.size[0], shape.size[2]) / 2;
703
+ return opening > 0 ? Math.min(opening, limit) : limit * 0.5;
704
+ };
705
+ var shapeHeight = (shape) => shape.kind === "cylinder" ? shape.height : shape.size[1];
706
+ var lipOf = (shape, opening, u) => {
707
+ const h = shapeHeight(shape);
708
+ const ro = lipRadius(shape, opening);
709
+ const center = [0, h, 0];
710
+ const uh = Math.hypot(u[0], u[2]);
711
+ if (uh < 1e-6) return { point: center, center, outward: null, level: u[1] * h };
712
+ const out = normalize([-u[0], 0, -u[2]]);
713
+ const point = [out[0] * ro, h, out[2] * ro];
714
+ return { point, center, outward: out, level: u[0] * point[0] + u[1] * point[1] + u[2] * point[2] };
715
+ };
716
+
717
+ // src/stains.ts
718
+ var SAMPLES = 16;
719
+ var COS = [];
720
+ var SIN = [];
721
+ for (let i = 0; i < SAMPLES; i++) {
722
+ COS.push(Math.cos(i / SAMPLES * Math.PI * 2));
723
+ SIN.push(Math.sin(i / SAMPLES * Math.PI * 2));
724
+ }
725
+ var makeBasis = (normalIn, tangentIn) => {
726
+ const normal = normalize(normalIn);
727
+ const ref = tangentIn && length(tangentIn) > 1e-6 ? tangentIn : Math.abs(normal[1]) > 0.9 ? [1, 0, 0] : [0, 1, 0];
728
+ const t1 = normalize(sub(ref, scale(normal, dot(ref, normal))), [1, 0, 0]);
729
+ return { normal, t1, t2: cross(normal, t1) };
730
+ };
731
+ var sampleDir = (b, i) => [
732
+ b.t1[0] * COS[i] + b.t2[0] * SIN[i],
733
+ b.t1[1] * COS[i] + b.t2[1] * SIN[i],
734
+ b.t1[2] * COS[i] + b.t2[2] * SIN[i]
735
+ ];
736
+ var stainArea = (front) => {
737
+ let a = 0;
738
+ for (let i = 0; i < SAMPLES; i++) a += front[i] * front[(i + 1) % SAMPLES];
739
+ return 0.5 * Math.sin(Math.PI * 2 / SAMPLES) * a;
740
+ };
741
+ var meanRadius = (front) => front.reduce((s, v) => s + v, 0) / front.length;
742
+ var minFilm = (def) => 1e-3 * (1 + 0.8 * Math.log10(Math.max(1, def.viscosity)));
743
+ var makeNoise = (rng) => {
744
+ const amp = [0.12 + rng.next() * 0.18, 0.08 + rng.next() * 0.14, 0.05 + rng.next() * 0.08];
745
+ const phase = [rng.next() * Math.PI * 2, rng.next() * Math.PI * 2, rng.next() * Math.PI * 2];
746
+ const raw = [];
747
+ for (let i = 0; i < SAMPLES; i++) {
748
+ const a = i / SAMPLES * Math.PI * 2;
749
+ let v = 1 + (rng.next() - 0.5) * 0.3;
750
+ for (let k = 0; k < 3; k++) v += amp[k] * Math.sin((k + 2) * a + phase[k]);
751
+ raw.push(v);
752
+ }
753
+ const out = raw.map((v, i) => 0.25 * raw[(i + SAMPLES - 1) % SAMPLES] + 0.5 * v + 0.25 * raw[(i + 1) % SAMPLES]);
754
+ return out.map((v) => clamp(v, 0.45, 1.7));
755
+ };
756
+ var downhill = (normal) => {
757
+ const d = add([0, -1, 0], scale(normal, normal[1]));
758
+ const slope = length(d);
759
+ return slope < 1e-6 ? { dir: [0, 0, 0], slope: 0 } : { dir: scale(d, 1 / slope), slope };
760
+ };
761
+ var createStain = (id, surfaceId, def, basis, center, liters, rng) => {
762
+ const r0 = clamp(Math.sqrt(liters / 1e3 / minFilm(def) / Math.PI) * 0.4, 0.012, 0.12);
763
+ return {
764
+ id,
765
+ surface: surfaceId,
766
+ liquid: def.id,
767
+ center: [center[0], center[1], center[2]],
768
+ front: new Array(SAMPLES).fill(r0),
769
+ liters,
770
+ soaked: 0,
771
+ wetness: 1,
772
+ wetRadius: 1,
773
+ normal: basis.normal,
774
+ tangent: basis.t1,
775
+ noise: makeNoise(rng),
776
+ sinceInflow: 0,
777
+ dry: 0,
778
+ mark: false,
779
+ age: 0
780
+ };
781
+ };
782
+ var addInflow = (s, liters, velocity) => {
783
+ s.liters += liters;
784
+ s.sinceInflow = 0;
785
+ if (s.dry > 0 || s.mark) {
786
+ s.dry = 0;
787
+ s.mark = false;
788
+ s.wetness = 1;
789
+ s.wetRadius = 1;
790
+ }
791
+ if (velocity) {
792
+ const n = s.normal;
793
+ const tangential = sub(velocity, scale(n, dot(velocity, n)));
794
+ if (length(tangential) > 0.2) s.flow = normalize(tangential);
795
+ }
796
+ };
797
+ var nudgeCenter = (s, point, weight, basis) => {
798
+ const delta = scale(sub(point, s.center), weight);
799
+ if (length(delta) < 1e-6) return;
800
+ s.center = add(s.center, delta);
801
+ for (let i = 0; i < SAMPLES; i++) s.front[i] = Math.max(8e-3, s.front[i] - dot(delta, sampleDir(basis, i)));
802
+ };
803
+ var edgeLimit = (s, surface, i) => {
804
+ if (!Number.isFinite(surface.half[0]) && !Number.isFinite(surface.half[1])) return Infinity;
805
+ const rel = sub(s.center, surface.point);
806
+ const cx = dot(rel, surface.t1);
807
+ const cy = dot(rel, surface.t2);
808
+ let limit = Infinity;
809
+ if (COS[i] > 1e-6) limit = Math.min(limit, (surface.half[0] - cx) / COS[i]);
810
+ else if (COS[i] < -1e-6) limit = Math.min(limit, (-surface.half[0] - cx) / COS[i]);
811
+ if (SIN[i] > 1e-6) limit = Math.min(limit, (surface.half[1] - cy) / SIN[i]);
812
+ else if (SIN[i] < -1e-6) limit = Math.min(limit, (-surface.half[1] - cy) / SIN[i]);
813
+ return Math.max(0.01, limit);
814
+ };
815
+ var stepStain = (s, def, surface, ambientC, dt) => {
816
+ s.age += dt;
817
+ s.sinceInflow += dt;
818
+ const basis = surface;
819
+ const { dir: down, slope } = downhill(s.normal);
820
+ if (s.liters > 0 && surface.porosity > 0) {
821
+ const soak = Math.min(s.liters, surface.porosity * 0.2 * stainArea(s.front) * dt);
822
+ s.liters -= soak;
823
+ s.soaked += soak;
824
+ }
825
+ if (s.liters > 1e-7 && s.wetness > 0) {
826
+ const area = stainArea(s.front);
827
+ const cover = s.liters / 1e3 / (minFilm(def) * (1 - 0.7 * slope));
828
+ if (area < cover) {
829
+ const taper = clamp(1 - area / cover, 0, 1);
830
+ const v0 = (0.02 + 0.2 * Math.sqrt(s.liters)) / Math.sqrt(Math.max(1, def.viscosity));
831
+ const delta = [];
832
+ for (let i = 0; i < SAMPLES; i++) {
833
+ let m = s.noise[i];
834
+ if (s.flow) m *= 1 + 0.5 * Math.max(0, dot(sampleDir(basis, i), s.flow));
835
+ if (slope > 0.02) {
836
+ const a = dot(sampleDir(basis, i), down);
837
+ m *= a > 0 ? 1 + 14 * slope * a : Math.max(0.1, 1 + 2.5 * slope * a);
838
+ }
839
+ delta.push(Math.max(0, Math.min(s.front[i] + v0 * m * taper * dt, edgeLimit(s, surface, i)) - s.front[i]));
840
+ }
841
+ let lambda = 1;
842
+ const apply = (k) => s.front.map((r, i) => r + delta[i] * k);
843
+ if (stainArea(apply(1)) > cover) {
844
+ let lo = 0;
845
+ let hi = 1;
846
+ for (let i = 0; i < 14; i++) {
847
+ const mid = (lo + hi) / 2;
848
+ if (stainArea(apply(mid)) > cover) hi = mid;
849
+ else lo = mid;
850
+ }
851
+ lambda = lo;
852
+ }
853
+ s.front = apply(lambda);
854
+ }
855
+ }
856
+ if (s.sinceInflow > 1 && def.dryTime > 0 && s.dry < 1) {
857
+ const warm = Math.pow(2, (ambientC - 20) / 12);
858
+ const ds = dt * warm * (1 + 0.5 * surface.porosity) / def.dryTime;
859
+ const before = s.dry;
860
+ s.dry = Math.min(1, s.dry + ds);
861
+ if (before < 1) s.liters *= 1 - (s.dry - before) / (1 - before);
862
+ s.wetness = 1 - s.dry;
863
+ s.wetRadius = Math.sqrt(Math.max(0, 1 - s.dry));
864
+ if (s.dry >= 1) {
865
+ s.liters = 0;
866
+ s.wetness = 0;
867
+ s.wetRadius = 0;
868
+ if (def.leavesMark) {
869
+ s.mark = true;
870
+ return "dried_mark";
871
+ }
872
+ return "dried_gone";
873
+ }
874
+ }
875
+ return "alive";
876
+ };
877
+ var stainDirection = (s, i) => sampleDir(makeBasis(s.normal ?? [0, 1, 0], s.tangent), i);
878
+
879
+ // src/world.ts
880
+ var GROUND_POROSITY = 0.2;
881
+ var MAX_STEP = 0.1;
882
+ var MAX_SUBSTEPS = 50;
883
+ var MAX_HOLES = 32;
884
+ var clone = (v) => JSON.parse(JSON.stringify(v));
885
+ function createFluids(options = {}) {
886
+ const strings = resolveStrings(options.locale);
887
+ const liquids = /* @__PURE__ */ new Map();
888
+ for (const def of LIQUIDS) liquids.set(def.id, def);
889
+ for (const def of options.liquids ?? []) liquids.set(def.id, def);
890
+ const ambientC = options.ambientC ?? 20;
891
+ const gravity = options.gravity ?? GRAVITY;
892
+ const groundAt = options.groundAt ?? (() => 0);
893
+ const mergeDistance = options.mergeDistance ?? 0.15;
894
+ const maxStains = options.maxStains ?? 64;
895
+ let rng = new Rng(options.seed ?? 1);
896
+ let time = 0;
897
+ const counters = { container: 0, stream: 0, stain: 0, tap: 0 };
898
+ let containers = /* @__PURE__ */ new Map();
899
+ let surfaces = /* @__PURE__ */ new Map();
900
+ let streams = /* @__PURE__ */ new Map();
901
+ let taps = /* @__PURE__ */ new Map();
902
+ let stains = /* @__PURE__ */ new Map();
903
+ const listeners = /* @__PURE__ */ new Map();
904
+ let queue = [];
905
+ let updating = false;
906
+ const groundSurface = {
907
+ normal: [0, 1, 0],
908
+ t1: [1, 0, 0],
909
+ t2: [0, 0, -1],
910
+ point: [0, 0, 0],
911
+ half: [Infinity, Infinity],
912
+ porosity: GROUND_POROSITY
913
+ };
914
+ const emit = (e) => {
915
+ if (updating) queue.push(e);
916
+ options.onEvent?.(e);
917
+ listeners.get(e.type)?.forEach((fn) => fn(e));
918
+ listeners.get("*")?.forEach((fn) => fn(e));
919
+ };
920
+ const toWorld = (c, p) => add(c.position, quatRotate(c.rotation, p));
921
+ const defOf = (c) => c.liquid ? liquids.get(c.liquid) ?? null : null;
922
+ const viscScale = (def) => 1 / Math.sqrt(Math.max(1, def.viscosity));
923
+ const refreshLevel = (c) => {
924
+ c.normal = normalize([c.slosh[0], 1, c.slosh[1]]);
925
+ c.u = normalize(quatRotate(quatConjugate(c.rotation), c.normal));
926
+ c.d = levelFor(c.cols, c.u, c.spec.capacity > 0 ? c.liters / c.spec.capacity : 0);
927
+ };
928
+ const surfaceFor = (id) => id === GROUND_ID ? groundSurface : surfaces.get(id) ?? groundSurface;
929
+ const landingSurfaces = () => {
930
+ const list = [];
931
+ for (const [id, s] of surfaces) list.push({ id, normal: s.normal, t1: s.t1, t2: s.t2, point: s.point, half: s.half });
932
+ return list;
933
+ };
934
+ const stopStream = (id) => {
935
+ const s = streams.get(id);
936
+ if (!s) return;
937
+ streams.delete(id);
938
+ const c = s.container ? containers.get(s.container) : void 0;
939
+ if (c) delete c.streams[s.key];
940
+ const tap = s.tap ? taps.get(s.tap) : void 0;
941
+ if (tap && tap.stream === id) tap.stream = void 0;
942
+ emit({ type: "stream_end", stream: id });
943
+ };
944
+ const endStreamKey = (c, key) => {
945
+ const id = c.streams[key];
946
+ if (id) stopStream(id);
947
+ };
948
+ const syncStream = (c, key, cause, rate, origin, velocity, visualOnly = false) => {
949
+ if (rate < 1e-5 || !c.liquid) {
950
+ endStreamKey(c, key);
951
+ return;
952
+ }
953
+ const existing = c.streams[key] ? streams.get(c.streams[key]) : void 0;
954
+ if (existing) {
955
+ existing.rate = rate;
956
+ existing.origin = origin;
957
+ existing.velocity = velocity;
958
+ existing.thin = rate < 0.04;
959
+ existing.visualOnly = visualOnly;
960
+ existing.temperature = c.temperature;
961
+ return;
962
+ }
963
+ const id = `stream_${++counters.stream}`;
964
+ const stream = {
965
+ id,
966
+ liquid: c.liquid,
967
+ cause,
968
+ container: c.id,
969
+ origin,
970
+ velocity,
971
+ rate,
972
+ landing: null,
973
+ thin: rate < 0.04,
974
+ flightTime: MAX_FLIGHT,
975
+ key,
976
+ lastSplash: -1,
977
+ visualOnly,
978
+ temperature: c.temperature
979
+ };
980
+ streams.set(id, stream);
981
+ c.streams[key] = id;
982
+ if (cause === "leak") emit({ type: "leak", container: c.id, liquid: c.liquid, rate, stream: id, point: copy3(origin) });
983
+ else if (cause === "spill") emit({ type: "spill", container: c.id, liquid: c.liquid, rate, stream: id });
984
+ else emit({ type: "pour", container: c.id, liquid: c.liquid, rate, stream: id });
985
+ };
986
+ const TAP_SPEED = 1.2;
987
+ const syncTap = (t) => {
988
+ if (!t.on || t.rate < 1e-5 || !liquids.has(t.liquid)) {
989
+ if (t.stream) stopStream(t.stream);
990
+ return;
991
+ }
992
+ const velocity = [t.direction[0] * TAP_SPEED, t.direction[1] * TAP_SPEED, t.direction[2] * TAP_SPEED];
993
+ const existing = t.stream ? streams.get(t.stream) : void 0;
994
+ if (existing) {
995
+ existing.rate = t.rate;
996
+ existing.thin = t.rate < 0.04;
997
+ existing.temperature = t.temperature;
998
+ existing.velocity = velocity;
999
+ return;
1000
+ }
1001
+ const id = `stream_${++counters.stream}`;
1002
+ streams.set(id, {
1003
+ id,
1004
+ liquid: t.liquid,
1005
+ cause: "tap",
1006
+ tap: t.id,
1007
+ origin: copy3(t.position),
1008
+ velocity,
1009
+ rate: t.rate,
1010
+ landing: null,
1011
+ thin: t.rate < 0.04,
1012
+ flightTime: MAX_FLIGHT,
1013
+ key: `tap:${t.id}`,
1014
+ lastSplash: -1,
1015
+ visualOnly: false,
1016
+ temperature: t.temperature
1017
+ });
1018
+ t.stream = id;
1019
+ };
1020
+ const lipFlow = (c, head) => {
1021
+ const lip = lipOf(c.spec.shape, c.spec.opening, c.u);
1022
+ const origin = toWorld(c, lip.point);
1023
+ let dir = [1, 0, 0];
1024
+ const horizontal = (v) => Math.hypot(v[0], v[2]) > 1e-3 ? normalize([v[0], 0, v[2]]) : null;
1025
+ if (lip.outward) dir = horizontal(quatRotate(c.rotation, lip.outward)) ?? horizontal(quatRotate(c.rotation, [1, 0, 0])) ?? dir;
1026
+ else dir = horizontal(quatRotate(c.rotation, [1, 0, 0])) ?? dir;
1027
+ const speed = clamp(0.3 + 0.5 * Math.sqrt(2 * gravity * Math.max(0, head)), 0.2, 2.5);
1028
+ return { origin, velocity: [dir[0] * speed, -0.1, dir[2] * speed] };
1029
+ };
1030
+ const removeLiters = (c, amount) => {
1031
+ c.liters = Math.max(0, c.liters - amount);
1032
+ };
1033
+ const settle = (c) => {
1034
+ if (c.liquid && c.liters <= 1e-6) {
1035
+ c.liters = 0;
1036
+ c.liquid = null;
1037
+ emit({ type: "empty", container: c.id });
1038
+ }
1039
+ };
1040
+ const fillInternal = (c, liquid, liters, temperature, announce) => {
1041
+ if (!(liters > 0) || !liquids.has(liquid)) return 0;
1042
+ const room = c.spec.capacity - c.liters;
1043
+ const accepted = Math.min(liters, Math.max(0, room));
1044
+ if (accepted <= 0) return 0;
1045
+ const t = temperature ?? ambientC;
1046
+ if (c.liquid && c.liquid !== liquid && c.liters > 1e-6) {
1047
+ const incomingWins = accepted > c.liters;
1048
+ const into = incomingWins ? liquid : c.liquid;
1049
+ const from = incomingWins ? c.liquid : liquid;
1050
+ if (announce || time - c.lastMix > 0.5) {
1051
+ c.lastMix = time;
1052
+ emit({ type: "mix", container: c.id, from, into });
1053
+ }
1054
+ c.liquid = into;
1055
+ } else if (!c.liquid || c.liters <= 1e-6) {
1056
+ c.liquid = liquid;
1057
+ }
1058
+ c.temperature = c.liters > 1e-6 ? (c.temperature * c.liters + t * accepted) / (c.liters + accepted) : t;
1059
+ c.liters += accepted;
1060
+ if (announce) emit({ type: "fill", container: c.id, liquid, liters: accepted });
1061
+ return accepted;
1062
+ };
1063
+ const stainAt = (surfaceId, liquid, point) => {
1064
+ let best = null;
1065
+ let bestD = Infinity;
1066
+ for (const s of stains.values()) {
1067
+ if (s.surface !== surfaceId || s.liquid !== liquid) continue;
1068
+ const d = distance(point, s.center);
1069
+ if (d < mergeDistance + 0.5 * meanRadius(s.front) && d < bestD) {
1070
+ best = s;
1071
+ bestD = d;
1072
+ }
1073
+ }
1074
+ return best;
1075
+ };
1076
+ const evictStain = () => {
1077
+ let victim = null;
1078
+ for (const s of stains.values()) {
1079
+ const rank = (x) => (x.wetness <= 0 ? 0 : 1) * 1e9 - x.age;
1080
+ if (!victim || rank(s) < rank(victim)) victim = s;
1081
+ }
1082
+ if (victim) stains.delete(victim.id);
1083
+ };
1084
+ const deposit = (liquid, hit, liters, velocity) => {
1085
+ const def = liquids.get(liquid);
1086
+ if (!def || liters <= 0) return;
1087
+ let s = stainAt(hit.surface, liquid, hit.point);
1088
+ if (s) {
1089
+ const basis2 = { normal: s.normal, t1: s.tangent, t2: cross(s.normal, s.tangent) };
1090
+ nudgeCenter(s, hit.point, clamp(liters / (s.liters + liters) * 0.5, 0, 0.5), basis2);
1091
+ addInflow(s, liters, velocity);
1092
+ return;
1093
+ }
1094
+ while (stains.size >= maxStains) evictStain();
1095
+ const surface = surfaceFor(hit.surface);
1096
+ const basis = hit.surface === GROUND_ID ? makeBasis(hit.normal) : surface;
1097
+ s = createStain(`stain_${++counters.stain}`, hit.surface, def, basis, hit.point, liters, rng);
1098
+ if (velocity) addInflow(s, 0, velocity);
1099
+ s.sinceInflow = 0;
1100
+ stains.set(s.id, s);
1101
+ emit({ type: "stain", stain: s.id, surface: hit.surface, liquid, point: copy3(hit.point) });
1102
+ };
1103
+ const lipCenterWorld = (c) => toWorld(c, [0, shapeHeight(c.spec.shape), 0]);
1104
+ const catcher = (s, landingT) => {
1105
+ let best = null;
1106
+ for (const oc of containers.values()) {
1107
+ if (oc.id === s.container || oc.closed || oc.liters >= oc.spec.capacity) continue;
1108
+ const lip = lipCenterWorld(oc);
1109
+ const disc = s.velocity[1] * s.velocity[1] - 2 * gravity * (lip[1] - s.origin[1]);
1110
+ if (disc < 0) continue;
1111
+ const t = (s.velocity[1] + Math.sqrt(disc)) / gravity;
1112
+ if (t <= 0 || t >= landingT || best && t >= best.t) continue;
1113
+ const p = ballistic(s.origin, s.velocity, gravity, t);
1114
+ if (Math.hypot(p[0] - lip[0], p[2] - lip[2]) <= lipRadius(oc.spec.shape, oc.spec.opening) + 0.01) best = { container: oc, t, point: p };
1115
+ }
1116
+ return best;
1117
+ };
1118
+ const stepStream = (s, dt) => {
1119
+ const hit = trace(landingSurfaces(), groundAt, s.origin, s.velocity, gravity);
1120
+ const caught = catcher(s, hit ? hit.t : MAX_FLIGHT);
1121
+ if (caught) {
1122
+ s.landing = caught.point;
1123
+ s.flightTime = caught.t;
1124
+ if (!s.visualOnly) fillInternal(caught.container, s.liquid, s.rate * dt, s.temperature, false);
1125
+ return;
1126
+ }
1127
+ s.landing = hit ? copy3(hit.point) : null;
1128
+ s.flightTime = hit ? hit.t : MAX_FLIGHT;
1129
+ if (!hit || s.visualOnly) return;
1130
+ const speed = length(hit.velocity);
1131
+ if (time - s.lastSplash >= 0.12) {
1132
+ s.lastSplash = time;
1133
+ emit({ type: "splash", liquid: s.liquid, point: copy3(hit.point), intensity: clamp(0.15 + s.rate * 1.2 + speed * 0.05, 0, 1) });
1134
+ }
1135
+ deposit(s.liquid, hit, s.rate * dt, hit.velocity);
1136
+ };
1137
+ const spillRate = (c, def, h) => {
1138
+ const ro = lipRadius(c.spec.shape, c.spec.opening);
1139
+ const area = 2 * ro * Math.min(h, ro);
1140
+ return Math.max(0.6 * area * Math.sqrt(2 * gravity * h) * 1e3 * viscScale(def), 4e-3);
1141
+ };
1142
+ const steamThreshold = (def) => def.steamC ?? (def.drink && def.drink.thirst > 0 ? 60 : Infinity);
1143
+ const stepContainer = (c, dt) => {
1144
+ if (c.hasPrev && dt > 0) {
1145
+ const ax = (c.velocity[0] - c.prevVelocity[0]) / dt;
1146
+ const az = (c.velocity[2] - c.prevVelocity[2]) / dt;
1147
+ const tx = clamp(ax / gravity, -0.35, 0.35);
1148
+ const tz = clamp(az / gravity, -0.35, 0.35);
1149
+ const w = 9;
1150
+ const z = 0.12;
1151
+ c.slosh[2] += (w * w * (tx - c.slosh[0]) - 2 * z * w * c.slosh[2]) * dt;
1152
+ c.slosh[3] += (w * w * (tz - c.slosh[1]) - 2 * z * w * c.slosh[3]) * dt;
1153
+ } else {
1154
+ c.slosh[2] *= 0.5;
1155
+ c.slosh[3] *= 0.5;
1156
+ }
1157
+ c.slosh[0] += c.slosh[2] * dt;
1158
+ c.slosh[1] += c.slosh[3] * dt;
1159
+ if (Math.abs(c.slosh[0]) + Math.abs(c.slosh[1]) + Math.abs(c.slosh[2]) + Math.abs(c.slosh[3]) < 1e-5) c.slosh = [0, 0, 0, 0];
1160
+ c.prevVelocity = copy3(c.velocity);
1161
+ c.hasPrev = true;
1162
+ if (c.temperature !== ambientC) {
1163
+ const tau = (600 + 2400 * c.liters) * (c.spec.insulated ? 10 : 1);
1164
+ c.temperature = ambientC + (c.temperature - ambientC) * Math.exp(-dt / tau);
1165
+ if (Math.abs(c.temperature - ambientC) < 0.01) c.temperature = ambientC;
1166
+ }
1167
+ refreshLevel(c);
1168
+ const def = defOf(c);
1169
+ const steaming = !!def && c.liters > 1e-6 && !c.closed && c.temperature > steamThreshold(def);
1170
+ if (steaming !== c.steaming) {
1171
+ c.steaming = steaming;
1172
+ emit({ type: "steam", container: c.id, on: steaming });
1173
+ }
1174
+ c.outflow = 0;
1175
+ if (!def || c.liters <= 1e-6) {
1176
+ for (const key of Object.keys(c.streams)) endStreamKey(c, key);
1177
+ c.pourTTL = 0;
1178
+ settle(c);
1179
+ return;
1180
+ }
1181
+ if (c.pourTTL > 0) {
1182
+ c.pourTTL -= dt;
1183
+ if (c.pourTTL > 0) {
1184
+ const flow = lipFlow(c, 0.05);
1185
+ syncStream(c, "pour", "pour", c.pourRate, flow.origin, flow.velocity, c.pourVisual);
1186
+ c.outflow += c.pourRate;
1187
+ } else endStreamKey(c, "pour");
1188
+ endStreamKey(c, "spill");
1189
+ } else {
1190
+ endStreamKey(c, "pour");
1191
+ const lip = lipOf(c.spec.shape, c.spec.opening, c.u);
1192
+ const h = c.d - lip.level;
1193
+ let handled = false;
1194
+ if (!c.closed && h > 1e-5) {
1195
+ const excess = c.liters - c.spec.capacity * planeFraction(c.cols, c.u, lip.level);
1196
+ if (excess > 1e-6) {
1197
+ const rate = Math.min(spillRate(c, def, h), excess / Math.max(dt, 1e-6));
1198
+ removeLiters(c, Math.min(rate * dt, excess, c.liters));
1199
+ const flow = lipFlow(c, h);
1200
+ syncStream(c, "spill", "spill", rate, flow.origin, flow.velocity);
1201
+ c.outflow += rate;
1202
+ handled = true;
1203
+ }
1204
+ }
1205
+ if (!handled) endStreamKey(c, "spill");
1206
+ }
1207
+ for (let i = 0; i < c.holes.length; i++) {
1208
+ const hole = c.holes[i];
1209
+ const key = `leak:${i}`;
1210
+ const hh = dot(c.u, hole.point);
1211
+ const head = c.d - hh;
1212
+ if (c.liters > 1e-6 && head > 1e-4) {
1213
+ const area = Math.PI * (hole.diameter / 2) ** 2;
1214
+ const natural = 0.6 * area * Math.sqrt(2 * gravity * head) * 1e3 * viscScale(def);
1215
+ const avail = c.liters - c.spec.capacity * planeFraction(c.cols, c.u, hh);
1216
+ if (avail > 1e-7) {
1217
+ const rate = Math.min(natural, avail / Math.max(dt, 1e-6));
1218
+ removeLiters(c, Math.min(rate * dt, avail));
1219
+ const nW = normalize(quatRotate(c.rotation, hole.normal));
1220
+ const v = Math.min(3, 0.6 * Math.sqrt(2 * gravity * head));
1221
+ syncStream(c, key, "leak", rate, toWorld(c, hole.point), [nW[0] * v, nW[1] * v - 0.05, nW[2] * v]);
1222
+ c.outflow += rate;
1223
+ continue;
1224
+ }
1225
+ }
1226
+ endStreamKey(c, key);
1227
+ }
1228
+ if (c.holes.length > 0 || c.outflow > 0) refreshLevel(c);
1229
+ settle(c);
1230
+ };
1231
+ const step = (dt) => {
1232
+ time += dt;
1233
+ for (const c of [...containers.values()]) stepContainer(c, dt);
1234
+ for (const s of [...streams.values()]) stepStream(s, dt);
1235
+ for (const st of [...stains.values()]) {
1236
+ const def = liquids.get(st.liquid);
1237
+ if (!def) continue;
1238
+ const status = stepStain(st, def, surfaceFor(st.surface), ambientC, dt);
1239
+ if (status === "dried_mark") emit({ type: "dried", stain: st.id, mark: true });
1240
+ else if (status === "dried_gone") {
1241
+ stains.delete(st.id);
1242
+ emit({ type: "dried", stain: st.id, mark: false });
1243
+ }
1244
+ }
1245
+ };
1246
+ const stateOf = (c) => ({
1247
+ id: c.id,
1248
+ liquid: c.liquid,
1249
+ liters: c.liters,
1250
+ capacity: c.spec.capacity,
1251
+ temperature: c.temperature,
1252
+ closed: c.closed,
1253
+ surfaceHeight: c.liters > 0 ? c.d : 0,
1254
+ surfacePoint: add(c.position, scale(c.normal, c.liters > 0 ? c.d : 0)),
1255
+ surfaceNormal: copy3(c.normal),
1256
+ holes: c.holes.map((h) => ({ point: copy3(h.point), normal: copy3(h.normal), diameter: h.diameter })),
1257
+ outflow: c.outflow,
1258
+ steaming: c.steaming,
1259
+ shape: c.spec.shape,
1260
+ lipRadius: lipRadius(c.spec.shape, c.spec.opening),
1261
+ position: copy3(c.position),
1262
+ rotation: [...c.rotation]
1263
+ });
1264
+ const streamView = (s) => ({
1265
+ id: s.id,
1266
+ liquid: s.liquid,
1267
+ cause: s.cause,
1268
+ container: s.container,
1269
+ tap: s.tap,
1270
+ origin: copy3(s.origin),
1271
+ velocity: copy3(s.velocity),
1272
+ rate: s.rate,
1273
+ landing: s.landing ? copy3(s.landing) : null,
1274
+ thin: s.thin,
1275
+ flightTime: s.flightTime
1276
+ });
1277
+ const stainView = (s) => ({
1278
+ id: s.id,
1279
+ surface: s.surface,
1280
+ liquid: s.liquid,
1281
+ center: copy3(s.center),
1282
+ front: [...s.front],
1283
+ liters: s.liters,
1284
+ soaked: s.soaked,
1285
+ wetness: s.wetness,
1286
+ wetRadius: s.wetRadius,
1287
+ normal: copy3(s.normal),
1288
+ tangent: copy3(s.tangent),
1289
+ flow: s.flow ? copy3(s.flow) : void 0,
1290
+ mark: s.mark,
1291
+ age: s.age
1292
+ });
1293
+ const world = {
1294
+ liquids,
1295
+ gravity,
1296
+ describe: (event) => describeEvent(strings, event),
1297
+ addContainer(spec, opts = {}) {
1298
+ const id = opts.id ?? `container_${++counters.container}`;
1299
+ if (containers.has(id)) world.removeContainer(id);
1300
+ const liquid = spec.liquid && liquids.has(spec.liquid) ? spec.liquid : null;
1301
+ const liters = liquid ? clamp(spec.liters ?? 0, 0, spec.capacity) : 0;
1302
+ const c = {
1303
+ id,
1304
+ spec: clone(spec),
1305
+ cols: makeColumns(spec.shape),
1306
+ liquid: liters > 0 ? liquid : null,
1307
+ liters,
1308
+ temperature: spec.temperature ?? ambientC,
1309
+ closed: spec.closed ?? spec.opening <= 0,
1310
+ position: copy3(opts.position ?? [0, 0, 0]),
1311
+ rotation: opts.rotation ? [...opts.rotation] : [0, 0, 0, 1],
1312
+ velocity: [0, 0, 0],
1313
+ prevVelocity: [0, 0, 0],
1314
+ hasPrev: false,
1315
+ holes: [],
1316
+ slosh: [0, 0, 0, 0],
1317
+ normal: [0, 1, 0],
1318
+ u: [0, 1, 0],
1319
+ d: 0,
1320
+ steaming: false,
1321
+ outflow: 0,
1322
+ streams: {},
1323
+ pourRate: 0,
1324
+ pourTTL: 0,
1325
+ pourVisual: false,
1326
+ lastMix: -10
1327
+ };
1328
+ containers.set(id, c);
1329
+ refreshLevel(c);
1330
+ return id;
1331
+ },
1332
+ addTap(spec) {
1333
+ const id = spec.id ?? `tap_${++counters.tap}`;
1334
+ if (taps.has(id)) world.removeTap(id);
1335
+ const d = spec.direction ?? [0, -1, 0];
1336
+ const len = Math.hypot(d[0], d[1], d[2]) || 1;
1337
+ const t = {
1338
+ id,
1339
+ position: copy3(spec.position),
1340
+ direction: [d[0] / len, d[1] / len, d[2] / len],
1341
+ liquid: spec.liquid,
1342
+ rate: Math.max(0, spec.rate),
1343
+ temperature: spec.temperature ?? ambientC,
1344
+ on: false
1345
+ };
1346
+ taps.set(id, t);
1347
+ if (spec.on ?? true) world.setTap(id, true);
1348
+ return id;
1349
+ },
1350
+ setTap(id, on, rate) {
1351
+ const t = taps.get(id);
1352
+ if (!t) return;
1353
+ if (rate !== void 0) t.rate = Math.max(0, rate);
1354
+ const changed = t.on !== on;
1355
+ t.on = on;
1356
+ syncTap(t);
1357
+ if (changed) emit({ type: "tap", tap: id, on });
1358
+ },
1359
+ removeTap(id) {
1360
+ const t = taps.get(id);
1361
+ if (!t) return;
1362
+ t.on = false;
1363
+ syncTap(t);
1364
+ taps.delete(id);
1365
+ },
1366
+ taps: () => [...taps.values()].map((t) => ({ id: t.id, position: copy3(t.position), direction: copy3(t.direction), liquid: t.liquid, rate: t.rate, temperature: t.temperature, on: t.on, stream: t.stream })),
1367
+ removeContainer(id) {
1368
+ const c = containers.get(id);
1369
+ if (!c) return;
1370
+ for (const key of Object.keys(c.streams)) endStreamKey(c, key);
1371
+ containers.delete(id);
1372
+ },
1373
+ setTransform(id, position, rotation, velocity) {
1374
+ const c = containers.get(id);
1375
+ if (!c) return;
1376
+ c.position = copy3(position);
1377
+ c.rotation = [...rotation];
1378
+ if (velocity) c.velocity = copy3(velocity);
1379
+ refreshLevel(c);
1380
+ },
1381
+ open(id) {
1382
+ const c = containers.get(id);
1383
+ if (c) c.closed = false;
1384
+ },
1385
+ close(id) {
1386
+ const c = containers.get(id);
1387
+ if (!c) return;
1388
+ c.closed = true;
1389
+ for (const key of ["pour", "spill"]) endStreamKey(c, key);
1390
+ c.pourTTL = 0;
1391
+ },
1392
+ fill(id, liquid, liters, temperature) {
1393
+ const c = containers.get(id);
1394
+ if (!c || c.closed) return 0;
1395
+ const accepted = fillInternal(c, liquid, liters, temperature, true);
1396
+ refreshLevel(c);
1397
+ return accepted;
1398
+ },
1399
+ pour(id, dt, into) {
1400
+ const c = containers.get(id);
1401
+ const def = c ? defOf(c) : null;
1402
+ if (!c || !def || c.closed || c.liters <= 1e-6 || !(dt > 0)) return 0;
1403
+ refreshLevel(c);
1404
+ const lip = lipOf(c.spec.shape, c.spec.opening, c.u);
1405
+ const depth = Math.max(0, c.d - planeRange(c.cols, c.u)[0]);
1406
+ const head = Math.max(c.d - lip.level, 0.02 + 0.5 * depth);
1407
+ const ro = lipRadius(c.spec.shape, c.spec.opening);
1408
+ const natural = 0.6 * Math.PI * ro * ro * Math.sqrt(2 * gravity * head) * 1e3;
1409
+ const rate = Math.min(natural, 0.15 + 12 * ro) * viscScale(def);
1410
+ const out = Math.min(rate * dt, c.liters);
1411
+ const target = into ? containers.get(into) : void 0;
1412
+ let delivered = out;
1413
+ if (target) {
1414
+ delivered = fillInternal(target, c.liquid, out, c.temperature, false);
1415
+ refreshLevel(target);
1416
+ const overflow = out - delivered;
1417
+ if (overflow > 1e-6) {
1418
+ const flow2 = lipFlow(c, head);
1419
+ const hit = trace(landingSurfaces(), groundAt, flow2.origin, flow2.velocity, gravity);
1420
+ if (hit) deposit(c.liquid, hit, overflow, hit.velocity);
1421
+ }
1422
+ }
1423
+ removeLiters(c, out);
1424
+ c.pourRate = out / dt;
1425
+ c.pourTTL = Math.max(0.12, dt * 1.5);
1426
+ c.pourVisual = !!target;
1427
+ const flow = lipFlow(c, head);
1428
+ syncStream(c, "pour", "pour", c.pourRate, flow.origin, flow.velocity, c.pourVisual);
1429
+ refreshLevel(c);
1430
+ settle(c);
1431
+ return out;
1432
+ },
1433
+ drink(id, liters) {
1434
+ const c = containers.get(id);
1435
+ const def = c ? defOf(c) : null;
1436
+ if (!c || !def || c.closed || c.liters <= 1e-6 || !(liters > 0)) return null;
1437
+ const amount = Math.min(liters, c.liters);
1438
+ removeLiters(c, amount);
1439
+ const event = {
1440
+ type: "drink",
1441
+ container: c.id,
1442
+ liquid: def.id,
1443
+ liters: amount,
1444
+ temperature: c.temperature,
1445
+ thirst: (def.drink?.thirst ?? 0) * amount,
1446
+ effects: [...def.drink?.effects ?? []],
1447
+ burn: c.temperature > 65
1448
+ };
1449
+ emit(event);
1450
+ refreshLevel(c);
1451
+ settle(c);
1452
+ return event;
1453
+ },
1454
+ puncture(id, point, normal, diameter) {
1455
+ const c = containers.get(id);
1456
+ if (!c || !(diameter > 0) || c.holes.length >= MAX_HOLES) return;
1457
+ c.holes.push({ point: copy3(point), normal: normalize(normal), diameter });
1458
+ },
1459
+ heat(id, deltaC) {
1460
+ const c = containers.get(id);
1461
+ if (c) c.temperature = clamp(c.temperature + deltaC, -50, 1e3);
1462
+ },
1463
+ addSurface(spec) {
1464
+ const basis = makeBasis(spec.normal, spec.tangent);
1465
+ surfaces.set(spec.id, {
1466
+ ...clone(spec),
1467
+ ...basis,
1468
+ point: copy3(spec.point),
1469
+ half: [spec.halfSize[0], spec.halfSize[1]]
1470
+ });
1471
+ },
1472
+ removeSurface(id) {
1473
+ surfaces.delete(id);
1474
+ for (const s of [...stains.values()]) if (s.surface === id) stains.delete(s.id);
1475
+ },
1476
+ splash(liquid, point, liters, velocity) {
1477
+ if (!liquids.has(liquid) || !(liters > 0)) return;
1478
+ const v = velocity ?? [0, -0.1, 0];
1479
+ const origin = add(point, [0, 0.02, 0]);
1480
+ const hit = trace(landingSurfaces(), groundAt, origin, v, gravity);
1481
+ if (!hit) return;
1482
+ emit({ type: "splash", liquid, point: copy3(hit.point), intensity: clamp(0.3 + liters * 2, 0, 1) });
1483
+ deposit(liquid, hit, liters, length(v) > 0.3 ? hit.velocity : null);
1484
+ },
1485
+ clearStains(point, radius) {
1486
+ for (const s of [...stains.values()]) {
1487
+ if (distance(point, s.center) <= radius + 0.5 * meanRadius(s.front)) {
1488
+ stains.delete(s.id);
1489
+ emit({ type: "dried", stain: s.id, mark: false });
1490
+ }
1491
+ }
1492
+ },
1493
+ update(dt) {
1494
+ if (dt > 0) {
1495
+ const n = clamp(Math.ceil(dt / MAX_STEP), 1, MAX_SUBSTEPS);
1496
+ updating = true;
1497
+ try {
1498
+ for (let i = 0; i < n; i++) step(dt / n);
1499
+ } finally {
1500
+ updating = false;
1501
+ }
1502
+ }
1503
+ const out = queue;
1504
+ queue = [];
1505
+ return out;
1506
+ },
1507
+ container: (id) => {
1508
+ const c = containers.get(id);
1509
+ return c ? stateOf(c) : null;
1510
+ },
1511
+ containers: () => [...containers.values()].map(stateOf),
1512
+ streams: () => [...streams.values()].map(streamView),
1513
+ stains: () => [...stains.values()].map(stainView),
1514
+ on(type, fn) {
1515
+ let set = listeners.get(type);
1516
+ if (!set) listeners.set(type, set = /* @__PURE__ */ new Set());
1517
+ set.add(fn);
1518
+ return () => {
1519
+ set?.delete(fn);
1520
+ };
1521
+ },
1522
+ serialize() {
1523
+ const snapshot = {
1524
+ version: 1,
1525
+ time,
1526
+ rng: rng.state,
1527
+ counters: { ...counters },
1528
+ containers: [...containers.values()].map((c) => {
1529
+ const { cols: _cols, ...rest } = c;
1530
+ return clone(rest);
1531
+ }),
1532
+ surfaces: [...surfaces.values()].map((s) => clone({ id: s.id, point: s.point, normal: s.normal, halfSize: s.half, tangent: s.t1, porosity: s.porosity })),
1533
+ streams: [...streams.values()].map((s) => clone(s)),
1534
+ taps: [...taps.values()].map((t) => clone(t)),
1535
+ stains: [...stains.values()].map((s) => clone(s))
1536
+ };
1537
+ return snapshot;
1538
+ },
1539
+ load(input) {
1540
+ const snap = clone(input);
1541
+ if (!snap || snap.version !== 1) throw new Error("game-fluids: unsupported snapshot");
1542
+ time = snap.time;
1543
+ rng = new Rng(0);
1544
+ rng.state = snap.rng;
1545
+ counters.container = snap.counters.container;
1546
+ counters.stream = snap.counters.stream;
1547
+ counters.stain = snap.counters.stain;
1548
+ containers = /* @__PURE__ */ new Map();
1549
+ for (const raw of snap.containers) containers.set(raw.id, { ...raw, cols: makeColumns(raw.spec.shape) });
1550
+ surfaces = /* @__PURE__ */ new Map();
1551
+ for (const s of snap.surfaces) world.addSurface(s);
1552
+ counters.tap = snap.counters.tap ?? 0;
1553
+ streams = new Map(snap.streams.map((s) => [s.id, s]));
1554
+ taps = new Map((snap.taps ?? []).map((t) => [t.id, t]));
1555
+ stains = new Map(snap.stains.map((s) => [s.id, s]));
1556
+ queue = [];
1557
+ }
1558
+ };
1559
+ return world;
1560
+ }
1561
+
1562
+ // src/data/item-uses.json
1563
+ var item_uses_default = [
1564
+ {
1565
+ id: "sip",
1566
+ duration: 1.2,
1567
+ keys: [
1568
+ {
1569
+ t: 0,
1570
+ position: [
1571
+ 0.22,
1572
+ -0.2,
1573
+ -0.4
1574
+ ],
1575
+ rotation: [
1576
+ 0,
1577
+ 0,
1578
+ 0
1579
+ ],
1580
+ action: "none"
1581
+ },
1582
+ {
1583
+ t: 0.35,
1584
+ position: [
1585
+ 0,
1586
+ 0.02,
1587
+ -0.2
1588
+ ],
1589
+ rotation: [
1590
+ 0,
1591
+ 0,
1592
+ 0
1593
+ ],
1594
+ action: "none"
1595
+ },
1596
+ {
1597
+ t: 0.45,
1598
+ position: [
1599
+ 0,
1600
+ 0.02,
1601
+ -0.2
1602
+ ],
1603
+ rotation: [
1604
+ -0.9,
1605
+ 0,
1606
+ 0
1607
+ ],
1608
+ action: "drink",
1609
+ rate: 0.06
1610
+ },
1611
+ {
1612
+ t: 0.95,
1613
+ position: [
1614
+ 0,
1615
+ 0.02,
1616
+ -0.2
1617
+ ],
1618
+ rotation: [
1619
+ -0.9,
1620
+ 0,
1621
+ 0
1622
+ ],
1623
+ action: "none"
1624
+ },
1625
+ {
1626
+ t: 1.2,
1627
+ position: [
1628
+ 0.22,
1629
+ -0.2,
1630
+ -0.4
1631
+ ],
1632
+ rotation: [
1633
+ 0,
1634
+ 0,
1635
+ 0
1636
+ ],
1637
+ action: "none"
1638
+ }
1639
+ ]
1640
+ },
1641
+ {
1642
+ id: "drink",
1643
+ duration: 2.5,
1644
+ keys: [
1645
+ {
1646
+ t: 0,
1647
+ position: [
1648
+ 0.22,
1649
+ -0.2,
1650
+ -0.4
1651
+ ],
1652
+ rotation: [
1653
+ 0,
1654
+ 0,
1655
+ 0
1656
+ ],
1657
+ action: "none"
1658
+ },
1659
+ {
1660
+ t: 0.5,
1661
+ position: [
1662
+ 0,
1663
+ 0.02,
1664
+ -0.2
1665
+ ],
1666
+ rotation: [
1667
+ 0,
1668
+ 0,
1669
+ 0
1670
+ ],
1671
+ action: "none"
1672
+ },
1673
+ {
1674
+ t: 0.8,
1675
+ position: [
1676
+ 0,
1677
+ 0.02,
1678
+ -0.2
1679
+ ],
1680
+ rotation: [
1681
+ -1.22,
1682
+ 0,
1683
+ 0
1684
+ ],
1685
+ action: "drink",
1686
+ rate: 0.1667
1687
+ },
1688
+ {
1689
+ t: 2,
1690
+ position: [
1691
+ 0,
1692
+ 0.02,
1693
+ -0.2
1694
+ ],
1695
+ rotation: [
1696
+ -1.22,
1697
+ 0,
1698
+ 0
1699
+ ],
1700
+ action: "none"
1701
+ },
1702
+ {
1703
+ t: 2.5,
1704
+ position: [
1705
+ 0.22,
1706
+ -0.2,
1707
+ -0.4
1708
+ ],
1709
+ rotation: [
1710
+ 0,
1711
+ 0,
1712
+ 0
1713
+ ],
1714
+ action: "none"
1715
+ }
1716
+ ]
1717
+ },
1718
+ {
1719
+ id: "chug",
1720
+ duration: 4,
1721
+ keys: [
1722
+ {
1723
+ t: 0,
1724
+ position: [
1725
+ 0.22,
1726
+ -0.2,
1727
+ -0.4
1728
+ ],
1729
+ rotation: [
1730
+ 0,
1731
+ 0,
1732
+ 0
1733
+ ],
1734
+ action: "none"
1735
+ },
1736
+ {
1737
+ t: 0.5,
1738
+ position: [
1739
+ 0,
1740
+ 0.02,
1741
+ -0.2
1742
+ ],
1743
+ rotation: [
1744
+ 0,
1745
+ 0,
1746
+ 0
1747
+ ],
1748
+ action: "none"
1749
+ },
1750
+ {
1751
+ t: 0.9,
1752
+ position: [
1753
+ 0,
1754
+ 0.02,
1755
+ -0.2
1756
+ ],
1757
+ rotation: [
1758
+ -1.45,
1759
+ 0,
1760
+ 0
1761
+ ],
1762
+ action: "drink",
1763
+ rate: 0.2
1764
+ },
1765
+ {
1766
+ t: 3.4,
1767
+ position: [
1768
+ 0,
1769
+ 0.02,
1770
+ -0.2
1771
+ ],
1772
+ rotation: [
1773
+ -1.45,
1774
+ 0,
1775
+ 0
1776
+ ],
1777
+ action: "none"
1778
+ },
1779
+ {
1780
+ t: 4,
1781
+ position: [
1782
+ 0.22,
1783
+ -0.2,
1784
+ -0.4
1785
+ ],
1786
+ rotation: [
1787
+ 0,
1788
+ 0,
1789
+ 0
1790
+ ],
1791
+ action: "none"
1792
+ }
1793
+ ]
1794
+ },
1795
+ {
1796
+ id: "pour",
1797
+ duration: 3,
1798
+ keys: [
1799
+ {
1800
+ t: 0,
1801
+ position: [
1802
+ 0.22,
1803
+ -0.2,
1804
+ -0.4
1805
+ ],
1806
+ rotation: [
1807
+ 0,
1808
+ 0,
1809
+ 0
1810
+ ],
1811
+ action: "none"
1812
+ },
1813
+ {
1814
+ t: 0.4,
1815
+ position: [
1816
+ 0.1,
1817
+ 0,
1818
+ -0.35
1819
+ ],
1820
+ rotation: [
1821
+ 0,
1822
+ 0,
1823
+ 0
1824
+ ],
1825
+ action: "none"
1826
+ },
1827
+ {
1828
+ t: 0.8,
1829
+ position: [
1830
+ 0.1,
1831
+ 0,
1832
+ -0.35
1833
+ ],
1834
+ rotation: [
1835
+ -1.745,
1836
+ 0,
1837
+ 0
1838
+ ],
1839
+ action: "pour",
1840
+ rate: 1
1841
+ },
1842
+ {
1843
+ t: 2.4,
1844
+ position: [
1845
+ 0.1,
1846
+ 0,
1847
+ -0.35
1848
+ ],
1849
+ rotation: [
1850
+ -1.745,
1851
+ 0,
1852
+ 0
1853
+ ],
1854
+ action: "none"
1855
+ },
1856
+ {
1857
+ t: 3,
1858
+ position: [
1859
+ 0.22,
1860
+ -0.2,
1861
+ -0.4
1862
+ ],
1863
+ rotation: [
1864
+ 0,
1865
+ 0,
1866
+ 0
1867
+ ],
1868
+ action: "none"
1869
+ }
1870
+ ]
1871
+ },
1872
+ {
1873
+ id: "pour_out",
1874
+ duration: 4,
1875
+ keys: [
1876
+ {
1877
+ t: 0,
1878
+ position: [
1879
+ 0.22,
1880
+ -0.2,
1881
+ -0.4
1882
+ ],
1883
+ rotation: [
1884
+ 0,
1885
+ 0,
1886
+ 0
1887
+ ],
1888
+ action: "none"
1889
+ },
1890
+ {
1891
+ t: 0.5,
1892
+ position: [
1893
+ 0.1,
1894
+ 0.1,
1895
+ -0.35
1896
+ ],
1897
+ rotation: [
1898
+ 0,
1899
+ 0,
1900
+ 0
1901
+ ],
1902
+ action: "none"
1903
+ },
1904
+ {
1905
+ t: 1,
1906
+ position: [
1907
+ 0.1,
1908
+ 0.1,
1909
+ -0.35
1910
+ ],
1911
+ rotation: [
1912
+ -2.2,
1913
+ 0,
1914
+ 0
1915
+ ],
1916
+ action: "pour",
1917
+ rate: 40
1918
+ },
1919
+ {
1920
+ t: 3.2,
1921
+ position: [
1922
+ 0.1,
1923
+ 0.1,
1924
+ -0.35
1925
+ ],
1926
+ rotation: [
1927
+ -2.2,
1928
+ 0,
1929
+ 0
1930
+ ],
1931
+ action: "none"
1932
+ },
1933
+ {
1934
+ t: 4,
1935
+ position: [
1936
+ 0.22,
1937
+ -0.2,
1938
+ -0.4
1939
+ ],
1940
+ rotation: [
1941
+ 0,
1942
+ 0,
1943
+ 0
1944
+ ],
1945
+ action: "none"
1946
+ }
1947
+ ]
1948
+ },
1949
+ {
1950
+ id: "open",
1951
+ duration: 1,
1952
+ keys: [
1953
+ {
1954
+ t: 0,
1955
+ position: [
1956
+ 0.22,
1957
+ -0.2,
1958
+ -0.4
1959
+ ],
1960
+ rotation: [
1961
+ 0,
1962
+ 0,
1963
+ 0
1964
+ ],
1965
+ action: "none"
1966
+ },
1967
+ {
1968
+ t: 0.25,
1969
+ position: [
1970
+ 0.1,
1971
+ 0,
1972
+ -0.3
1973
+ ],
1974
+ rotation: [
1975
+ 0,
1976
+ 0,
1977
+ 0
1978
+ ],
1979
+ action: "open"
1980
+ },
1981
+ {
1982
+ t: 0.7,
1983
+ position: [
1984
+ 0.1,
1985
+ 0,
1986
+ -0.3
1987
+ ],
1988
+ rotation: [
1989
+ 0,
1990
+ 0,
1991
+ 0
1992
+ ],
1993
+ action: "none"
1994
+ },
1995
+ {
1996
+ t: 1,
1997
+ position: [
1998
+ 0.22,
1999
+ -0.2,
2000
+ -0.4
2001
+ ],
2002
+ rotation: [
2003
+ 0,
2004
+ 0,
2005
+ 0
2006
+ ],
2007
+ action: "none"
2008
+ }
2009
+ ]
2010
+ },
2011
+ {
2012
+ id: "close",
2013
+ duration: 1,
2014
+ keys: [
2015
+ {
2016
+ t: 0,
2017
+ position: [
2018
+ 0.22,
2019
+ -0.2,
2020
+ -0.4
2021
+ ],
2022
+ rotation: [
2023
+ 0,
2024
+ 0,
2025
+ 0
2026
+ ],
2027
+ action: "none"
2028
+ },
2029
+ {
2030
+ t: 0.25,
2031
+ position: [
2032
+ 0.1,
2033
+ 0,
2034
+ -0.3
2035
+ ],
2036
+ rotation: [
2037
+ 0,
2038
+ 0,
2039
+ 0
2040
+ ],
2041
+ action: "close"
2042
+ },
2043
+ {
2044
+ t: 0.7,
2045
+ position: [
2046
+ 0.1,
2047
+ 0,
2048
+ -0.3
2049
+ ],
2050
+ rotation: [
2051
+ 0,
2052
+ 0,
2053
+ 0
2054
+ ],
2055
+ action: "none"
2056
+ },
2057
+ {
2058
+ t: 1,
2059
+ position: [
2060
+ 0.22,
2061
+ -0.2,
2062
+ -0.4
2063
+ ],
2064
+ rotation: [
2065
+ 0,
2066
+ 0,
2067
+ 0
2068
+ ],
2069
+ action: "none"
2070
+ }
2071
+ ]
2072
+ }
2073
+ ];
2074
+
2075
+ // src/item-use.ts
2076
+ var ITEM_USES = item_uses_default;
2077
+ var DEFAULT_DRINK_RATE = 0.1;
2078
+ function createItemUse(world, containerId, def, getHandTransform) {
2079
+ const keys = [...def.keys].sort((a, b) => a.t - b.t);
2080
+ let time = 0;
2081
+ let done = keys.length === 0 || def.duration <= 0;
2082
+ let checked = false;
2083
+ let lastPos = null;
2084
+ const fired = /* @__PURE__ */ new Set();
2085
+ const pose = () => {
2086
+ if (keys.length === 0) return { position: [0, 0, 0], rotation: [0, 0, 0] };
2087
+ if (time <= keys[0].t) return { position: [...keys[0].position], rotation: [...keys[0].rotation] };
2088
+ for (let i = 0; i < keys.length - 1; i++) {
2089
+ const a = keys[i];
2090
+ const b = keys[i + 1];
2091
+ if (time <= b.t) {
2092
+ const k = b.t > a.t ? (time - a.t) / (b.t - a.t) : 1;
2093
+ return { position: lerp3(a.position, b.position, k), rotation: lerp3(a.rotation, b.rotation, k) };
2094
+ }
2095
+ }
2096
+ const last = keys[keys.length - 1];
2097
+ return { position: [...last.position], rotation: [...last.rotation] };
2098
+ };
2099
+ const follow = (dt) => {
2100
+ if (!getHandTransform) return;
2101
+ const hand = getHandTransform();
2102
+ const p = pose();
2103
+ const position = add(hand.position, quatRotate(hand.rotation, p.position));
2104
+ const rotation = quatMultiply(hand.rotation, quatFromEuler(p.rotation[0], p.rotation[1], p.rotation[2]));
2105
+ const velocity = lastPos && dt > 0 ? [(position[0] - lastPos[0]) / dt, (position[1] - lastPos[1]) / dt, (position[2] - lastPos[2]) / dt] : void 0;
2106
+ lastPos = position;
2107
+ world.setTransform(containerId, position, rotation, velocity);
2108
+ };
2109
+ const player = {
2110
+ def,
2111
+ get time() {
2112
+ return time;
2113
+ },
2114
+ get done() {
2115
+ return done;
2116
+ },
2117
+ pose,
2118
+ update(dt) {
2119
+ if (done) return [];
2120
+ const events = [];
2121
+ const off = world.on("*", (e) => events.push(e));
2122
+ try {
2123
+ if (!checked) {
2124
+ checked = true;
2125
+ const state = world.container(containerId);
2126
+ if (!state || def.liquids && def.liquids.length > 0 && (!state.liquid || !def.liquids.includes(state.liquid))) {
2127
+ done = true;
2128
+ return events;
2129
+ }
2130
+ }
2131
+ const start = time;
2132
+ const end = Math.min(def.duration, time + Math.max(0, dt));
2133
+ for (let i = 0; i < keys.length && !done; i++) {
2134
+ const key = keys[i];
2135
+ const spanStart = key.t;
2136
+ const spanEnd = i + 1 < keys.length ? keys[i + 1].t : def.duration;
2137
+ if ((key.action === "open" || key.action === "close") && !fired.has(i) && end >= spanStart) {
2138
+ fired.add(i);
2139
+ if (key.action === "open") world.open(containerId);
2140
+ else world.close(containerId);
2141
+ }
2142
+ const overlap = Math.min(end, spanEnd) - Math.max(start, spanStart);
2143
+ if (overlap <= 0) continue;
2144
+ if (key.action === "drink") {
2145
+ const ev = world.drink(containerId, (key.rate ?? DEFAULT_DRINK_RATE) * overlap);
2146
+ const left = world.container(containerId);
2147
+ if (!ev || !left || left.liters <= 1e-6) {
2148
+ time = def.duration;
2149
+ done = true;
2150
+ }
2151
+ } else if (key.action === "pour") {
2152
+ world.pour(containerId, overlap * (key.rate ?? 1));
2153
+ }
2154
+ }
2155
+ if (!done) {
2156
+ time = end;
2157
+ done = time >= def.duration;
2158
+ }
2159
+ follow(dt);
2160
+ } finally {
2161
+ off();
2162
+ }
2163
+ return events;
2164
+ },
2165
+ cancel() {
2166
+ done = true;
2167
+ }
2168
+ };
2169
+ return player;
2170
+ }
2171
+ export {
2172
+ ITEM_USES,
2173
+ LIQUIDS,
2174
+ SAMPLES as STAIN_SAMPLES,
2175
+ ballistic,
2176
+ createFluids,
2177
+ createItemUse,
2178
+ describeEvent,
2179
+ locales,
2180
+ resolveStrings,
2181
+ sampleStream,
2182
+ stainDirection
2183
+ };
2184
+ //# sourceMappingURL=index.js.map