@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.
- package/dist/index.d.ts +273 -7
- package/dist/index.global.js +275 -23
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +275 -23
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -169,7 +169,15 @@ var G = 9.81;
|
|
|
169
169
|
var clamp2 = (x, a, b) => Math.max(a, Math.min(b, x));
|
|
170
170
|
var MAX_SUBSTEP = 1 / 240;
|
|
171
171
|
var FLAT_BELOW = 0.2;
|
|
172
|
+
var RIM_RADIUS_SHARE = 0.7;
|
|
173
|
+
var RIM_FRICTION = 0.4;
|
|
174
|
+
var RIM_ROLLING = 0.25;
|
|
175
|
+
var FLAT_SAG = 0.05;
|
|
176
|
+
var FLAT_ROLLING = 6;
|
|
172
177
|
var STALL_HEALTH = 0.3;
|
|
178
|
+
var CLUTCH_BITE = 0.7;
|
|
179
|
+
var STALL_RPM = 0.5;
|
|
180
|
+
var STALL_GAS = 0.35;
|
|
173
181
|
var defaultSemiSpec = {
|
|
174
182
|
mass: 1300,
|
|
175
183
|
centreHeight: 0.5,
|
|
@@ -310,7 +318,14 @@ function createSemiSimDriver(specification = {}, options = {}) {
|
|
|
310
318
|
const accelInput = reversing ? input.brake : input.throttle;
|
|
311
319
|
const brakeInput = clamp2(reversing ? input.throttle : input.brake, 0, 1);
|
|
312
320
|
const ratio = ratioOf(state.gear) * spec.finalDrive;
|
|
313
|
-
state.
|
|
321
|
+
const engaged = input.clutch == null || state.gear === 0 ? 1 : 1 - clamp2(input.clutch, 0, 1);
|
|
322
|
+
const coupled = wheelRpm * ratio;
|
|
323
|
+
const free = spec.engine.idleRpm + clamp2(accelInput, 0, 1) * (spec.engine.redlineRpm * 0.7 - spec.engine.idleRpm);
|
|
324
|
+
state.rpm = clamp2(coupled * engaged + Math.max(coupled, free) * (1 - engaged), spec.engine.idleRpm, spec.engine.redlineRpm + 200);
|
|
325
|
+
if (input.clutch != null && state.gear !== 0 && state.engineOn && engaged > CLUTCH_BITE && coupled < spec.engine.idleRpm * STALL_RPM && accelInput < STALL_GAS) {
|
|
326
|
+
state.engineOn = false;
|
|
327
|
+
events.push({ type: "stall" });
|
|
328
|
+
}
|
|
314
329
|
if (!state.engineOn || state.fuel <= 0) state.rpm = 0;
|
|
315
330
|
let throttle = clamp2(accelInput, 0, 1);
|
|
316
331
|
if (engineHealth < STALL_HEALTH && throttle > 0.8) {
|
|
@@ -321,12 +336,13 @@ function createSemiSimDriver(specification = {}, options = {}) {
|
|
|
321
336
|
}
|
|
322
337
|
throttle *= 0.5;
|
|
323
338
|
} else stallWatch.throttleTime = 0;
|
|
324
|
-
|
|
339
|
+
const starting = input.start ?? (accelInput > 0 && Math.abs(state.u) < 0.5);
|
|
340
|
+
if (!state.engineOn && starting && engineHealth >= STALL_HEALTH && state.fuel > 0) state.engineOn = true;
|
|
325
341
|
if (state.fuel <= 0 && state.engineOn) {
|
|
326
342
|
state.engineOn = false;
|
|
327
343
|
events.push({ type: "out_of_fuel" });
|
|
328
344
|
}
|
|
329
|
-
const engineTorque = state.engineOn ? torqueAt(spec, state.rpm) * throttle * (0.55 + 0.45 * engineHealth) : 0;
|
|
345
|
+
const engineTorque = state.engineOn ? torqueAt(spec, state.rpm) * throttle * (0.55 + 0.45 * engineHealth) * clamp2(input.powerLimit ?? 1, 0, 1) * engaged : 0;
|
|
330
346
|
const driveForceTotal = state.gear === 0 ? 0 : (reversing ? -1 : 1) * engineTorque * ratio * spec.efficiency / spec.wheelRadius;
|
|
331
347
|
if (state.engineOn) {
|
|
332
348
|
const burned = fuelRate(fuelModel, state.rpm / spec.engine.redlineRpm, throttle, cargo / baseMass, engineHealth) * dt;
|
|
@@ -344,19 +360,21 @@ function createSemiSimDriver(specification = {}, options = {}) {
|
|
|
344
360
|
const wz = state.z - w.x * sn + w.z * cs;
|
|
345
361
|
const ground = world.ground ? world.ground(wx, wz) : {};
|
|
346
362
|
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");
|
|
363
|
+
const tyreHealth = cond.tyres ? clamp2(cond.tyres[i] ?? 1, 0, 1) : 1;
|
|
364
|
+
const rim = !!cond.rims?.[i];
|
|
365
|
+
const flat = rim || tyreHealth < FLAT_BELOW;
|
|
366
|
+
const radius = rim ? spec.rimRadius ?? spec.wheelRadius * RIM_RADIUS_SHARE : flat ? spec.wheelRadius - FLAT_SAG : spec.wheelRadius;
|
|
347
367
|
const top = state.height + w.x * state.pitch - w.z * state.roll;
|
|
348
|
-
const length = top - (ground.height ?? 0) -
|
|
368
|
+
const length = top - (ground.height ?? 0) - radius;
|
|
349
369
|
const compression = ride - length;
|
|
350
370
|
const rate = -(state.heightRate + w.x * state.pitchRate - w.z * state.rollRate);
|
|
351
371
|
const k = spec.suspension.spring * (0.5 + 0.5 * suspensionHealth);
|
|
352
372
|
const c = spec.suspension.damper * (0.5 + 0.5 * suspensionHealth);
|
|
353
373
|
const load = Math.max(0, k * compression + c * rate);
|
|
354
|
-
const tyreHealth = cond.tyres ? clamp2(cond.tyres[i] ?? 1, 0, 1) : 1;
|
|
355
|
-
const flat = tyreHealth < FLAT_BELOW;
|
|
356
374
|
const bog = !!surface.bogs && !spec.fourWheelDrive;
|
|
357
375
|
if (bog) bogging = true;
|
|
358
|
-
const grip = surfaceGrip(surface, state.u) * (flat ? 0.5 : 1);
|
|
359
|
-
wheelState.push({ n: load, grip, surface: surface.id, bog, flat });
|
|
376
|
+
const grip = surfaceGrip(surface, state.u) * (rim ? RIM_FRICTION / spec.tyre.friction : flat ? 0.5 : 1);
|
|
377
|
+
wheelState.push({ n: load, grip, surface: surface.id, bog, flat, rim });
|
|
360
378
|
sumN += load;
|
|
361
379
|
sumNA += load * w.x;
|
|
362
380
|
sumNB += load * w.z;
|
|
@@ -392,7 +410,7 @@ function createSemiSimDriver(specification = {}, options = {}) {
|
|
|
392
410
|
if (locked || lockedByBrake) fl += moving ? -Math.sign(vl) * capacity * (lockedByBrake ? SLIDING_GRIP : 1) : 0;
|
|
393
411
|
else if (brakeShare > 0 && moving) fl += -Math.sign(vl) * brakeShare;
|
|
394
412
|
if (brakeShare > 0 && moving) brakeWork += Math.min(brakeShare, capacity) * Math.abs(vl) * dt;
|
|
395
|
-
const rolling = ws.bog && accelInput > 0 ? spec.bogResistance : spec.rolling * (ws.flat ?
|
|
413
|
+
const rolling = ws.bog && accelInput > 0 ? spec.bogResistance : ws.rim ? RIM_ROLLING : spec.rolling * (ws.flat ? FLAT_ROLLING : 1);
|
|
396
414
|
const alpha = Math.atan2(vc, Math.abs(vl) + 0.5);
|
|
397
415
|
let fc = -Math.tanh(spec.tyre.corneringStiffness * alpha) * capacity;
|
|
398
416
|
if (locked) fc *= 0.6;
|
|
@@ -476,6 +494,23 @@ function semiSpecFromStats(stats, body, base = {}) {
|
|
|
476
494
|
return { ...spec, mass: body.mass, wheels, fourWheelDrive: driveAll, engine, gears, drag, brakeForce, tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling } };
|
|
477
495
|
}
|
|
478
496
|
|
|
497
|
+
// src/driving-aids.ts
|
|
498
|
+
var DRIVE_PROFILES = {
|
|
499
|
+
eco: { throttle: 0.7, upshift: 0.55, downshift: 0.25 },
|
|
500
|
+
normal: { throttle: 1, upshift: 0.85, downshift: 0.35 },
|
|
501
|
+
sport: { throttle: 1, upshift: 0.97, downshift: 0.55 }
|
|
502
|
+
};
|
|
503
|
+
var DEFAULT_TRACTION = { slip: 0.97, floor: 0.35 };
|
|
504
|
+
function tractionLimit(slips, options = DEFAULT_TRACTION) {
|
|
505
|
+
const worst = Math.max(0, ...slips);
|
|
506
|
+
if (worst <= options.slip) return 1;
|
|
507
|
+
return Math.max(options.floor, 1 - (worst - options.slip) / (1 - options.slip));
|
|
508
|
+
}
|
|
509
|
+
var DEFAULT_CRUISE = { gain: 0.35, minSpeed: 8.3 };
|
|
510
|
+
function cruiseThrottle(target, speed, options = DEFAULT_CRUISE) {
|
|
511
|
+
return Math.max(0, Math.min(1, (target - speed) * options.gain + 0.12));
|
|
512
|
+
}
|
|
513
|
+
|
|
479
514
|
// src/controls.ts
|
|
480
515
|
var DEFAULT_CONTROL_KEYS = {
|
|
481
516
|
throttle: ["KeyW", "ArrowUp"],
|
|
@@ -484,6 +519,9 @@ var DEFAULT_CONTROL_KEYS = {
|
|
|
484
519
|
right: ["KeyD", "ArrowRight"],
|
|
485
520
|
handbrake: ["Space"],
|
|
486
521
|
horn: ["KeyH"],
|
|
522
|
+
clutch: ["KeyC"],
|
|
523
|
+
flash: ["KeyX"],
|
|
524
|
+
washer: ["KeyZ"],
|
|
487
525
|
ignition: ["KeyI"],
|
|
488
526
|
gearbox: ["KeyM"],
|
|
489
527
|
gear_up: ["KeyE"],
|
|
@@ -497,13 +535,21 @@ var DEFAULT_CONTROL_KEYS = {
|
|
|
497
535
|
alarm: ["KeyY"],
|
|
498
536
|
radio: ["KeyR"],
|
|
499
537
|
radio_source: ["KeyT"],
|
|
538
|
+
radio_track: ["BracketRight"],
|
|
500
539
|
exit: ["KeyF"],
|
|
501
540
|
indicator_left: ["Comma"],
|
|
502
541
|
indicator_right: ["Period"],
|
|
503
542
|
hazard: ["KeyB"],
|
|
504
543
|
wipers: ["KeyO"],
|
|
505
|
-
seatbelt: ["KeyJ"]
|
|
544
|
+
seatbelt: ["KeyJ"],
|
|
545
|
+
drive_profile: ["Digit7"],
|
|
546
|
+
traction_control: ["Digit8"],
|
|
547
|
+
abs: ["Digit9"],
|
|
548
|
+
cruise: ["Digit0"],
|
|
549
|
+
climate: ["Minus"],
|
|
550
|
+
demist: ["Equal"]
|
|
506
551
|
};
|
|
552
|
+
var HELD_KEYS = ["throttle", "brake", "left", "right", "handbrake", "horn", "clutch", "flash", "washer"];
|
|
507
553
|
function createVehicleControls(options) {
|
|
508
554
|
const keys = { ...DEFAULT_CONTROL_KEYS, ...options.keys };
|
|
509
555
|
const auto4 = options.autoFourWheel ?? { slip: 0.18, releaseSeconds: 2 };
|
|
@@ -511,7 +557,7 @@ function createVehicleControls(options) {
|
|
|
511
557
|
const held = /* @__PURE__ */ new Set();
|
|
512
558
|
const any = (codes) => codes.some((c) => held.has(c));
|
|
513
559
|
const commandOf = /* @__PURE__ */ new Map();
|
|
514
|
-
for (const [command2, codes] of Object.entries(keys)) if (!
|
|
560
|
+
for (const [command2, codes] of Object.entries(keys)) if (!HELD_KEYS.includes(command2)) for (const code of codes) commandOf.set(code, command2);
|
|
515
561
|
const driving = new Set(Object.values(keys).flat());
|
|
516
562
|
const state = {
|
|
517
563
|
ignition: !!options.running,
|
|
@@ -520,18 +566,29 @@ function createVehicleControls(options) {
|
|
|
520
566
|
parkingBrake: false,
|
|
521
567
|
driveMode: options.fourWheelDrive ? "auto" : "4x2",
|
|
522
568
|
fourWheelEngaged: false,
|
|
523
|
-
lamps: { mode: "off", fog: false, interior: false },
|
|
569
|
+
lamps: { mode: "off", fog: false, interior: false, flash: false },
|
|
524
570
|
brakeLights: false,
|
|
525
571
|
reverseLights: false,
|
|
526
572
|
horn: false,
|
|
527
573
|
alarm: "off",
|
|
528
|
-
radio: { on: false, source: "radio" },
|
|
574
|
+
radio: { on: false, source: "radio", track: 0 },
|
|
529
575
|
indicator: "off",
|
|
530
576
|
blinkOn: false,
|
|
531
577
|
tick: false,
|
|
532
578
|
wipers: "off",
|
|
533
|
-
|
|
579
|
+
washer: false,
|
|
580
|
+
seatbelt: false,
|
|
581
|
+
clutch: 0,
|
|
582
|
+
profile: "normal",
|
|
583
|
+
tractionControl: true,
|
|
584
|
+
abs: true,
|
|
585
|
+
cruise: { on: false, speed: 0 },
|
|
586
|
+
climate: { on: false, demist: false },
|
|
587
|
+
cranking: false
|
|
534
588
|
};
|
|
589
|
+
let crankAsked = false;
|
|
590
|
+
let lastSpeed = 0;
|
|
591
|
+
let engineRunning = !!options.running;
|
|
535
592
|
const blinkHz = options.blinkHz ?? 1.5;
|
|
536
593
|
const cancelAt = options.indicatorCancel ?? 0.15;
|
|
537
594
|
let blink = 0;
|
|
@@ -543,15 +600,19 @@ function createVehicleControls(options) {
|
|
|
543
600
|
const emit = (event) => options.onEvent?.(event);
|
|
544
601
|
const LAMPS = ["off", "position", "low"];
|
|
545
602
|
const MODES = ["4x2", "4x4", "auto"];
|
|
603
|
+
const WIPERS = ["off", "intermittent", "slow", "fast"];
|
|
604
|
+
const PROFILES = ["eco", "normal", "sport"];
|
|
546
605
|
const command = (c) => {
|
|
547
606
|
switch (c) {
|
|
548
607
|
case "ignition":
|
|
549
608
|
state.ignition = !state.ignition;
|
|
550
609
|
if (state.ignition && state.alarm !== "off") state.alarm = "off";
|
|
610
|
+
if (state.ignition) crankAsked = true;
|
|
611
|
+
else state.cruise.on = false;
|
|
551
612
|
break;
|
|
552
613
|
case "gearbox":
|
|
553
614
|
state.gearbox = state.gearbox === "automatic" ? "manual" : "automatic";
|
|
554
|
-
state.gear = Math.max(1, state.gear);
|
|
615
|
+
state.gear = options.clutchPedal && state.gearbox === "manual" && Math.abs(lastSpeed) < 0.5 ? 0 : Math.max(1, state.gear);
|
|
555
616
|
break;
|
|
556
617
|
case "gear_up":
|
|
557
618
|
case "gear_down":
|
|
@@ -590,6 +651,9 @@ function createVehicleControls(options) {
|
|
|
590
651
|
case "radio_source":
|
|
591
652
|
state.radio.source = state.radio.source === "radio" ? "tape" : "radio";
|
|
592
653
|
break;
|
|
654
|
+
case "radio_track":
|
|
655
|
+
state.radio.track = (state.radio.track + 1) % Math.max(1, options.radioTracks ?? 3);
|
|
656
|
+
break;
|
|
593
657
|
case "exit":
|
|
594
658
|
break;
|
|
595
659
|
case "indicator_left":
|
|
@@ -606,7 +670,30 @@ function createVehicleControls(options) {
|
|
|
606
670
|
blink = 0;
|
|
607
671
|
break;
|
|
608
672
|
case "wipers":
|
|
609
|
-
state.wipers = state.wipers
|
|
673
|
+
state.wipers = WIPERS[(WIPERS.indexOf(state.wipers) + 1) % WIPERS.length];
|
|
674
|
+
break;
|
|
675
|
+
case "drive_profile":
|
|
676
|
+
state.profile = PROFILES[(PROFILES.indexOf(state.profile) + 1) % PROFILES.length];
|
|
677
|
+
break;
|
|
678
|
+
case "traction_control":
|
|
679
|
+
state.tractionControl = !state.tractionControl;
|
|
680
|
+
break;
|
|
681
|
+
case "abs":
|
|
682
|
+
state.abs = !state.abs;
|
|
683
|
+
break;
|
|
684
|
+
case "cruise":
|
|
685
|
+
if (state.cruise.on) state.cruise.on = false;
|
|
686
|
+
else if (!engineRunning) return emit({ type: "refused", command: c, reason: "ignition_off" });
|
|
687
|
+
else if (lastSpeed < DEFAULT_CRUISE.minSpeed) return emit({ type: "refused", command: c, reason: "too_slow" });
|
|
688
|
+
else state.cruise = { on: true, speed: lastSpeed };
|
|
689
|
+
break;
|
|
690
|
+
case "climate":
|
|
691
|
+
state.climate.on = !state.climate.on;
|
|
692
|
+
if (!state.climate.on) state.climate.demist = false;
|
|
693
|
+
break;
|
|
694
|
+
case "demist":
|
|
695
|
+
state.climate.demist = !state.climate.demist;
|
|
696
|
+
if (state.climate.demist) state.climate.on = true;
|
|
610
697
|
break;
|
|
611
698
|
case "seatbelt":
|
|
612
699
|
state.seatbelt = !state.seatbelt;
|
|
@@ -650,16 +737,48 @@ function createVehicleControls(options) {
|
|
|
650
737
|
}
|
|
651
738
|
} else state.blinkOn = false;
|
|
652
739
|
state.horn = any(keys.horn);
|
|
653
|
-
|
|
740
|
+
state.lamps.flash = any(keys.flash);
|
|
741
|
+
state.washer = any(keys.washer);
|
|
742
|
+
state.clutch = ease(state.clutch, any(keys.clutch) ? 1 : 0, any(keys.clutch) ? 8 : 2.5, dt);
|
|
743
|
+
const speed = driver?.state.u ?? 0;
|
|
744
|
+
lastSpeed = speed;
|
|
745
|
+
if (state.cruise.on && (brake > 0.05 || state.clutch > 0.5 || handbrake)) {
|
|
746
|
+
state.cruise.on = false;
|
|
747
|
+
emit({ type: "cruise_cancelled" });
|
|
748
|
+
}
|
|
749
|
+
const tune = DRIVE_PROFILES[state.profile];
|
|
750
|
+
let gasNow = state.ignition ? throttle * tune.throttle : 0;
|
|
751
|
+
if (state.cruise.on && state.ignition) gasNow = Math.max(gasNow, cruiseThrottle(state.cruise.speed, speed));
|
|
752
|
+
const gasStart = state.gearbox === "automatic" && state.ignition && !engineRunning && throttle > 0 && Math.abs(speed) < 0.5;
|
|
753
|
+
state.cranking = false;
|
|
754
|
+
if (crankAsked || gasStart) {
|
|
755
|
+
crankAsked = false;
|
|
756
|
+
if (options.canCrank && !options.canCrank()) emit({ type: "refused", command: "ignition", reason: "battery_flat" });
|
|
757
|
+
else {
|
|
758
|
+
state.cranking = true;
|
|
759
|
+
options.onCrank?.();
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
const driven = (driver?.state.slip ?? []).filter((_, i) => driver?.spec.wheels?.[i]?.drive ?? true);
|
|
763
|
+
const powerLimit = (state.tractionControl ? tractionLimit(driven) : 1) * (options.powerLimit?.() ?? 1);
|
|
654
764
|
const reversing = state.gearbox === "manual" ? state.gear < 0 : (driver?.state.gear ?? 1) < 0;
|
|
655
765
|
state.reverseLights = reversing;
|
|
656
|
-
state.brakeLights = handbrake || (reversing ? gasNow > 0.05 &&
|
|
657
|
-
|
|
658
|
-
|
|
766
|
+
state.brakeLights = handbrake || (reversing ? gasNow > 0.05 && speed < -0.3 : brake > 0.05);
|
|
767
|
+
const common = { steer, handbrake, horn: state.horn, start: state.cranking, powerLimit };
|
|
768
|
+
if (state.gearbox === "automatic") return { ...common, throttle: gasNow, brake };
|
|
769
|
+
const pedals = state.gear < 0 ? { throttle: brake, brake: gasNow } : { throttle: gasNow, brake };
|
|
770
|
+
return { ...common, ...pedals, gear: state.gear, ...options.clutchPedal ? { clutch: state.clutch } : {} };
|
|
659
771
|
},
|
|
660
772
|
after(driver, dt) {
|
|
661
|
-
if (driver.state.engineOn && !state.ignition && driver.state.u !== 0) state.ignition = true;
|
|
662
773
|
if (!state.ignition && driver.state.engineOn) driver.state.engineOn = false;
|
|
774
|
+
engineRunning = driver.state.engineOn;
|
|
775
|
+
if (!engineRunning) state.cruise.on = false;
|
|
776
|
+
const spec = driver.spec;
|
|
777
|
+
spec.abs = state.abs;
|
|
778
|
+
if (spec.upshiftFraction != null) {
|
|
779
|
+
spec.upshiftFraction = DRIVE_PROFILES[state.profile].upshift;
|
|
780
|
+
spec.downshiftFraction = DRIVE_PROFILES[state.profile].downshift;
|
|
781
|
+
}
|
|
663
782
|
if (state.driveMode === "auto" && options.fourWheelDrive) {
|
|
664
783
|
const slip = Math.max(0, ...driver.state.slip);
|
|
665
784
|
if (slip > auto4.slip) {
|
|
@@ -673,6 +792,10 @@ function createVehicleControls(options) {
|
|
|
673
792
|
held.clear();
|
|
674
793
|
throttle = brake = steer = 0;
|
|
675
794
|
state.horn = false;
|
|
795
|
+
state.lamps.flash = false;
|
|
796
|
+
state.washer = false;
|
|
797
|
+
state.clutch = 0;
|
|
798
|
+
state.cruise.on = false;
|
|
676
799
|
}
|
|
677
800
|
};
|
|
678
801
|
}
|
|
@@ -689,33 +812,162 @@ function conditionFromDamage(spec, report) {
|
|
|
689
812
|
best = tyre;
|
|
690
813
|
}
|
|
691
814
|
}
|
|
692
|
-
return best
|
|
815
|
+
return best;
|
|
693
816
|
});
|
|
694
817
|
return {
|
|
695
|
-
tyres,
|
|
818
|
+
tyres: tyres.map((tyre) => tyre?.health ?? 1),
|
|
819
|
+
rims: tyres.map((tyre) => !!tyre?.rim),
|
|
696
820
|
...report.brakes != null ? { brakes: report.brakes } : {},
|
|
697
821
|
...report.engine != null ? { engine: report.engine } : {},
|
|
698
822
|
...report.suspension != null ? { suspension: report.suspension } : {}
|
|
699
823
|
};
|
|
700
824
|
}
|
|
825
|
+
|
|
826
|
+
// src/systems.ts
|
|
827
|
+
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 };
|
|
828
|
+
var clamp3 = (x, a, b) => Math.max(a, Math.min(b, x));
|
|
829
|
+
function createVehicleSystems(options = {}) {
|
|
830
|
+
const loads = { ...DEFAULT_LOADS, ...options.loads };
|
|
831
|
+
const capacity = options.batteryWh ?? 720;
|
|
832
|
+
const alternator = options.alternatorW ?? { idle: 350, full: 1100 };
|
|
833
|
+
const crankCharge = options.crankCharge ?? 0.25;
|
|
834
|
+
const startWh = options.startWh ?? 0.6;
|
|
835
|
+
const ambient = options.ambientC ?? 25;
|
|
836
|
+
const thermostat = options.thermostatC ?? 90;
|
|
837
|
+
const limp = options.limpC ?? 115;
|
|
838
|
+
const seize = options.seizeC ?? 130;
|
|
839
|
+
const heat = options.heatPerSecond ?? 0.5;
|
|
840
|
+
const cooling = options.cooling ?? { stopped: 4e-3, fast: 0.03 };
|
|
841
|
+
const state = { charge: clamp3(options.charge ?? 1, 0, 1), coolantC: ambient, temperature: 0, overheating: false, seized: false, spares: options.spares ?? 1, netW: 0 };
|
|
842
|
+
const draw = (c) => {
|
|
843
|
+
const lamps = c.lamps;
|
|
844
|
+
let w = lamps.mode === "off" ? 0 : loads.position;
|
|
845
|
+
if (lamps.mode === "low") w += loads.low;
|
|
846
|
+
if (lamps.mode === "high") w += loads.low + loads.high;
|
|
847
|
+
if (lamps.flash) w += loads.high;
|
|
848
|
+
if (lamps.fog) w += loads.fog;
|
|
849
|
+
if (lamps.interior) w += loads.interior;
|
|
850
|
+
if (c.radio.on) w += loads.radio;
|
|
851
|
+
if (c.wipers !== "off" || c.washer) w += loads.wipers;
|
|
852
|
+
if (c.indicator !== "off") w += loads.indicators * (c.indicator === "hazard" ? 2 : 1) * 0.5;
|
|
853
|
+
if (c.horn) w += loads.horn;
|
|
854
|
+
if (c.alarm !== "off") w += loads.alarm;
|
|
855
|
+
if (c.brakeLights) w += loads.brakeLights;
|
|
856
|
+
if (c.climate.on) w += loads.climate;
|
|
857
|
+
return w;
|
|
858
|
+
};
|
|
859
|
+
return {
|
|
860
|
+
state,
|
|
861
|
+
canCrank: () => state.charge >= crankCharge && !state.seized,
|
|
862
|
+
crank() {
|
|
863
|
+
state.charge = clamp3(state.charge - startWh / capacity, 0, 1);
|
|
864
|
+
},
|
|
865
|
+
powerLimit: () => state.coolantC <= limp ? 1 : clamp3(1 - (state.coolantC - limp) / (seize - limp), 0.25, 1),
|
|
866
|
+
step({ driver, controls, throttle, dt, radiator = 1, coolantC }) {
|
|
867
|
+
const events = [];
|
|
868
|
+
const running = driver.state.engineOn;
|
|
869
|
+
const charging = running ? alternator.idle + (alternator.full - alternator.idle) * clamp3((driver.state.rpm - 900) / 1100, 0, 1) : 0;
|
|
870
|
+
const net = charging - draw(controls);
|
|
871
|
+
state.netW = net;
|
|
872
|
+
const before = state.charge;
|
|
873
|
+
state.charge = clamp3(state.charge + net * dt / 3600 / capacity, 0, 1);
|
|
874
|
+
if (before >= crankCharge && state.charge < crankCharge && !running) events.push({ type: "battery_flat" });
|
|
875
|
+
const work = running ? clamp3(0.15 + 0.85 * throttle * (driver.state.rpm / 6500), 0, 1) : 0;
|
|
876
|
+
const air = cooling.stopped + (cooling.fast - cooling.stopped) * clamp3(Math.abs(driver.state.u) / 27.8, 0, 1);
|
|
877
|
+
const cool = air * clamp3(radiator, 0.05, 1) * (running ? 1 + (controls.climate.on ? 0.15 : 0) : 1);
|
|
878
|
+
const opened = state.coolantC >= thermostat ? 1 : 0.15;
|
|
879
|
+
state.coolantC += (heat * work - cool * opened * (state.coolantC - ambient)) * dt;
|
|
880
|
+
state.coolantC = coolantC ?? Math.max(ambient, state.coolantC);
|
|
881
|
+
state.temperature = clamp3((state.coolantC - ambient) / (seize - ambient), 0, 1);
|
|
882
|
+
const hot = state.coolantC > limp;
|
|
883
|
+
if (hot && !state.overheating) events.push({ type: "overheating" });
|
|
884
|
+
state.overheating = hot;
|
|
885
|
+
if (state.coolantC >= seize && !state.seized) {
|
|
886
|
+
state.seized = true;
|
|
887
|
+
events.push({ type: "seized" });
|
|
888
|
+
}
|
|
889
|
+
if (state.seized) driver.state.engineOn = false;
|
|
890
|
+
return events;
|
|
891
|
+
},
|
|
892
|
+
fitSpare(driver, wheel) {
|
|
893
|
+
if (state.spares <= 0) return { type: "no_spare" };
|
|
894
|
+
state.spares--;
|
|
895
|
+
const tyres = [...driver.condition.tyres ?? []];
|
|
896
|
+
tyres[wheel] = 1;
|
|
897
|
+
driver.condition = { ...driver.condition, tyres };
|
|
898
|
+
return { type: "spare_fitted", wheel };
|
|
899
|
+
},
|
|
900
|
+
charge(share) {
|
|
901
|
+
state.charge = clamp3(state.charge + share, 0, 1);
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
// src/ejection.ts
|
|
907
|
+
var DEFAULT_EJECTION = { frontal: 8.3, side: 6, keep: 0.8, lift: 2.5 };
|
|
908
|
+
function ejectionOf(pulse, belted, options = DEFAULT_EJECTION) {
|
|
909
|
+
if (belted) return null;
|
|
910
|
+
const du = pulse.after.u - pulse.before.u;
|
|
911
|
+
const dw = pulse.after.w - pulse.before.w;
|
|
912
|
+
const deltaV = Math.hypot(du, dw);
|
|
913
|
+
const frontal = Math.abs(du) >= Math.abs(dw);
|
|
914
|
+
if (frontal && Math.abs(du) < options.frontal && !pulse.rolled) return null;
|
|
915
|
+
if (!frontal && Math.abs(dw) < options.side && !pulse.rolled) return null;
|
|
916
|
+
const exit = frontal ? du < 0 ? "windshield" : "rear_window" : dw < 0 ? "right_window" : "left_window";
|
|
917
|
+
return { exit, velocity: [pulse.before.u * options.keep, pulse.before.w * options.keep, options.lift], deltaV };
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// src/parking-sensor.ts
|
|
921
|
+
var DEFAULT_PARKING_SENSOR = { range: 1.5, solid: 0.3, slowest: 0.8, fastest: 0.12 };
|
|
922
|
+
function parkingBeepInterval(distance, options = DEFAULT_PARKING_SENSOR) {
|
|
923
|
+
if (distance > options.range) return null;
|
|
924
|
+
if (distance <= options.solid) return 0;
|
|
925
|
+
const t = (distance - options.solid) / (options.range - options.solid);
|
|
926
|
+
return options.fastest + (options.slowest - options.fastest) * t;
|
|
927
|
+
}
|
|
928
|
+
function bumperDistance(params) {
|
|
929
|
+
const [hx, hz] = params.heading;
|
|
930
|
+
const sign = params.forward ? 1 : -1;
|
|
931
|
+
let best = Infinity;
|
|
932
|
+
for (const [ox, oz] of params.obstacles) {
|
|
933
|
+
const dx = ox - params.x;
|
|
934
|
+
const dz = oz - params.z;
|
|
935
|
+
const along = (dx * hx + dz * hz) * sign - params.length / 2;
|
|
936
|
+
const side = Math.abs(-dx * hz + dz * hx);
|
|
937
|
+
if (along >= 0 && side <= params.width / 2 + 0.2) best = Math.min(best, along);
|
|
938
|
+
}
|
|
939
|
+
return best;
|
|
940
|
+
}
|
|
701
941
|
export {
|
|
702
942
|
DEFAULT_CONTROL_KEYS,
|
|
943
|
+
DEFAULT_CRUISE,
|
|
944
|
+
DEFAULT_EJECTION,
|
|
945
|
+
DEFAULT_LOADS,
|
|
946
|
+
DEFAULT_PARKING_SENSOR,
|
|
947
|
+
DEFAULT_TRACTION,
|
|
948
|
+
DRIVE_PROFILES,
|
|
703
949
|
bodyDamage,
|
|
950
|
+
bumperDistance,
|
|
704
951
|
conditionFromDamage,
|
|
705
952
|
createArcadeDriver,
|
|
706
953
|
createSemiSimDriver,
|
|
707
954
|
createSurfaces,
|
|
708
955
|
createVehicleControls,
|
|
956
|
+
createVehicleSystems,
|
|
957
|
+
cruiseThrottle,
|
|
709
958
|
defaultArcadeSpec,
|
|
710
959
|
defaultFuelModel,
|
|
711
960
|
defaultSemiSpec,
|
|
712
961
|
defaultSurfaces,
|
|
962
|
+
ejectionOf,
|
|
713
963
|
fuelRate,
|
|
714
964
|
impactEnergy,
|
|
965
|
+
parkingBeepInterval,
|
|
715
966
|
runOver,
|
|
716
967
|
semiSpecFromStats,
|
|
717
968
|
speedLoss,
|
|
718
969
|
surfaceGrip,
|
|
970
|
+
tractionLimit,
|
|
719
971
|
vehicleCorners,
|
|
720
972
|
vehicleDamageFromImpact
|
|
721
973
|
};
|