56 lines
1.6 KiB
JavaScript
56 lines
1.6 KiB
JavaScript
import { fnv1a32, hash2u32, mulberry32 } from "../../public/src/rng.js";
|
|
import { generatePlanet, formatPlanet } from "../../public/src/planetGeneration.js";
|
|
|
|
const GRID_W = 100;
|
|
const GRID_H = 100;
|
|
const OUTER_RADIUS = 50;
|
|
const INNER_RADIUS = 30;
|
|
const PLANET_CHANCE = 0.1;
|
|
|
|
export function cellCenter(x, y) {
|
|
const cx = (GRID_W - 1) / 2;
|
|
const cy = (GRID_H - 1) / 2;
|
|
return { dx: x - cx, dy: y - cy };
|
|
}
|
|
|
|
export function isExploitable(x, y) {
|
|
const { dx, dy } = cellCenter(x, y);
|
|
const r = Math.hypot(dx, dy);
|
|
return r <= OUTER_RADIUS - 0.5 && r >= INNER_RADIUS - 0.5;
|
|
}
|
|
|
|
export function hasPlanetAt(x, y, seedU32) {
|
|
const h = hash2u32(x, y, seedU32);
|
|
return h / 4294967296 < PLANET_CHANCE;
|
|
}
|
|
|
|
export function computeCell(seedStr, x, y) {
|
|
const seedU32 = fnv1a32(seedStr);
|
|
const exploitable = isExploitable(x, y);
|
|
if (!exploitable) {
|
|
return { x, y, exploitable: false, hasPlanet: false, planet: null, formatted: null };
|
|
}
|
|
const hp = hasPlanetAt(x, y, seedU32);
|
|
if (!hp) {
|
|
return { x, y, exploitable: true, hasPlanet: false, planet: null, formatted: null };
|
|
}
|
|
const h = hash2u32(x, y, seedU32 ^ 0xa5a5a5a5);
|
|
const rng = mulberry32(h);
|
|
const planet = generatePlanet(rng);
|
|
return {
|
|
x, y, exploitable: true, hasPlanet: true,
|
|
planet, formatted: formatPlanet(planet),
|
|
};
|
|
}
|
|
|
|
export function rowToCellPayload(row) {
|
|
return {
|
|
x: row.x,
|
|
y: row.y,
|
|
exploitable: row.exploitable,
|
|
hasPlanet: row.has_planet,
|
|
planet: row.planet_json ?? null,
|
|
discoveredBy: row.discovered_by,
|
|
capturedBy: row.captured_by ?? null,
|
|
};
|
|
} |