game: seeded planet/moon generation per system + deterministic exploration roll
This commit is contained in:
@@ -123,3 +123,105 @@ export function generateRing(
|
||||
const systems = placeRing(rng, anchorX, anchorY, EXPAND_RING, takenNames, takenPositions)
|
||||
return { systems, edges: ringEdges(systems.length) }
|
||||
}
|
||||
|
||||
// --- Bodies (planets + moons) and exploration -------------------------------
|
||||
|
||||
export interface WorldMoon {
|
||||
name: string
|
||||
type: string
|
||||
hazard: boolean
|
||||
}
|
||||
|
||||
export interface WorldPlanet {
|
||||
name: string
|
||||
type: string
|
||||
hazard: boolean
|
||||
moons: WorldMoon[]
|
||||
}
|
||||
|
||||
// Derive an independent PRNG stream for one entity from the save seed, so a system's bodies
|
||||
// and a planet's exploration haul are reproducible offline (tests recompute them directly).
|
||||
export function bodyRng(seed: string, systemId: number): () => number {
|
||||
return mulberry32((Number(seed) + Math.imul(systemId, 0x9e3779b1)) >>> 0)
|
||||
}
|
||||
|
||||
export function explorationRng(seed: string, planetId: number): () => number {
|
||||
return mulberry32((Number(seed) + Math.imul(planetId, 0x85ebca6b)) >>> 0)
|
||||
}
|
||||
|
||||
const BODY_NAMES = [
|
||||
'AURELLE', 'BRIMSTONE', 'CINDERROCK', 'DRIFTBAND', 'PALEMOON', 'ASHFALL', 'VESPERINE', 'KORR',
|
||||
'MIRELLA', 'SABLET', 'EMBERLING', 'FROSTHOLD', 'GALEN', 'IRONVEIN', 'JUNO', 'KESTREL',
|
||||
'LUMEN', 'NADIR', 'OPALINE', 'PYRE', 'QUILL', 'RUSTBARK', 'SIROCCO', 'TARN',
|
||||
'UMBRA', 'VELVETINE', 'WYRM', 'ZEPHYR', 'HALCYON', 'MORROW', 'ONYX', 'THALASSA',
|
||||
]
|
||||
|
||||
const PLANET_TYPES = ['TERRESTRIAL', 'GAS GIANT', 'ROCKY', 'ICE WORLD', 'VOLCANIC', 'OCEAN', 'ASTEROID FIELD']
|
||||
const MOON_TYPES = ['MOON', 'ICE MOON', 'VOLCANIC MOON']
|
||||
|
||||
const HAZARD_CHANCE = 0.25
|
||||
const MOON_CHANCE = 0.6 // a planet has any moons at all
|
||||
const PLANET_COUNT_MIN = 2
|
||||
const PLANET_COUNT_SPREAD = 4 // 2..5 planets per system
|
||||
|
||||
// A body name unique within its system (planets and moons share one pool). Max bodies per
|
||||
// system is 5 + 5*3 = 20 < BODY_NAMES.length, so the loop always terminates.
|
||||
function makeBodyName(rng: () => number, taken: Set<string>): string {
|
||||
for (;;) {
|
||||
const name = title(BODY_NAMES[Math.floor(rng() * BODY_NAMES.length)])
|
||||
if (!taken.has(name)) {
|
||||
taken.add(name)
|
||||
return name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function pick<T>(rng: () => number, arr: T[]): T {
|
||||
return arr[Math.floor(rng() * arr.length)]
|
||||
}
|
||||
|
||||
// The bodies of one system: 2–5 planets, some with 1–3 moons. Pure — the caller derives the
|
||||
// rng from (save seed, system id) so a given system always gets the same bodies.
|
||||
export function generatePlanets(rng: () => number): WorldPlanet[] {
|
||||
const taken = new Set<string>()
|
||||
const planets: WorldPlanet[] = []
|
||||
for (let i = 0; i < PLANET_COUNT_MIN + Math.floor(rng() * PLANET_COUNT_SPREAD); i++) {
|
||||
const name = makeBodyName(rng, taken)
|
||||
const type = pick(rng, PLANET_TYPES)
|
||||
const hazard = rng() < HAZARD_CHANCE
|
||||
const moons: WorldMoon[] = []
|
||||
if (rng() < MOON_CHANCE) {
|
||||
for (let m = 0; m < 1 + Math.floor(rng() * 3); m++) {
|
||||
moons.push({ name: makeBodyName(rng, taken), type: pick(rng, MOON_TYPES), hazard: rng() < HAZARD_CHANCE })
|
||||
}
|
||||
}
|
||||
planets.push({ name, type, hazard, moons })
|
||||
}
|
||||
return planets
|
||||
}
|
||||
|
||||
export interface FoundItem {
|
||||
kind: 'resource' | 'part'
|
||||
name: string
|
||||
qty: number
|
||||
}
|
||||
|
||||
const RESOURCE_NAMES = ['IRON ORE', 'CRYSTAL SHARDS', 'GAS CELLS', 'WATER ICE', 'RARE EARTH']
|
||||
const PART_NAMES = ['NAV MODULE', 'HULL PLATE', 'REACTOR CORE', 'COMMS ARRAY', 'ENGINE TUNING']
|
||||
|
||||
const RESOURCE_CHANCE = 0.7
|
||||
const PART_CHANCE = 0.3 // parts are rarer than resources
|
||||
|
||||
// The haul from scanning one body: a resource and/or a part, or nothing (~21% of scans).
|
||||
// Pure — the caller derives the rng from (save seed, planet id), so the same save+planet
|
||||
// always yields the same outcome.
|
||||
export function rollExploration(rng: () => number): FoundItem[] {
|
||||
const found: FoundItem[] = []
|
||||
if (rng() < RESOURCE_CHANCE) {
|
||||
found.push({ kind: 'resource', name: pick(rng, RESOURCE_NAMES), qty: 1 + Math.floor(rng() * 4) }) // 1..4
|
||||
}
|
||||
if (rng() < PART_CHANCE) {
|
||||
found.push({ kind: 'part', name: pick(rng, PART_NAMES), qty: 1 })
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user