@adatechnology/game-interactables 0.12.0 → 0.13.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/README.md +17 -0
- package/dist/index.d.ts +248 -11
- package/dist/index.global.js +732 -41
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +640 -41
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,3 +1,196 @@
|
|
|
1
|
+
// src/basin-drain.ts
|
|
2
|
+
var FLUSH_POUR_SECONDS = 3;
|
|
3
|
+
var CLOGGED = 0.5;
|
|
4
|
+
var REFILL_RATE = 0.12;
|
|
5
|
+
var BREAK_DEPTH = 0.035;
|
|
6
|
+
var WEIR_SHARE = 0.25;
|
|
7
|
+
var SEEP = 0.04;
|
|
8
|
+
var TURN_PER_LITRE = 2.4;
|
|
9
|
+
var TRAP_LITRES = 3.5;
|
|
10
|
+
var PLUNGE_PACKED = 0.34;
|
|
11
|
+
var PLUNGE_LODGED = 0.2;
|
|
12
|
+
var PLUNGER_WATER = 0.03;
|
|
13
|
+
var SOFT_SQUEEZE = 1.6;
|
|
14
|
+
var SWIRL_SETTLED = 1e-4;
|
|
15
|
+
var clamp01 = (v) => Math.min(1, Math.max(0, v));
|
|
16
|
+
var passingSize = (size) => [...size].sort((a, b) => b - a)[1] ?? 0;
|
|
17
|
+
function createBasinDrain(basin, drain) {
|
|
18
|
+
const width = basin.to[0] - basin.from[0];
|
|
19
|
+
const deep = basin.to[2] - basin.from[2];
|
|
20
|
+
const radius = Math.min(width, deep) / 2;
|
|
21
|
+
const area = basin.shape === "round" ? Math.PI * radius * radius : width * deep;
|
|
22
|
+
const litresAt = (level) => area * level * 1e3;
|
|
23
|
+
const levelOf = (litres) => litres / (area * 1e3);
|
|
24
|
+
const capacity = litresAt(basin.to[1] - basin.from[1]);
|
|
25
|
+
const still = litresAt(drain.weir);
|
|
26
|
+
const water = { litres: still, phase: "still", swirl: 0, turned: 0, clog: 0, cause: "none", cistern: 1, pouring: 0, pourRate: 0, carried: 0 };
|
|
27
|
+
let serial = 0;
|
|
28
|
+
let contents = [];
|
|
29
|
+
let shown = null;
|
|
30
|
+
const step = { spilled: 0, swallowed: [] };
|
|
31
|
+
const open = () => Math.max(SEEP, 1 - water.clog);
|
|
32
|
+
const lodge = (c, packed) => {
|
|
33
|
+
c.stage = "lodged";
|
|
34
|
+
c.depth = 1;
|
|
35
|
+
c.radius = 0;
|
|
36
|
+
water.clog = packed ? Math.max(water.clog, Math.min(0.95, water.clog + 0.55 + 0.3 * (c.kg / drain.carries))) : 0.97;
|
|
37
|
+
water.cause = water.cause === "object" || !packed ? "object" : "packed";
|
|
38
|
+
};
|
|
39
|
+
const reachTrap = (c, swallowed) => {
|
|
40
|
+
const hard = c.kind === "object";
|
|
41
|
+
if (passingSize(c.size) > drain.trap * (hard ? 1 : SOFT_SQUEEZE) || !hard && water.carried + c.kg > drain.carries) {
|
|
42
|
+
lodge(c, !hard);
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
if (!hard) water.carried += c.kg;
|
|
46
|
+
c.stage = "gone";
|
|
47
|
+
if (c.objectId) swallowed.push(c.objectId);
|
|
48
|
+
return true;
|
|
49
|
+
};
|
|
50
|
+
const stepContents = (moved, pulled, swallowed) => {
|
|
51
|
+
water.turned += moved * TURN_PER_LITRE;
|
|
52
|
+
const takesUrine = water.phase === "siphon" && water.swirl > 0.3;
|
|
53
|
+
let leaving = false;
|
|
54
|
+
for (const c of contents) {
|
|
55
|
+
if (c.kind === "urine" && takesUrine) leaving = true;
|
|
56
|
+
if (c.stage === "gone" || c.stage === "lodged") continue;
|
|
57
|
+
c.angle = c.start + water.turned;
|
|
58
|
+
if (water.phase !== "siphon" || c.kind === "urine") continue;
|
|
59
|
+
c.stage = "going";
|
|
60
|
+
c.travel = clamp01(c.travel + pulled / TRAP_LITRES);
|
|
61
|
+
c.depth = Math.max(c.depth, c.travel);
|
|
62
|
+
c.radius = c.startRadius * (1 - c.travel);
|
|
63
|
+
if (c.travel >= 1 && reachTrap(c, swallowed)) leaving = true;
|
|
64
|
+
}
|
|
65
|
+
if (leaving) contents = contents.filter((c) => c.stage !== "gone" && !(c.kind === "urine" && takesUrine));
|
|
66
|
+
};
|
|
67
|
+
const outflow = (dt) => {
|
|
68
|
+
const level = levelOf(water.litres);
|
|
69
|
+
if (water.phase === "siphon") return drain.siphon * open() * dt * Math.sqrt(clamp01(level / Math.max(1e-3, drain.weir)));
|
|
70
|
+
const head = level - drain.weir;
|
|
71
|
+
if (head <= 0) return 0;
|
|
72
|
+
return drain.siphon * WEIR_SHARE * open() * dt * Math.sqrt(clamp01(head / drain.prime));
|
|
73
|
+
};
|
|
74
|
+
const stepPhase = () => {
|
|
75
|
+
const head = levelOf(water.litres) - drain.weir;
|
|
76
|
+
if (water.phase !== "siphon" && head >= drain.prime && open() > CLOGGED) {
|
|
77
|
+
water.phase = "siphon";
|
|
78
|
+
water.carried = 0;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (water.phase === "siphon" && (head <= -BREAK_DEPTH || open() <= CLOGGED)) water.phase = "refill";
|
|
82
|
+
if (water.phase === "refill" && water.litres >= still - 1e-6 && water.pouring <= 0) water.phase = "still";
|
|
83
|
+
if (water.phase === "still" && water.pouring > 0) water.phase = "filling";
|
|
84
|
+
if (water.phase === "filling" && water.pouring <= 0 && head <= 1e-4) water.phase = "still";
|
|
85
|
+
};
|
|
86
|
+
const stepInflow = (dt) => {
|
|
87
|
+
const poured = water.pouring > 0 ? water.pourRate * Math.min(dt, water.pouring) : 0;
|
|
88
|
+
water.pouring = Math.max(0, water.pouring - dt);
|
|
89
|
+
water.litres += poured;
|
|
90
|
+
if ((water.phase === "refill" || water.phase === "still") && water.litres < still && water.clog < CLOGGED) water.litres = Math.min(still, water.litres + REFILL_RATE * dt);
|
|
91
|
+
return poured;
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
flush(asked) {
|
|
95
|
+
shown = null;
|
|
96
|
+
const litresIn = asked * water.cistern;
|
|
97
|
+
water.cistern = 0;
|
|
98
|
+
if (litresIn <= 0) return 0;
|
|
99
|
+
water.pouring = FLUSH_POUR_SECONDS;
|
|
100
|
+
water.pourRate = litresIn / FLUSH_POUR_SECONDS;
|
|
101
|
+
if (water.phase === "still") water.phase = "filling";
|
|
102
|
+
return litresIn;
|
|
103
|
+
},
|
|
104
|
+
add(content) {
|
|
105
|
+
shown = null;
|
|
106
|
+
water.litres += content.litres ?? 0;
|
|
107
|
+
const k = serial++;
|
|
108
|
+
const start = k * 2.39996;
|
|
109
|
+
const startRadius = 0.25 + k * 0.37 % 0.45;
|
|
110
|
+
contents.push({
|
|
111
|
+
start,
|
|
112
|
+
startRadius,
|
|
113
|
+
travel: 0,
|
|
114
|
+
id: `c${k}`,
|
|
115
|
+
kind: content.kind,
|
|
116
|
+
kg: content.kg,
|
|
117
|
+
size: content.size,
|
|
118
|
+
...content.objectId ? { objectId: content.objectId } : {},
|
|
119
|
+
stage: "afloat",
|
|
120
|
+
angle: start + water.turned,
|
|
121
|
+
radius: startRadius,
|
|
122
|
+
depth: content.kind === "object" ? 0.5 : 0
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
plunge() {
|
|
126
|
+
shown = null;
|
|
127
|
+
if (levelOf(water.litres) < PLUNGER_WATER) return false;
|
|
128
|
+
if (water.clog <= 0) return true;
|
|
129
|
+
const lodged = contents.filter((c) => c.stage === "lodged");
|
|
130
|
+
const hard = lodged.some((c) => c.kind === "object");
|
|
131
|
+
water.clog = Math.max(0, water.clog - (hard ? PLUNGE_LODGED : PLUNGE_PACKED));
|
|
132
|
+
if (water.clog > CLOGGED * 0.4) return true;
|
|
133
|
+
water.clog = 0;
|
|
134
|
+
water.cause = "none";
|
|
135
|
+
for (const c of lodged) {
|
|
136
|
+
if (c.kind === "object") Object.assign(c, { stage: "afloat", depth: 0.6, radius: 0.3, startRadius: 0.3, travel: 0 });
|
|
137
|
+
else c.stage = "gone";
|
|
138
|
+
}
|
|
139
|
+
contents = contents.filter((c) => c.stage !== "gone");
|
|
140
|
+
return true;
|
|
141
|
+
},
|
|
142
|
+
update(dt) {
|
|
143
|
+
step.spilled = 0;
|
|
144
|
+
step.swallowed.length = 0;
|
|
145
|
+
if (dt <= 0) return step;
|
|
146
|
+
const { litres, swirl, cistern, phase, clog } = water;
|
|
147
|
+
const count = contents.length;
|
|
148
|
+
water.cistern = clamp01(water.cistern + dt / drain.refill);
|
|
149
|
+
const poured = stepInflow(dt);
|
|
150
|
+
const out = Math.min(water.litres, outflow(dt));
|
|
151
|
+
water.litres -= out;
|
|
152
|
+
stepPhase();
|
|
153
|
+
const pull = water.phase === "siphon" ? clamp01(out / (drain.siphon * dt)) : 0;
|
|
154
|
+
const jets = poured > 0 ? 0.35 : 0;
|
|
155
|
+
const swirlTo = Math.max(pull, jets);
|
|
156
|
+
water.swirl = Math.abs(swirlTo - water.swirl) < SWIRL_SETTLED ? swirlTo : water.swirl + (swirlTo - water.swirl) * clamp01(dt * 6);
|
|
157
|
+
const moved = poured + out;
|
|
158
|
+
stepContents(moved, water.phase === "siphon" ? out : 0, step.swallowed);
|
|
159
|
+
step.spilled = Math.max(0, water.litres - capacity);
|
|
160
|
+
water.litres -= step.spilled;
|
|
161
|
+
const changed = moved > 0 || water.litres !== litres || water.swirl !== swirl || water.cistern !== cistern || water.phase !== phase || water.clog !== clog || contents.length !== count;
|
|
162
|
+
if (changed) shown = null;
|
|
163
|
+
return step;
|
|
164
|
+
},
|
|
165
|
+
state() {
|
|
166
|
+
shown ?? (shown = {
|
|
167
|
+
litres: water.litres,
|
|
168
|
+
level: levelOf(water.litres),
|
|
169
|
+
phase: water.phase,
|
|
170
|
+
swirl: water.swirl,
|
|
171
|
+
turned: water.turned,
|
|
172
|
+
clog: water.clog,
|
|
173
|
+
cause: water.cause,
|
|
174
|
+
cistern: water.cistern,
|
|
175
|
+
contents: contents.map(({ start: _start, startRadius: _startRadius, travel: _travel, ...c }) => c)
|
|
176
|
+
});
|
|
177
|
+
return shown;
|
|
178
|
+
},
|
|
179
|
+
litres: () => water.litres,
|
|
180
|
+
level: () => levelOf(water.litres),
|
|
181
|
+
clog: () => water.clog,
|
|
182
|
+
cause: () => water.cause,
|
|
183
|
+
dirty: () => contents.some((c) => c.kind !== "object"),
|
|
184
|
+
save: () => ({ water: { ...water }, serial, contents: contents.map((c) => ({ ...c })) }),
|
|
185
|
+
restore(saved) {
|
|
186
|
+
shown = null;
|
|
187
|
+
Object.assign(water, saved.water);
|
|
188
|
+
serial = saved.serial;
|
|
189
|
+
contents = saved.contents.map((c) => ({ ...c }));
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
1
194
|
// src/gestures.ts
|
|
2
195
|
var GESTURE_OF_ACTION = {
|
|
3
196
|
toggle_part: null,
|
|
@@ -21,6 +214,7 @@ var GESTURE_OF_USE = {
|
|
|
21
214
|
empty: "tip",
|
|
22
215
|
fill: "pour",
|
|
23
216
|
relieve: null,
|
|
217
|
+
plunge: null,
|
|
24
218
|
wash: null,
|
|
25
219
|
drink: null
|
|
26
220
|
};
|
|
@@ -249,7 +443,17 @@ var locales = {
|
|
|
249
443
|
done_relieve_feces: "You pooed in {name}",
|
|
250
444
|
done_clean: "{name} flushed clean",
|
|
251
445
|
"use.wash_dishes": "Wash the dishes",
|
|
252
|
-
"use.drink_water": "Drink water",
|
|
446
|
+
"use.drink_water": "Drink water from the glass",
|
|
447
|
+
"use.drink_from_hands": "Drink water from your hands",
|
|
448
|
+
needs_in_hand: "Pick up {item} first",
|
|
449
|
+
needs_vessel: "You need a glass",
|
|
450
|
+
vessel_not_empty: "Empty {item} first",
|
|
451
|
+
hands_busy: "Your hands are busy",
|
|
452
|
+
no_animation: "Cannot be done here: the body has no movement for it",
|
|
453
|
+
needs_free_hand: "You need your hands free",
|
|
454
|
+
not_drunk: "No water was drunk",
|
|
455
|
+
drink_from_vessel: "You have a glass in your hand: drink from it",
|
|
456
|
+
done_drink_hands: "You drank {litres} l of water from your hands",
|
|
253
457
|
busy: "Already in use",
|
|
254
458
|
needs_running: "Turn the water on first",
|
|
255
459
|
needs_item: "You need {need}",
|
|
@@ -259,7 +463,21 @@ var locales = {
|
|
|
259
463
|
started_drink: "{label}\u2026",
|
|
260
464
|
done_wash: "Washed and rinsed",
|
|
261
465
|
done_wash_dishes: "Dishes washed: {n} on the rack",
|
|
262
|
-
done_drink: "You drank {litres} l of water"
|
|
466
|
+
done_drink: "You drank {litres} l of water",
|
|
467
|
+
"use.plunge": "Unclog {name} with the plunger",
|
|
468
|
+
needs_plunger: "Hold a plunger",
|
|
469
|
+
plunge_no_water: "No water over the trap for the plunger",
|
|
470
|
+
done_plunge: "You work the plunger in {name}",
|
|
471
|
+
plunge_cleared: "{name} unclogged: the water goes down",
|
|
472
|
+
plunge_object: "{name} unclogged: something came back up the trap",
|
|
473
|
+
plunge_still: "{name} is still clogged",
|
|
474
|
+
clogged: "{name} is clogged",
|
|
475
|
+
overflow: "{name} is overflowing",
|
|
476
|
+
needs_body: "There is no body here to use {name}",
|
|
477
|
+
host_lacks: "This place cannot run {name} (it lacks {capability})",
|
|
478
|
+
body_no_seat: "{name} has no seat to sit on",
|
|
479
|
+
body_object_gone: "{name} is not here",
|
|
480
|
+
body_busy: "You are busy with something else"
|
|
263
481
|
},
|
|
264
482
|
"pt-BR": {
|
|
265
483
|
"prompt.open": "Abrir {name}",
|
|
@@ -314,7 +532,17 @@ var locales = {
|
|
|
314
532
|
done_relieve_feces: "Voc\xEA fez coc\xF4 em {name}",
|
|
315
533
|
done_clean: "Descarga dada: {name} limpo",
|
|
316
534
|
"use.wash_dishes": "Lavar a lou\xE7a",
|
|
317
|
-
"use.drink_water": "Beber \xE1gua",
|
|
535
|
+
"use.drink_water": "Beber \xE1gua no copo",
|
|
536
|
+
"use.drink_from_hands": "Beber \xE1gua com a m\xE3o",
|
|
537
|
+
needs_in_hand: "Pegue {item} antes",
|
|
538
|
+
needs_vessel: "Precisa de um copo",
|
|
539
|
+
vessel_not_empty: "Esvazie {item} antes",
|
|
540
|
+
hands_busy: "As m\xE3os est\xE3o ocupadas",
|
|
541
|
+
no_animation: "N\xE3o d\xE1 para fazer isso aqui: o corpo n\xE3o tem o movimento",
|
|
542
|
+
needs_free_hand: "Precisa das m\xE3os livres",
|
|
543
|
+
not_drunk: "Nenhuma \xE1gua foi bebida",
|
|
544
|
+
drink_from_vessel: "Voc\xEA est\xE1 com um copo na m\xE3o: beba nele",
|
|
545
|
+
done_drink_hands: "Voc\xEA bebeu {litres} l de \xE1gua com a m\xE3o",
|
|
318
546
|
busy: "J\xE1 est\xE1 em uso",
|
|
319
547
|
needs_running: "Abra a \xE1gua primeiro",
|
|
320
548
|
needs_item: "Precisa de {need}",
|
|
@@ -324,7 +552,21 @@ var locales = {
|
|
|
324
552
|
started_drink: "{label}\u2026",
|
|
325
553
|
done_wash: "Lavado e enxaguado",
|
|
326
554
|
done_wash_dishes: "Lou\xE7a lavada: {n} no escorredor",
|
|
327
|
-
done_drink: "Voc\xEA bebeu {litres} l de \xE1gua"
|
|
555
|
+
done_drink: "Voc\xEA bebeu {litres} l de \xE1gua",
|
|
556
|
+
"use.plunge": "Desentupir {name} com o desentupidor",
|
|
557
|
+
needs_plunger: "Segure um desentupidor",
|
|
558
|
+
plunge_no_water: "Sem \xE1gua sobre o sif\xE3o para o desentupidor",
|
|
559
|
+
done_plunge: "Voc\xEA bombeia o desentupidor em {name}",
|
|
560
|
+
plunge_cleared: "{name} desentupido: a \xE1gua desce",
|
|
561
|
+
plunge_object: "{name} desentupido: uma coisa voltou do sif\xE3o",
|
|
562
|
+
plunge_still: "{name} ainda est\xE1 entupido",
|
|
563
|
+
clogged: "{name} entupiu",
|
|
564
|
+
overflow: "{name} est\xE1 transbordando",
|
|
565
|
+
needs_body: "N\xE3o h\xE1 corpo aqui para usar {name}",
|
|
566
|
+
host_lacks: "Este lugar n\xE3o sabe usar {name} (falta {capability})",
|
|
567
|
+
body_no_seat: "{name} n\xE3o tem assento para sentar",
|
|
568
|
+
body_object_gone: "{name} n\xE3o est\xE1 aqui",
|
|
569
|
+
body_busy: "Voc\xEA est\xE1 ocupado com outra coisa"
|
|
328
570
|
}
|
|
329
571
|
};
|
|
330
572
|
function pickLocale(locale) {
|
|
@@ -1731,6 +1973,7 @@ function createWorld(options = {}) {
|
|
|
1731
1973
|
burners: /* @__PURE__ */ new Map(),
|
|
1732
1974
|
tanks: /* @__PURE__ */ new Map(),
|
|
1733
1975
|
fixtures: /* @__PURE__ */ new Map(),
|
|
1976
|
+
drains: /* @__PURE__ */ new Map(),
|
|
1734
1977
|
soft: /* @__PURE__ */ new Map(),
|
|
1735
1978
|
hp: /* @__PURE__ */ new Map(),
|
|
1736
1979
|
interactions: spec.interactions ?? deriveInteractions(spec)
|
|
@@ -1763,6 +2006,7 @@ function createWorld(options = {}) {
|
|
|
1763
2006
|
for (const b of spec.burners ?? []) o.burners.set(b.id, { spec: b, on: false, lit: false, smoke: 0 });
|
|
1764
2007
|
for (const tk of spec.tanks ?? []) o.tanks.set(tk.id, { spec: tk, level: clamp(tk.level ?? tk.capacity, 0, tk.capacity) });
|
|
1765
2008
|
for (const f of spec.fixtures ?? []) o.fixtures.set(f.id, { spec: f, on: false, flowing: false, frac: 0, timer: 0 });
|
|
2009
|
+
for (const b of spec.basins ?? []) if (b.drain) o.drains.set(b.id, createBasinDrain(b, b.drain));
|
|
1766
2010
|
let n = 0;
|
|
1767
2011
|
for (const s of spec.soft ?? []) {
|
|
1768
2012
|
const driver = options.partPhysics?.attachSoft?.(id, s, pub.transform);
|
|
@@ -1795,7 +2039,8 @@ function createWorld(options = {}) {
|
|
|
1795
2039
|
burners: Object.fromEntries([...o.burners].map(([k, b]) => [k, { on: b.on, lit: b.lit }])),
|
|
1796
2040
|
tanks: Object.fromEntries([...o.tanks].map(([k, tk]) => [k, tk.level])),
|
|
1797
2041
|
fixtures: Object.fromEntries([...o.fixtures].map(([k, f]) => [k, f.on])),
|
|
1798
|
-
hp: Object.fromEntries([...o.hp].map(([k, h]) => [k, h.hp]))
|
|
2042
|
+
hp: Object.fromEntries([...o.hp].map(([k, h]) => [k, h.hp])),
|
|
2043
|
+
drains: Object.fromEntries([...o.drains].map(([k, d]) => [k, d.save()]))
|
|
1799
2044
|
};
|
|
1800
2045
|
}
|
|
1801
2046
|
return {
|
|
@@ -1861,6 +2106,7 @@ function createWorld(options = {}) {
|
|
|
1861
2106
|
const h = o.hp.get(k);
|
|
1862
2107
|
if (h) h.hp = v;
|
|
1863
2108
|
}
|
|
2109
|
+
for (const [k, v] of Object.entries(st.drains ?? {})) o.drains.get(k)?.restore(v);
|
|
1864
2110
|
}
|
|
1865
2111
|
grid = state.grid;
|
|
1866
2112
|
main = state.main ?? true;
|
|
@@ -1985,6 +2231,7 @@ function createWorld(options = {}) {
|
|
|
1985
2231
|
locale = l;
|
|
1986
2232
|
table = resolveStrings(locale, options.strings);
|
|
1987
2233
|
},
|
|
2234
|
+
drain: (objectId, basin) => objs.get(objectId)?.drains.get(basin),
|
|
1988
2235
|
serialize,
|
|
1989
2236
|
load
|
|
1990
2237
|
};
|
|
@@ -2509,6 +2756,178 @@ function castWithMember(member, other) {
|
|
|
2509
2756
|
return wrapsMember(member, other) || endsMember(member, other);
|
|
2510
2757
|
}
|
|
2511
2758
|
|
|
2759
|
+
// src/drain-uses.ts
|
|
2760
|
+
import { PLUNGE_STROKE_SECONDS } from "@adatechnology/game-contracts";
|
|
2761
|
+
var RELIEVE_SECONDS = 8;
|
|
2762
|
+
var LANDS_AT = 0.7;
|
|
2763
|
+
var FLUSH_LITRES = 6;
|
|
2764
|
+
var SWALLOW_SECONDS = 0.5;
|
|
2765
|
+
var SPILL_GATHER_SECONDS = 0.25;
|
|
2766
|
+
var URINE = { kind: "urine", kg: 0.3, litres: 0.3, size: [0.02, 0.02, 0.02] };
|
|
2767
|
+
var FECES = { kind: "feces", kg: 0.2, size: [0.12, 0.035, 0.035] };
|
|
2768
|
+
var PAPER = { kind: "paper", kg: 8e-3, size: [0.06, 0.03, 0.03] };
|
|
2769
|
+
var PEE = [URINE];
|
|
2770
|
+
var POO = [FECES, PAPER, PAPER];
|
|
2771
|
+
var HOST_NEEDS = ["setBasin", "spill", "looseInside", "swallow"];
|
|
2772
|
+
function createDrainUses(options) {
|
|
2773
|
+
const { spec, t, world, objectId } = options;
|
|
2774
|
+
const basins = /* @__PURE__ */ new Map();
|
|
2775
|
+
for (const b of spec.basins ?? []) {
|
|
2776
|
+
const drain = world.drain(objectId, b.id);
|
|
2777
|
+
if (!drain) continue;
|
|
2778
|
+
const centre = [(b.from[0] + b.to[0]) / 2, b.from[1], (b.from[2] + b.to[2]) / 2];
|
|
2779
|
+
basins.set(b.id, { spec: b, drain, centre, frontRim: [centre[0], b.to[1], b.to[2]], clogged: false, overflowing: false, spill: 0, spillTime: 0 });
|
|
2780
|
+
}
|
|
2781
|
+
const flushes = new Map(
|
|
2782
|
+
(spec.fixtures ?? []).flatMap((f) => f.cleans && basins.has(f.cleans) ? [[f.id, { basin: f.cleans, litres: f.volume ?? FLUSH_LITRES }]] : [])
|
|
2783
|
+
);
|
|
2784
|
+
const list = [...basins.values()];
|
|
2785
|
+
let states = {};
|
|
2786
|
+
const pending = [];
|
|
2787
|
+
const swallowing = [];
|
|
2788
|
+
let plunging = null;
|
|
2789
|
+
const typeKey = `type.${spec.type}`;
|
|
2790
|
+
const name = () => {
|
|
2791
|
+
const text = t(typeKey);
|
|
2792
|
+
return text === typeKey ? spec.type.replace(/_/g, " ") : text.charAt(0).toLowerCase() + text.slice(1);
|
|
2793
|
+
};
|
|
2794
|
+
const takeLoose = (b, host) => {
|
|
2795
|
+
const top = [b.spec.to[0], b.spec.to[1] + 0.08, b.spec.to[2]];
|
|
2796
|
+
const inWater = new Set(b.drain.state().contents.flatMap((c) => c.objectId ? [c.objectId] : []));
|
|
2797
|
+
for (const thing of host?.looseInside?.(b.spec.from, top) ?? []) {
|
|
2798
|
+
if (inWater.has(thing.id)) continue;
|
|
2799
|
+
b.drain.add({ kind: "object", kg: thing.kg, size: thing.size, objectId: thing.id });
|
|
2800
|
+
}
|
|
2801
|
+
};
|
|
2802
|
+
const report = (b, host) => {
|
|
2803
|
+
const clogged = b.drain.clog() >= CLOGGED;
|
|
2804
|
+
if (clogged && !b.clogged) host?.say(t("clogged", { name: name() }));
|
|
2805
|
+
b.clogged = clogged;
|
|
2806
|
+
};
|
|
2807
|
+
const stepPending = (dt) => {
|
|
2808
|
+
let kept = 0;
|
|
2809
|
+
for (const p of pending) {
|
|
2810
|
+
p.left -= dt;
|
|
2811
|
+
if (p.left > 0) {
|
|
2812
|
+
pending[kept++] = p;
|
|
2813
|
+
continue;
|
|
2814
|
+
}
|
|
2815
|
+
for (const content of p.lands) basins.get(p.basin)?.drain.add(content);
|
|
2816
|
+
}
|
|
2817
|
+
pending.length = kept;
|
|
2818
|
+
};
|
|
2819
|
+
const stepPlunge = (dt, host) => {
|
|
2820
|
+
if (!plunging) return;
|
|
2821
|
+
const b = basins.get(plunging.basin);
|
|
2822
|
+
plunging.left -= dt;
|
|
2823
|
+
plunging.stroke += dt;
|
|
2824
|
+
if (b && plunging.stroke >= PLUNGE_STROKE_SECONDS) {
|
|
2825
|
+
plunging.stroke -= PLUNGE_STROKE_SECONDS;
|
|
2826
|
+
const cause = b.drain.cause();
|
|
2827
|
+
b.drain.plunge();
|
|
2828
|
+
if (cause !== "none" && b.drain.cause() === "none") {
|
|
2829
|
+
host?.say(t(cause === "object" ? "plunge_object" : "plunge_cleared", { name: name() }));
|
|
2830
|
+
b.clogged = false;
|
|
2831
|
+
}
|
|
2832
|
+
}
|
|
2833
|
+
if (plunging.left > 0) return;
|
|
2834
|
+
if (b && b.drain.clog() >= CLOGGED) host?.say(t("plunge_still", { name: name() }));
|
|
2835
|
+
plunging = null;
|
|
2836
|
+
};
|
|
2837
|
+
const stepSwallowing = (dt, host) => {
|
|
2838
|
+
let kept = 0;
|
|
2839
|
+
for (const s of swallowing) {
|
|
2840
|
+
s.time += dt;
|
|
2841
|
+
const progress = Math.min(1, s.time / SWALLOW_SECONDS);
|
|
2842
|
+
host?.swallow?.(s.id, s.point, progress);
|
|
2843
|
+
if (progress < 1) swallowing[kept++] = s;
|
|
2844
|
+
}
|
|
2845
|
+
swallowing.length = kept;
|
|
2846
|
+
};
|
|
2847
|
+
const stepSpill = (b, spilled, dt, host) => {
|
|
2848
|
+
if (spilled > 0 && !b.overflowing) host?.say(t("overflow", { name: name() }));
|
|
2849
|
+
b.overflowing = spilled > 0;
|
|
2850
|
+
b.spill += spilled;
|
|
2851
|
+
if (b.spill <= 0) return;
|
|
2852
|
+
b.spillTime += dt;
|
|
2853
|
+
if (b.spillTime < SPILL_GATHER_SECONDS) return;
|
|
2854
|
+
host?.spill?.(b.frontRim, b.spill, b.drain.dirty() ? "sewage" : "water");
|
|
2855
|
+
b.spill = 0;
|
|
2856
|
+
b.spillTime = 0;
|
|
2857
|
+
};
|
|
2858
|
+
const stepBasin = (b, dt, host) => {
|
|
2859
|
+
const step = b.drain.update(dt);
|
|
2860
|
+
for (const id of step.swallowed) swallowing.push({ id, point: b.centre, time: 0 });
|
|
2861
|
+
stepSpill(b, step.spilled, dt, host);
|
|
2862
|
+
host?.setBasin?.(b.spec.id, b.drain.litres());
|
|
2863
|
+
report(b, host);
|
|
2864
|
+
};
|
|
2865
|
+
return {
|
|
2866
|
+
has: (basin) => basins.has(basin),
|
|
2867
|
+
hostBlocked: (host) => HOST_NEEDS.find((need) => host[need] === void 0) ?? null,
|
|
2868
|
+
relieveBody(use, host) {
|
|
2869
|
+
const body = host.body?.();
|
|
2870
|
+
if (!body) return null;
|
|
2871
|
+
const seconds = use.seconds ?? RELIEVE_SECONDS;
|
|
2872
|
+
const b = basins.get(use.basin);
|
|
2873
|
+
if (use.kind === "urine" && use.stand && body.sex === "male") return { pose: "relieve_stand", seconds, at: use.stand, faces: b ? b.centre : [0, 0, 0] };
|
|
2874
|
+
return { pose: "relieve_sit", seconds, ...use.seat ? { seat: use.seat } : {} };
|
|
2875
|
+
},
|
|
2876
|
+
relieve(use, host, body) {
|
|
2877
|
+
if (use.cover) host.setPart(use.cover, true);
|
|
2878
|
+
if (use.ring) host.setPart(use.ring, body.pose === "relieve_stand");
|
|
2879
|
+
pending.push({ basin: use.basin, lands: use.kind === "urine" ? PEE : POO, left: body.seconds * LANDS_AT });
|
|
2880
|
+
return { ok: true, text: t(`done_relieve_${use.kind}`, { name: name() }), body };
|
|
2881
|
+
},
|
|
2882
|
+
plungeBlocked(use, host) {
|
|
2883
|
+
if (!host.hand()?.tools?.includes(use.tool)) return "needs_plunger";
|
|
2884
|
+
const b = basins.get(use.basin);
|
|
2885
|
+
return b && b.drain.level() < PLUNGER_WATER ? "plunge_no_water" : null;
|
|
2886
|
+
},
|
|
2887
|
+
plungeBody(use) {
|
|
2888
|
+
const b = basins.get(use.basin);
|
|
2889
|
+
return { pose: "plunge", seconds: use.seconds, at: use.stand, faces: b ? b.centre : [0, 0, 0] };
|
|
2890
|
+
},
|
|
2891
|
+
plunge(use, body) {
|
|
2892
|
+
plunging = { basin: use.basin, left: use.seconds, stroke: 0 };
|
|
2893
|
+
return { ok: true, text: t("done_plunge", { name: name() }), body };
|
|
2894
|
+
},
|
|
2895
|
+
flushed(fixture, host) {
|
|
2896
|
+
const flush = flushes.get(fixture);
|
|
2897
|
+
const b = flush ? basins.get(flush.basin) : void 0;
|
|
2898
|
+
if (!flush || !b) return false;
|
|
2899
|
+
takeLoose(b, host);
|
|
2900
|
+
b.drain.flush(flush.litres);
|
|
2901
|
+
return true;
|
|
2902
|
+
},
|
|
2903
|
+
update(dt, host) {
|
|
2904
|
+
stepPending(dt);
|
|
2905
|
+
stepPlunge(dt, host);
|
|
2906
|
+
stepSwallowing(dt, host);
|
|
2907
|
+
for (const b of list) stepBasin(b, dt, host);
|
|
2908
|
+
},
|
|
2909
|
+
dispose(host) {
|
|
2910
|
+
for (const s of swallowing) host?.swallow?.(s.id, s.point, 1);
|
|
2911
|
+
swallowing.length = 0;
|
|
2912
|
+
pending.length = 0;
|
|
2913
|
+
plunging = null;
|
|
2914
|
+
},
|
|
2915
|
+
soil(basin) {
|
|
2916
|
+
const contents = basins.get(basin)?.drain.state().contents ?? [];
|
|
2917
|
+
if (contents.some((c) => c.kind === "feces" || c.kind === "paper")) return "feces";
|
|
2918
|
+
return contents.some((c) => c.kind === "urine") ? "urine" : "clean";
|
|
2919
|
+
},
|
|
2920
|
+
state() {
|
|
2921
|
+
for (const b of list) {
|
|
2922
|
+
if (states[b.spec.id] === b.drain.state()) continue;
|
|
2923
|
+
states = Object.fromEntries(list.map((each) => [each.spec.id, each.drain.state()]));
|
|
2924
|
+
break;
|
|
2925
|
+
}
|
|
2926
|
+
return states;
|
|
2927
|
+
}
|
|
2928
|
+
};
|
|
2929
|
+
}
|
|
2930
|
+
|
|
2512
2931
|
// src/fits-volume.ts
|
|
2513
2932
|
function fitsInVolume(size, half) {
|
|
2514
2933
|
const thing = [...size].sort((a, b) => b - a);
|
|
@@ -2521,8 +2940,11 @@ var WASH_SCRUB_SECONDS = 3;
|
|
|
2521
2940
|
var DRINK_LITRES = 0.25;
|
|
2522
2941
|
var FILL_SHARE = 0.85;
|
|
2523
2942
|
var DRINK_LPS = 0.06;
|
|
2943
|
+
var DRINK_HANDS_LITRES = 0.12;
|
|
2944
|
+
var DRINK_HANDS_LPS = 0.03;
|
|
2524
2945
|
var REACH_SECONDS = 0.6;
|
|
2525
2946
|
var DRY_SECONDS = 0.5;
|
|
2947
|
+
var UNDER_STREAM = 0.14;
|
|
2526
2948
|
var SCRUBBED_DIRT = 0.15;
|
|
2527
2949
|
var YES = { available: true };
|
|
2528
2950
|
var idsOf = (need) => typeof need === "string" ? [need] : need;
|
|
@@ -2541,16 +2963,59 @@ function dirtyDishes(use, spec, host) {
|
|
|
2541
2963
|
if (!dishes || !basin) return [];
|
|
2542
2964
|
return (host.itemsInside?.(dishes.ids, basin.from, basin.to) ?? []).filter((item) => (item.dirt ?? 0) > 0);
|
|
2543
2965
|
}
|
|
2966
|
+
function washBody(use, host) {
|
|
2967
|
+
const { playUse, lather, rinse, itemsInside, placeItem, setDirt, takeDose } = host;
|
|
2968
|
+
if (!playUse || !lather || !rinse) return null;
|
|
2969
|
+
if (use.dishes && (!itemsInside || !placeItem || !setDirt)) return null;
|
|
2970
|
+
if (use.dose && !takeDose) return null;
|
|
2971
|
+
return { playUse, lather, rinse, itemsInside, placeItem, setDirt, takeDose };
|
|
2972
|
+
}
|
|
2973
|
+
function vesselBody(host) {
|
|
2974
|
+
const { playUse, vesselAt, fillItem, drinkItem } = host;
|
|
2975
|
+
return playUse && vesselAt && fillItem && drinkItem ? { playUse, vesselAt, fillItem, drinkItem } : null;
|
|
2976
|
+
}
|
|
2977
|
+
function handsBody(host) {
|
|
2978
|
+
const { playUse, handsFree, drinkFromHands } = host;
|
|
2979
|
+
return playUse && handsFree && drinkFromHands ? { playUse, handsFree, drinkFromHands } : null;
|
|
2980
|
+
}
|
|
2981
|
+
var NO_BODY = { available: false, reason: "no_animation" };
|
|
2982
|
+
var NEEDS_FREE_HAND = { available: false, reason: "needs_free_hand" };
|
|
2544
2983
|
function sinkVerdict(use, spec, host, flowing) {
|
|
2545
|
-
if (use.do === "
|
|
2984
|
+
if (use.do === "drink") return drinkVerdict(use, host);
|
|
2985
|
+
if (!washBody(use, host)) return NO_BODY;
|
|
2986
|
+
if (use.requiresFlowing && !flowing) return { available: false, reason: "needs_running" };
|
|
2546
2987
|
const found = findNeeds(use.needs ?? [], host);
|
|
2988
|
+
if (!handsFreeFor(use, found.items, host)) return NEEDS_FREE_HAND;
|
|
2547
2989
|
if (found.missing) return { available: false, reason: "needs_item", vars: { need: found.missing[0] ?? "" } };
|
|
2548
|
-
if (use.do === "drink") return YES;
|
|
2549
2990
|
const dosed = use.dose ? found.items[use.dose.need] : void 0;
|
|
2550
2991
|
if (dosed && (dosed.remaining ?? 1) <= 0) return { available: false, reason: "item_spent", vars: { item: dosed.name } };
|
|
2551
2992
|
if (use.dishes && dirtyDishes(use, spec, host).length === 0) return { available: false, reason: "no_dirty_dishes" };
|
|
2552
2993
|
return YES;
|
|
2553
2994
|
}
|
|
2995
|
+
function handsAreFree(host) {
|
|
2996
|
+
return host.handsFree?.() ?? host.hand() === null;
|
|
2997
|
+
}
|
|
2998
|
+
function handsFreeFor(use, needed, host) {
|
|
2999
|
+
if (handsAreFree(host)) return true;
|
|
3000
|
+
if (host.hand() === null) return false;
|
|
3001
|
+
if (needed.some((item) => item.where === "hand")) return true;
|
|
3002
|
+
return use.dishes !== void 0 && host.itemAt?.(use.dishes.ids)?.where === "hand";
|
|
3003
|
+
}
|
|
3004
|
+
function drinkVerdict(use, host) {
|
|
3005
|
+
if (use.from === "hands") {
|
|
3006
|
+
const body2 = handsBody(host);
|
|
3007
|
+
if (!body2) return NO_BODY;
|
|
3008
|
+
if (body2.handsFree()) return YES;
|
|
3009
|
+
return host.vesselAt?.()?.where === "hand" ? { available: false, reason: "drink_from_vessel" } : NEEDS_FREE_HAND;
|
|
3010
|
+
}
|
|
3011
|
+
const body = vesselBody(host);
|
|
3012
|
+
if (!body) return NO_BODY;
|
|
3013
|
+
const vessel = body.vesselAt();
|
|
3014
|
+
if (!vessel) return { available: false, reason: "needs_vessel" };
|
|
3015
|
+
if (vessel.where !== "hand") return { available: false, reason: "needs_in_hand", vars: { item: vessel.name } };
|
|
3016
|
+
const other = vessel.liquid?.kind !== void 0 && vessel.liquid.kind !== "water" && vessel.liquid.litres > 1e-3;
|
|
3017
|
+
return other ? { available: false, reason: "vessel_not_empty", vars: { item: vessel.name } } : YES;
|
|
3018
|
+
}
|
|
2554
3019
|
var fixtureFlowLps = (fixture) => fixture ? (fixture.flow ?? FLOW_DEFAULTS[fixture.kind] ?? 6) / 60 : 0;
|
|
2555
3020
|
function runPhases(phases) {
|
|
2556
3021
|
let index = 0;
|
|
@@ -2574,6 +3039,10 @@ function createSinkRoutines(options) {
|
|
|
2574
3039
|
const fixtureOf = (id) => (spec.fixtures ?? []).find((f) => f.id === id);
|
|
2575
3040
|
const flowing = (use) => world.water.isFlowing(objectId, use.fixture);
|
|
2576
3041
|
const water = (use) => flowing(use) ? fixtureFlowLps(fixtureOf(use.fixture)) : 0;
|
|
3042
|
+
const underStream = (use) => {
|
|
3043
|
+
const outlet = fixtureOf(use.fixture)?.outlet;
|
|
3044
|
+
return outlet ? [outlet[0], outlet[1] - UNDER_STREAM, outlet[2]] : void 0;
|
|
3045
|
+
};
|
|
2577
3046
|
const waterOf = (use) => {
|
|
2578
3047
|
let dry = 0;
|
|
2579
3048
|
return (dt) => {
|
|
@@ -2582,6 +3051,14 @@ function createSinkRoutines(options) {
|
|
|
2582
3051
|
return dry > DRY_SECONDS ? null : flow;
|
|
2583
3052
|
};
|
|
2584
3053
|
};
|
|
3054
|
+
const approachPhase = (use, host) => {
|
|
3055
|
+
let walked = 0;
|
|
3056
|
+
const at = underStream(use);
|
|
3057
|
+
return (dt) => {
|
|
3058
|
+
walked += dt;
|
|
3059
|
+
return !at || !host.approach || walked > REACH_SECONDS * 3 || host.approach(at, dt);
|
|
3060
|
+
};
|
|
3061
|
+
};
|
|
2585
3062
|
const tapPhases = (use) => {
|
|
2586
3063
|
let opened = false;
|
|
2587
3064
|
const reach = wait(REACH_SECONDS);
|
|
@@ -2598,7 +3075,7 @@ function createSinkRoutines(options) {
|
|
|
2598
3075
|
}
|
|
2599
3076
|
};
|
|
2600
3077
|
};
|
|
2601
|
-
const scrubAndRinse = (use,
|
|
3078
|
+
const scrubAndRinse = (use, body, target, fail) => {
|
|
2602
3079
|
const item = typeof target === "string" ? void 0 : target;
|
|
2603
3080
|
const id = item?.objectId ?? target;
|
|
2604
3081
|
const seconds = use.scrubSeconds ?? WASH_SCRUB_SECONDS;
|
|
@@ -2607,11 +3084,14 @@ function createSinkRoutines(options) {
|
|
|
2607
3084
|
const scrub = (dt) => {
|
|
2608
3085
|
if (scrubbed < 0) {
|
|
2609
3086
|
scrubbed = 0;
|
|
2610
|
-
|
|
3087
|
+
if (!body.playUse(use.clip, item?.objectId, underStream(use))) {
|
|
3088
|
+
fail({ key: "hands_busy", ok: false });
|
|
3089
|
+
return true;
|
|
3090
|
+
}
|
|
2611
3091
|
}
|
|
2612
3092
|
scrubbed = Math.min(seconds, scrubbed + dt);
|
|
2613
|
-
|
|
2614
|
-
if (dirt > 0)
|
|
3093
|
+
body.lather(id, dt / seconds);
|
|
3094
|
+
if (dirt > 0) body.setDirt?.(id, dirt - (dirt - Math.min(dirt, SCRUBBED_DIRT)) * (scrubbed / seconds));
|
|
2615
3095
|
return scrubbed >= seconds;
|
|
2616
3096
|
};
|
|
2617
3097
|
const running = waterOf(use);
|
|
@@ -2621,13 +3101,13 @@ function createSinkRoutines(options) {
|
|
|
2621
3101
|
fail({ key: "no_water", ok: false });
|
|
2622
3102
|
return true;
|
|
2623
3103
|
}
|
|
2624
|
-
if (flow <= 0 ||
|
|
2625
|
-
if (dirt > 0)
|
|
3104
|
+
if (flow <= 0 || body.rinse(id, flow, dt) > 0) return false;
|
|
3105
|
+
if (dirt > 0) body.setDirt?.(id, 0);
|
|
2626
3106
|
return true;
|
|
2627
3107
|
};
|
|
2628
3108
|
return [scrub, rinse];
|
|
2629
3109
|
};
|
|
2630
|
-
const startWash = (use, host) => {
|
|
3110
|
+
const startWash = (use, host, body) => {
|
|
2631
3111
|
const { items } = findNeeds(use.needs ?? [], host);
|
|
2632
3112
|
let end = null;
|
|
2633
3113
|
const fail = (reason) => {
|
|
@@ -2636,59 +3116,136 @@ function createSinkRoutines(options) {
|
|
|
2636
3116
|
const tap = tapPhases(use);
|
|
2637
3117
|
const dosed = use.dose ? items[use.dose.need] : void 0;
|
|
2638
3118
|
const dose = () => {
|
|
2639
|
-
if (dosed &&
|
|
3119
|
+
if (dosed && body.takeDose?.(dosed.objectId, use.dose?.amount).taken === 0) fail({ key: "item_spent", ok: false, vars: { item: dosed.name } });
|
|
2640
3120
|
return true;
|
|
2641
3121
|
};
|
|
2642
3122
|
const dishes = use.dishes ? dirtyDishes(use, spec, host) : [];
|
|
2643
3123
|
const rack = use.dishes?.rack ?? [];
|
|
2644
|
-
const work = use.dishes ? dishes.flatMap((dish, index) => [...scrubAndRinse(use,
|
|
3124
|
+
const work = use.dishes ? dishes.flatMap((dish, index) => [...scrubAndRinse(use, body, dish, fail), placePhase(host, dish, rack[Math.min(index, rack.length - 1)])]) : scrubAndRinse(use, body, use.surface, fail);
|
|
2645
3125
|
const guarded = work.map((phase) => (dt) => end !== null || phase(dt));
|
|
2646
|
-
const run = runPhases([tap.open, dose, ...guarded, tap.close]);
|
|
3126
|
+
const run = runPhases([approachPhase(use, host), tap.open, dose, ...guarded, tap.close]);
|
|
2647
3127
|
const done = use.dishes ? { key: "done_wash_dishes", ok: true, vars: { n: String(dishes.length) } } : { key: "done_wash", ok: true, vars: { surface: use.surface } };
|
|
2648
3128
|
return { use: use.id, step: (dt) => run(dt) ? end ?? done : null };
|
|
2649
3129
|
};
|
|
2650
|
-
const startDrink = (use, host, vessel) => {
|
|
3130
|
+
const startDrink = (use, host, body, vessel) => {
|
|
2651
3131
|
const tap = tapPhases(use);
|
|
2652
3132
|
const want = Math.min(use.litres ?? DRINK_LITRES, (vessel.liquid?.capacity ?? DRINK_LITRES) * FILL_SHARE);
|
|
2653
3133
|
let filled = vessel.liquid?.kind === "water" ? vessel.liquid.litres : 0;
|
|
2654
3134
|
let drunk = 0;
|
|
2655
3135
|
let end = null;
|
|
2656
|
-
const take = () => {
|
|
2657
|
-
if (vessel.where !== "hand" && host.holdItem?.(vessel.objectId) === false) end = { key: "needs_item", ok: false, vars: { need: vessel.itemId } };
|
|
2658
|
-
else host.playUse?.(use.fillClip ?? "fill_cup", vessel.objectId);
|
|
2659
|
-
return true;
|
|
2660
|
-
};
|
|
2661
3136
|
const running = waterOf(use);
|
|
3137
|
+
const played = (clip, at) => {
|
|
3138
|
+
if (body.playUse(clip, vessel.objectId, at)) return true;
|
|
3139
|
+
end ?? (end = { key: "hands_busy", ok: false });
|
|
3140
|
+
return false;
|
|
3141
|
+
};
|
|
3142
|
+
let filling = false;
|
|
2662
3143
|
const fill = (dt) => {
|
|
2663
3144
|
if (end) return true;
|
|
3145
|
+
if (!filling && !(filling = played(use.fillClip ?? "fill_cup", underStream(use)))) return true;
|
|
2664
3146
|
const flow = running(dt);
|
|
2665
3147
|
if (flow === null) {
|
|
2666
3148
|
end = { key: "no_water", ok: false };
|
|
2667
3149
|
return true;
|
|
2668
3150
|
}
|
|
2669
3151
|
if (flow <= 0) return false;
|
|
2670
|
-
const took =
|
|
3152
|
+
const took = body.fillItem(vessel.objectId, "water", Math.min(flow * dt, want - filled));
|
|
2671
3153
|
filled += took;
|
|
2672
3154
|
return filled >= want - 1e-6 || took <= 0;
|
|
2673
3155
|
};
|
|
2674
3156
|
let sipping = false;
|
|
2675
3157
|
const drink = (dt) => {
|
|
2676
3158
|
if (end) return true;
|
|
2677
|
-
if (!sipping
|
|
2678
|
-
const sip =
|
|
3159
|
+
if (!sipping && !(sipping = played(use.clip))) return true;
|
|
3160
|
+
const sip = body.drinkItem(vessel.objectId, Math.min(DRINK_LPS * dt, filled - drunk));
|
|
2679
3161
|
drunk += sip;
|
|
2680
3162
|
return drunk >= filled - 1e-6 || sip <= 0;
|
|
2681
3163
|
};
|
|
2682
|
-
const
|
|
2683
|
-
const
|
|
2684
|
-
return { use: use.id, step: (dt) => run(dt) ? end ??
|
|
3164
|
+
const run = runPhases([approachPhase(use, host), tap.open, fill, tap.close, drink]);
|
|
3165
|
+
const done = () => drunk > 1e-3 ? { key: "done_drink", ok: true, vars: { litres: drunk.toFixed(2) } } : { key: "not_drunk", ok: false };
|
|
3166
|
+
return { use: use.id, step: (dt) => run(dt) ? end ?? done() : null };
|
|
3167
|
+
};
|
|
3168
|
+
const startDrinkHands = (use, host, body) => {
|
|
3169
|
+
const tap = tapPhases(use);
|
|
3170
|
+
const want = use.litres ?? DRINK_HANDS_LITRES;
|
|
3171
|
+
let drunk = 0;
|
|
3172
|
+
let end = null;
|
|
3173
|
+
const running = waterOf(use);
|
|
3174
|
+
let bent = false;
|
|
3175
|
+
const drink = (dt) => {
|
|
3176
|
+
if (!bent) {
|
|
3177
|
+
bent = body.playUse(use.clip, void 0, underStream(use));
|
|
3178
|
+
if (!bent) {
|
|
3179
|
+
end = { key: "hands_busy", ok: false };
|
|
3180
|
+
return true;
|
|
3181
|
+
}
|
|
3182
|
+
}
|
|
3183
|
+
const flow = running(dt);
|
|
3184
|
+
if (flow === null) {
|
|
3185
|
+
end = { key: "no_water", ok: false };
|
|
3186
|
+
return true;
|
|
3187
|
+
}
|
|
3188
|
+
if (flow <= 0) return false;
|
|
3189
|
+
const sip = body.drinkFromHands("water", Math.min(DRINK_HANDS_LPS * dt, flow * dt, want - drunk));
|
|
3190
|
+
drunk += sip;
|
|
3191
|
+
return drunk >= want - 1e-6 || sip <= 0;
|
|
3192
|
+
};
|
|
3193
|
+
const run = runPhases([approachPhase(use, host), tap.open, drink, tap.close]);
|
|
3194
|
+
const done = () => drunk > 1e-3 ? { key: "done_drink_hands", ok: true, vars: { litres: drunk.toFixed(2) } } : { key: "not_drunk", ok: false };
|
|
3195
|
+
return { use: use.id, step: (dt) => run(dt) ? end ?? done() : null };
|
|
2685
3196
|
};
|
|
3197
|
+
const refused = (use, key) => ({ use: use.id, step: () => ({ key, ok: false }) });
|
|
2686
3198
|
return {
|
|
2687
3199
|
start(use, host) {
|
|
2688
|
-
if (use.do === "wash")
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
3200
|
+
if (use.do === "wash") {
|
|
3201
|
+
const body2 = washBody(use, host);
|
|
3202
|
+
return body2 ? startWash(use, host, body2) : refused(use, "no_animation");
|
|
3203
|
+
}
|
|
3204
|
+
if (use.from === "hands") {
|
|
3205
|
+
const body2 = handsBody(host);
|
|
3206
|
+
return body2 ? startDrinkHands(use, host, body2) : refused(use, "no_animation");
|
|
3207
|
+
}
|
|
3208
|
+
const body = vesselBody(host);
|
|
3209
|
+
const vessel = body?.vesselAt();
|
|
3210
|
+
if (!body) return refused(use, "no_animation");
|
|
3211
|
+
return vessel ? startDrink(use, host, body, vessel) : refused(use, "needs_vessel");
|
|
3212
|
+
}
|
|
3213
|
+
};
|
|
3214
|
+
}
|
|
3215
|
+
|
|
3216
|
+
// src/use-body.ts
|
|
3217
|
+
function createUseBody() {
|
|
3218
|
+
let running = null;
|
|
3219
|
+
const end = () => {
|
|
3220
|
+
const was = running;
|
|
3221
|
+
running = null;
|
|
3222
|
+
was?.body.end();
|
|
3223
|
+
};
|
|
3224
|
+
return {
|
|
3225
|
+
blocked(action, host) {
|
|
3226
|
+
const body = host.body?.();
|
|
3227
|
+
if (!body) return "needs_body";
|
|
3228
|
+
const why = body.blocked(action);
|
|
3229
|
+
return why ? `body_${why}` : null;
|
|
3230
|
+
},
|
|
3231
|
+
begin(action, host) {
|
|
3232
|
+
const body = host.body?.();
|
|
3233
|
+
if (!body) return false;
|
|
3234
|
+
end();
|
|
3235
|
+
if (!body.begin(action)) return false;
|
|
3236
|
+
running = { action, body, time: 0 };
|
|
3237
|
+
return true;
|
|
3238
|
+
},
|
|
3239
|
+
update(dt) {
|
|
3240
|
+
if (!running) return;
|
|
3241
|
+
running.time += dt;
|
|
3242
|
+
const { action, body, time } = running;
|
|
3243
|
+
if (!body.step(time, Math.min(1, time / action.seconds))) running = null;
|
|
3244
|
+
else if (time >= action.seconds) end();
|
|
3245
|
+
},
|
|
3246
|
+
end,
|
|
3247
|
+
get pose() {
|
|
3248
|
+
return running ? { pose: running.action.pose, time: running.time, progress: Math.min(1, running.time / running.action.seconds) } : null;
|
|
2692
3249
|
}
|
|
2693
3250
|
};
|
|
2694
3251
|
}
|
|
@@ -2711,7 +3268,7 @@ var clock = (seconds) => {
|
|
|
2711
3268
|
};
|
|
2712
3269
|
var accepts = (bowl, hand) => (bowl.accepts ?? []).some((id) => hand.itemId === id || hand.itemId.endsWith(`_${id}`));
|
|
2713
3270
|
var YES2 = { available: true };
|
|
2714
|
-
var no = (reason) => ({ available: false, reason });
|
|
3271
|
+
var no = (reason, vars) => ({ available: false, reason, ...vars ? { vars } : {} });
|
|
2715
3272
|
function createUses(options) {
|
|
2716
3273
|
const { spec, world, objectId } = options;
|
|
2717
3274
|
const all = spec.uses ?? [];
|
|
@@ -2724,6 +3281,8 @@ function createUses(options) {
|
|
|
2724
3281
|
let lastHost = null;
|
|
2725
3282
|
const sinkRoutines = createSinkRoutines({ spec, world, objectId });
|
|
2726
3283
|
let routine = null;
|
|
3284
|
+
const drains = createDrainUses({ spec, world, objectId, t: (key, vars) => world.t(key, vars) });
|
|
3285
|
+
const useBody = createUseBody();
|
|
2727
3286
|
const typeKey = `type.${spec.type}`;
|
|
2728
3287
|
const name = () => {
|
|
2729
3288
|
const text = world.t(typeKey);
|
|
@@ -2754,12 +3313,28 @@ function createUses(options) {
|
|
|
2754
3313
|
return fits ? YES2 : no("not_accepted");
|
|
2755
3314
|
}
|
|
2756
3315
|
case "relieve":
|
|
2757
|
-
return YES2;
|
|
3316
|
+
return drains.has(use.basin) ? drainVerdict(host, () => handsAreFree(host) ? null : "needs_free_hand", () => drains.relieveBody(use, host)) : YES2;
|
|
2758
3317
|
case "wash":
|
|
2759
3318
|
case "drink":
|
|
2760
3319
|
return routine ? no("busy") : sinkVerdict(use, spec, host, world.water.isFlowing(objectId, use.fixture));
|
|
3320
|
+
case "plunge":
|
|
3321
|
+
return drainVerdict(host, () => drains.plungeBlocked(use, host), () => drains.plungeBody(use));
|
|
2761
3322
|
}
|
|
2762
3323
|
};
|
|
3324
|
+
const drainVerdict = (host, rule, body) => {
|
|
3325
|
+
const missing = drains.hostBlocked(host);
|
|
3326
|
+
if (missing) return no("host_lacks", { capability: missing });
|
|
3327
|
+
const broken = rule();
|
|
3328
|
+
if (broken) return no(broken);
|
|
3329
|
+
const action = body();
|
|
3330
|
+
if (!action) return no("needs_body");
|
|
3331
|
+
const why = useBody.blocked(action, host);
|
|
3332
|
+
return why ? no(why) : YES2;
|
|
3333
|
+
};
|
|
3334
|
+
const withBody = (action, host, vars, run) => {
|
|
3335
|
+
if (!action) return { ok: false, text: world.t("needs_body", vars) };
|
|
3336
|
+
return useBody.begin(action, host) ? run(action) : { ok: false, text: world.t("body_busy", vars) };
|
|
3337
|
+
};
|
|
2763
3338
|
const itemName = (id) => {
|
|
2764
3339
|
const text = world.t(`item.${id}`);
|
|
2765
3340
|
return text === `item.${id}` ? id.replace(/_/g, " ") : text;
|
|
@@ -2795,7 +3370,10 @@ function createUses(options) {
|
|
|
2795
3370
|
}
|
|
2796
3371
|
case "fill":
|
|
2797
3372
|
return fill(bowls.get(use.bowl), host, vars);
|
|
3373
|
+
case "plunge":
|
|
3374
|
+
return withBody(drains.plungeBody(use), host, vars, (action) => drains.plunge(use, action));
|
|
2798
3375
|
case "relieve": {
|
|
3376
|
+
if (drains.has(use.basin)) return withBody(drains.relieveBody(use, host), host, vars, (action) => drains.relieve(use, host, action));
|
|
2799
3377
|
const now = soil.get(use.basin) ?? "clean";
|
|
2800
3378
|
soil.set(use.basin, SOIL_RANK[use.kind] > SOIL_RANK[now] ? use.kind : now);
|
|
2801
3379
|
return { ok: true, text: world.t(`done_relieve_${use.kind}`, vars) };
|
|
@@ -2853,7 +3431,10 @@ function createUses(options) {
|
|
|
2853
3431
|
const v = verdict(use, host);
|
|
2854
3432
|
return v.available ? runUse(use, host) : { ok: false, text: reasonOf(v, use, host) };
|
|
2855
3433
|
},
|
|
2856
|
-
update(dt) {
|
|
3434
|
+
update(dt, host) {
|
|
3435
|
+
if (host) lastHost = host;
|
|
3436
|
+
drains.update(dt, lastHost);
|
|
3437
|
+
useBody.update(dt);
|
|
2857
3438
|
stepRoutine(dt);
|
|
2858
3439
|
if (!program) return;
|
|
2859
3440
|
program.left -= dt;
|
|
@@ -2863,17 +3444,25 @@ function createUses(options) {
|
|
|
2863
3444
|
program = null;
|
|
2864
3445
|
lastHost?.say(done);
|
|
2865
3446
|
},
|
|
2866
|
-
flushed(fixture) {
|
|
3447
|
+
flushed(fixture, host) {
|
|
3448
|
+
if (host) lastHost = host;
|
|
3449
|
+
if (drains.flushed(fixture, lastHost)) return;
|
|
2867
3450
|
const basin = cleans.get(fixture);
|
|
2868
3451
|
if (!basin || soil.get(basin) === "clean") return;
|
|
2869
3452
|
soil.set(basin, "clean");
|
|
2870
3453
|
lastHost?.say(world.t("done_clean", { name: name() }));
|
|
2871
3454
|
},
|
|
3455
|
+
dispose(host) {
|
|
3456
|
+
drains.dispose(host ?? lastHost);
|
|
3457
|
+
useBody.end();
|
|
3458
|
+
},
|
|
2872
3459
|
state: () => ({
|
|
2873
3460
|
bowls: Object.fromEntries(level),
|
|
2874
|
-
soil: Object.fromEntries(soil),
|
|
3461
|
+
soil: Object.fromEntries([...soil].map(([id, now]) => [id, drains.has(id) ? drains.soil(id) : now])),
|
|
3462
|
+
drains: drains.state(),
|
|
2875
3463
|
program: program ? { use: program.use.id, device: program.use.device, left: program.left } : null,
|
|
2876
|
-
routine: routine?.use ?? null
|
|
3464
|
+
routine: routine?.use ?? null,
|
|
3465
|
+
body: useBody.pose
|
|
2877
3466
|
})
|
|
2878
3467
|
};
|
|
2879
3468
|
}
|
|
@@ -2901,7 +3490,7 @@ function createObjectControls(options) {
|
|
|
2901
3490
|
onEvent: (event) => {
|
|
2902
3491
|
if (event.type === "message") said.push(event.text);
|
|
2903
3492
|
if (event.type === "part" && event.object === CONTROLLED_OBJECT) forwardPart(event.part, event.target >= 0.5);
|
|
2904
|
-
if (event.type === "fixture" && event.object === CONTROLLED_OBJECT && event.flowing) uses.flushed(event.fixture);
|
|
3493
|
+
if (event.type === "fixture" && event.object === CONTROLLED_OBJECT && event.flowing) uses.flushed(event.fixture, host ?? void 0);
|
|
2905
3494
|
}
|
|
2906
3495
|
});
|
|
2907
3496
|
const forwardPart = (part, open) => {
|
|
@@ -2946,14 +3535,17 @@ function createObjectControls(options) {
|
|
|
2946
3535
|
update(dt, lent) {
|
|
2947
3536
|
lend(lent);
|
|
2948
3537
|
world.update(dt);
|
|
2949
|
-
uses.update(dt);
|
|
3538
|
+
uses.update(dt, lent);
|
|
2950
3539
|
},
|
|
2951
3540
|
state() {
|
|
2952
3541
|
const screens = (spec.devices ?? []).filter((d) => d.kind === "screen" && world.power.isOn(CONTROLLED_OBJECT, d.id) && world.power.isPowered(CONTROLLED_OBJECT, d.id)).map((d) => ({ device: d.id, channel: world.power.channel(CONTROLLED_OBJECT, d.id) }));
|
|
2953
3542
|
const emitters = world.emitters().filter((e) => e.object === CONTROLLED_OBJECT && e.kind !== "water");
|
|
2954
3543
|
return { ...uses.state(), screens, emitters };
|
|
2955
3544
|
},
|
|
3545
|
+
serialize: () => world.serialize(),
|
|
3546
|
+
load: (state) => world.load(state),
|
|
2956
3547
|
dispose() {
|
|
3548
|
+
uses.dispose(host ?? void 0);
|
|
2957
3549
|
host = null;
|
|
2958
3550
|
world.remove(CONTROLLED_OBJECT);
|
|
2959
3551
|
world.remove(MAINS);
|
|
@@ -2961,14 +3553,18 @@ function createObjectControls(options) {
|
|
|
2961
3553
|
};
|
|
2962
3554
|
}
|
|
2963
3555
|
export {
|
|
3556
|
+
CLOGGED,
|
|
2964
3557
|
COLLAR_REACH,
|
|
2965
3558
|
CONTROLLED_OBJECT,
|
|
3559
|
+
DRINK_HANDS_LITRES,
|
|
3560
|
+
DRINK_HANDS_LPS,
|
|
2966
3561
|
DRINK_LITRES,
|
|
2967
3562
|
DRINK_LPS,
|
|
2968
3563
|
DRY_SECONDS,
|
|
2969
3564
|
END_GAP,
|
|
2970
3565
|
FILL_SHARE,
|
|
2971
3566
|
FLOW_DEFAULTS,
|
|
3567
|
+
FLUSH_POUR_SECONDS,
|
|
2972
3568
|
FLUSH_RUN_SECONDS,
|
|
2973
3569
|
GESTURE_OF_ACTION,
|
|
2974
3570
|
GESTURE_OF_USE,
|
|
@@ -2978,10 +3574,13 @@ export {
|
|
|
2978
3574
|
buildChain,
|
|
2979
3575
|
castWithMember,
|
|
2980
3576
|
chainPoints,
|
|
3577
|
+
createBasinDrain,
|
|
3578
|
+
createDrainUses,
|
|
2981
3579
|
createEnginePartPhysics,
|
|
2982
3580
|
createFixtureEmitters,
|
|
2983
3581
|
createObjectControls,
|
|
2984
3582
|
createSinkRoutines,
|
|
3583
|
+
createUseBody,
|
|
2985
3584
|
createWorld,
|
|
2986
3585
|
devicePointOf,
|
|
2987
3586
|
dirtyDishes,
|