The tide now runs every match, from the drop on, and water flows along the terrain instead of standing everywhere under one level. - Tide (src/sim/tide.gd): Tuning.TIDE_PHASES as a schedule of holds and rises. Level, forecast (each hold announces its level), stage, drowning rate, surge timing and the time limit are pure functions of the tick. - SafeGround (src/sim/safe_ground.gd): safe ground from elevation on an 8 m grid, and per-island planned levels. A phase's level is lowered, when it must be, to just under the top of the largest piece of land still dry when the phase begins; every phase still rises at least TIDE_MIN_RISE_METRES and the last keeps its table level. - Flood (src/sim/flood.gd): a 4 m grid aligned with building cells. Water reaches cells from the world's edge through ground under the sea and edges no breakwater stack seals. Cut-off ground stays dry until overtopped or breached, then fills at BREACH_FLOW_METRES_PER_SECOND. Incremental: only the edges where water stopped are re-tested, once a second. - Players, drowning and structure wear use the local water. Breakwaters wear by head and fail past BREAKWATER_MAX_HEAD; a breach floods what's behind. - Storm surges: each phase's `surge` damage hits pieces near the water when its rise begins. - Sudden death after the last rise, then a time limit. Match breaks ties (most health, highest feet, earliest join) at the limit and when the last players fall together, so every match ends with a winner. Tests: test_tide (18) covers the schedule and forecasts, planned levels, overtopping, a sealed ring, breach and fill rate, spill-over, pressure wear and head failure, dry pieces, surges, drowning under the real tide, sudden death, the time limit and simultaneous eliminations. test_island's 100-seed sweep now checks that every phase leaves safe ground reachable on foot from the whole main landmass within the phase's time (closest margin 14 s). A full rise with 24 bots measured p99 1.5 ms, max 1.9 ms per tick. Closes #25 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
175 lines
8.3 KiB
GDScript
175 lines
8.3 KiB
GDScript
extends TestCase
|
|
|
|
## The acceptance sweep from issue W2: every one of these seeds must make a good island.
|
|
const SWEEP_SEEDS: int = 100
|
|
const GENERATION_BUDGET_MSEC: int = 5000
|
|
## The sea level of the first flood line. Land must stay joined up to here.
|
|
const EARLY_TIDE_LEVELS: Array[float] = [0.0, 8.0]
|
|
## Dry land outside the main landmass may be at most this fraction: a few islets and
|
|
## sea stacks are fine, a second island is not.
|
|
const MAX_STRANDED_FRACTION: float = 0.03
|
|
const MIN_LAND_FRACTION: float = 0.30
|
|
const MAX_LAND_FRACTION: float = 0.70
|
|
## Distinct high points: at least this high, the highest within 80 m, and with a valley
|
|
## at least 6 m deep between each and any higher one.
|
|
const PEAK_MIN_HEIGHT: float = 35.0
|
|
const PEAK_RADIUS: float = 80.0
|
|
const PEAK_DROP: float = 6.0
|
|
const MIN_WALKABLE_LAND: float = 0.85
|
|
|
|
|
|
func test_same_seed_gives_the_same_heightmap() -> void:
|
|
var first: Island = Island.generate(777)
|
|
var second: Island = Island.generate(777)
|
|
assert_eq(first.heightmap_hash(), second.heightmap_hash())
|
|
assert_eq(first.heightmap_hash().length(), 64, "expected a SHA-256 hex digest")
|
|
assert_eq(first.summits, second.summits)
|
|
|
|
|
|
func test_different_seeds_give_different_islands() -> void:
|
|
assert_ne(Island.generate(777).heightmap_hash(), Island.generate(778).heightmap_hash())
|
|
|
|
|
|
func test_generation_does_not_touch_match_randomness() -> void:
|
|
# Worldgen uses its own copy of the "worldgen" stream, so generating the island on a
|
|
# client can never shift the server's gameplay rolls.
|
|
var sim: Simulation = Simulation.new(31337)
|
|
assert_eq(sim.rng.stream_names(), [] as Array[String])
|
|
assert_eq(sim.island.heightmap_hash(), Island.generate(31337).heightmap_hash())
|
|
|
|
|
|
func test_a_separate_server_process_builds_the_same_island() -> void:
|
|
# The same check a client makes on joining: an independent engine process given only
|
|
# the seed must arrive at the same heightmap.
|
|
var seed_value: int = 4_000_000_017
|
|
var output: Array = []
|
|
var args: PackedStringArray = PackedStringArray([
|
|
"--headless", "--path", ProjectSettings.globalize_path("res://"),
|
|
"--", "--server", "--seed=%d" % seed_value, "--ticks=1", "--unthrottled",
|
|
])
|
|
var code: int = OS.execute(OS.get_executable_path(), args, output, true)
|
|
assert_eq(code, 0, "server exited %d: %s" % [code, "\n".join(output)])
|
|
var world_hash: String = ""
|
|
for line: String in "\n".join(output).split("\n"):
|
|
if line.begins_with("{") and line.contains("\"event\":\"start\""):
|
|
world_hash = (JSON.parse_string(line) as Dictionary).get("world_hash", "")
|
|
assert_eq(world_hash, Island.generate(seed_value).heightmap_hash())
|
|
|
|
|
|
func test_bands_follow_height() -> void:
|
|
var island: Island = Island.generate(5)
|
|
var ground: SimGround = island.ground
|
|
for index: int in range(0, ground.heights.size(), 97):
|
|
var height: float = ground.heights[index]
|
|
var band: int = island.bands[index]
|
|
match band:
|
|
Island.Band.SEA:
|
|
assert_true(height <= 0.0, "sea at %.2f m" % height)
|
|
Island.Band.BEACH:
|
|
assert_true(height > 0.0 and height < Tuning.ISLAND_BEACH_TOP, "beach at %.2f m" % height)
|
|
Island.Band.MARSH:
|
|
assert_true(height < Tuning.ISLAND_MARSH_TOP, "marsh at %.2f m" % height)
|
|
Island.Band.FOREST:
|
|
assert_true(height < Tuning.ISLAND_FOREST_TOP, "forest at %.2f m" % height)
|
|
Island.Band.HIGHLAND:
|
|
assert_true(height >= Tuning.ISLAND_FOREST_TOP, "highland at %.2f m" % height)
|
|
Island.Band.SUMMIT:
|
|
assert_true(height >= Tuning.ISLAND_SUMMIT_BAND, "summit at %.2f m" % height)
|
|
assert_eq(island.band_at(-50.0, -50.0), Island.Band.SEA, "the world corner is open sea")
|
|
|
|
|
|
func test_players_spawn_on_dry_land() -> void:
|
|
var sim: Simulation = Simulation.new(2024)
|
|
for _index: int in Tuning.SOLO_PLAYERS:
|
|
var player: SimEntity = sim.entities.find(sim.add_player())
|
|
assert_true(player.position.y > Tuning.ISLAND_BEACH_TOP, "spawned at %s" % player.position)
|
|
|
|
|
|
func test_one_hundred_seeds_make_playable_islands() -> void:
|
|
var slowest: int = 0
|
|
var marsh_islands: int = 0
|
|
var worst_stranded: float = 0.0
|
|
var land_range: Vector2 = Vector2(INF, -INF)
|
|
var peak_counts: Dictionary = {}
|
|
var safe_margin: float = INF
|
|
var lowered_phases: int = 0
|
|
for seed_value: int in range(1, SWEEP_SEEDS + 1):
|
|
var started: int = Time.get_ticks_msec()
|
|
var island: Island = Island.generate(seed_value)
|
|
var elapsed: int = Time.get_ticks_msec() - started
|
|
slowest = maxi(slowest, elapsed)
|
|
var label: String = "seed %d" % seed_value
|
|
assert_true(elapsed < GENERATION_BUDGET_MSEC, "%s took %d ms" % [label, elapsed])
|
|
|
|
var survey: IslandSurvey = IslandSurvey.new(island)
|
|
var land: float = survey.land_fraction(0.0)
|
|
assert_true(land >= MIN_LAND_FRACTION and land <= MAX_LAND_FRACTION,
|
|
"%s: land covers %.2f of the world" % [label, land])
|
|
var highest: float = island.max_height()
|
|
assert_true(highest >= Tuning.ISLAND_PEAK_MIN - 1.0 and highest < Tuning.ISLAND_MAX_HEIGHT,
|
|
"%s: highest ground %.1f m" % [label, highest])
|
|
var final_level: float = Tuning.TIDE_PHASES[-1]["level"]
|
|
assert_true(highest < final_level, "%s: the last tide must cover the island" % label)
|
|
|
|
for level: float in EARLY_TIDE_LEVELS:
|
|
var dry: Dictionary = survey.dry_land(level)
|
|
var stranded: float = 1.0 - float(dry["largest"]) / maxf(dry["dry"], 1.0)
|
|
worst_stranded = maxf(worst_stranded, stranded)
|
|
assert_true(stranded <= MAX_STRANDED_FRACTION,
|
|
"%s: at %.0f m, %.3f of dry land is cut off from the main landmass" % [label, level, stranded])
|
|
|
|
# Safe ground (issue M2): every phase of the island's planned tide that leaves
|
|
# land leaves it within reach of the whole main landmass, on foot, in time.
|
|
var safe: SafeGroundSurvey = SafeGroundSurvey.new(island)
|
|
var checked: Array[Dictionary] = safe.check()
|
|
assert_true(checked.size() >= Tuning.TIDE_PHASES.size() - 2, "%s: only %d phases leave safe ground" % [label, checked.size()])
|
|
for result: Dictionary in checked:
|
|
var phase_label: String = "%s phase %d (%.1f m)" % [label, result["phase"], result["level"]]
|
|
assert_eq(result["unreached"], 0, "%s: main landmass cells with no way to safe ground" % phase_label)
|
|
assert_eq(result["swimmers"], 0, "%s: main landmass cells that must swim to safe ground" % phase_label)
|
|
assert_true(result["worst_seconds"] <= result["allowed_seconds"],
|
|
"%s: %.0f s to safe ground, %.0f s allowed" % [phase_label, result["worst_seconds"], result["allowed_seconds"]])
|
|
safe_margin = minf(safe_margin, result["allowed_seconds"] - result["worst_seconds"])
|
|
for index: int in safe.levels.size():
|
|
if not is_equal_approx(safe.levels[index], float(Tuning.TIDE_PHASES[index]["level"])):
|
|
lowered_phases += 1
|
|
assert_eq(safe.levels[safe.levels.size() - 1], float(Tuning.TIDE_PHASES[-1]["level"]), "%s: the last phase keeps its level" % label)
|
|
|
|
var peaks: Array[Vector3] = survey.peaks(PEAK_MIN_HEIGHT, PEAK_RADIUS, PEAK_DROP)
|
|
assert_true(peaks.size() >= 2, "%s: %d distinct high points" % [label, peaks.size()])
|
|
|
|
var walkable: float = _walkable_land(island)
|
|
assert_true(walkable >= MIN_WALKABLE_LAND, "%s: only %.2f of land is walkable" % [label, walkable])
|
|
|
|
var bands: Dictionary = {}
|
|
for band: int in island.bands:
|
|
bands[band] = true
|
|
for needed: int in [Island.Band.BEACH, Island.Band.FOREST, Island.Band.HIGHLAND, Island.Band.SUMMIT]:
|
|
assert_true(bands.has(needed), "%s lacks band %s" % [label, Island.Band.keys()[needed]])
|
|
if bands.has(Island.Band.MARSH):
|
|
marsh_islands += 1
|
|
land_range = Vector2(minf(land_range.x, land), maxf(land_range.y, land))
|
|
peak_counts[peaks.size()] = peak_counts.get(peaks.size(), 0) + 1
|
|
assert_true(marsh_islands >= SWEEP_SEEDS * 0.8, "only %d islands have marsh" % marsh_islands)
|
|
print(" island sweep: slowest %d ms, land %.2f to %.2f of the world, worst stranded %.4f," % [
|
|
slowest, land_range.x, land_range.y, worst_stranded])
|
|
print(" island sweep: safe ground reached with at least %.0f s to spare; %d phase levels lowered by planning" % [safe_margin, lowered_phases])
|
|
print(" island sweep: islands by distinct high points %s, %d of %d with marsh" % [
|
|
peak_counts, marsh_islands, SWEEP_SEEDS])
|
|
|
|
|
|
func _walkable_land(island: Island) -> float:
|
|
var ground: SimGround = island.ground
|
|
var land: int = 0
|
|
var walkable: int = 0
|
|
for gz: int in range(0, ground.samples, 4):
|
|
for gx: int in range(0, ground.samples, 4):
|
|
var x: float = gx * ground.cell
|
|
var z: float = gz * ground.cell
|
|
if ground.terrain_height(x, z) <= 0.0:
|
|
continue
|
|
land += 1
|
|
if ground.walkable_terrain(x, z):
|
|
walkable += 1
|
|
return float(walkable) / maxf(land, 1.0)
|