@adatechnology/game-vehicles-driving 0.2.10 → 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.
@@ -21,21 +21,35 @@ var GameVehiclesDriving = (() => {
21
21
  // src/index.ts
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
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,
24
31
  bodyDamage: () => bodyDamage,
32
+ bumperDistance: () => bumperDistance,
25
33
  conditionFromDamage: () => conditionFromDamage,
26
34
  createArcadeDriver: () => createArcadeDriver,
27
35
  createSemiSimDriver: () => createSemiSimDriver,
28
36
  createSurfaces: () => createSurfaces,
37
+ createVehicleControls: () => createVehicleControls,
38
+ createVehicleSystems: () => createVehicleSystems,
39
+ cruiseThrottle: () => cruiseThrottle,
29
40
  defaultArcadeSpec: () => defaultArcadeSpec,
30
41
  defaultFuelModel: () => defaultFuelModel,
31
42
  defaultSemiSpec: () => defaultSemiSpec,
32
43
  defaultSurfaces: () => defaultSurfaces,
44
+ ejectionOf: () => ejectionOf,
33
45
  fuelRate: () => fuelRate,
34
46
  impactEnergy: () => impactEnergy,
47
+ parkingBeepInterval: () => parkingBeepInterval,
35
48
  runOver: () => runOver,
36
49
  semiSpecFromStats: () => semiSpecFromStats,
37
50
  speedLoss: () => speedLoss,
38
51
  surfaceGrip: () => surfaceGrip,
52
+ tractionLimit: () => tractionLimit,
39
53
  vehicleCorners: () => vehicleCorners,
40
54
  vehicleDamageFromImpact: () => vehicleDamageFromImpact
41
55
  });
@@ -176,6 +190,7 @@ var GameVehiclesDriving = (() => {
176
190
  }
177
191
 
178
192
  // ../game-contracts/dist/index.js
193
+ var DEFAULT_BLAST_IMPULSE = { bodyArea: 0.7, lift: 0.15, minSpeed: 0.3, tntPerJoule: 0.24 / 2e5 };
179
194
  var DEFAULT_COST_ALLOCATION_BYTES = 1024 * 1024;
180
195
  var DEFERRED_QUEUE_ID = {
181
196
  DESTRUCTION_EVENTS: "destruction.events",
@@ -310,7 +325,15 @@ var GameVehiclesDriving = (() => {
310
325
  var clamp2 = (x, a, b) => Math.max(a, Math.min(b, x));
311
326
  var MAX_SUBSTEP = 1 / 240;
312
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;
313
333
  var STALL_HEALTH = 0.3;
334
+ var CLUTCH_BITE = 0.7;
335
+ var STALL_RPM = 0.5;
336
+ var STALL_GAS = 0.35;
314
337
  var defaultSemiSpec = {
315
338
  mass: 1300,
316
339
  centreHeight: 0.5,
@@ -451,7 +474,14 @@ var GameVehiclesDriving = (() => {
451
474
  const accelInput = reversing ? input.brake : input.throttle;
452
475
  const brakeInput = clamp2(reversing ? input.throttle : input.brake, 0, 1);
453
476
  const ratio = ratioOf(state.gear) * spec.finalDrive;
454
- 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
+ }
455
485
  if (!state.engineOn || state.fuel <= 0) state.rpm = 0;
456
486
  let throttle = clamp2(accelInput, 0, 1);
457
487
  if (engineHealth < STALL_HEALTH && throttle > 0.8) {
@@ -462,12 +492,13 @@ var GameVehiclesDriving = (() => {
462
492
  }
463
493
  throttle *= 0.5;
464
494
  } else stallWatch.throttleTime = 0;
465
- 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;
466
497
  if (state.fuel <= 0 && state.engineOn) {
467
498
  state.engineOn = false;
468
499
  events.push({ type: "out_of_fuel" });
469
500
  }
470
- 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;
471
502
  const driveForceTotal = state.gear === 0 ? 0 : (reversing ? -1 : 1) * engineTorque * ratio * spec.efficiency / spec.wheelRadius;
472
503
  if (state.engineOn) {
473
504
  const burned = fuelRate(fuelModel, state.rpm / spec.engine.redlineRpm, throttle, cargo / baseMass, engineHealth) * dt;
@@ -485,19 +516,21 @@ var GameVehiclesDriving = (() => {
485
516
  const wz = state.z - w.x * sn + w.z * cs;
486
517
  const ground = world.ground ? world.ground(wx, wz) : {};
487
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;
488
523
  const top = state.height + w.x * state.pitch - w.z * state.roll;
489
- const length = top - (ground.height ?? 0) - spec.wheelRadius;
524
+ const length = top - (ground.height ?? 0) - radius;
490
525
  const compression = ride - length;
491
526
  const rate = -(state.heightRate + w.x * state.pitchRate - w.z * state.rollRate);
492
527
  const k = spec.suspension.spring * (0.5 + 0.5 * suspensionHealth);
493
528
  const c = spec.suspension.damper * (0.5 + 0.5 * suspensionHealth);
494
529
  const load = Math.max(0, k * compression + c * rate);
495
- const tyreHealth = cond.tyres ? clamp2(cond.tyres[i] ?? 1, 0, 1) : 1;
496
- const flat = tyreHealth < FLAT_BELOW;
497
530
  const bog = !!surface.bogs && !spec.fourWheelDrive;
498
531
  if (bog) bogging = true;
499
- const grip = surfaceGrip(surface, state.u) * (flat ? 0.5 : 1);
500
- 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 });
501
534
  sumN += load;
502
535
  sumNA += load * w.x;
503
536
  sumNB += load * w.z;
@@ -533,7 +566,7 @@ var GameVehiclesDriving = (() => {
533
566
  if (locked || lockedByBrake) fl += moving ? -Math.sign(vl) * capacity * (lockedByBrake ? SLIDING_GRIP : 1) : 0;
534
567
  else if (brakeShare > 0 && moving) fl += -Math.sign(vl) * brakeShare;
535
568
  if (brakeShare > 0 && moving) brakeWork += Math.min(brakeShare, capacity) * Math.abs(vl) * dt;
536
- 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);
537
570
  const alpha = Math.atan2(vc, Math.abs(vl) + 0.5);
538
571
  let fc = -Math.tanh(spec.tyre.corneringStiffness * alpha) * capacity;
539
572
  if (locked) fc *= 0.6;
@@ -617,6 +650,312 @@ var GameVehiclesDriving = (() => {
617
650
  return { ...spec, mass: body.mass, wheels, fourWheelDrive: driveAll, engine, gears, drag, brakeForce, tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling } };
618
651
  }
619
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
+
670
+ // src/controls.ts
671
+ var DEFAULT_CONTROL_KEYS = {
672
+ throttle: ["KeyW", "ArrowUp"],
673
+ brake: ["KeyS", "ArrowDown"],
674
+ left: ["KeyA", "ArrowLeft"],
675
+ right: ["KeyD", "ArrowRight"],
676
+ handbrake: ["Space"],
677
+ horn: ["KeyH"],
678
+ clutch: ["KeyC"],
679
+ flash: ["KeyX"],
680
+ washer: ["KeyZ"],
681
+ ignition: ["KeyI"],
682
+ gearbox: ["KeyM"],
683
+ gear_up: ["KeyE"],
684
+ gear_down: ["KeyQ"],
685
+ parking_brake: ["KeyP"],
686
+ drive_mode: ["KeyG"],
687
+ lights: ["KeyL"],
688
+ high_beam: ["KeyK"],
689
+ fog_lights: ["KeyN"],
690
+ interior_light: ["KeyU"],
691
+ alarm: ["KeyY"],
692
+ radio: ["KeyR"],
693
+ radio_source: ["KeyT"],
694
+ radio_track: ["BracketRight"],
695
+ exit: ["KeyF"],
696
+ indicator_left: ["Comma"],
697
+ indicator_right: ["Period"],
698
+ hazard: ["KeyB"],
699
+ wipers: ["KeyO"],
700
+ seatbelt: ["KeyJ"],
701
+ drive_profile: ["Digit7"],
702
+ traction_control: ["Digit8"],
703
+ abs: ["Digit9"],
704
+ cruise: ["Digit0"],
705
+ climate: ["Minus"],
706
+ demist: ["Equal"]
707
+ };
708
+ var HELD_KEYS = ["throttle", "brake", "left", "right", "handbrake", "horn", "clutch", "flash", "washer"];
709
+ function createVehicleControls(options) {
710
+ const keys = { ...DEFAULT_CONTROL_KEYS, ...options.keys };
711
+ const auto4 = options.autoFourWheel ?? { slip: 0.18, releaseSeconds: 2 };
712
+ const hasFog = options.fogLamps !== false;
713
+ const held = /* @__PURE__ */ new Set();
714
+ const any = (codes) => codes.some((c) => held.has(c));
715
+ const commandOf = /* @__PURE__ */ new Map();
716
+ for (const [command2, codes] of Object.entries(keys)) if (!HELD_KEYS.includes(command2)) for (const code of codes) commandOf.set(code, command2);
717
+ const driving = new Set(Object.values(keys).flat());
718
+ const state = {
719
+ ignition: !!options.running,
720
+ gearbox: options.gearbox ?? "automatic",
721
+ gear: 1,
722
+ parkingBrake: false,
723
+ driveMode: options.fourWheelDrive ? "auto" : "4x2",
724
+ fourWheelEngaged: false,
725
+ lamps: { mode: "off", fog: false, interior: false, flash: false },
726
+ brakeLights: false,
727
+ reverseLights: false,
728
+ horn: false,
729
+ alarm: "off",
730
+ radio: { on: false, source: "radio", track: 0 },
731
+ indicator: "off",
732
+ blinkOn: false,
733
+ tick: false,
734
+ wipers: "off",
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
744
+ };
745
+ let crankAsked = false;
746
+ let lastSpeed = 0;
747
+ let engineRunning = !!options.running;
748
+ const blinkHz = options.blinkHz ?? 1.5;
749
+ const cancelAt = options.indicatorCancel ?? 0.15;
750
+ let blink = 0;
751
+ let turned = 0;
752
+ let throttle = 0;
753
+ let brake = 0;
754
+ let steer = 0;
755
+ let calm = 0;
756
+ const emit = (event) => options.onEvent?.(event);
757
+ const LAMPS = ["off", "position", "low"];
758
+ const MODES = ["4x2", "4x4", "auto"];
759
+ const WIPERS = ["off", "intermittent", "slow", "fast"];
760
+ const PROFILES = ["eco", "normal", "sport"];
761
+ const command = (c) => {
762
+ switch (c) {
763
+ case "ignition":
764
+ state.ignition = !state.ignition;
765
+ if (state.ignition && state.alarm !== "off") state.alarm = "off";
766
+ if (state.ignition) crankAsked = true;
767
+ else state.cruise.on = false;
768
+ break;
769
+ case "gearbox":
770
+ state.gearbox = state.gearbox === "automatic" ? "manual" : "automatic";
771
+ state.gear = options.clutchPedal && state.gearbox === "manual" && Math.abs(lastSpeed) < 0.5 ? 0 : Math.max(1, state.gear);
772
+ break;
773
+ case "gear_up":
774
+ case "gear_down":
775
+ if (state.gearbox !== "manual") return;
776
+ state.gear = Math.max(-1, Math.min(options.gears, state.gear + (c === "gear_up" ? 1 : -1)));
777
+ break;
778
+ case "parking_brake":
779
+ state.parkingBrake = !state.parkingBrake;
780
+ break;
781
+ case "drive_mode": {
782
+ if (!options.fourWheelDrive) return emit({ type: "refused", command: c, reason: "no_four_wheel_drive" });
783
+ state.driveMode = MODES[(MODES.indexOf(state.driveMode) + 1) % MODES.length];
784
+ state.fourWheelEngaged = state.driveMode === "4x4";
785
+ break;
786
+ }
787
+ case "lights":
788
+ state.lamps.mode = state.lamps.mode === "high" ? "off" : LAMPS[(LAMPS.indexOf(state.lamps.mode) + 1) % LAMPS.length];
789
+ break;
790
+ case "high_beam":
791
+ state.lamps.mode = state.lamps.mode === "high" ? "low" : "high";
792
+ break;
793
+ case "fog_lights":
794
+ if (!hasFog) return emit({ type: "refused", command: c, reason: "no_fog_lamps" });
795
+ state.lamps.fog = !state.lamps.fog;
796
+ if (state.lamps.fog && state.lamps.mode === "off") state.lamps.mode = "position";
797
+ break;
798
+ case "interior_light":
799
+ state.lamps.interior = !state.lamps.interior;
800
+ break;
801
+ case "alarm":
802
+ state.alarm = state.alarm === "off" ? "armed" : state.alarm === "armed" ? "ringing" : "off";
803
+ break;
804
+ case "radio":
805
+ state.radio.on = !state.radio.on;
806
+ break;
807
+ case "radio_source":
808
+ state.radio.source = state.radio.source === "radio" ? "tape" : "radio";
809
+ break;
810
+ case "radio_track":
811
+ state.radio.track = (state.radio.track + 1) % Math.max(1, options.radioTracks ?? 3);
812
+ break;
813
+ case "exit":
814
+ break;
815
+ case "indicator_left":
816
+ case "indicator_right": {
817
+ const side = c === "indicator_left" ? "left" : "right";
818
+ if (state.indicator === "hazard") return;
819
+ state.indicator = state.indicator === side ? "off" : side;
820
+ turned = 0;
821
+ blink = 0;
822
+ break;
823
+ }
824
+ case "hazard":
825
+ state.indicator = state.indicator === "hazard" ? "off" : "hazard";
826
+ blink = 0;
827
+ break;
828
+ case "wipers":
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;
853
+ break;
854
+ case "seatbelt":
855
+ state.seatbelt = !state.seatbelt;
856
+ break;
857
+ }
858
+ emit({ type: "command", command: c, state });
859
+ };
860
+ const ease = (from, to, rate, dt) => from + Math.sign(to - from) * Math.min(Math.abs(to - from), rate * dt);
861
+ return {
862
+ keys,
863
+ state,
864
+ key(code, down) {
865
+ if (!driving.has(code)) return false;
866
+ const was = held.has(code);
867
+ if (down) held.add(code);
868
+ else held.delete(code);
869
+ const c = commandOf.get(code);
870
+ if (c && down && !was) command(c);
871
+ return true;
872
+ },
873
+ command,
874
+ input(dt, driver) {
875
+ const gas = any(keys.throttle) ? 1 : 0;
876
+ const stop = any(keys.brake) ? 1 : 0;
877
+ const wheel = (any(keys.left) ? 1 : 0) - (any(keys.right) ? 1 : 0);
878
+ throttle = ease(throttle, gas, gas > throttle ? 4 : 8, dt);
879
+ brake = ease(brake, stop, stop > brake ? 6 : 10, dt);
880
+ steer = ease(steer, wheel, wheel !== 0 && Math.sign(wheel) === Math.sign(steer || wheel) ? 3 : 6, dt);
881
+ const handbrake = state.parkingBrake || any(keys.handbrake);
882
+ state.tick = false;
883
+ if (state.indicator !== "off") {
884
+ const before = Math.floor(blink * blinkHz * 2);
885
+ blink += dt;
886
+ const now = Math.floor(blink * blinkHz * 2);
887
+ state.blinkOn = now % 2 === 0;
888
+ state.tick = now !== before;
889
+ if (state.indicator !== "hazard") {
890
+ const way = state.indicator === "left" ? steer : -steer;
891
+ if (way > 0.5) turned = 1;
892
+ else if (turned && way < cancelAt) state.indicator = "off";
893
+ }
894
+ } else state.blinkOn = false;
895
+ state.horn = any(keys.horn);
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);
920
+ const reversing = state.gearbox === "manual" ? state.gear < 0 : (driver?.state.gear ?? 1) < 0;
921
+ state.reverseLights = reversing;
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 } : {} };
927
+ },
928
+ after(driver, dt) {
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
+ }
938
+ if (state.driveMode === "auto" && options.fourWheelDrive) {
939
+ const slip = Math.max(0, ...driver.state.slip);
940
+ if (slip > auto4.slip) {
941
+ state.fourWheelEngaged = true;
942
+ calm = 0;
943
+ } else if (state.fourWheelEngaged && (calm += dt) > auto4.releaseSeconds) state.fourWheelEngaged = false;
944
+ } else state.fourWheelEngaged = state.driveMode === "4x4";
945
+ driver.spec.fourWheelDrive = !!options.fourWheelDrive && state.fourWheelEngaged;
946
+ },
947
+ release() {
948
+ held.clear();
949
+ throttle = brake = steer = 0;
950
+ state.horn = false;
951
+ state.lamps.flash = false;
952
+ state.washer = false;
953
+ state.clutch = 0;
954
+ state.cruise.on = false;
955
+ }
956
+ };
957
+ }
958
+
620
959
  // src/semi/condition.ts
621
960
  function conditionFromDamage(spec, report) {
622
961
  const tyres = spec.wheels.map((wheel) => {
@@ -629,15 +968,132 @@ var GameVehiclesDriving = (() => {
629
968
  best = tyre;
630
969
  }
631
970
  }
632
- return best?.health ?? 1;
971
+ return best;
633
972
  });
634
973
  return {
635
- tyres,
974
+ tyres: tyres.map((tyre) => tyre?.health ?? 1),
975
+ rims: tyres.map((tyre) => !!tyre?.rim),
636
976
  ...report.brakes != null ? { brakes: report.brakes } : {},
637
977
  ...report.engine != null ? { engine: report.engine } : {},
638
978
  ...report.suspension != null ? { suspension: report.suspension } : {}
639
979
  };
640
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
+ }
641
1097
  return __toCommonJS(index_exports);
642
1098
  })();
643
1099
  //# sourceMappingURL=index.global.js.map