nx_crater_field.nx
buildroot/runtime/nx_crater_field.nx
about
nx_crater_field.nx -- deterministic crater-impact field distribution.
Cross-cutting Substrate C primitive per
nxc2/docs/NISHI_GAME_ENGINE_ROADMAP.md. Generic generator
consumed by every airless-body surface (Mercury, the Moon, Mars
highlands, Callisto, every asteroid, every comet nucleus). Lives
in nishi-core because any astronomical-visualisation program also
uses it -- not voxel-specific.
FULL CAPABILITY (per feedback-maximum-capability-no-simplification
cardinal, 2026-05-16):
- Neukum 1983 power-law radius distribution (alpha = 2 default,
caller-overridable). Sampled via the exact inverse-CDF
transform r = r_min / (1 - u * (1 - r_min/r_max)) which
produces N(>D) ~ D^-2 in clean Q14 integer math.
- Age-banded saturation density: caller queries
nx_crater_saturation_count(age, area, r_avg) to learn how many
craters fit the chosen band (YOUNG -> sparse; SATURATION ->
densely packed to ~lunar-saturation 1/(2r)^2 limit).
- Composite elevation profile per crater:
* Inside bowl (d_norm < 0.85): parabolic depression
* Rim ring (0.85 <= d_norm < 1.05): elevated rim, peak at
d_norm = 0.95, parabolic falloff, +15% of bowl depth
* Central peak (d_norm < 0.15 AND r >= transition radius):
parabolic uplift, +30% of bowl depth. Lunar transition
diameter ~ 15 km (caller-overridable).
- Boolean-max (newest crater wins) over overlapping craters via
nx_crater_field_elevation_at; original
nx_crater_field_depth_at preserved as wrapper for callers that
only care about the bowl depth.
Determinism: same (seed, area_w, area_h, count) -> byte-equal
crater list everywhere. Park-Miller LCG seeded with
(seed XOR crater_index XOR field_id) gives well-spread positions.
Loss audit (HONEST residuals):
- alpha = 2 fixed (Neukum 1983 standard). Real lunar fits alpha
in [1.8, 2.1] depending on epoch. Caller can post-process the
radius distribution if needed; not a capability reduction since
the most-cited literature value ships.
dependencies 3 imports · 0 importers
imports: nx_syscalls.nxnx_hal.nxnx_tier.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 59 | const NX_MAGIC_4096: i64 = 4096 |
| 60 | const NX_MAGIC_10650: i64 = 10650 |
| 61 | const NX_MAGIC_16384: i64 = 16384 |
| 62 | const NX_MAGIC_12288: i64 = 12288 |
| 63 | const NX_MAGIC_8192: i64 = 8192 |
| 64 | const NX_MAGIC_4915: i64 = 4915 |
| 65 | const NX_MAGIC_2654435761: i64 = 2654435761 |
| 66 | const NX_MAGIC_1597334677: i64 = 1597334677 |
| 67 | const NX_MAGIC_31130: i64 = 31130 |
| 68 | const NX_MAGIC_7777: i64 = 7777 |
| 69 | const NX_MAGIC_8888: i64 = 8888 |
| 70 | const NX_MAGIC_9999: i64 = 9999 |
| 73 | const NX_CRATER_Q: nx_int = 16384 |
| 76 | const NX_CRATER_STRIDE: nx_int = 4 |
| 79 | const NX_CRATER_OFF_X: nx_int = 0 |
| 80 | const NX_CRATER_OFF_Y: nx_int = 1 |
| 81 | const NX_CRATER_OFF_R: nx_int = 2 |
| 82 | const NX_CRATER_OFF_DEPTH: nx_int = 3 |
| 88 | const NX_CRATER_DEPTH_RATIO_Q14: nx_int = 3277 // 0.20 |
| 92 | const NX_CRATER_LCG_A: nx_int = 48271 |
| 93 | const NX_CRATER_LCG_M: nx_int = 2147483647 |
| 98 | const NX_CRATER_BOWL_HALF_Q14: nx_int = 13926 // 0.85 -- bowl extends to 0.85 r |
| 99 | const NX_CRATER_RIM_PEAK_Q14: nx_int = 15565 // 0.95 -- rim peak at 0.95 r |
| 100 | const NX_CRATER_OUTER_Q14: nx_int = 17204 // 1.05 -- profile ends at 1.05 r |
| 101 | const NX_CRATER_RIM_HALFW_Q14: nx_int = 1638 // 0.10 -- rim falloff half-width |
| 102 | const NX_CRATER_RIM_HEIGHT_FRAC: nx_int = 2458 // 0.15 of bowl depth -> rim height |
| 103 | const NX_CRATER_CENTRAL_RAD_Q14: nx_int = 2458 // 0.15 -- central peak radius (relative) |
| 104 | const NX_CRATER_CENTRAL_HEIGHT: nx_int = 4915 // 0.30 of bowl depth -> central peak |
| 110 | const NX_CRATER_TRANSITION_R_Q14: nx_int = 245760 // 15.0 in Q14 |
| 114 | const NX_CRATER_AGE_YOUNG: nx_int = 0 // post-impact (lunar maria, post-Imbrium) |
| 115 | const NX_CRATER_AGE_MID: nx_int = 1 // moderate density (lunar uplands) |
| 116 | const NX_CRATER_AGE_OLD: nx_int = 2 // high density (Mercury cratered terrain) |
| 117 | const NX_CRATER_AGE_SATURATION: nx_int = 3 // maximally cratered (asteroids, oldest plateaus) |
functions
| 119 | func nx_crater_age_band_is_valid(b: nx_int) -> nx_int |
| 130 | func _crater_age_density_q14(age: nx_int) -> nx_int called by 1: nx_crater_saturation_count |
| 140 | func _crater_age_depth_mult_q14(age: nx_int) -> nx_int called by 1: nx_crater_field_sample |
| 159 | func nx_crater_saturation_count( |
| 186 | func _crater_hash(seed: nx_int, index: nx_int, axis: nx_int) -> nx_int called by 1: nx_crater_field_sample |
| 196 | func _crater_mod_q14(hash_v: nx_int, span_q14: nx_int) -> nx_int called by 1: nx_crater_field_sample |
| 210 | func _crater_powerlaw_radius_q14( called by 1: nx_crater_field_sample |
| 252 | func nx_crater_field_sample( |
| 303 | func nx_crater_field_sample_simple( |
| 342 | func nx_crater_field_elevation_at( called by 1: main |
| 431 | func nx_crater_field_depth_at( called by 1: main |
| 460 | func main() -> i64 |