@adatechnology/game-player 0.9.0 → 0.9.2
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 +8 -0
- package/dist/index.d.ts +127 -4
- package/dist/index.global.js +181 -4
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +181 -4
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -99,3 +99,11 @@ weapon hand, and the weapon stays in its hand; one of both hands (a big thing ca
|
|
|
99
99
|
the hip for a pistol, a sling on the back for a rifle) and, with none, drops it to the ground. A two-handed weapon (a rifle, a shotgun) leaves no hand free, so any interaction puts it
|
|
100
100
|
away or drops it. `drawBackOf(plan)` says what comes back to the hands when the interaction ends: what was put away, never what fell. `interactionHandsFor({ massKg, size })` says
|
|
101
101
|
whether a thing goes in one hand (up to 4 kg and 0.7 m, `HANDS_DEFAULTS`). The hands and slot of each weapon come from `weaponHandsOf` of `@adatechnology/game-weapons`.
|
|
102
|
+
|
|
103
|
+
A long gun on its sling (owner, 2026-11-25) is not dropped: its slot is `sling` (`move: 'sling'`), it hangs on the body and stays there after the interaction until the player
|
|
104
|
+
brings it back; with no sling worn it goes to the next slot that takes it (`SLOT_FALLBACKS`: the back). Every move is seen: `planTakeSequence(plan)` lays the steps (each weapon
|
|
105
|
+
`stow`, `sling` or `drop`, the off hand first, then `reach`, `close`, `lift`; `HANDS_TIMING` in seconds), `planDrawSequence(moves)` the way back (`draw`), and
|
|
106
|
+
`createTakeTimeline(sequence).advance(dt)` says which steps began and ended in the host's frame and how far into the current one (eased). `gripOfItem(size, hand)` is how a hand closes on
|
|
107
|
+
a thing: its longest side through the fist along the thumb, its thinnest across the palm, its middle a palm's thickness plus half that side under the middle of the palm, the
|
|
108
|
+
fingers closed by its thickness; `itemPoseInHand(hand, grip)` and `handPoseOnItem(item, grip)` turn one into the other (the hand reaches the thing where it lies, and carries it in the
|
|
109
|
+
palm). The pose algebra they use is exported too (`composePose`, `poseHolding`, `blendPoses`, `quaternionOfBasis`).
|
package/dist/index.d.ts
CHANGED
|
@@ -207,6 +207,8 @@ declare function createDoorUse(rules?: Partial<DoorUseRules>): DoorUse;
|
|
|
207
207
|
* hold the weapon, and the weapon stays in its hand; one of both hands (a big thing carried, a push, a climb) first puts the weapon away in a slot of the body that takes it
|
|
208
208
|
* (a holster at the hip, a sling or back slot for a long gun) and, with none, lets it fall to the ground. A two-handed weapon (a rifle, a shotgun) leaves no hand free, so any
|
|
209
209
|
* interaction puts it away or drops it. When the interaction ends, what was put away comes back to its hand; what fell stays on the ground.
|
|
210
|
+
* A long gun on its sling (decision of the owner, 2026-11-25) is not put away: it swings onto the sling and hangs on the body (`sling`), and stays there after the interaction until
|
|
211
|
+
* the player brings it back to the hands; with no sling worn it goes to the next slot that takes it (`SLOT_FALLBACKS`: the back).
|
|
210
212
|
* Engine-agnostic: the host says what is in each hand and which slots the body wears; this says which hand acts and what happens to each weapon.
|
|
211
213
|
*/
|
|
212
214
|
/** `R` the weapon hand, `L` the other one. */
|
|
@@ -225,11 +227,19 @@ interface HandsRules {
|
|
|
225
227
|
};
|
|
226
228
|
}
|
|
227
229
|
declare const HANDS_DEFAULTS: HandsRules;
|
|
228
|
-
/**
|
|
230
|
+
/** The slot a weapon goes to when the body wears none of its own: a long gun with no sling goes across the back. */
|
|
231
|
+
declare const SLOT_FALLBACKS: Readonly<Record<string, readonly string[]>>;
|
|
232
|
+
/** The slot of a long gun that hangs on its sling. */
|
|
233
|
+
declare const SLING_SLOT = "sling";
|
|
234
|
+
/**
|
|
235
|
+
* What a weapon does when the hands are wanted: `holster` goes to `slot` and comes back after; `sling` hangs on the body by its sling and stays there until it is brought back;
|
|
236
|
+
* `drop` falls to the ground and stays there.
|
|
237
|
+
*/
|
|
238
|
+
type HandsMoveKind = 'holster' | 'sling' | 'drop';
|
|
229
239
|
interface HandsMove {
|
|
230
240
|
readonly hand: BodyHand;
|
|
231
241
|
readonly weapon: string;
|
|
232
|
-
readonly move:
|
|
242
|
+
readonly move: HandsMoveKind;
|
|
233
243
|
readonly slot?: string;
|
|
234
244
|
}
|
|
235
245
|
interface HandsPlan {
|
|
@@ -248,6 +258,8 @@ interface PlanHandsParams {
|
|
|
248
258
|
};
|
|
249
259
|
/** The slots the body wears and that are empty (a holster, a sling); one weapon each. */
|
|
250
260
|
readonly slots: readonly string[];
|
|
261
|
+
/** The slots tried when a weapon's own is not worn; default `SLOT_FALLBACKS`. */
|
|
262
|
+
readonly fallbacks?: Readonly<Record<string, readonly string[]>>;
|
|
251
263
|
}
|
|
252
264
|
/** How many hands it takes to carry a thing of `massKg` and `size` (metres, any order). */
|
|
253
265
|
declare function interactionHandsFor(thing: {
|
|
@@ -256,12 +268,123 @@ declare function interactionHandsFor(thing: {
|
|
|
256
268
|
}, rules?: HandsRules): 1 | 2;
|
|
257
269
|
/** Which hand acts and what happens to each weapon in the way. */
|
|
258
270
|
declare function planHands(params: PlanHandsParams): HandsPlan;
|
|
259
|
-
/** What comes back to the hands once the interaction ends: the weapons that were put away, each to its hand; the
|
|
271
|
+
/** What comes back to the hands once the interaction ends: the weapons that were put away, each to its hand; the slung ones stay on the sling and the dropped ones on the ground. */
|
|
260
272
|
declare function drawBackOf(plan: HandsPlan): readonly {
|
|
261
273
|
readonly hand: BodyHand;
|
|
262
274
|
readonly weapon: string;
|
|
263
275
|
}[];
|
|
264
276
|
|
|
277
|
+
/**
|
|
278
|
+
* The little pose algebra the hands need, engine-agnostic: a pose is a position and a unit quaternion `[x, y, z, w]` (local to world), as `HandPose` of game-contracts.
|
|
279
|
+
*/
|
|
280
|
+
type Vec3Tuple = readonly [number, number, number];
|
|
281
|
+
type QuatTuple = readonly [number, number, number, number];
|
|
282
|
+
interface Pose {
|
|
283
|
+
readonly position: Vec3Tuple;
|
|
284
|
+
readonly quaternion: QuatTuple;
|
|
285
|
+
}
|
|
286
|
+
/** The rotation whose local x, y and z go to `x`, `y` and `z` (an orthonormal, right-handed basis). */
|
|
287
|
+
declare function quaternionOfBasis(x: readonly number[], y: readonly number[], z: readonly number[]): [number, number, number, number];
|
|
288
|
+
declare function multiplyQuaternions(a: QuatTuple, b: QuatTuple): [number, number, number, number];
|
|
289
|
+
declare function conjugateQuaternion(q: QuatTuple): [number, number, number, number];
|
|
290
|
+
declare function rotateVector(q: QuatTuple, v: Vec3Tuple): [number, number, number];
|
|
291
|
+
/** `parent` then `local`: the pose of a thing given in the frame of `parent`. */
|
|
292
|
+
declare function composePose(parent: Pose, local: Pose): Pose;
|
|
293
|
+
/** The pose whose frame puts `local` at `world`: `composePose(result, local)` is `world`. */
|
|
294
|
+
declare function poseHolding(world: Pose, local: Pose): Pose;
|
|
295
|
+
/** Between `a` (k 0) and `b` (k 1): the position on the line, the turn the short way round. */
|
|
296
|
+
declare function blendPoses(a: Pose, b: Pose, k: number): Pose;
|
|
297
|
+
/** Slow in, slow out: a hand speeds up off its start and settles on its target. */
|
|
298
|
+
declare function easeInOut(t: number): number;
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* How a hand closes on a thing it picks up (decision of the owner, 2026-11-25: the hand on the thing's real grip, the fingers closed around it, the thing in the palm, aligned with
|
|
302
|
+
* the hand in both views). Engine-agnostic: the thing is its box (`size`, metres, in its own frame), the hand frame is the one of `HandTargets`: +x along the fingers, +y the back
|
|
303
|
+
* of the hand, the thumb at -z for a right hand and +z for a left one. The thing's longest side runs through the fist along the thumb (a bottle stands up in a hand held thumb up),
|
|
304
|
+
* its thinnest side across the palm (the palm lies on its broad face), and its middle sits a palm's thickness plus half that side under the middle of the palm.
|
|
305
|
+
*/
|
|
306
|
+
|
|
307
|
+
declare const ITEM_GRIP: {
|
|
308
|
+
/** Half the thickness of a palm (m): what lies between the middle of the palm and the thing. */
|
|
309
|
+
readonly palm: 0.011;
|
|
310
|
+
/** The closure of the fingers (0 open, 1 a fist) on a thing this thick (m) or thinner. */
|
|
311
|
+
readonly fistAt: 0.03;
|
|
312
|
+
readonly closed: 0.95;
|
|
313
|
+
/** How much the closure opens per metre the thing is thicker than `fistAt`, and the most it opens to. */
|
|
314
|
+
readonly openPerMetre: 4;
|
|
315
|
+
readonly open: 0.35;
|
|
316
|
+
/** The fingers before they close on it, as the hand comes to the thing. */
|
|
317
|
+
readonly reaching: 0.15;
|
|
318
|
+
};
|
|
319
|
+
interface ItemGrip {
|
|
320
|
+
/** The thing's middle and turn in the frame of the hand. */
|
|
321
|
+
readonly local: Pose;
|
|
322
|
+
/** How closed the fingers are on it (`HandTargets.grip`). */
|
|
323
|
+
readonly grip: number;
|
|
324
|
+
}
|
|
325
|
+
/** The grip of a hand on a thing of box `size` (its own frame, metres). */
|
|
326
|
+
declare function gripOfItem(size: readonly number[], hand: BodyHand): ItemGrip;
|
|
327
|
+
/** Where the thing is with the hand at `hand` holding it by `grip`. */
|
|
328
|
+
declare function itemPoseInHand(hand: Pose, grip: ItemGrip): Pose;
|
|
329
|
+
/** Where the hand goes to take the thing where it lies (`item`, its middle and turn) by `grip`. */
|
|
330
|
+
declare function handPoseOnItem(item: Pose, grip: ItemGrip): Pose;
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* The hands take a thing in steps one can see (decision of the owner, 2026-11-25): first each weapon in the way goes off the hands (`stow` to its holster, `sling` onto its sling, or
|
|
334
|
+
* `drop`: tossed to the ground), then the hand `reach`es the thing's grip, the fingers `close` on it, and it is `lift`ed to where it is carried. A weapon comes back to the hands the
|
|
335
|
+
* same way round (`draw`). Engine-agnostic and clock-free: the host advances it by its own frame's `dt` and does what each step says.
|
|
336
|
+
*/
|
|
337
|
+
|
|
338
|
+
/** Seconds each step takes. */
|
|
339
|
+
interface HandsTiming {
|
|
340
|
+
readonly stow: number;
|
|
341
|
+
readonly sling: number;
|
|
342
|
+
readonly drop: number;
|
|
343
|
+
readonly reach: number;
|
|
344
|
+
readonly close: number;
|
|
345
|
+
readonly lift: number;
|
|
346
|
+
readonly draw: number;
|
|
347
|
+
}
|
|
348
|
+
declare const HANDS_TIMING: HandsTiming;
|
|
349
|
+
type TakeStepKind = 'stow' | 'sling' | 'drop' | 'reach' | 'close' | 'lift' | 'draw';
|
|
350
|
+
interface TakeStep {
|
|
351
|
+
readonly kind: TakeStepKind;
|
|
352
|
+
/** The weapon the step moves (`stow`, `sling`, `drop`, `draw`). */
|
|
353
|
+
readonly move?: HandsMove;
|
|
354
|
+
readonly start: number;
|
|
355
|
+
readonly end: number;
|
|
356
|
+
}
|
|
357
|
+
interface TakeSequence {
|
|
358
|
+
readonly steps: readonly TakeStep[];
|
|
359
|
+
readonly duration: number;
|
|
360
|
+
}
|
|
361
|
+
/** The steps of taking a thing with `plan`: the weapons off the hands (the off hand first), then reach, close and lift. `take` false leaves the thing out (a push). */
|
|
362
|
+
declare function planTakeSequence(plan: HandsPlan, options?: {
|
|
363
|
+
readonly timing?: HandsTiming;
|
|
364
|
+
readonly take?: boolean;
|
|
365
|
+
}): TakeSequence;
|
|
366
|
+
/** The steps of bringing weapons back to the hands, one after the other. */
|
|
367
|
+
declare function planDrawSequence(moves: readonly HandsMove[], timing?: HandsTiming): TakeSequence;
|
|
368
|
+
interface TakeProgress {
|
|
369
|
+
/** The step going on now, null once the sequence is over. */
|
|
370
|
+
readonly step: TakeStep | null;
|
|
371
|
+
/** How far into it, 0..1, eased (slow in, slow out). */
|
|
372
|
+
readonly progress: number;
|
|
373
|
+
/** The steps that began during this advance, in order. */
|
|
374
|
+
readonly started: readonly TakeStep[];
|
|
375
|
+
/** The steps that ended during this advance, in order. */
|
|
376
|
+
readonly ended: readonly TakeStep[];
|
|
377
|
+
readonly done: boolean;
|
|
378
|
+
}
|
|
379
|
+
interface TakeTimeline {
|
|
380
|
+
readonly sequence: TakeSequence;
|
|
381
|
+
/** Seconds since it began. */
|
|
382
|
+
readonly time: number;
|
|
383
|
+
/** Moves the timeline on by `dt` seconds (the host's frame). The first call with 0 starts the first step. */
|
|
384
|
+
advance(dt: number): TakeProgress;
|
|
385
|
+
}
|
|
386
|
+
declare function createTakeTimeline(sequence: TakeSequence): TakeTimeline;
|
|
387
|
+
|
|
265
388
|
/**
|
|
266
389
|
* Draws a body that moves in fixed steps between two of them: remember the last two step poses, blend by the scheduler's `alpha` (CLAUDE.md §15).
|
|
267
390
|
* Drawn at the step pose, a body moves 0, 1 or 2 steps per frame at a display that is not the step's rate: it stutters.
|
|
@@ -288,4 +411,4 @@ declare const STEP_POSE_DEFAULTS: {
|
|
|
288
411
|
};
|
|
289
412
|
declare function createStepPose(start: StepPosition, options?: StepPoseOptions): StepPose;
|
|
290
413
|
|
|
291
|
-
export { 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, type HandWeapon, type HandsMove, type HandsPlan, type HandsRules, KnownDeathCause, PLAYER_TEXT, type PlanHandsParams, PlayerMessageId, type PlayerText, STEP_POSE_DEFAULTS, type StepPose, type StepPoseOptions, type StepPosition, canPush, createDoorUse, createFurniturePush, createRng, createStepPose, dragMarkFor, drawBackOf, frictionFor, gripSecondsFor, interactionHandsFor, planHands, playerTextOf, pushAccelerationFor, pushSpeedFor, slipsOnTop, swingSecondsFor };
|
|
414
|
+
export { 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, PLAYER_TEXT, type PlanHandsParams, PlayerMessageId, 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, blendPoses, canPush, composePose, conjugateQuaternion, createDoorUse, createFurniturePush, createRng, createStepPose, createTakeTimeline, dragMarkFor, drawBackOf, easeInOut, frictionFor, gripOfItem, gripSecondsFor, handPoseOnItem, interactionHandsFor, itemPoseInHand, multiplyQuaternions, planDrawSequence, planHands, planTakeSequence, playerTextOf, poseHolding, pushAccelerationFor, pushSpeedFor, quaternionOfBasis, rotateVector, slipsOnTop, swingSecondsFor };
|
package/dist/index.global.js
CHANGED
|
@@ -26,27 +26,45 @@ var GamePlayer = (() => {
|
|
|
26
26
|
DOOR_USE_DEFAULTS: () => DOOR_USE_DEFAULTS,
|
|
27
27
|
FURNITURE_PUSH_DEFAULTS: () => FURNITURE_PUSH_DEFAULTS,
|
|
28
28
|
HANDS_DEFAULTS: () => HANDS_DEFAULTS,
|
|
29
|
+
HANDS_TIMING: () => HANDS_TIMING,
|
|
30
|
+
ITEM_GRIP: () => ITEM_GRIP,
|
|
29
31
|
PLAYER_DEFAULTS: () => PLAYER_DEFAULTS,
|
|
30
32
|
PLAYER_TEXT: () => PLAYER_TEXT,
|
|
33
|
+
SLING_SLOT: () => SLING_SLOT,
|
|
34
|
+
SLOT_FALLBACKS: () => SLOT_FALLBACKS,
|
|
31
35
|
STEP_POSE_DEFAULTS: () => STEP_POSE_DEFAULTS,
|
|
36
|
+
blendPoses: () => blendPoses,
|
|
32
37
|
canPush: () => canPush,
|
|
38
|
+
composePose: () => composePose,
|
|
39
|
+
conjugateQuaternion: () => conjugateQuaternion,
|
|
33
40
|
createDoorUse: () => createDoorUse,
|
|
34
41
|
createFurniturePush: () => createFurniturePush,
|
|
35
42
|
createPlayer: () => createPlayer,
|
|
36
43
|
createPlayerTestModule: () => createPlayerTestModule,
|
|
37
44
|
createRng: () => createRng,
|
|
38
45
|
createStepPose: () => createStepPose,
|
|
46
|
+
createTakeTimeline: () => createTakeTimeline,
|
|
39
47
|
deriveVitals: () => deriveVitals,
|
|
40
48
|
dragMarkFor: () => dragMarkFor,
|
|
41
49
|
drawBackOf: () => drawBackOf,
|
|
42
50
|
drawWound: () => drawWound,
|
|
51
|
+
easeInOut: () => easeInOut,
|
|
43
52
|
frictionFor: () => frictionFor,
|
|
53
|
+
gripOfItem: () => gripOfItem,
|
|
44
54
|
gripSecondsFor: () => gripSecondsFor,
|
|
55
|
+
handPoseOnItem: () => handPoseOnItem,
|
|
45
56
|
interactionHandsFor: () => interactionHandsFor,
|
|
57
|
+
itemPoseInHand: () => itemPoseInHand,
|
|
58
|
+
multiplyQuaternions: () => multiplyQuaternions,
|
|
59
|
+
planDrawSequence: () => planDrawSequence,
|
|
46
60
|
planHands: () => planHands,
|
|
61
|
+
planTakeSequence: () => planTakeSequence,
|
|
47
62
|
playerTextOf: () => playerTextOf,
|
|
63
|
+
poseHolding: () => poseHolding,
|
|
48
64
|
pushAccelerationFor: () => pushAccelerationFor,
|
|
49
65
|
pushSpeedFor: () => pushSpeedFor,
|
|
66
|
+
quaternionOfBasis: () => quaternionOfBasis,
|
|
67
|
+
rotateVector: () => rotateVector,
|
|
50
68
|
slipsOnTop: () => slipsOnTop,
|
|
51
69
|
swingSecondsFor: () => swingSecondsFor
|
|
52
70
|
});
|
|
@@ -1566,6 +1584,8 @@ var GamePlayer = (() => {
|
|
|
1566
1584
|
|
|
1567
1585
|
// src/hands.ts
|
|
1568
1586
|
var HANDS_DEFAULTS = { oneHand: { maxKg: 4, maxLength: 0.7 } };
|
|
1587
|
+
var SLOT_FALLBACKS = { sling: ["back"] };
|
|
1588
|
+
var SLING_SLOT = "sling";
|
|
1569
1589
|
function interactionHandsFor(thing, rules = HANDS_DEFAULTS) {
|
|
1570
1590
|
const longest = Math.max(0, ...thing.size ?? []);
|
|
1571
1591
|
return thing.massKg > rules.oneHand.maxKg || longest > rules.oneHand.maxLength ? 2 : 1;
|
|
@@ -1574,11 +1594,13 @@ var GamePlayer = (() => {
|
|
|
1574
1594
|
function planHands(params) {
|
|
1575
1595
|
const { R, L } = params.weapons;
|
|
1576
1596
|
const free = [...params.slots];
|
|
1597
|
+
const fallbacks = params.fallbacks ?? SLOT_FALLBACKS;
|
|
1577
1598
|
const stow = (hand, weapon) => {
|
|
1578
|
-
const
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1599
|
+
const tried = weapon.slot === null ? [] : [weapon.slot, ...fallbacks[weapon.slot] ?? []];
|
|
1600
|
+
const slot = tried.find((name) => free.includes(name));
|
|
1601
|
+
if (slot === void 0) return { hand, weapon: weapon.id, move: "drop" };
|
|
1602
|
+
free.splice(free.indexOf(slot), 1);
|
|
1603
|
+
return { hand, weapon: weapon.id, move: slot === SLING_SLOT ? "sling" : "holster", slot };
|
|
1582
1604
|
};
|
|
1583
1605
|
if (params.hands === 2) {
|
|
1584
1606
|
const moves = [...holds(R) ? [stow("R", R)] : [], ...holds(L) ? [stow("L", L)] : []];
|
|
@@ -1592,6 +1614,161 @@ var GamePlayer = (() => {
|
|
|
1592
1614
|
return plan.moves.filter((move) => move.move === "holster").map((move) => ({ hand: move.hand, weapon: move.weapon }));
|
|
1593
1615
|
}
|
|
1594
1616
|
|
|
1617
|
+
// src/hand-math.ts
|
|
1618
|
+
function quaternionOfBasis(x, y, z) {
|
|
1619
|
+
const [m00, m10, m20] = [x[0] ?? 0, x[1] ?? 0, x[2] ?? 0];
|
|
1620
|
+
const [m01, m11, m21] = [y[0] ?? 0, y[1] ?? 0, y[2] ?? 0];
|
|
1621
|
+
const [m02, m12, m22] = [z[0] ?? 0, z[1] ?? 0, z[2] ?? 0];
|
|
1622
|
+
const trace = m00 + m11 + m22;
|
|
1623
|
+
if (trace > 0) {
|
|
1624
|
+
const s2 = 0.5 / Math.sqrt(trace + 1);
|
|
1625
|
+
return [(m21 - m12) * s2, (m02 - m20) * s2, (m10 - m01) * s2, 0.25 / s2];
|
|
1626
|
+
}
|
|
1627
|
+
if (m00 > m11 && m00 > m22) {
|
|
1628
|
+
const s2 = 2 * Math.sqrt(1 + m00 - m11 - m22);
|
|
1629
|
+
return [0.25 * s2, (m01 + m10) / s2, (m02 + m20) / s2, (m21 - m12) / s2];
|
|
1630
|
+
}
|
|
1631
|
+
if (m11 > m22) {
|
|
1632
|
+
const s2 = 2 * Math.sqrt(1 + m11 - m00 - m22);
|
|
1633
|
+
return [(m01 + m10) / s2, 0.25 * s2, (m12 + m21) / s2, (m02 - m20) / s2];
|
|
1634
|
+
}
|
|
1635
|
+
const s = 2 * Math.sqrt(1 + m22 - m00 - m11);
|
|
1636
|
+
return [(m02 + m20) / s, (m12 + m21) / s, 0.25 * s, (m10 - m01) / s];
|
|
1637
|
+
}
|
|
1638
|
+
function multiplyQuaternions(a, b) {
|
|
1639
|
+
const [ax, ay, az, aw] = a;
|
|
1640
|
+
const [bx, by, bz, bw] = b;
|
|
1641
|
+
return [ax * bw + aw * bx + ay * bz - az * by, ay * bw + aw * by + az * bx - ax * bz, az * bw + aw * bz + ax * by - ay * bx, aw * bw - ax * bx - ay * by - az * bz];
|
|
1642
|
+
}
|
|
1643
|
+
function conjugateQuaternion(q) {
|
|
1644
|
+
return [-q[0], -q[1], -q[2], q[3]];
|
|
1645
|
+
}
|
|
1646
|
+
function rotateVector(q, v) {
|
|
1647
|
+
const [x, y, z, w] = q;
|
|
1648
|
+
const tx = 2 * (y * v[2] - z * v[1]);
|
|
1649
|
+
const ty = 2 * (z * v[0] - x * v[2]);
|
|
1650
|
+
const tz = 2 * (x * v[1] - y * v[0]);
|
|
1651
|
+
return [v[0] + w * tx + (y * tz - z * ty), v[1] + w * ty + (z * tx - x * tz), v[2] + w * tz + (x * ty - y * tx)];
|
|
1652
|
+
}
|
|
1653
|
+
function composePose(parent, local) {
|
|
1654
|
+
const offset = rotateVector(parent.quaternion, local.position);
|
|
1655
|
+
return {
|
|
1656
|
+
position: [parent.position[0] + offset[0], parent.position[1] + offset[1], parent.position[2] + offset[2]],
|
|
1657
|
+
quaternion: multiplyQuaternions(parent.quaternion, local.quaternion)
|
|
1658
|
+
};
|
|
1659
|
+
}
|
|
1660
|
+
function poseHolding(world, local) {
|
|
1661
|
+
const quaternion = multiplyQuaternions(world.quaternion, conjugateQuaternion(local.quaternion));
|
|
1662
|
+
const offset = rotateVector(quaternion, local.position);
|
|
1663
|
+
return { position: [world.position[0] - offset[0], world.position[1] - offset[1], world.position[2] - offset[2]], quaternion };
|
|
1664
|
+
}
|
|
1665
|
+
function blendPoses(a, b, k) {
|
|
1666
|
+
const lerp2 = (i) => a.position[i] + (b.position[i] - a.position[i]) * k;
|
|
1667
|
+
const dot = a.quaternion[0] * b.quaternion[0] + a.quaternion[1] * b.quaternion[1] + a.quaternion[2] * b.quaternion[2] + a.quaternion[3] * b.quaternion[3];
|
|
1668
|
+
const sign = dot < 0 ? -1 : 1;
|
|
1669
|
+
const mixed = [0, 1, 2, 3].map((i) => (a.quaternion[i] ?? 0) * (1 - k) + (b.quaternion[i] ?? 0) * sign * k);
|
|
1670
|
+
const length = Math.hypot(...mixed) || 1;
|
|
1671
|
+
return { position: [lerp2(0), lerp2(1), lerp2(2)], quaternion: [(mixed[0] ?? 0) / length, (mixed[1] ?? 0) / length, (mixed[2] ?? 0) / length, (mixed[3] ?? 1) / length] };
|
|
1672
|
+
}
|
|
1673
|
+
function easeInOut(t) {
|
|
1674
|
+
const k = Math.min(1, Math.max(0, t));
|
|
1675
|
+
return k * k * (3 - 2 * k);
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
// src/item-grip.ts
|
|
1679
|
+
var ITEM_GRIP = {
|
|
1680
|
+
/** Half the thickness of a palm (m): what lies between the middle of the palm and the thing. */
|
|
1681
|
+
palm: 0.011,
|
|
1682
|
+
/** The closure of the fingers (0 open, 1 a fist) on a thing this thick (m) or thinner. */
|
|
1683
|
+
fistAt: 0.03,
|
|
1684
|
+
closed: 0.95,
|
|
1685
|
+
/** How much the closure opens per metre the thing is thicker than `fistAt`, and the most it opens to. */
|
|
1686
|
+
openPerMetre: 4,
|
|
1687
|
+
open: 0.35,
|
|
1688
|
+
/** The fingers before they close on it, as the hand comes to the thing. */
|
|
1689
|
+
reaching: 0.15
|
|
1690
|
+
};
|
|
1691
|
+
var AXES = [
|
|
1692
|
+
[1, 0, 0],
|
|
1693
|
+
[0, 1, 0],
|
|
1694
|
+
[0, 0, 1]
|
|
1695
|
+
];
|
|
1696
|
+
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]];
|
|
1697
|
+
var scaled = (v, k) => [v[0] * k, v[1] * k, v[2] * k];
|
|
1698
|
+
function gripOfItem(size, hand) {
|
|
1699
|
+
const sides = [0, 1, 2].map((axis) => ({ axis, length: Math.max(0, size[axis] ?? 0) })).sort((a, b) => b.length - a.length);
|
|
1700
|
+
const [long, mid, thin] = sides;
|
|
1701
|
+
const thumb = hand === "R" ? [0, 0, -1] : [0, 0, 1];
|
|
1702
|
+
const columns = [AXES[0], AXES[1], AXES[2]];
|
|
1703
|
+
columns[long.axis] = thumb;
|
|
1704
|
+
columns[thin.axis] = [0, 1, 0];
|
|
1705
|
+
const next = (axis) => (axis + 1) % 3;
|
|
1706
|
+
const before = columns[next(next(mid.axis))];
|
|
1707
|
+
const after = columns[next(mid.axis)];
|
|
1708
|
+
columns[mid.axis] = cross(after, before);
|
|
1709
|
+
const quaternion = quaternionOfBasis(columns[0], columns[1], columns[2]);
|
|
1710
|
+
const depth = ITEM_GRIP.palm + thin.length / 2;
|
|
1711
|
+
const across = (mid.length + thin.length) / 2;
|
|
1712
|
+
const grip = Math.max(ITEM_GRIP.open, Math.min(ITEM_GRIP.closed, ITEM_GRIP.closed - ITEM_GRIP.openPerMetre * Math.max(0, across - ITEM_GRIP.fistAt)));
|
|
1713
|
+
return { local: { position: scaled([0, -1, 0], depth), quaternion }, grip };
|
|
1714
|
+
}
|
|
1715
|
+
function itemPoseInHand(hand, grip) {
|
|
1716
|
+
return composePose(hand, grip.local);
|
|
1717
|
+
}
|
|
1718
|
+
function handPoseOnItem(item, grip) {
|
|
1719
|
+
return poseHolding(item, grip.local);
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
// src/take-sequence.ts
|
|
1723
|
+
var HANDS_TIMING = { stow: 0.5, sling: 0.45, drop: 0.4, reach: 0.38, close: 0.16, lift: 0.3, draw: 0.5 };
|
|
1724
|
+
var KIND_OF_MOVE = { holster: "stow", sling: "sling", drop: "drop" };
|
|
1725
|
+
var lay = (parts) => {
|
|
1726
|
+
let at = 0;
|
|
1727
|
+
const steps = parts.map((part) => {
|
|
1728
|
+
const step = { kind: part.kind, start: at, end: at + part.seconds, ...part.move ? { move: part.move } : {} };
|
|
1729
|
+
at = step.end;
|
|
1730
|
+
return step;
|
|
1731
|
+
});
|
|
1732
|
+
return { steps, duration: at };
|
|
1733
|
+
};
|
|
1734
|
+
function planTakeSequence(plan, options = {}) {
|
|
1735
|
+
const timing = options.timing ?? HANDS_TIMING;
|
|
1736
|
+
const moves = [...plan.moves].reverse().map((move) => ({ kind: KIND_OF_MOVE[move.move], seconds: timing[KIND_OF_MOVE[move.move]], move }));
|
|
1737
|
+
const take = options.take === false ? [] : [
|
|
1738
|
+
{ kind: "reach", seconds: timing.reach },
|
|
1739
|
+
{ kind: "close", seconds: timing.close },
|
|
1740
|
+
{ kind: "lift", seconds: timing.lift }
|
|
1741
|
+
];
|
|
1742
|
+
return lay([...moves, ...take]);
|
|
1743
|
+
}
|
|
1744
|
+
function planDrawSequence(moves, timing = HANDS_TIMING) {
|
|
1745
|
+
return lay(moves.map((move) => ({ kind: "draw", seconds: timing.draw, move })));
|
|
1746
|
+
}
|
|
1747
|
+
function createTakeTimeline(sequence) {
|
|
1748
|
+
let time = 0;
|
|
1749
|
+
let begun = 0;
|
|
1750
|
+
let over = 0;
|
|
1751
|
+
const steps = sequence.steps;
|
|
1752
|
+
return {
|
|
1753
|
+
sequence,
|
|
1754
|
+
get time() {
|
|
1755
|
+
return time;
|
|
1756
|
+
},
|
|
1757
|
+
advance(dt) {
|
|
1758
|
+
time = Math.min(sequence.duration, time + Math.max(0, dt));
|
|
1759
|
+
const startedFrom = begun;
|
|
1760
|
+
while (begun < steps.length && steps[begun].start <= time) begun++;
|
|
1761
|
+
const endedFrom = over;
|
|
1762
|
+
while (over < begun && steps[over].end <= time) over++;
|
|
1763
|
+
const started = steps.slice(startedFrom, begun);
|
|
1764
|
+
const ended = steps.slice(endedFrom, over);
|
|
1765
|
+
const step = steps.find((entry) => time < entry.end) ?? null;
|
|
1766
|
+
const progress = step ? easeInOut((time - step.start) / Math.max(1e-6, step.end - step.start)) : 1;
|
|
1767
|
+
return { step, progress, started, ended, done: time >= sequence.duration };
|
|
1768
|
+
}
|
|
1769
|
+
};
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1595
1772
|
// src/step-pose.ts
|
|
1596
1773
|
var STEP_POSE_DEFAULTS = { teleportDistance: 3 };
|
|
1597
1774
|
function createStepPose(start, options = {}) {
|