class_name WorldGen extends RefCounted # Deterministic procedural world generation: the world is divided into # square chunks, and each chunk's biome is derived from a seeded # low-frequency noise field sampled at the chunk's own coordinates - so # revisiting the same chunk always yields the same biome, and biomes form # smooth multi-chunk regions instead of a per-chunk random checkerboard. enum Biome { SPACE, ASTEROID_FIELD } const CHUNK_SIZE: float = 1200.0 const NOISE_SEED: int = 1337 const ASTEROID_FIELD_THRESHOLD: float = 0.15 const PLANET_THRESHOLD: float = 0.33 const PLANET_NAMES: Array = [ "Kepler's Rest", "Ceres Minor", "Vesta's Shadow", "Ironhold", "Drift Anchor", "Stonefall", "Grey Watch", "Silent Quarry", "Baros Rock", "Umbra Point", ] const GAS_GIANT_NAMES: Array = ["Aurelia", "Boreas", "Cerulean Deep", "Helios Major", "Vesper"] static var _noise: FastNoiseLite static var _planet_noise: FastNoiseLite static func _get_noise() -> FastNoiseLite: if _noise == null: _noise = FastNoiseLite.new() _noise.seed = NOISE_SEED _noise.frequency = 0.06 return _noise static func _get_planet_noise() -> FastNoiseLite: if _planet_noise == null: _planet_noise = FastNoiseLite.new() _planet_noise.seed = NOISE_SEED + 777 # Higher frequency than the biome noise so it spikes on individual # chunks rather than forming its own smooth multi-chunk regions - # "once or twice per asteroid biome [region]" needs a sparse, mostly # independent chance per chunk, not another smooth blob. _planet_noise.frequency = 0.35 return _planet_noise static func chunk_coords(world_pos: Vector2) -> Vector2i: return Vector2i(int(floor(world_pos.x / CHUNK_SIZE)), int(floor(world_pos.y / CHUNK_SIZE))) static func chunk_origin(chunk: Vector2i) -> Vector2: return Vector2(chunk.x * CHUNK_SIZE, chunk.y * CHUNK_SIZE) static func biome_at_chunk(chunk: Vector2i) -> int: var n: float = _get_noise().get_noise_2d(float(chunk.x), float(chunk.y)) if n > ASTEROID_FIELD_THRESHOLD: return Biome.ASTEROID_FIELD return Biome.SPACE static func has_planet_at_chunk(chunk: Vector2i) -> bool: if biome_at_chunk(chunk) != Biome.ASTEROID_FIELD: return false var n: float = _get_planet_noise().get_noise_2d(float(chunk.x), float(chunk.y)) return n > PLANET_THRESHOLD static func planet_data_at_chunk(chunk: Vector2i) -> Dictionary: # Single source of truth for a planet's identity within its chunk - # seeded purely from the chunk's own coordinates (same pattern as the # biome/planet noise fields above) so it's reproducible from anywhere, # without needing an actual spawned Planet node. Both PlanetField (to # actually spawn the planet) and the world map (to draw a marker for # an explored-but-not-currently-loaded chunk) call this, so the two # can never drift out of sync with each other. var rng: RandomNumberGenerator = RandomNumberGenerator.new() rng.seed = hash(chunk) var local_pos: Vector2 = Vector2(rng.randf() * CHUNK_SIZE, rng.randf() * CHUNK_SIZE) var planet_name: String = PLANET_NAMES[rng.randi() % PLANET_NAMES.size()] var shape_seed: int = rng.randi() # A stable per-chunk roll makes gas giants persistent landmarks rather # than a different resource whenever the scene reloads. var gas_giant: bool = rng.randf() < 0.28 if gas_giant: planet_name = GAS_GIANT_NAMES[rng.randi() % GAS_GIANT_NAMES.size()] return {"local_pos": local_pos, "name": planet_name, "shape_seed": shape_seed, "planet_type": "gas_giant" if gas_giant else "rocky", "resource_type": "gas" if gas_giant else "ore"} static func biome_color(biome: int) -> Color: match biome: Biome.ASTEROID_FIELD: return Color(0.55, 0.36, 0.18, 1.0) _: return Color(0.10, 0.13, 0.28, 1.0)