@adatechnology/game-vehicles-driving 0.2.10 → 0.2.11

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 CHANGED
@@ -402,6 +402,122 @@ interface BodyDimensions {
402
402
  */
403
403
  declare function semiSpecFromStats(stats: DesignStats, body: BodyDimensions, base?: Partial<SemiSimSpec>): SemiSimSpec;
404
404
 
405
+ /**
406
+ * The controls of a vehicle someone drives: pedals, wheel, gearbox (automatic or manual), handbrake (held or pulled), ignition, drive mode (4x2, 4x4,
407
+ * automatic 4x4), lamps (position, low, high, fog, interior), horn, alarm and radio. It turns key codes into a `SemiInput` for the semi driver and keeps
408
+ * what the cabin shows; it draws and plays nothing (an adapter reads `state` for the lamps and the sounds). Every key is an option.
409
+ */
410
+
411
+ type ControlCommand = 'ignition' | 'gearbox' | 'gear_up' | 'gear_down' | 'parking_brake' | 'drive_mode' | 'lights' | 'high_beam' | 'fog_lights' | 'interior_light' | 'alarm' | 'radio' | 'radio_source' | 'exit' | 'indicator_left' | 'indicator_right' | 'hazard' | 'wipers' | 'seatbelt';
412
+ type ControlKeyMap = {
413
+ readonly throttle: readonly string[];
414
+ readonly brake: readonly string[];
415
+ readonly left: readonly string[];
416
+ readonly right: readonly string[];
417
+ readonly handbrake: readonly string[];
418
+ readonly horn: readonly string[];
419
+ } & {
420
+ readonly [command in ControlCommand]: readonly string[];
421
+ };
422
+ /** Keys of a keyboard (`KeyboardEvent.code`). The host keeps them from its own bindings while someone drives. */
423
+ declare const DEFAULT_CONTROL_KEYS: ControlKeyMap;
424
+ type Gearbox = 'automatic' | 'manual';
425
+ type DriveMode = '4x2' | '4x4' | 'auto';
426
+ type LampMode = 'off' | 'position' | 'low' | 'high';
427
+ type AlarmState = 'off' | 'armed' | 'ringing';
428
+ type RadioSource = 'radio' | 'tape';
429
+ type Indicator = 'off' | 'left' | 'right' | 'hazard';
430
+ type Wipers = 'off' | 'slow' | 'fast';
431
+ interface ControlState {
432
+ ignition: boolean;
433
+ gearbox: Gearbox;
434
+ /** The gear the driver chose (manual): -1 reverse, 0 neutral, 1.. forward. */
435
+ gear: number;
436
+ /** Pulled and left pulled (P); Space holds it only while pressed. */
437
+ parkingBrake: boolean;
438
+ driveMode: DriveMode;
439
+ /** The front axle drives now (4x4, or automatic 4x4 that felt the rear slip). */
440
+ fourWheelEngaged: boolean;
441
+ lamps: {
442
+ mode: LampMode;
443
+ fog: boolean;
444
+ interior: boolean;
445
+ };
446
+ /** What the rear lamps show this step: brake (pedal or handbrake) and reversing. */
447
+ brakeLights: boolean;
448
+ reverseLights: boolean;
449
+ horn: boolean;
450
+ alarm: AlarmState;
451
+ radio: {
452
+ on: boolean;
453
+ source: RadioSource;
454
+ };
455
+ indicator: Indicator;
456
+ /** The indicator lamps are lit this instant (they blink at `blinkHz`); `tick` is true on the step the relay clicks. */
457
+ blinkOn: boolean;
458
+ tick: boolean;
459
+ wipers: Wipers;
460
+ seatbelt: boolean;
461
+ }
462
+ type ControlEvent = {
463
+ type: 'command';
464
+ command: ControlCommand;
465
+ state: ControlState;
466
+ } | {
467
+ type: 'refused';
468
+ command: ControlCommand;
469
+ reason: 'no_four_wheel_drive' | 'no_fog_lamps' | 'ignition_off';
470
+ };
471
+ interface VehicleControlOptions {
472
+ /** Forward gears of the gearbox. */
473
+ gears: number;
474
+ /** The vehicle can drive both axles (4x4 and automatic 4x4 are offered). */
475
+ fourWheelDrive?: boolean;
476
+ /** The vehicle has fog lamps. Default true. */
477
+ fogLamps?: boolean;
478
+ keys?: Partial<ControlKeyMap>;
479
+ gearbox?: Gearbox;
480
+ /** Automatic 4x4: the front axle comes in above this slip of a driven wheel (1 = sliding) and goes out after `releaseSeconds` below it. */
481
+ autoFourWheel?: {
482
+ slip: number;
483
+ releaseSeconds: number;
484
+ };
485
+ /** The engine runs from the start (a vehicle the player gets into with the engine on). Default false: the key turns it on (I) or the gas pedal starts it. */
486
+ running?: boolean;
487
+ /** Blinks a second of the indicators (default 1.5, the 90 a minute of the road codes). */
488
+ blinkHz?: number;
489
+ /** The steering wheel coming back this far (0..1 of the lock) after a turn cancels the indicator, as the column does. Default 0.15. */
490
+ indicatorCancel?: number;
491
+ onEvent?: (event: ControlEvent) => void;
492
+ }
493
+ /** What the controls read of the driver each step, and the one thing they change on it (which axles drive). */
494
+ interface ControlledDriver {
495
+ readonly spec: {
496
+ fourWheelDrive: boolean;
497
+ };
498
+ readonly state: {
499
+ u: number;
500
+ gear: number;
501
+ slip: readonly number[];
502
+ engineOn: boolean;
503
+ };
504
+ }
505
+ interface VehicleControls {
506
+ /** A key went down or up. True when it is a driving key (the host keeps it from its own bindings). */
507
+ key(code: string, down: boolean): boolean;
508
+ /** Runs a command as its key would (a button of a panel). */
509
+ command(command: ControlCommand): void;
510
+ /** The input for this step, pedals and wheel eased over `dt` seconds. */
511
+ input(dt: number, driver?: ControlledDriver): SemiInput;
512
+ /** After the driver stepped: the automatic 4x4 watches the slip, the ignition the engine. */
513
+ after(driver: ControlledDriver, dt: number): void;
514
+ readonly state: ControlState;
515
+ readonly keys: ControlKeyMap;
516
+ /** Lets go of every key (the window lost focus, the driver got out). */
517
+ release(): void;
518
+ }
519
+ declare function createVehicleControls(options: VehicleControlOptions): VehicleControls;
520
+
405
521
  /** Matches a damage model's wheels to a driver's: each wheel of the spec takes the health of the tyre nearest to it. */
406
522
 
407
523
  type DamagedTyre = {
@@ -419,4 +535,4 @@ type DamageReport = {
419
535
  };
420
536
  declare function conditionFromDamage(spec: Pick<SemiSimSpec, 'wheels'>, report: DamageReport): VehicleCondition;
421
537
 
422
- export { type ArcadeOptions, type ArcadeSpec, type BodyDimensions, type CrashRules, type DamageReport, type DamagedTyre, type DesignStats, type DriveInput, type Driver, type DriverState, type DrivingBody, type DrivingEvent, type DrivingWorld, type FuelModel, type RunOverOutcome, type RunOverRules, type SemiDriver, type SemiEvent, type SemiGround, type SemiInput, type SemiSimOptions, type SemiSimSpec, type SemiState, type SemiWheel, type SemiWorld, type StepResult, type Surface, type SurfaceOptions, type SurfaceRegistry, type VehicleCondition, bodyDamage, conditionFromDamage, createArcadeDriver, createSemiSimDriver, createSurfaces, defaultArcadeSpec, defaultFuelModel, defaultSemiSpec, defaultSurfaces, fuelRate, impactEnergy, runOver, semiSpecFromStats, speedLoss, surfaceGrip, vehicleCorners, vehicleDamageFromImpact };
538
+ export { type AlarmState, type ArcadeOptions, type ArcadeSpec, type BodyDimensions, type ControlCommand, type ControlEvent, type ControlKeyMap, type ControlState, type ControlledDriver, type CrashRules, DEFAULT_CONTROL_KEYS, type DamageReport, type DamagedTyre, type DesignStats, type DriveInput, type DriveMode, type Driver, type DriverState, type DrivingBody, type DrivingEvent, type DrivingWorld, type FuelModel, type Gearbox, type Indicator, type LampMode, type RadioSource, type RunOverOutcome, type RunOverRules, type SemiDriver, type SemiEvent, type SemiGround, type SemiInput, type SemiSimOptions, type SemiSimSpec, type SemiState, type SemiWheel, type SemiWorld, type StepResult, type Surface, type SurfaceOptions, type SurfaceRegistry, type VehicleCondition, type VehicleControlOptions, type VehicleControls, type Wipers, bodyDamage, conditionFromDamage, createArcadeDriver, createSemiSimDriver, createSurfaces, createVehicleControls, defaultArcadeSpec, defaultFuelModel, defaultSemiSpec, defaultSurfaces, fuelRate, impactEnergy, runOver, semiSpecFromStats, speedLoss, surfaceGrip, vehicleCorners, vehicleDamageFromImpact };
@@ -21,11 +21,13 @@ var GameVehiclesDriving = (() => {
21
21
  // src/index.ts
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
+ DEFAULT_CONTROL_KEYS: () => DEFAULT_CONTROL_KEYS,
24
25
  bodyDamage: () => bodyDamage,
25
26
  conditionFromDamage: () => conditionFromDamage,
26
27
  createArcadeDriver: () => createArcadeDriver,
27
28
  createSemiSimDriver: () => createSemiSimDriver,
28
29
  createSurfaces: () => createSurfaces,
30
+ createVehicleControls: () => createVehicleControls,
29
31
  defaultArcadeSpec: () => defaultArcadeSpec,
30
32
  defaultFuelModel: () => defaultFuelModel,
31
33
  defaultSemiSpec: () => defaultSemiSpec,
@@ -176,6 +178,7 @@ var GameVehiclesDriving = (() => {
176
178
  }
177
179
 
178
180
  // ../game-contracts/dist/index.js
181
+ var DEFAULT_BLAST_IMPULSE = { bodyArea: 0.7, lift: 0.15, minSpeed: 0.3, tntPerJoule: 0.24 / 2e5 };
179
182
  var DEFAULT_COST_ALLOCATION_BYTES = 1024 * 1024;
180
183
  var DEFERRED_QUEUE_ID = {
181
184
  DESTRUCTION_EVENTS: "destruction.events",
@@ -617,6 +620,207 @@ var GameVehiclesDriving = (() => {
617
620
  return { ...spec, mass: body.mass, wheels, fourWheelDrive: driveAll, engine, gears, drag, brakeForce, tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling } };
618
621
  }
619
622
 
623
+ // src/controls.ts
624
+ var DEFAULT_CONTROL_KEYS = {
625
+ throttle: ["KeyW", "ArrowUp"],
626
+ brake: ["KeyS", "ArrowDown"],
627
+ left: ["KeyA", "ArrowLeft"],
628
+ right: ["KeyD", "ArrowRight"],
629
+ handbrake: ["Space"],
630
+ horn: ["KeyH"],
631
+ ignition: ["KeyI"],
632
+ gearbox: ["KeyM"],
633
+ gear_up: ["KeyE"],
634
+ gear_down: ["KeyQ"],
635
+ parking_brake: ["KeyP"],
636
+ drive_mode: ["KeyG"],
637
+ lights: ["KeyL"],
638
+ high_beam: ["KeyK"],
639
+ fog_lights: ["KeyN"],
640
+ interior_light: ["KeyU"],
641
+ alarm: ["KeyY"],
642
+ radio: ["KeyR"],
643
+ radio_source: ["KeyT"],
644
+ exit: ["KeyF"],
645
+ indicator_left: ["Comma"],
646
+ indicator_right: ["Period"],
647
+ hazard: ["KeyB"],
648
+ wipers: ["KeyO"],
649
+ seatbelt: ["KeyJ"]
650
+ };
651
+ function createVehicleControls(options) {
652
+ const keys = { ...DEFAULT_CONTROL_KEYS, ...options.keys };
653
+ const auto4 = options.autoFourWheel ?? { slip: 0.18, releaseSeconds: 2 };
654
+ const hasFog = options.fogLamps !== false;
655
+ const held = /* @__PURE__ */ new Set();
656
+ const any = (codes) => codes.some((c) => held.has(c));
657
+ const commandOf = /* @__PURE__ */ new Map();
658
+ for (const [command2, codes] of Object.entries(keys)) if (!["throttle", "brake", "left", "right", "handbrake", "horn"].includes(command2)) for (const code of codes) commandOf.set(code, command2);
659
+ const driving = new Set(Object.values(keys).flat());
660
+ const state = {
661
+ ignition: !!options.running,
662
+ gearbox: options.gearbox ?? "automatic",
663
+ gear: 1,
664
+ parkingBrake: false,
665
+ driveMode: options.fourWheelDrive ? "auto" : "4x2",
666
+ fourWheelEngaged: false,
667
+ lamps: { mode: "off", fog: false, interior: false },
668
+ brakeLights: false,
669
+ reverseLights: false,
670
+ horn: false,
671
+ alarm: "off",
672
+ radio: { on: false, source: "radio" },
673
+ indicator: "off",
674
+ blinkOn: false,
675
+ tick: false,
676
+ wipers: "off",
677
+ seatbelt: false
678
+ };
679
+ const blinkHz = options.blinkHz ?? 1.5;
680
+ const cancelAt = options.indicatorCancel ?? 0.15;
681
+ let blink = 0;
682
+ let turned = 0;
683
+ let throttle = 0;
684
+ let brake = 0;
685
+ let steer = 0;
686
+ let calm = 0;
687
+ const emit = (event) => options.onEvent?.(event);
688
+ const LAMPS = ["off", "position", "low"];
689
+ const MODES = ["4x2", "4x4", "auto"];
690
+ const command = (c) => {
691
+ switch (c) {
692
+ case "ignition":
693
+ state.ignition = !state.ignition;
694
+ if (state.ignition && state.alarm !== "off") state.alarm = "off";
695
+ break;
696
+ case "gearbox":
697
+ state.gearbox = state.gearbox === "automatic" ? "manual" : "automatic";
698
+ state.gear = Math.max(1, state.gear);
699
+ break;
700
+ case "gear_up":
701
+ case "gear_down":
702
+ if (state.gearbox !== "manual") return;
703
+ state.gear = Math.max(-1, Math.min(options.gears, state.gear + (c === "gear_up" ? 1 : -1)));
704
+ break;
705
+ case "parking_brake":
706
+ state.parkingBrake = !state.parkingBrake;
707
+ break;
708
+ case "drive_mode": {
709
+ if (!options.fourWheelDrive) return emit({ type: "refused", command: c, reason: "no_four_wheel_drive" });
710
+ state.driveMode = MODES[(MODES.indexOf(state.driveMode) + 1) % MODES.length];
711
+ state.fourWheelEngaged = state.driveMode === "4x4";
712
+ break;
713
+ }
714
+ case "lights":
715
+ state.lamps.mode = state.lamps.mode === "high" ? "off" : LAMPS[(LAMPS.indexOf(state.lamps.mode) + 1) % LAMPS.length];
716
+ break;
717
+ case "high_beam":
718
+ state.lamps.mode = state.lamps.mode === "high" ? "low" : "high";
719
+ break;
720
+ case "fog_lights":
721
+ if (!hasFog) return emit({ type: "refused", command: c, reason: "no_fog_lamps" });
722
+ state.lamps.fog = !state.lamps.fog;
723
+ if (state.lamps.fog && state.lamps.mode === "off") state.lamps.mode = "position";
724
+ break;
725
+ case "interior_light":
726
+ state.lamps.interior = !state.lamps.interior;
727
+ break;
728
+ case "alarm":
729
+ state.alarm = state.alarm === "off" ? "armed" : state.alarm === "armed" ? "ringing" : "off";
730
+ break;
731
+ case "radio":
732
+ state.radio.on = !state.radio.on;
733
+ break;
734
+ case "radio_source":
735
+ state.radio.source = state.radio.source === "radio" ? "tape" : "radio";
736
+ break;
737
+ case "exit":
738
+ break;
739
+ case "indicator_left":
740
+ case "indicator_right": {
741
+ const side = c === "indicator_left" ? "left" : "right";
742
+ if (state.indicator === "hazard") return;
743
+ state.indicator = state.indicator === side ? "off" : side;
744
+ turned = 0;
745
+ blink = 0;
746
+ break;
747
+ }
748
+ case "hazard":
749
+ state.indicator = state.indicator === "hazard" ? "off" : "hazard";
750
+ blink = 0;
751
+ break;
752
+ case "wipers":
753
+ state.wipers = state.wipers === "off" ? "slow" : state.wipers === "slow" ? "fast" : "off";
754
+ break;
755
+ case "seatbelt":
756
+ state.seatbelt = !state.seatbelt;
757
+ break;
758
+ }
759
+ emit({ type: "command", command: c, state });
760
+ };
761
+ const ease = (from, to, rate, dt) => from + Math.sign(to - from) * Math.min(Math.abs(to - from), rate * dt);
762
+ return {
763
+ keys,
764
+ state,
765
+ key(code, down) {
766
+ if (!driving.has(code)) return false;
767
+ const was = held.has(code);
768
+ if (down) held.add(code);
769
+ else held.delete(code);
770
+ const c = commandOf.get(code);
771
+ if (c && down && !was) command(c);
772
+ return true;
773
+ },
774
+ command,
775
+ input(dt, driver) {
776
+ const gas = any(keys.throttle) ? 1 : 0;
777
+ const stop = any(keys.brake) ? 1 : 0;
778
+ const wheel = (any(keys.left) ? 1 : 0) - (any(keys.right) ? 1 : 0);
779
+ throttle = ease(throttle, gas, gas > throttle ? 4 : 8, dt);
780
+ brake = ease(brake, stop, stop > brake ? 6 : 10, dt);
781
+ steer = ease(steer, wheel, wheel !== 0 && Math.sign(wheel) === Math.sign(steer || wheel) ? 3 : 6, dt);
782
+ const handbrake = state.parkingBrake || any(keys.handbrake);
783
+ state.tick = false;
784
+ if (state.indicator !== "off") {
785
+ const before = Math.floor(blink * blinkHz * 2);
786
+ blink += dt;
787
+ const now = Math.floor(blink * blinkHz * 2);
788
+ state.blinkOn = now % 2 === 0;
789
+ state.tick = now !== before;
790
+ if (state.indicator !== "hazard") {
791
+ const way = state.indicator === "left" ? steer : -steer;
792
+ if (way > 0.5) turned = 1;
793
+ else if (turned && way < cancelAt) state.indicator = "off";
794
+ }
795
+ } else state.blinkOn = false;
796
+ state.horn = any(keys.horn);
797
+ const gasNow = state.ignition ? throttle : 0;
798
+ const reversing = state.gearbox === "manual" ? state.gear < 0 : (driver?.state.gear ?? 1) < 0;
799
+ state.reverseLights = reversing;
800
+ state.brakeLights = handbrake || (reversing ? gasNow > 0.05 && (driver?.state.u ?? 0) < -0.3 : brake > 0.05);
801
+ if (state.gearbox === "automatic") return { throttle: gasNow, brake, steer, handbrake, horn: state.horn };
802
+ return state.gear < 0 ? { throttle: brake, brake: gasNow, steer, handbrake, horn: state.horn, gear: state.gear } : { throttle: gasNow, brake, steer, handbrake, horn: state.horn, gear: state.gear };
803
+ },
804
+ after(driver, dt) {
805
+ if (driver.state.engineOn && !state.ignition && driver.state.u !== 0) state.ignition = true;
806
+ if (!state.ignition && driver.state.engineOn) driver.state.engineOn = false;
807
+ if (state.driveMode === "auto" && options.fourWheelDrive) {
808
+ const slip = Math.max(0, ...driver.state.slip);
809
+ if (slip > auto4.slip) {
810
+ state.fourWheelEngaged = true;
811
+ calm = 0;
812
+ } else if (state.fourWheelEngaged && (calm += dt) > auto4.releaseSeconds) state.fourWheelEngaged = false;
813
+ } else state.fourWheelEngaged = state.driveMode === "4x4";
814
+ driver.spec.fourWheelDrive = !!options.fourWheelDrive && state.fourWheelEngaged;
815
+ },
816
+ release() {
817
+ held.clear();
818
+ throttle = brake = steer = 0;
819
+ state.horn = false;
820
+ }
821
+ };
822
+ }
823
+
620
824
  // src/semi/condition.ts
621
825
  function conditionFromDamage(spec, report) {
622
826
  const tyres = spec.wheels.map((wheel) => {