@pinkaphant/create-kolmo 0.4.2 → 0.5.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.
- package/README.md +56 -17
- package/bin/create-kolmo.mjs +20 -12
- package/package.json +1 -1
- package/templates/demolition/README.md +17 -0
- package/templates/demolition/index.html +29 -0
- package/templates/demolition/package.json +39 -0
- package/templates/demolition/src/App.tsx +599 -0
- package/templates/{lonedrive → demolition}/tsconfig.json +4 -2
- package/templates/{lonedrive → demolition}/vite.config.ts +3 -1
- package/templates/dust/README.md +9 -1
- package/templates/dust/index.html +15 -1
- package/templates/dust/package.json +8 -6
- package/templates/dust/src/App.tsx +106 -122
- package/templates/dust/tsconfig.json +4 -2
- package/templates/evolution/README.md +15 -0
- package/templates/evolution/index.html +29 -0
- package/templates/{lonedrive → evolution}/package.json +8 -7
- package/templates/evolution/src/App.tsx +619 -0
- package/templates/evolution/src/main.tsx +10 -0
- package/templates/evolution/src/styles.css +237 -0
- package/templates/evolution/tsconfig.json +16 -0
- package/templates/evolution/vite.config.ts +10 -0
- package/templates/longline/README.md +16 -9
- package/templates/longline/index.html +26 -12
- package/templates/longline/package.json +11 -9
- package/templates/longline/src/App.tsx +191 -42
- package/templates/longline/src/highway.ts +222 -0
- package/templates/longline/src/main.tsx +7 -6
- package/templates/longline/tsconfig.json +14 -16
- package/templates/longline/vite.config.ts +5 -23
- package/templates/road/README.md +8 -4
- package/templates/road/index.html +15 -1
- package/templates/road/package.json +5 -5
- package/templates/road/src/App.tsx +21 -6
- package/templates/road/tsconfig.json +1 -0
- package/templates/tugline/README.md +16 -0
- package/templates/tugline/index.html +31 -0
- package/templates/tugline/package.json +41 -0
- package/templates/tugline/src/App.tsx +48 -0
- package/templates/{longline → tugline}/src/components/Boat.tsx +1 -2
- package/templates/{longline → tugline}/src/components/Crates.tsx +2 -2
- package/templates/tugline/src/components/Hud.tsx +281 -0
- package/templates/{longline → tugline}/src/components/Longline.tsx +2 -2
- package/templates/{longline → tugline}/src/components/Ocean.tsx +14 -3
- package/templates/{longline → tugline}/src/components/Radar.tsx +15 -14
- package/templates/{longline → tugline}/src/components/Referee.tsx +6 -4
- package/templates/{longline → tugline}/src/components/Scene.tsx +10 -9
- package/templates/{longline → tugline}/src/components/Sky.tsx +14 -8
- package/templates/{longline → tugline}/src/game/store.test.ts +2 -2
- package/templates/{longline → tugline}/src/game/store.ts +38 -19
- package/templates/tugline/src/main.tsx +13 -0
- package/templates/tugline/src/styles.css +495 -0
- package/templates/tugline/tsconfig.json +18 -0
- package/templates/tugline/vite.config.ts +16 -0
- package/templates/lonedrive/README.md +0 -11
- package/templates/lonedrive/index.html +0 -15
- package/templates/lonedrive/src/App.tsx +0 -149
- package/templates/longline/src/components/Hud.tsx +0 -197
- package/templates/longline/src/styles.css +0 -367
- /package/templates/{lonedrive → demolition}/src/main.tsx +0 -0
- /package/templates/{longline → tugline}/src/components/CameraRig.tsx +0 -0
- /package/templates/{longline → tugline}/src/components/ocean-material.ts +0 -0
- /package/templates/{longline → tugline}/src/components/round-context.ts +0 -0
- /package/templates/{longline → tugline}/src/game/boat-control.ts +0 -0
- /package/templates/{longline → tugline}/src/game/constants.ts +0 -0
- /package/templates/{longline → tugline}/src/game/input.test.ts +0 -0
- /package/templates/{longline → tugline}/src/game/input.ts +0 -0
- /package/templates/{longline → tugline}/src/game/logic.test.ts +0 -0
- /package/templates/{longline → tugline}/src/game/logic.ts +0 -0
- /package/templates/{longline → tugline}/src/game/physics.test.ts +0 -0
- /package/templates/{longline → tugline}/src/game/radar.ts +0 -0
- /package/templates/{longline → tugline}/src/game/registry.ts +0 -0
- /package/templates/{longline → tugline}/src/game/sea.ts +0 -0
- /package/templates/{longline → tugline}/src/game/session.ts +0 -0
- /package/templates/{longline → tugline}/src/game/simulation.test.ts +0 -0
- /package/templates/{longline → tugline}/src/game/simulation.ts +0 -0
- /package/templates/{longline → tugline}/src/game/test-world.ts +0 -0
- /package/templates/{longline → tugline}/src/sfx.ts +0 -0
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import * as THREE from "three";
|
|
3
|
+
import { useFrame } from "@react-three/fiber";
|
|
4
|
+
import { useBeforePhysicsStep } from "@react-three/rapier";
|
|
5
|
+
import { damp3 } from "maath/easing";
|
|
6
|
+
import {
|
|
7
|
+
Game,
|
|
8
|
+
RoadRibbon,
|
|
9
|
+
Terrain,
|
|
10
|
+
computeSplineSpawnPose,
|
|
11
|
+
createEmptyVehicleCmd,
|
|
12
|
+
type SurfaceSpawnPose,
|
|
13
|
+
} from "@pinkaphant/kolmo";
|
|
14
|
+
import {
|
|
15
|
+
DERBY_PANEL_ROLES,
|
|
16
|
+
breedNextGeneration,
|
|
17
|
+
compileKolmogorovCarBundle,
|
|
18
|
+
computeGauntletFitness,
|
|
19
|
+
createInitialPopulation,
|
|
20
|
+
describeKolmogorovGene,
|
|
21
|
+
exportKolmogorovCarGlb,
|
|
22
|
+
formatKolmogorovGene,
|
|
23
|
+
parseKolmogorovGene,
|
|
24
|
+
unpackKolmogorovGene,
|
|
25
|
+
} from "@pinkaphant/kolmo/cars";
|
|
26
|
+
import {
|
|
27
|
+
disposeKhrBundle,
|
|
28
|
+
isLiveRapierBody,
|
|
29
|
+
type KhrAssetBundle,
|
|
30
|
+
} from "@khr3/core";
|
|
31
|
+
import { KhrVehicle, type KhrVehicleJointsHandle } from "@khr3/core/vehicle";
|
|
32
|
+
import { makeTerrain, roadFromPoints, type Road, type V3 } from "@khr3/roads";
|
|
33
|
+
import type { EcctrlVehicleHandle, VehicleInput } from "@pinkaphant/ecctrl/vehicle";
|
|
34
|
+
|
|
35
|
+
const POP_SIZE = 8;
|
|
36
|
+
const GAUNTLET_SEED = 314;
|
|
37
|
+
const GAUNTLET_SIZE_X = 96;
|
|
38
|
+
const GAUNTLET_SIZE_Z = 384;
|
|
39
|
+
const GAUNTLET_ROWS_Z = 192;
|
|
40
|
+
const GAUNTLET_COLS_X = 48;
|
|
41
|
+
const MAX_HEAT_SECONDS = 24;
|
|
42
|
+
const FIXED_STEP_DT = 1 / 60;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Builds a progressive `340 m` `+Z` climb, mogul rhythm section, and jump ramp spline on `@khr3/roads`.
|
|
46
|
+
*/
|
|
47
|
+
function buildGauntletCourse(): {
|
|
48
|
+
readonly road: Road;
|
|
49
|
+
readonly heights: Float32Array;
|
|
50
|
+
readonly terrainPos: [number, number, number];
|
|
51
|
+
} {
|
|
52
|
+
const pts: V3[] = [];
|
|
53
|
+
const zMin = -24;
|
|
54
|
+
const zMax = 336;
|
|
55
|
+
for (let z = zMin; z <= zMax; z += 12) {
|
|
56
|
+
const progress = Math.max(0, (z - 0) / 320);
|
|
57
|
+
const climb = progress * progress * 34;
|
|
58
|
+
const mogulAmp = progress < 0.12 ? 0 : Math.min(2.4, progress * 3.1);
|
|
59
|
+
const moguls = mogulAmp * Math.sin(z * 0.19) + mogulAmp * 0.45 * Math.cos(z * 0.37);
|
|
60
|
+
const swayX = 5.5 * Math.sin(z * 0.032) * Math.min(1, progress * 1.8);
|
|
61
|
+
pts.push([swayX, 1.5 + climb + moguls, z]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const road = roadFromPoints(pts, 7.5, 2.0);
|
|
65
|
+
const land = makeTerrain(GAUNTLET_SEED, road, {
|
|
66
|
+
amplitude: 9,
|
|
67
|
+
noise: { wavelength: 96, octaves: 3, gain: 0.5, lacunarity: 2 },
|
|
68
|
+
maxSlope: 0.58,
|
|
69
|
+
minShoulder: 8,
|
|
70
|
+
verge: 3.5,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const originX = -GAUNTLET_SIZE_X * 0.5;
|
|
74
|
+
const originZ = -32;
|
|
75
|
+
const heights = new Float32Array((GAUNTLET_ROWS_Z + 1) * (GAUNTLET_COLS_X + 1));
|
|
76
|
+
const strideZ = GAUNTLET_ROWS_Z + 1;
|
|
77
|
+
for (let col = 0; col <= GAUNTLET_COLS_X; col++) {
|
|
78
|
+
const x = originX + (col / GAUNTLET_COLS_X) * GAUNTLET_SIZE_X;
|
|
79
|
+
const base = col * strideZ;
|
|
80
|
+
for (let row = 0; row <= GAUNTLET_ROWS_Z; row++) {
|
|
81
|
+
const z = originZ + (row / GAUNTLET_ROWS_Z) * GAUNTLET_SIZE_Z;
|
|
82
|
+
heights[base + row] = land.height(x, z);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
road,
|
|
88
|
+
heights,
|
|
89
|
+
terrainPos: [0, 0, originZ + GAUNTLET_SIZE_Z * 0.5],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface CompiledSlot {
|
|
94
|
+
readonly slot: number;
|
|
95
|
+
readonly gene: number;
|
|
96
|
+
readonly hex: string;
|
|
97
|
+
readonly desc: string;
|
|
98
|
+
readonly bundle: KhrAssetBundle;
|
|
99
|
+
readonly pose: SurfaceSpawnPose;
|
|
100
|
+
readonly hasBoosters: boolean;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface LiveSlotProgress {
|
|
104
|
+
startZ: number;
|
|
105
|
+
maxDistM: number;
|
|
106
|
+
peakTimeS: number;
|
|
107
|
+
stallSeconds: number;
|
|
108
|
+
intactRatio: number;
|
|
109
|
+
fitness: number;
|
|
110
|
+
prevVx: number;
|
|
111
|
+
prevVy: number;
|
|
112
|
+
prevVz: number;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function GauntletRunner({
|
|
116
|
+
generation,
|
|
117
|
+
road,
|
|
118
|
+
slots,
|
|
119
|
+
selectedSlotRef,
|
|
120
|
+
followLeaderRef,
|
|
121
|
+
vehicleRefs,
|
|
122
|
+
jointsRefs,
|
|
123
|
+
clockRef,
|
|
124
|
+
leaderRef,
|
|
125
|
+
distRefs,
|
|
126
|
+
fitRefs,
|
|
127
|
+
}: {
|
|
128
|
+
generation: number;
|
|
129
|
+
road: Road;
|
|
130
|
+
slots: readonly CompiledSlot[];
|
|
131
|
+
selectedSlotRef: React.RefObject<number>;
|
|
132
|
+
followLeaderRef: React.RefObject<boolean>;
|
|
133
|
+
vehicleRefs: React.RefObject<Array<EcctrlVehicleHandle | null>>;
|
|
134
|
+
jointsRefs: React.RefObject<Array<KhrVehicleJointsHandle | null>>;
|
|
135
|
+
clockRef: React.RefObject<HTMLSpanElement | null>;
|
|
136
|
+
leaderRef: React.RefObject<HTMLSpanElement | null>;
|
|
137
|
+
distRefs: React.RefObject<Array<HTMLSpanElement | null>>;
|
|
138
|
+
fitRefs: React.RefObject<Array<HTMLSpanElement | null>>;
|
|
139
|
+
}) {
|
|
140
|
+
const stateRef = useRef<{
|
|
141
|
+
generation: number;
|
|
142
|
+
elapsedS: number;
|
|
143
|
+
bred: boolean;
|
|
144
|
+
progress: LiveSlotProgress[];
|
|
145
|
+
cmd: VehicleInput;
|
|
146
|
+
quat: THREE.Quaternion;
|
|
147
|
+
fwd: THREE.Vector3;
|
|
148
|
+
camTarget: THREE.Vector3;
|
|
149
|
+
camLook: THREE.Vector3;
|
|
150
|
+
desiredCam: THREE.Vector3;
|
|
151
|
+
desiredLook: THREE.Vector3;
|
|
152
|
+
activeFocusSlot: number;
|
|
153
|
+
} | null>(null);
|
|
154
|
+
|
|
155
|
+
if (stateRef.current === null || stateRef.current.generation !== generation) {
|
|
156
|
+
stateRef.current = {
|
|
157
|
+
generation,
|
|
158
|
+
elapsedS: 0,
|
|
159
|
+
bred: false,
|
|
160
|
+
progress: slots.map((s) => ({
|
|
161
|
+
startZ: s.pose.position[2],
|
|
162
|
+
maxDistM: 0,
|
|
163
|
+
peakTimeS: 0.5,
|
|
164
|
+
stallSeconds: 0,
|
|
165
|
+
intactRatio: 1,
|
|
166
|
+
fitness: 5,
|
|
167
|
+
prevVx: 0,
|
|
168
|
+
prevVy: 0,
|
|
169
|
+
prevVz: 0,
|
|
170
|
+
})),
|
|
171
|
+
cmd: createEmptyVehicleCmd(false),
|
|
172
|
+
quat: new THREE.Quaternion(),
|
|
173
|
+
fwd: new THREE.Vector3(),
|
|
174
|
+
camTarget: new THREE.Vector3(0, 8, -14),
|
|
175
|
+
camLook: new THREE.Vector3(0, 2, 12),
|
|
176
|
+
desiredCam: new THREE.Vector3(),
|
|
177
|
+
desiredLook: new THREE.Vector3(),
|
|
178
|
+
activeFocusSlot: 0,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
useBeforePhysicsStep((world) => {
|
|
183
|
+
const st = stateRef.current;
|
|
184
|
+
if (!st || st.bred) return;
|
|
185
|
+
const dt = FIXED_STEP_DT;
|
|
186
|
+
st.elapsedS += dt;
|
|
187
|
+
|
|
188
|
+
let stalledCount = 0;
|
|
189
|
+
let bestSlot = 0;
|
|
190
|
+
let bestFit = -Infinity;
|
|
191
|
+
|
|
192
|
+
for (let i = 0; i < slots.length; i++) {
|
|
193
|
+
const slot = slots[i]!;
|
|
194
|
+
const vh = vehicleRefs.current[i];
|
|
195
|
+
const jh = jointsRefs.current[i];
|
|
196
|
+
const prog = st.progress[i]!;
|
|
197
|
+
const body = vh?.body;
|
|
198
|
+
if (!vh || !body || !isLiveRapierBody(world, body)) continue;
|
|
199
|
+
|
|
200
|
+
const pos = body.translation();
|
|
201
|
+
const rot = body.rotation();
|
|
202
|
+
const lv = body.linvel();
|
|
203
|
+
|
|
204
|
+
// Route hard landing / collision impulses into KHR breakaway panels
|
|
205
|
+
const dv = Math.hypot(lv.x - prog.prevVx, Math.max(0, Math.abs(lv.y - prog.prevVy) - 9.81 * dt * 2), lv.z - prog.prevVz);
|
|
206
|
+
prog.prevVx = lv.x;
|
|
207
|
+
prog.prevVy = lv.y;
|
|
208
|
+
prog.prevVz = lv.z;
|
|
209
|
+
const impNs = dv * Math.max(600, body.mass());
|
|
210
|
+
if (impNs > 380 && jh) {
|
|
211
|
+
for (let r = 0; r < DERBY_PANEL_ROLES.length; r++) {
|
|
212
|
+
const rt = jh.getJointRuntime(DERBY_PANEL_ROLES[r]!);
|
|
213
|
+
if (rt && !rt.detached) {
|
|
214
|
+
rt.applyImpactImpulse(impNs * 0.48);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
let intact: number = DERBY_PANEL_ROLES.length;
|
|
220
|
+
if (jh) {
|
|
221
|
+
intact = 0;
|
|
222
|
+
for (let r = 0; r < DERBY_PANEL_ROLES.length; r++) {
|
|
223
|
+
if (!jh.getJointRuntime(DERBY_PANEL_ROLES[r]!)?.detached) intact += 1;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
prog.intactRatio = intact / DERBY_PANEL_ROLES.length;
|
|
227
|
+
|
|
228
|
+
const distM = Math.max(0, pos.z - prog.startZ);
|
|
229
|
+
if (distM > prog.maxDistM + 0.25) {
|
|
230
|
+
prog.maxDistM = distM;
|
|
231
|
+
prog.peakTimeS = st.elapsedS;
|
|
232
|
+
prog.stallSeconds = 0;
|
|
233
|
+
} else {
|
|
234
|
+
prog.stallSeconds += dt;
|
|
235
|
+
}
|
|
236
|
+
if (prog.stallSeconds > 4.2 || pos.y < -12) {
|
|
237
|
+
stalledCount += 1;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
prog.fitness = computeGauntletFitness({
|
|
241
|
+
maxDistM: prog.maxDistM,
|
|
242
|
+
peakTimeS: prog.peakTimeS,
|
|
243
|
+
intactRatio: prog.intactRatio,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
if (prog.fitness > bestFit) {
|
|
247
|
+
bestFit = prog.fitness;
|
|
248
|
+
bestSlot = i;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Deterministic spline-tracking throttle + steering + periodic 6DOF boost bursts
|
|
252
|
+
const lookS = Math.min(road.length, Math.max(0, pos.z + 14));
|
|
253
|
+
const targetSample = road.at(lookS);
|
|
254
|
+
const dx = targetSample.pos[0] - pos.x;
|
|
255
|
+
const dz = Math.max(2, targetSample.pos[2] - pos.z);
|
|
256
|
+
const aimLen = Math.hypot(dx, dz) || 1;
|
|
257
|
+
const aimX = dx / aimLen;
|
|
258
|
+
const aimZ = dz / aimLen;
|
|
259
|
+
|
|
260
|
+
st.quat.set(rot.x, rot.y, rot.z, rot.w);
|
|
261
|
+
st.fwd.set(0, 0, 1).applyQuaternion(st.quat);
|
|
262
|
+
const fLen = Math.hypot(st.fwd.x, st.fwd.z) || 1;
|
|
263
|
+
const fx = st.fwd.x / fLen;
|
|
264
|
+
const fz = st.fwd.z / fLen;
|
|
265
|
+
const cross = fx * aimZ - fz * aimX;
|
|
266
|
+
|
|
267
|
+
const boostPulse = slot.hasBoosters && Math.floor(st.elapsedS * 1.5 + i * 0.35) % 3 === 0;
|
|
268
|
+
const cmd = st.cmd;
|
|
269
|
+
cmd.forward = true;
|
|
270
|
+
cmd.backward = false;
|
|
271
|
+
cmd.steerLeft = cross < -0.045;
|
|
272
|
+
cmd.steerRight = cross > 0.045;
|
|
273
|
+
cmd.brake = false;
|
|
274
|
+
cmd.throttleUp = boostPulse;
|
|
275
|
+
cmd.boost = boostPulse;
|
|
276
|
+
vh.setMovement(cmd);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
st.activeFocusSlot = followLeaderRef.current ? bestSlot : selectedSlotRef.current;
|
|
280
|
+
|
|
281
|
+
if (st.elapsedS >= MAX_HEAT_SECONDS || (st.elapsedS > 5 && stalledCount >= slots.length)) {
|
|
282
|
+
st.bred = true;
|
|
283
|
+
const fitness = new Float32Array(slots.length);
|
|
284
|
+
for (let i = 0; i < slots.length; i++) {
|
|
285
|
+
fitness[i] = st.progress[i]!.fitness;
|
|
286
|
+
}
|
|
287
|
+
window.dispatchEvent(new CustomEvent("kolmo:evo-complete", { detail: fitness }));
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
useFrame((state, rawDelta) => {
|
|
292
|
+
const st = stateRef.current;
|
|
293
|
+
if (!st) return;
|
|
294
|
+
const delta = Math.min(rawDelta, 0.1);
|
|
295
|
+
|
|
296
|
+
const focusIdx = Math.max(0, Math.min(slots.length - 1, st.activeFocusSlot));
|
|
297
|
+
const focusBody = vehicleRefs.current[focusIdx]?.body;
|
|
298
|
+
if (focusBody) {
|
|
299
|
+
const p = focusBody.translation();
|
|
300
|
+
st.desiredLook.set(p.x, p.y + 1.1, p.z + 3.5);
|
|
301
|
+
st.desiredCam.set(p.x - 4.5, p.y + 5.2, p.z - 11.5);
|
|
302
|
+
damp3(st.camTarget, st.desiredCam, 0.22, delta);
|
|
303
|
+
damp3(st.camLook, st.desiredLook, 0.16, delta);
|
|
304
|
+
state.camera.position.copy(st.camTarget);
|
|
305
|
+
state.camera.lookAt(st.camLook);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Mutate tabular-nums DOM refs directly at display refresh rate — zero React state updates!
|
|
309
|
+
if (clockRef.current) {
|
|
310
|
+
clockRef.current.textContent = `${st.elapsedS.toFixed(1)}s / ${MAX_HEAT_SECONDS}s`;
|
|
311
|
+
}
|
|
312
|
+
let leaderSlot = 0;
|
|
313
|
+
let maxFit = -Infinity;
|
|
314
|
+
for (let i = 0; i < slots.length; i++) {
|
|
315
|
+
const prog = st.progress[i]!;
|
|
316
|
+
const dEl = distRefs.current[i];
|
|
317
|
+
if (dEl) dEl.textContent = `${prog.maxDistM.toFixed(1)}m`;
|
|
318
|
+
const fEl = fitRefs.current[i];
|
|
319
|
+
if (fEl) fEl.textContent = prog.fitness.toFixed(1);
|
|
320
|
+
if (prog.fitness > maxFit) {
|
|
321
|
+
maxFit = prog.fitness;
|
|
322
|
+
leaderSlot = i;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (leaderRef.current && slots[leaderSlot]) {
|
|
326
|
+
leaderRef.current.textContent = `${slots[leaderSlot]!.hex} (${maxFit.toFixed(1)})`;
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export default function App() {
|
|
334
|
+
const course = useMemo(() => buildGauntletCourse(), []);
|
|
335
|
+
|
|
336
|
+
const [generation, setGeneration] = useState(1);
|
|
337
|
+
const [genes, setGenes] = useState<Uint32Array>(() => createInitialPopulation(GAUNTLET_SEED, POP_SIZE));
|
|
338
|
+
const [paused, setPaused] = useState(false);
|
|
339
|
+
const [speed, setSpeed] = useState<1 | 2 | 4>(1);
|
|
340
|
+
const [selectedSlot, setSelectedSlot] = useState(0);
|
|
341
|
+
const [followLeader, setFollowLeader] = useState(true);
|
|
342
|
+
const [hexInput, setHexInput] = useState("0xBFA4D5AC");
|
|
343
|
+
const [injectError, setInjectError] = useState("");
|
|
344
|
+
const [queuedGenes, setQueuedGenes] = useState<number[]>([]);
|
|
345
|
+
|
|
346
|
+
const selectedSlotRef = useRef(selectedSlot);
|
|
347
|
+
selectedSlotRef.current = selectedSlot;
|
|
348
|
+
const followLeaderRef = useRef(followLeader);
|
|
349
|
+
followLeaderRef.current = followLeader;
|
|
350
|
+
|
|
351
|
+
const vehicleRefs = useRef<Array<EcctrlVehicleHandle | null>>(Array.from({ length: POP_SIZE }, () => null));
|
|
352
|
+
const jointsRefs = useRef<Array<KhrVehicleJointsHandle | null>>(Array.from({ length: POP_SIZE }, () => null));
|
|
353
|
+
const clockRef = useRef<HTMLSpanElement>(null);
|
|
354
|
+
const leaderRef = useRef<HTMLSpanElement>(null);
|
|
355
|
+
const distRefs = useRef<Array<HTMLSpanElement | null>>(Array.from({ length: POP_SIZE }, () => null));
|
|
356
|
+
const fitRefs = useRef<Array<HTMLSpanElement | null>>(Array.from({ length: POP_SIZE }, () => null));
|
|
357
|
+
|
|
358
|
+
const slots = useMemo<CompiledSlot[]>(() => {
|
|
359
|
+
const out: CompiledSlot[] = [];
|
|
360
|
+
for (let i = 0; i < genes.length; i++) {
|
|
361
|
+
const gene = genes[i]! >>> 0;
|
|
362
|
+
const bp = unpackKolmogorovGene(gene);
|
|
363
|
+
const bundle = compileKolmogorovCarBundle(gene, { armorScale: 0.85 });
|
|
364
|
+
const laneCol = i % 2 === 0 ? -2.8 : 2.8;
|
|
365
|
+
const rowZ = 6 + Math.floor(i / 2) * 7.5;
|
|
366
|
+
const contactPlaneY = -(2 * bp.tireRadiusM);
|
|
367
|
+
const pose = computeSplineSpawnPose(course.road, rowZ, laneCol, contactPlaneY, 0.05);
|
|
368
|
+
out.push({
|
|
369
|
+
slot: i,
|
|
370
|
+
gene,
|
|
371
|
+
hex: formatKolmogorovGene(gene),
|
|
372
|
+
desc: describeKolmogorovGene(gene),
|
|
373
|
+
bundle,
|
|
374
|
+
pose,
|
|
375
|
+
hasBoosters: bp.addThrusters,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return out;
|
|
379
|
+
}, [genes, course.road]);
|
|
380
|
+
|
|
381
|
+
// StrictMode-safe microtask disposal of compiled KHR car bundles when advancing generations
|
|
382
|
+
const mountsRef = useRef(0);
|
|
383
|
+
useEffect(() => {
|
|
384
|
+
mountsRef.current += 1;
|
|
385
|
+
return () => {
|
|
386
|
+
mountsRef.current -= 1;
|
|
387
|
+
queueMicrotask(() => {
|
|
388
|
+
if (mountsRef.current === 0) {
|
|
389
|
+
for (const s of slots) {
|
|
390
|
+
disposeKhrBundle(s.bundle, { disposeTemplate: true });
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
}, [slots]);
|
|
396
|
+
|
|
397
|
+
const advanceGeneration = useCallback(
|
|
398
|
+
(fitness: Float32Array) => {
|
|
399
|
+
const { nextGenes } = breedNextGeneration(genes, fitness, GAUNTLET_SEED + generation * 977, {
|
|
400
|
+
eliteCount: 2,
|
|
401
|
+
immigrantCount: 2,
|
|
402
|
+
injectedGenes: queuedGenes,
|
|
403
|
+
});
|
|
404
|
+
setQueuedGenes([]);
|
|
405
|
+
setGenes(nextGenes);
|
|
406
|
+
setGeneration((g) => g + 1);
|
|
407
|
+
},
|
|
408
|
+
[genes, generation, queuedGenes],
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
useEffect(() => {
|
|
412
|
+
const onComplete = (event: Event) => {
|
|
413
|
+
const fitness = (event as CustomEvent<Float32Array>).detail;
|
|
414
|
+
if (fitness instanceof Float32Array) {
|
|
415
|
+
advanceGeneration(fitness);
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
window.addEventListener("kolmo:evo-complete", onComplete);
|
|
419
|
+
return () => window.removeEventListener("kolmo:evo-complete", onComplete);
|
|
420
|
+
}, [advanceGeneration]);
|
|
421
|
+
|
|
422
|
+
useEffect(() => {
|
|
423
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
424
|
+
const tag = document.activeElement?.tagName;
|
|
425
|
+
if (tag === "INPUT" || tag === "TEXTAREA") return;
|
|
426
|
+
if (e.code === "Space" && tag !== "BUTTON" && !e.repeat) {
|
|
427
|
+
e.preventDefault();
|
|
428
|
+
setPaused((p) => !p);
|
|
429
|
+
} else if (e.code === "Digit1") {
|
|
430
|
+
setSpeed(1);
|
|
431
|
+
} else if (e.code === "Digit2") {
|
|
432
|
+
setSpeed(2);
|
|
433
|
+
} else if (e.code === "Digit4") {
|
|
434
|
+
setSpeed(4);
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
window.addEventListener("keydown", onKeyDown);
|
|
438
|
+
return () => window.removeEventListener("keydown", onKeyDown);
|
|
439
|
+
}, []);
|
|
440
|
+
|
|
441
|
+
const handleInject = () => {
|
|
442
|
+
const parsed = parseKolmogorovGene(hexInput);
|
|
443
|
+
if (parsed === null) {
|
|
444
|
+
setInjectError("Enter a valid 32-bit hex gene (0x00000000..0xFFFFFFFF).");
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
setInjectError("");
|
|
448
|
+
const next = new Uint32Array(genes);
|
|
449
|
+
next[selectedSlot] = parsed;
|
|
450
|
+
setQueuedGenes((prev) => [parsed, ...prev.slice(0, 1)]);
|
|
451
|
+
setGenes(next);
|
|
452
|
+
setFollowLeader(false);
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
const handleExportGlb = async () => {
|
|
456
|
+
const target = slots[selectedSlot] ?? slots[0];
|
|
457
|
+
if (!target) return;
|
|
458
|
+
const bytes = await exportKolmogorovCarGlb(target.gene);
|
|
459
|
+
const copy = new Uint8Array(bytes.byteLength);
|
|
460
|
+
copy.set(bytes);
|
|
461
|
+
const blob = new Blob([copy.buffer], { type: "model/gltf-binary" });
|
|
462
|
+
const url = URL.createObjectURL(blob);
|
|
463
|
+
const a = document.createElement("a");
|
|
464
|
+
a.href = url;
|
|
465
|
+
a.download = `kolmo-${target.hex}.glb`;
|
|
466
|
+
a.click();
|
|
467
|
+
URL.revokeObjectURL(url);
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
const activeSpec = slots[selectedSlot] ?? slots[0];
|
|
471
|
+
|
|
472
|
+
return (
|
|
473
|
+
<>
|
|
474
|
+
<div className="evo-stage">
|
|
475
|
+
<Game skybox="dusk" timeScale={paused ? 0 : speed}>
|
|
476
|
+
<Terrain
|
|
477
|
+
heights={course.heights}
|
|
478
|
+
segments={[GAUNTLET_ROWS_Z, GAUNTLET_COLS_X]}
|
|
479
|
+
size={[GAUNTLET_SIZE_X, GAUNTLET_SIZE_Z]}
|
|
480
|
+
position={course.terrainPos}
|
|
481
|
+
color="#444b3e"
|
|
482
|
+
/>
|
|
483
|
+
<RoadRibbon road={course.road} />
|
|
484
|
+
<GauntletRunner
|
|
485
|
+
key={`runner-${generation}`}
|
|
486
|
+
generation={generation}
|
|
487
|
+
road={course.road}
|
|
488
|
+
slots={slots}
|
|
489
|
+
selectedSlotRef={selectedSlotRef}
|
|
490
|
+
followLeaderRef={followLeaderRef}
|
|
491
|
+
vehicleRefs={vehicleRefs}
|
|
492
|
+
jointsRefs={jointsRefs}
|
|
493
|
+
clockRef={clockRef}
|
|
494
|
+
leaderRef={leaderRef}
|
|
495
|
+
distRefs={distRefs}
|
|
496
|
+
fitRefs={fitRefs}
|
|
497
|
+
/>
|
|
498
|
+
{slots.map((slot, idx) => (
|
|
499
|
+
<KhrVehicle
|
|
500
|
+
key={`gen-${generation}-slot-${slot.slot}-${slot.hex}`}
|
|
501
|
+
bundle={slot.bundle}
|
|
502
|
+
position={slot.pose.position}
|
|
503
|
+
quaternion={slot.pose.quaternion}
|
|
504
|
+
flightConfig={slot.hasBoosters ? { controlMode: "6DOF", linearDamping6DOF: 0 } : undefined}
|
|
505
|
+
vehicleRef={(h) => {
|
|
506
|
+
vehicleRefs.current[idx] = h;
|
|
507
|
+
}}
|
|
508
|
+
jointsRef={(jh) => {
|
|
509
|
+
jointsRefs.current[idx] = jh;
|
|
510
|
+
}}
|
|
511
|
+
/>
|
|
512
|
+
))}
|
|
513
|
+
</Game>
|
|
514
|
+
</div>
|
|
515
|
+
|
|
516
|
+
<div className="evo-topbar">
|
|
517
|
+
<span className="evo-label">GEN</span>
|
|
518
|
+
<span className="evo-val">{String(generation).padStart(3, "0")}</span>
|
|
519
|
+
<span className="evo-sep" />
|
|
520
|
+
<span className="evo-label">HEAT</span>
|
|
521
|
+
<span ref={clockRef} className="evo-cyan">
|
|
522
|
+
0.0s / 24s
|
|
523
|
+
</span>
|
|
524
|
+
<span className="evo-sep" />
|
|
525
|
+
<span className="evo-label">LEADER</span>
|
|
526
|
+
<span ref={leaderRef} className="evo-val">
|
|
527
|
+
{slots[0]?.hex ?? "0x00000000"}
|
|
528
|
+
</span>
|
|
529
|
+
</div>
|
|
530
|
+
|
|
531
|
+
<aside className="evo-dock" aria-label="Evolution CAD Dock">
|
|
532
|
+
<div className="evo-dock-section">
|
|
533
|
+
<div className="evo-controls">
|
|
534
|
+
<button type="button" className={paused ? "evo-btn active" : "evo-btn"} onClick={() => setPaused((p) => !p)}>
|
|
535
|
+
{paused ? "RESUME [SPACE]" : "PAUSE [SPACE]"}
|
|
536
|
+
</button>
|
|
537
|
+
{([1, 2, 4] as const).map((s) => (
|
|
538
|
+
<button
|
|
539
|
+
key={s}
|
|
540
|
+
type="button"
|
|
541
|
+
className={speed === s ? "evo-btn active" : "evo-btn"}
|
|
542
|
+
onClick={() => setSpeed(s)}
|
|
543
|
+
>
|
|
544
|
+
{s}x
|
|
545
|
+
</button>
|
|
546
|
+
))}
|
|
547
|
+
<button
|
|
548
|
+
type="button"
|
|
549
|
+
className={followLeader ? "evo-btn active" : "evo-btn"}
|
|
550
|
+
onClick={() => setFollowLeader((f) => !f)}
|
|
551
|
+
>
|
|
552
|
+
{followLeader ? "CAM: LEADER" : `CAM: #${selectedSlot + 1}`}
|
|
553
|
+
</button>
|
|
554
|
+
</div>
|
|
555
|
+
</div>
|
|
556
|
+
|
|
557
|
+
<div className="evo-standings" role="listbox" aria-label="Population genomes">
|
|
558
|
+
{slots.map((slot, idx) => (
|
|
559
|
+
<button
|
|
560
|
+
key={slot.slot}
|
|
561
|
+
type="button"
|
|
562
|
+
role="option"
|
|
563
|
+
aria-selected={selectedSlot === idx}
|
|
564
|
+
className={selectedSlot === idx ? "evo-row selected" : "evo-row"}
|
|
565
|
+
onClick={() => {
|
|
566
|
+
setSelectedSlot(idx);
|
|
567
|
+
setFollowLeader(false);
|
|
568
|
+
setHexInput(slot.hex);
|
|
569
|
+
}}
|
|
570
|
+
>
|
|
571
|
+
<span className="evo-slot">#{idx + 1}</span>
|
|
572
|
+
<span className="evo-gene">{slot.hex}</span>
|
|
573
|
+
<span
|
|
574
|
+
ref={(el) => {
|
|
575
|
+
distRefs.current[idx] = el;
|
|
576
|
+
}}
|
|
577
|
+
className="evo-dist"
|
|
578
|
+
>
|
|
579
|
+
0.0m
|
|
580
|
+
</span>
|
|
581
|
+
<span
|
|
582
|
+
ref={(el) => {
|
|
583
|
+
fitRefs.current[idx] = el;
|
|
584
|
+
}}
|
|
585
|
+
className="evo-fit"
|
|
586
|
+
>
|
|
587
|
+
0.0
|
|
588
|
+
</span>
|
|
589
|
+
</button>
|
|
590
|
+
))}
|
|
591
|
+
</div>
|
|
592
|
+
|
|
593
|
+
<div className="evo-dock-section">
|
|
594
|
+
<div className="evo-spec">{activeSpec ? activeSpec.desc : ""}</div>
|
|
595
|
+
</div>
|
|
596
|
+
|
|
597
|
+
<div className="evo-dock-section">
|
|
598
|
+
<div className="evo-inject">
|
|
599
|
+
<input
|
|
600
|
+
type="text"
|
|
601
|
+
className="evo-input"
|
|
602
|
+
aria-label="32-bit Kolmogorov hex gene"
|
|
603
|
+
value={hexInput}
|
|
604
|
+
onChange={(e) => setHexInput(e.target.value)}
|
|
605
|
+
placeholder="0xBFA4D5AC"
|
|
606
|
+
/>
|
|
607
|
+
<button type="button" className="evo-btn" onClick={handleInject}>
|
|
608
|
+
INJECT HEX
|
|
609
|
+
</button>
|
|
610
|
+
<button type="button" className="evo-btn primary" onClick={() => void handleExportGlb()}>
|
|
611
|
+
EXPORT .GLB
|
|
612
|
+
</button>
|
|
613
|
+
</div>
|
|
614
|
+
{injectError ? <div className="evo-error">{injectError}</div> : null}
|
|
615
|
+
</div>
|
|
616
|
+
</aside>
|
|
617
|
+
</>
|
|
618
|
+
);
|
|
619
|
+
}
|