@adatechnology/game-contracts 0.13.0 → 0.14.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,14 +21,18 @@ var GameContracts = (() => {
21
21
  // src/index.ts
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
+ DEFAULT_BLAST_SHOVE: () => DEFAULT_BLAST_SHOVE,
24
25
  DEFAULT_ENVIRONMENT: () => DEFAULT_ENVIRONMENT,
26
+ DEFAULT_IGNITION_RULES: () => DEFAULT_IGNITION_RULES,
25
27
  IMPACT_CONTRACT: () => IMPACT_CONTRACT,
26
28
  MISSING_BITS: () => MISSING_BITS,
27
29
  STAGE_EVENT: () => STAGE_EVENT,
28
30
  TOOL_CLIP: () => TOOL_CLIP,
29
31
  applyPatches: () => applyPatches,
32
+ blastShove: () => blastShove,
30
33
  checkAnimation: () => checkAnimation,
31
34
  checkBench: () => checkBench,
35
+ checkCategorized: () => checkCategorized,
32
36
  checkDestructible: () => checkDestructible,
33
37
  checkHit: () => checkHit,
34
38
  checkSpec: () => checkSpec,
@@ -36,20 +40,46 @@ var GameContracts = (() => {
36
40
  createEnvironmentBus: () => createEnvironmentBus,
37
41
  createToolClipPlayer: () => createToolClipPlayer,
38
42
  deepMerge: () => deepMerge,
43
+ defineCategories: () => defineCategories,
44
+ fillFactor: () => fillFactor,
45
+ ignitionChance: () => ignitionChance,
39
46
  mergeById: () => mergeById,
40
47
  removeIds: () => removeIds,
48
+ resolveEntry: () => resolveEntry,
49
+ resolveIgnitionRules: () => resolveIgnitionRules,
41
50
  sampleClip: () => sampleClip,
42
51
  scaleClip: () => scaleClip,
52
+ shotExtras: () => shotExtras,
43
53
  shotToHit: () => shotToHit,
44
- toolAnimationStep: () => toolAnimationStep
54
+ sparkTargetFactor: () => sparkTargetFactor,
55
+ toolAnimationStep: () => toolAnimationStep,
56
+ useToHit: () => useToHit
45
57
  });
46
58
 
59
+ // src/blast-shove.ts
60
+ var DEFAULT_BLAST_SHOVE = { coupling: 0.02, maxSpeed: 20, lift: 0.3, minSpeed: 0.3, falloffPower: 2 };
61
+ function blastShove(blast, target, options = {}) {
62
+ const o = { ...DEFAULT_BLAST_SHOVE, ...options };
63
+ const dx = target.position[0] - blast.point[0];
64
+ const dy = target.position[1] - blast.point[1];
65
+ const dz = target.position[2] - blast.point[2];
66
+ const distance = Math.hypot(dx, dy, dz);
67
+ if (!(blast.radius > 0) || distance > blast.radius) return void 0;
68
+ const falloff = (1 - distance / blast.radius) ** o.falloffPower;
69
+ const mass = Math.max(target.mass, 0.05);
70
+ const speed = Math.min(o.maxSpeed, Math.sqrt(2 * blast.energy * (blast.reach ?? 1) * o.coupling / mass)) * falloff;
71
+ if (!(speed >= o.minSpeed)) return void 0;
72
+ const direction = distance > 1e-4 ? [dx / distance, dy / distance + o.lift, dz / distance] : [0, 1, 0];
73
+ const length = Math.hypot(direction[0], direction[1], direction[2]);
74
+ return { speed, direction: [direction[0] / length, direction[1] / length, direction[2] / length], distance, falloff };
75
+ }
76
+
47
77
  // src/impact.ts
48
78
  var IMPACT_CONTRACT = {
49
79
  ballistic: { required: ["type", "point", "direction", "energy", "caliber"], optional: ["space", "source"], note: "bullets and pellets: joules and diameter in millimetres" },
50
80
  blunt: { required: ["type", "point", "energy"], optional: ["direction", "radius", "space", "source"], note: "contact radius in metres (default 3 cm)" },
51
81
  slash: { required: ["type", "point", "energy"], optional: ["direction", "radius", "space", "source"], note: "axes, knives, machetes" },
52
- explosive: { required: ["type", "point", "energy", "radius"], optional: ["space", "source"], note: "point is the centre of the blast, radius the range in metres" },
82
+ explosive: { required: ["type", "point", "energy", "radius"], optional: ["lethalRadius", "space", "source"], note: "point is the centre of the blast, radius the range in metres" },
53
83
  fire: { required: ["type", "point", "temperature", "radius"], optional: ["duration", "space", "source"], note: "flame temperature in \xB0C; duration in seconds (call every frame with `dt`)" },
54
84
  heat: { required: ["type", "point", "temperature", "radius"], optional: ["duration", "space", "source"], note: "heat without flame (cutting torch, hot plate)" }
55
85
  };
@@ -272,6 +302,19 @@ var GameContracts = (() => {
272
302
  };
273
303
 
274
304
  // src/weapons.ts
305
+ var shotExtras = (shot, distance = 0) => {
306
+ const out = {};
307
+ if (shot.penetration) {
308
+ const k = shot.energy > 0 ? Math.min(1, Math.max(0, shot.energyAt(distance) / shot.energy)) : 1;
309
+ const scaled = {};
310
+ for (const key of Object.keys(shot.penetration)) scaled[key] = shot.penetration[key] * k;
311
+ out.penetration = scaled;
312
+ }
313
+ if (shot.incendiary) out.incendiary = true;
314
+ if (shot.tracer) out.tracer = true;
315
+ if (shot.ignition) out.ignition = { spark: shot.ignition.spark, flame: shot.ignition.flame };
316
+ return out;
317
+ };
275
318
  var shotToHit = (shot, point, normal) => {
276
319
  void normal;
277
320
  const dx = point[0] - shot.origin[0];
@@ -285,9 +328,79 @@ var GameContracts = (() => {
285
328
  energy: shot.energyAt(distance),
286
329
  caliber: shot.caliber,
287
330
  ...shot.mass === void 0 ? {} : { mass: shot.mass },
331
+ ...shotExtras(shot, distance),
288
332
  source: shot.source ?? shot.weapon
289
333
  };
290
334
  };
335
+ var useToHit = (use, point, normal) => {
336
+ void normal;
337
+ const p = [point[0], point[1], point[2]];
338
+ switch (use.kind) {
339
+ case "melee":
340
+ return { type: use.damage, point: p, direction: [use.direction[0], use.direction[1], use.direction[2]], energy: use.energy, radius: use.radius };
341
+ case "flame":
342
+ case "heat":
343
+ return {
344
+ type: use.kind === "flame" ? "fire" : "heat",
345
+ point: p,
346
+ direction: [use.direction[0], use.direction[1], use.direction[2]],
347
+ temperature: use.temperature,
348
+ radius: Math.min(use.radius, 0.3),
349
+ duration: use.duration
350
+ };
351
+ case "throw":
352
+ if (use.energy > 0) return { type: "explosive", point: p, energy: use.energy, radius: use.radius };
353
+ if (use.fire) return { type: "fire", point: p, temperature: use.fire.temperature, radius: use.fire.radius, duration: use.fire.seconds };
354
+ return null;
355
+ default:
356
+ return null;
357
+ }
358
+ };
359
+
360
+ // src/ignition.ts
361
+ var DEFAULT_IGNITION_RULES = {
362
+ fillCurve: [
363
+ [0, 0.6],
364
+ [0.1, 1],
365
+ [0.35, 0.6],
366
+ [0.65, 0.3],
367
+ [0.9, 0.15],
368
+ [1, 0.12]
369
+ ],
370
+ flameFillInfluence: 0.5,
371
+ flameDefaults: { incendiary: 0.7, tracer: 0.2 },
372
+ sparkTarget: { steel: 1, iron: 1, aluminium: 0.25 },
373
+ sparkMetalDefault: 0.5,
374
+ sparkFluid: { diesel: 0.15 },
375
+ fireNear: { radius: 1.5, chancePerSecond: 0.5 }
376
+ };
377
+ var resolveIgnitionRules = (rules) => ({
378
+ ...DEFAULT_IGNITION_RULES,
379
+ ...rules,
380
+ flameDefaults: { ...DEFAULT_IGNITION_RULES.flameDefaults, ...rules?.flameDefaults },
381
+ sparkTarget: { ...DEFAULT_IGNITION_RULES.sparkTarget, ...rules?.sparkTarget },
382
+ sparkFluid: { ...DEFAULT_IGNITION_RULES.sparkFluid, ...rules?.sparkFluid },
383
+ fireNear: { ...DEFAULT_IGNITION_RULES.fireNear, ...rules?.fireNear }
384
+ });
385
+ var fillFactor = (fill, curve = DEFAULT_IGNITION_RULES.fillCurve) => {
386
+ if (curve.length === 0) return 1;
387
+ const f = Math.min(1, Math.max(0, fill));
388
+ if (f <= curve[0][0]) return curve[0][1];
389
+ for (let i = 1; i < curve.length; i++) {
390
+ const [x1, y1] = curve[i];
391
+ const [x0, y0] = curve[i - 1];
392
+ if (f <= x1) return x1 === x0 ? y1 : y0 + (y1 - y0) * (f - x0) / (x1 - x0);
393
+ }
394
+ return curve[curve.length - 1][1];
395
+ };
396
+ var sparkTargetFactor = (materialId, metal, rules = DEFAULT_IGNITION_RULES) => !metal ? 0 : rules.sparkTarget[materialId] ?? rules.sparkMetalDefault;
397
+ var ignitionChance = (hit, target, rules = DEFAULT_IGNITION_RULES) => {
398
+ const k = fillFactor(target.fill, rules.fillCurve);
399
+ const spark = (hit.ignition?.spark ?? 0) * target.sparkTarget * (target.fluid === void 0 ? 1 : rules.sparkFluid[target.fluid] ?? 1) * k;
400
+ const flameBase = hit.ignition?.flame ?? Math.max(hit.incendiary ? rules.flameDefaults.incendiary : 0, hit.tracer ? rules.flameDefaults.tracer : 0);
401
+ const flame = flameBase * (1 - rules.flameFillInfluence * (1 - k));
402
+ return 1 - (1 - Math.min(1, Math.max(0, spark))) * (1 - Math.min(1, Math.max(0, flame)));
403
+ };
291
404
 
292
405
  // src/animation.ts
293
406
  var finite3 = (v) => Array.isArray(v) && v.length === 3 && v.every((n) => typeof n === "number" && Number.isFinite(n));
@@ -503,6 +616,9 @@ var GameContracts = (() => {
503
616
  },
504
617
  get playing() {
505
618
  return instances.map((instance) => instance.name);
619
+ },
620
+ get running() {
621
+ return instances.filter((instance) => instance.delay <= 0).map((instance) => ({ name: instance.name, time: instance.time, clip: instance.clip }));
506
622
  }
507
623
  };
508
624
  };
@@ -616,6 +732,111 @@ var GameContracts = (() => {
616
732
  }
617
733
  };
618
734
  }
735
+
736
+ // src/categories.ts
737
+ var defineCategories = (defs, options = {}) => {
738
+ const problems = [];
739
+ const duplicates = (list) => {
740
+ const ids = /* @__PURE__ */ new Set();
741
+ for (const def of list) {
742
+ if (ids.has(def.id)) problems.push(`duplicate category "${def.id}"`);
743
+ ids.add(def.id);
744
+ }
745
+ };
746
+ duplicates(defs);
747
+ duplicates(options.extra ?? []);
748
+ const entries = mergeById(defs, options.extra);
749
+ applyPatches(entries, options.patches);
750
+ removeIds(entries, options.remove);
751
+ const chain = (id) => {
752
+ const path = [];
753
+ let current = id;
754
+ while (current !== void 0) {
755
+ const def = entries.get(current);
756
+ if (!def) throw new Error(path.length === 0 ? `unknown category "${current}"` : `category "${path[0]}" has unknown parent "${current}"`);
757
+ if (path.includes(current)) throw new Error(`category cycle: ${[...path, current].reverse().join(" -> ")}`);
758
+ path.push(current);
759
+ current = def.extends;
760
+ }
761
+ return path.reverse();
762
+ };
763
+ const registry = {
764
+ get: (id) => entries.get(id),
765
+ has: (id) => entries.has(id),
766
+ ids: () => [...entries.keys()],
767
+ chain,
768
+ defaults: (id) => chain(id).reduce((acc, ancestor) => deepMerge(acc, entries.get(ancestor)?.defaults), {}),
769
+ requires: (id) => {
770
+ const out = [];
771
+ for (const ancestor of chain(id)) for (const contract of entries.get(ancestor)?.requires ?? []) if (!out.includes(contract)) out.push(contract);
772
+ return out;
773
+ },
774
+ isA: (id, ancestorId) => {
775
+ try {
776
+ return chain(id).includes(ancestorId);
777
+ } catch {
778
+ return false;
779
+ }
780
+ },
781
+ check: () => {
782
+ const found = [...problems];
783
+ for (const [id, def] of entries) {
784
+ if (def.extends !== void 0 && !entries.has(def.extends)) found.push(`category "${id}" extends unknown "${def.extends}"`);
785
+ else {
786
+ try {
787
+ chain(id);
788
+ } catch (error) {
789
+ const message = error.message;
790
+ if (message.startsWith("category cycle") && !found.includes(message)) found.push(message);
791
+ }
792
+ }
793
+ }
794
+ return found;
795
+ }
796
+ };
797
+ return registry;
798
+ };
799
+ var resolveEntry = (registry, entry, kindField = "kind") => deepMerge(registry.defaults(String(entry[kindField])), entry);
800
+ var checkCategorized = (entries, registry, checkers, options = {}) => {
801
+ const kindField = options.kindField ?? "kind";
802
+ const byId = new Map(checkers.map((checker) => [checker.id, checker]));
803
+ const problems = [...registry.check()];
804
+ const allowed = [];
805
+ for (const entry of entries) {
806
+ const kind = String(entry[kindField]);
807
+ if (!registry.has(kind)) {
808
+ problems.push(`"${entry.id}" has unknown kind "${kind}"`);
809
+ continue;
810
+ }
811
+ if (registry.get(kind)?.abstract) {
812
+ problems.push(`"${entry.id}" has abstract kind "${kind}"`);
813
+ continue;
814
+ }
815
+ let path;
816
+ try {
817
+ path = registry.chain(kind);
818
+ } catch (error) {
819
+ problems.push(`"${entry.id}" (${kind}): ${error.message}`);
820
+ continue;
821
+ }
822
+ const resolved = resolveEntry(registry, entry, kindField);
823
+ for (const contract of registry.requires(kind)) {
824
+ const owner = path.find((ancestor) => registry.get(ancestor)?.requires?.includes(contract)) ?? kind;
825
+ const checker = byId.get(contract);
826
+ if (!checker) {
827
+ problems.push(`kind "${kind}" requires contract "${contract}" but no checker is registered`);
828
+ continue;
829
+ }
830
+ const prefix = `"${entry.id}" (${kind}): `;
831
+ for (const problem of checker.check(resolved, { kind, chain: path })) {
832
+ const text = `${prefix}${problem}, required by ${owner}`;
833
+ if ((options.allow?.[entry.id] ?? []).some((start) => problem.startsWith(start))) allowed.push(text);
834
+ else problems.push(text);
835
+ }
836
+ }
837
+ }
838
+ return { ok: problems.length === 0, problems: [...new Set(problems)], allowed };
839
+ };
619
840
  return __toCommonJS(index_exports);
620
841
  })();
621
842
  //# sourceMappingURL=index.global.js.map