@adatechnology/game-contracts 0.20.0 → 0.22.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,13 +21,28 @@ var GameContracts = (() => {
21
21
  // src/index.ts
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
+ BLOW_IDS: () => BLOW_IDS,
25
+ BODY_FOLEY_IDS: () => BODY_FOLEY_IDS,
26
+ BODY_SOUND_IDS: () => BODY_SOUND_IDS,
24
27
  DEFAULT_BLAST_SHOVE: () => DEFAULT_BLAST_SHOVE,
25
28
  DEFAULT_ENVIRONMENT: () => DEFAULT_ENVIRONMENT,
26
29
  DEFAULT_IGNITION_RULES: () => DEFAULT_IGNITION_RULES,
30
+ DEFAULT_SURFACE: () => DEFAULT_SURFACE,
31
+ DEFERRED_PRIORITY: () => DEFERRED_PRIORITY,
32
+ DEFERRED_QUEUE_ID: () => DEFERRED_QUEUE_ID,
33
+ FOOTSTEP_SOUND_IDS: () => FOOTSTEP_SOUND_IDS,
34
+ FRAME_PHASES: () => FRAME_PHASES,
35
+ GAIT_IDS: () => GAIT_IDS,
36
+ HEAVY_LANDING_SPEED: () => HEAVY_LANDING_SPEED,
27
37
  IMPACT_CONTRACT: () => IMPACT_CONTRACT,
28
38
  KIT_ORIGIN_KEY: () => KIT_ORIGIN_KEY,
39
+ LANDING_IDS: () => LANDING_IDS,
40
+ LAND_SOUND_IDS: () => LAND_SOUND_IDS,
41
+ MATERIAL_SURFACES: () => MATERIAL_SURFACES,
29
42
  MISSING_BITS: () => MISSING_BITS,
30
43
  STAGE_EVENT: () => STAGE_EVENT,
44
+ SURFACE_IDS: () => SURFACE_IDS,
45
+ SWING_LEAD_SECONDS: () => SWING_LEAD_SECONDS,
31
46
  TOOL_CLIP: () => TOOL_CLIP,
32
47
  TWIST_LIFT: () => TWIST_LIFT,
33
48
  applyPatches: () => applyPatches,
@@ -35,6 +50,7 @@ var GameContracts = (() => {
35
50
  checkAnimation: () => checkAnimation,
36
51
  checkBench: () => checkBench,
37
52
  checkCategorized: () => checkCategorized,
53
+ checkDeferredQueue: () => checkDeferredQueue,
38
54
  checkDestructible: () => checkDestructible,
39
55
  checkHit: () => checkHit,
40
56
  checkSpec: () => checkSpec,
@@ -45,11 +61,16 @@ var GameContracts = (() => {
45
61
  defineCategories: () => defineCategories,
46
62
  fillFactor: () => fillFactor,
47
63
  footprintBlocker: () => footprintBlocker,
64
+ footstepSoundId: () => footstepSoundId,
65
+ hitSoundId: () => hitSoundId,
48
66
  ignitionChance: () => ignitionChance,
67
+ isSurfaceId: () => isSurfaceId,
49
68
  kitOriginOf: () => kitOriginOf,
69
+ landSoundId: () => landSoundId,
50
70
  markKitOrigin: () => markKitOrigin,
51
71
  mergeById: () => mergeById,
52
72
  removeIds: () => removeIds,
73
+ resetWarnOnce: () => resetWarnOnce2,
53
74
  resolveEntry: () => resolveEntry,
54
75
  resolveIgnitionRules: () => resolveIgnitionRules,
55
76
  sampleClip: () => sampleClip,
@@ -59,10 +80,13 @@ var GameContracts = (() => {
59
80
  shotExtras: () => shotExtras,
60
81
  shotToHit: () => shotToHit,
61
82
  sparkTargetFactor: () => sparkTargetFactor,
83
+ surfaceOfMaterial: () => surfaceOfMaterial,
84
+ swingSoundId: () => swingSoundId,
62
85
  toolAnimationStep: () => toolAnimationStep,
63
86
  twistStroke: () => twistStroke,
64
87
  unmarkedDrawings: () => unmarkedDrawings,
65
- useToHit: () => useToHit
88
+ useToHit: () => useToHit,
89
+ warnOnce: () => warnOnce
66
90
  });
67
91
 
68
92
  // src/blast-shove.ts
@@ -83,6 +107,30 @@ var GameContracts = (() => {
83
107
  return { speed, direction: [direction[0] / length, direction[1] / length, direction[2] / length], distance, falloff };
84
108
  }
85
109
 
110
+ // src/impact-cost.ts
111
+ var DEFAULT_COST_READS = 1e4;
112
+ var DEFAULT_COST_CEILING_MS = 50;
113
+ var DEFAULT_COST_ALLOCATION_BYTES = 1024 * 1024;
114
+ var heapUsedBytes = () => {
115
+ const nodeProcess = globalThis.process;
116
+ return nodeProcess?.memoryUsage?.().heapUsed;
117
+ };
118
+ var checkReadCost = ({ name, read, options, problems }) => {
119
+ const reads = options.reads ?? DEFAULT_COST_READS;
120
+ const ceilingMs = options.ceilingMs ?? DEFAULT_COST_CEILING_MS;
121
+ const allocationBytes = options.allocationBytes ?? DEFAULT_COST_ALLOCATION_BYTES;
122
+ if (read() !== read()) problems.push(`${name}() returns a new object on each read: cache it between changes (cost contract)`);
123
+ for (let i = 0; i < reads; i++) read();
124
+ const heapBefore = heapUsedBytes();
125
+ const started = performance.now();
126
+ for (let i = 0; i < reads; i++) read();
127
+ const elapsedMs = performance.now() - started;
128
+ const heapAfter = heapUsedBytes();
129
+ if (elapsedMs > ceilingMs) problems.push(`${name}() took ${elapsedMs.toFixed(1)} ms for ${reads} reads, over the ${ceilingMs} ms ceiling (cost contract)`);
130
+ if (heapBefore !== void 0 && heapAfter !== void 0 && heapAfter - heapBefore > allocationBytes)
131
+ problems.push(`${name}() grew the heap by ${heapAfter - heapBefore} bytes over ${reads} reads, over the ${allocationBytes} byte ceiling (cost contract)`);
132
+ };
133
+
86
134
  // src/impact.ts
87
135
  var IMPACT_CONTRACT = {
88
136
  ballistic: { required: ["type", "point", "direction", "energy", "caliber"], optional: ["space", "source"], note: "bullets and pellets: joules and diameter in millimetres" },
@@ -189,6 +237,15 @@ var GameContracts = (() => {
189
237
  } catch (e) {
190
238
  problems.push(`reset() threw: ${String(e)}`);
191
239
  }
240
+ if (options.cost) {
241
+ const cost = options.cost === true ? {} : options.cost;
242
+ try {
243
+ checkReadCost({ name: "state", read: () => impl.state(), options: cost, problems });
244
+ checkReadCost({ name: "describe", read: () => impl.describe(), options: cost, problems });
245
+ } catch (e) {
246
+ problems.push(`cost measurement threw: ${String(e)}`);
247
+ }
248
+ }
192
249
  return { ok: problems.length === 0, problems };
193
250
  };
194
251
 
@@ -664,6 +721,66 @@ var GameContracts = (() => {
664
721
  };
665
722
  };
666
723
 
724
+ // src/frame.ts
725
+ var FRAME_PHASES = ["input", "fixedSimulate", "lateSimulate", "present", "render", "idle"];
726
+ var DEFERRED_QUEUE_ID = {
727
+ DESTRUCTION_EVENTS: "destruction.events",
728
+ BREACH_PAINT: "floorplan.breach-paint",
729
+ AUDIO_VOICES: "audio.voices",
730
+ VEHICLE_CREASE: "vehicles.crease"
731
+ };
732
+ var DEFERRED_PRIORITY = {
733
+ [DEFERRED_QUEUE_ID.DESTRUCTION_EVENTS]: { priority: 300, phase: "present" },
734
+ [DEFERRED_QUEUE_ID.BREACH_PAINT]: { priority: 250, phase: "present" },
735
+ [DEFERRED_QUEUE_ID.AUDIO_VOICES]: { priority: 200, phase: "idle", reserveMs: 1 },
736
+ [DEFERRED_QUEUE_ID.VEHICLE_CREASE]: { priority: 100, phase: "idle" }
737
+ };
738
+ function checkDeferredQueue(queue, fill) {
739
+ const problems = [];
740
+ fill();
741
+ const waiting = queue.pending(1);
742
+ if (waiting < 2) problems.push(`fill() must leave at least 2 units waiting (pending is ${waiting})`);
743
+ if (queue.pending(1) !== waiting) problems.push("pending(frame) changed between two calls in the same frame");
744
+ const ran = queue.run(0, () => 1);
745
+ if (ran !== 1) problems.push(`run with a deadline already past must run exactly one unit (ran ${ran})`);
746
+ if (waiting >= 2 && queue.pending(2) !== waiting - 1) problems.push(`one unit ran but pending went from ${waiting} to ${queue.pending(2)}`);
747
+ try {
748
+ queue.attach?.();
749
+ queue.attach?.();
750
+ queue.detach?.();
751
+ queue.detach?.();
752
+ } catch (error) {
753
+ problems.push(`attach/detach must be idempotent: ${error instanceof Error ? error.message : String(error)}`);
754
+ }
755
+ queue.flush();
756
+ if (queue.pending(3) !== 0) problems.push(`flush must leave nothing pending (pending is ${queue.pending(3)})`);
757
+ if (queue.run(0, () => 1) !== 0) problems.push("run on an empty queue must run no unit");
758
+ return { ok: problems.length === 0, problems };
759
+ }
760
+
761
+ // src/warn.ts
762
+ var WARNED_KEY = /* @__PURE__ */ Symbol.for("@adatechnology/game-contracts.warned");
763
+ var warnedKeys = () => {
764
+ const holder = globalThis;
765
+ return holder[WARNED_KEY] ?? (holder[WARNED_KEY] = /* @__PURE__ */ new Set());
766
+ };
767
+ var isProduction = () => {
768
+ try {
769
+ return process.env.NODE_ENV === "production";
770
+ } catch {
771
+ return false;
772
+ }
773
+ };
774
+ var isQuiet = () => globalThis.__GAME_KIT_QUIET__ === true;
775
+ var warnOnce = (key, message) => {
776
+ if (isQuiet() || isProduction()) return;
777
+ const seen = warnedKeys();
778
+ if (seen.has(key)) return;
779
+ seen.add(key);
780
+ console.warn(`[game-kit] ${message}`);
781
+ };
782
+ var resetWarnOnce = () => warnedKeys().clear();
783
+
667
784
  // src/registry.ts
668
785
  var UNSAFE_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
669
786
  var isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype;
@@ -686,10 +803,13 @@ var GameContracts = (() => {
686
803
  for (const [id, patch] of Object.entries(patches ?? {})) {
687
804
  const entry = entries.get(id);
688
805
  if (entry) entries.set(id, deepMerge(entry, patch));
806
+ else warnOnce(`registry.patch.${id}`, `patch for "${id}" ignored: no entry has that id`);
689
807
  }
690
808
  };
691
809
  var removeIds = (entries, ids) => {
692
- for (const id of ids ?? []) entries.delete(id);
810
+ for (const id of ids ?? []) {
811
+ if (!entries.delete(id)) warnOnce(`registry.remove.${id}`, `remove of "${id}" ignored: no entry has that id`);
812
+ }
693
813
  };
694
814
 
695
815
  // src/bench.ts
@@ -928,6 +1048,112 @@ var GameContracts = (() => {
928
1048
  return lx > fp.minX - radius && lx < fp.maxX + radius && lz > fp.minZ - radius && lz < fp.maxZ + radius;
929
1049
  };
930
1050
  }
1051
+
1052
+ // src/foley.ts
1053
+ var SURFACE_IDS = ["concrete", "asphalt", "wood", "tile", "dirt", "grass", "gravel", "sand", "metal", "water", "glass", "carpet"];
1054
+ var DEFAULT_SURFACE = "concrete";
1055
+ var GAIT_IDS = ["walk", "run"];
1056
+ var LANDING_IDS = ["light", "heavy"];
1057
+ var HEAVY_LANDING_SPEED = 5.5;
1058
+ var BLOW_IDS = ["punch", "kick", "shove", "blade", "blunt", "chop"];
1059
+ var BODY_SOUND_IDS = [
1060
+ "jump",
1061
+ "cloth_rustle",
1062
+ "body_fall",
1063
+ "fall_damage",
1064
+ "player_hurt",
1065
+ "player_breath_run",
1066
+ "player_death",
1067
+ "bite",
1068
+ "grab",
1069
+ "punch_swing",
1070
+ "punch_hit_body",
1071
+ "punch_hit_face",
1072
+ "punch_hit_object",
1073
+ "kick_swing",
1074
+ "kick_hit",
1075
+ "kick_hit_object",
1076
+ "shove_two_hands",
1077
+ "knife_swing",
1078
+ "knife_stab",
1079
+ "knife_slash",
1080
+ "knife_hit_object",
1081
+ "blunt_swing",
1082
+ "blunt_hit",
1083
+ "blunt_hit_object",
1084
+ "axe_swing",
1085
+ "axe_chop",
1086
+ "axe_hit_object"
1087
+ ];
1088
+ var footstepSoundId = (surface, gait) => `footstep_${surface}_${gait}`;
1089
+ var landSoundId = (surface, fallSpeed) => `land_${surface}_${fallSpeed >= HEAVY_LANDING_SPEED ? "heavy" : "light"}`;
1090
+ var FOOTSTEP_SOUND_IDS = SURFACE_IDS.flatMap((surface) => GAIT_IDS.map((gait) => footstepSoundId(surface, gait)));
1091
+ var LAND_SOUND_IDS = SURFACE_IDS.flatMap((surface) => LANDING_IDS.map((landing) => `land_${surface}_${landing}`));
1092
+ var BODY_FOLEY_IDS = [...FOOTSTEP_SOUND_IDS, ...LAND_SOUND_IDS, ...BODY_SOUND_IDS];
1093
+ var SWING = {
1094
+ punch: "punch_swing",
1095
+ kick: "kick_swing",
1096
+ // two hands pushed out do not whistle: the shove is one sound, at the contact
1097
+ shove: null,
1098
+ blade: "knife_swing",
1099
+ blunt: "blunt_swing",
1100
+ chop: "axe_swing"
1101
+ };
1102
+ var SWING_LEAD_SECONDS = { punch: 0.07, kick: 0.09, shove: 0, blade: 0.05, blunt: 0.1, chop: 0.11 };
1103
+ var swingSoundId = (blow) => SWING[blow];
1104
+ var HIT = {
1105
+ punch: { body: "punch_hit_body", head: "punch_hit_face", object: "punch_hit_object" },
1106
+ kick: { body: "kick_hit", head: "kick_hit", object: "kick_hit_object" },
1107
+ shove: { body: "shove_two_hands", head: "shove_two_hands", object: "shove_two_hands" },
1108
+ blade: { body: "knife_stab", head: "knife_stab", object: "knife_hit_object" },
1109
+ blunt: { body: "blunt_hit", head: "blunt_hit", object: "blunt_hit_object" },
1110
+ chop: { body: "axe_chop", head: "axe_chop", object: "axe_hit_object" }
1111
+ };
1112
+ var hitSoundId = (blow, target) => HIT[blow][target];
1113
+ var MATERIAL_SURFACES = {
1114
+ concrete: "concrete",
1115
+ mortar: "concrete",
1116
+ stone: "concrete",
1117
+ brick: "concrete",
1118
+ plaster: "concrete",
1119
+ asphalt: "asphalt",
1120
+ wood_soft: "wood",
1121
+ wood_hard: "wood",
1122
+ green_wood: "wood",
1123
+ plywood: "wood",
1124
+ mdf: "wood",
1125
+ ceramic: "tile",
1126
+ plastic: "tile",
1127
+ soil: "dirt",
1128
+ wax: "dirt",
1129
+ flesh_fruit: "dirt",
1130
+ turf: "grass",
1131
+ foliage: "grass",
1132
+ cactus: "grass",
1133
+ gravel: "gravel",
1134
+ sand: "sand",
1135
+ steel: "metal",
1136
+ cast_iron: "metal",
1137
+ aluminium: "metal",
1138
+ copper: "metal",
1139
+ water: "water",
1140
+ glass: "glass",
1141
+ tempered_glass: "glass",
1142
+ fabric: "carpet",
1143
+ foam: "carpet",
1144
+ leather: "carpet",
1145
+ rubber: "carpet",
1146
+ paper: "carpet"
1147
+ };
1148
+ var isSurfaceId = (value) => typeof value === "string" && SURFACE_IDS.includes(value);
1149
+ function surfaceOfMaterial(material, table = MATERIAL_SURFACES, fallback = DEFAULT_SURFACE) {
1150
+ if (material === void 0) return fallback;
1151
+ if (isSurfaceId(material)) return material;
1152
+ return table[material] ?? fallback;
1153
+ }
1154
+
1155
+ // src/index.ts
1156
+ var resetWarnOnce2 = resetWarnOnce;
931
1157
  return __toCommonJS(index_exports);
932
1158
  })();
933
1159
  //# sourceMappingURL=index.global.js.map