@adatechnology/game-vehicles-driving 0.2.11 → 0.2.12

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.
@@ -22,22 +22,34 @@ var GameVehiclesDriving = (() => {
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
24
  DEFAULT_CONTROL_KEYS: () => DEFAULT_CONTROL_KEYS,
25
+ DEFAULT_CRUISE: () => DEFAULT_CRUISE,
26
+ DEFAULT_EJECTION: () => DEFAULT_EJECTION,
27
+ DEFAULT_LOADS: () => DEFAULT_LOADS,
28
+ DEFAULT_PARKING_SENSOR: () => DEFAULT_PARKING_SENSOR,
29
+ DEFAULT_TRACTION: () => DEFAULT_TRACTION,
30
+ DRIVE_PROFILES: () => DRIVE_PROFILES,
25
31
  bodyDamage: () => bodyDamage,
32
+ bumperDistance: () => bumperDistance,
26
33
  conditionFromDamage: () => conditionFromDamage,
27
34
  createArcadeDriver: () => createArcadeDriver,
28
35
  createSemiSimDriver: () => createSemiSimDriver,
29
36
  createSurfaces: () => createSurfaces,
30
37
  createVehicleControls: () => createVehicleControls,
38
+ createVehicleSystems: () => createVehicleSystems,
39
+ cruiseThrottle: () => cruiseThrottle,
31
40
  defaultArcadeSpec: () => defaultArcadeSpec,
32
41
  defaultFuelModel: () => defaultFuelModel,
33
42
  defaultSemiSpec: () => defaultSemiSpec,
34
43
  defaultSurfaces: () => defaultSurfaces,
44
+ ejectionOf: () => ejectionOf,
35
45
  fuelRate: () => fuelRate,
36
46
  impactEnergy: () => impactEnergy,
47
+ parkingBeepInterval: () => parkingBeepInterval,
37
48
  runOver: () => runOver,
38
49
  semiSpecFromStats: () => semiSpecFromStats,
39
50
  speedLoss: () => speedLoss,
40
51
  surfaceGrip: () => surfaceGrip,
52
+ tractionLimit: () => tractionLimit,
41
53
  vehicleCorners: () => vehicleCorners,
42
54
  vehicleDamageFromImpact: () => vehicleDamageFromImpact
43
55
  });
@@ -313,7 +325,15 @@ var GameVehiclesDriving = (() => {
313
325
  var clamp2 = (x, a, b) => Math.max(a, Math.min(b, x));
314
326
  var MAX_SUBSTEP = 1 / 240;
315
327
  var FLAT_BELOW = 0.2;
328
+ var RIM_RADIUS_SHARE = 0.7;
329
+ var RIM_FRICTION = 0.4;
330
+ var RIM_ROLLING = 0.25;
331
+ var FLAT_SAG = 0.05;
332
+ var FLAT_ROLLING = 6;
316
333
  var STALL_HEALTH = 0.3;
334
+ var CLUTCH_BITE = 0.7;
335
+ var STALL_RPM = 0.5;
336
+ var STALL_GAS = 0.35;
317
337
  var defaultSemiSpec = {
318
338
  mass: 1300,
319
339
  centreHeight: 0.5,
@@ -454,7 +474,14 @@ var GameVehiclesDriving = (() => {
454
474
  const accelInput = reversing ? input.brake : input.throttle;
455
475
  const brakeInput = clamp2(reversing ? input.throttle : input.brake, 0, 1);
456
476
  const ratio = ratioOf(state.gear) * spec.finalDrive;
457
- state.rpm = clamp2(wheelRpm * ratio, spec.engine.idleRpm, spec.engine.redlineRpm + 200);
477
+ const engaged = input.clutch == null || state.gear === 0 ? 1 : 1 - clamp2(input.clutch, 0, 1);
478
+ const coupled = wheelRpm * ratio;
479
+ const free = spec.engine.idleRpm + clamp2(accelInput, 0, 1) * (spec.engine.redlineRpm * 0.7 - spec.engine.idleRpm);
480
+ state.rpm = clamp2(coupled * engaged + Math.max(coupled, free) * (1 - engaged), spec.engine.idleRpm, spec.engine.redlineRpm + 200);
481
+ if (input.clutch != null && state.gear !== 0 && state.engineOn && engaged > CLUTCH_BITE && coupled < spec.engine.idleRpm * STALL_RPM && accelInput < STALL_GAS) {
482
+ state.engineOn = false;
483
+ events.push({ type: "stall" });
484
+ }
458
485
  if (!state.engineOn || state.fuel <= 0) state.rpm = 0;
459
486
  let throttle = clamp2(accelInput, 0, 1);
460
487
  if (engineHealth < STALL_HEALTH && throttle > 0.8) {
@@ -465,12 +492,13 @@ var GameVehiclesDriving = (() => {
465
492
  }
466
493
  throttle *= 0.5;
467
494
  } else stallWatch.throttleTime = 0;
468
- if (!state.engineOn && accelInput > 0 && Math.abs(state.u) < 0.5 && engineHealth >= STALL_HEALTH && state.fuel > 0) state.engineOn = true;
495
+ const starting = input.start ?? (accelInput > 0 && Math.abs(state.u) < 0.5);
496
+ if (!state.engineOn && starting && engineHealth >= STALL_HEALTH && state.fuel > 0) state.engineOn = true;
469
497
  if (state.fuel <= 0 && state.engineOn) {
470
498
  state.engineOn = false;
471
499
  events.push({ type: "out_of_fuel" });
472
500
  }
473
- const engineTorque = state.engineOn ? torqueAt(spec, state.rpm) * throttle * (0.55 + 0.45 * engineHealth) : 0;
501
+ const engineTorque = state.engineOn ? torqueAt(spec, state.rpm) * throttle * (0.55 + 0.45 * engineHealth) * clamp2(input.powerLimit ?? 1, 0, 1) * engaged : 0;
474
502
  const driveForceTotal = state.gear === 0 ? 0 : (reversing ? -1 : 1) * engineTorque * ratio * spec.efficiency / spec.wheelRadius;
475
503
  if (state.engineOn) {
476
504
  const burned = fuelRate(fuelModel, state.rpm / spec.engine.redlineRpm, throttle, cargo / baseMass, engineHealth) * dt;
@@ -488,19 +516,21 @@ var GameVehiclesDriving = (() => {
488
516
  const wz = state.z - w.x * sn + w.z * cs;
489
517
  const ground = world.ground ? world.ground(wx, wz) : {};
490
518
  const surface = surfaces.get(contamination && contamination.left > 0 && surfaces.get(contamination.id).grip < surfaces.get(ground.surface ?? "dry_asphalt").grip ? contamination.id : ground.surface ?? "dry_asphalt");
519
+ const tyreHealth = cond.tyres ? clamp2(cond.tyres[i] ?? 1, 0, 1) : 1;
520
+ const rim = !!cond.rims?.[i];
521
+ const flat = rim || tyreHealth < FLAT_BELOW;
522
+ const radius = rim ? spec.rimRadius ?? spec.wheelRadius * RIM_RADIUS_SHARE : flat ? spec.wheelRadius - FLAT_SAG : spec.wheelRadius;
491
523
  const top = state.height + w.x * state.pitch - w.z * state.roll;
492
- const length = top - (ground.height ?? 0) - spec.wheelRadius;
524
+ const length = top - (ground.height ?? 0) - radius;
493
525
  const compression = ride - length;
494
526
  const rate = -(state.heightRate + w.x * state.pitchRate - w.z * state.rollRate);
495
527
  const k = spec.suspension.spring * (0.5 + 0.5 * suspensionHealth);
496
528
  const c = spec.suspension.damper * (0.5 + 0.5 * suspensionHealth);
497
529
  const load = Math.max(0, k * compression + c * rate);
498
- const tyreHealth = cond.tyres ? clamp2(cond.tyres[i] ?? 1, 0, 1) : 1;
499
- const flat = tyreHealth < FLAT_BELOW;
500
530
  const bog = !!surface.bogs && !spec.fourWheelDrive;
501
531
  if (bog) bogging = true;
502
- const grip = surfaceGrip(surface, state.u) * (flat ? 0.5 : 1);
503
- wheelState.push({ n: load, grip, surface: surface.id, bog, flat });
532
+ const grip = surfaceGrip(surface, state.u) * (rim ? RIM_FRICTION / spec.tyre.friction : flat ? 0.5 : 1);
533
+ wheelState.push({ n: load, grip, surface: surface.id, bog, flat, rim });
504
534
  sumN += load;
505
535
  sumNA += load * w.x;
506
536
  sumNB += load * w.z;
@@ -536,7 +566,7 @@ var GameVehiclesDriving = (() => {
536
566
  if (locked || lockedByBrake) fl += moving ? -Math.sign(vl) * capacity * (lockedByBrake ? SLIDING_GRIP : 1) : 0;
537
567
  else if (brakeShare > 0 && moving) fl += -Math.sign(vl) * brakeShare;
538
568
  if (brakeShare > 0 && moving) brakeWork += Math.min(brakeShare, capacity) * Math.abs(vl) * dt;
539
- const rolling = ws.bog && accelInput > 0 ? spec.bogResistance : spec.rolling * (ws.flat ? 6 : 1);
569
+ const rolling = ws.bog && accelInput > 0 ? spec.bogResistance : ws.rim ? RIM_ROLLING : spec.rolling * (ws.flat ? FLAT_ROLLING : 1);
540
570
  const alpha = Math.atan2(vc, Math.abs(vl) + 0.5);
541
571
  let fc = -Math.tanh(spec.tyre.corneringStiffness * alpha) * capacity;
542
572
  if (locked) fc *= 0.6;
@@ -620,6 +650,23 @@ var GameVehiclesDriving = (() => {
620
650
  return { ...spec, mass: body.mass, wheels, fourWheelDrive: driveAll, engine, gears, drag, brakeForce, tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling } };
621
651
  }
622
652
 
653
+ // src/driving-aids.ts
654
+ var DRIVE_PROFILES = {
655
+ eco: { throttle: 0.7, upshift: 0.55, downshift: 0.25 },
656
+ normal: { throttle: 1, upshift: 0.85, downshift: 0.35 },
657
+ sport: { throttle: 1, upshift: 0.97, downshift: 0.55 }
658
+ };
659
+ var DEFAULT_TRACTION = { slip: 0.97, floor: 0.35 };
660
+ function tractionLimit(slips, options = DEFAULT_TRACTION) {
661
+ const worst = Math.max(0, ...slips);
662
+ if (worst <= options.slip) return 1;
663
+ return Math.max(options.floor, 1 - (worst - options.slip) / (1 - options.slip));
664
+ }
665
+ var DEFAULT_CRUISE = { gain: 0.35, minSpeed: 8.3 };
666
+ function cruiseThrottle(target, speed, options = DEFAULT_CRUISE) {
667
+ return Math.max(0, Math.min(1, (target - speed) * options.gain + 0.12));
668
+ }
669
+
623
670
  // src/controls.ts
624
671
  var DEFAULT_CONTROL_KEYS = {
625
672
  throttle: ["KeyW", "ArrowUp"],
@@ -628,6 +675,9 @@ var GameVehiclesDriving = (() => {
628
675
  right: ["KeyD", "ArrowRight"],
629
676
  handbrake: ["Space"],
630
677
  horn: ["KeyH"],
678
+ clutch: ["KeyC"],
679
+ flash: ["KeyX"],
680
+ washer: ["KeyZ"],
631
681
  ignition: ["KeyI"],
632
682
  gearbox: ["KeyM"],
633
683
  gear_up: ["KeyE"],
@@ -641,13 +691,21 @@ var GameVehiclesDriving = (() => {
641
691
  alarm: ["KeyY"],
642
692
  radio: ["KeyR"],
643
693
  radio_source: ["KeyT"],
694
+ radio_track: ["BracketRight"],
644
695
  exit: ["KeyF"],
645
696
  indicator_left: ["Comma"],
646
697
  indicator_right: ["Period"],
647
698
  hazard: ["KeyB"],
648
699
  wipers: ["KeyO"],
649
- seatbelt: ["KeyJ"]
700
+ seatbelt: ["KeyJ"],
701
+ drive_profile: ["Digit7"],
702
+ traction_control: ["Digit8"],
703
+ abs: ["Digit9"],
704
+ cruise: ["Digit0"],
705
+ climate: ["Minus"],
706
+ demist: ["Equal"]
650
707
  };
708
+ var HELD_KEYS = ["throttle", "brake", "left", "right", "handbrake", "horn", "clutch", "flash", "washer"];
651
709
  function createVehicleControls(options) {
652
710
  const keys = { ...DEFAULT_CONTROL_KEYS, ...options.keys };
653
711
  const auto4 = options.autoFourWheel ?? { slip: 0.18, releaseSeconds: 2 };
@@ -655,7 +713,7 @@ var GameVehiclesDriving = (() => {
655
713
  const held = /* @__PURE__ */ new Set();
656
714
  const any = (codes) => codes.some((c) => held.has(c));
657
715
  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);
716
+ for (const [command2, codes] of Object.entries(keys)) if (!HELD_KEYS.includes(command2)) for (const code of codes) commandOf.set(code, command2);
659
717
  const driving = new Set(Object.values(keys).flat());
660
718
  const state = {
661
719
  ignition: !!options.running,
@@ -664,18 +722,29 @@ var GameVehiclesDriving = (() => {
664
722
  parkingBrake: false,
665
723
  driveMode: options.fourWheelDrive ? "auto" : "4x2",
666
724
  fourWheelEngaged: false,
667
- lamps: { mode: "off", fog: false, interior: false },
725
+ lamps: { mode: "off", fog: false, interior: false, flash: false },
668
726
  brakeLights: false,
669
727
  reverseLights: false,
670
728
  horn: false,
671
729
  alarm: "off",
672
- radio: { on: false, source: "radio" },
730
+ radio: { on: false, source: "radio", track: 0 },
673
731
  indicator: "off",
674
732
  blinkOn: false,
675
733
  tick: false,
676
734
  wipers: "off",
677
- seatbelt: false
735
+ washer: false,
736
+ seatbelt: false,
737
+ clutch: 0,
738
+ profile: "normal",
739
+ tractionControl: true,
740
+ abs: true,
741
+ cruise: { on: false, speed: 0 },
742
+ climate: { on: false, demist: false },
743
+ cranking: false
678
744
  };
745
+ let crankAsked = false;
746
+ let lastSpeed = 0;
747
+ let engineRunning = !!options.running;
679
748
  const blinkHz = options.blinkHz ?? 1.5;
680
749
  const cancelAt = options.indicatorCancel ?? 0.15;
681
750
  let blink = 0;
@@ -687,15 +756,19 @@ var GameVehiclesDriving = (() => {
687
756
  const emit = (event) => options.onEvent?.(event);
688
757
  const LAMPS = ["off", "position", "low"];
689
758
  const MODES = ["4x2", "4x4", "auto"];
759
+ const WIPERS = ["off", "intermittent", "slow", "fast"];
760
+ const PROFILES = ["eco", "normal", "sport"];
690
761
  const command = (c) => {
691
762
  switch (c) {
692
763
  case "ignition":
693
764
  state.ignition = !state.ignition;
694
765
  if (state.ignition && state.alarm !== "off") state.alarm = "off";
766
+ if (state.ignition) crankAsked = true;
767
+ else state.cruise.on = false;
695
768
  break;
696
769
  case "gearbox":
697
770
  state.gearbox = state.gearbox === "automatic" ? "manual" : "automatic";
698
- state.gear = Math.max(1, state.gear);
771
+ state.gear = options.clutchPedal && state.gearbox === "manual" && Math.abs(lastSpeed) < 0.5 ? 0 : Math.max(1, state.gear);
699
772
  break;
700
773
  case "gear_up":
701
774
  case "gear_down":
@@ -734,6 +807,9 @@ var GameVehiclesDriving = (() => {
734
807
  case "radio_source":
735
808
  state.radio.source = state.radio.source === "radio" ? "tape" : "radio";
736
809
  break;
810
+ case "radio_track":
811
+ state.radio.track = (state.radio.track + 1) % Math.max(1, options.radioTracks ?? 3);
812
+ break;
737
813
  case "exit":
738
814
  break;
739
815
  case "indicator_left":
@@ -750,7 +826,30 @@ var GameVehiclesDriving = (() => {
750
826
  blink = 0;
751
827
  break;
752
828
  case "wipers":
753
- state.wipers = state.wipers === "off" ? "slow" : state.wipers === "slow" ? "fast" : "off";
829
+ state.wipers = WIPERS[(WIPERS.indexOf(state.wipers) + 1) % WIPERS.length];
830
+ break;
831
+ case "drive_profile":
832
+ state.profile = PROFILES[(PROFILES.indexOf(state.profile) + 1) % PROFILES.length];
833
+ break;
834
+ case "traction_control":
835
+ state.tractionControl = !state.tractionControl;
836
+ break;
837
+ case "abs":
838
+ state.abs = !state.abs;
839
+ break;
840
+ case "cruise":
841
+ if (state.cruise.on) state.cruise.on = false;
842
+ else if (!engineRunning) return emit({ type: "refused", command: c, reason: "ignition_off" });
843
+ else if (lastSpeed < DEFAULT_CRUISE.minSpeed) return emit({ type: "refused", command: c, reason: "too_slow" });
844
+ else state.cruise = { on: true, speed: lastSpeed };
845
+ break;
846
+ case "climate":
847
+ state.climate.on = !state.climate.on;
848
+ if (!state.climate.on) state.climate.demist = false;
849
+ break;
850
+ case "demist":
851
+ state.climate.demist = !state.climate.demist;
852
+ if (state.climate.demist) state.climate.on = true;
754
853
  break;
755
854
  case "seatbelt":
756
855
  state.seatbelt = !state.seatbelt;
@@ -794,16 +893,48 @@ var GameVehiclesDriving = (() => {
794
893
  }
795
894
  } else state.blinkOn = false;
796
895
  state.horn = any(keys.horn);
797
- const gasNow = state.ignition ? throttle : 0;
896
+ state.lamps.flash = any(keys.flash);
897
+ state.washer = any(keys.washer);
898
+ state.clutch = ease(state.clutch, any(keys.clutch) ? 1 : 0, any(keys.clutch) ? 8 : 2.5, dt);
899
+ const speed = driver?.state.u ?? 0;
900
+ lastSpeed = speed;
901
+ if (state.cruise.on && (brake > 0.05 || state.clutch > 0.5 || handbrake)) {
902
+ state.cruise.on = false;
903
+ emit({ type: "cruise_cancelled" });
904
+ }
905
+ const tune = DRIVE_PROFILES[state.profile];
906
+ let gasNow = state.ignition ? throttle * tune.throttle : 0;
907
+ if (state.cruise.on && state.ignition) gasNow = Math.max(gasNow, cruiseThrottle(state.cruise.speed, speed));
908
+ const gasStart = state.gearbox === "automatic" && state.ignition && !engineRunning && throttle > 0 && Math.abs(speed) < 0.5;
909
+ state.cranking = false;
910
+ if (crankAsked || gasStart) {
911
+ crankAsked = false;
912
+ if (options.canCrank && !options.canCrank()) emit({ type: "refused", command: "ignition", reason: "battery_flat" });
913
+ else {
914
+ state.cranking = true;
915
+ options.onCrank?.();
916
+ }
917
+ }
918
+ const driven = (driver?.state.slip ?? []).filter((_, i) => driver?.spec.wheels?.[i]?.drive ?? true);
919
+ const powerLimit = (state.tractionControl ? tractionLimit(driven) : 1) * (options.powerLimit?.() ?? 1);
798
920
  const reversing = state.gearbox === "manual" ? state.gear < 0 : (driver?.state.gear ?? 1) < 0;
799
921
  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 };
922
+ state.brakeLights = handbrake || (reversing ? gasNow > 0.05 && speed < -0.3 : brake > 0.05);
923
+ const common = { steer, handbrake, horn: state.horn, start: state.cranking, powerLimit };
924
+ if (state.gearbox === "automatic") return { ...common, throttle: gasNow, brake };
925
+ const pedals = state.gear < 0 ? { throttle: brake, brake: gasNow } : { throttle: gasNow, brake };
926
+ return { ...common, ...pedals, gear: state.gear, ...options.clutchPedal ? { clutch: state.clutch } : {} };
803
927
  },
804
928
  after(driver, dt) {
805
- if (driver.state.engineOn && !state.ignition && driver.state.u !== 0) state.ignition = true;
806
929
  if (!state.ignition && driver.state.engineOn) driver.state.engineOn = false;
930
+ engineRunning = driver.state.engineOn;
931
+ if (!engineRunning) state.cruise.on = false;
932
+ const spec = driver.spec;
933
+ spec.abs = state.abs;
934
+ if (spec.upshiftFraction != null) {
935
+ spec.upshiftFraction = DRIVE_PROFILES[state.profile].upshift;
936
+ spec.downshiftFraction = DRIVE_PROFILES[state.profile].downshift;
937
+ }
807
938
  if (state.driveMode === "auto" && options.fourWheelDrive) {
808
939
  const slip = Math.max(0, ...driver.state.slip);
809
940
  if (slip > auto4.slip) {
@@ -817,6 +948,10 @@ var GameVehiclesDriving = (() => {
817
948
  held.clear();
818
949
  throttle = brake = steer = 0;
819
950
  state.horn = false;
951
+ state.lamps.flash = false;
952
+ state.washer = false;
953
+ state.clutch = 0;
954
+ state.cruise.on = false;
820
955
  }
821
956
  };
822
957
  }
@@ -833,15 +968,132 @@ var GameVehiclesDriving = (() => {
833
968
  best = tyre;
834
969
  }
835
970
  }
836
- return best?.health ?? 1;
971
+ return best;
837
972
  });
838
973
  return {
839
- tyres,
974
+ tyres: tyres.map((tyre) => tyre?.health ?? 1),
975
+ rims: tyres.map((tyre) => !!tyre?.rim),
840
976
  ...report.brakes != null ? { brakes: report.brakes } : {},
841
977
  ...report.engine != null ? { engine: report.engine } : {},
842
978
  ...report.suspension != null ? { suspension: report.suspension } : {}
843
979
  };
844
980
  }
981
+
982
+ // src/systems.ts
983
+ var DEFAULT_LOADS = { position: 20, low: 110, high: 130, fog: 110, interior: 10, radio: 25, wipers: 60, indicators: 42, horn: 60, alarm: 5, brakeLights: 42, climate: 180 };
984
+ var clamp3 = (x, a, b) => Math.max(a, Math.min(b, x));
985
+ function createVehicleSystems(options = {}) {
986
+ const loads = { ...DEFAULT_LOADS, ...options.loads };
987
+ const capacity = options.batteryWh ?? 720;
988
+ const alternator = options.alternatorW ?? { idle: 350, full: 1100 };
989
+ const crankCharge = options.crankCharge ?? 0.25;
990
+ const startWh = options.startWh ?? 0.6;
991
+ const ambient = options.ambientC ?? 25;
992
+ const thermostat = options.thermostatC ?? 90;
993
+ const limp = options.limpC ?? 115;
994
+ const seize = options.seizeC ?? 130;
995
+ const heat = options.heatPerSecond ?? 0.5;
996
+ const cooling = options.cooling ?? { stopped: 4e-3, fast: 0.03 };
997
+ const state = { charge: clamp3(options.charge ?? 1, 0, 1), coolantC: ambient, temperature: 0, overheating: false, seized: false, spares: options.spares ?? 1, netW: 0 };
998
+ const draw = (c) => {
999
+ const lamps = c.lamps;
1000
+ let w = lamps.mode === "off" ? 0 : loads.position;
1001
+ if (lamps.mode === "low") w += loads.low;
1002
+ if (lamps.mode === "high") w += loads.low + loads.high;
1003
+ if (lamps.flash) w += loads.high;
1004
+ if (lamps.fog) w += loads.fog;
1005
+ if (lamps.interior) w += loads.interior;
1006
+ if (c.radio.on) w += loads.radio;
1007
+ if (c.wipers !== "off" || c.washer) w += loads.wipers;
1008
+ if (c.indicator !== "off") w += loads.indicators * (c.indicator === "hazard" ? 2 : 1) * 0.5;
1009
+ if (c.horn) w += loads.horn;
1010
+ if (c.alarm !== "off") w += loads.alarm;
1011
+ if (c.brakeLights) w += loads.brakeLights;
1012
+ if (c.climate.on) w += loads.climate;
1013
+ return w;
1014
+ };
1015
+ return {
1016
+ state,
1017
+ canCrank: () => state.charge >= crankCharge && !state.seized,
1018
+ crank() {
1019
+ state.charge = clamp3(state.charge - startWh / capacity, 0, 1);
1020
+ },
1021
+ powerLimit: () => state.coolantC <= limp ? 1 : clamp3(1 - (state.coolantC - limp) / (seize - limp), 0.25, 1),
1022
+ step({ driver, controls, throttle, dt, radiator = 1, coolantC }) {
1023
+ const events = [];
1024
+ const running = driver.state.engineOn;
1025
+ const charging = running ? alternator.idle + (alternator.full - alternator.idle) * clamp3((driver.state.rpm - 900) / 1100, 0, 1) : 0;
1026
+ const net = charging - draw(controls);
1027
+ state.netW = net;
1028
+ const before = state.charge;
1029
+ state.charge = clamp3(state.charge + net * dt / 3600 / capacity, 0, 1);
1030
+ if (before >= crankCharge && state.charge < crankCharge && !running) events.push({ type: "battery_flat" });
1031
+ const work = running ? clamp3(0.15 + 0.85 * throttle * (driver.state.rpm / 6500), 0, 1) : 0;
1032
+ const air = cooling.stopped + (cooling.fast - cooling.stopped) * clamp3(Math.abs(driver.state.u) / 27.8, 0, 1);
1033
+ const cool = air * clamp3(radiator, 0.05, 1) * (running ? 1 + (controls.climate.on ? 0.15 : 0) : 1);
1034
+ const opened = state.coolantC >= thermostat ? 1 : 0.15;
1035
+ state.coolantC += (heat * work - cool * opened * (state.coolantC - ambient)) * dt;
1036
+ state.coolantC = coolantC ?? Math.max(ambient, state.coolantC);
1037
+ state.temperature = clamp3((state.coolantC - ambient) / (seize - ambient), 0, 1);
1038
+ const hot = state.coolantC > limp;
1039
+ if (hot && !state.overheating) events.push({ type: "overheating" });
1040
+ state.overheating = hot;
1041
+ if (state.coolantC >= seize && !state.seized) {
1042
+ state.seized = true;
1043
+ events.push({ type: "seized" });
1044
+ }
1045
+ if (state.seized) driver.state.engineOn = false;
1046
+ return events;
1047
+ },
1048
+ fitSpare(driver, wheel) {
1049
+ if (state.spares <= 0) return { type: "no_spare" };
1050
+ state.spares--;
1051
+ const tyres = [...driver.condition.tyres ?? []];
1052
+ tyres[wheel] = 1;
1053
+ driver.condition = { ...driver.condition, tyres };
1054
+ return { type: "spare_fitted", wheel };
1055
+ },
1056
+ charge(share) {
1057
+ state.charge = clamp3(state.charge + share, 0, 1);
1058
+ }
1059
+ };
1060
+ }
1061
+
1062
+ // src/ejection.ts
1063
+ var DEFAULT_EJECTION = { frontal: 8.3, side: 6, keep: 0.8, lift: 2.5 };
1064
+ function ejectionOf(pulse, belted, options = DEFAULT_EJECTION) {
1065
+ if (belted) return null;
1066
+ const du = pulse.after.u - pulse.before.u;
1067
+ const dw = pulse.after.w - pulse.before.w;
1068
+ const deltaV = Math.hypot(du, dw);
1069
+ const frontal = Math.abs(du) >= Math.abs(dw);
1070
+ if (frontal && Math.abs(du) < options.frontal && !pulse.rolled) return null;
1071
+ if (!frontal && Math.abs(dw) < options.side && !pulse.rolled) return null;
1072
+ const exit = frontal ? du < 0 ? "windshield" : "rear_window" : dw < 0 ? "right_window" : "left_window";
1073
+ return { exit, velocity: [pulse.before.u * options.keep, pulse.before.w * options.keep, options.lift], deltaV };
1074
+ }
1075
+
1076
+ // src/parking-sensor.ts
1077
+ var DEFAULT_PARKING_SENSOR = { range: 1.5, solid: 0.3, slowest: 0.8, fastest: 0.12 };
1078
+ function parkingBeepInterval(distance, options = DEFAULT_PARKING_SENSOR) {
1079
+ if (distance > options.range) return null;
1080
+ if (distance <= options.solid) return 0;
1081
+ const t = (distance - options.solid) / (options.range - options.solid);
1082
+ return options.fastest + (options.slowest - options.fastest) * t;
1083
+ }
1084
+ function bumperDistance(params) {
1085
+ const [hx, hz] = params.heading;
1086
+ const sign = params.forward ? 1 : -1;
1087
+ let best = Infinity;
1088
+ for (const [ox, oz] of params.obstacles) {
1089
+ const dx = ox - params.x;
1090
+ const dz = oz - params.z;
1091
+ const along = (dx * hx + dz * hz) * sign - params.length / 2;
1092
+ const side = Math.abs(-dx * hz + dz * hx);
1093
+ if (along >= 0 && side <= params.width / 2 + 0.2) best = Math.min(best, along);
1094
+ }
1095
+ return best;
1096
+ }
845
1097
  return __toCommonJS(index_exports);
846
1098
  })();
847
1099
  //# sourceMappingURL=index.global.js.map