feat(simulation): cache TerrainAnalysis + derive basin_direction from the D8 thalweg (T-1044, T-1047)

T-1044: run_layer1 now returns TerrainAnalysis (carried transiently on
CascadeSnapshot, dropped after the district + road-graph passes), eliminating the
redundant per-body drainage::analyze + TerrainAnalysis::analyze re-run flagged by
PERF/TODO(T-1044). Not persisted on the LRU-cached state (D-203/T-1048 size concern).

T-1047: basin_direction is now derived from the real D8 thalweg. run_layer1
aggregates a per-district dominant D8 direction from the live fdir grid (carried
transiently on DrainageResult), threaded via Layer1Output.district_basin_dirs ->
derive_all_districts -> DistrictProfile.basin_direction; derive_chunk_context reads
it directly. Removed the false derive_basin_direction (it branched on ocean_fraction_q
then read seed bits despite a doc comment claiming an elev_q/slope_q D8 proxy) +
corrected the module contract. D-239 §8 (D8 thalweg) now actually honoured.

1559 tests pass; golden byte-identical (district_basin_dirs is #[serde(skip)], transient).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-17 09:46:42 +02:00
co-authored by Claude Opus 4.8
parent 711ac65d20
commit 0263b68df9
12 changed files with 349 additions and 139 deletions
+9
View File
@@ -53,6 +53,14 @@ pub struct DrainageResult {
/// Maximum flow accumulation across the grid — the denominator for
/// normalized attractor strength (D-209). Always ≥ 1.
pub max_accumulation: i32,
/// Per-cell D8 flow-direction index into `D8` (0–7), or -1 for no outflow
/// (edge, flat peak, or ocean). Row-major, `w × h`.
///
/// **Transient — used within the Layer-1 pass only.** The caller aggregates a
/// per-district dominant direction from this grid (T-1047) and carries that
/// compact result on `Layer1Output.district_basin_dirs`; the full 131 KB
/// grid is NOT persisted on `BodyWorldState` or the LRU cache (D-203).
pub fdir: Vec<i8>,
}
// ---------------------------------------------------------------------------
@@ -104,6 +112,7 @@ pub fn analyze(elevation: &[f32], width: u32, height: u32, sea_level: f32) -> Dr
drainage_basins,
flow_accumulation: accum,
max_accumulation,
fdir,
}
}