@adatechnology/game-contracts 0.21.0 → 0.23.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,20 +21,40 @@ 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
+ KNOCKED_DOWN_ANGLE: () => KNOCKED_DOWN_ANGLE,
40
+ KNOCK_CHEST: () => KNOCK_CHEST,
41
+ KNOCK_DOWN_SPEED: () => KNOCK_DOWN_SPEED,
42
+ LANDING_IDS: () => LANDING_IDS,
43
+ LAND_SOUND_IDS: () => LAND_SOUND_IDS,
44
+ MATERIAL_SURFACES: () => MATERIAL_SURFACES,
29
45
  MISSING_BITS: () => MISSING_BITS,
30
46
  STAGE_EVENT: () => STAGE_EVENT,
47
+ SURFACE_IDS: () => SURFACE_IDS,
48
+ SWING_LEAD_SECONDS: () => SWING_LEAD_SECONDS,
31
49
  TOOL_CLIP: () => TOOL_CLIP,
32
50
  TWIST_LIFT: () => TWIST_LIFT,
33
51
  applyPatches: () => applyPatches,
34
52
  blastShove: () => blastShove,
53
+ bodyMass: () => bodyMass,
35
54
  checkAnimation: () => checkAnimation,
36
55
  checkBench: () => checkBench,
37
56
  checkCategorized: () => checkCategorized,
57
+ checkDeferredQueue: () => checkDeferredQueue,
38
58
  checkDestructible: () => checkDestructible,
39
59
  checkHit: () => checkHit,
40
60
  checkSpec: () => checkSpec,
@@ -45,11 +65,18 @@ var GameContracts = (() => {
45
65
  defineCategories: () => defineCategories,
46
66
  fillFactor: () => fillFactor,
47
67
  footprintBlocker: () => footprintBlocker,
68
+ footstepSoundId: () => footstepSoundId,
69
+ hitSoundId: () => hitSoundId,
48
70
  ignitionChance: () => ignitionChance,
71
+ isSurfaceId: () => isSurfaceId,
49
72
  kitOriginOf: () => kitOriginOf,
73
+ knockOf: () => knockOf,
74
+ landSoundId: () => landSoundId,
75
+ leadClipEvents: () => leadClipEvents,
50
76
  markKitOrigin: () => markKitOrigin,
51
77
  mergeById: () => mergeById,
52
78
  removeIds: () => removeIds,
79
+ resetWarnOnce: () => resetWarnOnce2,
53
80
  resolveEntry: () => resolveEntry,
54
81
  resolveIgnitionRules: () => resolveIgnitionRules,
55
82
  sampleClip: () => sampleClip,
@@ -59,10 +86,14 @@ var GameContracts = (() => {
59
86
  shotExtras: () => shotExtras,
60
87
  shotToHit: () => shotToHit,
61
88
  sparkTargetFactor: () => sparkTargetFactor,
89
+ stepKnock: () => stepKnock,
90
+ surfaceOfMaterial: () => surfaceOfMaterial,
91
+ swingSoundId: () => swingSoundId,
62
92
  toolAnimationStep: () => toolAnimationStep,
63
93
  twistStroke: () => twistStroke,
64
94
  unmarkedDrawings: () => unmarkedDrawings,
65
- useToHit: () => useToHit
95
+ useToHit: () => useToHit,
96
+ warnOnce: () => warnOnce
66
97
  });
67
98
 
68
99
  // src/blast-shove.ts
@@ -83,6 +114,83 @@ var GameContracts = (() => {
83
114
  return { speed, direction: [direction[0] / length, direction[1] / length, direction[2] / length], distance, falloff };
84
115
  }
85
116
 
117
+ // src/blast-knock.ts
118
+ var KNOCK_DOWN_SPEED = 2.5;
119
+ var KNOCKED_DOWN_ANGLE = 1.35;
120
+ var LIE_SECONDS = 1.6;
121
+ var STAND_UP_PER_SPEED = 0.25;
122
+ var FALL_SECONDS = 0.25;
123
+ var RISE_SECONDS = 0.6;
124
+ var GROUND_FRICTION = 7;
125
+ var GRAVITY = 9.81;
126
+ var KNOCK_CHEST = 1.1;
127
+ var CHEST = KNOCK_CHEST;
128
+ var bodyMass = (height, build = 1) => 70 * (height / 1.75) ** 2 * build;
129
+ function knockOf(blast, feet, mass, options) {
130
+ const hit = blastShove(blast, { position: [feet[0], feet[1] + CHEST, feet[2]], mass }, options);
131
+ if (!hit) return void 0;
132
+ const [dx, dy, dz] = hit.direction;
133
+ return { velocity: [dx * hit.speed, Math.max(0, dy * hit.speed), dz * hit.speed], airborne: dy > 0, lying: 0, lieLeft: 0, down: hit.speed >= KNOCK_DOWN_SPEED };
134
+ }
135
+ function stepKnock(knock, position, dt, groundAt, alive) {
136
+ const v = knock.velocity;
137
+ position.x += v[0] * dt;
138
+ position.z += v[2] * dt;
139
+ const ground = groundAt(position.x, position.z);
140
+ if (knock.airborne || position.y > ground + 1e-4) {
141
+ v[1] -= GRAVITY * dt;
142
+ position.y += v[1] * dt;
143
+ if (position.y <= ground) {
144
+ position.y = ground;
145
+ v[1] = 0;
146
+ knock.airborne = false;
147
+ }
148
+ } else position.y = ground;
149
+ const horizontal = Math.hypot(v[0], v[2]);
150
+ if (!knock.airborne && horizontal > 0) {
151
+ const slowed = Math.max(0, horizontal - GROUND_FRICTION * dt);
152
+ v[0] *= slowed / horizontal;
153
+ v[2] *= slowed / horizontal;
154
+ }
155
+ const moving = horizontal > 0.05 || knock.airborne;
156
+ if (knock.down) {
157
+ if (moving) knock.lying = Math.min(1, knock.lying + dt / FALL_SECONDS);
158
+ else {
159
+ knock.lying = 1;
160
+ if (alive) {
161
+ knock.lieLeft = knock.lieLeft > 0 ? knock.lieLeft : LIE_SECONDS + Math.hypot(v[0], v[2]) * STAND_UP_PER_SPEED;
162
+ knock.lieLeft -= dt;
163
+ if (knock.lieLeft <= 0) knock.down = false;
164
+ }
165
+ }
166
+ } else if (knock.lying > 0) knock.lying = Math.max(0, knock.lying - dt / RISE_SECONDS);
167
+ return !moving && !knock.down && knock.lying === 0;
168
+ }
169
+
170
+ // src/impact-cost.ts
171
+ var DEFAULT_COST_READS = 1e4;
172
+ var DEFAULT_COST_CEILING_MS = 50;
173
+ var DEFAULT_COST_ALLOCATION_BYTES = 1024 * 1024;
174
+ var heapUsedBytes = () => {
175
+ const nodeProcess = globalThis.process;
176
+ return nodeProcess?.memoryUsage?.().heapUsed;
177
+ };
178
+ var checkReadCost = ({ name, read, options, problems }) => {
179
+ const reads = options.reads ?? DEFAULT_COST_READS;
180
+ const ceilingMs = options.ceilingMs ?? DEFAULT_COST_CEILING_MS;
181
+ const allocationBytes = options.allocationBytes ?? DEFAULT_COST_ALLOCATION_BYTES;
182
+ if (read() !== read()) problems.push(`${name}() returns a new object on each read: cache it between changes (cost contract)`);
183
+ for (let i = 0; i < reads; i++) read();
184
+ const heapBefore = heapUsedBytes();
185
+ const started = performance.now();
186
+ for (let i = 0; i < reads; i++) read();
187
+ const elapsedMs = performance.now() - started;
188
+ const heapAfter = heapUsedBytes();
189
+ if (elapsedMs > ceilingMs) problems.push(`${name}() took ${elapsedMs.toFixed(1)} ms for ${reads} reads, over the ${ceilingMs} ms ceiling (cost contract)`);
190
+ if (heapBefore !== void 0 && heapAfter !== void 0 && heapAfter - heapBefore > allocationBytes)
191
+ problems.push(`${name}() grew the heap by ${heapAfter - heapBefore} bytes over ${reads} reads, over the ${allocationBytes} byte ceiling (cost contract)`);
192
+ };
193
+
86
194
  // src/impact.ts
87
195
  var IMPACT_CONTRACT = {
88
196
  ballistic: { required: ["type", "point", "direction", "energy", "caliber"], optional: ["space", "source"], note: "bullets and pellets: joules and diameter in millimetres" },
@@ -189,6 +297,15 @@ var GameContracts = (() => {
189
297
  } catch (e) {
190
298
  problems.push(`reset() threw: ${String(e)}`);
191
299
  }
300
+ if (options.cost) {
301
+ const cost = options.cost === true ? {} : options.cost;
302
+ try {
303
+ checkReadCost({ name: "state", read: () => impl.state(), options: cost, problems });
304
+ checkReadCost({ name: "describe", read: () => impl.describe(), options: cost, problems });
305
+ } catch (e) {
306
+ problems.push(`cost measurement threw: ${String(e)}`);
307
+ }
308
+ }
192
309
  return { ok: problems.length === 0, problems };
193
310
  };
194
311
 
@@ -593,6 +710,21 @@ var GameContracts = (() => {
593
710
  }
594
711
  return { ok: problems.length === 0, problems };
595
712
  };
713
+ var leadClipEvents = (clip) => {
714
+ const events = clip.events ?? [];
715
+ if (!events.some((event) => (event.lead ?? 0) > 0)) return clip;
716
+ let floor = 0;
717
+ const led = events.map((event) => {
718
+ const lead = Math.max(0, Math.min(event.t - floor, event.lead ?? 0));
719
+ const t = event.t - lead;
720
+ floor = Math.max(floor, t);
721
+ if (event.lead === void 0) return event;
722
+ const moved = { ...event, t, data: lead > 0 ? { ...event.data, lead } : { ...event.data } };
723
+ delete moved.lead;
724
+ return moved;
725
+ });
726
+ return { ...clip, events: led };
727
+ };
596
728
  var createToolClipPlayer = (options) => {
597
729
  const { clips, rig, onEvent } = options;
598
730
  let instances = [];
@@ -635,7 +767,7 @@ var GameContracts = (() => {
635
767
  }
636
768
  const found = find(step.clip, step.variant);
637
769
  if (!found) return;
638
- const clip = step.duration && step.duration > 0 ? scaleClip(found.clip, step.duration) : found.clip;
770
+ const clip = leadClipEvents(step.duration && step.duration > 0 ? scaleClip(found.clip, step.duration) : found.clip);
639
771
  const interval = options.repeatInterval?.() ?? 0.1;
640
772
  instances = instances.filter((existing) => existing.name !== found.name || existing.delay > 0);
641
773
  for (let round = 0; round < (step.repeat ?? 1); round++) {
@@ -664,6 +796,66 @@ var GameContracts = (() => {
664
796
  };
665
797
  };
666
798
 
799
+ // src/frame.ts
800
+ var FRAME_PHASES = ["input", "fixedSimulate", "lateSimulate", "present", "render", "idle"];
801
+ var DEFERRED_QUEUE_ID = {
802
+ DESTRUCTION_EVENTS: "destruction.events",
803
+ BREACH_PAINT: "floorplan.breach-paint",
804
+ AUDIO_VOICES: "audio.voices",
805
+ VEHICLE_CREASE: "vehicles.crease"
806
+ };
807
+ var DEFERRED_PRIORITY = {
808
+ [DEFERRED_QUEUE_ID.DESTRUCTION_EVENTS]: { priority: 300, phase: "present" },
809
+ [DEFERRED_QUEUE_ID.BREACH_PAINT]: { priority: 250, phase: "present" },
810
+ [DEFERRED_QUEUE_ID.AUDIO_VOICES]: { priority: 200, phase: "idle", reserveMs: 1 },
811
+ [DEFERRED_QUEUE_ID.VEHICLE_CREASE]: { priority: 100, phase: "idle" }
812
+ };
813
+ function checkDeferredQueue(queue, fill) {
814
+ const problems = [];
815
+ fill();
816
+ const waiting = queue.pending(1);
817
+ if (waiting < 2) problems.push(`fill() must leave at least 2 units waiting (pending is ${waiting})`);
818
+ if (queue.pending(1) !== waiting) problems.push("pending(frame) changed between two calls in the same frame");
819
+ const ran = queue.run(0, () => 1);
820
+ if (ran !== 1) problems.push(`run with a deadline already past must run exactly one unit (ran ${ran})`);
821
+ if (waiting >= 2 && queue.pending(2) !== waiting - 1) problems.push(`one unit ran but pending went from ${waiting} to ${queue.pending(2)}`);
822
+ try {
823
+ queue.attach?.();
824
+ queue.attach?.();
825
+ queue.detach?.();
826
+ queue.detach?.();
827
+ } catch (error) {
828
+ problems.push(`attach/detach must be idempotent: ${error instanceof Error ? error.message : String(error)}`);
829
+ }
830
+ queue.flush();
831
+ if (queue.pending(3) !== 0) problems.push(`flush must leave nothing pending (pending is ${queue.pending(3)})`);
832
+ if (queue.run(0, () => 1) !== 0) problems.push("run on an empty queue must run no unit");
833
+ return { ok: problems.length === 0, problems };
834
+ }
835
+
836
+ // src/warn.ts
837
+ var WARNED_KEY = /* @__PURE__ */ Symbol.for("@adatechnology/game-contracts.warned");
838
+ var warnedKeys = () => {
839
+ const holder = globalThis;
840
+ return holder[WARNED_KEY] ?? (holder[WARNED_KEY] = /* @__PURE__ */ new Set());
841
+ };
842
+ var isProduction = () => {
843
+ try {
844
+ return process.env.NODE_ENV === "production";
845
+ } catch {
846
+ return false;
847
+ }
848
+ };
849
+ var isQuiet = () => globalThis.__GAME_KIT_QUIET__ === true;
850
+ var warnOnce = (key, message) => {
851
+ if (isQuiet() || isProduction()) return;
852
+ const seen = warnedKeys();
853
+ if (seen.has(key)) return;
854
+ seen.add(key);
855
+ console.warn(`[game-kit] ${message}`);
856
+ };
857
+ var resetWarnOnce = () => warnedKeys().clear();
858
+
667
859
  // src/registry.ts
668
860
  var UNSAFE_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
669
861
  var isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype;
@@ -686,10 +878,13 @@ var GameContracts = (() => {
686
878
  for (const [id, patch] of Object.entries(patches ?? {})) {
687
879
  const entry = entries.get(id);
688
880
  if (entry) entries.set(id, deepMerge(entry, patch));
881
+ else warnOnce(`registry.patch.${id}`, `patch for "${id}" ignored: no entry has that id`);
689
882
  }
690
883
  };
691
884
  var removeIds = (entries, ids) => {
692
- for (const id of ids ?? []) entries.delete(id);
885
+ for (const id of ids ?? []) {
886
+ if (!entries.delete(id)) warnOnce(`registry.remove.${id}`, `remove of "${id}" ignored: no entry has that id`);
887
+ }
693
888
  };
694
889
 
695
890
  // src/bench.ts
@@ -928,6 +1123,112 @@ var GameContracts = (() => {
928
1123
  return lx > fp.minX - radius && lx < fp.maxX + radius && lz > fp.minZ - radius && lz < fp.maxZ + radius;
929
1124
  };
930
1125
  }
1126
+
1127
+ // src/foley.ts
1128
+ var SURFACE_IDS = ["concrete", "asphalt", "wood", "tile", "dirt", "grass", "gravel", "sand", "metal", "water", "glass", "carpet"];
1129
+ var DEFAULT_SURFACE = "concrete";
1130
+ var GAIT_IDS = ["walk", "run"];
1131
+ var LANDING_IDS = ["light", "heavy"];
1132
+ var HEAVY_LANDING_SPEED = 5.5;
1133
+ var BLOW_IDS = ["punch", "kick", "shove", "blade", "blunt", "chop"];
1134
+ var BODY_SOUND_IDS = [
1135
+ "jump",
1136
+ "cloth_rustle",
1137
+ "body_fall",
1138
+ "fall_damage",
1139
+ "player_hurt",
1140
+ "player_breath_run",
1141
+ "player_death",
1142
+ "bite",
1143
+ "grab",
1144
+ "punch_swing",
1145
+ "punch_hit_body",
1146
+ "punch_hit_face",
1147
+ "punch_hit_object",
1148
+ "kick_swing",
1149
+ "kick_hit",
1150
+ "kick_hit_object",
1151
+ "shove_two_hands",
1152
+ "knife_swing",
1153
+ "knife_stab",
1154
+ "knife_slash",
1155
+ "knife_hit_object",
1156
+ "blunt_swing",
1157
+ "blunt_hit",
1158
+ "blunt_hit_object",
1159
+ "axe_swing",
1160
+ "axe_chop",
1161
+ "axe_hit_object"
1162
+ ];
1163
+ var footstepSoundId = (surface, gait) => `footstep_${surface}_${gait}`;
1164
+ var landSoundId = (surface, fallSpeed) => `land_${surface}_${fallSpeed >= HEAVY_LANDING_SPEED ? "heavy" : "light"}`;
1165
+ var FOOTSTEP_SOUND_IDS = SURFACE_IDS.flatMap((surface) => GAIT_IDS.map((gait) => footstepSoundId(surface, gait)));
1166
+ var LAND_SOUND_IDS = SURFACE_IDS.flatMap((surface) => LANDING_IDS.map((landing) => `land_${surface}_${landing}`));
1167
+ var BODY_FOLEY_IDS = [...FOOTSTEP_SOUND_IDS, ...LAND_SOUND_IDS, ...BODY_SOUND_IDS];
1168
+ var SWING = {
1169
+ punch: "punch_swing",
1170
+ kick: "kick_swing",
1171
+ // two hands pushed out do not whistle: the shove is one sound, at the contact
1172
+ shove: null,
1173
+ blade: "knife_swing",
1174
+ blunt: "blunt_swing",
1175
+ chop: "axe_swing"
1176
+ };
1177
+ var SWING_LEAD_SECONDS = { punch: 0.07, kick: 0.09, shove: 0, blade: 0.05, blunt: 0.1, chop: 0.11 };
1178
+ var swingSoundId = (blow) => SWING[blow];
1179
+ var HIT = {
1180
+ punch: { body: "punch_hit_body", head: "punch_hit_face", object: "punch_hit_object" },
1181
+ kick: { body: "kick_hit", head: "kick_hit", object: "kick_hit_object" },
1182
+ shove: { body: "shove_two_hands", head: "shove_two_hands", object: "shove_two_hands" },
1183
+ blade: { body: "knife_stab", head: "knife_stab", object: "knife_hit_object" },
1184
+ blunt: { body: "blunt_hit", head: "blunt_hit", object: "blunt_hit_object" },
1185
+ chop: { body: "axe_chop", head: "axe_chop", object: "axe_hit_object" }
1186
+ };
1187
+ var hitSoundId = (blow, target) => HIT[blow][target];
1188
+ var MATERIAL_SURFACES = {
1189
+ concrete: "concrete",
1190
+ mortar: "concrete",
1191
+ stone: "concrete",
1192
+ brick: "concrete",
1193
+ plaster: "concrete",
1194
+ asphalt: "asphalt",
1195
+ wood_soft: "wood",
1196
+ wood_hard: "wood",
1197
+ green_wood: "wood",
1198
+ plywood: "wood",
1199
+ mdf: "wood",
1200
+ ceramic: "tile",
1201
+ plastic: "tile",
1202
+ soil: "dirt",
1203
+ wax: "dirt",
1204
+ flesh_fruit: "dirt",
1205
+ turf: "grass",
1206
+ foliage: "grass",
1207
+ cactus: "grass",
1208
+ gravel: "gravel",
1209
+ sand: "sand",
1210
+ steel: "metal",
1211
+ cast_iron: "metal",
1212
+ aluminium: "metal",
1213
+ copper: "metal",
1214
+ water: "water",
1215
+ glass: "glass",
1216
+ tempered_glass: "glass",
1217
+ fabric: "carpet",
1218
+ foam: "carpet",
1219
+ leather: "carpet",
1220
+ rubber: "carpet",
1221
+ paper: "carpet"
1222
+ };
1223
+ var isSurfaceId = (value) => typeof value === "string" && SURFACE_IDS.includes(value);
1224
+ function surfaceOfMaterial(material, table = MATERIAL_SURFACES, fallback = DEFAULT_SURFACE) {
1225
+ if (material === void 0) return fallback;
1226
+ if (isSurfaceId(material)) return material;
1227
+ return table[material] ?? fallback;
1228
+ }
1229
+
1230
+ // src/index.ts
1231
+ var resetWarnOnce2 = resetWarnOnce;
931
1232
  return __toCommonJS(index_exports);
932
1233
  })();
933
1234
  //# sourceMappingURL=index.global.js.map