@adatechnology/game-player 0.13.5 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { a as PlayerCollider, b as PlayerController, c as PlayerMessageId, K as KnownDeathCause } from './player-D2QiWWBg.js';
2
2
  export { A as AttributeId, d as Attributes, B as BodyVitals, e as BodyVitalsOptions, f as BodyVitalsState, D as DeathCause, g as Derived, h as DerivedFormulas, i as DerivedId, I as InfectionRule, j as ItemEffect, k as ItemEffectResult, L as LinearFormula, M as MeleeItem, N as NEEDS_DEFAULTS, l as NeedsRules, m as NeedsState, n as Nourishment, o as PLAYER_DEFAULTS, P as Player, p as PlayerAction, q as PlayerCrowd, r as PlayerEvent, s as PlayerEventMap, t as PlayerEventName, u as PlayerInput, v as PlayerNumbers, w as PlayerOptions, x as PlayerSave, y as PlayerSoundId, z as PlayerState, C as PlayerWorld, V as VitalsBody, W as WoundRule, E as WoundTable, F as advanceNeeds, G as applyItemEffect, H as createBodyVitals, J as createNeedsState, O as createPlayer, Q as deriveVitals, R as drawWound, S as isStarving, T as needsHoursOf, U as nourish } from './player-D2QiWWBg.js';
3
3
  import { Vec3, CharacterId, PhysicsWorld, Layer, PhysicsServices } from '@adatechnology/game-physics';
4
- import { SurfaceId } from '@adatechnology/game-contracts';
4
+ import { SurfaceId, BodyGesture, Vec3 as Vec3$1, HandTargets } from '@adatechnology/game-contracts';
5
5
  export { PlayerTestModule, PlayerTestModuleOptions, createPlayerTestModule } from './testing.js';
6
6
 
7
7
  /** The part of the physics contract the player's body uses: any `PhysicsWorld` (or the loop's `SeamWorld`) has it. */
@@ -321,7 +321,18 @@ interface HandsRules {
321
321
  readonly maxKg: number;
322
322
  readonly maxLength: number;
323
323
  };
324
+ /**
325
+ * A thing with a carry handle (a bucket of water, a jerrycan, a toolbox) hangs from one hand by it up to `maxKg`, whatever its size. Optional, so rules written
326
+ * before it keep working: without it the default (`HANDS_BY_HANDLE`) holds.
327
+ */
328
+ readonly byHandle?: {
329
+ readonly maxKg: number;
330
+ };
324
331
  }
332
+ /** How much a hand carries by a handle when the rules say nothing of it: a 20 L jerrycan full, a bucket of water. */
333
+ declare const HANDS_BY_HANDLE: {
334
+ readonly maxKg: 25;
335
+ };
325
336
  declare const HANDS_DEFAULTS: HandsRules;
326
337
  /** The slot a weapon goes to when the body wears none of its own (or it is taken): a long gun with no sling goes across the back, a second handgun or knife into the belt. */
327
338
  declare const SLOT_FALLBACKS: Readonly<Record<string, readonly string[]>>;
@@ -359,10 +370,11 @@ interface PlanHandsParams {
359
370
  /** The slots tried when a weapon's own is not worn; default `SLOT_FALLBACKS`. */
360
371
  readonly fallbacks?: Readonly<Record<string, readonly string[]>>;
361
372
  }
362
- /** How many hands it takes to carry a thing of `massKg` and `size` (metres, any order). */
373
+ /** How many hands it takes to carry a thing of `massKg` and `size` (metres, any order); by its carry handle (`handle`), one up to `byHandle.maxKg`. */
363
374
  declare function interactionHandsFor(thing: {
364
375
  readonly massKg: number;
365
376
  readonly size?: readonly number[];
377
+ readonly handle?: boolean;
366
378
  }, rules?: HandsRules): 1 | 2;
367
379
  /** Which hand acts and what happens to each weapon in the way. */
368
380
  declare function planHands(params: PlanHandsParams): HandsPlan;
@@ -451,9 +463,16 @@ interface ItemGrip {
451
463
  readonly local: Pose;
452
464
  /** How closed the fingers are on it (`HandTargets.grip`). */
453
465
  readonly grip: number;
466
+ /** The hand holds it by a carry handle (a bucket's bail) and it hangs under the hand, upright: carried with the arm down at the side, not in front of the chest. */
467
+ readonly hanging?: boolean;
454
468
  }
455
- /** The grip of a hand on a thing of box `size` (its own frame, metres). */
456
- declare function gripOfItem(size: readonly number[], hand: BodyHand): ItemGrip;
469
+ /**
470
+ * The grip of a hand on the carry handle of a thing (a bucket's bail, a jerrycan's or a toolbox's handle), `handle` the middle of the handle in the thing's own frame (metres from its
471
+ * middle): the fist closes round the handle, the thing upright (its +y up, along the thumb) and hanging under the hand, its handle along the fingers.
472
+ */
473
+ declare function gripOnHandle(handle: readonly number[], hand: BodyHand): ItemGrip;
474
+ /** The grip of a hand on a thing of box `size` (its own frame, metres); by its carry handle when it has one (`gripOnHandle`). */
475
+ declare function gripOfItem(size: readonly number[], hand: BodyHand, handle?: readonly number[] | null): ItemGrip;
457
476
  /** Where the thing is with the hand at `hand` holding it by `grip`. */
458
477
  declare function itemPoseInHand(hand: Pose, grip: ItemGrip): Pose;
459
478
  /** Where the hand goes to take the thing where it lies (`item`, its middle and turn) by `grip`. */
@@ -541,4 +560,146 @@ declare const STEP_POSE_DEFAULTS: {
541
560
  };
542
561
  declare function createStepPose(start: StepPosition, options?: StepPoseOptions): StepPose;
543
562
 
544
- export { BELT_SLOT, type BodyHand, DOOR_ARM_REACH, DOOR_SHOULDER_HEIGHT, DOOR_USE_DEFAULTS, type DoorUse, type DoorUseBlock, type DoorUseHand, type DoorUsePhase, type DoorUseRules, type DoorUseStep, type DoorUseTarget, type DragMark, FURNITURE_PUSH_DEFAULTS, type FurniturePush, type FurniturePushBlock, type FurniturePushInput, type FurniturePushLoad, type FurniturePushPhase, type FurniturePushRules, type FurniturePushStep, type FurniturePushTarget, HANDS_DEFAULTS, HANDS_TIMING, type HandWeapon, type HandsMove, type HandsMoveKind, type HandsPlan, type HandsRules, type HandsTiming, ITEM_GRIP, type ItemGrip, KnownDeathCause, LIFT_DEFAULTS, type LiftCheck, type LiftRules, type LiftVerdict, PLAYER_MASS_KG, PLAYER_PUSH_STRENGTH_NEWTONS, PLAYER_TEXT, PLAYER_WALK_PUSH_NEWTONS, type PhysicsControllerOptions, type PhysicsPlayerController, type PlanHandsParams, PlayerCollider, PlayerController, PlayerMessageId, type PlayerPhysicsWorld, type PlayerText, type Pose, type QuatTuple, SLING_SLOT, SLOT_FALLBACKS, STEP_POSE_DEFAULTS, type StepPose, type StepPoseOptions, type StepPosition, type TakeProgress, type TakeSequence, type TakeStep, type TakeStepKind, type TakeTimeline, type Vec3Tuple, WALKER_JUMP_SPEED, type Walker, type WalkerOptions, type WalkerServices, type WalkerStance, blendPoses, canPush, composePose, conjugateQuaternion, createDoorUse, createFurniturePush, createPhysicsController, createRng, createStepPose, createTakeTimeline, createWalker, dragMarkFor, drawBackOf, easeInOut, footRestHeight, frictionFor, gripOfItem, gripSecondsFor, handPoseOnItem, interactionHandsFor, itemPoseInHand, liftCheckFor, multiplyQuaternions, planDrawSequence, planHands, planTakeSequence, playerTextOf, poseHolding, pushAccelerationFor, pushSpeedFor, quaternionOfBasis, rotateVector, slipsOnTop, swingSecondsFor };
563
+ /**
564
+ * The body works a control with one hand (CLAUDE.md §18, owner 2026-11-30: every action has a body animation): the free hand `reach`es the point, does the `act` of the gesture
565
+ * (presses a button, turns a knob, flips a rocker, strikes a match, pours, lets go inside, tips over, closes on the thing to stow it, holds the paper up to read) and goes `back`.
566
+ * The thing reacts at the act's moment (`GestureProgress.acted`), so the TV goes on when the finger is on the button. One gesture serves every control of its kind: a press is
567
+ * the TV, the microwave and the radio alike. Pure and clock-free: the host advances it by its own frame's `dt`, says where the shoulder, the hand at rest and the point are,
568
+ * and puts the hand where `gestureHandPose` says.
569
+ */
570
+
571
+ type GesturePhase = 'reach' | 'act' | 'back';
572
+ interface GestureTiming {
573
+ readonly reach: number;
574
+ readonly back: number;
575
+ /** Seconds the act of each gesture takes. */
576
+ readonly act: Readonly<Record<BodyGesture, number>>;
577
+ }
578
+ declare const GESTURE_TIMING: GestureTiming;
579
+ /** How far a hand reaches from its shoulder, metres: a point farther than that is touched at the end of the arm, never with the arm stretched past it. */
580
+ declare const GESTURE_ARM_REACH = 0.62;
581
+ interface GestureProgress {
582
+ readonly phase: GesturePhase;
583
+ /** How far into the phase, 0..1, eased. */
584
+ readonly progress: number;
585
+ /** True on the advance during which the hand got to the act's moment: the host works the thing now. */
586
+ readonly acted: boolean;
587
+ readonly done: boolean;
588
+ }
589
+ interface GestureTimeline {
590
+ readonly gesture: BodyGesture;
591
+ readonly duration: number;
592
+ /** Moves it on by `dt` seconds (the host's frame). */
593
+ advance(dt: number): GestureProgress;
594
+ }
595
+ declare function createGestureTimeline(gesture: BodyGesture, timing?: GestureTiming): GestureTimeline;
596
+ /** The free hand: the weapon hand only when it holds nothing, else the off hand; null when both hold something (a pistol in one, a torch in the other). */
597
+ declare function freeHandOf(busy: {
598
+ readonly R: boolean;
599
+ readonly L: boolean;
600
+ }): BodyHand | null;
601
+ interface GestureHandChoice {
602
+ /** Whether each hand holds something (a weapon, a carried thing). */
603
+ readonly busy: {
604
+ readonly R: boolean;
605
+ readonly L: boolean;
606
+ };
607
+ readonly gesture: BodyGesture;
608
+ /** The hand that carries the thing a `put_in` or a `pour` is done with, null when none does. */
609
+ readonly carrying?: BodyHand | null;
610
+ }
611
+ /** The hand that works a control: the one that carries the thing for a gesture done with it, the free one for the rest; null when no hand can (the hands are full). */
612
+ declare function gestureHandOf(choice: GestureHandChoice): BodyHand | null;
613
+ interface GestureHandParams {
614
+ readonly gesture: BodyGesture;
615
+ readonly phase: GesturePhase;
616
+ readonly progress: number;
617
+ /** The hand that works (`gestureHandOf`): a left hand is turned the other way round, its thumb on the other side. */
618
+ readonly hand: BodyHand;
619
+ /** The shoulder of the hand that works, world space. */
620
+ readonly shoulder: Vec3Tuple;
621
+ /** Where the hand is when it does nothing of this (by the chest, or on the weapon's forend). */
622
+ readonly rest: Pose;
623
+ /** The point the hand works, world space. */
624
+ readonly at: Vec3Tuple;
625
+ /** The body's facing about the vertical (radians, 0 looks down -z): the way the hand goes when the point is right over the shoulder. */
626
+ readonly yaw: number;
627
+ }
628
+ interface GestureHand {
629
+ readonly pose: Pose;
630
+ /** How closed the fingers are, 0 open to 1 closed. */
631
+ readonly grip: number;
632
+ /** The index finger alone, 0 straight (a finger that presses) to 1 curled. */
633
+ readonly index: number;
634
+ }
635
+ /** The hand of a gesture now: its pose (world space, HandPose of game-contracts: +x to the knuckles, +z to the little finger) and its fingers. */
636
+ declare function gestureHandPose(params: GestureHandParams): GestureHand;
637
+
638
+ /**
639
+ * The body working a control (CLAUDE.md §18): one gesture at a time, by the hand the gesture calls for (`gestureHandOf`: the free one for a button, the one that holds the
640
+ * thing to put in or pour), played over whatever the hands do already (the weapon's targets, a carried thing); the body steps up to the point, stoops for a low one, and the
641
+ * thing is worked when the hand gets there. Any host with a body plays it (the test room, a game): it says where the shoulders, the hands at rest and the feet are, and puts
642
+ * the hands where `targets` says. The timing and the pose are `createGestureTimeline` and `gestureHandPose`.
643
+ */
644
+
645
+ interface GestureBody {
646
+ /** Where the body's middle stands, metres on the floor (x, z). */
647
+ at(): readonly [number, number];
648
+ /** Steps by (dx, dz) metres, stopped by what is in the way. */
649
+ moveBy(dx: number, dz: number): void;
650
+ /** Turns the head and the body a share `k` (0..1) of the way toward the point. */
651
+ face(point: Vec3$1, k: number): void;
652
+ /** Stoops while `on`: the hand reaches something lower than the arm reaches standing (a stove's knob). */
653
+ stoop?(on: boolean): void;
654
+ /** The body is on its feet and can work a thing (false once it is knocked down or falls): a gesture going on stops without working the thing. */
655
+ able?(): boolean;
656
+ }
657
+ interface GestureHandDeps {
658
+ /** The shoulder of a hand, world space; null for a body without one. */
659
+ shoulder(hand: BodyHand): Vec3$1 | null;
660
+ /** Where a hand rests when the frame's targets have nothing for it (by the chest), world space. */
661
+ restOf(hand: BodyHand): Pose;
662
+ /** Whether each hand holds something (a weapon, a carried thing). */
663
+ holding(): {
664
+ readonly R: boolean;
665
+ readonly L: boolean;
666
+ };
667
+ /** The hand that carries the thing a `put_in` puts in or a `pour` pours (not a weapon), or null when no hand carries one. */
668
+ carrying?(): BodyHand | null;
669
+ /** The body's facing about the vertical, radians. */
670
+ yaw(): number;
671
+ /** The body on the floor, for the steps up to a point out of the arm's reach; without it the hand goes from where the body stands. */
672
+ readonly body?: GestureBody;
673
+ }
674
+ interface GestureStart {
675
+ readonly gesture: BodyGesture;
676
+ /** The point the hand works, world space, asked every frame (a thing that moves is followed). */
677
+ at(): Vec3$1;
678
+ /** The hand got there: the thing is worked now. */
679
+ onAct(): void;
680
+ }
681
+ /** Why a gesture did not start: another one goes on, no hand can do it (both hold something, or none holds the thing to put in), or the body has no arm for it. */
682
+ type GestureRefusal = 'busy' | 'hands_full' | 'no_body';
683
+ type GestureStartResult = {
684
+ readonly ok: true;
685
+ readonly hand: BodyHand;
686
+ } | {
687
+ readonly ok: false;
688
+ readonly reason: GestureRefusal;
689
+ };
690
+ interface GestureHands {
691
+ /** A gesture is going on. */
692
+ readonly busy: boolean;
693
+ /** The hand of the gesture going on. */
694
+ readonly hand: BodyHand | null;
695
+ /** Starts a gesture; nothing is done when it is refused (the reason says why). */
696
+ start(start: GestureStart): GestureStartResult;
697
+ step(dt: number): void;
698
+ /** The frame's targets with the working hand put on the gesture; the other hand keeps what it had. */
699
+ targets(base: HandTargets | null): HandTargets | null;
700
+ /** Stops the gesture going on, without working the thing. */
701
+ cancel(): void;
702
+ }
703
+ declare function createGestureHands(deps: GestureHandDeps): GestureHands;
704
+
705
+ export { BELT_SLOT, type BodyHand, DOOR_ARM_REACH, DOOR_SHOULDER_HEIGHT, DOOR_USE_DEFAULTS, type DoorUse, type DoorUseBlock, type DoorUseHand, type DoorUsePhase, type DoorUseRules, type DoorUseStep, type DoorUseTarget, type DragMark, FURNITURE_PUSH_DEFAULTS, type FurniturePush, type FurniturePushBlock, type FurniturePushInput, type FurniturePushLoad, type FurniturePushPhase, type FurniturePushRules, type FurniturePushStep, type FurniturePushTarget, GESTURE_ARM_REACH, GESTURE_TIMING, type GestureBody, type GestureHand, type GestureHandChoice, type GestureHandDeps, type GestureHandParams, type GestureHands, type GesturePhase, type GestureProgress, type GestureRefusal, type GestureStart, type GestureStartResult, type GestureTimeline, type GestureTiming, HANDS_BY_HANDLE, HANDS_DEFAULTS, HANDS_TIMING, type HandWeapon, type HandsMove, type HandsMoveKind, type HandsPlan, type HandsRules, type HandsTiming, ITEM_GRIP, type ItemGrip, KnownDeathCause, LIFT_DEFAULTS, type LiftCheck, type LiftRules, type LiftVerdict, PLAYER_MASS_KG, PLAYER_PUSH_STRENGTH_NEWTONS, PLAYER_TEXT, PLAYER_WALK_PUSH_NEWTONS, type PhysicsControllerOptions, type PhysicsPlayerController, type PlanHandsParams, PlayerCollider, PlayerController, PlayerMessageId, type PlayerPhysicsWorld, type PlayerText, type Pose, type QuatTuple, SLING_SLOT, SLOT_FALLBACKS, STEP_POSE_DEFAULTS, type StepPose, type StepPoseOptions, type StepPosition, type TakeProgress, type TakeSequence, type TakeStep, type TakeStepKind, type TakeTimeline, type Vec3Tuple, WALKER_JUMP_SPEED, type Walker, type WalkerOptions, type WalkerServices, type WalkerStance, blendPoses, canPush, composePose, conjugateQuaternion, createDoorUse, createFurniturePush, createGestureHands, createGestureTimeline, createPhysicsController, createRng, createStepPose, createTakeTimeline, createWalker, dragMarkFor, drawBackOf, easeInOut, footRestHeight, freeHandOf, frictionFor, gestureHandOf, gestureHandPose, gripOfItem, gripOnHandle, gripSecondsFor, handPoseOnItem, interactionHandsFor, itemPoseInHand, liftCheckFor, multiplyQuaternions, planDrawSequence, planHands, planTakeSequence, playerTextOf, poseHolding, pushAccelerationFor, pushSpeedFor, quaternionOfBasis, rotateVector, slipsOnTop, swingSecondsFor };
@@ -26,6 +26,9 @@ var GamePlayer = (() => {
26
26
  DOOR_SHOULDER_HEIGHT: () => DOOR_SHOULDER_HEIGHT,
27
27
  DOOR_USE_DEFAULTS: () => DOOR_USE_DEFAULTS,
28
28
  FURNITURE_PUSH_DEFAULTS: () => FURNITURE_PUSH_DEFAULTS,
29
+ GESTURE_ARM_REACH: () => GESTURE_ARM_REACH,
30
+ GESTURE_TIMING: () => GESTURE_TIMING,
31
+ HANDS_BY_HANDLE: () => HANDS_BY_HANDLE,
29
32
  HANDS_DEFAULTS: () => HANDS_DEFAULTS,
30
33
  HANDS_TIMING: () => HANDS_TIMING,
31
34
  ITEM_GRIP: () => ITEM_GRIP,
@@ -49,6 +52,8 @@ var GamePlayer = (() => {
49
52
  createBodyVitals: () => createBodyVitals,
50
53
  createDoorUse: () => createDoorUse,
51
54
  createFurniturePush: () => createFurniturePush,
55
+ createGestureHands: () => createGestureHands,
56
+ createGestureTimeline: () => createGestureTimeline,
52
57
  createNeedsState: () => createNeedsState,
53
58
  createPhysicsController: () => createPhysicsController,
54
59
  createPlayer: () => createPlayer,
@@ -63,8 +68,12 @@ var GamePlayer = (() => {
63
68
  drawWound: () => drawWound,
64
69
  easeInOut: () => easeInOut,
65
70
  footRestHeight: () => footRestHeight,
71
+ freeHandOf: () => freeHandOf,
66
72
  frictionFor: () => frictionFor,
73
+ gestureHandOf: () => gestureHandOf,
74
+ gestureHandPose: () => gestureHandPose,
67
75
  gripOfItem: () => gripOfItem,
76
+ gripOnHandle: () => gripOnHandle,
68
77
  gripSecondsFor: () => gripSecondsFor,
69
78
  handPoseOnItem: () => handPoseOnItem,
70
79
  interactionHandsFor: () => interactionHandsFor,
@@ -1878,11 +1887,13 @@ var GamePlayer = (() => {
1878
1887
  }
1879
1888
 
1880
1889
  // src/hands.ts
1881
- var HANDS_DEFAULTS = { oneHand: { maxKg: 4, maxLength: 0.7 } };
1890
+ var HANDS_BY_HANDLE = { maxKg: 25 };
1891
+ var HANDS_DEFAULTS = { oneHand: { maxKg: 4, maxLength: 0.7 }, byHandle: HANDS_BY_HANDLE };
1882
1892
  var SLOT_FALLBACKS = { sling: ["back"], hip: ["belt"] };
1883
1893
  var BELT_SLOT = "belt";
1884
1894
  var SLING_SLOT = "sling";
1885
1895
  function interactionHandsFor(thing, rules = HANDS_DEFAULTS) {
1896
+ if (thing.handle) return thing.massKg > (rules.byHandle ?? HANDS_BY_HANDLE).maxKg ? 2 : 1;
1886
1897
  const longest = Math.max(0, ...thing.size ?? []);
1887
1898
  return thing.massKg > rules.oneHand.maxKg || longest > rules.oneHand.maxLength ? 2 : 1;
1888
1899
  }
@@ -2004,7 +2015,16 @@ var GamePlayer = (() => {
2004
2015
  ];
2005
2016
  var cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
2006
2017
  var scaled = (v, k) => [v[0] * k, v[1] * k, v[2] * k];
2007
- function gripOfItem(size, hand) {
2018
+ function gripOnHandle(handle, hand) {
2019
+ const up = hand === "R" ? [0, 0, -1] : [0, 0, 1];
2020
+ const along = [1, 0, 0];
2021
+ const quaternion = quaternionOfBasis(along, up, cross(along, up));
2022
+ const local = { position: [0, 0, 0], quaternion };
2023
+ const handleInHand = composePose(local, { position: [handle[0] ?? 0, handle[1] ?? 0, handle[2] ?? 0], quaternion: [0, 0, 0, 1] }).position;
2024
+ return { local: { position: [-handleInHand[0], -handleInHand[1] - ITEM_GRIP.palm, -handleInHand[2]], quaternion }, grip: ITEM_GRIP.closed, hanging: true };
2025
+ }
2026
+ function gripOfItem(size, hand, handle) {
2027
+ if (handle) return gripOnHandle(handle, hand);
2008
2028
  const sides = [0, 1, 2].map((axis) => ({ axis, length: Math.max(0, size[axis] ?? 0) })).sort((a, b) => b.length - a.length);
2009
2029
  const [long, mid, thin] = sides;
2010
2030
  const thumb = hand === "R" ? [0, 0, -1] : [0, 0, 1];
@@ -2100,6 +2120,188 @@ var GamePlayer = (() => {
2100
2120
  }
2101
2121
  };
2102
2122
  }
2123
+
2124
+ // src/gesture.ts
2125
+ var GESTURE_TIMING = {
2126
+ reach: 0.32,
2127
+ back: 0.3,
2128
+ act: { press: 0.22, turn: 0.4, flip: 0.2, strike: 0.35, pour: 0.7, put_in: 0.3, tip: 0.7, stow: 0.25, read: 1.2, pull: 0.45 }
2129
+ };
2130
+ var ACTS_AT_START = /* @__PURE__ */ new Set(["read"]);
2131
+ var GESTURE_ARM_REACH = 0.62;
2132
+ function createGestureTimeline(gesture, timing = GESTURE_TIMING) {
2133
+ const reach = timing.reach;
2134
+ const act = timing.act[gesture];
2135
+ const duration = reach + act + timing.back;
2136
+ const actsAt = ACTS_AT_START.has(gesture) ? reach : reach + act;
2137
+ let time = 0;
2138
+ let fired = false;
2139
+ return {
2140
+ gesture,
2141
+ duration,
2142
+ advance(dt) {
2143
+ time = Math.min(duration, time + Math.max(0, dt));
2144
+ const acted = !fired && time >= actsAt;
2145
+ if (acted) fired = true;
2146
+ const phase = time < reach ? "reach" : time < reach + act ? "act" : "back";
2147
+ const start = phase === "reach" ? 0 : phase === "act" ? reach : reach + act;
2148
+ const length = phase === "reach" ? reach : phase === "act" ? act : timing.back;
2149
+ return { phase, progress: easeInOut((time - start) / Math.max(1e-6, length)), acted, done: time >= duration };
2150
+ }
2151
+ };
2152
+ }
2153
+ var WITH_THE_HELD_THING = /* @__PURE__ */ new Set(["put_in", "pour"]);
2154
+ function freeHandOf(busy) {
2155
+ if (!busy.R) return "R";
2156
+ return busy.L ? null : "L";
2157
+ }
2158
+ function gestureHandOf(choice) {
2159
+ if (WITH_THE_HELD_THING.has(choice.gesture)) return choice.carrying ?? null;
2160
+ return freeHandOf(choice.busy);
2161
+ }
2162
+ var add = (a, b, k = 1) => [a[0] + b[0] * k, a[1] + b[1] * k, a[2] + b[2] * k];
2163
+ var cross2 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
2164
+ var axisTurn = (axis, angle) => {
2165
+ const s = Math.sin(angle / 2);
2166
+ return [axis[0] * s, axis[1] * s, axis[2] * s, Math.cos(angle / 2)];
2167
+ };
2168
+ var UP = [0, 1, 0];
2169
+ var actPose = (gesture, frame, k) => {
2170
+ const { at, way, side, q } = frame;
2171
+ switch (gesture) {
2172
+ case "press":
2173
+ return { position: add(at, way, -0.06 + 0.05 * Math.sin(Math.PI * k)), quaternion: q };
2174
+ case "turn":
2175
+ return { position: add(at, way, -0.02), quaternion: multiplyQuaternions(axisTurn(way, -k * Math.PI * 0.5), q) };
2176
+ case "flip":
2177
+ return { position: add(add(at, way, -0.02), UP, -0.03 * k), quaternion: q };
2178
+ case "strike":
2179
+ return { position: add(add(at, way, -0.04), side, 0.08 * (k - 0.5)), quaternion: q };
2180
+ case "pour":
2181
+ return { position: add(at, UP, 0.15), quaternion: multiplyQuaternions(axisTurn(side, -k * Math.PI * 0.4), q) };
2182
+ case "tip":
2183
+ return { position: add(at, UP, 0.05 * k), quaternion: multiplyQuaternions(axisTurn(side, -k * Math.PI * 0.55), q) };
2184
+ case "pull":
2185
+ return { position: add(at, way, -0.02 - 0.15 * k), quaternion: q };
2186
+ case "put_in":
2187
+ case "stow":
2188
+ return { position: [at[0], at[1], at[2]], quaternion: q };
2189
+ case "read":
2190
+ return { position: [at[0], at[1], at[2]], quaternion: multiplyQuaternions(axisTurn(way, Math.PI * 0.5), q) };
2191
+ }
2192
+ };
2193
+ var GRIP = {
2194
+ press: [0.85, 0.85],
2195
+ turn: [0.9, 0.9],
2196
+ flip: [0.55, 0.55],
2197
+ strike: [0.95, 0.95],
2198
+ pour: [1, 1],
2199
+ tip: [1, 1],
2200
+ put_in: [1, 0.15],
2201
+ stow: [0.2, 0.95],
2202
+ read: [0.7, 0.7],
2203
+ pull: [0.9, 0.9]
2204
+ };
2205
+ var OPEN_HAND = 0.2;
2206
+ function frameOf(params) {
2207
+ const { shoulder } = params;
2208
+ const flatX = params.at[0] - shoulder[0];
2209
+ const flatZ = params.at[2] - shoulder[2];
2210
+ const flat = Math.hypot(flatX, flatZ);
2211
+ const way = flat < 1e-4 ? [-Math.sin(params.yaw), 0, -Math.cos(params.yaw)] : [flatX / flat, 0, flatZ / flat];
2212
+ const little = params.hand === "R" ? [0, -1, 0] : [0, 1, 0];
2213
+ const side = cross2(little, way);
2214
+ const toward = [params.at[0] - shoulder[0], params.at[1] - shoulder[1], params.at[2] - shoulder[2]];
2215
+ const reach = Math.hypot(toward[0], toward[1], toward[2]);
2216
+ const at = reach > GESTURE_ARM_REACH ? add(shoulder, toward, GESTURE_ARM_REACH / reach) : params.at;
2217
+ return { at, way, side, q: quaternionOfBasis(way, side, little) };
2218
+ }
2219
+ function gestureHandPose(params) {
2220
+ const { rest, phase, progress, gesture } = params;
2221
+ const frame = frameOf(params);
2222
+ const [gripIn, gripOut] = GRIP[gesture];
2223
+ const index = gesture === "press" ? 0 : gesture === "flip" ? 0.2 : 1;
2224
+ if (phase === "reach") return { pose: blendPoses(rest, actPose(gesture, frame, 0), progress), grip: OPEN_HAND + (gripIn - OPEN_HAND) * progress, index: 1 + (index - 1) * progress };
2225
+ if (phase === "act") return { pose: actPose(gesture, frame, progress), grip: gripIn + (gripOut - gripIn) * progress, index };
2226
+ const stowed = gesture === "stow";
2227
+ const home = stowed ? { position: add(rest.position, UP, -0.3), quaternion: rest.quaternion } : rest;
2228
+ const grip = stowed ? gripOut : gripOut + (OPEN_HAND - gripOut) * progress;
2229
+ return { pose: blendPoses(actPose(gesture, frame, 1), home, progress), grip, index: index + (1 - index) * progress };
2230
+ }
2231
+
2232
+ // src/gesture-hands.ts
2233
+ var STAND_OFF = 0.5;
2234
+ var STEP_SPEED = 2.5;
2235
+ var MAX_WALK_SECONDS = 1.5;
2236
+ var TURN_RATE = 8;
2237
+ var STOOP_BELOW = 0.5;
2238
+ function stepUp(body, now, dt) {
2239
+ if (!body) return true;
2240
+ const point = now.start.at();
2241
+ body.face(point, Math.min(1, dt * TURN_RATE));
2242
+ const [x, z] = body.at();
2243
+ const dx = point[0] - x;
2244
+ const dz = point[2] - z;
2245
+ const d = Math.hypot(dx, dz);
2246
+ if (d <= STAND_OFF + 0.03 || now.walked > MAX_WALK_SECONDS) return true;
2247
+ const go = Math.min(d - STAND_OFF, STEP_SPEED * dt);
2248
+ if (go < 1e-4) return false;
2249
+ body.moveBy(dx / d * go, dz / d * go);
2250
+ const [x1, z1] = body.at();
2251
+ now.walked += dt;
2252
+ return Math.hypot(x1 - x, z1 - z) < go * 0.2;
2253
+ }
2254
+ function createGestureHands(deps) {
2255
+ let going = null;
2256
+ const stop = () => {
2257
+ going = null;
2258
+ deps.body?.stoop?.(false);
2259
+ };
2260
+ return {
2261
+ get busy() {
2262
+ return going !== null;
2263
+ },
2264
+ get hand() {
2265
+ return going?.hand ?? null;
2266
+ },
2267
+ start(start) {
2268
+ if (going) return { ok: false, reason: "busy" };
2269
+ const hand = gestureHandOf({ busy: deps.holding(), gesture: start.gesture, carrying: deps.carrying?.() ?? null });
2270
+ if (!hand) return { ok: false, reason: "hands_full" };
2271
+ const shoulder = deps.shoulder(hand);
2272
+ if (!shoulder || deps.body?.able?.() === false) return { ok: false, reason: "no_body" };
2273
+ const timeline = createGestureTimeline(start.gesture);
2274
+ going = { start, hand, timeline, progress: timeline.advance(0), walked: 0, there: false };
2275
+ if (shoulder[1] - start.at()[1] > STOOP_BELOW) deps.body?.stoop?.(true);
2276
+ return { ok: true, hand };
2277
+ },
2278
+ step(dt) {
2279
+ if (!going) return;
2280
+ const now = going;
2281
+ if (deps.body?.able?.() === false) return stop();
2282
+ if (!now.there) {
2283
+ now.there = stepUp(deps.body, now, dt);
2284
+ if (!now.there) return;
2285
+ }
2286
+ now.progress = now.timeline.advance(dt);
2287
+ if (now.progress.acted) now.start.onAct();
2288
+ if (now.progress.done && going === now) stop();
2289
+ },
2290
+ targets(base) {
2291
+ if (!going) return base;
2292
+ const { hand, start, progress } = going;
2293
+ const shoulder = deps.shoulder(hand);
2294
+ if (!shoulder) return base;
2295
+ const rest = base?.[hand] ?? deps.restOf(hand);
2296
+ const worked = gestureHandPose({ gesture: start.gesture, hand, phase: progress.phase, progress: progress.progress, shoulder, rest, at: start.at(), yaw: deps.yaw() });
2297
+ const [x, y, z] = worked.pose.position;
2298
+ const [qx, qy, qz, qw] = worked.pose.quaternion;
2299
+ const pose = { position: [x, y, z], quaternion: [qx, qy, qz, qw] };
2300
+ return hand === "R" ? { ...base ?? {}, R: pose, grip: worked.grip, fingers: { index: worked.index } } : { ...base ?? {}, L: pose, gripL: worked.grip, fingersL: { index: worked.index }, slideL: null };
2301
+ },
2302
+ cancel: stop
2303
+ };
2304
+ }
2103
2305
  return __toCommonJS(index_exports);
2104
2306
  })();
2105
2307
  //# sourceMappingURL=index.global.js.map