Files
Emberwild/tests/test_terrain_edges.gd

77 lines
3.1 KiB
GDScript

extends SceneTree
## Terrain edge check, for several seeds: in the band just inside the boundary
## wall there must be (1) no water deeper than you can wade out of, and (2) no
## water without a bank, i.e. ground that drops away outward. The inland water
## surface stops at the wall, so unbanked water hangs in the air over the drop.
## This is the bug where the player fell into floating water by the sea cliff.
## Shallow ponds at the foot of the mountains are fine: the slope holds them.
##
## Run: godot --headless --path . --script res://tests/test_terrain_edges.gd
const BAND := 10.0 # metres inside the wall to check
const MAX_EDGE_DEPTH := 1.0 # deepest allowed water near the edge (wadeable)
const BANK_CHECK := 12.0 # how far outward the bank must rise above water
## The original island plus a spread of other seeds: new worlds get random
## seeds, so the edge rule has to hold for any of them.
const SEEDS := [1337, 1, 42, 777, 4242, 9001, 31337, 123456]
func _initialize() -> void:
_all.call_deferred()
## Each worldlet's edges (W2): the Valley, and a test worldlet with two
## ponds and a beach (tests/worldlets/testbay.json).
const WORLDLETS := ["valley", "testbay"]
func _all() -> void:
var failed := 0
for w in WORLDLETS:
for s in SEEDS:
var terrain: Node3D = load("res://scripts/world/terrain.gd").new()
terrain.noise_seed = s
terrain.worldlet_id = w
root.add_child(terrain) # builds in _ready
var bad := _check(terrain)
print("%-8s seed %6d: %s" % [w, s, "ok" if bad == 0 and terrain.worldlet_id == w else "%d bad samples" % bad])
if bad > 0 or terrain.worldlet_id != w:
failed += 1
terrain.free()
var n := WORLDLETS.size() * SEEDS.size()
print("RESULT: %s (%d/%d seeds clean)" % ["PASS" if failed == 0 else "FAIL", n - failed, n])
quit(1 if failed > 0 else 0)
func _check(terrain: Node3D) -> int:
var bad := 0
var worst := INF
var worst_at := Vector2.ZERO
var half: float = terrain.size * 0.5
var x := -half
while x < half:
var z := -half
while z < half:
# (Off a beach the edge is open sea, and the current keeps you
# from the wall: tests/test_boundary.gd swims out there.)
if terrain.inside_boundary(x, z, 0.0) and not terrain.inside_boundary(x, z, BAND) \
and terrain.beach_weight(x, z) <= 0.5:
var h: float = terrain.sample_height(x, z)
# Hazard 1: a pit too deep to wade out of near the edge.
var too_deep: bool = h < terrain.water_level - MAX_EDGE_DEPTH
# Hazard 2: water that isn't held in by a bank. The inland water
# surface stops at the wall, so wet ground must rise again just
# outward, or the water edge hangs over a drop.
var unbanked := false
if h < terrain.water_level:
var out := Vector2(x, z).normalized() * BANK_CHECK
unbanked = terrain.sample_height(x + out.x, z + out.y) < terrain.water_level
if too_deep or unbanked:
bad += 1
if h < worst:
worst = h
worst_at = Vector2(x, z)
z += 1.0
x += 1.0
if bad > 0:
print(" lowest %.1f m at (%.0f, %.0f), sea weight %.2f" % [
worst, worst_at.x, worst_at.y, terrain.sea_weight(worst_at.x, worst_at.y)])
return bad