@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.
- package/dist/index.d.ts +429 -7
- package/dist/index.global.js +415 -23
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +415 -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,10 @@ 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"],
|
|
525
|
+
hotwire: ["KeyI"],
|
|
487
526
|
ignition: ["KeyI"],
|
|
488
527
|
gearbox: ["KeyM"],
|
|
489
528
|
gear_up: ["KeyE"],
|
|
@@ -497,13 +536,24 @@ var DEFAULT_CONTROL_KEYS = {
|
|
|
497
536
|
alarm: ["KeyY"],
|
|
498
537
|
radio: ["KeyR"],
|
|
499
538
|
radio_source: ["KeyT"],
|
|
539
|
+
radio_track: ["BracketRight"],
|
|
500
540
|
exit: ["KeyF"],
|
|
501
541
|
indicator_left: ["Comma"],
|
|
502
542
|
indicator_right: ["Period"],
|
|
503
543
|
hazard: ["KeyB"],
|
|
504
544
|
wipers: ["KeyO"],
|
|
505
|
-
seatbelt: ["KeyJ"]
|
|
545
|
+
seatbelt: ["KeyJ"],
|
|
546
|
+
drive_profile: ["Digit7"],
|
|
547
|
+
traction_control: ["Digit8"],
|
|
548
|
+
abs: ["Digit9"],
|
|
549
|
+
cruise: ["Digit0"],
|
|
550
|
+
climate: ["Minus"],
|
|
551
|
+
demist: ["Equal"],
|
|
552
|
+
siren: ["Semicolon"],
|
|
553
|
+
beacon: ["Quote"],
|
|
554
|
+
glovebox: ["Slash"]
|
|
506
555
|
};
|
|
556
|
+
var HELD_KEYS = ["throttle", "brake", "left", "right", "handbrake", "horn", "clutch", "flash", "washer", "hotwire"];
|
|
507
557
|
function createVehicleControls(options) {
|
|
508
558
|
const keys = { ...DEFAULT_CONTROL_KEYS, ...options.keys };
|
|
509
559
|
const auto4 = options.autoFourWheel ?? { slip: 0.18, releaseSeconds: 2 };
|
|
@@ -511,7 +561,7 @@ function createVehicleControls(options) {
|
|
|
511
561
|
const held = /* @__PURE__ */ new Set();
|
|
512
562
|
const any = (codes) => codes.some((c) => held.has(c));
|
|
513
563
|
const commandOf = /* @__PURE__ */ new Map();
|
|
514
|
-
for (const [command2, codes] of Object.entries(keys)) if (!
|
|
564
|
+
for (const [command2, codes] of Object.entries(keys)) if (!HELD_KEYS.includes(command2)) for (const code of codes) commandOf.set(code, command2);
|
|
515
565
|
const driving = new Set(Object.values(keys).flat());
|
|
516
566
|
const state = {
|
|
517
567
|
ignition: !!options.running,
|
|
@@ -520,18 +570,38 @@ function createVehicleControls(options) {
|
|
|
520
570
|
parkingBrake: false,
|
|
521
571
|
driveMode: options.fourWheelDrive ? "auto" : "4x2",
|
|
522
572
|
fourWheelEngaged: false,
|
|
523
|
-
lamps: { mode: "off", fog: false, interior: false },
|
|
573
|
+
lamps: { mode: "off", fog: false, interior: false, flash: false },
|
|
524
574
|
brakeLights: false,
|
|
525
575
|
reverseLights: false,
|
|
526
576
|
horn: false,
|
|
527
577
|
alarm: "off",
|
|
528
|
-
radio: { on: false, source: "radio" },
|
|
578
|
+
radio: { on: false, source: "radio", track: 0 },
|
|
529
579
|
indicator: "off",
|
|
530
580
|
blinkOn: false,
|
|
531
581
|
tick: false,
|
|
532
582
|
wipers: "off",
|
|
533
|
-
|
|
583
|
+
washer: false,
|
|
584
|
+
seatbelt: false,
|
|
585
|
+
clutch: 0,
|
|
586
|
+
profile: "normal",
|
|
587
|
+
tractionControl: true,
|
|
588
|
+
abs: true,
|
|
589
|
+
cruise: { on: false, speed: 0 },
|
|
590
|
+
climate: { on: false, demist: false },
|
|
591
|
+
cranking: false,
|
|
592
|
+
hotwire: { progress: 0, done: false },
|
|
593
|
+
siren: "off",
|
|
594
|
+
beacon: false,
|
|
595
|
+
beaconSide: 0,
|
|
596
|
+
glovebox: false
|
|
534
597
|
};
|
|
598
|
+
const hasKey = options.hasKey !== false;
|
|
599
|
+
const hotwireSeconds = Math.max(0.1, options.hotwireSeconds ?? 8);
|
|
600
|
+
const beaconHz = options.beaconHz ?? 2.5;
|
|
601
|
+
let beaconClock = 0;
|
|
602
|
+
let crankAsked = false;
|
|
603
|
+
let lastSpeed = 0;
|
|
604
|
+
let engineRunning = !!options.running;
|
|
535
605
|
const blinkHz = options.blinkHz ?? 1.5;
|
|
536
606
|
const cancelAt = options.indicatorCancel ?? 0.15;
|
|
537
607
|
let blink = 0;
|
|
@@ -543,15 +613,21 @@ function createVehicleControls(options) {
|
|
|
543
613
|
const emit = (event) => options.onEvent?.(event);
|
|
544
614
|
const LAMPS = ["off", "position", "low"];
|
|
545
615
|
const MODES = ["4x2", "4x4", "auto"];
|
|
616
|
+
const WIPERS = ["off", "intermittent", "slow", "fast"];
|
|
617
|
+
const PROFILES = ["eco", "normal", "sport"];
|
|
618
|
+
const SIRENS = ["off", "wail", "yelp"];
|
|
546
619
|
const command = (c) => {
|
|
547
620
|
switch (c) {
|
|
548
621
|
case "ignition":
|
|
622
|
+
if (!hasKey && !state.hotwire.done) return emit({ type: "refused", command: c, reason: "no_key" });
|
|
549
623
|
state.ignition = !state.ignition;
|
|
550
624
|
if (state.ignition && state.alarm !== "off") state.alarm = "off";
|
|
625
|
+
if (state.ignition) crankAsked = true;
|
|
626
|
+
else state.cruise.on = false;
|
|
551
627
|
break;
|
|
552
628
|
case "gearbox":
|
|
553
629
|
state.gearbox = state.gearbox === "automatic" ? "manual" : "automatic";
|
|
554
|
-
state.gear = Math.max(1, state.gear);
|
|
630
|
+
state.gear = options.clutchPedal && state.gearbox === "manual" && Math.abs(lastSpeed) < 0.5 ? 0 : Math.max(1, state.gear);
|
|
555
631
|
break;
|
|
556
632
|
case "gear_up":
|
|
557
633
|
case "gear_down":
|
|
@@ -590,6 +666,9 @@ function createVehicleControls(options) {
|
|
|
590
666
|
case "radio_source":
|
|
591
667
|
state.radio.source = state.radio.source === "radio" ? "tape" : "radio";
|
|
592
668
|
break;
|
|
669
|
+
case "radio_track":
|
|
670
|
+
state.radio.track = (state.radio.track + 1) % Math.max(1, options.radioTracks ?? 3);
|
|
671
|
+
break;
|
|
593
672
|
case "exit":
|
|
594
673
|
break;
|
|
595
674
|
case "indicator_left":
|
|
@@ -606,11 +685,48 @@ function createVehicleControls(options) {
|
|
|
606
685
|
blink = 0;
|
|
607
686
|
break;
|
|
608
687
|
case "wipers":
|
|
609
|
-
state.wipers = state.wipers
|
|
688
|
+
state.wipers = WIPERS[(WIPERS.indexOf(state.wipers) + 1) % WIPERS.length];
|
|
689
|
+
break;
|
|
690
|
+
case "drive_profile":
|
|
691
|
+
state.profile = PROFILES[(PROFILES.indexOf(state.profile) + 1) % PROFILES.length];
|
|
692
|
+
break;
|
|
693
|
+
case "traction_control":
|
|
694
|
+
state.tractionControl = !state.tractionControl;
|
|
695
|
+
break;
|
|
696
|
+
case "abs":
|
|
697
|
+
state.abs = !state.abs;
|
|
698
|
+
break;
|
|
699
|
+
case "cruise":
|
|
700
|
+
if (state.cruise.on) state.cruise.on = false;
|
|
701
|
+
else if (!engineRunning) return emit({ type: "refused", command: c, reason: "ignition_off" });
|
|
702
|
+
else if (lastSpeed < DEFAULT_CRUISE.minSpeed) return emit({ type: "refused", command: c, reason: "too_slow" });
|
|
703
|
+
else state.cruise = { on: true, speed: lastSpeed };
|
|
704
|
+
break;
|
|
705
|
+
case "climate":
|
|
706
|
+
state.climate.on = !state.climate.on;
|
|
707
|
+
if (!state.climate.on) state.climate.demist = false;
|
|
708
|
+
break;
|
|
709
|
+
case "demist":
|
|
710
|
+
state.climate.demist = !state.climate.demist;
|
|
711
|
+
if (state.climate.demist) state.climate.on = true;
|
|
610
712
|
break;
|
|
611
713
|
case "seatbelt":
|
|
612
714
|
state.seatbelt = !state.seatbelt;
|
|
613
715
|
break;
|
|
716
|
+
case "siren":
|
|
717
|
+
if (!options.emergency) return emit({ type: "refused", command: c, reason: "no_siren" });
|
|
718
|
+
state.siren = SIRENS[(SIRENS.indexOf(state.siren) + 1) % SIRENS.length];
|
|
719
|
+
if (state.siren !== "off") state.beacon = true;
|
|
720
|
+
break;
|
|
721
|
+
case "beacon":
|
|
722
|
+
if (!options.emergency) return emit({ type: "refused", command: c, reason: "no_siren" });
|
|
723
|
+
state.beacon = !state.beacon;
|
|
724
|
+
if (!state.beacon) state.siren = "off";
|
|
725
|
+
beaconClock = 0;
|
|
726
|
+
break;
|
|
727
|
+
case "glovebox":
|
|
728
|
+
state.glovebox = !state.glovebox;
|
|
729
|
+
break;
|
|
614
730
|
}
|
|
615
731
|
emit({ type: "command", command: c, state });
|
|
616
732
|
};
|
|
@@ -649,17 +765,66 @@ function createVehicleControls(options) {
|
|
|
649
765
|
else if (turned && way < cancelAt) state.indicator = "off";
|
|
650
766
|
}
|
|
651
767
|
} else state.blinkOn = false;
|
|
768
|
+
if (state.beacon) {
|
|
769
|
+
beaconClock += dt;
|
|
770
|
+
state.beaconSide = Math.floor(beaconClock * beaconHz) % 2;
|
|
771
|
+
}
|
|
772
|
+
if (!hasKey && !state.hotwire.done && !state.ignition && any(keys.hotwire)) {
|
|
773
|
+
if (state.hotwire.progress === 0) {
|
|
774
|
+
if (state.alarm === "armed") state.alarm = "ringing";
|
|
775
|
+
emit({ type: "hotwire", stage: "started" });
|
|
776
|
+
}
|
|
777
|
+
state.hotwire.progress = Math.min(1, state.hotwire.progress + dt / hotwireSeconds);
|
|
778
|
+
if (state.hotwire.progress >= 1) {
|
|
779
|
+
state.hotwire.done = true;
|
|
780
|
+
state.ignition = true;
|
|
781
|
+
crankAsked = true;
|
|
782
|
+
emit({ type: "hotwire", stage: "joined" });
|
|
783
|
+
}
|
|
784
|
+
}
|
|
652
785
|
state.horn = any(keys.horn);
|
|
653
|
-
|
|
786
|
+
state.lamps.flash = any(keys.flash);
|
|
787
|
+
state.washer = any(keys.washer);
|
|
788
|
+
state.clutch = ease(state.clutch, any(keys.clutch) ? 1 : 0, any(keys.clutch) ? 8 : 2.5, dt);
|
|
789
|
+
const speed = driver?.state.u ?? 0;
|
|
790
|
+
lastSpeed = speed;
|
|
791
|
+
if (state.cruise.on && (brake > 0.05 || state.clutch > 0.5 || handbrake)) {
|
|
792
|
+
state.cruise.on = false;
|
|
793
|
+
emit({ type: "cruise_cancelled" });
|
|
794
|
+
}
|
|
795
|
+
const tune = DRIVE_PROFILES[state.profile];
|
|
796
|
+
let gasNow = state.ignition ? throttle * tune.throttle : 0;
|
|
797
|
+
if (state.cruise.on && state.ignition) gasNow = Math.max(gasNow, cruiseThrottle(state.cruise.speed, speed));
|
|
798
|
+
const gasStart = state.gearbox === "automatic" && state.ignition && !engineRunning && throttle > 0 && Math.abs(speed) < 0.5;
|
|
799
|
+
state.cranking = false;
|
|
800
|
+
if (crankAsked || gasStart) {
|
|
801
|
+
crankAsked = false;
|
|
802
|
+
if (options.canCrank && !options.canCrank()) emit({ type: "refused", command: "ignition", reason: "battery_flat" });
|
|
803
|
+
else {
|
|
804
|
+
state.cranking = true;
|
|
805
|
+
options.onCrank?.();
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
const driven = (driver?.state.slip ?? []).filter((_, i) => driver?.spec.wheels?.[i]?.drive ?? true);
|
|
809
|
+
const powerLimit = (state.tractionControl ? tractionLimit(driven) : 1) * (options.powerLimit?.() ?? 1);
|
|
654
810
|
const reversing = state.gearbox === "manual" ? state.gear < 0 : (driver?.state.gear ?? 1) < 0;
|
|
655
811
|
state.reverseLights = reversing;
|
|
656
|
-
state.brakeLights = handbrake || (reversing ? gasNow > 0.05 &&
|
|
657
|
-
|
|
658
|
-
|
|
812
|
+
state.brakeLights = handbrake || (reversing ? gasNow > 0.05 && speed < -0.3 : brake > 0.05);
|
|
813
|
+
const common = { steer, handbrake, horn: state.horn, start: state.cranking, powerLimit };
|
|
814
|
+
if (state.gearbox === "automatic") return { ...common, throttle: gasNow, brake };
|
|
815
|
+
const pedals = state.gear < 0 ? { throttle: brake, brake: gasNow } : { throttle: gasNow, brake };
|
|
816
|
+
return { ...common, ...pedals, gear: state.gear, ...options.clutchPedal ? { clutch: state.clutch } : {} };
|
|
659
817
|
},
|
|
660
818
|
after(driver, dt) {
|
|
661
|
-
if (driver.state.engineOn && !state.ignition && driver.state.u !== 0) state.ignition = true;
|
|
662
819
|
if (!state.ignition && driver.state.engineOn) driver.state.engineOn = false;
|
|
820
|
+
engineRunning = driver.state.engineOn;
|
|
821
|
+
if (!engineRunning) state.cruise.on = false;
|
|
822
|
+
const spec = driver.spec;
|
|
823
|
+
spec.abs = state.abs;
|
|
824
|
+
if (spec.upshiftFraction != null) {
|
|
825
|
+
spec.upshiftFraction = DRIVE_PROFILES[state.profile].upshift;
|
|
826
|
+
spec.downshiftFraction = DRIVE_PROFILES[state.profile].downshift;
|
|
827
|
+
}
|
|
663
828
|
if (state.driveMode === "auto" && options.fourWheelDrive) {
|
|
664
829
|
const slip = Math.max(0, ...driver.state.slip);
|
|
665
830
|
if (slip > auto4.slip) {
|
|
@@ -673,6 +838,10 @@ function createVehicleControls(options) {
|
|
|
673
838
|
held.clear();
|
|
674
839
|
throttle = brake = steer = 0;
|
|
675
840
|
state.horn = false;
|
|
841
|
+
state.lamps.flash = false;
|
|
842
|
+
state.washer = false;
|
|
843
|
+
state.clutch = 0;
|
|
844
|
+
state.cruise.on = false;
|
|
676
845
|
}
|
|
677
846
|
};
|
|
678
847
|
}
|
|
@@ -689,33 +858,256 @@ function conditionFromDamage(spec, report) {
|
|
|
689
858
|
best = tyre;
|
|
690
859
|
}
|
|
691
860
|
}
|
|
692
|
-
return best
|
|
861
|
+
return best;
|
|
693
862
|
});
|
|
694
863
|
return {
|
|
695
|
-
tyres,
|
|
864
|
+
tyres: tyres.map((tyre) => tyre?.health ?? 1),
|
|
865
|
+
rims: tyres.map((tyre) => !!tyre?.rim),
|
|
696
866
|
...report.brakes != null ? { brakes: report.brakes } : {},
|
|
697
867
|
...report.engine != null ? { engine: report.engine } : {},
|
|
698
868
|
...report.suspension != null ? { suspension: report.suspension } : {}
|
|
699
869
|
};
|
|
700
870
|
}
|
|
871
|
+
|
|
872
|
+
// src/systems.ts
|
|
873
|
+
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 };
|
|
874
|
+
var clamp3 = (x, a, b) => Math.max(a, Math.min(b, x));
|
|
875
|
+
function createVehicleSystems(options = {}) {
|
|
876
|
+
const loads = { ...DEFAULT_LOADS, ...options.loads };
|
|
877
|
+
const capacity = options.batteryWh ?? 720;
|
|
878
|
+
const alternator = options.alternatorW ?? { idle: 350, full: 1100 };
|
|
879
|
+
const crankCharge = options.crankCharge ?? 0.25;
|
|
880
|
+
const startWh = options.startWh ?? 0.6;
|
|
881
|
+
const ambient = options.ambientC ?? 25;
|
|
882
|
+
const thermostat = options.thermostatC ?? 90;
|
|
883
|
+
const limp = options.limpC ?? 115;
|
|
884
|
+
const seize = options.seizeC ?? 130;
|
|
885
|
+
const heat = options.heatPerSecond ?? 0.5;
|
|
886
|
+
const cooling = options.cooling ?? { stopped: 4e-3, fast: 0.03 };
|
|
887
|
+
const state = { charge: clamp3(options.charge ?? 1, 0, 1), coolantC: ambient, temperature: 0, overheating: false, seized: false, spares: options.spares ?? 1, netW: 0 };
|
|
888
|
+
const draw = (c) => {
|
|
889
|
+
const lamps = c.lamps;
|
|
890
|
+
let w = lamps.mode === "off" ? 0 : loads.position;
|
|
891
|
+
if (lamps.mode === "low") w += loads.low;
|
|
892
|
+
if (lamps.mode === "high") w += loads.low + loads.high;
|
|
893
|
+
if (lamps.flash) w += loads.high;
|
|
894
|
+
if (lamps.fog) w += loads.fog;
|
|
895
|
+
if (lamps.interior) w += loads.interior;
|
|
896
|
+
if (c.radio.on) w += loads.radio;
|
|
897
|
+
if (c.wipers !== "off" || c.washer) w += loads.wipers;
|
|
898
|
+
if (c.indicator !== "off") w += loads.indicators * (c.indicator === "hazard" ? 2 : 1) * 0.5;
|
|
899
|
+
if (c.horn) w += loads.horn;
|
|
900
|
+
if (c.alarm !== "off") w += loads.alarm;
|
|
901
|
+
if (c.brakeLights) w += loads.brakeLights;
|
|
902
|
+
if (c.climate.on) w += loads.climate;
|
|
903
|
+
if (c.siren !== "off") w += loads.siren;
|
|
904
|
+
if (c.beacon) w += loads.beacon;
|
|
905
|
+
return w;
|
|
906
|
+
};
|
|
907
|
+
return {
|
|
908
|
+
state,
|
|
909
|
+
canCrank: () => state.charge >= crankCharge && !state.seized,
|
|
910
|
+
crank() {
|
|
911
|
+
state.charge = clamp3(state.charge - startWh / capacity, 0, 1);
|
|
912
|
+
},
|
|
913
|
+
powerLimit: () => state.coolantC <= limp ? 1 : clamp3(1 - (state.coolantC - limp) / (seize - limp), 0.25, 1),
|
|
914
|
+
step({ driver, controls, throttle, dt, radiator = 1, coolantC }) {
|
|
915
|
+
const events = [];
|
|
916
|
+
const running = driver.state.engineOn;
|
|
917
|
+
const charging = running ? alternator.idle + (alternator.full - alternator.idle) * clamp3((driver.state.rpm - 900) / 1100, 0, 1) : 0;
|
|
918
|
+
const net = charging - draw(controls);
|
|
919
|
+
state.netW = net;
|
|
920
|
+
const before = state.charge;
|
|
921
|
+
state.charge = clamp3(state.charge + net * dt / 3600 / capacity, 0, 1);
|
|
922
|
+
if (before >= crankCharge && state.charge < crankCharge && !running) events.push({ type: "battery_flat" });
|
|
923
|
+
const work = running ? clamp3(0.15 + 0.85 * throttle * (driver.state.rpm / 6500), 0, 1) : 0;
|
|
924
|
+
const air = cooling.stopped + (cooling.fast - cooling.stopped) * clamp3(Math.abs(driver.state.u) / 27.8, 0, 1);
|
|
925
|
+
const cool = air * clamp3(radiator, 0.05, 1) * (running ? 1 + (controls.climate.on ? 0.15 : 0) : 1);
|
|
926
|
+
const opened = state.coolantC >= thermostat ? 1 : 0.15;
|
|
927
|
+
state.coolantC += (heat * work - cool * opened * (state.coolantC - ambient)) * dt;
|
|
928
|
+
state.coolantC = coolantC ?? Math.max(ambient, state.coolantC);
|
|
929
|
+
state.temperature = clamp3((state.coolantC - ambient) / (seize - ambient), 0, 1);
|
|
930
|
+
const hot = state.coolantC > limp;
|
|
931
|
+
if (hot && !state.overheating) events.push({ type: "overheating" });
|
|
932
|
+
state.overheating = hot;
|
|
933
|
+
if (state.coolantC >= seize && !state.seized) {
|
|
934
|
+
state.seized = true;
|
|
935
|
+
events.push({ type: "seized" });
|
|
936
|
+
}
|
|
937
|
+
if (state.seized) driver.state.engineOn = false;
|
|
938
|
+
return events;
|
|
939
|
+
},
|
|
940
|
+
fitSpare(driver, wheel) {
|
|
941
|
+
if (state.spares <= 0) return { type: "no_spare" };
|
|
942
|
+
state.spares--;
|
|
943
|
+
const tyres = [...driver.condition.tyres ?? []];
|
|
944
|
+
tyres[wheel] = 1;
|
|
945
|
+
driver.condition = { ...driver.condition, tyres };
|
|
946
|
+
return { type: "spare_fitted", wheel };
|
|
947
|
+
},
|
|
948
|
+
charge(share) {
|
|
949
|
+
state.charge = clamp3(state.charge + share, 0, 1);
|
|
950
|
+
}
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
// src/ejection.ts
|
|
955
|
+
var DEFAULT_EJECTION = { frontal: 8.3, side: 6, keep: 0.8, lift: 2.5 };
|
|
956
|
+
function ejectionOf(pulse, belted, options = DEFAULT_EJECTION) {
|
|
957
|
+
if (belted) return null;
|
|
958
|
+
const du = pulse.after.u - pulse.before.u;
|
|
959
|
+
const dw = pulse.after.w - pulse.before.w;
|
|
960
|
+
const deltaV = Math.hypot(du, dw);
|
|
961
|
+
const frontal = Math.abs(du) >= Math.abs(dw);
|
|
962
|
+
if (frontal && Math.abs(du) < options.frontal && !pulse.rolled) return null;
|
|
963
|
+
if (!frontal && Math.abs(dw) < options.side && !pulse.rolled) return null;
|
|
964
|
+
const exit = frontal ? du < 0 ? "windshield" : "rear_window" : dw < 0 ? "right_window" : "left_window";
|
|
965
|
+
return { exit, velocity: [pulse.before.u * options.keep, pulse.before.w * options.keep, options.lift], deltaV };
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
// src/parking-sensor.ts
|
|
969
|
+
var DEFAULT_PARKING_SENSOR = { range: 1.5, solid: 0.3, slowest: 0.8, fastest: 0.12 };
|
|
970
|
+
function parkingBeepInterval(distance, options = DEFAULT_PARKING_SENSOR) {
|
|
971
|
+
if (distance > options.range) return null;
|
|
972
|
+
if (distance <= options.solid) return 0;
|
|
973
|
+
const t = (distance - options.solid) / (options.range - options.solid);
|
|
974
|
+
return options.fastest + (options.slowest - options.fastest) * t;
|
|
975
|
+
}
|
|
976
|
+
function bumperDistance(params) {
|
|
977
|
+
const [hx, hz] = params.heading;
|
|
978
|
+
const sign = params.forward ? 1 : -1;
|
|
979
|
+
let best = Infinity;
|
|
980
|
+
for (const [ox, oz] of params.obstacles) {
|
|
981
|
+
const dx = ox - params.x;
|
|
982
|
+
const dz = oz - params.z;
|
|
983
|
+
const along = (dx * hx + dz * hz) * sign - params.length / 2;
|
|
984
|
+
const side = Math.abs(-dx * hz + dz * hx);
|
|
985
|
+
if (along >= 0 && side <= params.width / 2 + 0.2) best = Math.min(best, along);
|
|
986
|
+
}
|
|
987
|
+
return best;
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
// src/storage.ts
|
|
991
|
+
var GLOVEBOX = { id: "glovebox", slots: 6, capacityKg: 4, access: "cabin" };
|
|
992
|
+
var trunkFor = (litres = 450) => ({ id: "trunk", slots: Math.max(4, Math.round(litres / 25)), capacityKg: Math.round(litres / 4), access: "lid" });
|
|
993
|
+
var weightOf = (stack) => stack.unitKg * stack.count;
|
|
994
|
+
function createVehicleStorage(options = {}) {
|
|
995
|
+
const compartments = [...options.compartments ?? [GLOVEBOX, trunkFor()]];
|
|
996
|
+
const slots = new Map(compartments.map((c) => [c.id, Array.from({ length: c.slots }, () => null)]));
|
|
997
|
+
const specOf = (id) => compartments.find((c) => c.id === id);
|
|
998
|
+
const load = (id) => (slots.get(id) ?? []).reduce((kg, s) => kg + (s ? weightOf(s) : 0), 0);
|
|
999
|
+
const reachable = (id, access) => {
|
|
1000
|
+
const spec = specOf(id);
|
|
1001
|
+
if (!spec) return false;
|
|
1002
|
+
return spec.access === "lid" ? access.lid >= 0.5 : access.gloveboxOpen && (access.seated || access.frontDoorOpen);
|
|
1003
|
+
};
|
|
1004
|
+
const place = (id, stack) => {
|
|
1005
|
+
const spec = specOf(id);
|
|
1006
|
+
const list = slots.get(id);
|
|
1007
|
+
if (!spec || !list) return "missing";
|
|
1008
|
+
if (load(id) + weightOf(stack) > spec.capacityKg + 1e-9) return "too_heavy";
|
|
1009
|
+
const same = stack.data === void 0 ? list.findIndex((s) => s !== null && s.itemId === stack.itemId && s.data === void 0) : -1;
|
|
1010
|
+
if (same >= 0) {
|
|
1011
|
+
const s = list[same];
|
|
1012
|
+
list[same] = { ...s, count: s.count + stack.count };
|
|
1013
|
+
return "ok";
|
|
1014
|
+
}
|
|
1015
|
+
const free = list.indexOf(null);
|
|
1016
|
+
if (free < 0) return "full";
|
|
1017
|
+
list[free] = { ...stack };
|
|
1018
|
+
return "ok";
|
|
1019
|
+
};
|
|
1020
|
+
return {
|
|
1021
|
+
compartments,
|
|
1022
|
+
reachable,
|
|
1023
|
+
contents: (id) => [...slots.get(id) ?? []],
|
|
1024
|
+
loadKg: (id) => id === void 0 ? compartments.reduce((kg, c) => kg + load(c.id), 0) : load(id),
|
|
1025
|
+
put(id, stack, access) {
|
|
1026
|
+
if (!specOf(id)) return "missing";
|
|
1027
|
+
if (!reachable(id, access)) return "shut";
|
|
1028
|
+
return place(id, stack);
|
|
1029
|
+
},
|
|
1030
|
+
take(id, slot, access) {
|
|
1031
|
+
const list = slots.get(id);
|
|
1032
|
+
if (!list || !reachable(id, access)) return null;
|
|
1033
|
+
const stack = list[slot] ?? null;
|
|
1034
|
+
if (stack) list[slot] = null;
|
|
1035
|
+
return stack;
|
|
1036
|
+
},
|
|
1037
|
+
stock(id, stacks) {
|
|
1038
|
+
return stacks.filter((s) => place(id, s) !== "ok");
|
|
1039
|
+
},
|
|
1040
|
+
clear() {
|
|
1041
|
+
for (const list of slots.values()) list.fill(null);
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
// src/windshield-fog.ts
|
|
1047
|
+
var clamp4 = (x, a, b) => Math.max(a, Math.min(b, x));
|
|
1048
|
+
function createWindshieldFog(options = {}) {
|
|
1049
|
+
const outside = options.outsideHumidity ?? 0.6;
|
|
1050
|
+
const breath = options.breath ?? 4e-3;
|
|
1051
|
+
const wet = options.wet ?? 2e-3;
|
|
1052
|
+
const air = options.air ?? { shut: 2e-3, window: 0.05, climate: 0.01, demist: 0.06 };
|
|
1053
|
+
const coldSpan = options.coldSpan ?? 15;
|
|
1054
|
+
const rates = options.rates ?? { build: 0.05, clear: 0.03, demist: 0.25 };
|
|
1055
|
+
const state = { humidity: outside, fog: 0 };
|
|
1056
|
+
return {
|
|
1057
|
+
state,
|
|
1058
|
+
step({ dt, occupants, outsideC, cabinC, rain = 0, speed = 0, windows = 0, climate, engineWarm = 1 }) {
|
|
1059
|
+
const exchange = air.shut + air.window * clamp4(windows, 0, 1) + (climate.on ? air.climate : 0) + (climate.demist ? air.demist : 0);
|
|
1060
|
+
const source = breath * Math.max(0, occupants) + wet * clamp4(rain, 0, 1) * (occupants > 0 ? 1 : 0);
|
|
1061
|
+
const towards = climate.on ? outside * 0.4 : outside;
|
|
1062
|
+
state.humidity = clamp4(state.humidity + (source - exchange * (state.humidity - towards)) * dt, 0, 1);
|
|
1063
|
+
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);
|
|
1064
|
+
const warmed = climate.demist ? cold * (1 - 0.9 * clamp4(engineWarm, 0, 1)) : cold;
|
|
1065
|
+
const clearUpTo = 1 - 0.5 * clamp4(warmed / coldSpan, 0, 1);
|
|
1066
|
+
const target = clamp4((state.humidity - clearUpTo) * 6, 0, 1);
|
|
1067
|
+
const down = climate.demist ? rates.demist * (0.3 + 0.7 * clamp4(engineWarm, 0, 1)) : rates.clear;
|
|
1068
|
+
state.fog = target > state.fog ? Math.min(target, state.fog + rates.build * dt) : Math.max(target, state.fog - down * dt);
|
|
1069
|
+
return state;
|
|
1070
|
+
},
|
|
1071
|
+
set(next) {
|
|
1072
|
+
if (next.humidity !== void 0) state.humidity = clamp4(next.humidity, 0, 1);
|
|
1073
|
+
if (next.fog !== void 0) state.fog = clamp4(next.fog, 0, 1);
|
|
1074
|
+
}
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
701
1077
|
export {
|
|
702
1078
|
DEFAULT_CONTROL_KEYS,
|
|
1079
|
+
DEFAULT_CRUISE,
|
|
1080
|
+
DEFAULT_EJECTION,
|
|
1081
|
+
DEFAULT_LOADS,
|
|
1082
|
+
DEFAULT_PARKING_SENSOR,
|
|
1083
|
+
DEFAULT_TRACTION,
|
|
1084
|
+
DRIVE_PROFILES,
|
|
1085
|
+
GLOVEBOX,
|
|
703
1086
|
bodyDamage,
|
|
1087
|
+
bumperDistance,
|
|
704
1088
|
conditionFromDamage,
|
|
705
1089
|
createArcadeDriver,
|
|
706
1090
|
createSemiSimDriver,
|
|
707
1091
|
createSurfaces,
|
|
708
1092
|
createVehicleControls,
|
|
1093
|
+
createVehicleStorage,
|
|
1094
|
+
createVehicleSystems,
|
|
1095
|
+
createWindshieldFog,
|
|
1096
|
+
cruiseThrottle,
|
|
709
1097
|
defaultArcadeSpec,
|
|
710
1098
|
defaultFuelModel,
|
|
711
1099
|
defaultSemiSpec,
|
|
712
1100
|
defaultSurfaces,
|
|
1101
|
+
ejectionOf,
|
|
713
1102
|
fuelRate,
|
|
714
1103
|
impactEnergy,
|
|
1104
|
+
parkingBeepInterval,
|
|
715
1105
|
runOver,
|
|
716
1106
|
semiSpecFromStats,
|
|
717
1107
|
speedLoss,
|
|
718
1108
|
surfaceGrip,
|
|
1109
|
+
tractionLimit,
|
|
1110
|
+
trunkFor,
|
|
719
1111
|
vehicleCorners,
|
|
720
1112
|
vehicleDamageFromImpact
|
|
721
1113
|
};
|