@lubomirklimes/character-runtime 0.0.0-stage → 0.1.0-preview.1
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/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/acs/acs15.d.ts +9 -0
- package/dist/acs/acs15.js +382 -0
- package/dist/acs/acs2-reader.d.ts +2 -0
- package/dist/acs/acs2-reader.js +277 -0
- package/dist/acs/binary.d.ts +1 -0
- package/dist/acs/binary.js +107 -0
- package/dist/acs/compound-file.d.ts +1 -0
- package/dist/acs/compound-file.js +326 -0
- package/dist/acs/document.d.ts +33 -0
- package/dist/acs/document.js +99 -0
- package/dist/acs/errors.d.ts +28 -0
- package/dist/acs/errors.js +41 -0
- package/dist/acs/image.d.ts +30 -0
- package/dist/acs/image.js +113 -0
- package/dist/acs/index.d.ts +11 -0
- package/dist/acs/index.js +10 -0
- package/dist/acs/lz.d.ts +7 -0
- package/dist/acs/lz.js +102 -0
- package/dist/acs/metadata.d.ts +14 -0
- package/dist/acs/metadata.js +126 -0
- package/dist/acs/model.d.ts +130 -0
- package/dist/acs/model.js +12 -0
- package/dist/acs/mouth.d.ts +37 -0
- package/dist/acs/mouth.js +43 -0
- package/dist/acs/wave.d.ts +53 -0
- package/dist/acs/wave.js +422 -0
- package/dist/browser/animation-frame-clock.d.ts +18 -0
- package/dist/browser/animation-frame-clock.js +42 -0
- package/dist/browser/canvas-renderer.d.ts +50 -0
- package/dist/browser/canvas-renderer.js +145 -0
- package/dist/browser/host.d.ts +84 -0
- package/dist/browser/host.js +206 -0
- package/dist/browser/index.d.ts +6 -0
- package/dist/browser/index.js +8 -0
- package/dist/browser/web-audio.d.ts +48 -0
- package/dist/browser/web-audio.js +205 -0
- package/dist/browser/web-speech.d.ts +131 -0
- package/dist/browser/web-speech.js +302 -0
- package/dist/core/activities.d.ts +10 -0
- package/dist/core/activities.js +479 -0
- package/dist/core/agent.d.ts +263 -0
- package/dist/core/agent.js +764 -0
- package/dist/core/audio.d.ts +17 -0
- package/dist/core/audio.js +174 -0
- package/dist/core/balloon.d.ts +43 -0
- package/dist/core/balloon.js +94 -0
- package/dist/core/clock.d.ts +32 -0
- package/dist/core/clock.js +51 -0
- package/dist/core/compositor.d.ts +22 -0
- package/dist/core/compositor.js +67 -0
- package/dist/core/events.d.ts +131 -0
- package/dist/core/events.js +1 -0
- package/dist/core/host.d.ts +29 -0
- package/dist/core/host.js +77 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +13 -0
- package/dist/core/lipsync.d.ts +41 -0
- package/dist/core/lipsync.js +209 -0
- package/dist/core/random.d.ts +52 -0
- package/dist/core/random.js +105 -0
- package/dist/core/request.d.ts +68 -0
- package/dist/core/request.js +131 -0
- package/dist/core/resources.d.ts +70 -0
- package/dist/core/resources.js +243 -0
- package/dist/core/scheduler.d.ts +25 -0
- package/dist/core/scheduler.js +766 -0
- package/dist/core/source.d.ts +21 -0
- package/dist/core/source.js +54 -0
- package/dist/core/speech.d.ts +91 -0
- package/dist/core/speech.js +86 -0
- package/dist/core/text.d.ts +38 -0
- package/dist/core/text.js +97 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -0
- package/package.json +68 -4
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import { buildLipSync, MouthTimeline } from './lipsync.js';
|
|
2
|
+
import { pacedSpeech } from './text.js';
|
|
3
|
+
/** @internal Show/Hide: Showing/Hiding while the state animation plays, then Visible/Hidden. */
|
|
4
|
+
export class VisibilityActivity {
|
|
5
|
+
agent;
|
|
6
|
+
show;
|
|
7
|
+
began = false;
|
|
8
|
+
animated = false;
|
|
9
|
+
beganAt = 0;
|
|
10
|
+
fastRest = null;
|
|
11
|
+
note = null;
|
|
12
|
+
constructor(agent, show) {
|
|
13
|
+
this.agent = agent;
|
|
14
|
+
this.show = show;
|
|
15
|
+
}
|
|
16
|
+
/** The state animation, or null (already there, fast, or the state is missing, empty or unusable). */
|
|
17
|
+
choose(fast) {
|
|
18
|
+
const agent = this.agent, show = this.show;
|
|
19
|
+
const current = agent.visibility;
|
|
20
|
+
if (show ? current === 'visible' : current === 'hidden')
|
|
21
|
+
return null;
|
|
22
|
+
const stateName = show ? 'Showing' : 'Hiding';
|
|
23
|
+
const state = agent.stateKey(stateName);
|
|
24
|
+
if (state === null) {
|
|
25
|
+
if (!fast)
|
|
26
|
+
this.note = `No ${stateName} state; ${show ? 'shown' : 'hidden'} without an animation.`;
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const usable = agent.usableAnimations(state).filter((n) => this.frameCount(n) > 0);
|
|
30
|
+
const verb = show ? 'shown' : 'hidden';
|
|
31
|
+
if (usable.length === 0) {
|
|
32
|
+
this.note = `${stateName} state has no usable animation; ${verb} without an animation.`;
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (fast) {
|
|
36
|
+
if (show)
|
|
37
|
+
this.fastRest = usable[0];
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const chosen = usable.length === 1 ? usable[0] : usable[agent.scheduler.random.nextInt(usable.length)];
|
|
41
|
+
const refusal = agent.checkAnimation(chosen);
|
|
42
|
+
if (refusal !== null) {
|
|
43
|
+
this.note = `${stateName} animation ${chosen} cannot play (${refusal}); ${verb} without an animation.`;
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
this.animated = true;
|
|
47
|
+
return chosen;
|
|
48
|
+
}
|
|
49
|
+
frameCount(name) {
|
|
50
|
+
const i = this.agent.scheduler.indexOf(name);
|
|
51
|
+
return i >= 0 ? this.agent.character.animations[i].frames.length : 0;
|
|
52
|
+
}
|
|
53
|
+
begin(time) {
|
|
54
|
+
this.began = true;
|
|
55
|
+
this.beganAt = time;
|
|
56
|
+
const agent = this.agent, show = this.show, current = agent.visibility;
|
|
57
|
+
if (show ? current === 'visible' : current === 'hidden')
|
|
58
|
+
return;
|
|
59
|
+
if (show && !this.animated) {
|
|
60
|
+
if (this.fastRest !== null)
|
|
61
|
+
agent.scheduler.presentResting(agent.scheduler.indexOf(this.fastRest), time);
|
|
62
|
+
else
|
|
63
|
+
agent.scheduler.clearPresentation(time);
|
|
64
|
+
}
|
|
65
|
+
agent.setVisibility(time, this.animated ? (show ? 'showing' : 'hiding') : show ? 'visible' : 'hidden');
|
|
66
|
+
}
|
|
67
|
+
poll() {
|
|
68
|
+
return this.began ? this.beganAt : null;
|
|
69
|
+
}
|
|
70
|
+
end(time, status) {
|
|
71
|
+
if (!this.began)
|
|
72
|
+
return;
|
|
73
|
+
// Cancelled or failed after it began: a Show leaves the character visible, and a Hide did not happen.
|
|
74
|
+
this.agent.setVisibility(time, status === 'completed' && !this.show ? 'hidden' : 'visible');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** @internal MoveTo: linear movement over a duration from distance and speed (Runtime.md §13.2). */
|
|
78
|
+
export class MoveActivity {
|
|
79
|
+
agent;
|
|
80
|
+
target;
|
|
81
|
+
speed;
|
|
82
|
+
maxDurationMs;
|
|
83
|
+
from = { x: 0, y: 0 };
|
|
84
|
+
start = 0;
|
|
85
|
+
duration = 0;
|
|
86
|
+
began = false;
|
|
87
|
+
ended = false;
|
|
88
|
+
constructor(agent, target, speed, maxDurationMs) {
|
|
89
|
+
this.agent = agent;
|
|
90
|
+
this.target = target;
|
|
91
|
+
this.speed = speed;
|
|
92
|
+
this.maxDurationMs = maxDurationMs;
|
|
93
|
+
}
|
|
94
|
+
/** The directional Moving state (character-relative), or null when hidden, instant or missing. */
|
|
95
|
+
choose(time) {
|
|
96
|
+
const p = this.agent.positionAt(time);
|
|
97
|
+
const dx = this.target.x - p.x, dy = this.target.y - p.y;
|
|
98
|
+
if (this.agent.visibility === 'hidden' || this.speed <= 0 || (dx === 0 && dy === 0))
|
|
99
|
+
return null;
|
|
100
|
+
const direction = Math.abs(dx) >= Math.abs(dy) ? (dx > 0 ? 'Left' : 'Right') : dy > 0 ? 'Down' : 'Up';
|
|
101
|
+
const state = this.agent.stateKey('Moving' + direction);
|
|
102
|
+
return state === null ? null : this.agent.chooseState(state);
|
|
103
|
+
}
|
|
104
|
+
begin(time) {
|
|
105
|
+
this.began = true;
|
|
106
|
+
this.start = time;
|
|
107
|
+
this.from = this.agent.positionAt(time);
|
|
108
|
+
const dx = this.target.x - this.from.x, dy = this.target.y - this.from.y;
|
|
109
|
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
110
|
+
this.duration = this.agent.visibility === 'hidden' || this.speed <= 0 ? 0 : Math.min(this.maxDurationMs, (distance / this.speed) * 1000);
|
|
111
|
+
this.agent.move = this;
|
|
112
|
+
this.agent.raise({ type: 'move-started', timeMs: time, requestId: this.agent.scheduler.activeRequestId, from: this.from, to: this.target, durationMs: this.duration });
|
|
113
|
+
}
|
|
114
|
+
/** The host moved the character during the movement: continue from there for the remaining time. */
|
|
115
|
+
restart(time, at) {
|
|
116
|
+
if (!this.began || this.ended)
|
|
117
|
+
return;
|
|
118
|
+
const remaining = Math.max(0, this.start + this.duration - time);
|
|
119
|
+
this.from = at;
|
|
120
|
+
this.start = time;
|
|
121
|
+
this.duration = remaining;
|
|
122
|
+
}
|
|
123
|
+
positionAt(time) {
|
|
124
|
+
if (!this.began || this.duration <= 0)
|
|
125
|
+
return this.began ? this.target : this.from;
|
|
126
|
+
const k = Math.min(1, Math.max(0, (time - this.start) / this.duration));
|
|
127
|
+
return { x: this.from.x + (this.target.x - this.from.x) * k, y: this.from.y + (this.target.y - this.from.y) * k };
|
|
128
|
+
}
|
|
129
|
+
poll(now) {
|
|
130
|
+
return this.began && this.start + this.duration <= now ? this.start + this.duration : null;
|
|
131
|
+
}
|
|
132
|
+
shift(delta) {
|
|
133
|
+
this.start += delta;
|
|
134
|
+
}
|
|
135
|
+
end(time, status) {
|
|
136
|
+
if (!this.began || this.ended)
|
|
137
|
+
return;
|
|
138
|
+
this.ended = true;
|
|
139
|
+
// A move cancelled while the agent is paused ends where it was paused.
|
|
140
|
+
const at = status === 'completed' ? this.target : this.positionAt(this.agent.frozen(time));
|
|
141
|
+
const id = this.agent.scheduler.activeRequestId;
|
|
142
|
+
this.agent.endMove(this, at);
|
|
143
|
+
this.agent.raise({ type: 'move-ended', timeMs: time, requestId: id, position: at, completed: status === 'completed' });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/** @internal Wait: holds the queue until another request (of any agent) is terminal (Runtime.md §4.4). */
|
|
147
|
+
export class WaitActivity {
|
|
148
|
+
target;
|
|
149
|
+
beganAt = 0;
|
|
150
|
+
constructor(target) {
|
|
151
|
+
this.target = target;
|
|
152
|
+
}
|
|
153
|
+
begin(time) {
|
|
154
|
+
this.beganAt = time;
|
|
155
|
+
}
|
|
156
|
+
poll(now, floor) {
|
|
157
|
+
const end = this.target.endedAtMs;
|
|
158
|
+
if (end === null)
|
|
159
|
+
return null;
|
|
160
|
+
const at = Math.max(Math.max(this.beganAt, end), Math.min(floor, now));
|
|
161
|
+
return at <= now ? at : null;
|
|
162
|
+
}
|
|
163
|
+
end() { }
|
|
164
|
+
shift(delta) {
|
|
165
|
+
this.beganAt += delta;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/** @internal Speak and Think: speech audio, word timing, lip-sync and the balloon under the runtime clock (Runtime.md §14–§16). */
|
|
169
|
+
export class SpeakActivity {
|
|
170
|
+
agent;
|
|
171
|
+
handle;
|
|
172
|
+
text;
|
|
173
|
+
kind;
|
|
174
|
+
showBalloon;
|
|
175
|
+
pacing;
|
|
176
|
+
abort = new AbortController();
|
|
177
|
+
synthesis = null;
|
|
178
|
+
ready = null;
|
|
179
|
+
lipSource = 'none';
|
|
180
|
+
mouth = new MouthTimeline();
|
|
181
|
+
began = false;
|
|
182
|
+
started = false;
|
|
183
|
+
ended = false;
|
|
184
|
+
speechDone = false;
|
|
185
|
+
recorded;
|
|
186
|
+
beganAt = 0;
|
|
187
|
+
start = 0;
|
|
188
|
+
duration = 0;
|
|
189
|
+
wordIndex = 0;
|
|
190
|
+
nextCue = 0;
|
|
191
|
+
cueTimes = [];
|
|
192
|
+
lastMouth = null;
|
|
193
|
+
constructor(agent, handle, text, kind, content, showBalloon, pacing) {
|
|
194
|
+
this.agent = agent;
|
|
195
|
+
this.handle = handle;
|
|
196
|
+
this.text = text;
|
|
197
|
+
this.kind = kind;
|
|
198
|
+
this.showBalloon = showBalloon;
|
|
199
|
+
this.pacing = pacing;
|
|
200
|
+
this.recorded = content !== null;
|
|
201
|
+
if (content) {
|
|
202
|
+
const paced = pacedSpeech(text, pacing);
|
|
203
|
+
const duration = content.durationMs ?? content.audio?.durationMs ?? paced.durationMs;
|
|
204
|
+
this.ready = { audio: content.audio ?? null, durationMs: duration, words: content.words ?? (content.audio ? [] : paced.words), visemes: content.mouth ?? [], phonemes: [], voice: null, note: 'Recorded speech.' };
|
|
205
|
+
}
|
|
206
|
+
else if (kind === 'thought')
|
|
207
|
+
this.ready = this.silent('Thought balloon (no speech).');
|
|
208
|
+
if (this.ready)
|
|
209
|
+
this.progress('ready');
|
|
210
|
+
}
|
|
211
|
+
progress(p) {
|
|
212
|
+
this.handle.setSpeech(p, this.ready ? this.ready.audio !== null : null, this.ready?.durationMs ?? null, this.ready?.note ?? null);
|
|
213
|
+
}
|
|
214
|
+
silent(reason) {
|
|
215
|
+
const p = pacedSpeech(this.text, this.pacing);
|
|
216
|
+
return { audio: null, durationMs: p.durationMs, words: p.words, visemes: [], phonemes: [], voice: null, note: reason };
|
|
217
|
+
}
|
|
218
|
+
/** Starts synthesis at submission (it overlaps the wait in the queue). Without a provider the speech is paced from the text. */
|
|
219
|
+
startSynthesis(provider, voice, ratePercent) {
|
|
220
|
+
if (!provider) {
|
|
221
|
+
this.ready = this.silent(this.agent.speechAudioProblem ?? 'Speech disabled; paced without audio.');
|
|
222
|
+
this.progress('ready');
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
try {
|
|
226
|
+
const result = provider.synthesize({ text: this.text, voice, ratePercent }, this.abort.signal);
|
|
227
|
+
if (isThenable(result)) {
|
|
228
|
+
const pending = { state: 'pending' };
|
|
229
|
+
this.synthesis = pending;
|
|
230
|
+
Promise.resolve(result).then((value) => { if (this.synthesis === pending)
|
|
231
|
+
this.synthesis = { state: 'done', value }; }, (error) => { if (this.synthesis === pending)
|
|
232
|
+
this.synthesis = { state: 'failed', error }; });
|
|
233
|
+
}
|
|
234
|
+
else
|
|
235
|
+
this.synthesis = { state: 'done', value: result };
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
this.synthesis = { state: 'failed', error };
|
|
239
|
+
}
|
|
240
|
+
this.progress('synthesizing');
|
|
241
|
+
}
|
|
242
|
+
tryReady() {
|
|
243
|
+
if (this.ready)
|
|
244
|
+
return true;
|
|
245
|
+
const s = this.synthesis;
|
|
246
|
+
if (!s || s.state === 'pending')
|
|
247
|
+
return false;
|
|
248
|
+
const audio = s.state === 'done' ? s.value : null;
|
|
249
|
+
if (audio && Number.isFinite(audio.durationMs) && audio.durationMs >= 0)
|
|
250
|
+
this.ready = audio;
|
|
251
|
+
else {
|
|
252
|
+
const why = s.state === 'failed' ? (s.error instanceof Error ? s.error.message : String(s.error)) : 'invalid result';
|
|
253
|
+
this.ready = this.silent(`Speech synthesis failed (${why}); paced without audio.`);
|
|
254
|
+
this.agent.raise({ type: 'runtime-error', timeMs: Math.max(this.beganAt, this.agent.hostNow), source: 'speech', message: this.ready.note ?? '' });
|
|
255
|
+
}
|
|
256
|
+
this.progress('ready');
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
begin(time) {
|
|
260
|
+
this.began = true;
|
|
261
|
+
this.beganAt = time;
|
|
262
|
+
this.agent.speech = this;
|
|
263
|
+
if (this.tryReady())
|
|
264
|
+
this.startSpeech(time);
|
|
265
|
+
}
|
|
266
|
+
startSpeech(time) {
|
|
267
|
+
this.started = true;
|
|
268
|
+
this.start = time;
|
|
269
|
+
const speech = this.ready;
|
|
270
|
+
this.duration = Math.min(Math.max(0, speech.durationMs), 600_000);
|
|
271
|
+
const { cues, source } = this.kind === 'thought' ? { cues: [], source: 'none' } : buildLipSync(speech, this.text, this.recorded);
|
|
272
|
+
this.mouth.load(cues);
|
|
273
|
+
this.cueTimes = [...new Set(cues.map((c) => c.timeMs))];
|
|
274
|
+
this.lipSource = source;
|
|
275
|
+
const note = speech.note ?? '';
|
|
276
|
+
this.agent.raise({ type: 'speech-started', timeMs: time, requestId: this.handle.id, text: this.text, voice: speech.voice?.name ?? null, lipSync: source, durationMs: this.duration, note });
|
|
277
|
+
if (this.showBalloon && this.agent.visibility !== 'hidden') {
|
|
278
|
+
const style = this.agent.balloonStyle;
|
|
279
|
+
this.agent.balloonController.show(time, this.handle.id, this.kind, this.text, style, this.agent.anchor, !style.autoPace);
|
|
280
|
+
}
|
|
281
|
+
this.progress('speaking');
|
|
282
|
+
if (speech.audio)
|
|
283
|
+
this.agent.playSpeech(speech.audio, time);
|
|
284
|
+
else if (this.kind === 'speech')
|
|
285
|
+
this.agent.reportNoSpeechAudio(time, this.handle.id, note);
|
|
286
|
+
}
|
|
287
|
+
poll(now, floor) {
|
|
288
|
+
if (!this.began)
|
|
289
|
+
return null;
|
|
290
|
+
if (!this.started) {
|
|
291
|
+
if (!this.tryReady())
|
|
292
|
+
return null;
|
|
293
|
+
this.startSpeech(Math.min(now, Math.max(this.beganAt, floor)));
|
|
294
|
+
}
|
|
295
|
+
this.tick(now);
|
|
296
|
+
return this.start + this.duration <= now ? this.start + this.duration : null;
|
|
297
|
+
}
|
|
298
|
+
/** Emits word, mouth and balloon progression up to `now`. */
|
|
299
|
+
tick(now) {
|
|
300
|
+
if (!this.started || this.ended || this.speechDone)
|
|
301
|
+
return;
|
|
302
|
+
const until = Math.min(now, this.start + this.duration);
|
|
303
|
+
const words = this.ready.words;
|
|
304
|
+
while (this.wordIndex < words.length && this.start + words[this.wordIndex].offsetMs <= until) {
|
|
305
|
+
const w = words[this.wordIndex];
|
|
306
|
+
const at = this.start + Math.max(0, w.offsetMs);
|
|
307
|
+
this.agent.raise({ type: 'speech-word', timeMs: at, requestId: this.handle.id, word: this.wordIndex, textStart: w.textStart, textLength: w.textLength });
|
|
308
|
+
this.agent.balloonController.reveal(at, this.handle.id, w.textStart + w.textLength);
|
|
309
|
+
this.wordIndex++;
|
|
310
|
+
}
|
|
311
|
+
if (this.kind === 'speech') {
|
|
312
|
+
while (this.nextCue < this.cueTimes.length && this.start + this.cueTimes[this.nextCue] <= until) {
|
|
313
|
+
const at = this.start + this.cueTimes[this.nextCue++];
|
|
314
|
+
const shape = this.mouth.at(at - this.start);
|
|
315
|
+
if (shape !== this.lastMouth) {
|
|
316
|
+
this.lastMouth = shape;
|
|
317
|
+
this.agent.raise({ type: 'mouth', timeMs: at, requestId: this.handle.id, shape });
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (now >= this.start + this.duration)
|
|
322
|
+
this.finishSpeech(this.start + this.duration, true);
|
|
323
|
+
}
|
|
324
|
+
finishSpeech(time, completed) {
|
|
325
|
+
if (this.speechDone)
|
|
326
|
+
return;
|
|
327
|
+
this.speechDone = true;
|
|
328
|
+
this.progress('ended');
|
|
329
|
+
if (this.ready?.audio && !completed)
|
|
330
|
+
this.agent.stopSpeechAudio();
|
|
331
|
+
if (this.kind === 'speech' && this.lastMouth !== null) {
|
|
332
|
+
this.lastMouth = null;
|
|
333
|
+
this.agent.raise({ type: 'mouth', timeMs: time, requestId: this.handle.id, shape: null });
|
|
334
|
+
}
|
|
335
|
+
this.agent.raise({ type: 'speech-ended', timeMs: time, requestId: this.handle.id, completed });
|
|
336
|
+
this.agent.balloonController.finish(time, this.handle.id, completed, this.pacing.hideDelayMs);
|
|
337
|
+
}
|
|
338
|
+
mouthAt(now) {
|
|
339
|
+
return this.started && !this.ended && !this.speechDone && this.kind === 'speech' && now >= this.start && now < this.start + this.duration ? this.mouth.at(now - this.start) : null;
|
|
340
|
+
}
|
|
341
|
+
/** The presented speech. */
|
|
342
|
+
state(now) {
|
|
343
|
+
return { requestId: this.handle.id, text: this.text, startedAtMs: this.start, durationMs: this.duration, voice: this.ready?.voice?.name ?? null, lipSync: this.lipSource, mouth: this.mouthAt(now), hasAudio: !!this.ready?.audio };
|
|
344
|
+
}
|
|
345
|
+
pause() {
|
|
346
|
+
if (this.started && !this.ended && !this.speechDone)
|
|
347
|
+
this.agent.stopSpeechAudio();
|
|
348
|
+
}
|
|
349
|
+
resume(time) {
|
|
350
|
+
const audio = this.ready?.audio;
|
|
351
|
+
if (this.started && !this.ended && !this.speechDone && audio && time - this.start < this.duration)
|
|
352
|
+
this.agent.playSpeech(audio, this.start);
|
|
353
|
+
}
|
|
354
|
+
shift(delta) {
|
|
355
|
+
this.beganAt += delta;
|
|
356
|
+
if (this.started)
|
|
357
|
+
this.start += delta;
|
|
358
|
+
}
|
|
359
|
+
end(time, status, _reason) {
|
|
360
|
+
if (this.ended)
|
|
361
|
+
return;
|
|
362
|
+
this.abort.abort();
|
|
363
|
+
const completed = status === 'completed';
|
|
364
|
+
if (this.started && completed)
|
|
365
|
+
this.tick(time);
|
|
366
|
+
if (this.started)
|
|
367
|
+
this.finishSpeech(time, completed);
|
|
368
|
+
this.ended = true;
|
|
369
|
+
if (this.agent.speech === this)
|
|
370
|
+
this.agent.speech = null;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
function isThenable(value) {
|
|
374
|
+
return typeof value === 'object' && value !== null && typeof value.then === 'function';
|
|
375
|
+
}
|
|
376
|
+
const IDLE_STATES = ['IdlingLevel1', 'IdlingLevel2', 'IdlingLevel3'];
|
|
377
|
+
/** @internal Idle scheduling: IdlingLevel1 → 2 → 3, level 3 persists. */
|
|
378
|
+
export class IdleBehavior {
|
|
379
|
+
agent;
|
|
380
|
+
levelIndex = -1;
|
|
381
|
+
plays = 0;
|
|
382
|
+
lastWasIdle = false;
|
|
383
|
+
lastActivity = 0;
|
|
384
|
+
enabled;
|
|
385
|
+
firstDelayMs;
|
|
386
|
+
intervalMs;
|
|
387
|
+
playsBeforeNextLevel;
|
|
388
|
+
constructor(agent, options) {
|
|
389
|
+
this.agent = agent;
|
|
390
|
+
this.enabled = options.enabled ?? true;
|
|
391
|
+
this.firstDelayMs = options.firstDelayMs ?? 4000;
|
|
392
|
+
this.intervalMs = options.intervalMs ?? 4000;
|
|
393
|
+
this.playsBeforeNextLevel = options.playsBeforeNextLevel ?? 10;
|
|
394
|
+
if (!(Number.isFinite(this.firstDelayMs) && this.firstDelayMs >= 0 && Number.isFinite(this.intervalMs) && this.intervalMs >= 0 && this.playsBeforeNextLevel >= 1))
|
|
395
|
+
throw new RangeError('Invalid idle options.');
|
|
396
|
+
}
|
|
397
|
+
/** 1–3, or 0 when the character has no usable idle state. */
|
|
398
|
+
get level() {
|
|
399
|
+
const i = this.first();
|
|
400
|
+
return this.levelIndex >= 0 ? this.levelIndex + 1 : i >= 0 ? i + 1 : 0;
|
|
401
|
+
}
|
|
402
|
+
first() {
|
|
403
|
+
for (let i = 0; i < IDLE_STATES.length; i++)
|
|
404
|
+
if (this.usable(i))
|
|
405
|
+
return i;
|
|
406
|
+
return -1;
|
|
407
|
+
}
|
|
408
|
+
usable(i) {
|
|
409
|
+
const key = this.agent.stateKey(IDLE_STATES[i]);
|
|
410
|
+
return key !== null && this.agent.usableAnimations(key).length > 0;
|
|
411
|
+
}
|
|
412
|
+
setEnabled(value, time) {
|
|
413
|
+
if (this.enabled === value)
|
|
414
|
+
return;
|
|
415
|
+
this.enabled = value;
|
|
416
|
+
this.activity(time);
|
|
417
|
+
}
|
|
418
|
+
next(idleSince, now) {
|
|
419
|
+
if (!this.enabled || this.agent.visibility !== 'visible')
|
|
420
|
+
return null;
|
|
421
|
+
if (this.levelIndex < 0)
|
|
422
|
+
this.levelIndex = this.first();
|
|
423
|
+
if (this.levelIndex < 0)
|
|
424
|
+
return null;
|
|
425
|
+
const due = Math.max(idleSince, this.lastActivity) + (this.lastWasIdle ? this.intervalMs : this.firstDelayMs);
|
|
426
|
+
if (due > now)
|
|
427
|
+
return null;
|
|
428
|
+
const key = this.agent.stateKey(IDLE_STATES[this.levelIndex]);
|
|
429
|
+
const handle = this.agent.createHandle('idle', key, null);
|
|
430
|
+
return {
|
|
431
|
+
due,
|
|
432
|
+
plan: { kind: 'idle', name: key, stateName: key, tag: handle, interruptible: true, chooseAnimation: () => this.agent.chooseState(key), activity: new IdleCounter(this) },
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
activity(time) {
|
|
436
|
+
this.lastActivity = Math.max(this.lastActivity, time);
|
|
437
|
+
this.lastWasIdle = false;
|
|
438
|
+
this.plays = 0;
|
|
439
|
+
const first = this.first();
|
|
440
|
+
if (this.levelIndex !== first && first >= 0 && this.levelIndex >= 0) {
|
|
441
|
+
this.levelIndex = first;
|
|
442
|
+
this.agent.raise({ type: 'idle-level', timeMs: time, level: first + 1 });
|
|
443
|
+
}
|
|
444
|
+
else
|
|
445
|
+
this.levelIndex = first;
|
|
446
|
+
}
|
|
447
|
+
/** @internal */
|
|
448
|
+
played(time) {
|
|
449
|
+
this.lastWasIdle = true;
|
|
450
|
+
if (++this.plays < this.playsBeforeNextLevel)
|
|
451
|
+
return;
|
|
452
|
+
for (let next = this.levelIndex + 1; next < IDLE_STATES.length; next++) {
|
|
453
|
+
if (this.usable(next)) {
|
|
454
|
+
this.levelIndex = next;
|
|
455
|
+
this.plays = 0;
|
|
456
|
+
this.agent.raise({ type: 'idle-level', timeMs: time, level: next + 1 });
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
this.plays = 0;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
class IdleCounter {
|
|
464
|
+
idle;
|
|
465
|
+
began = null;
|
|
466
|
+
constructor(idle) {
|
|
467
|
+
this.idle = idle;
|
|
468
|
+
}
|
|
469
|
+
begin(time) {
|
|
470
|
+
this.began = time;
|
|
471
|
+
}
|
|
472
|
+
poll() {
|
|
473
|
+
return this.began;
|
|
474
|
+
}
|
|
475
|
+
end(time, status) {
|
|
476
|
+
if (status === 'completed')
|
|
477
|
+
this.idle.played(time);
|
|
478
|
+
}
|
|
479
|
+
}
|