Files
tidewall/test/sim/island_survey.gd
T
claudeandClaude Opus 5.5 c70d0c1c4f W2: Procedural island generator
Island.generate(seed) builds a 513 x 513 heightfield (2 m spacing, the whole
1,024 m world) from the match seed's "worldgen" stream: a warped coastline,
a coastal shelf and interior dome, two to four summits joined by ridges,
rolling and rocky detail, and flattened marsh in wet lowland. Land is scaled
so the tallest summit lands between ISLAND_PEAK_MIN and ISLAND_PEAK_MAX,
under the last tide. Each sample gets an elevation band (sea, beach, marsh,
forest, highland, summit) for terrain materials.

- Simulation generates the island unless a test supplies its own ground,
  and spawns players on dry land until the drop arrives.
- The headless server logs world_hash in its start line.
- Island.heightmap_hash() is SHA-256 over millimetre heights and bands.
- tools/island_map.gd draws a top-down map with tide contours.
- New island constants in tuning.gd; GDD gains an island section.

Tests: a 100-seed sweep checks generation time (slowest 617 ms against
5 s), land area, at least two distinct high points, walkable land, bands,
and that no more than 3% of dry land is cut off at sea levels 0 and 8 m
(worst seen 0.87%). A separate server process given only the seed must
log the same heightmap hash.

Closes #9

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 14:15:59 -05:00

134 lines
4.7 KiB
GDScript

## Measures a generated island for the worldgen tests: land area, how land splits up as
## the sea rises, and where the separate high points are. It reads every other
## heightfield sample (4 m apart), which is plenty for kilometre-scale questions.
class_name IslandSurvey
extends RefCounted
const STRIDE: int = 2
const NEIGHBOURS: Array[Vector2i] = [Vector2i(1, 0), Vector2i(-1, 0), Vector2i(0, 1), Vector2i(0, -1)]
var island: Island
var side: int = 0
var spacing: float = 0.0
var heights: PackedFloat32Array = PackedFloat32Array()
func _init(target: Island) -> void:
island = target
var ground: SimGround = target.ground
side = (ground.samples - 1) / STRIDE + 1
spacing = ground.cell * STRIDE
heights.resize(side * side)
for gz: int in side:
for gx: int in side:
heights[gz * side + gx] = ground.heights[gz * STRIDE * ground.samples + gx * STRIDE]
## Fraction of the world above `level`.
func land_fraction(level: float) -> float:
var count: int = 0
for value: float in heights:
if value > level:
count += 1
return float(count) / heights.size()
## How the land splits when the sea stands at `level`. Water floods inward from the
## world edge through every cell at or below the level; low ground the sea can't reach
## stays dry, as the GDD says. Returns the dry area, the largest connected dry piece,
## and the number of dry pieces.
func dry_land(level: float) -> Dictionary:
var flooded: PackedByteArray = PackedByteArray()
flooded.resize(side * side)
var queue: PackedInt32Array = PackedInt32Array()
for index: int in side:
for edge: int in [index, (side - 1) * side + index, index * side, index * side + side - 1]:
if heights[edge] <= level and flooded[edge] == 0:
flooded[edge] = 1
queue.append(edge)
_spread(queue, flooded, func(cell: int) -> bool: return heights[cell] <= level)
var seen: PackedByteArray = flooded.duplicate()
var total: int = 0
var largest: int = 0
var pieces: int = 0
for start: int in side * side:
if seen[start] != 0:
continue
seen[start] = 1
var piece: PackedInt32Array = PackedInt32Array([start])
var size: int = _spread(piece, seen, func(_cell: int) -> bool: return true)
total += size
largest = maxi(largest, size)
pieces += 1
return {"dry": total, "largest": largest, "pieces": pieces}
## Separate high points: cells that are the highest within `radius` metres and at least
## `min_height` high, each with a valley of at least `drop` metres between it and every
## higher peak (measured along the straight line between them).
func peaks(min_height: float, radius: float, drop: float) -> Array[Vector3]:
var reach: int = ceili(radius / spacing)
var found: Array[Vector3] = []
for gz: int in side:
for gx: int in side:
var height: float = heights[gz * side + gx]
if height < min_height or not _local_max(gx, gz, height, reach):
continue
found.append(Vector3(gx * spacing, height, gz * spacing))
found.sort_custom(func(a: Vector3, b: Vector3) -> bool: return a.y > b.y)
var distinct: Array[Vector3] = []
for peak: Vector3 in found:
var separate: bool = true
for higher: Vector3 in distinct:
if _lowest_between(peak, higher) > peak.y - drop:
separate = false
break
if separate:
distinct.append(peak)
return distinct
func _local_max(gx: int, gz: int, height: float, reach: int) -> bool:
for dz: int in range(-reach, reach + 1):
for dx: int in range(-reach, reach + 1):
var x: int = gx + dx
var z: int = gz + dz
if x < 0 or z < 0 or x >= side or z >= side or (dx == 0 and dz == 0):
continue
var other: float = heights[z * side + x]
# Ties go to the lower index, so a flat top yields one peak.
if other > height or (other == height and z * side + x < gz * side + gx):
return false
return true
func _lowest_between(a: Vector3, b: Vector3) -> float:
var lowest: float = INF
var steps: int = ceili(Vector2(a.x, a.z).distance_to(Vector2(b.x, b.z)) / spacing)
for step: int in steps + 1:
var point: Vector3 = a.lerp(b, float(step) / maxf(steps, 1))
lowest = minf(lowest, island.ground.terrain_height(point.x, point.z))
return lowest
## Flood fill from the cells in `queue`, marking `marks`, through cells `admit` accepts.
## Returns how many cells were reached, including the starting ones.
func _spread(queue: PackedInt32Array, marks: PackedByteArray, admit: Callable) -> int:
var head: int = 0
while head < queue.size():
var cell: int = queue[head]
head += 1
var x: int = cell % side
var z: int = cell / side
for step: Vector2i in NEIGHBOURS:
var nx: int = x + step.x
var nz: int = z + step.y
if nx < 0 or nz < 0 or nx >= side or nz >= side:
continue
var next: int = nz * side + nx
if marks[next] == 0 and admit.call(next):
marks[next] = 1
queue.append(next)
return queue.size()