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