@adatechnology/game-vehicles-driving 0.2.9 → 0.2.11

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,141 @@ 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
+ /**
406
+ * The controls of a vehicle someone drives: pedals, wheel, gearbox (automatic or manual), handbrake (held or pulled), ignition, drive mode (4x2, 4x4,
407
+ * automatic 4x4), lamps (position, low, high, fog, interior), horn, alarm and radio. It turns key codes into a `SemiInput` for the semi driver and keeps
408
+ * what the cabin shows; it draws and plays nothing (an adapter reads `state` for the lamps and the sounds). Every key is an option.
409
+ */
410
+
411
+ type ControlCommand = 'ignition' | 'gearbox' | 'gear_up' | 'gear_down' | 'parking_brake' | 'drive_mode' | 'lights' | 'high_beam' | 'fog_lights' | 'interior_light' | 'alarm' | 'radio' | 'radio_source' | 'exit' | 'indicator_left' | 'indicator_right' | 'hazard' | 'wipers' | 'seatbelt';
412
+ type ControlKeyMap = {
413
+ readonly throttle: readonly string[];
414
+ readonly brake: readonly string[];
415
+ readonly left: readonly string[];
416
+ readonly right: readonly string[];
417
+ readonly handbrake: readonly string[];
418
+ readonly horn: readonly string[];
419
+ } & {
420
+ readonly [command in ControlCommand]: readonly string[];
421
+ };
422
+ /** Keys of a keyboard (`KeyboardEvent.code`). The host keeps them from its own bindings while someone drives. */
423
+ declare const DEFAULT_CONTROL_KEYS: ControlKeyMap;
424
+ type Gearbox = 'automatic' | 'manual';
425
+ type DriveMode = '4x2' | '4x4' | 'auto';
426
+ type LampMode = 'off' | 'position' | 'low' | 'high';
427
+ type AlarmState = 'off' | 'armed' | 'ringing';
428
+ type RadioSource = 'radio' | 'tape';
429
+ type Indicator = 'off' | 'left' | 'right' | 'hazard';
430
+ type Wipers = 'off' | 'slow' | 'fast';
431
+ interface ControlState {
432
+ ignition: boolean;
433
+ gearbox: Gearbox;
434
+ /** The gear the driver chose (manual): -1 reverse, 0 neutral, 1.. forward. */
435
+ gear: number;
436
+ /** Pulled and left pulled (P); Space holds it only while pressed. */
437
+ parkingBrake: boolean;
438
+ driveMode: DriveMode;
439
+ /** The front axle drives now (4x4, or automatic 4x4 that felt the rear slip). */
440
+ fourWheelEngaged: boolean;
441
+ lamps: {
442
+ mode: LampMode;
443
+ fog: boolean;
444
+ interior: boolean;
445
+ };
446
+ /** What the rear lamps show this step: brake (pedal or handbrake) and reversing. */
447
+ brakeLights: boolean;
448
+ reverseLights: boolean;
449
+ horn: boolean;
450
+ alarm: AlarmState;
451
+ radio: {
452
+ on: boolean;
453
+ source: RadioSource;
454
+ };
455
+ indicator: Indicator;
456
+ /** The indicator lamps are lit this instant (they blink at `blinkHz`); `tick` is true on the step the relay clicks. */
457
+ blinkOn: boolean;
458
+ tick: boolean;
459
+ wipers: Wipers;
460
+ seatbelt: boolean;
461
+ }
462
+ type ControlEvent = {
463
+ type: 'command';
464
+ command: ControlCommand;
465
+ state: ControlState;
466
+ } | {
467
+ type: 'refused';
468
+ command: ControlCommand;
469
+ reason: 'no_four_wheel_drive' | 'no_fog_lamps' | 'ignition_off';
470
+ };
471
+ interface VehicleControlOptions {
472
+ /** Forward gears of the gearbox. */
473
+ gears: number;
474
+ /** The vehicle can drive both axles (4x4 and automatic 4x4 are offered). */
475
+ fourWheelDrive?: boolean;
476
+ /** The vehicle has fog lamps. Default true. */
477
+ fogLamps?: boolean;
478
+ keys?: Partial<ControlKeyMap>;
479
+ gearbox?: Gearbox;
480
+ /** Automatic 4x4: the front axle comes in above this slip of a driven wheel (1 = sliding) and goes out after `releaseSeconds` below it. */
481
+ autoFourWheel?: {
482
+ slip: number;
483
+ releaseSeconds: number;
484
+ };
485
+ /** The engine runs from the start (a vehicle the player gets into with the engine on). Default false: the key turns it on (I) or the gas pedal starts it. */
486
+ running?: boolean;
487
+ /** Blinks a second of the indicators (default 1.5, the 90 a minute of the road codes). */
488
+ blinkHz?: number;
489
+ /** The steering wheel coming back this far (0..1 of the lock) after a turn cancels the indicator, as the column does. Default 0.15. */
490
+ indicatorCancel?: number;
491
+ onEvent?: (event: ControlEvent) => void;
492
+ }
493
+ /** What the controls read of the driver each step, and the one thing they change on it (which axles drive). */
494
+ interface ControlledDriver {
495
+ readonly spec: {
496
+ fourWheelDrive: boolean;
497
+ };
498
+ readonly state: {
499
+ u: number;
500
+ gear: number;
501
+ slip: readonly number[];
502
+ engineOn: boolean;
503
+ };
504
+ }
505
+ interface VehicleControls {
506
+ /** A key went down or up. True when it is a driving key (the host keeps it from its own bindings). */
507
+ key(code: string, down: boolean): boolean;
508
+ /** Runs a command as its key would (a button of a panel). */
509
+ command(command: ControlCommand): void;
510
+ /** The input for this step, pedals and wheel eased over `dt` seconds. */
511
+ input(dt: number, driver?: ControlledDriver): SemiInput;
512
+ /** After the driver stepped: the automatic 4x4 watches the slip, the ignition the engine. */
513
+ after(driver: ControlledDriver, dt: number): void;
514
+ readonly state: ControlState;
515
+ readonly keys: ControlKeyMap;
516
+ /** Lets go of every key (the window lost focus, the driver got out). */
517
+ release(): void;
518
+ }
519
+ declare function createVehicleControls(options: VehicleControlOptions): VehicleControls;
520
+
521
+ /** Matches a damage model's wheels to a driver's: each wheel of the spec takes the health of the tyre nearest to it. */
522
+
523
+ type DamagedTyre = {
524
+ /** Where the tyre is in the vehicle's space, metres: x forward, z to the side. */
525
+ readonly x: number;
526
+ readonly z: number;
527
+ /** 0 flat or gone, 1 whole. */
528
+ readonly health: number;
529
+ };
530
+ type DamageReport = {
531
+ readonly tyres: readonly DamagedTyre[];
532
+ readonly brakes?: number;
533
+ readonly engine?: number;
534
+ readonly suspension?: number;
535
+ };
536
+ declare function conditionFromDamage(spec: Pick<SemiSimSpec, 'wheels'>, report: DamageReport): VehicleCondition;
537
+
538
+ export { type AlarmState, type ArcadeOptions, type ArcadeSpec, type BodyDimensions, type ControlCommand, type ControlEvent, type ControlKeyMap, type ControlState, type ControlledDriver, type CrashRules, DEFAULT_CONTROL_KEYS, type DamageReport, type DamagedTyre, type DesignStats, type DriveInput, type DriveMode, type Driver, type DriverState, type DrivingBody, type DrivingEvent, type DrivingWorld, type FuelModel, type Gearbox, type Indicator, type LampMode, type RadioSource, 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, type VehicleControlOptions, type VehicleControls, type Wipers, bodyDamage, conditionFromDamage, createArcadeDriver, createSemiSimDriver, createSurfaces, createVehicleControls, defaultArcadeSpec, defaultFuelModel, defaultSemiSpec, defaultSurfaces, fuelRate, impactEnergy, runOver, semiSpecFromStats, speedLoss, surfaceGrip, vehicleCorners, vehicleDamageFromImpact };
@@ -21,10 +21,13 @@ var GameVehiclesDriving = (() => {
21
21
  // src/index.ts
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
+ DEFAULT_CONTROL_KEYS: () => DEFAULT_CONTROL_KEYS,
24
25
  bodyDamage: () => bodyDamage,
26
+ conditionFromDamage: () => conditionFromDamage,
25
27
  createArcadeDriver: () => createArcadeDriver,
26
28
  createSemiSimDriver: () => createSemiSimDriver,
27
29
  createSurfaces: () => createSurfaces,
30
+ createVehicleControls: () => createVehicleControls,
28
31
  defaultArcadeSpec: () => defaultArcadeSpec,
29
32
  defaultFuelModel: () => defaultFuelModel,
30
33
  defaultSemiSpec: () => defaultSemiSpec,
@@ -175,6 +178,7 @@ var GameVehiclesDriving = (() => {
175
178
  }
176
179
 
177
180
  // ../game-contracts/dist/index.js
181
+ var DEFAULT_BLAST_IMPULSE = { bodyArea: 0.7, lift: 0.15, minSpeed: 0.3, tntPerJoule: 0.24 / 2e5 };
178
182
  var DEFAULT_COST_ALLOCATION_BYTES = 1024 * 1024;
179
183
  var DEFERRED_QUEUE_ID = {
180
184
  DESTRUCTION_EVENTS: "destruction.events",
@@ -343,6 +347,14 @@ var GameVehiclesDriving = (() => {
343
347
  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));
344
348
  return e.peakTorque * t;
345
349
  };
350
+ var SLIDING_GRIP = 0.75;
351
+ var LOCKED_STEER = 0.15;
352
+ var AMBIENT_C = 25;
353
+ var BRAKE_FADE_FROM_C = 350;
354
+ var BRAKE_FADE_TO_C = 650;
355
+ var BRAKE_FADED = 0.5;
356
+ var BRAKE_HEAT_CAPACITY = 3;
357
+ var BRAKE_COOL_S = 60;
346
358
  function createSemiSimDriver(specification = {}, options = {}) {
347
359
  const spec = { ...defaultSemiSpec, ...specification };
348
360
  const surfaces = options.surfaces ?? createSurfaces();
@@ -408,6 +420,7 @@ var GameVehiclesDriving = (() => {
408
420
  const mass = baseMass + cargo;
409
421
  const engineHealth = clamp2(cond.engine ?? 1, 0, 1);
410
422
  const suspensionHealth = clamp2(cond.suspension ?? 1, 0, 1);
423
+ const brakeHealth = clamp2(cond.brakes ?? 1, 0, 1);
411
424
  const inertia = (spec.yawInertia ?? baseMass * (wheelbase * wheelbase + track * track) / 12) * (mass / baseMass);
412
425
  const pitchInertia = mass * (wheelbase * wheelbase + 1) / 12;
413
426
  const rollInertia = mass * (track * track + 1) / 12;
@@ -492,6 +505,9 @@ var GameVehiclesDriving = (() => {
492
505
  sumNA += load * w.x;
493
506
  sumNB += load * w.z;
494
507
  }
508
+ const brakeHeat = state.brakeHeat ?? AMBIENT_C;
509
+ const fade = 1 - clamp2((brakeHeat - BRAKE_FADE_FROM_C) / (BRAKE_FADE_TO_C - BRAKE_FADE_FROM_C), 0, 1) * (1 - BRAKE_FADED);
510
+ let brakeWork = 0;
495
511
  let fFwd = 0;
496
512
  let fRight = 0;
497
513
  let torque = 0;
@@ -514,14 +530,17 @@ var GameVehiclesDriving = (() => {
514
530
  let fl = 0;
515
531
  if (driveWheels.some((d) => d.i === i)) fl += driveForceTotal / drivenCount;
516
532
  const locked = handbrake && w.handbrake;
517
- const brakeShare = spec.brakeForce * brakeInput * ws.n / loadSum;
533
+ const brakeShare = spec.brakeForce * brakeHealth * fade * brakeInput * ws.n / loadSum;
518
534
  const moving = Math.abs(vl) > 0.05;
519
- if (locked) fl += moving ? -Math.sign(vl) * capacity : 0;
535
+ const lockedByBrake = spec.abs === false && moving && brakeShare > capacity;
536
+ if (locked || lockedByBrake) fl += moving ? -Math.sign(vl) * capacity * (lockedByBrake ? SLIDING_GRIP : 1) : 0;
520
537
  else if (brakeShare > 0 && moving) fl += -Math.sign(vl) * brakeShare;
538
+ if (brakeShare > 0 && moving) brakeWork += Math.min(brakeShare, capacity) * Math.abs(vl) * dt;
521
539
  const rolling = ws.bog && accelInput > 0 ? spec.bogResistance : spec.rolling * (ws.flat ? 6 : 1);
522
540
  const alpha = Math.atan2(vc, Math.abs(vl) + 0.5);
523
541
  let fc = -Math.tanh(spec.tyre.corneringStiffness * alpha) * capacity;
524
542
  if (locked) fc *= 0.6;
543
+ if (lockedByBrake) fc *= LOCKED_STEER;
525
544
  const total = Math.hypot(fl, fc);
526
545
  let used = 1;
527
546
  if (total > capacity && total > 0) used = capacity / total;
@@ -542,6 +561,7 @@ var GameVehiclesDriving = (() => {
542
561
  const bogNow = bogging && Math.abs(state.u) < 1 && accelInput > 0;
543
562
  if (bogNow && !state.bogged) events.push({ type: "bogged" });
544
563
  state.bogged = bogNow;
564
+ state.brakeHeat = brakeHeat + brakeWork / (baseMass * BRAKE_HEAT_CAPACITY) - (brakeHeat - AMBIENT_C) * Math.min(1, dt / BRAKE_COOL_S);
545
565
  fFwd += -spec.drag * state.u * Math.abs(state.u);
546
566
  fRight += -spec.drag * state.w * Math.abs(state.w);
547
567
  const du = fFwd / mass - state.yawRate * state.w;
@@ -594,7 +614,233 @@ var GameVehiclesDriving = (() => {
594
614
  const thrust = torqueAtTop * lastRatio * spec.finalDrive * spec.efficiency / spec.wheelRadius;
595
615
  const rollingForce = spec.rolling * body.mass * 9.81;
596
616
  const drag = Math.max(0.01, (thrust - rollingForce) / (top * top));
597
- return { ...spec, mass: body.mass, wheels, fourWheelDrive: driveAll, engine, gears, drag, tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling } };
617
+ const carRatio = defaultSemiSpec.brakeForce / (defaultSemiSpec.mass * 9.81);
618
+ const heavyShare = Math.max(0, Math.min(1, (body.mass - 2e3) / 8e3));
619
+ const brakeForce = base.brakeForce ?? body.mass * 9.81 * (carRatio + (0.6 - carRatio) * heavyShare);
620
+ return { ...spec, mass: body.mass, wheels, fourWheelDrive: driveAll, engine, gears, drag, brakeForce, tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling } };
621
+ }
622
+
623
+ // src/controls.ts
624
+ var DEFAULT_CONTROL_KEYS = {
625
+ throttle: ["KeyW", "ArrowUp"],
626
+ brake: ["KeyS", "ArrowDown"],
627
+ left: ["KeyA", "ArrowLeft"],
628
+ right: ["KeyD", "ArrowRight"],
629
+ handbrake: ["Space"],
630
+ horn: ["KeyH"],
631
+ ignition: ["KeyI"],
632
+ gearbox: ["KeyM"],
633
+ gear_up: ["KeyE"],
634
+ gear_down: ["KeyQ"],
635
+ parking_brake: ["KeyP"],
636
+ drive_mode: ["KeyG"],
637
+ lights: ["KeyL"],
638
+ high_beam: ["KeyK"],
639
+ fog_lights: ["KeyN"],
640
+ interior_light: ["KeyU"],
641
+ alarm: ["KeyY"],
642
+ radio: ["KeyR"],
643
+ radio_source: ["KeyT"],
644
+ exit: ["KeyF"],
645
+ indicator_left: ["Comma"],
646
+ indicator_right: ["Period"],
647
+ hazard: ["KeyB"],
648
+ wipers: ["KeyO"],
649
+ seatbelt: ["KeyJ"]
650
+ };
651
+ function createVehicleControls(options) {
652
+ const keys = { ...DEFAULT_CONTROL_KEYS, ...options.keys };
653
+ const auto4 = options.autoFourWheel ?? { slip: 0.18, releaseSeconds: 2 };
654
+ const hasFog = options.fogLamps !== false;
655
+ const held = /* @__PURE__ */ new Set();
656
+ const any = (codes) => codes.some((c) => held.has(c));
657
+ const commandOf = /* @__PURE__ */ new Map();
658
+ for (const [command2, codes] of Object.entries(keys)) if (!["throttle", "brake", "left", "right", "handbrake", "horn"].includes(command2)) for (const code of codes) commandOf.set(code, command2);
659
+ const driving = new Set(Object.values(keys).flat());
660
+ const state = {
661
+ ignition: !!options.running,
662
+ gearbox: options.gearbox ?? "automatic",
663
+ gear: 1,
664
+ parkingBrake: false,
665
+ driveMode: options.fourWheelDrive ? "auto" : "4x2",
666
+ fourWheelEngaged: false,
667
+ lamps: { mode: "off", fog: false, interior: false },
668
+ brakeLights: false,
669
+ reverseLights: false,
670
+ horn: false,
671
+ alarm: "off",
672
+ radio: { on: false, source: "radio" },
673
+ indicator: "off",
674
+ blinkOn: false,
675
+ tick: false,
676
+ wipers: "off",
677
+ seatbelt: false
678
+ };
679
+ const blinkHz = options.blinkHz ?? 1.5;
680
+ const cancelAt = options.indicatorCancel ?? 0.15;
681
+ let blink = 0;
682
+ let turned = 0;
683
+ let throttle = 0;
684
+ let brake = 0;
685
+ let steer = 0;
686
+ let calm = 0;
687
+ const emit = (event) => options.onEvent?.(event);
688
+ const LAMPS = ["off", "position", "low"];
689
+ const MODES = ["4x2", "4x4", "auto"];
690
+ const command = (c) => {
691
+ switch (c) {
692
+ case "ignition":
693
+ state.ignition = !state.ignition;
694
+ if (state.ignition && state.alarm !== "off") state.alarm = "off";
695
+ break;
696
+ case "gearbox":
697
+ state.gearbox = state.gearbox === "automatic" ? "manual" : "automatic";
698
+ state.gear = Math.max(1, state.gear);
699
+ break;
700
+ case "gear_up":
701
+ case "gear_down":
702
+ if (state.gearbox !== "manual") return;
703
+ state.gear = Math.max(-1, Math.min(options.gears, state.gear + (c === "gear_up" ? 1 : -1)));
704
+ break;
705
+ case "parking_brake":
706
+ state.parkingBrake = !state.parkingBrake;
707
+ break;
708
+ case "drive_mode": {
709
+ if (!options.fourWheelDrive) return emit({ type: "refused", command: c, reason: "no_four_wheel_drive" });
710
+ state.driveMode = MODES[(MODES.indexOf(state.driveMode) + 1) % MODES.length];
711
+ state.fourWheelEngaged = state.driveMode === "4x4";
712
+ break;
713
+ }
714
+ case "lights":
715
+ state.lamps.mode = state.lamps.mode === "high" ? "off" : LAMPS[(LAMPS.indexOf(state.lamps.mode) + 1) % LAMPS.length];
716
+ break;
717
+ case "high_beam":
718
+ state.lamps.mode = state.lamps.mode === "high" ? "low" : "high";
719
+ break;
720
+ case "fog_lights":
721
+ if (!hasFog) return emit({ type: "refused", command: c, reason: "no_fog_lamps" });
722
+ state.lamps.fog = !state.lamps.fog;
723
+ if (state.lamps.fog && state.lamps.mode === "off") state.lamps.mode = "position";
724
+ break;
725
+ case "interior_light":
726
+ state.lamps.interior = !state.lamps.interior;
727
+ break;
728
+ case "alarm":
729
+ state.alarm = state.alarm === "off" ? "armed" : state.alarm === "armed" ? "ringing" : "off";
730
+ break;
731
+ case "radio":
732
+ state.radio.on = !state.radio.on;
733
+ break;
734
+ case "radio_source":
735
+ state.radio.source = state.radio.source === "radio" ? "tape" : "radio";
736
+ break;
737
+ case "exit":
738
+ break;
739
+ case "indicator_left":
740
+ case "indicator_right": {
741
+ const side = c === "indicator_left" ? "left" : "right";
742
+ if (state.indicator === "hazard") return;
743
+ state.indicator = state.indicator === side ? "off" : side;
744
+ turned = 0;
745
+ blink = 0;
746
+ break;
747
+ }
748
+ case "hazard":
749
+ state.indicator = state.indicator === "hazard" ? "off" : "hazard";
750
+ blink = 0;
751
+ break;
752
+ case "wipers":
753
+ state.wipers = state.wipers === "off" ? "slow" : state.wipers === "slow" ? "fast" : "off";
754
+ break;
755
+ case "seatbelt":
756
+ state.seatbelt = !state.seatbelt;
757
+ break;
758
+ }
759
+ emit({ type: "command", command: c, state });
760
+ };
761
+ const ease = (from, to, rate, dt) => from + Math.sign(to - from) * Math.min(Math.abs(to - from), rate * dt);
762
+ return {
763
+ keys,
764
+ state,
765
+ key(code, down) {
766
+ if (!driving.has(code)) return false;
767
+ const was = held.has(code);
768
+ if (down) held.add(code);
769
+ else held.delete(code);
770
+ const c = commandOf.get(code);
771
+ if (c && down && !was) command(c);
772
+ return true;
773
+ },
774
+ command,
775
+ input(dt, driver) {
776
+ const gas = any(keys.throttle) ? 1 : 0;
777
+ const stop = any(keys.brake) ? 1 : 0;
778
+ const wheel = (any(keys.left) ? 1 : 0) - (any(keys.right) ? 1 : 0);
779
+ throttle = ease(throttle, gas, gas > throttle ? 4 : 8, dt);
780
+ brake = ease(brake, stop, stop > brake ? 6 : 10, dt);
781
+ steer = ease(steer, wheel, wheel !== 0 && Math.sign(wheel) === Math.sign(steer || wheel) ? 3 : 6, dt);
782
+ const handbrake = state.parkingBrake || any(keys.handbrake);
783
+ state.tick = false;
784
+ if (state.indicator !== "off") {
785
+ const before = Math.floor(blink * blinkHz * 2);
786
+ blink += dt;
787
+ const now = Math.floor(blink * blinkHz * 2);
788
+ state.blinkOn = now % 2 === 0;
789
+ state.tick = now !== before;
790
+ if (state.indicator !== "hazard") {
791
+ const way = state.indicator === "left" ? steer : -steer;
792
+ if (way > 0.5) turned = 1;
793
+ else if (turned && way < cancelAt) state.indicator = "off";
794
+ }
795
+ } else state.blinkOn = false;
796
+ state.horn = any(keys.horn);
797
+ const gasNow = state.ignition ? throttle : 0;
798
+ const reversing = state.gearbox === "manual" ? state.gear < 0 : (driver?.state.gear ?? 1) < 0;
799
+ state.reverseLights = reversing;
800
+ state.brakeLights = handbrake || (reversing ? gasNow > 0.05 && (driver?.state.u ?? 0) < -0.3 : brake > 0.05);
801
+ if (state.gearbox === "automatic") return { throttle: gasNow, brake, steer, handbrake, horn: state.horn };
802
+ return state.gear < 0 ? { throttle: brake, brake: gasNow, steer, handbrake, horn: state.horn, gear: state.gear } : { throttle: gasNow, brake, steer, handbrake, horn: state.horn, gear: state.gear };
803
+ },
804
+ after(driver, dt) {
805
+ if (driver.state.engineOn && !state.ignition && driver.state.u !== 0) state.ignition = true;
806
+ if (!state.ignition && driver.state.engineOn) driver.state.engineOn = false;
807
+ if (state.driveMode === "auto" && options.fourWheelDrive) {
808
+ const slip = Math.max(0, ...driver.state.slip);
809
+ if (slip > auto4.slip) {
810
+ state.fourWheelEngaged = true;
811
+ calm = 0;
812
+ } else if (state.fourWheelEngaged && (calm += dt) > auto4.releaseSeconds) state.fourWheelEngaged = false;
813
+ } else state.fourWheelEngaged = state.driveMode === "4x4";
814
+ driver.spec.fourWheelDrive = !!options.fourWheelDrive && state.fourWheelEngaged;
815
+ },
816
+ release() {
817
+ held.clear();
818
+ throttle = brake = steer = 0;
819
+ state.horn = false;
820
+ }
821
+ };
822
+ }
823
+
824
+ // src/semi/condition.ts
825
+ function conditionFromDamage(spec, report) {
826
+ const tyres = spec.wheels.map((wheel) => {
827
+ let best;
828
+ let distance = Infinity;
829
+ for (const tyre of report.tyres) {
830
+ const d = Math.hypot(tyre.x - wheel.x, tyre.z - wheel.z);
831
+ if (d < distance) {
832
+ distance = d;
833
+ best = tyre;
834
+ }
835
+ }
836
+ return best?.health ?? 1;
837
+ });
838
+ return {
839
+ tyres,
840
+ ...report.brakes != null ? { brakes: report.brakes } : {},
841
+ ...report.engine != null ? { engine: report.engine } : {},
842
+ ...report.suspension != null ? { suspension: report.suspension } : {}
843
+ };
598
844
  }
599
845
  return __toCommonJS(index_exports);
600
846
  })();