@inventure71/paddockjs 0.3.0 → 2.0.0

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.
Files changed (202) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/INSTALL_AND_UPDATE.md +5 -2
  3. package/README.md +328 -13
  4. package/docs/architecture.md +150 -41
  5. package/docs/concepts.md +22 -3
  6. package/docs/custom_model_controller.md +329 -0
  7. package/docs/data_contract.md +474 -34
  8. package/docs/data_model.md +1 -1
  9. package/docs/index.md +1 -0
  10. package/docs/learnings.md +23 -1
  11. package/docs/rules.md +169 -21
  12. package/docs/system_specs.md +56 -20
  13. package/docs/training.md +369 -25
  14. package/examples/python/README.md +62 -0
  15. package/examples/python/base_policy_server.py +191 -0
  16. package/examples/python/requirements.txt +2 -0
  17. package/examples/train-basic-policy.mjs +5 -6
  18. package/examples/trainingData.mjs +133 -0
  19. package/package.json +15 -4
  20. package/src/api/PaddockSimulatorController.js +36 -2
  21. package/src/app/BrowserExpertAdapter.js +367 -9
  22. package/src/app/F1SimulatorApp.js +615 -940
  23. package/src/app/banners/bannerFormatters.js +55 -0
  24. package/src/app/banners/raceDataBannerController.js +141 -0
  25. package/src/app/banners/stewardMessageController.js +99 -0
  26. package/src/app/camera/cameraConstants.js +21 -0
  27. package/src/app/camera/cameraController.js +265 -0
  28. package/src/app/camera/cameraGeometry.js +176 -0
  29. package/src/app/domBindings.js +14 -1
  30. package/src/app/readouts/carOverviewRenderer.js +109 -0
  31. package/src/app/readouts/controlStateRenderer.js +37 -0
  32. package/src/app/readouts/raceStatusRenderer.js +133 -0
  33. package/src/app/readouts/readoutFormatters.js +58 -0
  34. package/src/app/readouts/telemetryRenderer.js +113 -0
  35. package/src/app/readouts/timingTowerRenderer.js +177 -0
  36. package/src/app/rendering/appAssets.js +38 -0
  37. package/src/app/rendering/carRenderer.js +286 -0
  38. package/src/app/rendering/displayUtils.js +22 -0
  39. package/src/app/rendering/drsTrailRenderer.js +90 -0
  40. package/src/app/rendering/expertSensorRenderer.js +126 -0
  41. package/src/app/rendering/pitLaneStatusRenderer.js +50 -0
  42. package/src/app/rendering/replayGhostRenderer.js +124 -0
  43. package/src/app/rendering/trackRenderer.js +25 -0
  44. package/src/app/runtime/frameLoop.js +66 -0
  45. package/src/app/runtime/runtimeTiming.js +34 -0
  46. package/src/app/runtime/runtimeVisibility.js +45 -0
  47. package/src/config/defaultOptions.js +24 -4
  48. package/src/environment/actions.js +38 -7
  49. package/src/environment/controllerLoop.js +246 -0
  50. package/src/environment/episode.js +86 -2
  51. package/src/environment/evaluation.js +206 -0
  52. package/src/environment/index.d.ts +638 -6
  53. package/src/environment/index.js +14 -0
  54. package/src/environment/metrics.js +159 -0
  55. package/src/environment/observationOptions.js +33 -0
  56. package/src/environment/observations.js +500 -43
  57. package/src/environment/options.js +41 -2
  58. package/src/environment/recorder.js +28 -0
  59. package/src/environment/runtime.js +260 -24
  60. package/src/environment/scenarios.js +249 -0
  61. package/src/environment/sensors/bodySenses.js +23 -0
  62. package/src/environment/sensors/boundarySenses.js +17 -0
  63. package/src/environment/sensors/carRays.js +75 -0
  64. package/src/environment/sensors/contactSenses.js +32 -0
  65. package/src/environment/sensors/index.js +157 -0
  66. package/src/environment/sensors/indexedRayBands.js +103 -0
  67. package/src/environment/sensors/nearbyCars.js +52 -0
  68. package/src/environment/sensors/opponentRadar.js +65 -0
  69. package/src/environment/sensors/rayConfig.js +110 -0
  70. package/src/environment/sensors/rayDefaults.js +6 -0
  71. package/src/environment/sensors/rayGeometry.js +60 -0
  72. package/src/environment/sensors/rayGuards.js +19 -0
  73. package/src/environment/sensors/sensorTargets.js +60 -0
  74. package/src/environment/sensors/surfaceRays.js +296 -0
  75. package/src/environment/sensors/trackRays.js +261 -0
  76. package/src/environment/sensors.js +1 -257
  77. package/src/environment/specs.js +188 -9
  78. package/src/environment/workerProtocol.js +64 -0
  79. package/src/index.d.ts +533 -7
  80. package/src/index.js +26 -0
  81. package/src/rendering/proceduralTrackAsset.js +20 -510
  82. package/src/rendering/renderSnapshot.js +38 -12
  83. package/src/rendering/track/finishLineRenderer.js +34 -0
  84. package/src/rendering/track/gridRenderer.js +47 -0
  85. package/src/rendering/track/offsetStrokeSafety.js +136 -0
  86. package/src/rendering/track/pitLaneRenderer.js +248 -0
  87. package/src/rendering/track/trackMaterialRenderer.js +326 -0
  88. package/src/rendering/track/trackRenderConstants.js +38 -0
  89. package/src/rendering/track/trackRenderGeometry.js +94 -0
  90. package/src/simulation/collisionGeometry.js +214 -0
  91. package/src/simulation/driver/driverControlConstants.js +28 -0
  92. package/src/simulation/driver/driverController.js +47 -0
  93. package/src/simulation/driver/driverInput.js +28 -0
  94. package/src/simulation/driver/driverMath.js +6 -0
  95. package/src/simulation/driver/driverPersonality.js +25 -0
  96. package/src/simulation/driver/edgeRecovery.js +48 -0
  97. package/src/simulation/driver/racingControls.js +399 -0
  98. package/src/simulation/driver/racingLinePlan.js +166 -0
  99. package/src/simulation/driver/recoveryDynamics.js +38 -0
  100. package/src/simulation/driver/rejoinControls.js +213 -0
  101. package/src/simulation/driver/safetyCarControls.js +35 -0
  102. package/src/simulation/driver/trafficScan.js +51 -0
  103. package/src/simulation/driverController.js +4 -253
  104. package/src/simulation/participants/participantInteractions.js +120 -0
  105. package/src/simulation/pit/pitFlow.js +234 -0
  106. package/src/simulation/pit/pitIntent.js +92 -0
  107. package/src/simulation/pit/pitOccupancy.js +47 -0
  108. package/src/simulation/pit/pitPenaltyService.js +71 -0
  109. package/src/simulation/pit/pitQueue.js +55 -0
  110. package/src/simulation/pit/pitRouteMovement.js +160 -0
  111. package/src/simulation/pit/pitRouting.js +319 -0
  112. package/src/simulation/pit/pitService.js +41 -0
  113. package/src/simulation/pit/pitServiceConstants.js +21 -0
  114. package/src/simulation/pit/pitServiceProfile.js +45 -0
  115. package/src/simulation/pit/pitSnapshots.js +61 -0
  116. package/src/simulation/pit/pitState.js +113 -0
  117. package/src/simulation/pit/pitTireService.js +92 -0
  118. package/src/simulation/race/classification.js +104 -0
  119. package/src/simulation/race/gridPenalties.js +37 -0
  120. package/src/simulation/race/raceControlState.js +38 -0
  121. package/src/simulation/race/raceDistance.js +32 -0
  122. package/src/simulation/race/raceFinish.js +65 -0
  123. package/src/simulation/race/raceLifecycle.js +71 -0
  124. package/src/simulation/race/raceOrder.js +113 -0
  125. package/src/simulation/race/raceProgress.js +124 -0
  126. package/src/simulation/race/raceSetup.js +93 -0
  127. package/src/simulation/race/raceStep.js +80 -0
  128. package/src/simulation/race/redFlag.js +15 -0
  129. package/src/simulation/race/retirements.js +44 -0
  130. package/src/simulation/race/safetyCar.js +86 -0
  131. package/src/simulation/race/stalledDnf.js +86 -0
  132. package/src/simulation/raceSimulation.js +386 -1012
  133. package/src/simulation/replay/replayGhosts.js +128 -0
  134. package/src/simulation/rules/collisionSteward.js +69 -0
  135. package/src/simulation/rules/moduleConfig.js +72 -0
  136. package/src/simulation/rules/penaltyConfig.js +99 -0
  137. package/src/simulation/rules/penaltyEvents.js +18 -0
  138. package/src/simulation/rules/penaltyLedger.js +153 -0
  139. package/src/simulation/rules/penaltyStats.js +26 -0
  140. package/src/simulation/rules/pitLaneSpeedingSteward.js +71 -0
  141. package/src/simulation/rules/ruleConfigMerge.js +41 -0
  142. package/src/simulation/rules/ruleDefaults.js +85 -0
  143. package/src/simulation/rules/rulesReview.js +141 -0
  144. package/src/simulation/rules/rulesetPresets.js +47 -0
  145. package/src/simulation/rules/tireRequirementSteward.js +29 -0
  146. package/src/simulation/rules/trackLimitsSteward.js +85 -0
  147. package/src/simulation/rulesConfig.js +34 -0
  148. package/src/simulation/snapshots/raceSnapshots.js +192 -0
  149. package/src/simulation/timing/drsTiming.js +87 -0
  150. package/src/simulation/timing/gapEstimation.js +38 -0
  151. package/src/simulation/timing/lapTelemetry.js +182 -0
  152. package/src/simulation/timing/raceTiming.js +15 -0
  153. package/src/simulation/timing/sectorPerformance.js +60 -0
  154. package/src/simulation/timing/timingConstants.js +5 -0
  155. package/src/simulation/timing/timingHistory.js +46 -0
  156. package/src/simulation/timing/timingLines.js +79 -0
  157. package/src/simulation/track/centerlineSpline.js +59 -0
  158. package/src/simulation/track/drsZones.js +67 -0
  159. package/src/simulation/track/pitLaneAccess.js +103 -0
  160. package/src/simulation/track/pitLaneLayout.js +395 -0
  161. package/src/simulation/track/pitLaneState.js +160 -0
  162. package/src/simulation/track/proceduralCenterline.js +237 -0
  163. package/src/simulation/track/sampleGeometry.js +49 -0
  164. package/src/simulation/track/spatialQueries.js +154 -0
  165. package/src/simulation/track/startStraight.js +141 -0
  166. package/src/simulation/track/trackConstants.js +109 -0
  167. package/src/simulation/track/trackGenerationOptions.js +283 -0
  168. package/src/simulation/track/trackMath.js +241 -0
  169. package/src/simulation/track/trackModel.js +240 -0
  170. package/src/simulation/track/trackQueryIndex.js +945 -0
  171. package/src/simulation/track/trackSectors.js +19 -0
  172. package/src/simulation/track/trackStatePolicy.js +24 -0
  173. package/src/simulation/track/trackValidation.js +98 -0
  174. package/src/simulation/trackModel.js +1 -628
  175. package/src/simulation/units.js +6 -4
  176. package/src/simulation/vehicle/contactResolution.js +116 -0
  177. package/src/simulation/vehicle/mainTrackWheelSurface.js +82 -0
  178. package/src/simulation/vehicle/pitWheelSurface.js +69 -0
  179. package/src/simulation/vehicle/runoffResponse.js +88 -0
  180. package/src/simulation/vehicle/surfacePriority.js +28 -0
  181. package/src/simulation/vehicle/vehicleGeometry.js +247 -0
  182. package/src/simulation/vehicle/vehiclePhysics.js +363 -0
  183. package/src/simulation/vehicle/vehicleSnapshots.js +316 -0
  184. package/src/simulation/vehicle/vehicleState.js +186 -0
  185. package/src/simulation/vehicle/wheelSurface.js +150 -0
  186. package/src/simulation/vehicleGeometry.js +1 -0
  187. package/src/simulation/vehiclePhysics.js +1 -104
  188. package/src/simulation/warmup/runtimeWarmup.js +224 -0
  189. package/src/simulation/wheelSurface.js +1 -0
  190. package/src/styles.css +342 -42
  191. package/src/ui/bannerTemplates.js +69 -0
  192. package/src/ui/cameraControlsTemplate.js +20 -0
  193. package/src/ui/carOverviewTemplate.js +33 -0
  194. package/src/ui/componentTemplates.js +20 -426
  195. package/src/ui/raceCanvasTemplate.js +69 -0
  196. package/src/ui/raceControlStatusBanner.js +39 -0
  197. package/src/ui/raceControlsTemplate.js +36 -0
  198. package/src/ui/shellTemplate.js +6 -5
  199. package/src/ui/telemetryDrawerTemplate.js +55 -0
  200. package/src/ui/telemetryTemplates.js +136 -0
  201. package/src/ui/templateUtils.js +23 -0
  202. package/src/ui/timingTowerTemplate.js +34 -0
@@ -0,0 +1,47 @@
1
+ import { REJOIN_HOLD_FRAMES } from './driverControlConstants.js';
2
+ import { createDriverInput } from './driverInput.js';
3
+ import { decideRacingControls } from './racingControls.js';
4
+ import { decideRejoinControls } from './rejoinControls.js';
5
+ import { shouldContinueRejoinRecovery } from './edgeRecovery.js';
6
+ import { decideSafetyCarControls } from './safetyCarControls.js';
7
+ import { VEHICLE_LIMITS } from '../vehicle/vehiclePhysics.js';
8
+
9
+ export function decideDriverControls({ car, orderIndex, race }) {
10
+ if (car.manualControls) return car.manualControls;
11
+
12
+ if (car.gridLocked) {
13
+ return createDriverInput().brake(1).controls();
14
+ }
15
+
16
+ if (race.safetyCar.deployed) {
17
+ return decideSafetyCarControls(car, orderIndex, race);
18
+ }
19
+
20
+ if (!car.trackState.onTrack) {
21
+ car.rejoinRecoveryFrames = REJOIN_HOLD_FRAMES;
22
+ car.attackCommitmentFrames = 0;
23
+ return decideRejoinControls(car, race);
24
+ }
25
+
26
+ if (shouldStabilizeSimulatorCar(car, race)) {
27
+ car.rejoinRecoveryFrames = Math.max(car.rejoinRecoveryFrames ?? 0, Math.floor(REJOIN_HOLD_FRAMES * 0.45));
28
+ car.attackCommitmentFrames = 0;
29
+ return decideRejoinControls(car, race);
30
+ }
31
+
32
+ if (shouldContinueRejoinRecovery(car, race)) {
33
+ return decideRejoinControls(car, race);
34
+ }
35
+
36
+ return decideRacingControls(car, orderIndex, race);
37
+ }
38
+
39
+ function shouldStabilizeSimulatorCar(car, race) {
40
+ if (race.physicsMode !== 'simulator') return false;
41
+ const gripUsage = car.gripUsage ?? 0;
42
+ const slipAngle = Math.abs(car.slipAngleRadians ?? 0);
43
+ const edgeDistance = race.track.width / 2 - VEHICLE_LIMITS.carWidth * 2.25;
44
+ const nearEdge = (car.trackState?.crossTrackError ?? 0) > edgeDistance;
45
+ const unstable = car.stabilityState === 'spin-risk' || gripUsage > 1.25 || slipAngle > 0.24;
46
+ return unstable && (nearEdge || slipAngle > 0.34 || gripUsage > 1.7);
47
+ }
@@ -0,0 +1,28 @@
1
+ import { clamp } from '../simMath.js';
2
+ import { VEHICLE_LIMITS } from '../vehicle/vehiclePhysics.js';
3
+
4
+ export function createDriverInput() {
5
+ const state = {
6
+ steering: 0,
7
+ throttle: 0,
8
+ brake: 0,
9
+ };
10
+
11
+ return {
12
+ steer(amount) {
13
+ state.steering = clamp(amount, -VEHICLE_LIMITS.maxSteer, VEHICLE_LIMITS.maxSteer);
14
+ return this;
15
+ },
16
+ accelerate(amount) {
17
+ state.throttle = clamp(amount, 0, 1);
18
+ return this;
19
+ },
20
+ brake(amount) {
21
+ state.brake = clamp(amount, 0, 1);
22
+ return this;
23
+ },
24
+ controls() {
25
+ return { ...state };
26
+ },
27
+ };
28
+ }
@@ -0,0 +1,6 @@
1
+ import { normalizeAngle } from '../simMath.js';
2
+
3
+ export function angleToPoint(car, target) {
4
+ const angle = Math.atan2(target.y - car.y, target.x - car.x);
5
+ return normalizeAngle(angle - car.heading);
6
+ }
@@ -0,0 +1,25 @@
1
+ import { clamp, seededRange } from '../simMath.js';
2
+
3
+ export function buildDriverPersonality(driver, index, racecraft, random) {
4
+ const numberSeed = Number(driver.driverNumber ?? index + 1);
5
+ const numberBias = (((Number.isFinite(numberSeed) ? numberSeed : index + 1) % 11) - 5) * 0.014;
6
+ const baseAggression = clamp(
7
+ driver.personality?.aggression ?? 0.36 + racecraft * 0.34 + numberBias + seededRange(random, -0.045, 0.045),
8
+ 0.18,
9
+ 0.88,
10
+ );
11
+
12
+ return {
13
+ baseAggression,
14
+ riskTolerance: clamp(
15
+ driver.personality?.riskTolerance ?? baseAggression * 0.72 + racecraft * 0.24 + seededRange(random, -0.04, 0.04),
16
+ 0.12,
17
+ 0.95,
18
+ ),
19
+ patience: clamp(
20
+ driver.personality?.patience ?? 0.72 - baseAggression * 0.36 + racecraft * 0.12 + seededRange(random, -0.05, 0.05),
21
+ 0.18,
22
+ 0.9,
23
+ ),
24
+ };
25
+ }
@@ -0,0 +1,48 @@
1
+ import { clamp, normalizeAngle } from '../simMath.js';
2
+ import { metersToSimUnits } from '../units.js';
3
+ import { pointAt } from '../track/trackModel.js';
4
+ import { VEHICLE_LIMITS } from '../vehicle/vehiclePhysics.js';
5
+ import { REJOIN_LOOKAHEAD_BASE, REJOIN_LOOKAHEAD_MAX } from './driverControlConstants.js';
6
+
7
+ export function calculateTrackEdgeGuard(car, race) {
8
+ const simulatorMode = race.physicsMode === 'simulator';
9
+ const trackLimit = race.track.width / 2;
10
+ const halfCarWidth = VEHICLE_LIMITS.carWidth / 2;
11
+ const wholeCarOutsideLimit = trackLimit + halfCarWidth;
12
+ const softLimit = trackLimit - halfCarWidth * (simulatorMode ? 3.0 : 1.9);
13
+ const recoveryOffset = trackLimit - halfCarWidth * (simulatorMode ? 2.2 : 1.62);
14
+ const crossTrackError = car.trackState?.crossTrackError ?? 0;
15
+ const side = Math.sign(car.trackState?.signedOffset ?? 0);
16
+ const pressure = clamp(
17
+ (crossTrackError - softLimit) / Math.max(1, wholeCarOutsideLimit - softLimit),
18
+ 0,
19
+ 1,
20
+ );
21
+ const overLimitPressure = clamp(
22
+ (crossTrackError - wholeCarOutsideLimit) / Math.max(1, (race.track.kerbWidth ?? 0) + halfCarWidth),
23
+ 0,
24
+ 1,
25
+ );
26
+
27
+ return {
28
+ softLimit,
29
+ recoveryOffset,
30
+ pressure,
31
+ overLimitPressure,
32
+ side,
33
+ };
34
+ }
35
+
36
+ export function shouldContinueRejoinRecovery(car, race) {
37
+ const frames = Math.max(0, Math.floor(car.rejoinRecoveryFrames ?? 0));
38
+ if (frames <= 0) return false;
39
+
40
+ const lookahead = clamp(car.speed * 0.66 + REJOIN_LOOKAHEAD_BASE, REJOIN_LOOKAHEAD_BASE, REJOIN_LOOKAHEAD_MAX);
41
+ const targetBase = pointAt(race.track, car.progress + lookahead);
42
+ const headingError = Math.abs(normalizeAngle(targetBase.heading - car.heading));
43
+ const edgeBuffer = race.track.width / 2 - VEHICLE_LIMITS.carWidth * 1.35;
44
+ const stillUnsettled = car.trackState.crossTrackError > edgeBuffer || headingError > 0.34;
45
+
46
+ car.rejoinRecoveryFrames = stillUnsettled ? frames - 1 : 0;
47
+ return stillUnsettled;
48
+ }
@@ -0,0 +1,399 @@
1
+ import { clamp, normalizeAngle } from '../simMath.js';
2
+ import { kphToSimSpeed, metersToSimUnits, simUnitsToMeters } from '../units.js';
3
+ import { offsetTrackPoint, pointAt } from '../track/trackModel.js';
4
+ import { VEHICLE_LIMITS } from '../vehicle/vehiclePhysics.js';
5
+ import { BRAKING_LOOKAHEAD_MAX_DISTANCE, CURVATURE_LOOKAHEAD_SAMPLES, EDGE_RECOVERY_MIN_LOOKAHEAD, LOOKAHEAD_BASE_DISTANCE, LOOKAHEAD_MAX_DISTANCE, OVERTAKE_LATERAL_MIN, PREVIEW_CURVATURE_CAP, TRAFFIC_GAP_AHEAD, TRAFFIC_SIDE_GAP } from './driverControlConstants.js';
6
+ import { createDriverInput } from './driverInput.js';
7
+ import { angleToPoint } from './driverMath.js';
8
+ import { calculateTrackEdgeGuard } from './edgeRecovery.js';
9
+ import { calculateActualOverlapPenalty, calculatePlannedTrafficPenalty, planRacingLine } from './racingLinePlan.js';
10
+ import { analyzeTrackEdgeMotion } from './recoveryDynamics.js';
11
+
12
+ export function decideRacingControls(car, orderIndex, race) {
13
+ if (race.physicsMode === 'simulator') {
14
+ return decideSimulatorRacingControls(car, orderIndex, race);
15
+ }
16
+ return decideArcadeRacingControls(car, orderIndex, race);
17
+ }
18
+
19
+ export function decideArcadeRacingControls(car, orderIndex, race) {
20
+ return decideRacingControlsForMode(car, orderIndex, race, {
21
+ simulatorMode: false,
22
+ baseLookaheadSpeedFactor: (aggression) => 1.62 - aggression * 0.12,
23
+ previewLookaheadSpeedFactor: 3.15,
24
+ edgeLookaheadPenaltyMeters: 18,
25
+ cornerPressureCurvature: 0.001,
26
+ edgeLineDamping: 0.8,
27
+ edgeRecoveryWeight: 0.8,
28
+ headingFeedForwardBase: (carEntry, edgeGuard) => 0.34 + carEntry.racecraft * 0.18 + edgeGuard.pressure * 0.12,
29
+ headingFeedForwardLimit: 0.42,
30
+ pathSteerGain: (carEntry, edgeGuard) => 0.72 + carEntry.racecraft * 0.12 + edgeGuard.pressure * 0.42,
31
+ edgeRecoveryStart: 0.22,
32
+ edgeRecoveryGain: 1.18,
33
+ edgeRecoveryOverLimitGain: 0.55,
34
+ edgeRecoveryLimit: 0.52,
35
+ steeringLimitScale: () => 1,
36
+ gripBudget: (carEntry, aggression) => 14.8 + carEntry.racecraft * 3.4 + (carEntry.tireEnergy ?? 100) * 0.011 + aggression * 1.8,
37
+ cornerSpeedMultiplier: (aggression) => 1.12 + aggression * 0.08,
38
+ paceBonus: (carEntry) => (carEntry.pace - 1) * 20,
39
+ aggressionSpeedBonus: (aggression) => aggression * 10,
40
+ minCornerSpeedKph: 96,
41
+ maxCornerSpeedKph: 318,
42
+ edgePenaltyMetersScale: (aggression) => 5.8 - aggression * 1.1,
43
+ edgePenaltyPressure: 48,
44
+ edgePressureSpeedPenalty: 0,
45
+ edgeOutwardSpeedPenalty: 0,
46
+ edgeHeadingOutwardPenalty: 0,
47
+ steeringPenaltyStart: 0.38,
48
+ steeringPenaltyGain: 24,
49
+ steeringPenaltyMax: 12,
50
+ headingPenaltyStart: 0.48,
51
+ headingPenaltyGain: 42,
52
+ headingPenaltyMax: 24,
53
+ recoveryHeadingStart: 0.46,
54
+ recoveryHeadingGain: 64,
55
+ recoveryHeadingMax: 38,
56
+ steeringLoadStart: 0.82,
57
+ steeringLoadMax: 18,
58
+ steeringLoadHeadingScale: 0.48,
59
+ loadPenalty: () => 0,
60
+ minimumSpeeds: { edge: 34, kerb: 58, track: 52 },
61
+ desiredSpeedMaxKph: 330,
62
+ minimumThrottle: (aggression) => 0.1 + aggression * 0.08,
63
+ brakeLimits: { kerb: (aggression) => 0.42, track: (aggression) => 0.72 - aggression * 0.08 },
64
+ brakeResponseKph: (aggression) => 28 + aggression * 11,
65
+ extraGripBrake: () => 0,
66
+ edgeBrakeLimitScale: 1,
67
+ edgeSlideBrakeStart: 1.1,
68
+ edgeSlideBrakeGain: 0,
69
+ edgeSlideVelocityBrakeGain: 0,
70
+ edgeSlideHeadingBrake: 0,
71
+ edgeSlideBrakeLimitScale: 0,
72
+ edgeSlideThrottleDamping: 0,
73
+ recoveryHeadingThrottleStart: 0.38,
74
+ throttleResponseKph: 16,
75
+ throttleScale: () => 1,
76
+ });
77
+ }
78
+
79
+ export function decideSimulatorRacingControls(car, orderIndex, race) {
80
+ return decideRacingControlsForMode(car, orderIndex, race, {
81
+ simulatorMode: true,
82
+ baseLookaheadSpeedFactor: (aggression) => 2.45 - aggression * 0.06,
83
+ previewLookaheadSpeedFactor: 6.15,
84
+ edgeLookaheadPenaltyMeters: 34,
85
+ cornerPressureCurvature: 0.00072,
86
+ edgeLineDamping: 1.55,
87
+ edgeRecoveryWeight: 1.45,
88
+ headingFeedForwardBase: (carEntry, edgeGuard) => 0.2 + carEntry.racecraft * 0.08 + edgeGuard.pressure * 0.07,
89
+ headingFeedForwardLimit: 0.25,
90
+ pathSteerGain: (carEntry, edgeGuard) => 0.46 + carEntry.racecraft * 0.06 + edgeGuard.pressure * 0.18,
91
+ edgeRecoveryStart: 0.04,
92
+ edgeRecoveryGain: 3.1,
93
+ edgeRecoveryOverLimitGain: 1.15,
94
+ edgeRecoveryLimit: 0.78,
95
+ steeringLimitScale: (carEntry, speedRatio, gripUsage) => clamp(1 - speedRatio * 0.55 - gripUsage * 0.14, 0.26, 1),
96
+ gripBudget: (carEntry, aggression) => 10.6 + carEntry.racecraft * 1.55 + (carEntry.tireEnergy ?? 100) * 0.006 + aggression * 0.55,
97
+ cornerSpeedMultiplier: (aggression) => 0.93 + aggression * 0.035,
98
+ paceBonus: (carEntry) => (carEntry.pace - 1) * 12,
99
+ aggressionSpeedBonus: (aggression) => aggression * 4,
100
+ minCornerSpeedKph: 58,
101
+ maxCornerSpeedKph: 292,
102
+ edgePenaltyMetersScale: (aggression) => 10.5 - aggression * 0.8,
103
+ edgePenaltyPressure: 92,
104
+ edgePressureSpeedPenalty: 32,
105
+ edgeOutwardSpeedPenalty: 30,
106
+ edgeHeadingOutwardPenalty: 22,
107
+ steeringPenaltyStart: 0.27,
108
+ steeringPenaltyGain: 42,
109
+ steeringPenaltyMax: 28,
110
+ headingPenaltyStart: 0.32,
111
+ headingPenaltyGain: 62,
112
+ headingPenaltyMax: 42,
113
+ recoveryHeadingStart: 0.28,
114
+ recoveryHeadingGain: 86,
115
+ recoveryHeadingMax: 58,
116
+ steeringLoadStart: 0.58,
117
+ steeringLoadMax: 36,
118
+ steeringLoadHeadingScale: 0.32,
119
+ loadPenalty: (entry, gripUsage, slipAngle, cornerPressure, outwardDriftAngle) =>
120
+ clamp((gripUsage - 0.52) * 92, 0, 54) +
121
+ clamp(slipAngle * 130, 0, 42) +
122
+ cornerPressure * 18 +
123
+ clamp(outwardDriftAngle * 330, 0, 62),
124
+ minimumSpeeds: { edge: 44, kerb: 58, track: 76 },
125
+ desiredSpeedMaxKph: 302,
126
+ minimumThrottle: (aggression) => 0.03 + aggression * 0.03,
127
+ brakeLimits: { kerb: (aggression) => 0.36, track: (aggression) => 0.9 - aggression * 0.03 },
128
+ brakeResponseKph: (aggression) => 15 + aggression * 5,
129
+ extraGripBrake: (gripUsage, slipAngle, brakeLimit) => clamp((gripUsage - 0.68) * 0.42 + slipAngle * 0.5, 0, brakeLimit * 0.6),
130
+ edgeBrakeLimitScale: 1.08,
131
+ edgeSlideBrakeStart: 0.22,
132
+ edgeSlideBrakeGain: 0.78,
133
+ edgeSlideVelocityBrakeGain: 0.12,
134
+ edgeSlideHeadingBrake: 0.1,
135
+ edgeSlideBrakeLimitScale: 0.86,
136
+ edgeSlideThrottleDamping: 0.9,
137
+ recoveryHeadingThrottleStart: 0.3,
138
+ throttleResponseKph: 18,
139
+ throttleScale: (steeringLoad, gripUsage, slipAngle, edgeGuard) => clamp(
140
+ 1 - steeringLoad * 0.42 - Math.max(0, gripUsage - 0.58) * 0.58 - slipAngle * 1.25 - edgeGuard.pressure * 0.42,
141
+ 0,
142
+ 1,
143
+ ),
144
+ });
145
+ }
146
+
147
+ function decideRacingControlsForMode(car, orderIndex, race, profile) {
148
+ const aggression = car.aggression ?? car.personality?.baseAggression ?? 0.5;
149
+ const simulatorMode = profile.simulatorMode;
150
+ const edgeGuard = calculateTrackEdgeGuard(car, race);
151
+ const edgeMotion = analyzeTrackEdgeMotion(car, race);
152
+ const gripUsage = Number.isFinite(car.gripUsage) ? car.gripUsage : 0;
153
+ const slipAngle = Math.abs(car.slipAngleRadians ?? 0);
154
+ const baseLookahead = clamp(
155
+ car.speed * profile.baseLookaheadSpeedFactor(aggression) +
156
+ LOOKAHEAD_BASE_DISTANCE -
157
+ edgeGuard.pressure * metersToSimUnits(profile.edgeLookaheadPenaltyMeters),
158
+ EDGE_RECOVERY_MIN_LOOKAHEAD,
159
+ LOOKAHEAD_MAX_DISTANCE,
160
+ );
161
+ const previewLookahead = clamp(
162
+ car.speed * profile.previewLookaheadSpeedFactor + LOOKAHEAD_BASE_DISTANCE,
163
+ baseLookahead,
164
+ BRAKING_LOOKAHEAD_MAX_DISTANCE,
165
+ );
166
+ const previewCurvature = maxLookaheadCurvature(race.track, car.progress, previewLookahead);
167
+ const cornerPressure = clamp(previewCurvature / profile.cornerPressureCurvature, 0, 1);
168
+ const lookahead = clamp(
169
+ baseLookahead * (1 - cornerPressure * 0.46),
170
+ EDGE_RECOVERY_MIN_LOOKAHEAD,
171
+ LOOKAHEAD_MAX_DISTANCE,
172
+ );
173
+ const targetBase = pointAt(race.track, car.progress + lookahead);
174
+ const curvature = Math.max(
175
+ car.trackState.curvature,
176
+ targetBase.curvature,
177
+ previewCurvature,
178
+ );
179
+ const lanePlan = planRacingLine(car, orderIndex, race);
180
+ const cornerOffsetDamping = clamp(curvature / 0.0012, 0, 0.58);
181
+ const recoveryOffset = edgeGuard.side * edgeGuard.recoveryOffset;
182
+ const racingOffset = calculateRacingLineOffset(car, race, lookahead, curvature, edgeGuard);
183
+ const trafficPressure = lanePlan.attackCommitted || lanePlan.sameLaneAhead || lanePlan.actualLaneAhead || lanePlan.sideRisk ? 1 : 0;
184
+ const racingLineWeight = lanePlan.attackCommitted ? 0.18 : trafficPressure ? 0.52 : 1;
185
+ const cornerOffsetFactor = 1 - cornerOffsetDamping * (lanePlan.attackCommitted ? 0.28 : trafficPressure ? 0.58 : 1);
186
+ const targetOffset = clamp(
187
+ (lanePlan.offset + racingOffset * racingLineWeight) * cornerOffsetFactor * (1 - edgeGuard.pressure * profile.edgeLineDamping) +
188
+ recoveryOffset * edgeGuard.pressure * profile.edgeRecoveryWeight,
189
+ -edgeGuard.recoveryOffset,
190
+ edgeGuard.recoveryOffset,
191
+ );
192
+ const target = offsetTrackPoint(targetBase, targetOffset);
193
+ const angleError = angleToPoint(car, target);
194
+ const headingError = normalizeAngle(targetBase.heading - car.heading);
195
+ const lateralError = car.trackState?.signedOffset ?? 0;
196
+ const outwardDriftAngle = simulatorMode &&
197
+ Math.sign(lateralError) !== 0 &&
198
+ Math.sign(normalizeAngle(car.heading - (car.trackState?.heading ?? targetBase.heading))) === Math.sign(lateralError)
199
+ ? Math.abs(normalizeAngle(car.heading - (car.trackState?.heading ?? targetBase.heading)))
200
+ : 0;
201
+ const cornerCommitment = clamp(Math.abs(headingError) / 0.9, 0, 0.72);
202
+ const lateralCorrection = -Math.atan2(
203
+ lateralError * (0.85 + edgeGuard.pressure * 0.5),
204
+ Math.max(car.speed, kphToSimSpeed(35)),
205
+ ) * (1 - cornerCommitment * (simulatorMode ? 0.78 : 0.62));
206
+ const headingFeedForward = clamp(
207
+ headingError * profile.headingFeedForwardBase(car, edgeGuard),
208
+ -profile.headingFeedForwardLimit,
209
+ profile.headingFeedForwardLimit,
210
+ );
211
+ let pathSteer = angleError * profile.pathSteerGain(car, edgeGuard) + headingFeedForward;
212
+ if (edgeGuard.side !== 0 && Math.sign(pathSteer) === edgeGuard.side) {
213
+ pathSteer *= 1 - edgeGuard.pressure * 0.85;
214
+ }
215
+ const edgeRecoverySteer = -edgeGuard.side * clamp(
216
+ (edgeGuard.pressure - profile.edgeRecoveryStart) * profile.edgeRecoveryGain +
217
+ edgeGuard.overLimitPressure * profile.edgeRecoveryOverLimitGain +
218
+ (simulatorMode ? outwardDriftAngle * clamp(Math.abs(lateralError) / (race.track.width * 0.26), 0, 1) * 1.8 : 0),
219
+ 0,
220
+ profile.edgeRecoveryLimit,
221
+ );
222
+ const speedRatio = clamp(car.speed / VEHICLE_LIMITS.maxSpeed, 0, 1);
223
+ const simulatorSteeringLimit = VEHICLE_LIMITS.maxSteer *
224
+ profile.steeringLimitScale(car, speedRatio, gripUsage);
225
+ const steeringLimit = simulatorMode
226
+ ? (car.trackState.surface === 'kerb' ? simulatorSteeringLimit * 0.82 : simulatorSteeringLimit)
227
+ : car.trackState.surface === 'kerb'
228
+ ? VEHICLE_LIMITS.maxSteer * 0.96
229
+ : VEHICLE_LIMITS.maxSteer;
230
+ let steeringRequest = clamp(pathSteer + lateralCorrection + edgeRecoverySteer, -steeringLimit, steeringLimit);
231
+ if (simulatorMode && edgeGuard.pressure > 0.62 && Math.sign(steeringRequest) === edgeGuard.side) {
232
+ steeringRequest = clamp(
233
+ -edgeGuard.side * Math.max(Math.abs(steeringRequest), steeringLimit * 0.55),
234
+ -steeringLimit,
235
+ steeringLimit,
236
+ );
237
+ }
238
+ const cornerRadiusMeters = simUnitsToMeters(1 / Math.max(curvature, 1e-7));
239
+ const lateralGripBudget = profile.gripBudget(car, aggression);
240
+ const cornerTargetKph = clamp(
241
+ Math.sqrt(lateralGripBudget * cornerRadiusMeters) * 3.6 *
242
+ profile.cornerSpeedMultiplier(aggression) +
243
+ profile.paceBonus(car) + profile.aggressionSpeedBonus(aggression),
244
+ profile.minCornerSpeedKph,
245
+ profile.maxCornerSpeedKph,
246
+ );
247
+ const edgePenalty = edgeGuard.pressure > 0.42
248
+ ? simUnitsToMeters(Math.max(0, car.trackState.crossTrackError - edgeGuard.softLimit)) *
249
+ profile.edgePenaltyMetersScale(aggression) +
250
+ edgeGuard.overLimitPressure * profile.edgePenaltyPressure
251
+ : edgeGuard.overLimitPressure * profile.edgePenaltyPressure +
252
+ edgeGuard.pressure * profile.edgePressureSpeedPenalty;
253
+ const edgeMotionPenalty =
254
+ edgeMotion.outwardSpeedMps * profile.edgeOutwardSpeedPenalty * clamp(edgeGuard.pressure, 0, 1) +
255
+ (edgeMotion.headingOutward ? profile.edgeHeadingOutwardPenalty * clamp(edgeGuard.pressure, 0, 1) : 0);
256
+ const steeringPenalty = clamp((Math.abs(angleError) - profile.steeringPenaltyStart) * profile.steeringPenaltyGain, 0, profile.steeringPenaltyMax);
257
+ const headingPenalty = clamp((Math.abs(headingError) - profile.headingPenaltyStart) * profile.headingPenaltyGain, 0, profile.headingPenaltyMax);
258
+ const recoveryAlignmentPenalty = edgeGuard.pressure > 0.2
259
+ ? clamp((Math.abs(headingError) - profile.recoveryHeadingStart) * profile.recoveryHeadingGain, 0, profile.recoveryHeadingMax)
260
+ : 0;
261
+ const steeringLoadPenalty = clamp(
262
+ (Math.abs(steeringRequest) / Math.max(steeringLimit, 1e-6) - profile.steeringLoadStart) * 42,
263
+ 0,
264
+ profile.steeringLoadMax,
265
+ ) * clamp(Math.abs(headingError) / profile.steeringLoadHeadingScale, 0, 1);
266
+ const simulatorLoadPenalty = profile.loadPenalty(car, gripUsage, slipAngle, cornerPressure, outwardDriftAngle);
267
+ const actualLaneLateral = lanePlan.actualLaneAhead
268
+ ? Math.abs(lanePlan.actualLaneAhead.signedOffset - (car.trackState?.signedOffset ?? 0))
269
+ : Infinity;
270
+ const plannedPassingOverlap = lanePlan.attackCommitted && lanePlan.sameLaneAhead
271
+ ? Math.abs(lanePlan.sameLaneAhead.signedOffset - (car.trackState?.signedOffset ?? 0)) > OVERTAKE_LATERAL_MIN
272
+ : false;
273
+ const trafficPenalty = Math.max(
274
+ lanePlan.sameLaneAhead
275
+ ? calculatePlannedTrafficPenalty(lanePlan.sameLaneAhead, plannedPassingOverlap)
276
+ : 0,
277
+ lanePlan.actualLaneAhead && actualLaneLateral < OVERTAKE_LATERAL_MIN
278
+ ? calculateActualOverlapPenalty(car, lanePlan.actualLaneAhead)
279
+ : 0,
280
+ lanePlan.sideRisk ? clamp(simUnitsToMeters(TRAFFIC_SIDE_GAP - lanePlan.sideRisk.lateral) * 0.78, 0, 30) : 0,
281
+ ) * (1 - aggression * 0.28);
282
+ const minimumDesiredSpeedKph = edgeGuard.pressure > 0.55
283
+ ? profile.minimumSpeeds.edge
284
+ : car.trackState.surface === 'kerb' ? profile.minimumSpeeds.kerb : profile.minimumSpeeds.track;
285
+ const desiredSpeedKph = clamp(
286
+ (car.drsActive ? cornerTargetKph + 22 : cornerTargetKph) -
287
+ edgePenalty -
288
+ edgeMotionPenalty -
289
+ steeringPenalty -
290
+ headingPenalty -
291
+ recoveryAlignmentPenalty -
292
+ steeringLoadPenalty -
293
+ simulatorLoadPenalty -
294
+ trafficPenalty,
295
+ minimumDesiredSpeedKph,
296
+ profile.desiredSpeedMaxKph,
297
+ );
298
+ const desiredSpeed = clamp(
299
+ kphToSimSpeed(desiredSpeedKph),
300
+ kphToSimSpeed(minimumDesiredSpeedKph),
301
+ VEHICLE_LIMITS.maxSpeed,
302
+ );
303
+ const speedError = desiredSpeed - car.speed;
304
+ const minimumThrottle = edgeGuard.pressure > 0.2 ? 0 : profile.minimumThrottle(aggression);
305
+ const brakeLimit = simulatorMode
306
+ ? car.trackState.surface === 'kerb'
307
+ ? profile.brakeLimits.kerb(aggression)
308
+ : profile.brakeLimits.track(aggression)
309
+ : car.trackState.surface === 'kerb'
310
+ ? profile.brakeLimits.kerb(aggression)
311
+ : profile.brakeLimits.track(aggression);
312
+ let brakeAmount = speedError < -kphToSimSpeed(3)
313
+ ? clamp(Math.abs(speedError) / kphToSimSpeed(profile.brakeResponseKph(aggression)), 0, brakeLimit)
314
+ : 0;
315
+ const simulatorBrakeRisk = cornerPressure > 0.15 ||
316
+ edgeGuard.pressure > 0.2 ||
317
+ outwardDriftAngle > 0.03 ||
318
+ car.speed > kphToSimSpeed(132) ||
319
+ speedError < kphToSimSpeed(8);
320
+ if (simulatorMode && simulatorBrakeRisk && (gripUsage > 0.9 || slipAngle > 0.12)) {
321
+ brakeAmount = Math.max(brakeAmount, profile.extraGripBrake(gripUsage, slipAngle, brakeLimit));
322
+ }
323
+ if (simulatorMode && car.speed < kphToSimSpeed(58) && edgeGuard.pressure < 0.75) {
324
+ brakeAmount *= 0.25;
325
+ }
326
+ if (edgeGuard.pressure > 0.46 && car.speed > kphToSimSpeed(48)) {
327
+ brakeAmount = Math.max(brakeAmount, clamp((edgeGuard.pressure - 0.42) * 0.9 * profile.edgeBrakeLimitScale, 0, brakeLimit));
328
+ }
329
+ if (edgeGuard.pressure > profile.edgeSlideBrakeStart && edgeMotion.outwardSpeedMps > 0.25) {
330
+ brakeAmount = Math.max(
331
+ brakeAmount,
332
+ clamp(
333
+ (edgeGuard.pressure - profile.edgeSlideBrakeStart) * profile.edgeSlideBrakeGain +
334
+ edgeMotion.outwardSpeedMps * profile.edgeSlideVelocityBrakeGain +
335
+ (edgeMotion.headingOutward ? profile.edgeSlideHeadingBrake : 0),
336
+ 0,
337
+ brakeLimit * profile.edgeSlideBrakeLimitScale,
338
+ ),
339
+ );
340
+ }
341
+ const recoveryThrottleScale = edgeGuard.pressure > 0.24
342
+ ? 1 - clamp((Math.abs(headingError) - profile.recoveryHeadingThrottleStart) / 0.92, 0, 0.82)
343
+ : 1;
344
+ const throttleRequest = speedError > kphToSimSpeed(1)
345
+ ? clamp(speedError / kphToSimSpeed(profile.throttleResponseKph), minimumThrottle, 1)
346
+ : 0;
347
+ const steeringLoad = Math.abs(steeringRequest) / Math.max(steeringLimit, 1e-6);
348
+ const simulatorThrottleScale = profile.throttleScale(steeringLoad, gripUsage, slipAngle, edgeGuard);
349
+ const edgeSlideThrottleScale = 1 - clamp(
350
+ (
351
+ Math.max(0, edgeGuard.pressure - 0.18) +
352
+ edgeMotion.outwardSpeedMps * 0.16 +
353
+ (edgeMotion.headingOutward ? 0.18 : 0)
354
+ ) * profile.edgeSlideThrottleDamping,
355
+ 0,
356
+ 0.92,
357
+ );
358
+
359
+ return createDriverInput()
360
+ .steer(steeringRequest)
361
+ .accelerate(brakeAmount > 0.05 ? 0 : throttleRequest * recoveryThrottleScale * simulatorThrottleScale * edgeSlideThrottleScale)
362
+ .brake(brakeAmount)
363
+ .controls();
364
+ }
365
+
366
+ export function maxLookaheadCurvature(track, progress, lookahead) {
367
+ let maximum = 0;
368
+ let previous = pointAt(track, progress);
369
+ for (let index = 1; index <= CURVATURE_LOOKAHEAD_SAMPLES; index += 1) {
370
+ const sample = pointAt(track, progress + lookahead * (index / CURVATURE_LOOKAHEAD_SAMPLES));
371
+ const segmentDistance = Math.max(1, lookahead / CURVATURE_LOOKAHEAD_SAMPLES);
372
+ const segmentCurvature = Math.abs(normalizeAngle(sample.heading - previous.heading)) / segmentDistance;
373
+ maximum = Math.max(maximum, Math.abs(sample.curvature ?? 0), Math.min(segmentCurvature, PREVIEW_CURVATURE_CAP));
374
+ previous = sample;
375
+ }
376
+ return maximum;
377
+ }
378
+
379
+ export function calculateRacingLineOffset(car, race, lookahead, curvature, edgeGuard) {
380
+ const cornerStrength = clamp((curvature - 0.00012) / 0.00095, 0, 1);
381
+ if (cornerStrength <= 0) return 0;
382
+
383
+ const entry = pointAt(race.track, car.progress - lookahead * 0.35);
384
+ const apex = pointAt(race.track, car.progress + lookahead * 0.55);
385
+ const exit = pointAt(race.track, car.progress + lookahead * 1.25);
386
+ const signedEntry = normalizeAngle(apex.heading - entry.heading);
387
+ const signedExit = normalizeAngle(exit.heading - apex.heading);
388
+ const turnDirection = Math.sign(Math.abs(signedEntry) > Math.abs(signedExit) ? signedEntry : signedExit);
389
+ if (turnDirection === 0) return 0;
390
+
391
+ const aggression = car.aggression ?? car.personality?.baseAggression ?? 0.5;
392
+ const simulatorMode = race.physicsMode === 'simulator';
393
+ const safeEdge = race.track.width / 2 - VEHICLE_LIMITS.carWidth *
394
+ (simulatorMode ? 2.05 - aggression * 0.08 : 1.15 - aggression * 0.12);
395
+ const apexOffset = turnDirection * safeEdge *
396
+ (simulatorMode ? 0.42 + aggression * 0.05 : 0.72 + aggression * 0.08);
397
+
398
+ return apexOffset * cornerStrength * (1 - edgeGuard.pressure * (simulatorMode ? 0.82 : 0.65));
399
+ }