@adatechnology/game-vehicles-proving 0.1.13 → 0.2.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.
@@ -22,6 +22,7 @@ var GameVehiclesProving = (() => {
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
24
  DEFAULT_DENSITY: () => DEFAULT_DENSITY,
25
+ STEPS_PER_TICK: () => STEPS_PER_TICK,
25
26
  accelerationRange: () => accelerationRange,
26
27
  brakeRange: () => brakeRange,
27
28
  certify: () => certify,
@@ -46,18 +47,41 @@ var GameVehiclesProving = (() => {
46
47
  // ../game-contracts/dist/index.js
47
48
  var DEFAULT_BLAST_IMPULSE = { bodyArea: 0.7, lift: 0.15, minSpeed: 0.3, tntPerJoule: 0.24 / 2e5 };
48
49
  var DEFAULT_COST_ALLOCATION_BYTES = 1024 * 1024;
50
+ var FRAME_PHASES = ["input", "fixedSimulate", "lateSimulate", "present", "render", "idle"];
49
51
  var DEFERRED_QUEUE_ID = {
50
52
  DESTRUCTION_EVENTS: "destruction.events",
51
53
  BREACH_PAINT: "floorplan.breach-paint",
54
+ BREACH_CUT: "floorplan.breach-cut",
52
55
  AUDIO_VOICES: "audio.voices",
53
- VEHICLE_CREASE: "vehicles.crease"
56
+ VEHICLE_CREASE: "vehicles.crease",
57
+ AUDIO_OCCLUSION: "audio.occlusion",
58
+ FLUID_FILM: "fluids.film",
59
+ VEHICLE_LOD_BATCH: "vehicles.lod-batch",
60
+ SETTLED_BATCH: "destruction.settled-batch",
61
+ STATIC_BATCH: "render.static-batch",
62
+ ANIMALS_STATIC_BATCH: "animals.static-batch",
63
+ CHARACTERS_STATIC_BATCH: "characters.static-batch",
64
+ HOUSE_SHADOW_BATCH: "floorplan.shadow-batch",
65
+ DESTRUCTION_PREFIT: "destruction.prefit"
54
66
  };
55
67
  var DEFERRED_PRIORITY = {
56
68
  [DEFERRED_QUEUE_ID.DESTRUCTION_EVENTS]: { priority: 300, phase: "present" },
57
- [DEFERRED_QUEUE_ID.BREACH_PAINT]: { priority: 250, phase: "present" },
69
+ // every frame: the walls cut since the last frame are painted once each, and a hole must show in the frame of the blast
70
+ [DEFERRED_QUEUE_ID.BREACH_CUT]: { priority: 260, phase: "present", minEveryFrames: 1 },
71
+ [DEFERRED_QUEUE_ID.BREACH_PAINT]: { priority: 250, phase: "present", minEveryFrames: 1 },
58
72
  [DEFERRED_QUEUE_ID.AUDIO_VOICES]: { priority: 200, phase: "idle", reserveMs: 1 },
59
- [DEFERRED_QUEUE_ID.VEHICLE_CREASE]: { priority: 100, phase: "idle" }
73
+ [DEFERRED_QUEUE_ID.AUDIO_OCCLUSION]: { priority: 190, phase: "idle", minEveryFrames: 8 },
74
+ [DEFERRED_QUEUE_ID.FLUID_FILM]: { priority: 120, phase: "idle", minEveryFrames: 2 },
75
+ [DEFERRED_QUEUE_ID.VEHICLE_CREASE]: { priority: 100, phase: "idle" },
76
+ [DEFERRED_QUEUE_ID.VEHICLE_LOD_BATCH]: { priority: 90, phase: "idle", minEveryFrames: 4 },
77
+ [DEFERRED_QUEUE_ID.SETTLED_BATCH]: { priority: 70, phase: "idle", minEveryFrames: 8 },
78
+ [DEFERRED_QUEUE_ID.STATIC_BATCH]: { priority: 60, phase: "idle", minEveryFrames: 8 },
79
+ [DEFERRED_QUEUE_ID.ANIMALS_STATIC_BATCH]: { priority: 60, phase: "idle", minEveryFrames: 8 },
80
+ [DEFERRED_QUEUE_ID.CHARACTERS_STATIC_BATCH]: { priority: 60, phase: "idle", minEveryFrames: 8 },
81
+ [DEFERRED_QUEUE_ID.HOUSE_SHADOW_BATCH]: { priority: 50, phase: "idle", minEveryFrames: 8 },
82
+ [DEFERRED_QUEUE_ID.DESTRUCTION_PREFIT]: { priority: 40, phase: "idle", minEveryFrames: 30 }
60
83
  };
84
+ var MUST_DRAIN_QUEUE_IDS = /* @__PURE__ */ new Set([DEFERRED_QUEUE_ID.DESTRUCTION_EVENTS, DEFERRED_QUEUE_ID.BREACH_CUT]);
61
85
  var WARNED_KEY = /* @__PURE__ */ Symbol.for("@adatechnology/game-contracts.warned");
62
86
  var warnedKeys = () => {
63
87
  const holder = globalThis;
@@ -143,6 +167,605 @@ var GameVehiclesProving = (() => {
143
167
  var FOOTSTEP_SOUND_IDS = SURFACE_IDS.flatMap((surface) => GAIT_IDS.map((gait) => footstepSoundId(surface, gait)));
144
168
  var LAND_SOUND_IDS = SURFACE_IDS.flatMap((surface) => LANDING_IDS.map((landing) => `land_${surface}_${landing}`));
145
169
  var BODY_FOLEY_IDS = [...FOOTSTEP_SOUND_IDS, ...LAND_SOUND_IDS, ...BODY_SOUND_IDS];
170
+ var WORKER_KIND_ID = {
171
+ GROUND_TILES: "destruction.ground-tiles",
172
+ CROWD_PHYSICS: "crowd.physics",
173
+ PIECE_BOXES: "destruction.piece-boxes",
174
+ STATIC_COLLIDERS: "destruction.static-colliders",
175
+ SHAPES_GENERATE: "shapes.generate",
176
+ WORLD_REGION: "world.region"
177
+ };
178
+ var WORKER_KIND_DEFAULTS = {
179
+ [WORKER_KIND_ID.GROUND_TILES]: { lane: "sim", priority: 300, latencyFrames: 2, late: "sync" },
180
+ [WORKER_KIND_ID.CROWD_PHYSICS]: { lane: "sim", priority: 250, latencyFrames: 1, late: "sync" },
181
+ [WORKER_KIND_ID.PIECE_BOXES]: { lane: "sim", priority: 200, latencyFrames: 2, late: "sync" },
182
+ [WORKER_KIND_ID.STATIC_COLLIDERS]: { lane: "build", priority: 150, late: "sync" },
183
+ [WORKER_KIND_ID.SHAPES_GENERATE]: { lane: "build", priority: 100, late: "sync" },
184
+ [WORKER_KIND_ID.WORLD_REGION]: { lane: "background", priority: 10, latencyFrames: 10, late: "drop" }
185
+ };
186
+
187
+ // ../game-runtime/dist/index.js
188
+ var DEFAULT_MIN_EVERY_FRAMES = 4;
189
+ function createQueueEntry(queue, options) {
190
+ return {
191
+ queue,
192
+ phase: options.phase ?? "idle",
193
+ priority: options.priority,
194
+ minEveryFrames: options.minEveryFrames ?? DEFAULT_MIN_EVERY_FRAMES,
195
+ reserveMs: options.reserveMs ?? 0,
196
+ mustDrain: options.mustDrain === true,
197
+ reserveLeft: 0,
198
+ waitingFrames: 0,
199
+ maxWaitingFrames: 0,
200
+ ranUnits: 0,
201
+ spentMs: 0,
202
+ lastSpentMs: 0,
203
+ forced: 0,
204
+ lastPending: 0
205
+ };
206
+ }
207
+ function insertByPriority(entries, entry) {
208
+ let index = entries.length;
209
+ while (index > 0 && entries[index - 1].priority < entry.priority) index--;
210
+ entries.splice(index, 0, entry);
211
+ }
212
+ function queueStats(entry) {
213
+ return {
214
+ id: entry.queue.id,
215
+ pending: entry.lastPending,
216
+ ranUnits: entry.ranUnits,
217
+ spentMs: entry.spentMs,
218
+ lastSpentMs: entry.lastSpentMs,
219
+ mustDrain: entry.mustDrain,
220
+ framesWaiting: entry.waitingFrames,
221
+ maxFramesWaiting: entry.maxWaitingFrames,
222
+ forced: entry.forced
223
+ };
224
+ }
225
+ var MICROS_PER_SECOND = 1e6;
226
+ var toMicros = (seconds) => Math.round(seconds * MICROS_PER_SECOND);
227
+ function defaultClock() {
228
+ if (typeof performance !== "undefined" && typeof performance.now === "function") return () => performance.now();
229
+ return () => Date.now();
230
+ }
231
+ var RENDER_ESTIMATE_WEIGHT = 0.125;
232
+ function createRenderEstimate() {
233
+ let value = 0;
234
+ let seeded = false;
235
+ return {
236
+ get value() {
237
+ return value;
238
+ },
239
+ add(sample) {
240
+ value = seeded ? value + (sample - value) * RENDER_ESTIMATE_WEIGHT : sample;
241
+ seeded = true;
242
+ }
243
+ };
244
+ }
245
+ var DEFAULT_MS = 4;
246
+ var DEFAULT_MIN_MS = 1;
247
+ var MAX_RESERVE_SHARE = 0.5;
248
+ function createFrameBudget(options = {}) {
249
+ const clock = options.clock ?? defaultClock();
250
+ const entries = [];
251
+ const reportedQueues = /* @__PURE__ */ new Set();
252
+ let frame = 0;
253
+ let frameOpen = false;
254
+ let debt = 0;
255
+ let common = 0;
256
+ let spentThisFrame = 0;
257
+ let mustDrainSpent = 0;
258
+ let unbounded = false;
259
+ let mode = "quota";
260
+ let allowance = 0;
261
+ let isAllowanceReady = false;
262
+ let frameStart = 0;
263
+ let frameTarget = 0;
264
+ let isRenderSkipped = false;
265
+ const forcedMs = [];
266
+ const minMs = options.minMs ?? DEFAULT_MIN_MS;
267
+ const mustDrainIds = options.mustDrainIds ?? MUST_DRAIN_QUEUE_IDS;
268
+ const renderEstimate = createRenderEstimate();
269
+ const reservedTotal = () => entries.reduce((sum, entry) => sum + entry.reserveMs, 0);
270
+ const carveReserves = (pool) => {
271
+ for (const entry of entries) {
272
+ entry.reserveLeft = 0;
273
+ if (entry.reserveMs <= 0 || entry.mustDrain || entry.queue.pending(frame) <= 0) continue;
274
+ entry.reserveLeft = Math.min(entry.reserveMs, pool);
275
+ pool -= entry.reserveLeft;
276
+ }
277
+ return pool;
278
+ };
279
+ const beginFrame = (frameStartMs) => {
280
+ frame++;
281
+ frameOpen = true;
282
+ spentThisFrame = 0;
283
+ mustDrainSpent = 0;
284
+ forcedMs.length = 0;
285
+ isRenderSkipped = false;
286
+ for (const entry of entries) entry.lastSpentMs = 0;
287
+ const target = budget.targetMs;
288
+ if (target !== void 0 && frameStartMs !== void 0 && !unbounded) {
289
+ const now = clock();
290
+ const lag = now - frameStartMs;
291
+ mode = "deadline";
292
+ frameTarget = target;
293
+ frameStart = lag >= 0 && lag <= target ? frameStartMs : now;
294
+ isAllowanceReady = false;
295
+ allowance = 0;
296
+ common = 0;
297
+ debt = 0;
298
+ return;
299
+ }
300
+ mode = "quota";
301
+ allowance = Math.max(0, budget.ms - debt);
302
+ common = Math.max(0, carveReserves(budget.ms) - debt);
303
+ debt = 0;
304
+ };
305
+ const ensureAllowance = () => {
306
+ if (isAllowanceReady) return;
307
+ isAllowanceReady = true;
308
+ const left = frameStart + frameTarget - renderEstimate.value - clock();
309
+ allowance = Math.max(minMs, Math.min(budget.ms, left));
310
+ common = carveReserves(allowance);
311
+ };
312
+ const refreshForIdle = () => {
313
+ for (const entry of entries) if (entry.phase === "present") entry.reserveLeft = 0;
314
+ const realLeft = frameStart + frameTarget - clock();
315
+ const total = Math.max(Math.min(allowance - spentThisFrame, realLeft), minMs - spentThisFrame);
316
+ common = Math.max(0, total - entries.reduce((sum, entry) => sum + entry.reserveLeft, 0));
317
+ };
318
+ const reportError = (error, entry) => {
319
+ if (options.onError) return options.onError(error, { id: entry.queue.id, phase: entry.phase });
320
+ if (reportedQueues.has(entry.queue.id)) return;
321
+ reportedQueues.add(entry.queue.id);
322
+ console.error(`game-runtime: queue ${entry.queue.id} threw`, error);
323
+ };
324
+ const runQueue = (entry, deadline) => {
325
+ try {
326
+ return entry.queue.run(deadline, clock);
327
+ } catch (error) {
328
+ reportError(error, entry);
329
+ return 0;
330
+ }
331
+ };
332
+ const runEntry = (entry, deadline) => {
333
+ const started = clock();
334
+ const ran = runQueue(entry, deadline);
335
+ const spent = Math.max(0, clock() - started);
336
+ entry.ranUnits += ran;
337
+ entry.spentMs += spent;
338
+ entry.lastSpentMs += spent;
339
+ entry.waitingFrames = 0;
340
+ if (entry.mustDrain) {
341
+ mustDrainSpent += spent;
342
+ return spent;
343
+ }
344
+ spentThisFrame += spent;
345
+ if (unbounded) return spent;
346
+ const fromReserve = Math.min(entry.reserveLeft, spent);
347
+ entry.reserveLeft -= fromReserve;
348
+ common -= spent - fromReserve;
349
+ if (common < 0) {
350
+ if (mode === "quota") debt = Math.min(budget.ms, debt - common);
351
+ common = 0;
352
+ }
353
+ return spent;
354
+ };
355
+ const drainMustDrain = (entry) => {
356
+ entry.lastPending = entry.queue.pending(frame);
357
+ if (entry.lastPending <= 0) {
358
+ entry.waitingFrames = 0;
359
+ return;
360
+ }
361
+ runEntry(entry, Infinity);
362
+ };
363
+ const drainEntry = (entry) => {
364
+ entry.lastPending = entry.queue.pending(frame);
365
+ if (entry.lastPending <= 0) {
366
+ entry.waitingFrames = 0;
367
+ return;
368
+ }
369
+ if (unbounded) {
370
+ runEntry(entry, Infinity);
371
+ return;
372
+ }
373
+ const share = entry.reserveLeft + common;
374
+ if (share > 0) {
375
+ runEntry(entry, clock() + share);
376
+ return;
377
+ }
378
+ entry.waitingFrames++;
379
+ entry.maxWaitingFrames = Math.max(entry.maxWaitingFrames, entry.waitingFrames);
380
+ if (entry.waitingFrames < entry.minEveryFrames) return;
381
+ entry.forced++;
382
+ forcedMs.push(runEntry(entry, clock()));
383
+ };
384
+ const budget = {
385
+ ms: options.ms ?? DEFAULT_MS,
386
+ register(queue, queueOptions) {
387
+ if (entries.some((candidate) => candidate.queue === queue)) throw new Error(`budget.register(${queue.id}): this queue is already registered`);
388
+ if (queueOptions.mustDrain === true && !mustDrainIds.has(queue.id)) {
389
+ throw new Error(`budget.register(${queue.id}): mustDrain is only for ${[...mustDrainIds].join(", ")} (CLAUDE.md \xA715: a new queue does not enter the mustDrain class without the owner's decision; a host passes its own list as the budget's mustDrainIds)`);
390
+ }
391
+ const reserveMs = queueOptions.reserveMs ?? 0;
392
+ if (reservedTotal() + reserveMs > budget.ms * MAX_RESERVE_SHARE) {
393
+ throw new Error(`budget.register(${queue.id}): the reserves would pass half of the ${budget.ms} ms budget`);
394
+ }
395
+ if (budget.targetMs !== void 0 && reservedTotal() + reserveMs > minMs) {
396
+ throw new Error(`budget.register(${queue.id}): with a deadline the reserves come out of the ${minMs} ms floor and would pass it`);
397
+ }
398
+ const entry = createQueueEntry(queue, queueOptions);
399
+ insertByPriority(entries, entry);
400
+ queue.attach?.();
401
+ return () => {
402
+ const at = entries.indexOf(entry);
403
+ if (at < 0) return;
404
+ entries.splice(at, 1);
405
+ queue.detach?.();
406
+ };
407
+ },
408
+ targetMs: options.targetMs,
409
+ get mode() {
410
+ return mode;
411
+ },
412
+ beginFrame,
413
+ recordRender(ms) {
414
+ if (budget.targetMs === void 0 || isRenderSkipped || !Number.isFinite(ms)) return;
415
+ renderEstimate.add(Math.min(Math.max(ms, 0), budget.targetMs));
416
+ },
417
+ skipRenderSample() {
418
+ isRenderSkipped = true;
419
+ },
420
+ drain(phase) {
421
+ if (phase === "present" && !frameOpen) beginFrame();
422
+ for (const entry of [...entries]) if (entry.mustDrain && entry.phase === phase) drainMustDrain(entry);
423
+ if (mode === "deadline" && !unbounded) {
424
+ ensureAllowance();
425
+ if (phase === "idle") refreshForIdle();
426
+ }
427
+ for (const entry of [...entries]) if (!entry.mustDrain && entry.phase === phase) drainEntry(entry);
428
+ if (phase === "idle") frameOpen = false;
429
+ },
430
+ flushAll() {
431
+ for (const entry of entries) entry.queue.flush();
432
+ },
433
+ get unbounded() {
434
+ return unbounded;
435
+ },
436
+ set unbounded(value) {
437
+ unbounded = value;
438
+ },
439
+ stats() {
440
+ return {
441
+ mode,
442
+ ms: budget.ms,
443
+ targetMs: mode === "deadline" ? frameTarget : void 0,
444
+ allowanceMs: allowance,
445
+ renderEstimateMs: renderEstimate.value,
446
+ spentMs: spentThisFrame,
447
+ mustDrainSpentMs: mustDrainSpent,
448
+ overMs: Math.max(0, spentThisFrame - Math.max(allowance, minMs)),
449
+ forcedThisFrame: forcedMs.length,
450
+ forcedMs: [...forcedMs],
451
+ debtMs: debt,
452
+ queues: entries.map(queueStats)
453
+ };
454
+ },
455
+ dispose() {
456
+ for (const entry of entries.splice(0)) entry.queue.detach?.();
457
+ }
458
+ };
459
+ return budget;
460
+ }
461
+ var NONE = [];
462
+ function createCommandBuffer(nextTick) {
463
+ let pending = [];
464
+ return {
465
+ push(kind, payload, tick) {
466
+ pending.push({ kind, payload, tick: tick ?? nextTick() });
467
+ },
468
+ take(tick) {
469
+ if (pending.length === 0) return NONE;
470
+ const due = [];
471
+ const later = [];
472
+ for (const command of pending) (command.tick <= tick ? due : later).push(command);
473
+ pending = later;
474
+ return due;
475
+ },
476
+ clear() {
477
+ pending = [];
478
+ }
479
+ };
480
+ }
481
+ var sanitizeSeconds = (seconds) => Number.isFinite(seconds) && seconds > 0 ? seconds : 0;
482
+ var sanitizeTimeScale = (scale) => Number.isFinite(scale) ? Math.max(0, scale) : 1;
483
+ function resolveFrameTime(realSeconds, exact, residual, config) {
484
+ const given = sanitizeSeconds(realSeconds);
485
+ if (exact) return { seconds: given, micros: toMicros(given), residual };
486
+ const clamped = Math.min(given, config.maxFrameSeconds);
487
+ const micros = toMicros(clamped) + residual;
488
+ const wholeSteps = Math.round(micros / config.fixedMicros);
489
+ const drift = micros - wholeSteps * config.fixedMicros;
490
+ if (config.snapMicros > 0 && wholeSteps >= 1 && Math.abs(drift) <= config.snapMicros) {
491
+ return { seconds: wholeSteps * config.fixedDt, micros: wholeSteps * config.fixedMicros, residual: drift };
492
+ }
493
+ return { seconds: clamped, micros, residual: 0 };
494
+ }
495
+ function createPhaseRunner(deps) {
496
+ const { clock, ctx, lists, reportError, fixedDt, probe } = deps;
497
+ const phaseMs = Object.fromEntries(FRAME_PHASES.map((phase) => [phase, 0]));
498
+ let tick = 0;
499
+ const runEntries = (phase) => {
500
+ for (const entry of lists[phase].entries) {
501
+ if (entry.dead) continue;
502
+ const watcher = probe();
503
+ watcher?.enter(entry.id);
504
+ try {
505
+ entry.fn(ctx);
506
+ } catch (error) {
507
+ reportError(error, { id: entry.id, phase });
508
+ } finally {
509
+ watcher?.leave(entry.id);
510
+ }
511
+ }
512
+ };
513
+ return {
514
+ runPhase(phase, dt) {
515
+ const started = clock();
516
+ ctx.dt = dt;
517
+ runEntries(phase);
518
+ phaseMs[phase] = clock() - started;
519
+ },
520
+ runFixed(steps, isHeld) {
521
+ const started = clock();
522
+ ctx.dt = fixedDt;
523
+ let ran = 0;
524
+ for (let substep = 0; substep < steps; substep++) {
525
+ if (isHeld?.()) break;
526
+ ran++;
527
+ ctx.substep = substep;
528
+ ctx.tick = tick;
529
+ runEntries("fixedSimulate");
530
+ tick++;
531
+ }
532
+ ctx.substep = 0;
533
+ ctx.tick = tick;
534
+ phaseMs.fixedSimulate = clock() - started;
535
+ return ran;
536
+ },
537
+ get tick() {
538
+ return tick;
539
+ },
540
+ phaseMs
541
+ };
542
+ }
543
+ function createSystemList(isRunning) {
544
+ let entries = [];
545
+ const waiting = [];
546
+ const insert = (entry) => {
547
+ let index = entries.length;
548
+ while (index > 0 && entries[index - 1].order > entry.order) index--;
549
+ entries.splice(index, 0, entry);
550
+ };
551
+ return {
552
+ add(entry) {
553
+ if (isRunning()) waiting.push(entry);
554
+ else insert(entry);
555
+ },
556
+ remove(entry) {
557
+ entry.dead = true;
558
+ if (!isRunning()) entries = entries.filter((candidate) => candidate !== entry);
559
+ },
560
+ get entries() {
561
+ return entries;
562
+ },
563
+ settle() {
564
+ if (entries.some((entry) => entry.dead)) entries = entries.filter((entry) => !entry.dead);
565
+ for (const entry of waiting.splice(0)) if (!entry.dead) insert(entry);
566
+ },
567
+ clear() {
568
+ for (const entry of entries) entry.dead = true;
569
+ for (const entry of waiting.splice(0)) entry.dead = true;
570
+ entries = isRunning() ? entries : [];
571
+ }
572
+ };
573
+ }
574
+ function createScheduler(options = {}) {
575
+ const fixedDt = options.fixedDt ?? 1 / 60;
576
+ const fixedMicros = toMicros(fixedDt);
577
+ const maxSubsteps = options.maxSubsteps ?? 4;
578
+ const timeConfig = { fixedMicros, fixedDt, maxFrameSeconds: options.maxFrameSeconds ?? 0.25, snapMicros: Math.round((options.snapMs ?? 0.25) * 1e3) };
579
+ const clock = options.clock ?? defaultClock();
580
+ let accumulator = 0;
581
+ let residual = 0;
582
+ let droppedMicros = 0;
583
+ let heldMicros = 0;
584
+ let substepsLastFrame = 0;
585
+ let running = false;
586
+ let disposed = false;
587
+ let paused = false;
588
+ let scripted = false;
589
+ let timeScale = 1;
590
+ let systemProbe;
591
+ let nextSystem = 0;
592
+ const reportedErrors = /* @__PURE__ */ new Set();
593
+ const lists = Object.fromEntries(FRAME_PHASES.map((phase) => [phase, createSystemList(() => running)]));
594
+ const ctx = { dt: 0, realDt: 0, fixedDt, alpha: 0, frame: 0, tick: 0, substep: 0, simTime: 0, paused: false, scripted: false, simulated: true, fixedFrozen: false };
595
+ const reportError = options.onError ?? ((error, system) => {
596
+ if (reportedErrors.has(system.id)) return;
597
+ reportedErrors.add(system.id);
598
+ console.error(`game-runtime: system ${system.id} (${system.phase}) threw`, error);
599
+ });
600
+ const budget = createFrameBudget({ ms: options.budgetMs ?? 4, targetMs: options.targetMs, minMs: options.minBudgetMs, mustDrainIds: options.mustDrainIds, clock, onError: (error, queue) => reportError(error, { id: `queue:${queue.id}`, phase: queue.phase }) });
601
+ const runner = createPhaseRunner({ clock, ctx, lists, reportError, fixedDt, probe: () => systemProbe });
602
+ const { runPhase, runFixed } = runner;
603
+ const settleAll = () => {
604
+ for (const phase of FRAME_PHASES) lists[phase].settle();
605
+ };
606
+ const begin = (frameStartMs) => {
607
+ running = true;
608
+ budget.beginFrame(frameStartMs);
609
+ ctx.frame++;
610
+ ctx.paused = paused;
611
+ ctx.scripted = scripted;
612
+ ctx.tick = runner.tick;
613
+ };
614
+ const end = () => {
615
+ running = false;
616
+ settleAll();
617
+ };
618
+ const simulate = (seconds, micros, exact) => {
619
+ runPhase("input", seconds);
620
+ if (paused) return;
621
+ if (ctx.fixedFrozen) {
622
+ heldMicros += Math.round(micros * timeScale);
623
+ ctx.simTime += seconds * timeScale;
624
+ runPhase("lateSimulate", seconds * timeScale);
625
+ return;
626
+ }
627
+ accumulator += Math.round(micros * timeScale);
628
+ let steps = Math.floor(accumulator / fixedMicros);
629
+ if (!exact && steps > maxSubsteps) {
630
+ droppedMicros += accumulator - maxSubsteps * fixedMicros;
631
+ steps = maxSubsteps;
632
+ accumulator = maxSubsteps * fixedMicros;
633
+ }
634
+ accumulator -= steps * fixedMicros;
635
+ substepsLastFrame = runFixed(steps, options.holdFixed);
636
+ if (substepsLastFrame < steps) {
637
+ heldMicros += (steps - substepsLastFrame) * fixedMicros;
638
+ ctx.fixedFrozen = true;
639
+ }
640
+ ctx.alpha = accumulator / fixedMicros;
641
+ ctx.simTime += seconds * timeScale;
642
+ runPhase("lateSimulate", seconds * timeScale);
643
+ };
644
+ const frame = (realSeconds, run = {}) => {
645
+ if (running) throw new Error("scheduler.frame: re-entrant call");
646
+ if (disposed) throw new Error("scheduler.frame: the scheduler was disposed");
647
+ const time = resolveFrameTime(realSeconds, run.exact === true, residual, timeConfig);
648
+ residual = time.residual;
649
+ const seconds = time.seconds;
650
+ begin(run.frameStartMs);
651
+ try {
652
+ ctx.simulated = run.simulate !== false;
653
+ ctx.fixedFrozen = ctx.simulated && (run.freezeFixed === true || options.holdFixed?.() === true);
654
+ ctx.realDt = seconds;
655
+ ctx.alpha = accumulator / fixedMicros;
656
+ substepsLastFrame = 0;
657
+ if (ctx.simulated) simulate(seconds, time.micros, run.exact === true);
658
+ ctx.alpha = accumulator / fixedMicros;
659
+ runPhase("present", paused ? 0 : seconds * timeScale);
660
+ budget.drain("present");
661
+ if (run.render !== false) {
662
+ runPhase("render", seconds);
663
+ budget.recordRender(runner.phaseMs.render);
664
+ }
665
+ runPhase("idle", seconds);
666
+ budget.drain("idle");
667
+ } finally {
668
+ ctx.simulated = true;
669
+ ctx.fixedFrozen = false;
670
+ end();
671
+ }
672
+ };
673
+ const advanceStep = (dt, steps) => {
674
+ begin();
675
+ try {
676
+ ctx.simulated = true;
677
+ ctx.realDt = dt;
678
+ ctx.alpha = 0;
679
+ runPhase("input", dt);
680
+ if (!paused) {
681
+ runFixed(steps);
682
+ ctx.simTime += dt;
683
+ runPhase("lateSimulate", dt);
684
+ }
685
+ runPhase("present", paused ? 0 : dt);
686
+ budget.drain("present");
687
+ runPhase("idle", dt);
688
+ budget.drain("idle");
689
+ } finally {
690
+ end();
691
+ }
692
+ };
693
+ const advance = (seconds, advanceOptions = {}) => {
694
+ if (running) throw new Error("scheduler.advance: re-entrant call");
695
+ if (disposed) throw new Error("scheduler.advance: the scheduler was disposed");
696
+ const dt = sanitizeSeconds(advanceOptions.dt ?? fixedDt) || fixedDt;
697
+ const iterations = Math.max(1, Math.round(sanitizeSeconds(seconds) / dt));
698
+ const steps = Math.max(1, Math.round(dt / fixedDt));
699
+ const wasUnbounded = budget.unbounded;
700
+ budget.unbounded = true;
701
+ scripted = true;
702
+ accumulator = 0;
703
+ residual = 0;
704
+ scheduler.commands.clear();
705
+ try {
706
+ for (let i = 0; i < iterations; i++) advanceStep(dt, steps);
707
+ } finally {
708
+ scripted = false;
709
+ ctx.scripted = false;
710
+ budget.unbounded = wasUnbounded;
711
+ accumulator = 0;
712
+ ctx.alpha = 0;
713
+ }
714
+ };
715
+ const scheduler = {
716
+ add(system, systemOptions) {
717
+ if (disposed) return () => void 0;
718
+ const entry = {
719
+ id: systemOptions.id ?? `${systemOptions.phase}#${nextSystem++}`,
720
+ phase: systemOptions.phase,
721
+ fn: system,
722
+ order: systemOptions.order ?? 0,
723
+ dead: false
724
+ };
725
+ lists[entry.phase].add(entry);
726
+ return () => lists[entry.phase].remove(entry);
727
+ },
728
+ frame,
729
+ advance,
730
+ pause() {
731
+ paused = true;
732
+ },
733
+ resume() {
734
+ paused = false;
735
+ },
736
+ get paused() {
737
+ return paused;
738
+ },
739
+ get timeScale() {
740
+ return timeScale;
741
+ },
742
+ set timeScale(value) {
743
+ timeScale = sanitizeTimeScale(value);
744
+ },
745
+ resetClock() {
746
+ accumulator = 0;
747
+ residual = 0;
748
+ },
749
+ get systemProbe() {
750
+ return systemProbe;
751
+ },
752
+ set systemProbe(value) {
753
+ systemProbe = value;
754
+ },
755
+ budget,
756
+ commands: createCommandBuffer(() => runner.tick),
757
+ stats() {
758
+ return { frame: ctx.frame, tick: runner.tick, substepsLastFrame, droppedSeconds: droppedMicros / MICROS_PER_SECOND, heldSeconds: heldMicros / MICROS_PER_SECOND, phaseMs: { ...runner.phaseMs }, budget: budget.stats() };
759
+ },
760
+ dispose() {
761
+ disposed = true;
762
+ budget.dispose();
763
+ for (const phase of FRAME_PHASES) lists[phase].clear();
764
+ settleAll();
765
+ }
766
+ };
767
+ return scheduler;
768
+ }
146
769
 
147
770
  // ../game-vehicles-driving/dist/index.js
148
771
  var defaultFuelModel = { idleRate: 3e-4, fullRate: 35e-4, loadFactor: 0.5, damagedEngineExtra: 0.4 };
@@ -556,6 +1179,7 @@ var GameVehiclesProving = (() => {
556
1179
 
557
1180
  // src/run.ts
558
1181
  var subjectSpec = (subject) => ({ ...semiSpecFromStats(subject.stats, subject.body), ...subject.spec });
1182
+ var STEPS_PER_TICK = 2;
559
1183
  function startRun(test, subject, options = {}) {
560
1184
  const procedure = test.procedure;
561
1185
  if (test.kind !== "measured" || !procedure) throw new Error(`startRun: ${test.id} is not a measured test`);
@@ -571,7 +1195,7 @@ var GameVehiclesProving = (() => {
571
1195
  let time = 0;
572
1196
  let done = false;
573
1197
  let nextSample = 0;
574
- let carry = 0;
1198
+ let scheduler;
575
1199
  let last = { throttle: 0, brake: 0, steer: 0, handbrake: false };
576
1200
  const tick = (h) => {
577
1201
  const input = procedure.input(driver.state, time, memo);
@@ -602,12 +1226,16 @@ var GameVehiclesProving = (() => {
602
1226
  get input() {
603
1227
  return last;
604
1228
  },
605
- step(dt) {
606
- carry += dt;
607
- while (!done && carry >= fixed) {
608
- carry -= fixed;
609
- tick(fixed);
1229
+ fixedStep: fixed,
1230
+ stepFixed(steps) {
1231
+ for (let i = 0; i < steps && !done; i++) tick(fixed);
1232
+ },
1233
+ advance(seconds) {
1234
+ if (!scheduler) {
1235
+ scheduler = createScheduler({ fixedDt: fixed * STEPS_PER_TICK });
1236
+ scheduler.add(() => this.stepFixed(STEPS_PER_TICK), { phase: "fixedSimulate", id: "proving.run" });
610
1237
  }
1238
+ scheduler.frame(seconds, { exact: true, render: false });
611
1239
  },
612
1240
  result() {
613
1241
  const range = test.range?.(subject) ?? {};
@@ -623,8 +1251,7 @@ var GameVehiclesProving = (() => {
623
1251
  }
624
1252
  function runTest(test, subject, options = {}) {
625
1253
  const run = startRun(test, subject, options);
626
- const fixed = options.fixedStep ?? 1 / 120;
627
- while (!run.done) run.step(fixed * 60);
1254
+ while (!run.done) run.stepFixed(STEPS_PER_TICK);
628
1255
  return run.result();
629
1256
  }
630
1257