Files
tidewall/test/sim/test_sim_ground.gd
T
claudeandClaude Opus 5.5 5021d13686 W4: Points of interest
PoiSet.generate(island) places 10 to 16 points of interest per island from
the seed's "poi" stream: hilltop shrines, lighthouses, docks, salt works,
quarries and fishing hamlets, at least 85 m apart. Each kind has a siting
rule (height band, flatness, coast or summit) that loosens step by step
until its minimum is placed. Piers are sited so their deck clears the
ground all the way out to water 1.5 m deep.

Poi assembles each from modular parts in a local frame turned in quarter
turns, so every solid stays an axis-aligned box: walls with a door and
lintel, plinths, steps down to the ground, roofs, a stair to a loft. Each
POI carries loot spawn markers, exposed through PoiSet.loot_markers(),
and walking routes from open ground into its interiors.

- Simulation adds every POI solid to its ground and never spawns players
  inside one.
- SimGround indexes solids in 8 m cells, so collision queries only test
  nearby boxes. Smoke p95 tick is 0.47 ms with about 300 solids.
- Movement tests the body where it will stand after stepping up, so
  stairs with treads narrower than the body can be climbed. The
  traversal test found the lighthouse stair impassable without it.
- PoiView draws parts as one MultiMesh per look, in the seafaring
  palette. Capture scenarios poi_<kind> frame each kind.
- Summit stone is darker so it no longer reads as snow.

Tests: 30 seeds get every kind within its limits, 10 to 16 in total,
spaced and sited; placement is deterministic; loot markers stand on
floors and are not inside solids; a player walks every route of every
POI on 4 seeds through doors, up steps and the lighthouse stair; stairs
with 0.45 m treads are climbed and a step doesn't carry the body into a
wall; the solid index matches a full scan on 500 queries.

Closes #11

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

115 lines
5.3 KiB
GDScript

extends TestCase
func _tilted() -> SimGround:
# h = 0.5 * x, a plane rising along +x.
var values: PackedFloat32Array = PackedFloat32Array()
for z: int in 4:
for x: int in 4:
values.append(0.5 * x * 2.0)
return SimGround.from_heights(values, 4, 2.0)
func test_flat_ground() -> void:
var ground: SimGround = SimGround.flat(3.0)
assert_eq(ground.terrain_height(10.0, 900.0), 3.0)
assert_true(ground.terrain_normal(10.0, 10.0).is_equal_approx(Vector3.UP))
assert_true(ground.walkable_terrain(10.0, 10.0))
func test_interpolation_and_edges() -> void:
var ground: SimGround = _tilted()
assert_near(ground.terrain_height(1.0, 1.0), 0.5, 0.0001)
assert_near(ground.terrain_height(3.0, 5.0), 1.5, 0.0001)
# Outside the grid, the edge sample is used.
assert_near(ground.terrain_height(-5.0, 2.0), 0.0, 0.0001)
assert_near(ground.terrain_height(100.0, 2.0), 3.0, 0.0001)
func test_normal_and_walkability() -> void:
var ground: SimGround = _tilted()
var normal: Vector3 = ground.terrain_normal(3.0, 3.0)
assert_near(rad_to_deg(normal.angle_to(Vector3.UP)), rad_to_deg(atan(0.5)), 0.01)
assert_true(normal.x < 0.0, "the normal leans away from the rise")
assert_true(ground.walkable_terrain(3.0, 3.0))
func test_support_from_solids() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(0.0, 0.0, 0.0), Vector3(2.0, 1.0, 2.0)))
ground.add_solid(AABB(Vector3(0.0, 5.0, 0.0), Vector3(2.0, 1.0, 2.0)))
assert_eq(ground.support_height(1.0, 1.0, 1.2), 1.0)
assert_eq(ground.support_height(1.0, 1.0, 10.0), 6.0)
assert_eq(ground.support_height(1.0, 1.0, 0.5), 0.0)
assert_eq(ground.support_height(3.0, 1.0, 10.0), 0.0)
assert_true(ground.support_is_solid(1.0, 1.0, 1.2))
assert_false(ground.support_is_solid(3.0, 1.0, 1.2))
func test_negative_size_solids_are_normalised() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(2.0, 1.0, 2.0), Vector3(-2.0, -1.0, -2.0)))
assert_eq(ground.support_height(1.0, 1.0, 5.0), 1.0)
func test_raycast_against_a_box() -> void:
var ground: SimGround = SimGround.flat(-100.0)
ground.add_solid(AABB(Vector3(4.0, -1.0, -1.0), Vector3(2.0, 2.0, 2.0)))
assert_near(ground.raycast(Vector3.ZERO, Vector3(10.0, 0.0, 0.0)), 0.4, 0.0001)
assert_eq(ground.raycast(Vector3.ZERO, Vector3(3.0, 0.0, 0.0)), 1.0)
assert_eq(ground.raycast(Vector3(0.0, 5.0, 0.0), Vector3(10.0, 5.0, 0.0)), 1.0)
assert_eq(ground.raycast(Vector3(5.0, 0.0, 0.0), Vector3(10.0, 0.0, 0.0)), 0.0, "starting inside")
func test_raycast_against_terrain() -> void:
var ground: SimGround = SimGround.flat(0.0)
var hit: float = ground.raycast(Vector3(0.0, 2.0, 0.0), Vector3(0.0, -2.0, 0.0))
assert_near(hit, 0.5, 0.001)
assert_eq(ground.raycast(Vector3(0.0, 2.0, 0.0), Vector3(5.0, 1.0, 0.0)), 1.0)
assert_eq(ground.raycast(Vector3(0.0, -1.0, 0.0), Vector3(0.0, 3.0, 0.0)), 0.0, "starting below")
# A ray grazing up a slope finds where it enters.
var tilted: SimGround = _tilted()
var slope_hit: float = tilted.raycast(Vector3(0.0, 2.0, 2.0), Vector3(6.0, 2.0, 2.0))
assert_near(slope_hit * 6.0, 4.0, 0.01)
func test_body_blocked_respects_step_and_height() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(0.0, 0.0, 0.0), Vector3(2.0, 0.4, 2.0)))
ground.add_solid(AABB(Vector3(10.0, 2.0, 0.0), Vector3(2.0, 1.0, 2.0)))
assert_false(ground.body_blocked(Vector3(1.0, 0.0, 1.0), 0.4, 1.8, 0.45), "low step")
assert_true(ground.body_blocked(Vector3(1.0, 0.0, 1.0), 0.4, 1.8, 0.0))
assert_true(ground.body_blocked(Vector3(2.3, 0.0, 1.0), 0.4, 1.8, 0.0), "radius overlaps")
assert_false(ground.body_blocked(Vector3(2.5, 0.0, 1.0), 0.4, 1.8, 0.0))
assert_false(ground.body_blocked(Vector3(11.0, 0.0, 1.0), 0.4, 1.8, 0.0), "fits under")
assert_true(ground.body_blocked(Vector3(11.0, 0.0, 1.0), 0.4, 2.5, 0.0))
func test_index_finds_solids_across_cells() -> void:
var ground: SimGround = SimGround.flat()
# Spans several index cells, including negative ones.
ground.add_solid(AABB(Vector3(-5.0, 0.0, -5.0), Vector3(30.0, 2.0, 3.0)))
assert_eq(ground.support_height(20.0, -4.0, 5.0), 2.0)
assert_eq(ground.support_height(-4.0, -4.0, 5.0), 2.0)
assert_eq(ground.support_height(20.0, 4.0, 5.0), 0.0)
assert_true(ground.body_blocked(Vector3(12.0, 0.0, -1.7), 0.4, 1.8, 0.0), "radius reaches into the next cell")
# The box's far side from the ray's start is at z = -2, 22 m along a 40 m ray.
assert_near(ground.raycast(Vector3(15.0, 1.0, 20.0), Vector3(15.0, 1.0, -20.0)), 22.0 / 40.0, 0.0001)
func test_index_gives_the_same_answers_as_a_full_scan() -> void:
var ground: SimGround = SimGround.flat()
var random: RandomNumberGenerator = RandomNumberGenerator.new()
random.seed = 5
for _index: int in 200:
var at: Vector3 = Vector3(random.randf_range(0.0, 100.0), random.randf_range(0.0, 4.0), random.randf_range(0.0, 100.0))
ground.add_solid(AABB(at, Vector3(random.randf_range(0.2, 12.0), random.randf_range(0.2, 3.0), random.randf_range(0.2, 12.0))))
for _index: int in 500:
var x: float = random.randf_range(0.0, 100.0)
var z: float = random.randf_range(0.0, 100.0)
var expected: float = 0.0
for box: AABB in ground.solids:
if box.end.y <= 6.0 and x > box.position.x and x < box.end.x and z > box.position.z and z < box.end.z:
expected = maxf(expected, box.end.y)
assert_eq(ground.support_height(x, z, 6.0), expected)