@adatechnology/game-player 0.12.2 → 0.13.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.
@@ -21,6 +21,7 @@ var GamePlayer = (() => {
21
21
  // src/index.ts
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
+ BELT_SLOT: () => BELT_SLOT,
24
25
  DOOR_ARM_REACH: () => DOOR_ARM_REACH,
25
26
  DOOR_SHOULDER_HEIGHT: () => DOOR_SHOULDER_HEIGHT,
26
27
  DOOR_USE_DEFAULTS: () => DOOR_USE_DEFAULTS,
@@ -28,20 +29,27 @@ var GamePlayer = (() => {
28
29
  HANDS_DEFAULTS: () => HANDS_DEFAULTS,
29
30
  HANDS_TIMING: () => HANDS_TIMING,
30
31
  ITEM_GRIP: () => ITEM_GRIP,
32
+ LIFT_DEFAULTS: () => LIFT_DEFAULTS,
33
+ NEEDS_DEFAULTS: () => NEEDS_DEFAULTS,
31
34
  PLAYER_DEFAULTS: () => PLAYER_DEFAULTS,
32
35
  PLAYER_MASS_KG: () => PLAYER_MASS_KG,
33
36
  PLAYER_PUSH_STRENGTH_NEWTONS: () => PLAYER_PUSH_STRENGTH_NEWTONS,
34
37
  PLAYER_TEXT: () => PLAYER_TEXT,
38
+ PLAYER_WALK_PUSH_NEWTONS: () => PLAYER_WALK_PUSH_NEWTONS,
35
39
  SLING_SLOT: () => SLING_SLOT,
36
40
  SLOT_FALLBACKS: () => SLOT_FALLBACKS,
37
41
  STEP_POSE_DEFAULTS: () => STEP_POSE_DEFAULTS,
38
42
  WALKER_JUMP_SPEED: () => WALKER_JUMP_SPEED,
43
+ advanceNeeds: () => advanceNeeds,
44
+ applyItemEffect: () => applyItemEffect,
39
45
  blendPoses: () => blendPoses,
40
46
  canPush: () => canPush,
41
47
  composePose: () => composePose,
42
48
  conjugateQuaternion: () => conjugateQuaternion,
49
+ createBodyVitals: () => createBodyVitals,
43
50
  createDoorUse: () => createDoorUse,
44
51
  createFurniturePush: () => createFurniturePush,
52
+ createNeedsState: () => createNeedsState,
45
53
  createPhysicsController: () => createPhysicsController,
46
54
  createPlayer: () => createPlayer,
47
55
  createPlayerTestModule: () => createPlayerTestModule,
@@ -54,13 +62,18 @@ var GamePlayer = (() => {
54
62
  drawBackOf: () => drawBackOf,
55
63
  drawWound: () => drawWound,
56
64
  easeInOut: () => easeInOut,
65
+ footRestHeight: () => footRestHeight,
57
66
  frictionFor: () => frictionFor,
58
67
  gripOfItem: () => gripOfItem,
59
68
  gripSecondsFor: () => gripSecondsFor,
60
69
  handPoseOnItem: () => handPoseOnItem,
61
70
  interactionHandsFor: () => interactionHandsFor,
71
+ isStarving: () => isStarving,
62
72
  itemPoseInHand: () => itemPoseInHand,
73
+ liftCheckFor: () => liftCheckFor,
63
74
  multiplyQuaternions: () => multiplyQuaternions,
75
+ needsHoursOf: () => needsHoursOf,
76
+ nourish: () => nourish,
64
77
  planDrawSequence: () => planDrawSequence,
65
78
  planHands: () => planHands,
66
79
  planTakeSequence: () => planTakeSequence,
@@ -245,7 +258,20 @@ var GamePlayer = (() => {
245
258
  derived: "G 62005-62012 DERIV",
246
259
  wounds: "G 62016 FERIDA (Personagens 3.7): share of each wound kind, and its infection chance (used only when an infection rule is on)",
247
260
  infection: "G 62015 INFEC (the slice's accelerated clock: fever 30 s, delirium 70 s, death 120 s, reanimation 8 s after); G 68509 fever noise",
248
- numbers: "G 68427-68640 player, playerUpdate, frontZombies, shove, knife; G 68678-68681 the aim spread modifiers; G 65507 walkCad"
261
+ numbers: "G 68427-68640 player, playerUpdate, frontZombies, shove, knife; G 68678-68681 the aim spread modifiers; G 65507 walkCad",
262
+ needs: "G 57255 needs (fome 20, sede 20 at the start; 0 is fed, 100 starving), G 57271 useItem (food takes off its f, drink its s), G 57341-57344 (1 game hour = 50 s of the slice; hunger 2.5/h, 4/h running, thirst 4/h, times 1.1 - 0.02 x vigor; over 90 the body loses 0.2 health a second)"
263
+ },
264
+ needs: {
265
+ start: { hunger: 20, thirst: 20 },
266
+ max: 100,
267
+ gameHourSeconds: 50,
268
+ hungerPerHour: 2.5,
269
+ hungerRunningPerHour: 4,
270
+ thirstPerHour: 4,
271
+ vigorBase: 1.1,
272
+ vigorPerPoint: -0.02,
273
+ starvingAt: 90,
274
+ starvingDamagePerSecond: 0.2
249
275
  },
250
276
  attributes: { strength: 5, vigor: 5, agility: 5, perception: 5, intellect: 5, presence: 5 },
251
277
  derived: {
@@ -372,6 +398,26 @@ var GamePlayer = (() => {
372
398
  fist: { damage: 8, crit: 0, reach: 0.5, noise: 1 }
373
399
  };
374
400
 
401
+ // src/needs.ts
402
+ var NEEDS_DEFAULTS = defaults_default.needs;
403
+ var createNeedsState = (rules = NEEDS_DEFAULTS) => ({ hunger: rules.start.hunger, thirst: rules.start.thirst });
404
+ var clamp = (value, max) => value < 0 ? 0 : value > max ? max : value;
405
+ var needsHoursOf = (dt, vigor, rules = NEEDS_DEFAULTS) => Math.max(0, dt) / rules.gameHourSeconds * Math.max(0, rules.vigorBase + rules.vigorPerPoint * vigor);
406
+ var isStarving = (state, rules = NEEDS_DEFAULTS) => state.hunger > rules.starvingAt || state.thirst > rules.starvingAt;
407
+ function advanceNeeds(state, dt, body, rules = NEEDS_DEFAULTS) {
408
+ const hours = needsHoursOf(dt, body.vigor, rules);
409
+ state.hunger = clamp(state.hunger + (body.running ? rules.hungerRunningPerHour : rules.hungerPerHour) * hours, rules.max);
410
+ state.thirst = clamp(state.thirst + rules.thirstPerHour * hours, rules.max);
411
+ return isStarving(state, rules) ? rules.starvingDamagePerSecond * Math.max(0, dt) : 0;
412
+ }
413
+ function nourish(state, food, rules = NEEDS_DEFAULTS) {
414
+ const hunger = state.hunger;
415
+ const thirst = state.thirst;
416
+ state.hunger = clamp(hunger - (food.satiety ?? 0), rules.max);
417
+ state.thirst = clamp(thirst - (food.hydration ?? 0), rules.max);
418
+ return { hunger: state.hunger - hunger, thirst: state.thirst - thirst };
419
+ }
420
+
375
421
  // src/rng.ts
376
422
  function createRng(seed) {
377
423
  let state = seed >>> 0;
@@ -383,6 +429,66 @@ var GamePlayer = (() => {
383
429
  };
384
430
  }
385
431
 
432
+ // src/vitals.ts
433
+ var NOTHING = { applied: false, health: 0, hunger: 0, thirst: 0, stoppedBleeding: false, calmed: false };
434
+ var numberOf = (value) => typeof value === "number" && Number.isFinite(value) ? value : 0;
435
+ function applyItemEffect(body, effect, limits) {
436
+ switch (effect.id) {
437
+ case "consume_nutrition": {
438
+ const moved = nourish(body, { satiety: numberOf(effect.satiety), hydration: numberOf(effect.hydration) }, limits.needs ?? NEEDS_DEFAULTS);
439
+ return { ...NOTHING, applied: true, hunger: moved.hunger, thirst: moved.thirst };
440
+ }
441
+ case "heal": {
442
+ const before = body.health;
443
+ body.health = Math.min(limits.maxHealth, before + Math.max(0, numberOf(effect.params?.amount)));
444
+ return { ...NOTHING, applied: true, health: body.health - before };
445
+ }
446
+ case "stop_bleeding": {
447
+ const stopped = body.bleeding > 0;
448
+ body.bleeding = 0;
449
+ return { ...NOTHING, applied: true, stoppedBleeding: stopped };
450
+ }
451
+ case "clean_wound": {
452
+ const cleaned = body.wounds.pop();
453
+ return cleaned === void 0 ? NOTHING : { ...NOTHING, applied: true, cleaned };
454
+ }
455
+ case "calm": {
456
+ const calmed = body.panic > 0;
457
+ body.panic = 0;
458
+ return { ...NOTHING, applied: true, calmed };
459
+ }
460
+ default:
461
+ return NOTHING;
462
+ }
463
+ }
464
+ function createBodyVitals(options = {}) {
465
+ const maxHealth = options.maxHealth ?? 100;
466
+ const bleedPerSecond = options.bleedPerSecond ?? 0.15;
467
+ const vigor = options.vigor ?? 5;
468
+ const rules = { ...NEEDS_DEFAULTS, ...options.needs, start: { ...NEEDS_DEFAULTS.start, ...options.needs?.start } };
469
+ const fresh = () => ({ ...createNeedsState(rules), health: maxHealth, bleeding: 0, panic: 0, wounds: [], alive: true, cause: "" });
470
+ const state = fresh();
471
+ return {
472
+ state,
473
+ maxHealth,
474
+ rules,
475
+ update(dt, input = {}) {
476
+ if (!state.alive || dt <= 0) return;
477
+ state.health -= advanceNeeds(state, dt, { running: input.running ?? false, vigor }, rules);
478
+ if (state.bleeding > 0) state.health -= bleedPerSecond * dt;
479
+ if (state.health <= 0) {
480
+ state.health = 0;
481
+ state.alive = false;
482
+ state.cause = state.bleeding > 0 ? "bled_out" : "hunger_thirst";
483
+ }
484
+ },
485
+ apply: (effect) => state.alive ? applyItemEffect(state, effect, { maxHealth, needs: rules }) : NOTHING,
486
+ reset() {
487
+ Object.assign(state, fresh());
488
+ }
489
+ };
490
+ }
491
+
386
492
  // src/player.ts
387
493
  var PLAYER_DEFAULTS = defaults_default;
388
494
  var TAU_APPROX = 6.2832;
@@ -406,9 +512,9 @@ var GamePlayer = (() => {
406
512
  }
407
513
  return kinds[kinds.length - 1];
408
514
  };
409
- var clamp = (v, a, b) => v < a ? a : v > b ? b : v;
515
+ var clamp2 = (v, a, b) => v < a ? a : v > b ? b : v;
410
516
  var lerp = (a, b, t) => a + (b - a) * t;
411
- var freshState = (derived, position) => ({
517
+ var freshState = (derived, position, needs) => ({
412
518
  x: position?.x ?? 0,
413
519
  z: position?.z ?? 0,
414
520
  y: 0,
@@ -420,6 +526,8 @@ var GamePlayer = (() => {
420
526
  turned: false,
421
527
  health: derived.maxHealth,
422
528
  stamina: derived.maxStamina,
529
+ hunger: needs?.start.hunger ?? 0,
530
+ thirst: needs?.start.thirst ?? 0,
423
531
  crouch: false,
424
532
  running: false,
425
533
  stepTimer: 0,
@@ -460,9 +568,10 @@ var GamePlayer = (() => {
460
568
  const heightScale = heightMetres / numbers.heightMetres;
461
569
  const wounds = options.wounds ?? PLAYER_DEFAULTS.wounds;
462
570
  const infection = options.infection === void 0 || options.infection === false ? null : options.infection === true ? PLAYER_DEFAULTS.infection : { ...PLAYER_DEFAULTS.infection, ...options.infection };
571
+ const needs = options.needs === false ? null : options.needs === void 0 || options.needs === true ? NEEDS_DEFAULTS : { ...NEEDS_DEFAULTS, ...options.needs, start: { ...NEEDS_DEFAULTS.start, ...options.needs.start } };
463
572
  const fist = options.fist ?? PLAYER_DEFAULTS.fist;
464
573
  const listeners = /* @__PURE__ */ new Map();
465
- let P = freshState(derived, options.position);
574
+ let P = freshState(derived, options.position, needs);
466
575
  const feet = new Float64Array(3);
467
576
  let defaultWorld = options.world ?? {};
468
577
  let world = defaultWorld;
@@ -633,6 +742,7 @@ var GamePlayer = (() => {
633
742
  message("bandaged", 2);
634
743
  }
635
744
  }
745
+ if (needs) P.health -= advanceNeeds(P, dt, { running: P.running, vigor: attributes.vigor }, needs);
636
746
  if (P.health <= 0) {
637
747
  die(P.infected ? "infected" : P.bleeding ? "bled_out" : "hunger_thirst");
638
748
  return;
@@ -792,7 +902,7 @@ var GamePlayer = (() => {
792
902
  const lx = P.vx * cy - P.vz * sy;
793
903
  const lz = P.vx * sy + P.vz * cy;
794
904
  const back = lz < numbers.backwardsBelow ? -1 : 1;
795
- const hip = spd > numbers.hipMinSpeed ? clamp(Math.atan2(lx * back, lz * back), -numbers.hipLimit, numbers.hipLimit) : 0;
905
+ const hip = spd > numbers.hipMinSpeed ? clamp2(Math.atan2(lx * back, lz * back), -numbers.hipLimit, numbers.hipLimit) : 0;
796
906
  P.hip = P.hip + (hip - P.hip) * Math.min(1, dt * numbers.hipFollow);
797
907
  P.phase += dt * (spd < numbers.cadenceMinSpeed ? 0 : numbers.cadenceBase + numbers.cadencePerSpeed * spd) * back;
798
908
  }
@@ -827,7 +937,7 @@ var GamePlayer = (() => {
827
937
  const n = crowd.count();
828
938
  for (let i = 0; i < n; i++) if (crowd.active(i) && crowd.chasing(i)) near += crowd.panicWeight?.(i) ?? 1;
829
939
  }
830
- P.panic = clamp(P.panic + (near * numbers.panicGain - (near ? 0 : numbers.panicDecay)) * dt, 0, numbers.panicMax);
940
+ P.panic = clamp2(P.panic + (near * numbers.panicGain - (near ? 0 : numbers.panicDecay)) * dt, 0, numbers.panicMax);
831
941
  };
832
942
  return {
833
943
  get state() {
@@ -842,6 +952,8 @@ var GamePlayer = (() => {
842
952
  derived,
843
953
  numbers,
844
954
  infection,
955
+ needs,
956
+ use: (effect) => P.alive ? applyItemEffect(P, effect, { maxHealth: derived.maxHealth, ...needs ? { needs } : {} }) : { applied: false, health: 0, hunger: 0, thirst: 0, stoppedBleeding: false, calmed: false },
845
957
  update,
846
958
  hurt: (amount) => hurt(amount),
847
959
  bite: () => bite(),
@@ -874,6 +986,8 @@ var GamePlayer = (() => {
874
986
  save: () => ({ state: structuredClone(P), attributes }),
875
987
  load(save) {
876
988
  P = structuredClone(save.state);
989
+ P.hunger ?? (P.hunger = needs?.start.hunger ?? 0);
990
+ P.thirst ?? (P.thirst = needs?.start.thirst ?? 0);
877
991
  world.controller?.teleport(P.x, P.y, P.z);
878
992
  },
879
993
  setWorld(next) {
@@ -885,6 +999,7 @@ var GamePlayer = (() => {
885
999
  // src/physics-controller.ts
886
1000
  var PLAYER_MASS_KG = 75;
887
1001
  var PLAYER_PUSH_STRENGTH_NEWTONS = 400;
1002
+ var PLAYER_WALK_PUSH_NEWTONS = 100;
888
1003
  var createPhysicsController = (world, options) => {
889
1004
  const { radiusMetres, heightMetres } = options.collider;
890
1005
  const id = world.createCharacter({
@@ -905,6 +1020,22 @@ var GamePlayer = (() => {
905
1020
  };
906
1021
  };
907
1022
 
1023
+ // src/foot-rest.ts
1024
+ var RINGS = 32;
1025
+ var SAMPLES_PER_RING = 48;
1026
+ var footRestHeight = (heightAt, x, z, radius) => {
1027
+ let rest = heightAt(x, z);
1028
+ for (let ring = 1; ring <= RINGS; ring++) {
1029
+ const distance = radius * ring / RINGS;
1030
+ const lift = radius - Math.sqrt(radius * radius - distance * distance);
1031
+ for (let sample = 0; sample < SAMPLES_PER_RING; sample++) {
1032
+ const angle = 2 * Math.PI * sample / SAMPLES_PER_RING;
1033
+ rest = Math.max(rest, heightAt(x + distance * Math.cos(angle), z + distance * Math.sin(angle)) - lift);
1034
+ }
1035
+ }
1036
+ return rest;
1037
+ };
1038
+
908
1039
  // src/walker.ts
909
1040
  var WALKER_JUMP_SPEED = 3.99;
910
1041
  var STANCE_HEIGHT_RATIO = { standing: 1, crouched: 2 / 3, prone: 1 / 3 };
@@ -1747,7 +1878,8 @@ var GamePlayer = (() => {
1747
1878
 
1748
1879
  // src/hands.ts
1749
1880
  var HANDS_DEFAULTS = { oneHand: { maxKg: 4, maxLength: 0.7 } };
1750
- var SLOT_FALLBACKS = { sling: ["back"] };
1881
+ var SLOT_FALLBACKS = { sling: ["back"], hip: ["belt"] };
1882
+ var BELT_SLOT = "belt";
1751
1883
  var SLING_SLOT = "sling";
1752
1884
  function interactionHandsFor(thing, rules = HANDS_DEFAULTS) {
1753
1885
  const longest = Math.max(0, ...thing.size ?? []);
@@ -1759,7 +1891,8 @@ var GamePlayer = (() => {
1759
1891
  const free = [...params.slots];
1760
1892
  const fallbacks = params.fallbacks ?? SLOT_FALLBACKS;
1761
1893
  const stow = (hand, weapon) => {
1762
- const tried = weapon.slot === null ? [] : [weapon.slot, ...fallbacks[weapon.slot] ?? []];
1894
+ const own = weapon.slot === null ? [] : [weapon.slot, ...fallbacks[weapon.slot] ?? []];
1895
+ const tried = weapon.hands === 1 && !own.includes(BELT_SLOT) ? [...own, BELT_SLOT] : own;
1763
1896
  const slot = tried.find((name) => free.includes(name));
1764
1897
  if (slot === void 0) return { hand, weapon: weapon.id, move: "drop" };
1765
1898
  free.splice(free.indexOf(slot), 1);
@@ -1777,6 +1910,18 @@ var GamePlayer = (() => {
1777
1910
  return plan.moves.filter((move) => move.move === "holster").map((move) => ({ hand: move.hand, weapon: move.weapon }));
1778
1911
  }
1779
1912
 
1913
+ // src/lift.ts
1914
+ var LIFT_DEFAULTS = { maxKg: 60, closeReach: 0.25, bodyDepth: 0.15, maxSpan: 1.3 };
1915
+ function liftCheckFor(thing, rules = {}) {
1916
+ const r = { ...LIFT_DEFAULTS, ...rules };
1917
+ const [, middle = 0, thinnest = 0] = [...thing.size ?? []].sort((a, b) => b - a);
1918
+ const reach = r.bodyDepth + Math.max(0, thinnest) / 2;
1919
+ const liftableKg = r.maxKg * Math.min(1, r.closeReach / Math.max(reach, 1e-6));
1920
+ if (thing.massKg > r.maxKg) return { verdict: "too_heavy", liftableKg };
1921
+ if (middle > r.maxSpan || thing.massKg > liftableKg) return { verdict: "too_bulky", liftableKg };
1922
+ return { verdict: "ok", liftableKg };
1923
+ }
1924
+
1780
1925
  // src/hand-math.ts
1781
1926
  function quaternionOfBasis(x, y, z) {
1782
1927
  const [m00, m10, m20] = [x[0] ?? 0, x[1] ?? 0, x[2] ?? 0];