@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.
- package/dist/index.d.ts +383 -1
- package/dist/index.global.js +467 -11
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +466 -11
- 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,312 @@ 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
|
+
|
|
514
|
+
// src/controls.ts
|
|
515
|
+
var DEFAULT_CONTROL_KEYS = {
|
|
516
|
+
throttle: ["KeyW", "ArrowUp"],
|
|
517
|
+
brake: ["KeyS", "ArrowDown"],
|
|
518
|
+
left: ["KeyA", "ArrowLeft"],
|
|
519
|
+
right: ["KeyD", "ArrowRight"],
|
|
520
|
+
handbrake: ["Space"],
|
|
521
|
+
horn: ["KeyH"],
|
|
522
|
+
clutch: ["KeyC"],
|
|
523
|
+
flash: ["KeyX"],
|
|
524
|
+
washer: ["KeyZ"],
|
|
525
|
+
ignition: ["KeyI"],
|
|
526
|
+
gearbox: ["KeyM"],
|
|
527
|
+
gear_up: ["KeyE"],
|
|
528
|
+
gear_down: ["KeyQ"],
|
|
529
|
+
parking_brake: ["KeyP"],
|
|
530
|
+
drive_mode: ["KeyG"],
|
|
531
|
+
lights: ["KeyL"],
|
|
532
|
+
high_beam: ["KeyK"],
|
|
533
|
+
fog_lights: ["KeyN"],
|
|
534
|
+
interior_light: ["KeyU"],
|
|
535
|
+
alarm: ["KeyY"],
|
|
536
|
+
radio: ["KeyR"],
|
|
537
|
+
radio_source: ["KeyT"],
|
|
538
|
+
radio_track: ["BracketRight"],
|
|
539
|
+
exit: ["KeyF"],
|
|
540
|
+
indicator_left: ["Comma"],
|
|
541
|
+
indicator_right: ["Period"],
|
|
542
|
+
hazard: ["KeyB"],
|
|
543
|
+
wipers: ["KeyO"],
|
|
544
|
+
seatbelt: ["KeyJ"],
|
|
545
|
+
drive_profile: ["Digit7"],
|
|
546
|
+
traction_control: ["Digit8"],
|
|
547
|
+
abs: ["Digit9"],
|
|
548
|
+
cruise: ["Digit0"],
|
|
549
|
+
climate: ["Minus"],
|
|
550
|
+
demist: ["Equal"]
|
|
551
|
+
};
|
|
552
|
+
var HELD_KEYS = ["throttle", "brake", "left", "right", "handbrake", "horn", "clutch", "flash", "washer"];
|
|
553
|
+
function createVehicleControls(options) {
|
|
554
|
+
const keys = { ...DEFAULT_CONTROL_KEYS, ...options.keys };
|
|
555
|
+
const auto4 = options.autoFourWheel ?? { slip: 0.18, releaseSeconds: 2 };
|
|
556
|
+
const hasFog = options.fogLamps !== false;
|
|
557
|
+
const held = /* @__PURE__ */ new Set();
|
|
558
|
+
const any = (codes) => codes.some((c) => held.has(c));
|
|
559
|
+
const commandOf = /* @__PURE__ */ new Map();
|
|
560
|
+
for (const [command2, codes] of Object.entries(keys)) if (!HELD_KEYS.includes(command2)) for (const code of codes) commandOf.set(code, command2);
|
|
561
|
+
const driving = new Set(Object.values(keys).flat());
|
|
562
|
+
const state = {
|
|
563
|
+
ignition: !!options.running,
|
|
564
|
+
gearbox: options.gearbox ?? "automatic",
|
|
565
|
+
gear: 1,
|
|
566
|
+
parkingBrake: false,
|
|
567
|
+
driveMode: options.fourWheelDrive ? "auto" : "4x2",
|
|
568
|
+
fourWheelEngaged: false,
|
|
569
|
+
lamps: { mode: "off", fog: false, interior: false, flash: false },
|
|
570
|
+
brakeLights: false,
|
|
571
|
+
reverseLights: false,
|
|
572
|
+
horn: false,
|
|
573
|
+
alarm: "off",
|
|
574
|
+
radio: { on: false, source: "radio", track: 0 },
|
|
575
|
+
indicator: "off",
|
|
576
|
+
blinkOn: false,
|
|
577
|
+
tick: false,
|
|
578
|
+
wipers: "off",
|
|
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
|
|
588
|
+
};
|
|
589
|
+
let crankAsked = false;
|
|
590
|
+
let lastSpeed = 0;
|
|
591
|
+
let engineRunning = !!options.running;
|
|
592
|
+
const blinkHz = options.blinkHz ?? 1.5;
|
|
593
|
+
const cancelAt = options.indicatorCancel ?? 0.15;
|
|
594
|
+
let blink = 0;
|
|
595
|
+
let turned = 0;
|
|
596
|
+
let throttle = 0;
|
|
597
|
+
let brake = 0;
|
|
598
|
+
let steer = 0;
|
|
599
|
+
let calm = 0;
|
|
600
|
+
const emit = (event) => options.onEvent?.(event);
|
|
601
|
+
const LAMPS = ["off", "position", "low"];
|
|
602
|
+
const MODES = ["4x2", "4x4", "auto"];
|
|
603
|
+
const WIPERS = ["off", "intermittent", "slow", "fast"];
|
|
604
|
+
const PROFILES = ["eco", "normal", "sport"];
|
|
605
|
+
const command = (c) => {
|
|
606
|
+
switch (c) {
|
|
607
|
+
case "ignition":
|
|
608
|
+
state.ignition = !state.ignition;
|
|
609
|
+
if (state.ignition && state.alarm !== "off") state.alarm = "off";
|
|
610
|
+
if (state.ignition) crankAsked = true;
|
|
611
|
+
else state.cruise.on = false;
|
|
612
|
+
break;
|
|
613
|
+
case "gearbox":
|
|
614
|
+
state.gearbox = state.gearbox === "automatic" ? "manual" : "automatic";
|
|
615
|
+
state.gear = options.clutchPedal && state.gearbox === "manual" && Math.abs(lastSpeed) < 0.5 ? 0 : Math.max(1, state.gear);
|
|
616
|
+
break;
|
|
617
|
+
case "gear_up":
|
|
618
|
+
case "gear_down":
|
|
619
|
+
if (state.gearbox !== "manual") return;
|
|
620
|
+
state.gear = Math.max(-1, Math.min(options.gears, state.gear + (c === "gear_up" ? 1 : -1)));
|
|
621
|
+
break;
|
|
622
|
+
case "parking_brake":
|
|
623
|
+
state.parkingBrake = !state.parkingBrake;
|
|
624
|
+
break;
|
|
625
|
+
case "drive_mode": {
|
|
626
|
+
if (!options.fourWheelDrive) return emit({ type: "refused", command: c, reason: "no_four_wheel_drive" });
|
|
627
|
+
state.driveMode = MODES[(MODES.indexOf(state.driveMode) + 1) % MODES.length];
|
|
628
|
+
state.fourWheelEngaged = state.driveMode === "4x4";
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
case "lights":
|
|
632
|
+
state.lamps.mode = state.lamps.mode === "high" ? "off" : LAMPS[(LAMPS.indexOf(state.lamps.mode) + 1) % LAMPS.length];
|
|
633
|
+
break;
|
|
634
|
+
case "high_beam":
|
|
635
|
+
state.lamps.mode = state.lamps.mode === "high" ? "low" : "high";
|
|
636
|
+
break;
|
|
637
|
+
case "fog_lights":
|
|
638
|
+
if (!hasFog) return emit({ type: "refused", command: c, reason: "no_fog_lamps" });
|
|
639
|
+
state.lamps.fog = !state.lamps.fog;
|
|
640
|
+
if (state.lamps.fog && state.lamps.mode === "off") state.lamps.mode = "position";
|
|
641
|
+
break;
|
|
642
|
+
case "interior_light":
|
|
643
|
+
state.lamps.interior = !state.lamps.interior;
|
|
644
|
+
break;
|
|
645
|
+
case "alarm":
|
|
646
|
+
state.alarm = state.alarm === "off" ? "armed" : state.alarm === "armed" ? "ringing" : "off";
|
|
647
|
+
break;
|
|
648
|
+
case "radio":
|
|
649
|
+
state.radio.on = !state.radio.on;
|
|
650
|
+
break;
|
|
651
|
+
case "radio_source":
|
|
652
|
+
state.radio.source = state.radio.source === "radio" ? "tape" : "radio";
|
|
653
|
+
break;
|
|
654
|
+
case "radio_track":
|
|
655
|
+
state.radio.track = (state.radio.track + 1) % Math.max(1, options.radioTracks ?? 3);
|
|
656
|
+
break;
|
|
657
|
+
case "exit":
|
|
658
|
+
break;
|
|
659
|
+
case "indicator_left":
|
|
660
|
+
case "indicator_right": {
|
|
661
|
+
const side = c === "indicator_left" ? "left" : "right";
|
|
662
|
+
if (state.indicator === "hazard") return;
|
|
663
|
+
state.indicator = state.indicator === side ? "off" : side;
|
|
664
|
+
turned = 0;
|
|
665
|
+
blink = 0;
|
|
666
|
+
break;
|
|
667
|
+
}
|
|
668
|
+
case "hazard":
|
|
669
|
+
state.indicator = state.indicator === "hazard" ? "off" : "hazard";
|
|
670
|
+
blink = 0;
|
|
671
|
+
break;
|
|
672
|
+
case "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;
|
|
697
|
+
break;
|
|
698
|
+
case "seatbelt":
|
|
699
|
+
state.seatbelt = !state.seatbelt;
|
|
700
|
+
break;
|
|
701
|
+
}
|
|
702
|
+
emit({ type: "command", command: c, state });
|
|
703
|
+
};
|
|
704
|
+
const ease = (from, to, rate, dt) => from + Math.sign(to - from) * Math.min(Math.abs(to - from), rate * dt);
|
|
705
|
+
return {
|
|
706
|
+
keys,
|
|
707
|
+
state,
|
|
708
|
+
key(code, down) {
|
|
709
|
+
if (!driving.has(code)) return false;
|
|
710
|
+
const was = held.has(code);
|
|
711
|
+
if (down) held.add(code);
|
|
712
|
+
else held.delete(code);
|
|
713
|
+
const c = commandOf.get(code);
|
|
714
|
+
if (c && down && !was) command(c);
|
|
715
|
+
return true;
|
|
716
|
+
},
|
|
717
|
+
command,
|
|
718
|
+
input(dt, driver) {
|
|
719
|
+
const gas = any(keys.throttle) ? 1 : 0;
|
|
720
|
+
const stop = any(keys.brake) ? 1 : 0;
|
|
721
|
+
const wheel = (any(keys.left) ? 1 : 0) - (any(keys.right) ? 1 : 0);
|
|
722
|
+
throttle = ease(throttle, gas, gas > throttle ? 4 : 8, dt);
|
|
723
|
+
brake = ease(brake, stop, stop > brake ? 6 : 10, dt);
|
|
724
|
+
steer = ease(steer, wheel, wheel !== 0 && Math.sign(wheel) === Math.sign(steer || wheel) ? 3 : 6, dt);
|
|
725
|
+
const handbrake = state.parkingBrake || any(keys.handbrake);
|
|
726
|
+
state.tick = false;
|
|
727
|
+
if (state.indicator !== "off") {
|
|
728
|
+
const before = Math.floor(blink * blinkHz * 2);
|
|
729
|
+
blink += dt;
|
|
730
|
+
const now = Math.floor(blink * blinkHz * 2);
|
|
731
|
+
state.blinkOn = now % 2 === 0;
|
|
732
|
+
state.tick = now !== before;
|
|
733
|
+
if (state.indicator !== "hazard") {
|
|
734
|
+
const way = state.indicator === "left" ? steer : -steer;
|
|
735
|
+
if (way > 0.5) turned = 1;
|
|
736
|
+
else if (turned && way < cancelAt) state.indicator = "off";
|
|
737
|
+
}
|
|
738
|
+
} else state.blinkOn = false;
|
|
739
|
+
state.horn = any(keys.horn);
|
|
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);
|
|
764
|
+
const reversing = state.gearbox === "manual" ? state.gear < 0 : (driver?.state.gear ?? 1) < 0;
|
|
765
|
+
state.reverseLights = reversing;
|
|
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 } : {} };
|
|
771
|
+
},
|
|
772
|
+
after(driver, dt) {
|
|
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
|
+
}
|
|
782
|
+
if (state.driveMode === "auto" && options.fourWheelDrive) {
|
|
783
|
+
const slip = Math.max(0, ...driver.state.slip);
|
|
784
|
+
if (slip > auto4.slip) {
|
|
785
|
+
state.fourWheelEngaged = true;
|
|
786
|
+
calm = 0;
|
|
787
|
+
} else if (state.fourWheelEngaged && (calm += dt) > auto4.releaseSeconds) state.fourWheelEngaged = false;
|
|
788
|
+
} else state.fourWheelEngaged = state.driveMode === "4x4";
|
|
789
|
+
driver.spec.fourWheelDrive = !!options.fourWheelDrive && state.fourWheelEngaged;
|
|
790
|
+
},
|
|
791
|
+
release() {
|
|
792
|
+
held.clear();
|
|
793
|
+
throttle = brake = steer = 0;
|
|
794
|
+
state.horn = false;
|
|
795
|
+
state.lamps.flash = false;
|
|
796
|
+
state.washer = false;
|
|
797
|
+
state.clutch = 0;
|
|
798
|
+
state.cruise.on = false;
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
|
|
479
803
|
// src/semi/condition.ts
|
|
480
804
|
function conditionFromDamage(spec, report) {
|
|
481
805
|
const tyres = spec.wheels.map((wheel) => {
|
|
@@ -488,31 +812,162 @@ function conditionFromDamage(spec, report) {
|
|
|
488
812
|
best = tyre;
|
|
489
813
|
}
|
|
490
814
|
}
|
|
491
|
-
return best
|
|
815
|
+
return best;
|
|
492
816
|
});
|
|
493
817
|
return {
|
|
494
|
-
tyres,
|
|
818
|
+
tyres: tyres.map((tyre) => tyre?.health ?? 1),
|
|
819
|
+
rims: tyres.map((tyre) => !!tyre?.rim),
|
|
495
820
|
...report.brakes != null ? { brakes: report.brakes } : {},
|
|
496
821
|
...report.engine != null ? { engine: report.engine } : {},
|
|
497
822
|
...report.suspension != null ? { suspension: report.suspension } : {}
|
|
498
823
|
};
|
|
499
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
|
+
}
|
|
500
941
|
export {
|
|
942
|
+
DEFAULT_CONTROL_KEYS,
|
|
943
|
+
DEFAULT_CRUISE,
|
|
944
|
+
DEFAULT_EJECTION,
|
|
945
|
+
DEFAULT_LOADS,
|
|
946
|
+
DEFAULT_PARKING_SENSOR,
|
|
947
|
+
DEFAULT_TRACTION,
|
|
948
|
+
DRIVE_PROFILES,
|
|
501
949
|
bodyDamage,
|
|
950
|
+
bumperDistance,
|
|
502
951
|
conditionFromDamage,
|
|
503
952
|
createArcadeDriver,
|
|
504
953
|
createSemiSimDriver,
|
|
505
954
|
createSurfaces,
|
|
955
|
+
createVehicleControls,
|
|
956
|
+
createVehicleSystems,
|
|
957
|
+
cruiseThrottle,
|
|
506
958
|
defaultArcadeSpec,
|
|
507
959
|
defaultFuelModel,
|
|
508
960
|
defaultSemiSpec,
|
|
509
961
|
defaultSurfaces,
|
|
962
|
+
ejectionOf,
|
|
510
963
|
fuelRate,
|
|
511
964
|
impactEnergy,
|
|
965
|
+
parkingBeepInterval,
|
|
512
966
|
runOver,
|
|
513
967
|
semiSpecFromStats,
|
|
514
968
|
speedLoss,
|
|
515
969
|
surfaceGrip,
|
|
970
|
+
tractionLimit,
|
|
516
971
|
vehicleCorners,
|
|
517
972
|
vehicleDamageFromImpact
|
|
518
973
|
};
|