@adatechnology/game-vehicles-driving 0.2.11 → 0.2.13

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,38 @@ 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,
31
+ GLOVEBOX: () => GLOVEBOX,
25
32
  bodyDamage: () => bodyDamage,
33
+ bumperDistance: () => bumperDistance,
26
34
  conditionFromDamage: () => conditionFromDamage,
27
35
  createArcadeDriver: () => createArcadeDriver,
28
36
  createSemiSimDriver: () => createSemiSimDriver,
29
37
  createSurfaces: () => createSurfaces,
30
38
  createVehicleControls: () => createVehicleControls,
39
+ createVehicleStorage: () => createVehicleStorage,
40
+ createVehicleSystems: () => createVehicleSystems,
41
+ createWindshieldFog: () => createWindshieldFog,
42
+ cruiseThrottle: () => cruiseThrottle,
31
43
  defaultArcadeSpec: () => defaultArcadeSpec,
32
44
  defaultFuelModel: () => defaultFuelModel,
33
45
  defaultSemiSpec: () => defaultSemiSpec,
34
46
  defaultSurfaces: () => defaultSurfaces,
47
+ ejectionOf: () => ejectionOf,
35
48
  fuelRate: () => fuelRate,
36
49
  impactEnergy: () => impactEnergy,
50
+ parkingBeepInterval: () => parkingBeepInterval,
37
51
  runOver: () => runOver,
38
52
  semiSpecFromStats: () => semiSpecFromStats,
39
53
  speedLoss: () => speedLoss,
40
54
  surfaceGrip: () => surfaceGrip,
55
+ tractionLimit: () => tractionLimit,
56
+ trunkFor: () => trunkFor,
41
57
  vehicleCorners: () => vehicleCorners,
42
58
  vehicleDamageFromImpact: () => vehicleDamageFromImpact
43
59
  });
@@ -313,7 +329,15 @@ var GameVehiclesDriving = (() => {
313
329
  var clamp2 = (x, a, b) => Math.max(a, Math.min(b, x));
314
330
  var MAX_SUBSTEP = 1 / 240;
315
331
  var FLAT_BELOW = 0.2;
332
+ var RIM_RADIUS_SHARE = 0.7;
333
+ var RIM_FRICTION = 0.4;
334
+ var RIM_ROLLING = 0.25;
335
+ var FLAT_SAG = 0.05;
336
+ var FLAT_ROLLING = 6;
316
337
  var STALL_HEALTH = 0.3;
338
+ var CLUTCH_BITE = 0.7;
339
+ var STALL_RPM = 0.5;
340
+ var STALL_GAS = 0.35;
317
341
  var defaultSemiSpec = {
318
342
  mass: 1300,
319
343
  centreHeight: 0.5,
@@ -454,7 +478,14 @@ var GameVehiclesDriving = (() => {
454
478
  const accelInput = reversing ? input.brake : input.throttle;
455
479
  const brakeInput = clamp2(reversing ? input.throttle : input.brake, 0, 1);
456
480
  const ratio = ratioOf(state.gear) * spec.finalDrive;
457
- state.rpm = clamp2(wheelRpm * ratio, spec.engine.idleRpm, spec.engine.redlineRpm + 200);
481
+ const engaged = input.clutch == null || state.gear === 0 ? 1 : 1 - clamp2(input.clutch, 0, 1);
482
+ const coupled = wheelRpm * ratio;
483
+ const free = spec.engine.idleRpm + clamp2(accelInput, 0, 1) * (spec.engine.redlineRpm * 0.7 - spec.engine.idleRpm);
484
+ state.rpm = clamp2(coupled * engaged + Math.max(coupled, free) * (1 - engaged), spec.engine.idleRpm, spec.engine.redlineRpm + 200);
485
+ if (input.clutch != null && state.gear !== 0 && state.engineOn && engaged > CLUTCH_BITE && coupled < spec.engine.idleRpm * STALL_RPM && accelInput < STALL_GAS) {
486
+ state.engineOn = false;
487
+ events.push({ type: "stall" });
488
+ }
458
489
  if (!state.engineOn || state.fuel <= 0) state.rpm = 0;
459
490
  let throttle = clamp2(accelInput, 0, 1);
460
491
  if (engineHealth < STALL_HEALTH && throttle > 0.8) {
@@ -465,12 +496,13 @@ var GameVehiclesDriving = (() => {
465
496
  }
466
497
  throttle *= 0.5;
467
498
  } 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;
499
+ const starting = input.start ?? (accelInput > 0 && Math.abs(state.u) < 0.5);
500
+ if (!state.engineOn && starting && engineHealth >= STALL_HEALTH && state.fuel > 0) state.engineOn = true;
469
501
  if (state.fuel <= 0 && state.engineOn) {
470
502
  state.engineOn = false;
471
503
  events.push({ type: "out_of_fuel" });
472
504
  }
473
- const engineTorque = state.engineOn ? torqueAt(spec, state.rpm) * throttle * (0.55 + 0.45 * engineHealth) : 0;
505
+ const engineTorque = state.engineOn ? torqueAt(spec, state.rpm) * throttle * (0.55 + 0.45 * engineHealth) * clamp2(input.powerLimit ?? 1, 0, 1) * engaged : 0;
474
506
  const driveForceTotal = state.gear === 0 ? 0 : (reversing ? -1 : 1) * engineTorque * ratio * spec.efficiency / spec.wheelRadius;
475
507
  if (state.engineOn) {
476
508
  const burned = fuelRate(fuelModel, state.rpm / spec.engine.redlineRpm, throttle, cargo / baseMass, engineHealth) * dt;
@@ -488,19 +520,21 @@ var GameVehiclesDriving = (() => {
488
520
  const wz = state.z - w.x * sn + w.z * cs;
489
521
  const ground = world.ground ? world.ground(wx, wz) : {};
490
522
  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");
523
+ const tyreHealth = cond.tyres ? clamp2(cond.tyres[i] ?? 1, 0, 1) : 1;
524
+ const rim = !!cond.rims?.[i];
525
+ const flat = rim || tyreHealth < FLAT_BELOW;
526
+ const radius = rim ? spec.rimRadius ?? spec.wheelRadius * RIM_RADIUS_SHARE : flat ? spec.wheelRadius - FLAT_SAG : spec.wheelRadius;
491
527
  const top = state.height + w.x * state.pitch - w.z * state.roll;
492
- const length = top - (ground.height ?? 0) - spec.wheelRadius;
528
+ const length = top - (ground.height ?? 0) - radius;
493
529
  const compression = ride - length;
494
530
  const rate = -(state.heightRate + w.x * state.pitchRate - w.z * state.rollRate);
495
531
  const k = spec.suspension.spring * (0.5 + 0.5 * suspensionHealth);
496
532
  const c = spec.suspension.damper * (0.5 + 0.5 * suspensionHealth);
497
533
  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
534
  const bog = !!surface.bogs && !spec.fourWheelDrive;
501
535
  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 });
536
+ const grip = surfaceGrip(surface, state.u) * (rim ? RIM_FRICTION / spec.tyre.friction : flat ? 0.5 : 1);
537
+ wheelState.push({ n: load, grip, surface: surface.id, bog, flat, rim });
504
538
  sumN += load;
505
539
  sumNA += load * w.x;
506
540
  sumNB += load * w.z;
@@ -536,7 +570,7 @@ var GameVehiclesDriving = (() => {
536
570
  if (locked || lockedByBrake) fl += moving ? -Math.sign(vl) * capacity * (lockedByBrake ? SLIDING_GRIP : 1) : 0;
537
571
  else if (brakeShare > 0 && moving) fl += -Math.sign(vl) * brakeShare;
538
572
  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);
573
+ const rolling = ws.bog && accelInput > 0 ? spec.bogResistance : ws.rim ? RIM_ROLLING : spec.rolling * (ws.flat ? FLAT_ROLLING : 1);
540
574
  const alpha = Math.atan2(vc, Math.abs(vl) + 0.5);
541
575
  let fc = -Math.tanh(spec.tyre.corneringStiffness * alpha) * capacity;
542
576
  if (locked) fc *= 0.6;
@@ -620,6 +654,23 @@ var GameVehiclesDriving = (() => {
620
654
  return { ...spec, mass: body.mass, wheels, fourWheelDrive: driveAll, engine, gears, drag, brakeForce, tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling } };
621
655
  }
622
656
 
657
+ // src/driving-aids.ts
658
+ var DRIVE_PROFILES = {
659
+ eco: { throttle: 0.7, upshift: 0.55, downshift: 0.25 },
660
+ normal: { throttle: 1, upshift: 0.85, downshift: 0.35 },
661
+ sport: { throttle: 1, upshift: 0.97, downshift: 0.55 }
662
+ };
663
+ var DEFAULT_TRACTION = { slip: 0.97, floor: 0.35 };
664
+ function tractionLimit(slips, options = DEFAULT_TRACTION) {
665
+ const worst = Math.max(0, ...slips);
666
+ if (worst <= options.slip) return 1;
667
+ return Math.max(options.floor, 1 - (worst - options.slip) / (1 - options.slip));
668
+ }
669
+ var DEFAULT_CRUISE = { gain: 0.35, minSpeed: 8.3 };
670
+ function cruiseThrottle(target, speed, options = DEFAULT_CRUISE) {
671
+ return Math.max(0, Math.min(1, (target - speed) * options.gain + 0.12));
672
+ }
673
+
623
674
  // src/controls.ts
624
675
  var DEFAULT_CONTROL_KEYS = {
625
676
  throttle: ["KeyW", "ArrowUp"],
@@ -628,6 +679,10 @@ var GameVehiclesDriving = (() => {
628
679
  right: ["KeyD", "ArrowRight"],
629
680
  handbrake: ["Space"],
630
681
  horn: ["KeyH"],
682
+ clutch: ["KeyC"],
683
+ flash: ["KeyX"],
684
+ washer: ["KeyZ"],
685
+ hotwire: ["KeyI"],
631
686
  ignition: ["KeyI"],
632
687
  gearbox: ["KeyM"],
633
688
  gear_up: ["KeyE"],
@@ -641,13 +696,24 @@ var GameVehiclesDriving = (() => {
641
696
  alarm: ["KeyY"],
642
697
  radio: ["KeyR"],
643
698
  radio_source: ["KeyT"],
699
+ radio_track: ["BracketRight"],
644
700
  exit: ["KeyF"],
645
701
  indicator_left: ["Comma"],
646
702
  indicator_right: ["Period"],
647
703
  hazard: ["KeyB"],
648
704
  wipers: ["KeyO"],
649
- seatbelt: ["KeyJ"]
705
+ seatbelt: ["KeyJ"],
706
+ drive_profile: ["Digit7"],
707
+ traction_control: ["Digit8"],
708
+ abs: ["Digit9"],
709
+ cruise: ["Digit0"],
710
+ climate: ["Minus"],
711
+ demist: ["Equal"],
712
+ siren: ["Semicolon"],
713
+ beacon: ["Quote"],
714
+ glovebox: ["Slash"]
650
715
  };
716
+ var HELD_KEYS = ["throttle", "brake", "left", "right", "handbrake", "horn", "clutch", "flash", "washer", "hotwire"];
651
717
  function createVehicleControls(options) {
652
718
  const keys = { ...DEFAULT_CONTROL_KEYS, ...options.keys };
653
719
  const auto4 = options.autoFourWheel ?? { slip: 0.18, releaseSeconds: 2 };
@@ -655,7 +721,7 @@ var GameVehiclesDriving = (() => {
655
721
  const held = /* @__PURE__ */ new Set();
656
722
  const any = (codes) => codes.some((c) => held.has(c));
657
723
  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);
724
+ for (const [command2, codes] of Object.entries(keys)) if (!HELD_KEYS.includes(command2)) for (const code of codes) commandOf.set(code, command2);
659
725
  const driving = new Set(Object.values(keys).flat());
660
726
  const state = {
661
727
  ignition: !!options.running,
@@ -664,18 +730,38 @@ var GameVehiclesDriving = (() => {
664
730
  parkingBrake: false,
665
731
  driveMode: options.fourWheelDrive ? "auto" : "4x2",
666
732
  fourWheelEngaged: false,
667
- lamps: { mode: "off", fog: false, interior: false },
733
+ lamps: { mode: "off", fog: false, interior: false, flash: false },
668
734
  brakeLights: false,
669
735
  reverseLights: false,
670
736
  horn: false,
671
737
  alarm: "off",
672
- radio: { on: false, source: "radio" },
738
+ radio: { on: false, source: "radio", track: 0 },
673
739
  indicator: "off",
674
740
  blinkOn: false,
675
741
  tick: false,
676
742
  wipers: "off",
677
- seatbelt: false
743
+ washer: false,
744
+ seatbelt: false,
745
+ clutch: 0,
746
+ profile: "normal",
747
+ tractionControl: true,
748
+ abs: true,
749
+ cruise: { on: false, speed: 0 },
750
+ climate: { on: false, demist: false },
751
+ cranking: false,
752
+ hotwire: { progress: 0, done: false },
753
+ siren: "off",
754
+ beacon: false,
755
+ beaconSide: 0,
756
+ glovebox: false
678
757
  };
758
+ const hasKey = options.hasKey !== false;
759
+ const hotwireSeconds = Math.max(0.1, options.hotwireSeconds ?? 8);
760
+ const beaconHz = options.beaconHz ?? 2.5;
761
+ let beaconClock = 0;
762
+ let crankAsked = false;
763
+ let lastSpeed = 0;
764
+ let engineRunning = !!options.running;
679
765
  const blinkHz = options.blinkHz ?? 1.5;
680
766
  const cancelAt = options.indicatorCancel ?? 0.15;
681
767
  let blink = 0;
@@ -687,15 +773,21 @@ var GameVehiclesDriving = (() => {
687
773
  const emit = (event) => options.onEvent?.(event);
688
774
  const LAMPS = ["off", "position", "low"];
689
775
  const MODES = ["4x2", "4x4", "auto"];
776
+ const WIPERS = ["off", "intermittent", "slow", "fast"];
777
+ const PROFILES = ["eco", "normal", "sport"];
778
+ const SIRENS = ["off", "wail", "yelp"];
690
779
  const command = (c) => {
691
780
  switch (c) {
692
781
  case "ignition":
782
+ if (!hasKey && !state.hotwire.done) return emit({ type: "refused", command: c, reason: "no_key" });
693
783
  state.ignition = !state.ignition;
694
784
  if (state.ignition && state.alarm !== "off") state.alarm = "off";
785
+ if (state.ignition) crankAsked = true;
786
+ else state.cruise.on = false;
695
787
  break;
696
788
  case "gearbox":
697
789
  state.gearbox = state.gearbox === "automatic" ? "manual" : "automatic";
698
- state.gear = Math.max(1, state.gear);
790
+ state.gear = options.clutchPedal && state.gearbox === "manual" && Math.abs(lastSpeed) < 0.5 ? 0 : Math.max(1, state.gear);
699
791
  break;
700
792
  case "gear_up":
701
793
  case "gear_down":
@@ -734,6 +826,9 @@ var GameVehiclesDriving = (() => {
734
826
  case "radio_source":
735
827
  state.radio.source = state.radio.source === "radio" ? "tape" : "radio";
736
828
  break;
829
+ case "radio_track":
830
+ state.radio.track = (state.radio.track + 1) % Math.max(1, options.radioTracks ?? 3);
831
+ break;
737
832
  case "exit":
738
833
  break;
739
834
  case "indicator_left":
@@ -750,11 +845,48 @@ var GameVehiclesDriving = (() => {
750
845
  blink = 0;
751
846
  break;
752
847
  case "wipers":
753
- state.wipers = state.wipers === "off" ? "slow" : state.wipers === "slow" ? "fast" : "off";
848
+ state.wipers = WIPERS[(WIPERS.indexOf(state.wipers) + 1) % WIPERS.length];
849
+ break;
850
+ case "drive_profile":
851
+ state.profile = PROFILES[(PROFILES.indexOf(state.profile) + 1) % PROFILES.length];
852
+ break;
853
+ case "traction_control":
854
+ state.tractionControl = !state.tractionControl;
855
+ break;
856
+ case "abs":
857
+ state.abs = !state.abs;
858
+ break;
859
+ case "cruise":
860
+ if (state.cruise.on) state.cruise.on = false;
861
+ else if (!engineRunning) return emit({ type: "refused", command: c, reason: "ignition_off" });
862
+ else if (lastSpeed < DEFAULT_CRUISE.minSpeed) return emit({ type: "refused", command: c, reason: "too_slow" });
863
+ else state.cruise = { on: true, speed: lastSpeed };
864
+ break;
865
+ case "climate":
866
+ state.climate.on = !state.climate.on;
867
+ if (!state.climate.on) state.climate.demist = false;
868
+ break;
869
+ case "demist":
870
+ state.climate.demist = !state.climate.demist;
871
+ if (state.climate.demist) state.climate.on = true;
754
872
  break;
755
873
  case "seatbelt":
756
874
  state.seatbelt = !state.seatbelt;
757
875
  break;
876
+ case "siren":
877
+ if (!options.emergency) return emit({ type: "refused", command: c, reason: "no_siren" });
878
+ state.siren = SIRENS[(SIRENS.indexOf(state.siren) + 1) % SIRENS.length];
879
+ if (state.siren !== "off") state.beacon = true;
880
+ break;
881
+ case "beacon":
882
+ if (!options.emergency) return emit({ type: "refused", command: c, reason: "no_siren" });
883
+ state.beacon = !state.beacon;
884
+ if (!state.beacon) state.siren = "off";
885
+ beaconClock = 0;
886
+ break;
887
+ case "glovebox":
888
+ state.glovebox = !state.glovebox;
889
+ break;
758
890
  }
759
891
  emit({ type: "command", command: c, state });
760
892
  };
@@ -793,17 +925,66 @@ var GameVehiclesDriving = (() => {
793
925
  else if (turned && way < cancelAt) state.indicator = "off";
794
926
  }
795
927
  } else state.blinkOn = false;
928
+ if (state.beacon) {
929
+ beaconClock += dt;
930
+ state.beaconSide = Math.floor(beaconClock * beaconHz) % 2;
931
+ }
932
+ if (!hasKey && !state.hotwire.done && !state.ignition && any(keys.hotwire)) {
933
+ if (state.hotwire.progress === 0) {
934
+ if (state.alarm === "armed") state.alarm = "ringing";
935
+ emit({ type: "hotwire", stage: "started" });
936
+ }
937
+ state.hotwire.progress = Math.min(1, state.hotwire.progress + dt / hotwireSeconds);
938
+ if (state.hotwire.progress >= 1) {
939
+ state.hotwire.done = true;
940
+ state.ignition = true;
941
+ crankAsked = true;
942
+ emit({ type: "hotwire", stage: "joined" });
943
+ }
944
+ }
796
945
  state.horn = any(keys.horn);
797
- const gasNow = state.ignition ? throttle : 0;
946
+ state.lamps.flash = any(keys.flash);
947
+ state.washer = any(keys.washer);
948
+ state.clutch = ease(state.clutch, any(keys.clutch) ? 1 : 0, any(keys.clutch) ? 8 : 2.5, dt);
949
+ const speed = driver?.state.u ?? 0;
950
+ lastSpeed = speed;
951
+ if (state.cruise.on && (brake > 0.05 || state.clutch > 0.5 || handbrake)) {
952
+ state.cruise.on = false;
953
+ emit({ type: "cruise_cancelled" });
954
+ }
955
+ const tune = DRIVE_PROFILES[state.profile];
956
+ let gasNow = state.ignition ? throttle * tune.throttle : 0;
957
+ if (state.cruise.on && state.ignition) gasNow = Math.max(gasNow, cruiseThrottle(state.cruise.speed, speed));
958
+ const gasStart = state.gearbox === "automatic" && state.ignition && !engineRunning && throttle > 0 && Math.abs(speed) < 0.5;
959
+ state.cranking = false;
960
+ if (crankAsked || gasStart) {
961
+ crankAsked = false;
962
+ if (options.canCrank && !options.canCrank()) emit({ type: "refused", command: "ignition", reason: "battery_flat" });
963
+ else {
964
+ state.cranking = true;
965
+ options.onCrank?.();
966
+ }
967
+ }
968
+ const driven = (driver?.state.slip ?? []).filter((_, i) => driver?.spec.wheels?.[i]?.drive ?? true);
969
+ const powerLimit = (state.tractionControl ? tractionLimit(driven) : 1) * (options.powerLimit?.() ?? 1);
798
970
  const reversing = state.gearbox === "manual" ? state.gear < 0 : (driver?.state.gear ?? 1) < 0;
799
971
  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 };
972
+ state.brakeLights = handbrake || (reversing ? gasNow > 0.05 && speed < -0.3 : brake > 0.05);
973
+ const common = { steer, handbrake, horn: state.horn, start: state.cranking, powerLimit };
974
+ if (state.gearbox === "automatic") return { ...common, throttle: gasNow, brake };
975
+ const pedals = state.gear < 0 ? { throttle: brake, brake: gasNow } : { throttle: gasNow, brake };
976
+ return { ...common, ...pedals, gear: state.gear, ...options.clutchPedal ? { clutch: state.clutch } : {} };
803
977
  },
804
978
  after(driver, dt) {
805
- if (driver.state.engineOn && !state.ignition && driver.state.u !== 0) state.ignition = true;
806
979
  if (!state.ignition && driver.state.engineOn) driver.state.engineOn = false;
980
+ engineRunning = driver.state.engineOn;
981
+ if (!engineRunning) state.cruise.on = false;
982
+ const spec = driver.spec;
983
+ spec.abs = state.abs;
984
+ if (spec.upshiftFraction != null) {
985
+ spec.upshiftFraction = DRIVE_PROFILES[state.profile].upshift;
986
+ spec.downshiftFraction = DRIVE_PROFILES[state.profile].downshift;
987
+ }
807
988
  if (state.driveMode === "auto" && options.fourWheelDrive) {
808
989
  const slip = Math.max(0, ...driver.state.slip);
809
990
  if (slip > auto4.slip) {
@@ -817,6 +998,10 @@ var GameVehiclesDriving = (() => {
817
998
  held.clear();
818
999
  throttle = brake = steer = 0;
819
1000
  state.horn = false;
1001
+ state.lamps.flash = false;
1002
+ state.washer = false;
1003
+ state.clutch = 0;
1004
+ state.cruise.on = false;
820
1005
  }
821
1006
  };
822
1007
  }
@@ -833,15 +1018,222 @@ var GameVehiclesDriving = (() => {
833
1018
  best = tyre;
834
1019
  }
835
1020
  }
836
- return best?.health ?? 1;
1021
+ return best;
837
1022
  });
838
1023
  return {
839
- tyres,
1024
+ tyres: tyres.map((tyre) => tyre?.health ?? 1),
1025
+ rims: tyres.map((tyre) => !!tyre?.rim),
840
1026
  ...report.brakes != null ? { brakes: report.brakes } : {},
841
1027
  ...report.engine != null ? { engine: report.engine } : {},
842
1028
  ...report.suspension != null ? { suspension: report.suspension } : {}
843
1029
  };
844
1030
  }
1031
+
1032
+ // src/systems.ts
1033
+ 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, siren: 100, beacon: 70 };
1034
+ var clamp3 = (x, a, b) => Math.max(a, Math.min(b, x));
1035
+ function createVehicleSystems(options = {}) {
1036
+ const loads = { ...DEFAULT_LOADS, ...options.loads };
1037
+ const capacity = options.batteryWh ?? 720;
1038
+ const alternator = options.alternatorW ?? { idle: 350, full: 1100 };
1039
+ const crankCharge = options.crankCharge ?? 0.25;
1040
+ const startWh = options.startWh ?? 0.6;
1041
+ const ambient = options.ambientC ?? 25;
1042
+ const thermostat = options.thermostatC ?? 90;
1043
+ const limp = options.limpC ?? 115;
1044
+ const seize = options.seizeC ?? 130;
1045
+ const heat = options.heatPerSecond ?? 0.5;
1046
+ const cooling = options.cooling ?? { stopped: 4e-3, fast: 0.03 };
1047
+ const state = { charge: clamp3(options.charge ?? 1, 0, 1), coolantC: ambient, temperature: 0, overheating: false, seized: false, spares: options.spares ?? 1, netW: 0 };
1048
+ const draw = (c) => {
1049
+ const lamps = c.lamps;
1050
+ let w = lamps.mode === "off" ? 0 : loads.position;
1051
+ if (lamps.mode === "low") w += loads.low;
1052
+ if (lamps.mode === "high") w += loads.low + loads.high;
1053
+ if (lamps.flash) w += loads.high;
1054
+ if (lamps.fog) w += loads.fog;
1055
+ if (lamps.interior) w += loads.interior;
1056
+ if (c.radio.on) w += loads.radio;
1057
+ if (c.wipers !== "off" || c.washer) w += loads.wipers;
1058
+ if (c.indicator !== "off") w += loads.indicators * (c.indicator === "hazard" ? 2 : 1) * 0.5;
1059
+ if (c.horn) w += loads.horn;
1060
+ if (c.alarm !== "off") w += loads.alarm;
1061
+ if (c.brakeLights) w += loads.brakeLights;
1062
+ if (c.climate.on) w += loads.climate;
1063
+ if (c.siren !== "off") w += loads.siren;
1064
+ if (c.beacon) w += loads.beacon;
1065
+ return w;
1066
+ };
1067
+ return {
1068
+ state,
1069
+ canCrank: () => state.charge >= crankCharge && !state.seized,
1070
+ crank() {
1071
+ state.charge = clamp3(state.charge - startWh / capacity, 0, 1);
1072
+ },
1073
+ powerLimit: () => state.coolantC <= limp ? 1 : clamp3(1 - (state.coolantC - limp) / (seize - limp), 0.25, 1),
1074
+ step({ driver, controls, throttle, dt, radiator = 1, coolantC }) {
1075
+ const events = [];
1076
+ const running = driver.state.engineOn;
1077
+ const charging = running ? alternator.idle + (alternator.full - alternator.idle) * clamp3((driver.state.rpm - 900) / 1100, 0, 1) : 0;
1078
+ const net = charging - draw(controls);
1079
+ state.netW = net;
1080
+ const before = state.charge;
1081
+ state.charge = clamp3(state.charge + net * dt / 3600 / capacity, 0, 1);
1082
+ if (before >= crankCharge && state.charge < crankCharge && !running) events.push({ type: "battery_flat" });
1083
+ const work = running ? clamp3(0.15 + 0.85 * throttle * (driver.state.rpm / 6500), 0, 1) : 0;
1084
+ const air = cooling.stopped + (cooling.fast - cooling.stopped) * clamp3(Math.abs(driver.state.u) / 27.8, 0, 1);
1085
+ const cool = air * clamp3(radiator, 0.05, 1) * (running ? 1 + (controls.climate.on ? 0.15 : 0) : 1);
1086
+ const opened = state.coolantC >= thermostat ? 1 : 0.15;
1087
+ state.coolantC += (heat * work - cool * opened * (state.coolantC - ambient)) * dt;
1088
+ state.coolantC = coolantC ?? Math.max(ambient, state.coolantC);
1089
+ state.temperature = clamp3((state.coolantC - ambient) / (seize - ambient), 0, 1);
1090
+ const hot = state.coolantC > limp;
1091
+ if (hot && !state.overheating) events.push({ type: "overheating" });
1092
+ state.overheating = hot;
1093
+ if (state.coolantC >= seize && !state.seized) {
1094
+ state.seized = true;
1095
+ events.push({ type: "seized" });
1096
+ }
1097
+ if (state.seized) driver.state.engineOn = false;
1098
+ return events;
1099
+ },
1100
+ fitSpare(driver, wheel) {
1101
+ if (state.spares <= 0) return { type: "no_spare" };
1102
+ state.spares--;
1103
+ const tyres = [...driver.condition.tyres ?? []];
1104
+ tyres[wheel] = 1;
1105
+ driver.condition = { ...driver.condition, tyres };
1106
+ return { type: "spare_fitted", wheel };
1107
+ },
1108
+ charge(share) {
1109
+ state.charge = clamp3(state.charge + share, 0, 1);
1110
+ }
1111
+ };
1112
+ }
1113
+
1114
+ // src/ejection.ts
1115
+ var DEFAULT_EJECTION = { frontal: 8.3, side: 6, keep: 0.8, lift: 2.5 };
1116
+ function ejectionOf(pulse, belted, options = DEFAULT_EJECTION) {
1117
+ if (belted) return null;
1118
+ const du = pulse.after.u - pulse.before.u;
1119
+ const dw = pulse.after.w - pulse.before.w;
1120
+ const deltaV = Math.hypot(du, dw);
1121
+ const frontal = Math.abs(du) >= Math.abs(dw);
1122
+ if (frontal && Math.abs(du) < options.frontal && !pulse.rolled) return null;
1123
+ if (!frontal && Math.abs(dw) < options.side && !pulse.rolled) return null;
1124
+ const exit = frontal ? du < 0 ? "windshield" : "rear_window" : dw < 0 ? "right_window" : "left_window";
1125
+ return { exit, velocity: [pulse.before.u * options.keep, pulse.before.w * options.keep, options.lift], deltaV };
1126
+ }
1127
+
1128
+ // src/parking-sensor.ts
1129
+ var DEFAULT_PARKING_SENSOR = { range: 1.5, solid: 0.3, slowest: 0.8, fastest: 0.12 };
1130
+ function parkingBeepInterval(distance, options = DEFAULT_PARKING_SENSOR) {
1131
+ if (distance > options.range) return null;
1132
+ if (distance <= options.solid) return 0;
1133
+ const t = (distance - options.solid) / (options.range - options.solid);
1134
+ return options.fastest + (options.slowest - options.fastest) * t;
1135
+ }
1136
+ function bumperDistance(params) {
1137
+ const [hx, hz] = params.heading;
1138
+ const sign = params.forward ? 1 : -1;
1139
+ let best = Infinity;
1140
+ for (const [ox, oz] of params.obstacles) {
1141
+ const dx = ox - params.x;
1142
+ const dz = oz - params.z;
1143
+ const along = (dx * hx + dz * hz) * sign - params.length / 2;
1144
+ const side = Math.abs(-dx * hz + dz * hx);
1145
+ if (along >= 0 && side <= params.width / 2 + 0.2) best = Math.min(best, along);
1146
+ }
1147
+ return best;
1148
+ }
1149
+
1150
+ // src/storage.ts
1151
+ var GLOVEBOX = { id: "glovebox", slots: 6, capacityKg: 4, access: "cabin" };
1152
+ var trunkFor = (litres = 450) => ({ id: "trunk", slots: Math.max(4, Math.round(litres / 25)), capacityKg: Math.round(litres / 4), access: "lid" });
1153
+ var weightOf = (stack) => stack.unitKg * stack.count;
1154
+ function createVehicleStorage(options = {}) {
1155
+ const compartments = [...options.compartments ?? [GLOVEBOX, trunkFor()]];
1156
+ const slots = new Map(compartments.map((c) => [c.id, Array.from({ length: c.slots }, () => null)]));
1157
+ const specOf = (id) => compartments.find((c) => c.id === id);
1158
+ const load = (id) => (slots.get(id) ?? []).reduce((kg, s) => kg + (s ? weightOf(s) : 0), 0);
1159
+ const reachable = (id, access) => {
1160
+ const spec = specOf(id);
1161
+ if (!spec) return false;
1162
+ return spec.access === "lid" ? access.lid >= 0.5 : access.gloveboxOpen && (access.seated || access.frontDoorOpen);
1163
+ };
1164
+ const place = (id, stack) => {
1165
+ const spec = specOf(id);
1166
+ const list = slots.get(id);
1167
+ if (!spec || !list) return "missing";
1168
+ if (load(id) + weightOf(stack) > spec.capacityKg + 1e-9) return "too_heavy";
1169
+ const same = stack.data === void 0 ? list.findIndex((s) => s !== null && s.itemId === stack.itemId && s.data === void 0) : -1;
1170
+ if (same >= 0) {
1171
+ const s = list[same];
1172
+ list[same] = { ...s, count: s.count + stack.count };
1173
+ return "ok";
1174
+ }
1175
+ const free = list.indexOf(null);
1176
+ if (free < 0) return "full";
1177
+ list[free] = { ...stack };
1178
+ return "ok";
1179
+ };
1180
+ return {
1181
+ compartments,
1182
+ reachable,
1183
+ contents: (id) => [...slots.get(id) ?? []],
1184
+ loadKg: (id) => id === void 0 ? compartments.reduce((kg, c) => kg + load(c.id), 0) : load(id),
1185
+ put(id, stack, access) {
1186
+ if (!specOf(id)) return "missing";
1187
+ if (!reachable(id, access)) return "shut";
1188
+ return place(id, stack);
1189
+ },
1190
+ take(id, slot, access) {
1191
+ const list = slots.get(id);
1192
+ if (!list || !reachable(id, access)) return null;
1193
+ const stack = list[slot] ?? null;
1194
+ if (stack) list[slot] = null;
1195
+ return stack;
1196
+ },
1197
+ stock(id, stacks) {
1198
+ return stacks.filter((s) => place(id, s) !== "ok");
1199
+ },
1200
+ clear() {
1201
+ for (const list of slots.values()) list.fill(null);
1202
+ }
1203
+ };
1204
+ }
1205
+
1206
+ // src/windshield-fog.ts
1207
+ var clamp4 = (x, a, b) => Math.max(a, Math.min(b, x));
1208
+ function createWindshieldFog(options = {}) {
1209
+ const outside = options.outsideHumidity ?? 0.6;
1210
+ const breath = options.breath ?? 4e-3;
1211
+ const wet = options.wet ?? 2e-3;
1212
+ const air = options.air ?? { shut: 2e-3, window: 0.05, climate: 0.01, demist: 0.06 };
1213
+ const coldSpan = options.coldSpan ?? 15;
1214
+ const rates = options.rates ?? { build: 0.05, clear: 0.03, demist: 0.25 };
1215
+ const state = { humidity: outside, fog: 0 };
1216
+ return {
1217
+ state,
1218
+ step({ dt, occupants, outsideC, cabinC, rain = 0, speed = 0, windows = 0, climate, engineWarm = 1 }) {
1219
+ const exchange = air.shut + air.window * clamp4(windows, 0, 1) + (climate.on ? air.climate : 0) + (climate.demist ? air.demist : 0);
1220
+ const source = breath * Math.max(0, occupants) + wet * clamp4(rain, 0, 1) * (occupants > 0 ? 1 : 0);
1221
+ const towards = climate.on ? outside * 0.4 : outside;
1222
+ state.humidity = clamp4(state.humidity + (source - exchange * (state.humidity - towards)) * dt, 0, 1);
1223
+ const cold = Math.max(0, cabinC - outsideC) * (1 - 0.7 * clamp4(windows, 0, 1)) + 6 * clamp4(rain, 0, 1) + 2 * clamp4(speed / 27.8, 0, 1);
1224
+ const warmed = climate.demist ? cold * (1 - 0.9 * clamp4(engineWarm, 0, 1)) : cold;
1225
+ const clearUpTo = 1 - 0.5 * clamp4(warmed / coldSpan, 0, 1);
1226
+ const target = clamp4((state.humidity - clearUpTo) * 6, 0, 1);
1227
+ const down = climate.demist ? rates.demist * (0.3 + 0.7 * clamp4(engineWarm, 0, 1)) : rates.clear;
1228
+ state.fog = target > state.fog ? Math.min(target, state.fog + rates.build * dt) : Math.max(target, state.fog - down * dt);
1229
+ return state;
1230
+ },
1231
+ set(next) {
1232
+ if (next.humidity !== void 0) state.humidity = clamp4(next.humidity, 0, 1);
1233
+ if (next.fog !== void 0) state.fog = clamp4(next.fog, 0, 1);
1234
+ }
1235
+ };
1236
+ }
845
1237
  return __toCommonJS(index_exports);
846
1238
  })();
847
1239
  //# sourceMappingURL=index.global.js.map