@adatechnology/game-player 0.13.5 → 0.15.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.js CHANGED
@@ -467,11 +467,13 @@ function createDoorUse(rules = {}) {
467
467
  }
468
468
 
469
469
  // src/hands.ts
470
- var HANDS_DEFAULTS = { oneHand: { maxKg: 4, maxLength: 0.7 } };
470
+ var HANDS_BY_HANDLE = { maxKg: 25 };
471
+ var HANDS_DEFAULTS = { oneHand: { maxKg: 4, maxLength: 0.7 }, byHandle: HANDS_BY_HANDLE };
471
472
  var SLOT_FALLBACKS = { sling: ["back"], hip: ["belt"] };
472
473
  var BELT_SLOT = "belt";
473
474
  var SLING_SLOT = "sling";
474
475
  function interactionHandsFor(thing, rules = HANDS_DEFAULTS) {
476
+ if (thing.handle) return thing.massKg > (rules.byHandle ?? HANDS_BY_HANDLE).maxKg ? 2 : 1;
475
477
  const longest = Math.max(0, ...thing.size ?? []);
476
478
  return thing.massKg > rules.oneHand.maxKg || longest > rules.oneHand.maxLength ? 2 : 1;
477
479
  }
@@ -593,7 +595,16 @@ var AXES = [
593
595
  ];
594
596
  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]];
595
597
  var scaled = (v, k) => [v[0] * k, v[1] * k, v[2] * k];
596
- function gripOfItem(size, hand) {
598
+ function gripOnHandle(handle, hand) {
599
+ const up = hand === "R" ? [0, 0, -1] : [0, 0, 1];
600
+ const along = [1, 0, 0];
601
+ const quaternion = quaternionOfBasis(along, up, cross(along, up));
602
+ const local = { position: [0, 0, 0], quaternion };
603
+ const handleInHand = composePose(local, { position: [handle[0] ?? 0, handle[1] ?? 0, handle[2] ?? 0], quaternion: [0, 0, 0, 1] }).position;
604
+ return { local: { position: [-handleInHand[0], -handleInHand[1] - ITEM_GRIP.palm, -handleInHand[2]], quaternion }, grip: ITEM_GRIP.closed, hanging: true };
605
+ }
606
+ function gripOfItem(size, hand, handle) {
607
+ if (handle) return gripOnHandle(handle, hand);
597
608
  const sides = [0, 1, 2].map((axis) => ({ axis, length: Math.max(0, size[axis] ?? 0) })).sort((a, b) => b.length - a.length);
598
609
  const [long, mid, thin] = sides;
599
610
  const thumb = hand === "R" ? [0, 0, -1] : [0, 0, 1];
@@ -689,12 +700,434 @@ function createStepPose(start, options = {}) {
689
700
  }
690
701
  };
691
702
  }
703
+
704
+ // src/vehicle-boarding.ts
705
+ var VEHICLE_BOARDING_DEFAULTS = { rattleSeconds: 0.75, endTolerance: 0.02 };
706
+ function createVehicleBoarding(ports, rules = {}) {
707
+ const r = { ...VEHICLE_BOARDING_DEFAULTS, ...rules };
708
+ let phase = "out";
709
+ let clock = 0;
710
+ const isOpen = () => ports.doorOpenness() >= 1 - r.endTolerance;
711
+ const isShut = () => ports.doorOpenness() <= r.endTolerance;
712
+ const sitDown = (events) => {
713
+ if (ports.sit()) phase = "sit";
714
+ else {
715
+ phase = "out";
716
+ events.push({ type: "refused", reason: "no_seat" });
717
+ }
718
+ };
719
+ const pending = [];
720
+ return {
721
+ get phase() {
722
+ return phase;
723
+ },
724
+ get driving() {
725
+ return phase === "in";
726
+ },
727
+ get handOnDoor() {
728
+ return phase === "shut" || phase === "unlatch";
729
+ },
730
+ enter() {
731
+ if (phase !== "out") return false;
732
+ if (!ports.hasDoor) {
733
+ if (!ports.sit()) return false;
734
+ phase = "sit";
735
+ return true;
736
+ }
737
+ if (isOpen()) {
738
+ phase = "open";
739
+ return true;
740
+ }
741
+ phase = "reach";
742
+ const locked = ports.doorLocked();
743
+ ports.reachHandle({
744
+ pull: true,
745
+ locked,
746
+ onGrip: () => {
747
+ if (phase !== "reach") return;
748
+ if (locked) {
749
+ phase = "refused";
750
+ clock = 0;
751
+ return;
752
+ }
753
+ ports.swingDoor(true);
754
+ phase = "open";
755
+ },
756
+ onMiss: () => {
757
+ if (phase !== "reach") return;
758
+ phase = "out";
759
+ pending.push({ type: "refused", reason: "blocked" });
760
+ }
761
+ });
762
+ return true;
763
+ },
764
+ takeSeat() {
765
+ if (phase !== "out" || !ports.sit()) return false;
766
+ phase = "sit";
767
+ return true;
768
+ },
769
+ leave() {
770
+ if (phase !== "in") return false;
771
+ if (!ports.hasDoor) {
772
+ if (!ports.stand()) {
773
+ pending.push({ type: "refused", reason: "blocked" });
774
+ return false;
775
+ }
776
+ phase = "stand";
777
+ return true;
778
+ }
779
+ ports.swingDoor(true);
780
+ phase = "unlatch";
781
+ return true;
782
+ },
783
+ dropped() {
784
+ if (phase === "out") return;
785
+ phase = "out";
786
+ pending.push({ type: "out" });
787
+ },
788
+ update(dt) {
789
+ const events = pending.splice(0);
790
+ switch (phase) {
791
+ case "refused":
792
+ clock += dt;
793
+ if (clock >= r.rattleSeconds) {
794
+ phase = "out";
795
+ events.push({ type: "refused", reason: "locked" });
796
+ }
797
+ break;
798
+ case "open":
799
+ if (isOpen()) sitDown(events);
800
+ break;
801
+ case "sit":
802
+ if (ports.seated() >= 1) {
803
+ if (ports.hasDoor && !isShut()) {
804
+ ports.swingDoor(false);
805
+ phase = "shut";
806
+ } else {
807
+ phase = "in";
808
+ events.push({ type: "in" });
809
+ }
810
+ }
811
+ break;
812
+ case "shut":
813
+ if (isShut()) {
814
+ phase = "in";
815
+ events.push({ type: "in" });
816
+ }
817
+ break;
818
+ case "unlatch":
819
+ if (!isOpen()) break;
820
+ if (ports.stand()) phase = "stand";
821
+ else {
822
+ ports.swingDoor(false);
823
+ phase = "shut";
824
+ events.push({ type: "refused", reason: "blocked" });
825
+ }
826
+ break;
827
+ case "stand":
828
+ if (ports.seated() > 0) break;
829
+ if (!ports.hasDoor) {
830
+ phase = "out";
831
+ events.push({ type: "out" });
832
+ break;
833
+ }
834
+ phase = "close";
835
+ ports.reachHandle({
836
+ pull: false,
837
+ locked: false,
838
+ onGrip: () => {
839
+ if (phase === "close") ports.swingDoor(false);
840
+ },
841
+ // the door out of reach stays open: the driver is out all the same
842
+ onMiss: () => {
843
+ if (phase !== "close") return;
844
+ phase = "out";
845
+ pending.push({ type: "out" });
846
+ }
847
+ });
848
+ break;
849
+ case "close":
850
+ if (isShut()) {
851
+ phase = "out";
852
+ events.push({ type: "out" });
853
+ }
854
+ break;
855
+ default:
856
+ break;
857
+ }
858
+ return events;
859
+ }
860
+ };
861
+ }
862
+
863
+ // src/driving-hands.ts
864
+ var DRIVING_HANDS_DEFAULTS = { regripAt: 1.75, firstAt: 0.7, lead: 0.9, stillRate: 0.3, settleAt: 0.25, reachLimit: 2.3, regripSeconds: 0.24, leverReachSeconds: 0.22, leverHoldSeconds: 0.26 };
865
+ var smooth = (k) => k * k * (3 - 2 * k);
866
+ function createDrivingHands(rules = {}) {
867
+ const r = { ...DRIVING_HANDS_DEFAULTS, ...rules };
868
+ const hands = { left: { at: 0, hold: 1, toLever: 0, regrip: -1, from: 0, to: 0 }, right: { at: 0, hold: 1, toLever: 0, regrip: -1, from: 0, to: 0 } };
869
+ let last = 0;
870
+ let shifting = null;
871
+ const busy = (h) => h.regrip >= 0 || h.toLever > 0;
872
+ const total = r.leverReachSeconds * 2 + r.leverHoldSeconds;
873
+ return {
874
+ left: hands.left,
875
+ right: hands.right,
876
+ get leverThrow() {
877
+ if (!shifting) return 1;
878
+ return smooth(Math.max(0, Math.min(1, (shifting.clock - r.leverReachSeconds) / r.leverHoldSeconds)));
879
+ },
880
+ shift(hand) {
881
+ if (shifting) return;
882
+ shifting = { hand, clock: 0 };
883
+ hands[hand].regrip = -1;
884
+ },
885
+ reset(wheel) {
886
+ last = wheel;
887
+ shifting = null;
888
+ for (const h of [hands.left, hands.right]) Object.assign(h, { at: 0, hold: 1, toLever: 0, regrip: -1, from: 0, to: 0 });
889
+ },
890
+ update(dt, wheel) {
891
+ const turned = wheel - last;
892
+ last = wheel;
893
+ if (shifting) {
894
+ shifting.clock += dt;
895
+ const h = hands[shifting.hand];
896
+ const c = shifting.clock;
897
+ const out = smooth(Math.min(1, c / r.leverReachSeconds));
898
+ const back = smooth(Math.max(0, Math.min(1, (c - r.leverReachSeconds - r.leverHoldSeconds) / r.leverReachSeconds)));
899
+ h.toLever = out * (1 - back);
900
+ h.hold = 1 - h.toLever;
901
+ h.at = 0;
902
+ if (c >= total) {
903
+ h.toLever = 0;
904
+ h.hold = 1;
905
+ shifting = null;
906
+ }
907
+ }
908
+ for (const side of ["left", "right"]) {
909
+ const h = hands[side];
910
+ const other = hands[side === "left" ? "right" : "left"];
911
+ if (h.toLever > 0) continue;
912
+ if (h.regrip >= 0) {
913
+ h.regrip += dt;
914
+ const k = Math.min(1, h.regrip / r.regripSeconds);
915
+ h.at = h.from + (h.to - h.from) * smooth(k);
916
+ h.hold = Math.abs(2 * k - 1);
917
+ if (k >= 1) {
918
+ h.regrip = -1;
919
+ h.at = h.to;
920
+ h.hold = 1;
921
+ }
922
+ continue;
923
+ }
924
+ h.at += turned;
925
+ const far = Math.abs(h.at);
926
+ const rate = dt > 0 ? turned / dt : 0;
927
+ const turning = Math.abs(rate) > r.stillRate;
928
+ const goesDown = turning && side === "left" === rate > 0 && Math.sign(h.at) === Math.sign(rate);
929
+ const wants = far > (goesDown ? r.firstAt : r.regripAt) || !turning && far > r.settleAt;
930
+ if (wants && !busy(other) || far > r.reachLimit) {
931
+ h.regrip = 0;
932
+ h.from = h.at;
933
+ h.to = turning ? -Math.sign(rate) * r.lead : 0;
934
+ }
935
+ }
936
+ }
937
+ };
938
+ }
939
+
940
+ // src/gesture.ts
941
+ var GESTURE_TIMING = {
942
+ reach: 0.32,
943
+ back: 0.3,
944
+ 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 }
945
+ };
946
+ var ACTS_AT_START = /* @__PURE__ */ new Set(["read"]);
947
+ var GESTURE_ARM_REACH = 0.62;
948
+ function createGestureTimeline(gesture, timing = GESTURE_TIMING) {
949
+ const reach = timing.reach;
950
+ const act = timing.act[gesture];
951
+ const duration = reach + act + timing.back;
952
+ const actsAt = ACTS_AT_START.has(gesture) ? reach : reach + act;
953
+ let time = 0;
954
+ let fired = false;
955
+ return {
956
+ gesture,
957
+ duration,
958
+ advance(dt) {
959
+ time = Math.min(duration, time + Math.max(0, dt));
960
+ const acted = !fired && time >= actsAt;
961
+ if (acted) fired = true;
962
+ const phase = time < reach ? "reach" : time < reach + act ? "act" : "back";
963
+ const start = phase === "reach" ? 0 : phase === "act" ? reach : reach + act;
964
+ const length = phase === "reach" ? reach : phase === "act" ? act : timing.back;
965
+ return { phase, progress: easeInOut((time - start) / Math.max(1e-6, length)), acted, done: time >= duration };
966
+ }
967
+ };
968
+ }
969
+ var WITH_THE_HELD_THING = /* @__PURE__ */ new Set(["put_in", "pour"]);
970
+ function freeHandOf(busy) {
971
+ if (!busy.R) return "R";
972
+ return busy.L ? null : "L";
973
+ }
974
+ function gestureHandOf(choice) {
975
+ if (WITH_THE_HELD_THING.has(choice.gesture)) return choice.carrying ?? null;
976
+ return freeHandOf(choice.busy);
977
+ }
978
+ var add = (a, b, k = 1) => [a[0] + b[0] * k, a[1] + b[1] * k, a[2] + b[2] * k];
979
+ 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]];
980
+ var axisTurn = (axis, angle) => {
981
+ const s = Math.sin(angle / 2);
982
+ return [axis[0] * s, axis[1] * s, axis[2] * s, Math.cos(angle / 2)];
983
+ };
984
+ var UP = [0, 1, 0];
985
+ var actPose = (gesture, frame, k) => {
986
+ const { at, way, side, q } = frame;
987
+ switch (gesture) {
988
+ case "press":
989
+ return { position: add(at, way, -0.06 + 0.05 * Math.sin(Math.PI * k)), quaternion: q };
990
+ case "turn":
991
+ return { position: add(at, way, -0.02), quaternion: multiplyQuaternions(axisTurn(way, -k * Math.PI * 0.5), q) };
992
+ case "flip":
993
+ return { position: add(add(at, way, -0.02), UP, -0.03 * k), quaternion: q };
994
+ case "strike":
995
+ return { position: add(add(at, way, -0.04), side, 0.08 * (k - 0.5)), quaternion: q };
996
+ case "pour":
997
+ return { position: add(at, UP, 0.15), quaternion: multiplyQuaternions(axisTurn(side, -k * Math.PI * 0.4), q) };
998
+ case "tip":
999
+ return { position: add(at, UP, 0.05 * k), quaternion: multiplyQuaternions(axisTurn(side, -k * Math.PI * 0.55), q) };
1000
+ case "pull":
1001
+ return { position: add(at, way, -0.02 - 0.15 * k), quaternion: q };
1002
+ case "put_in":
1003
+ case "stow":
1004
+ return { position: [at[0], at[1], at[2]], quaternion: q };
1005
+ case "read":
1006
+ return { position: [at[0], at[1], at[2]], quaternion: multiplyQuaternions(axisTurn(way, Math.PI * 0.5), q) };
1007
+ }
1008
+ };
1009
+ var GRIP = {
1010
+ press: [0.85, 0.85],
1011
+ turn: [0.9, 0.9],
1012
+ flip: [0.55, 0.55],
1013
+ strike: [0.95, 0.95],
1014
+ pour: [1, 1],
1015
+ tip: [1, 1],
1016
+ put_in: [1, 0.15],
1017
+ stow: [0.2, 0.95],
1018
+ read: [0.7, 0.7],
1019
+ pull: [0.9, 0.9]
1020
+ };
1021
+ var OPEN_HAND = 0.2;
1022
+ function frameOf(params) {
1023
+ const { shoulder } = params;
1024
+ const flatX = params.at[0] - shoulder[0];
1025
+ const flatZ = params.at[2] - shoulder[2];
1026
+ const flat = Math.hypot(flatX, flatZ);
1027
+ const way = flat < 1e-4 ? [-Math.sin(params.yaw), 0, -Math.cos(params.yaw)] : [flatX / flat, 0, flatZ / flat];
1028
+ const little = params.hand === "R" ? [0, -1, 0] : [0, 1, 0];
1029
+ const side = cross2(little, way);
1030
+ const toward = [params.at[0] - shoulder[0], params.at[1] - shoulder[1], params.at[2] - shoulder[2]];
1031
+ const reach = Math.hypot(toward[0], toward[1], toward[2]);
1032
+ const at = reach > GESTURE_ARM_REACH ? add(shoulder, toward, GESTURE_ARM_REACH / reach) : params.at;
1033
+ return { at, way, side, q: quaternionOfBasis(way, side, little) };
1034
+ }
1035
+ function gestureHandPose(params) {
1036
+ const { rest, phase, progress, gesture } = params;
1037
+ const frame = frameOf(params);
1038
+ const [gripIn, gripOut] = GRIP[gesture];
1039
+ const index = gesture === "press" ? 0 : gesture === "flip" ? 0.2 : 1;
1040
+ if (phase === "reach") return { pose: blendPoses(rest, actPose(gesture, frame, 0), progress), grip: OPEN_HAND + (gripIn - OPEN_HAND) * progress, index: 1 + (index - 1) * progress };
1041
+ if (phase === "act") return { pose: actPose(gesture, frame, progress), grip: gripIn + (gripOut - gripIn) * progress, index };
1042
+ const stowed = gesture === "stow";
1043
+ const home = stowed ? { position: add(rest.position, UP, -0.3), quaternion: rest.quaternion } : rest;
1044
+ const grip = stowed ? gripOut : gripOut + (OPEN_HAND - gripOut) * progress;
1045
+ return { pose: blendPoses(actPose(gesture, frame, 1), home, progress), grip, index: index + (1 - index) * progress };
1046
+ }
1047
+
1048
+ // src/gesture-hands.ts
1049
+ var STAND_OFF = 0.5;
1050
+ var STEP_SPEED = 2.5;
1051
+ var MAX_WALK_SECONDS = 1.5;
1052
+ var TURN_RATE = 8;
1053
+ var STOOP_BELOW = 0.5;
1054
+ function stepUp(body, now, dt) {
1055
+ if (!body) return true;
1056
+ const point = now.start.at();
1057
+ body.face(point, Math.min(1, dt * TURN_RATE));
1058
+ const [x, z] = body.at();
1059
+ const dx = point[0] - x;
1060
+ const dz = point[2] - z;
1061
+ const d = Math.hypot(dx, dz);
1062
+ if (d <= STAND_OFF + 0.03 || now.walked > MAX_WALK_SECONDS) return true;
1063
+ const go = Math.min(d - STAND_OFF, STEP_SPEED * dt);
1064
+ if (go < 1e-4) return false;
1065
+ body.moveBy(dx / d * go, dz / d * go);
1066
+ const [x1, z1] = body.at();
1067
+ now.walked += dt;
1068
+ return Math.hypot(x1 - x, z1 - z) < go * 0.2;
1069
+ }
1070
+ function createGestureHands(deps) {
1071
+ let going = null;
1072
+ const stop = () => {
1073
+ going = null;
1074
+ deps.body?.stoop?.(false);
1075
+ };
1076
+ return {
1077
+ get busy() {
1078
+ return going !== null;
1079
+ },
1080
+ get hand() {
1081
+ return going?.hand ?? null;
1082
+ },
1083
+ start(start) {
1084
+ if (going) return { ok: false, reason: "busy" };
1085
+ const hand = gestureHandOf({ busy: deps.holding(), gesture: start.gesture, carrying: deps.carrying?.() ?? null });
1086
+ if (!hand) return { ok: false, reason: "hands_full" };
1087
+ const shoulder = deps.shoulder(hand);
1088
+ if (!shoulder || deps.body?.able?.() === false) return { ok: false, reason: "no_body" };
1089
+ const timeline = createGestureTimeline(start.gesture);
1090
+ going = { start, hand, timeline, progress: timeline.advance(0), walked: 0, there: false };
1091
+ if (shoulder[1] - start.at()[1] > STOOP_BELOW) deps.body?.stoop?.(true);
1092
+ return { ok: true, hand };
1093
+ },
1094
+ step(dt) {
1095
+ if (!going) return;
1096
+ const now = going;
1097
+ if (deps.body?.able?.() === false) return stop();
1098
+ if (!now.there) {
1099
+ now.there = stepUp(deps.body, now, dt);
1100
+ if (!now.there) return;
1101
+ }
1102
+ now.progress = now.timeline.advance(dt);
1103
+ if (now.progress.acted) now.start.onAct();
1104
+ if (now.progress.done && going === now) stop();
1105
+ },
1106
+ targets(base) {
1107
+ if (!going) return base;
1108
+ const { hand, start, progress } = going;
1109
+ const shoulder = deps.shoulder(hand);
1110
+ if (!shoulder) return base;
1111
+ const rest = base?.[hand] ?? deps.restOf(hand);
1112
+ const worked = gestureHandPose({ gesture: start.gesture, hand, phase: progress.phase, progress: progress.progress, shoulder, rest, at: start.at(), yaw: deps.yaw() });
1113
+ const [x, y, z] = worked.pose.position;
1114
+ const [qx, qy, qz, qw] = worked.pose.quaternion;
1115
+ const pose = { position: [x, y, z], quaternion: [qx, qy, qz, qw] };
1116
+ 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 };
1117
+ },
1118
+ cancel: stop
1119
+ };
1120
+ }
692
1121
  export {
693
1122
  BELT_SLOT,
694
1123
  DOOR_ARM_REACH,
695
1124
  DOOR_SHOULDER_HEIGHT,
696
1125
  DOOR_USE_DEFAULTS,
1126
+ DRIVING_HANDS_DEFAULTS,
697
1127
  FURNITURE_PUSH_DEFAULTS,
1128
+ GESTURE_ARM_REACH,
1129
+ GESTURE_TIMING,
1130
+ HANDS_BY_HANDLE,
698
1131
  HANDS_DEFAULTS,
699
1132
  HANDS_TIMING,
700
1133
  ITEM_GRIP,
@@ -708,6 +1141,7 @@ export {
708
1141
  SLING_SLOT,
709
1142
  SLOT_FALLBACKS,
710
1143
  STEP_POSE_DEFAULTS,
1144
+ VEHICLE_BOARDING_DEFAULTS,
711
1145
  WALKER_JUMP_SPEED,
712
1146
  advanceNeeds,
713
1147
  applyItemEffect,
@@ -717,7 +1151,10 @@ export {
717
1151
  conjugateQuaternion,
718
1152
  createBodyVitals,
719
1153
  createDoorUse,
1154
+ createDrivingHands,
720
1155
  createFurniturePush,
1156
+ createGestureHands,
1157
+ createGestureTimeline,
721
1158
  createNeedsState,
722
1159
  createPhysicsController,
723
1160
  createPlayer,
@@ -725,6 +1162,7 @@ export {
725
1162
  createRng,
726
1163
  createStepPose,
727
1164
  createTakeTimeline,
1165
+ createVehicleBoarding,
728
1166
  createWalker,
729
1167
  deriveVitals,
730
1168
  dragMarkFor,
@@ -732,8 +1170,12 @@ export {
732
1170
  drawWound,
733
1171
  easeInOut,
734
1172
  footRestHeight,
1173
+ freeHandOf,
735
1174
  frictionFor,
1175
+ gestureHandOf,
1176
+ gestureHandPose,
736
1177
  gripOfItem,
1178
+ gripOnHandle,
737
1179
  gripSecondsFor,
738
1180
  handPoseOnItem,
739
1181
  interactionHandsFor,