Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Duck Race stream: every seed lays rocks, eddies and fast water inside the stream, checked across seeds in `duck-race/stream.test.mjs` #290
Check · Review · Land · Live done — Landed and live in production.
torque/pixel-plays:torque/286-duck-race-stream-every-seed-lays-rocks-e → main 2 commits fork 3 threads · 3 unresolved
2 files
changed +59
−2
duck-race/stream.js +16 −1
| @@ -18,6 +18,10 @@ | ||
| 18 | 18 | const FAST_PATCHES = 8; // patches of fast water in the stream |
| 19 | 19 | const EDDY_HOLD = 1.5; // seconds an eddy holds a duck |
| 20 | 20 | const STEER_COOLDOWN = 0.4; // seconds steering is refused after a lane change |
| 21 | // Guarantee: every seed has at least one rock, one eddy and one patch of fast water (FAST_PATCHES >= 1), so every | |
| 22 | // stream is a race. A seed that draws fewer than two obstacles from SLOT, FIRST_SLOT and OBSTACLE_CHANCE gets more | |
| 23 | // on free slots, scanning on from a seeded slot; a seed with only one kind has its first obstacle turned into the | |
| 24 | // missing kind. Seeds that already had both are untouched, and it all stays a pure function of the seed. | |
| 21 | 25 | const SLOT = 5; // spacing of obstacle slots |
| 22 | 26 | const FIRST_SLOT = 2; // no obstacle before this slot, so the start is clear |
| 23 | 27 | const OBSTACLE_CHANCE = 0.6; // chance a slot holds a rock or an eddy |
| @@ -52,6 +56,17 @@ | ||
| 52 | 56 | const lane = Math.floor(noise(seed, slot++) * LANES); |
| 53 | 57 | obstacles.push({ id: obstacles.length, kind, lane, at: k * SLOT }); |
| 54 | 58 | } |
| 59 | // the guarantee of a rock, an eddy and fast water in every seed is stated beside SLOT, above | |
| 60 | const slots = LENGTH / SLOT - FIRST_SLOT; | |
| 61 | for (let j = 0; obstacles.length < 2 && j < slots; j++) { | |
| 62 | const k = FIRST_SLOT + (Math.floor(noise(seed, 700) * slots) + j) % slots; | |
| 63 | if (obstacles.some(o => o.at === k * SLOT)) continue; | |
| 64 | obstacles.push({ id: obstacles.length, kind: 'rock', lane: Math.floor(noise(seed, 701 + j) * LANES), at: k * SLOT }); | |
| 65 | } | |
| 66 | obstacles.sort((a, b) => a.at - b.at).forEach((o, i) => { o.id = i; }); | |
| 67 | for (const kind of ['rock', 'eddy']) { | |
| 68 | if (!obstacles.some(o => o.kind === kind)) obstacles.find(o => o.kind !== kind).kind = kind; | |
| 69 | } | |
| 55 | 70 | for (let i = 0; i < FAST_PATCHES; i++) { |
| 56 | 71 | const lane = Math.floor(noise(seed, 500 + i * 2) * LANES); |
| 57 | 72 | const from = round3(noise(seed, 501 + i * 2) * (LENGTH - FAST_LENGTH)); |
| @@ -177,7 +192,7 @@ | ||
| 177 | 192 | |
| 178 | 193 | const api = { |
| 179 | 194 | LENGTH, LANES, TIME_LIMIT, BASE_SPEED, FAST_FACTOR, EDDY_HOLD, STEER_COOLDOWN, RIVALS, LOOKAHEAD, WIN_SCORE, |
| 180 | CLEAN_BONUS, layout, rivalTurn, start, tick, steer, | |
| 195 | CLEAN_BONUS, SLOT, FIRST_SLOT, layout, rivalTurn, start, tick, steer, | |
| 181 | 196 | }; |
| 182 | 197 | if (typeof module !== 'undefined') module.exports = api; else root.DuckRace = api; |
| 183 | 198 | })(typeof window !== 'undefined' ? window : globalThis); |
duck-race/stream.test.mjs +43 −1
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | import { test, expect } from 'bun:test'; |
| 2 | 2 | import { |
| 3 | 3 | LENGTH, LANES, TIME_LIMIT, BASE_SPEED, FAST_FACTOR, EDDY_HOLD, STEER_COOLDOWN, RIVALS, LOOKAHEAD, WIN_SCORE, |
| 4 | CLEAN_BONUS, layout, rivalTurn, start, tick, steer, | |
| 4 | CLEAN_BONUS, SLOT, FIRST_SLOT, layout, rivalTurn, start, tick, steer, | |
| 5 | 5 | } from './stream.js'; |
| 6 | 6 | |
| 7 | 7 | // a stream with hand-placed obstacles, so each rule is checked against a count done by hand |
| @@ -167,3 +167,45 @@ test('across seeds, no rival is left stuck on a rock at the end of a long run', | ||
| 167 | 167 | expect(end.ducks.slice(1).every(d => d.finishTime !== null)).toBe(true); |
| 168 | 168 | } |
| 169 | 169 | }); |
| 170 | ||
Add a check that o.id === index for every obstacle, and a case for the sparse-seed branch of the fix. |
||
| 171 | test('every seed from 1 to 200 gives a real race: rocks, eddies and fast water inside the stream, ducks on the line', () => { | |
| 172 | for (let seed = 1; seed <= 200; seed++) { | |
| 173 | const s = start(seed); | |
| 174 | expect(start(seed)).toEqual(s); | |
| 175 | const why = `seed ${seed}`; | |
| 176 | if (!s.obstacles.some(o => o.kind === 'rock')) throw new Error(`${why}: no rock`); | |
| 177 | if (!s.obstacles.some(o => o.kind === 'eddy')) throw new Error(`${why}: no eddy`); | |
| 178 | if (s.fast.length < 1) throw new Error(`${why}: no fast water`); | |
| 179 | for (const f of s.fast) { | |
| 180 | expect(Number.isInteger(f.lane) && f.lane >= 0 && f.lane <= LANES - 1).toBe(true); | |
| 181 | expect(f.from).toBeGreaterThanOrEqual(0); | |
| 182 | expect(f.from).toBeLessThan(f.to); | |
| 183 | expect(f.to).toBeLessThanOrEqual(LENGTH); | |
| 184 | } | |
| 185 | s.obstacles.forEach((o, i) => expect(o.id).toBe(i)); | |
| 186 | for (const o of s.obstacles) { | |
| 187 | expect(o.at).toBeGreaterThanOrEqual(FIRST_SLOT * SLOT); | |
| 188 | expect(o.at).toBeLessThan(LENGTH); | |
| 189 | expect(o.lane >= 0 && o.lane < LANES && Number.isInteger(o.lane)).toBe(true); | |
| 190 | } | |
| 191 | expect(s.ducks.length).toBe(RIVALS + 1); | |
| 192 | expect(s.ducks.map(d => d.id)).toEqual([0, 1, 2, 3]); | |
| 193 | expect(s.ducks.every(d => d.x === 0)).toBe(true); | |
| 194 | expect(new Set(s.ducks.map(d => d.lane)).size).toBe(RIVALS + 1); | |
| 195 | expect(s.ducks.filter(d => d.id === 0).length).toBe(1); | |
| 196 | } | |
| 197 | }); | |
| 198 | ||
| 199 | test('a seed whose base draw holds fewer than two obstacles still gets a rock and an eddy, in order, on free slots', () => { | |
| 200 | // found by scanning: from the base rules these seeds draw exactly one obstacle (the scan is in the pull) | |
| 201 | for (const seed of [675585, 950906, 1555868]) { | |
| 202 | const s = start(seed); | |
| 203 | expect(s.obstacles.length).toBeGreaterThanOrEqual(2); | |
| 204 | expect(s.obstacles.some(o => o.kind === 'rock') && s.obstacles.some(o => o.kind === 'eddy')).toBe(true); | |
| 205 | s.obstacles.forEach((o, i) => expect(o.id).toBe(i)); | |
| 206 | expect(s.obstacles.map(o => o.at)).toEqual(s.obstacles.map(o => o.at).sort((a, b) => a - b)); | |
| 207 | expect(new Set(s.obstacles.map(o => o.at)).size).toBe(s.obstacles.length); | |
| 208 | for (const o of s.obstacles) { expect(o.at).toBeGreaterThanOrEqual(FIRST_SLOT * SLOT); expect(o.at).toBeLessThan(LENGTH); } | |
| 209 | expect(start(seed)).toEqual(s); | |
| 210 | } | |
| 211 | }); |
new above
offline
This sparse-seed loop has no test that reaches it. Either find a seed that needs it and test it, or delete it.