@adatechnology/game-vehicles-driving 0.2.8 → 0.2.10

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.d.ts CHANGED
@@ -252,6 +252,8 @@ interface SemiSimSpec {
252
252
  maxSteer: number;
253
253
  /** Total brake force, newtons, split by the wheel loads. */
254
254
  brakeForce: number;
255
+ /** Anti-lock brakes (default true). Without them a hard stop locks the wheels: they slide on less grip and the car does not steer. */
256
+ abs?: boolean;
255
257
  engine: {
256
258
  idleRpm: number;
257
259
  peakRpm: number;
@@ -288,6 +290,8 @@ interface VehicleCondition {
288
290
  tyres?: readonly number[];
289
291
  engine?: number;
290
292
  suspension?: number;
293
+ /** How much of the brake force is left, 0..1: worn or broken brakes, no brake fluid. */
294
+ brakes?: number;
291
295
  }
292
296
  interface SemiInput extends DriveInput {
293
297
  /** Forces a gear (1-based, 0 = neutral, -1 = reverse). Absent = automatic. */
@@ -303,6 +307,8 @@ interface SemiWorld {
303
307
  ground?(x: number, z: number): SemiGround;
304
308
  }
305
309
  interface SemiState {
310
+ /** Temperature of the brakes, °C: hard stops heat them, and past about 350 °C they fade. */
311
+ brakeHeat?: number;
306
312
  x: number;
307
313
  z: number;
308
314
  yaw: number;
@@ -365,12 +371,6 @@ interface SemiDriver extends Omit<Driver, 'step' | 'state'> {
365
371
  events: SemiEvent[];
366
372
  };
367
373
  }
368
- /**
369
- * The semi-simulation driver (Frota document, section 03): a single rigid body with a low centre of mass, a spring and damper on each wheel
370
- * measured against the ground under it, separate longitudinal and lateral tyre forces inside a friction circle, an automatic gearbox with a
371
- * simulated RPM, a handbrake that locks the rear wheels, surfaces with their grip, and damage that changes how it drives. No team code exists
372
- * for it: the numbers are the document's and the tests check them.
373
- */
374
374
  declare function createSemiSimDriver(specification?: Partial<SemiSimSpec>, options?: SemiSimOptions): SemiDriver;
375
375
 
376
376
  /** Kinetic energy of a vehicle against a body: `0.5 * mass * speed²` (joules, with kg and m/s). Frota document, section 08. */
@@ -398,8 +398,25 @@ interface BodyDimensions {
398
398
  }
399
399
  /**
400
400
  * A semi-simulation spec for a two-axle vehicle from its design ratings: the grip follows the handling, the torque follows the acceleration,
401
- * and the air drag is set so that the top gear tops out at the design top speed.
401
+ * the air drag is set so that the top gear tops out at the design top speed, and the brake force follows the mass.
402
402
  */
403
403
  declare function semiSpecFromStats(stats: DesignStats, body: BodyDimensions, base?: Partial<SemiSimSpec>): SemiSimSpec;
404
404
 
405
- export { type ArcadeOptions, type ArcadeSpec, type BodyDimensions, type CrashRules, type DesignStats, type DriveInput, type Driver, type DriverState, type DrivingBody, type DrivingEvent, type DrivingWorld, type FuelModel, type RunOverOutcome, type RunOverRules, type SemiDriver, type SemiEvent, type SemiGround, type SemiInput, type SemiSimOptions, type SemiSimSpec, type SemiState, type SemiWheel, type SemiWorld, type StepResult, type Surface, type SurfaceOptions, type SurfaceRegistry, type VehicleCondition, bodyDamage, createArcadeDriver, createSemiSimDriver, createSurfaces, defaultArcadeSpec, defaultFuelModel, defaultSemiSpec, defaultSurfaces, fuelRate, impactEnergy, runOver, semiSpecFromStats, speedLoss, surfaceGrip, vehicleCorners, vehicleDamageFromImpact };
405
+ /** Matches a damage model's wheels to a driver's: each wheel of the spec takes the health of the tyre nearest to it. */
406
+
407
+ type DamagedTyre = {
408
+ /** Where the tyre is in the vehicle's space, metres: x forward, z to the side. */
409
+ readonly x: number;
410
+ readonly z: number;
411
+ /** 0 flat or gone, 1 whole. */
412
+ readonly health: number;
413
+ };
414
+ type DamageReport = {
415
+ readonly tyres: readonly DamagedTyre[];
416
+ readonly brakes?: number;
417
+ readonly engine?: number;
418
+ readonly suspension?: number;
419
+ };
420
+ declare function conditionFromDamage(spec: Pick<SemiSimSpec, 'wheels'>, report: DamageReport): VehicleCondition;
421
+
422
+ export { type ArcadeOptions, type ArcadeSpec, type BodyDimensions, type CrashRules, type DamageReport, type DamagedTyre, type DesignStats, type DriveInput, type Driver, type DriverState, type DrivingBody, type DrivingEvent, type DrivingWorld, type FuelModel, type RunOverOutcome, type RunOverRules, type SemiDriver, type SemiEvent, type SemiGround, type SemiInput, type SemiSimOptions, type SemiSimSpec, type SemiState, type SemiWheel, type SemiWorld, type StepResult, type Surface, type SurfaceOptions, type SurfaceRegistry, type VehicleCondition, bodyDamage, conditionFromDamage, createArcadeDriver, createSemiSimDriver, createSurfaces, defaultArcadeSpec, defaultFuelModel, defaultSemiSpec, defaultSurfaces, fuelRate, impactEnergy, runOver, semiSpecFromStats, speedLoss, surfaceGrip, vehicleCorners, vehicleDamageFromImpact };
@@ -22,6 +22,7 @@ var GameVehiclesDriving = (() => {
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
24
  bodyDamage: () => bodyDamage,
25
+ conditionFromDamage: () => conditionFromDamage,
25
26
  createArcadeDriver: () => createArcadeDriver,
26
27
  createSemiSimDriver: () => createSemiSimDriver,
27
28
  createSurfaces: () => createSurfaces,
@@ -175,6 +176,39 @@ var GameVehiclesDriving = (() => {
175
176
  }
176
177
 
177
178
  // ../game-contracts/dist/index.js
179
+ var DEFAULT_COST_ALLOCATION_BYTES = 1024 * 1024;
180
+ var DEFERRED_QUEUE_ID = {
181
+ DESTRUCTION_EVENTS: "destruction.events",
182
+ BREACH_PAINT: "floorplan.breach-paint",
183
+ AUDIO_VOICES: "audio.voices",
184
+ VEHICLE_CREASE: "vehicles.crease"
185
+ };
186
+ var DEFERRED_PRIORITY = {
187
+ [DEFERRED_QUEUE_ID.DESTRUCTION_EVENTS]: { priority: 300, phase: "present" },
188
+ [DEFERRED_QUEUE_ID.BREACH_PAINT]: { priority: 250, phase: "present" },
189
+ [DEFERRED_QUEUE_ID.AUDIO_VOICES]: { priority: 200, phase: "idle", reserveMs: 1 },
190
+ [DEFERRED_QUEUE_ID.VEHICLE_CREASE]: { priority: 100, phase: "idle" }
191
+ };
192
+ var WARNED_KEY = /* @__PURE__ */ Symbol.for("@adatechnology/game-contracts.warned");
193
+ var warnedKeys = () => {
194
+ const holder = globalThis;
195
+ return holder[WARNED_KEY] ?? (holder[WARNED_KEY] = /* @__PURE__ */ new Set());
196
+ };
197
+ var isProduction = () => {
198
+ try {
199
+ return process.env.NODE_ENV === "production";
200
+ } catch {
201
+ return false;
202
+ }
203
+ };
204
+ var isQuiet = () => globalThis.__GAME_KIT_QUIET__ === true;
205
+ var warnOnce = (key, message) => {
206
+ if (isQuiet() || isProduction()) return;
207
+ const seen = warnedKeys();
208
+ if (seen.has(key)) return;
209
+ seen.add(key);
210
+ console.warn(`[game-kit] ${message}`);
211
+ };
178
212
  var UNSAFE_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
179
213
  var isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype;
180
214
  var deepMerge = (base, patch) => {
@@ -196,11 +230,50 @@ var GameVehiclesDriving = (() => {
196
230
  for (const [id, patch] of Object.entries(patches ?? {})) {
197
231
  const entry = entries.get(id);
198
232
  if (entry) entries.set(id, deepMerge(entry, patch));
233
+ else warnOnce(`registry.patch.${id}`, `patch for "${id}" ignored: no entry has that id`);
199
234
  }
200
235
  };
201
236
  var removeIds = (entries, ids) => {
202
- for (const id of ids ?? []) entries.delete(id);
237
+ for (const id of ids ?? []) {
238
+ if (!entries.delete(id)) warnOnce(`registry.remove.${id}`, `remove of "${id}" ignored: no entry has that id`);
239
+ }
203
240
  };
241
+ var SURFACE_IDS = ["concrete", "asphalt", "wood", "tile", "dirt", "grass", "gravel", "sand", "metal", "water", "glass", "carpet"];
242
+ var GAIT_IDS = ["walk", "run"];
243
+ var LANDING_IDS = ["light", "heavy"];
244
+ var BODY_SOUND_IDS = [
245
+ "jump",
246
+ "cloth_rustle",
247
+ "body_fall",
248
+ "fall_damage",
249
+ "player_hurt",
250
+ "player_breath_run",
251
+ "player_death",
252
+ "bite",
253
+ "grab",
254
+ "punch_swing",
255
+ "punch_hit_body",
256
+ "punch_hit_face",
257
+ "punch_hit_object",
258
+ "kick_swing",
259
+ "kick_hit",
260
+ "kick_hit_object",
261
+ "shove_two_hands",
262
+ "knife_swing",
263
+ "knife_stab",
264
+ "knife_slash",
265
+ "knife_hit_object",
266
+ "blunt_swing",
267
+ "blunt_hit",
268
+ "blunt_hit_object",
269
+ "axe_swing",
270
+ "axe_chop",
271
+ "axe_hit_object"
272
+ ];
273
+ var footstepSoundId = (surface, gait) => `footstep_${surface}_${gait}`;
274
+ var FOOTSTEP_SOUND_IDS = SURFACE_IDS.flatMap((surface) => GAIT_IDS.map((gait) => footstepSoundId(surface, gait)));
275
+ var LAND_SOUND_IDS = SURFACE_IDS.flatMap((surface) => LANDING_IDS.map((landing) => `land_${surface}_${landing}`));
276
+ var BODY_FOLEY_IDS = [...FOOTSTEP_SOUND_IDS, ...LAND_SOUND_IDS, ...BODY_SOUND_IDS];
204
277
 
205
278
  // src/semi/surfaces.ts
206
279
  var defaultSurfaces = [
@@ -271,6 +344,14 @@ var GameVehiclesDriving = (() => {
271
344
  const t = rpm <= e.peakRpm ? 0.7 + 0.3 * clamp2((rpm - e.idleRpm) / (e.peakRpm - e.idleRpm), 0, 1) : 1 - 0.4 * ((rpm - e.peakRpm) / (e.redlineRpm - e.peakRpm));
272
345
  return e.peakTorque * t;
273
346
  };
347
+ var SLIDING_GRIP = 0.75;
348
+ var LOCKED_STEER = 0.15;
349
+ var AMBIENT_C = 25;
350
+ var BRAKE_FADE_FROM_C = 350;
351
+ var BRAKE_FADE_TO_C = 650;
352
+ var BRAKE_FADED = 0.5;
353
+ var BRAKE_HEAT_CAPACITY = 3;
354
+ var BRAKE_COOL_S = 60;
274
355
  function createSemiSimDriver(specification = {}, options = {}) {
275
356
  const spec = { ...defaultSemiSpec, ...specification };
276
357
  const surfaces = options.surfaces ?? createSurfaces();
@@ -336,6 +417,7 @@ var GameVehiclesDriving = (() => {
336
417
  const mass = baseMass + cargo;
337
418
  const engineHealth = clamp2(cond.engine ?? 1, 0, 1);
338
419
  const suspensionHealth = clamp2(cond.suspension ?? 1, 0, 1);
420
+ const brakeHealth = clamp2(cond.brakes ?? 1, 0, 1);
339
421
  const inertia = (spec.yawInertia ?? baseMass * (wheelbase * wheelbase + track * track) / 12) * (mass / baseMass);
340
422
  const pitchInertia = mass * (wheelbase * wheelbase + 1) / 12;
341
423
  const rollInertia = mass * (track * track + 1) / 12;
@@ -420,6 +502,9 @@ var GameVehiclesDriving = (() => {
420
502
  sumNA += load * w.x;
421
503
  sumNB += load * w.z;
422
504
  }
505
+ const brakeHeat = state.brakeHeat ?? AMBIENT_C;
506
+ const fade = 1 - clamp2((brakeHeat - BRAKE_FADE_FROM_C) / (BRAKE_FADE_TO_C - BRAKE_FADE_FROM_C), 0, 1) * (1 - BRAKE_FADED);
507
+ let brakeWork = 0;
423
508
  let fFwd = 0;
424
509
  let fRight = 0;
425
510
  let torque = 0;
@@ -442,14 +527,17 @@ var GameVehiclesDriving = (() => {
442
527
  let fl = 0;
443
528
  if (driveWheels.some((d) => d.i === i)) fl += driveForceTotal / drivenCount;
444
529
  const locked = handbrake && w.handbrake;
445
- const brakeShare = spec.brakeForce * brakeInput * ws.n / loadSum;
530
+ const brakeShare = spec.brakeForce * brakeHealth * fade * brakeInput * ws.n / loadSum;
446
531
  const moving = Math.abs(vl) > 0.05;
447
- if (locked) fl += moving ? -Math.sign(vl) * capacity : 0;
532
+ const lockedByBrake = spec.abs === false && moving && brakeShare > capacity;
533
+ if (locked || lockedByBrake) fl += moving ? -Math.sign(vl) * capacity * (lockedByBrake ? SLIDING_GRIP : 1) : 0;
448
534
  else if (brakeShare > 0 && moving) fl += -Math.sign(vl) * brakeShare;
535
+ if (brakeShare > 0 && moving) brakeWork += Math.min(brakeShare, capacity) * Math.abs(vl) * dt;
449
536
  const rolling = ws.bog && accelInput > 0 ? spec.bogResistance : spec.rolling * (ws.flat ? 6 : 1);
450
537
  const alpha = Math.atan2(vc, Math.abs(vl) + 0.5);
451
538
  let fc = -Math.tanh(spec.tyre.corneringStiffness * alpha) * capacity;
452
539
  if (locked) fc *= 0.6;
540
+ if (lockedByBrake) fc *= LOCKED_STEER;
453
541
  const total = Math.hypot(fl, fc);
454
542
  let used = 1;
455
543
  if (total > capacity && total > 0) used = capacity / total;
@@ -470,6 +558,7 @@ var GameVehiclesDriving = (() => {
470
558
  const bogNow = bogging && Math.abs(state.u) < 1 && accelInput > 0;
471
559
  if (bogNow && !state.bogged) events.push({ type: "bogged" });
472
560
  state.bogged = bogNow;
561
+ state.brakeHeat = brakeHeat + brakeWork / (baseMass * BRAKE_HEAT_CAPACITY) - (brakeHeat - AMBIENT_C) * Math.min(1, dt / BRAKE_COOL_S);
473
562
  fFwd += -spec.drag * state.u * Math.abs(state.u);
474
563
  fRight += -spec.drag * state.w * Math.abs(state.w);
475
564
  const du = fFwd / mass - state.yawRate * state.w;
@@ -522,7 +611,32 @@ var GameVehiclesDriving = (() => {
522
611
  const thrust = torqueAtTop * lastRatio * spec.finalDrive * spec.efficiency / spec.wheelRadius;
523
612
  const rollingForce = spec.rolling * body.mass * 9.81;
524
613
  const drag = Math.max(0.01, (thrust - rollingForce) / (top * top));
525
- return { ...spec, mass: body.mass, wheels, fourWheelDrive: driveAll, engine, gears, drag, tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling } };
614
+ const carRatio = defaultSemiSpec.brakeForce / (defaultSemiSpec.mass * 9.81);
615
+ const heavyShare = Math.max(0, Math.min(1, (body.mass - 2e3) / 8e3));
616
+ const brakeForce = base.brakeForce ?? body.mass * 9.81 * (carRatio + (0.6 - carRatio) * heavyShare);
617
+ return { ...spec, mass: body.mass, wheels, fourWheelDrive: driveAll, engine, gears, drag, brakeForce, tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling } };
618
+ }
619
+
620
+ // src/semi/condition.ts
621
+ function conditionFromDamage(spec, report) {
622
+ const tyres = spec.wheels.map((wheel) => {
623
+ let best;
624
+ let distance = Infinity;
625
+ for (const tyre of report.tyres) {
626
+ const d = Math.hypot(tyre.x - wheel.x, tyre.z - wheel.z);
627
+ if (d < distance) {
628
+ distance = d;
629
+ best = tyre;
630
+ }
631
+ }
632
+ return best?.health ?? 1;
633
+ });
634
+ return {
635
+ tyres,
636
+ ...report.brakes != null ? { brakes: report.brakes } : {},
637
+ ...report.engine != null ? { engine: report.engine } : {},
638
+ ...report.suspension != null ? { suspension: report.suspension } : {}
639
+ };
526
640
  }
527
641
  return __toCommonJS(index_exports);
528
642
  })();