@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
package/src/index.js CHANGED
@@ -30,7 +30,9 @@ export {
30
30
  export { DEMO_PROJECT_DRIVERS } from './data/demoDrivers.js';
31
31
  export { DEFAULT_F1_SIMULATOR_ASSETS } from './config/defaultAssets.js';
32
32
  export { PADDOCK_SIMULATOR_PRESETS } from './config/defaultOptions.js';
33
+ export { createPaddockDriverControllerLoop } from './environment/controllerLoop.js';
33
34
  export { normalizeSimulatorDrivers } from './data/normalizeDrivers.js';
35
+ export { createProceduralTrack } from './simulation/track/trackModel.js';
34
36
  export {
35
37
  REAL_F1_CAR_LENGTH_METERS,
36
38
  SIM_UNITS_PER_METER,
@@ -109,6 +111,12 @@ export async function mountF1Simulator(root, options = {}) {
109
111
  setSafetyCarDeployed(deployed) {
110
112
  app.setSafetyCarDeployed(deployed);
111
113
  },
114
+ setRedFlagDeployed(deployed) {
115
+ app.setRedFlagDeployed(deployed);
116
+ },
117
+ setPitLaneOpen(open) {
118
+ app.setPitLaneOpen(open);
119
+ },
112
120
  callSafetyCar() {
113
121
  app.setSafetyCarDeployed(true);
114
122
  },
@@ -119,6 +127,24 @@ export async function mountF1Simulator(root, options = {}) {
119
127
  const active = app.getSnapshot()?.raceControl.mode === 'safety-car';
120
128
  app.setSafetyCarDeployed(!active);
121
129
  },
130
+ setPitIntent(driverId, intent, targetCompound) {
131
+ return app.setPitIntent(driverId, intent, targetCompound);
132
+ },
133
+ getPitIntent(driverId) {
134
+ return app.getPitIntent(driverId);
135
+ },
136
+ getPitTargetCompound(driverId) {
137
+ return app.getPitTargetCompound(driverId);
138
+ },
139
+ getSimulationSpeed() {
140
+ return app.simulationSpeed;
141
+ },
142
+ servePenalty(penaltyId) {
143
+ return app.servePenalty(penaltyId);
144
+ },
145
+ cancelPenalty(penaltyId) {
146
+ return app.cancelPenalty(penaltyId);
147
+ },
122
148
  getSnapshot() {
123
149
  return app.getSnapshot();
124
150
  },
@@ -1,271 +1,13 @@
1
- import { Container, Graphics, Texture, TilingSprite } from 'pixi.js';
2
- import { nearestTrackState, offsetTrackPoint, pointAt, WORLD } from '../simulation/trackModel.js';
1
+ import { Container } from 'pixi.js';
2
+ import { WORLD } from '../simulation/track/trackModel.js';
3
+ import { destroyDisplayChildren } from './track/trackRenderGeometry.js';
4
+ import { addAsphalt, addBarriers, addBorders, addBoundaryUnderlay, addGrass, addGravelRunoff, addKerbs } from './track/trackMaterialRenderer.js';
5
+ import { addPitLane, addPitLaneRunoff } from './track/pitLaneRenderer.js';
6
+ import { addStartingGrid } from './track/gridRenderer.js';
7
+ import { addFinishLine } from './track/finishLineRenderer.js';
3
8
 
4
- const MATERIAL_TILE_SCALE = {
5
- asphalt: { x: 0.66, y: 0.66 },
6
- };
7
- const WORLD_BACKGROUND_PADDING = 2200;
8
- const GRASS_COLOR = 0x2e7d32;
9
- const GRAVEL_COLOR = 0xb49a68;
10
- const ASPHALT_COLOR = 0x4a4d52;
11
- const EDGE_REVEAL_OFFSET = 5;
12
- const EDGE_REVEAL_WIDTH = 16;
13
- const OUTER_BOUNDARY_OFFSET = 23;
14
- const OUTER_BOUNDARY_WIDTH = 16;
15
- const KERB_OFFSET = 9;
16
- const KERB_WIDTH = 29;
17
- const FINISH_LINE_DEPTH = 58;
18
- const FINISH_LINE_COLUMNS = 10;
19
- const START_GRID_SLOT_COUNT = 20;
20
- const START_GRID_SLOT_SPACING = 82;
21
- const START_GRID_FIRST_DISTANCE = -42;
22
- const START_GRID_LATERAL_OFFSET = 42;
23
- const START_GRID_BOX_LENGTH = 58;
24
- const START_GRID_BOX_WIDTH = 34;
25
- const SEGMENTED_STROKE_STEP = 2;
26
- const KERB_STEP = 4;
27
- const KERB_CURVATURE_THRESHOLD = 0.00038;
28
- const OFFSET_SEGMENT_SAMPLE_COUNT = 7;
29
- const NON_LOCAL_SAMPLE_STEP = 8;
30
- const OFFSET_GAP_SAMPLE_COUNT = 4;
31
-
32
- function makeTrackPath(track, offset = 0) {
33
- const path = new Graphics();
34
- const samples = track.samples.slice(0, -1);
35
- const first = offset === 0 ? samples[0] : offsetTrackPoint(samples[0], offset);
36
- path.moveTo(first.x, first.y);
37
-
38
- samples.slice(1).forEach((sample) => {
39
- const point = offset === 0 ? sample : offsetTrackPoint(sample, offset);
40
- path.lineTo(point.x, point.y);
41
- });
42
- path.closePath();
43
-
44
- return path;
45
- }
46
-
47
- function textureOrWhite(texture) {
48
- return texture ?? Texture.WHITE;
49
- }
50
-
51
- function destroyDisplayChildren(container) {
52
- container.removeChildren().forEach((child) => {
53
- child.destroy?.({ children: true, texture: false, textureSource: false });
54
- });
55
- }
56
-
57
- function arcDistance(track, first, second) {
58
- const delta = Math.abs(first - second);
59
- return Math.min(delta, track.length - delta);
60
- }
61
-
62
- function offsetPointIsLocal(track, source, point, offset) {
63
- const state = nearestTrackState(track, point);
64
- const localTolerance = Math.max(220, Math.abs(offset) * 2.1);
65
- const minimumEdgeDistance = Math.min(track.width / 2 - 8, Math.abs(offset) * 0.72);
66
-
67
- return (
68
- arcDistance(track, state.distance, source.distance) <= localTolerance &&
69
- state.crossTrackError >= minimumEdgeDistance
70
- );
71
- }
72
-
73
- function offsetPointOverlapsNonLocalRoad(track, source, point, offset) {
74
- const localTolerance = Math.max(220, Math.abs(offset) * 2.1);
75
- const roadBand = track.width / 2 + (track.kerbWidth ?? 0) + 8;
76
- const roadBandSquared = (roadBand + 42) ** 2;
77
-
78
- for (let index = 0; index < track.samples.length - 1; index += NON_LOCAL_SAMPLE_STEP) {
79
- const sample = track.samples[index];
80
- if (arcDistance(track, sample.distance, source.distance) <= localTolerance) continue;
81
-
82
- const dx = point.x - sample.x;
83
- const dy = point.y - sample.y;
84
- if (dx * dx + dy * dy > roadBandSquared) continue;
85
-
86
- const signedOffset = dx * sample.normalX + dy * sample.normalY;
87
- if (Math.abs(signedOffset) <= roadBand) return true;
88
- }
89
-
90
- return false;
91
- }
92
-
93
- function interpolatedSegmentPoint(start, end, amount) {
94
- return {
95
- x: start.x + (end.x - start.x) * amount,
96
- y: start.y + (end.y - start.y) * amount,
97
- };
98
- }
99
-
100
- function pointDistance(first, second) {
101
- return Math.hypot(second.x - first.x, second.y - first.y);
102
- }
103
-
104
- export function offsetSegmentIsSafe(track, current, next, start, end, offset) {
105
- const centerDistance = Math.hypot(next.x - current.x, next.y - current.y);
106
- const edgeDistance = Math.hypot(end.x - start.x, end.y - start.y);
107
- const maxEdgeDistance = Math.max(centerDistance * 2.6, track.width * 0.58);
108
- if (edgeDistance > maxEdgeDistance || Math.abs(offset) > track.width + track.gravelWidth + track.runoffWidth) {
109
- return false;
110
- }
111
-
112
- const forwardDistance = ((next.distance - current.distance + track.length) % track.length) || centerDistance;
113
-
114
- for (let sample = 0; sample <= OFFSET_SEGMENT_SAMPLE_COUNT; sample += 1) {
115
- const amount = sample / OFFSET_SEGMENT_SAMPLE_COUNT;
116
- const source = {
117
- distance: (current.distance + forwardDistance * amount) % track.length,
118
- };
119
- const point = interpolatedSegmentPoint(start, end, amount);
120
- if (!offsetPointIsLocal(track, source, point, offset)) return false;
121
- if (offsetPointOverlapsNonLocalRoad(track, source, point, offset)) return false;
122
- }
123
-
124
- return true;
125
- }
126
-
127
- export function offsetGapBridgeIsSafe(track, start, end, width) {
128
- const bridgeDistance = pointDistance(start, end);
129
- const maximumBridgeDistance = Math.max(track.width * 0.08, width * 3.4);
130
- if (bridgeDistance > maximumBridgeDistance) return false;
131
-
132
- const minimumEdgeDistance = track.width / 2 - Math.max(width * 2.5, 10);
133
- for (let sample = 1; sample < OFFSET_GAP_SAMPLE_COUNT; sample += 1) {
134
- const amount = sample / OFFSET_GAP_SAMPLE_COUNT;
135
- const point = interpolatedSegmentPoint(start, end, amount);
136
- const state = nearestTrackState(track, point);
137
- if (state.crossTrackError < minimumEdgeDistance) return false;
138
- }
139
-
140
- return true;
141
- }
142
-
143
- export function getOffsetStrokeSegments(track, {
144
- side,
145
- offset,
146
- step = SEGMENTED_STROKE_STEP,
147
- }) {
148
- const samples = track.samples.slice(0, -1);
149
- const signedOffset = side * offset;
150
-
151
- return samples.map((current, index) => {
152
- if (index % step !== 0) return null;
153
-
154
- const next = samples[(index + step) % samples.length];
155
- const start = offsetTrackPoint(current, signedOffset);
156
- const end = offsetTrackPoint(next, signedOffset);
157
-
158
- return {
159
- current,
160
- next,
161
- start,
162
- end,
163
- safe: offsetSegmentIsSafe(track, current, next, start, end, signedOffset),
164
- };
165
- }).filter(Boolean);
166
- }
167
-
168
- export function getOffsetGapBridges(track, segments, width) {
169
- if (segments.length < 2) return [];
170
-
171
- return segments.flatMap((segment, index) => {
172
- if (!segment.safe) return [];
173
-
174
- const previousIndex = (index - 1 + segments.length) % segments.length;
175
- if (segments[previousIndex].safe) return [];
176
-
177
- let previousSafe = null;
178
- for (let lookup = 2; lookup <= segments.length; lookup += 1) {
179
- const candidate = segments[(index - lookup + segments.length) % segments.length];
180
- if (!candidate.safe) continue;
181
- previousSafe = candidate;
182
- break;
183
- }
184
-
185
- if (!previousSafe || previousSafe === segment) return [];
186
- if (!offsetGapBridgeIsSafe(track, previousSafe.end, segment.start, width)) return [];
187
-
188
- return [{
189
- start: previousSafe.end,
190
- end: segment.start,
191
- }];
192
- });
193
- }
194
-
195
- function drawOffsetSegment(graphics, start, end, {
196
- width,
197
- color,
198
- alpha,
199
- cap,
200
- }) {
201
- graphics.moveTo(start.x, start.y);
202
- graphics.lineTo(end.x, end.y);
203
- graphics.stroke({
204
- width,
205
- color,
206
- alpha,
207
- join: 'round',
208
- cap,
209
- });
210
- }
211
-
212
- function drawContinuousOffsetStroke(graphics, track, {
213
- side,
214
- offset,
215
- width,
216
- color,
217
- alpha = 1,
218
- step = SEGMENTED_STROKE_STEP,
219
- }) {
220
- const samples = track.samples.slice(0, -1).filter((_, index) => index % step === 0);
221
- if (samples.length < 2) return;
222
-
223
- const first = offsetTrackPoint(samples[0], side * offset);
224
- graphics.moveTo(first.x, first.y);
225
- samples.slice(1).forEach((sample) => {
226
- const point = offsetTrackPoint(sample, side * offset);
227
- graphics.lineTo(point.x, point.y);
228
- });
229
- graphics.closePath();
230
- graphics.stroke({
231
- width,
232
- color,
233
- alpha,
234
- join: 'round',
235
- cap: 'round',
236
- });
237
- }
238
-
239
- function drawSegmentedOffsetStroke(graphics, track, {
240
- side,
241
- offset,
242
- width,
243
- color,
244
- alpha = 1,
245
- step = SEGMENTED_STROKE_STEP,
246
- cap = 'round',
247
- }) {
248
- const segments = getOffsetStrokeSegments(track, { side, offset, step });
249
-
250
- segments.forEach((segment) => {
251
- if (!segment.safe) return;
252
- drawOffsetSegment(graphics, segment.start, segment.end, {
253
- width,
254
- color,
255
- alpha,
256
- cap,
257
- });
258
- });
259
-
260
- getOffsetGapBridges(track, segments, width).forEach((bridge) => {
261
- drawOffsetSegment(graphics, bridge.start, bridge.end, {
262
- width,
263
- color,
264
- alpha,
265
- cap: 'round',
266
- });
267
- });
268
- }
9
+ export { offsetGapBridgeIsSafe, offsetSegmentIsSafe, getOffsetGapBridges, getOffsetStrokeSegments } from './track/offsetStrokeSafety.js';
10
+ export { getTrackMaterialBands } from './track/trackMaterialRenderer.js';
269
11
 
270
12
  export class ProceduralTrackAsset {
271
13
  constructor({ textures = {}, world = WORLD } = {}) {
@@ -276,248 +18,16 @@ export class ProceduralTrackAsset {
276
18
 
277
19
  render(track) {
278
20
  destroyDisplayChildren(this.container);
279
- this.addGrass();
280
- this.addGravelRunoff(track);
281
- this.addBoundaryUnderlay(track);
282
- this.addAsphalt(track);
283
- this.addKerbs(track);
284
- this.addBorders(track);
285
- this.addStartingGrid(track);
286
- this.addFinishLine(track);
287
- }
288
-
289
- addGrass() {
290
- const grass = new Graphics();
291
- grass.rect(
292
- -WORLD_BACKGROUND_PADDING,
293
- -WORLD_BACKGROUND_PADDING,
294
- this.world.width + WORLD_BACKGROUND_PADDING * 2,
295
- this.world.height + WORLD_BACKGROUND_PADDING * 2,
296
- ).fill(GRASS_COLOR);
297
- this.container.addChild(grass);
298
- }
299
-
300
- addGravelRunoff(track) {
301
- const gravel = new Graphics();
302
- const innerOffset = track.width / 2 + 8;
303
- const outerOffset = track.width / 2 + track.gravelWidth;
304
- const centerOffset = (innerOffset + outerOffset) / 2;
305
- const width = outerOffset - innerOffset;
306
-
307
- [-1, 1].forEach((side) => {
308
- drawContinuousOffsetStroke(gravel, track, {
309
- side,
310
- offset: centerOffset,
311
- width,
312
- color: GRAVEL_COLOR,
313
- step: 4,
314
- });
315
- });
316
- this.container.addChild(gravel);
317
- }
318
-
319
- addBoundaryUnderlay(track) {
320
- const boundary = new Graphics();
321
- [-1, 1].forEach((side) => {
322
- drawContinuousOffsetStroke(boundary, track, {
323
- side,
324
- offset: track.width / 2 + OUTER_BOUNDARY_OFFSET,
325
- width: OUTER_BOUNDARY_WIDTH,
326
- color: 0x090a0d,
327
- alpha: 0.64,
328
- step: 2,
329
- });
330
- });
331
-
332
- this.drawKerbStripes(boundary, track, { clipUnsafeSegments: false });
333
-
334
- [-1, 1].forEach((side) => {
335
- drawContinuousOffsetStroke(boundary, track, {
336
- side,
337
- offset: track.width / 2 + EDGE_REVEAL_OFFSET,
338
- width: EDGE_REVEAL_WIDTH,
339
- color: 0xf8fafc,
340
- alpha: 0.94,
341
- step: 2,
342
- });
343
- });
344
-
345
- this.container.addChild(boundary);
346
- }
347
-
348
- drawKerbStripes(kerbs, track, { clipUnsafeSegments }) {
349
- const samples = track.samples.slice(0, -1);
350
-
351
- for (let index = 0; index < samples.length; index += KERB_STEP) {
352
- const sample = samples[index];
353
- const next = samples[(index + KERB_STEP) % samples.length];
354
- const curvature = Math.max(sample.curvature, next.curvature);
355
- if (curvature < KERB_CURVATURE_THRESHOLD) continue;
356
-
357
- const color = Math.floor(index / KERB_STEP) % 2 === 0 ? 0xe10600 : 0xf8fafc;
358
- [-1, 1].forEach((side) => {
359
- const offset = side * (track.width / 2 + KERB_OFFSET);
360
- const start = offsetTrackPoint(sample, offset);
361
- const end = offsetTrackPoint(next, offset);
362
- if (clipUnsafeSegments && !offsetSegmentIsSafe(track, sample, next, start, end, offset)) return;
363
- kerbs.moveTo(start.x, start.y);
364
- kerbs.lineTo(end.x, end.y);
365
- kerbs.stroke({
366
- width: KERB_WIDTH,
367
- color,
368
- alpha: 1,
369
- join: 'round',
370
- cap: 'butt',
371
- });
372
- });
373
- }
374
- }
375
-
376
- addMaskedMaterial({ track, texture, strokeWidth, alpha, tileScale }) {
377
- const sprite = new TilingSprite({
378
- texture: textureOrWhite(texture),
379
- width: this.world.width,
380
- height: this.world.height,
381
- tileScale,
382
- });
383
- sprite.alpha = alpha;
384
-
385
- const mask = makeTrackPath(track);
386
- mask.stroke({
387
- width: strokeWidth,
388
- color: 0xffffff,
389
- alpha: 1,
390
- join: 'round',
391
- cap: 'butt',
392
- });
393
- mask.renderable = false;
394
- sprite.mask = mask;
395
- this.container.addChild(sprite, mask);
396
- }
397
-
398
- addAsphalt(track) {
399
- const asphaltBase = makeTrackPath(track);
400
- asphaltBase.stroke({
401
- width: track.width,
402
- color: ASPHALT_COLOR,
403
- alpha: 1,
404
- join: 'round',
405
- cap: 'butt',
406
- });
407
- this.container.addChild(asphaltBase);
408
-
409
- this.addMaskedMaterial({
410
- track,
411
- texture: this.textures.asphalt,
412
- strokeWidth: track.width,
413
- alpha: 0.18,
414
- tileScale: MATERIAL_TILE_SCALE.asphalt,
415
- });
416
-
417
- }
418
-
419
- addBorders(track) {
420
- const borders = new Graphics();
421
-
422
- [-1, 1].forEach((side) => {
423
- drawSegmentedOffsetStroke(borders, track, {
424
- side,
425
- offset: track.width / 2,
426
- width: 5,
427
- color: 0xf8fafc,
428
- alpha: 0.86,
429
- step: 4,
430
- });
431
-
432
- drawSegmentedOffsetStroke(borders, track, {
433
- side,
434
- offset: track.width / 2 + 15,
435
- width: 7,
436
- color: 0x090a0d,
437
- alpha: 0.56,
438
- step: 4,
439
- });
440
- });
441
-
442
- this.container.addChild(borders);
443
- }
444
-
445
- addKerbs(track) {
446
- const kerbs = new Graphics();
447
- this.drawKerbStripes(kerbs, track, { clipUnsafeSegments: true });
448
- this.container.addChild(kerbs);
21
+ addGrass(this);
22
+ addGravelRunoff(this, track);
23
+ addBoundaryUnderlay(this, track);
24
+ addPitLaneRunoff(this, track);
25
+ addPitLane(this, track);
26
+ addAsphalt(this, track);
27
+ addKerbs(this, track);
28
+ addBorders(this, track);
29
+ addStartingGrid(this, track);
30
+ addFinishLine(this, track);
31
+ addBarriers(this, track);
449
32
  }
450
-
451
- addStartingGrid(track) {
452
- const grid = new Graphics();
453
-
454
- for (let index = 0; index < START_GRID_SLOT_COUNT; index += 1) {
455
- const base = pointAt(track, START_GRID_FIRST_DISTANCE - index * START_GRID_SLOT_SPACING);
456
- const lateralOffset = index % 2 === 0 ? -START_GRID_LATERAL_OFFSET : START_GRID_LATERAL_OFFSET;
457
- const center = offsetTrackPoint(base, lateralOffset);
458
- const forwardX = Math.cos(base.heading);
459
- const forwardY = Math.sin(base.heading);
460
- const normalX = base.normalX;
461
- const normalY = base.normalY;
462
- const halfLength = START_GRID_BOX_LENGTH / 2;
463
- const halfWidth = START_GRID_BOX_WIDTH / 2;
464
- const corners = [
465
- {
466
- x: center.x + forwardX * halfLength + normalX * halfWidth,
467
- y: center.y + forwardY * halfLength + normalY * halfWidth,
468
- },
469
- {
470
- x: center.x + forwardX * halfLength - normalX * halfWidth,
471
- y: center.y + forwardY * halfLength - normalY * halfWidth,
472
- },
473
- {
474
- x: center.x - forwardX * halfLength - normalX * halfWidth,
475
- y: center.y - forwardY * halfLength - normalY * halfWidth,
476
- },
477
- {
478
- x: center.x - forwardX * halfLength + normalX * halfWidth,
479
- y: center.y - forwardY * halfLength + normalY * halfWidth,
480
- },
481
- ];
482
-
483
- grid.poly(corners.flatMap((corner) => [corner.x, corner.y])).stroke({
484
- width: 3,
485
- color: 0xf8fafc,
486
- alpha: 0.8,
487
- join: 'round',
488
- });
489
- }
490
-
491
- this.container.addChild(grid);
492
- }
493
-
494
- addFinishLine(track) {
495
- const finishLine = new Graphics();
496
- const halfDepth = FINISH_LINE_DEPTH / 2;
497
- this.addFinishLineHalf(finishLine, track, -halfDepth, 0, 0);
498
- this.addFinishLineHalf(finishLine, track, 0, halfDepth, 1);
499
- this.container.addChild(finishLine);
500
- }
501
-
502
- addFinishLineHalf(graphics, track, startDistance, endDistance, rowOffset) {
503
- const start = pointAt(track, startDistance);
504
- const end = pointAt(track, endDistance);
505
- const roadPadding = 10;
506
- const width = track.width - roadPadding * 2;
507
- const leftEdge = -width / 2;
508
- const cellWidth = width / FINISH_LINE_COLUMNS;
509
-
510
- for (let column = 0; column < FINISH_LINE_COLUMNS; column += 1) {
511
- const innerOffset = leftEdge + column * cellWidth;
512
- const outerOffset = innerOffset + cellWidth;
513
- const color = (column + rowOffset) % 2 === 0 ? 0xf8fafc : 0x0b0d12;
514
- const a = offsetTrackPoint(start, innerOffset);
515
- const b = offsetTrackPoint(start, outerOffset);
516
- const c = offsetTrackPoint(end, outerOffset);
517
- const d = offsetTrackPoint(end, innerOffset);
518
-
519
- graphics.poly([a.x, a.y, b.x, b.y, c.x, c.y, d.x, d.y]).fill(color);
520
- }
521
- }
522
-
523
33
  }
@@ -1,23 +1,49 @@
1
1
  import { lerp, normalizeAngle } from '../simulation/simMath.js';
2
2
 
3
+ const CAR_BUFFER = Symbol('renderSnapshotCars');
4
+ const GHOST_BUFFER = Symbol('renderSnapshotGhosts');
5
+ const SAFETY_CAR_BUFFER = Symbol('renderSnapshotSafetyCar');
6
+
3
7
  function interpolateAngle(previous, current, amount) {
4
8
  if (!Number.isFinite(previous)) return current;
5
9
  return previous + normalizeAngle(current - previous) * amount;
6
10
  }
7
11
 
8
- function interpolateMovingEntity(entity, alpha) {
9
- return {
10
- ...entity,
11
- x: lerp(entity.previousX ?? entity.x, entity.x, alpha),
12
- y: lerp(entity.previousY ?? entity.y, entity.y, alpha),
13
- heading: interpolateAngle(entity.previousHeading ?? entity.heading, entity.heading, alpha),
14
- };
12
+ function interpolateMovingEntityInto(target, entity, alpha) {
13
+ Object.assign(target, entity);
14
+ target.x = lerp(entity.previousX ?? entity.x, entity.x, alpha);
15
+ target.y = lerp(entity.previousY ?? entity.y, entity.y, alpha);
16
+ target.heading = interpolateAngle(entity.previousHeading ?? entity.heading, entity.heading, alpha);
17
+ return target;
18
+ }
19
+
20
+ function interpolateEntityListInto(sourceEntities = [], targetEntities = [], alpha) {
21
+ targetEntities.length = sourceEntities.length;
22
+ for (let index = 0; index < sourceEntities.length; index += 1) {
23
+ const source = sourceEntities[index];
24
+ const target = targetEntities[index] ?? {};
25
+ targetEntities[index] = interpolateMovingEntityInto(target, source, alpha);
26
+ }
27
+ return targetEntities;
28
+ }
29
+
30
+ export function interpolateRenderSnapshotInto(target, snapshot, alpha) {
31
+ if (!target || typeof target !== 'object') target = {};
32
+ const carBuffer = target[CAR_BUFFER] ?? [];
33
+ const ghostBuffer = target[GHOST_BUFFER] ?? [];
34
+ const safetyCarBuffer = target[SAFETY_CAR_BUFFER] ?? {};
35
+ target[CAR_BUFFER] = carBuffer;
36
+ target[GHOST_BUFFER] = ghostBuffer;
37
+ target[SAFETY_CAR_BUFFER] = safetyCarBuffer;
38
+ Object.assign(target, snapshot);
39
+ target.cars = interpolateEntityListInto(snapshot.cars ?? [], carBuffer, alpha);
40
+ target.replayGhosts = interpolateEntityListInto(snapshot.replayGhosts ?? [], ghostBuffer, alpha);
41
+ target.safetyCar = snapshot.safetyCar
42
+ ? interpolateMovingEntityInto(safetyCarBuffer, snapshot.safetyCar, alpha)
43
+ : snapshot.safetyCar;
44
+ return target;
15
45
  }
16
46
 
17
47
  export function createRenderSnapshot(snapshot, alpha) {
18
- return {
19
- ...snapshot,
20
- cars: snapshot.cars.map((car) => interpolateMovingEntity(car, alpha)),
21
- safetyCar: interpolateMovingEntity(snapshot.safetyCar, alpha),
22
- };
48
+ return interpolateRenderSnapshotInto({}, snapshot, alpha);
23
49
  }
@@ -0,0 +1,34 @@
1
+ import { Graphics } from 'pixi.js';
2
+ import { offsetTrackPoint, pointAt } from '../../simulation/trackModel.js';
3
+ import { FINISH_LINE_COLUMNS, FINISH_LINE_DEPTH } from './trackRenderConstants.js';
4
+
5
+ export function addFinishLine(asset, track) {
6
+ const finishLine = new Graphics();
7
+ const halfDepth = FINISH_LINE_DEPTH / 2;
8
+ addFinishLineHalf(finishLine, track, -halfDepth, 0, 0);
9
+ addFinishLineHalf(finishLine, track, 0, halfDepth, 1);
10
+ asset.container.addChild(finishLine);
11
+
12
+ }
13
+
14
+ export function addFinishLineHalf(graphics, track, startDistance, endDistance, rowOffset) {
15
+ const start = pointAt(track, startDistance);
16
+ const end = pointAt(track, endDistance);
17
+ const roadPadding = 10;
18
+ const width = track.width - roadPadding * 2;
19
+ const leftEdge = -width / 2;
20
+ const cellWidth = width / FINISH_LINE_COLUMNS;
21
+
22
+ for (let column = 0; column < FINISH_LINE_COLUMNS; column += 1) {
23
+ const innerOffset = leftEdge + column * cellWidth;
24
+ const outerOffset = innerOffset + cellWidth;
25
+ const color = (column + rowOffset) % 2 === 0 ? 0xf8fafc : 0x0b0d12;
26
+ const a = offsetTrackPoint(start, innerOffset);
27
+ const b = offsetTrackPoint(start, outerOffset);
28
+ const c = offsetTrackPoint(end, outerOffset);
29
+ const d = offsetTrackPoint(end, innerOffset);
30
+
31
+ graphics.poly([a.x, a.y, b.x, b.y, c.x, c.y, d.x, d.y]).fill(color);
32
+ }
33
+
34
+ }