ballistics_engine/adjustment.rs
1//! Turret adjustment-unit conversions and click-value parsing (MBA-1355).
2//!
3//! Shared by the CLI (`main.rs`) and the WASM terminal (`wasm.rs`). This lives in the
4//! library crate — not `main.rs` — so it compiles for `wasm32-unknown-unknown` with no
5//! feature gate. To avoid a circular dependency on `main.rs`'s clap `AdjustmentUnit`
6//! `ValueEnum`, this module defines its own minimal angular base (`ClickBase`) and factor
7//! table; `main.rs` maps its own `AdjustmentUnit` onto `ClickBase`/`adjustment_factor`
8//! locally (see `drop_to_adjustment` in `main.rs`).
9
10use serde::{Deserialize, Deserializer, Serialize, Serializer};
11
12/// Angular base unit for a turret click graduation (MBA-1355). A click graduation is
13/// always expressed in mil, (true) MOA, or SMOA/IPHY — never in whole clicks itself.
14///
15/// Does not itself derive `Serialize`/`Deserialize`: `ClickValue` serializes as ONE
16/// suffixed string (`"0.1mil"`, `"0.25moa"`, ...) via its own hand-written impls below,
17/// not as a structural `{size, base}` object, so nothing needs this type's wire form on
18/// its own (MBA-1348).
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub enum ClickBase {
21 Mil,
22 Moa,
23 Smoa,
24}
25
26/// The `(drop_yd / range_yd) * factor` conversion factor for a `ClickBase`:
27/// - `Mil`: 1000.0 (milliradians)
28/// - `Moa`: 3438.0 (true MOA; this crate's locked printed-table constant — see MBA-724 —
29/// deliberately not the exact-angle 3437.7467)
30/// - `Smoa`: 3600.0 ("shooter's MOA" / inches-per-hundred-yards; exact by definition)
31pub fn adjustment_factor(base: ClickBase) -> f64 {
32 match base {
33 ClickBase::Mil => 1000.0,
34 ClickBase::Moa => 3438.0,
35 ClickBase::Smoa => 3600.0,
36 }
37}
38
39/// A turret click graduation, parsed from suffixed CLI/profile syntax
40/// like "0.1mil" / "0.25moa" / "0.125smoa" (MBA-1355).
41#[derive(Debug, Clone, Copy, PartialEq)]
42pub struct ClickValue {
43 pub size: f64,
44 pub base: ClickBase,
45}
46
47/// Parses a suffixed click graduation string. The unit suffix is mandatory — one of
48/// `mil`, `moa`, `smoa`, `iphy` (`iphy` is accepted as an alias for `smoa`, the identical
49/// unit) — and the magnitude must be a positive, finite number.
50pub fn parse_click_value(s: &str) -> Result<ClickValue, String> {
51 let t = s.trim().to_ascii_lowercase();
52 let (num, base) = if let Some(n) = t.strip_suffix("smoa") {
53 (n, ClickBase::Smoa)
54 } else if let Some(n) = t.strip_suffix("iphy") {
55 (n, ClickBase::Smoa)
56 } else if let Some(n) = t.strip_suffix("moa") {
57 (n, ClickBase::Moa)
58 } else if let Some(n) = t.strip_suffix("mil") {
59 (n, ClickBase::Mil)
60 } else {
61 return Err(format!(
62 "click value '{s}' needs a unit suffix: mil, moa, smoa, or iphy (e.g. 0.1mil, 0.25moa)"
63 ));
64 };
65 let size: f64 = num
66 .trim()
67 .parse()
68 .map_err(|_| format!("click value '{s}' has an invalid number '{num}'"))?;
69 if !size.is_finite() || size <= 0.0 {
70 return Err(format!("click value '{s}' must be a positive, finite graduation"));
71 }
72 Ok(ClickValue { size, base })
73}
74
75/// Serializes as the crate's one existing suffixed-string representation — `"0.1mil"`,
76/// `"0.25moa"`, `"1smoa"` — the exact shape `parse_click_value` parses and a CLI
77/// `--elevation-click` flag already accepts (MBA-1348). Deliberately NOT the structural
78/// `{"size": 0.1, "base": "mil"}` a plain derive would produce: the string form is stable
79/// against internal field changes and makes a profile file's click entries identical to
80/// the CLI flag syntax a shooter already types. `size` uses `f64`'s default `Display`,
81/// which is Rust's shortest string that round-trips back to the same value (so `1.0`
82/// prints as `"1"`, matching `parse_click_value`'s own accepted syntax).
83impl Serialize for ClickValue {
84 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
85 where
86 S: Serializer,
87 {
88 let suffix = match self.base {
89 ClickBase::Mil => "mil",
90 ClickBase::Moa => "moa",
91 ClickBase::Smoa => "smoa",
92 };
93 let size = self.size;
94 serializer.serialize_str(&format!("{size}{suffix}"))
95 }
96}
97
98/// Deserializes via `parse_click_value` — the ONLY parser for this syntax, not a second,
99/// divergent implementation of it — so a malformed string is rejected with the identical
100/// message the CLI already gives, and `parse_click_value`'s positive-and-finite rule
101/// applies on every deserialize (MBA-1348).
102impl<'de> Deserialize<'de> for ClickValue {
103 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
104 where
105 D: Deserializer<'de>,
106 {
107 let raw = String::deserialize(deserializer)?;
108 parse_click_value(&raw).map_err(serde::de::Error::custom)
109 }
110}
111
112/// One quantized dial setting: the detent count nearest `angle`, and what remains.
113/// `residual` is in the same angular unit as `angle` (the click's base unit) and is
114/// DEFINED by the exact reconstruction identity
115/// `angle == clicks as f64 * click.size + residual` (bit-for-bit, by construction).
116#[derive(Debug, Clone, Copy, PartialEq)]
117pub struct Quantized {
118 pub clicks: i64,
119 pub residual: f64,
120}
121
122/// Quantize an angular value (in `click.base` units) onto a click detent.
123/// Rounding is nearest, ties away from zero — identical to `clicks_for`, which delegates here.
124pub fn quantize_angle(angle: f64, click: &ClickValue) -> Quantized {
125 let clicks_f = (angle / click.size).round();
126 Quantized { clicks: clicks_f as i64, residual: angle - clicks_f * click.size }
127}
128
129/// This click's size in milliradians, the crate's true-angular unit. `adjustment_factor`
130/// maps one physical ratio into each display unit, so conversion is a factor ratio —
131/// using the LOCKED printed-table MOA constant (3438; MBA-724), deliberately not 3437.7467.
132pub fn click_size_mil(click: &ClickValue) -> f64 {
133 click.size * adjustment_factor(ClickBase::Mil) / adjustment_factor(click.base)
134}
135
136/// Whole-click adjustment for a drop at a range: the angular value in the graduation's
137/// own base unit divided by the graduation, rounded to the nearest click (ties away from
138/// zero). Sign is preserved. Ranges under 1 yard are defined as zero adjustment, matching
139/// the CLI's `drop_to_adjustment` short-range guard.
140pub fn clicks_for(drop_yd: f64, range_yd: f64, click: &ClickValue) -> i64 {
141 if range_yd < 1.0 {
142 return 0;
143 }
144 let angle = (drop_yd / range_yd) * adjustment_factor(click.base);
145 quantize_angle(angle, click).clicks
146}
147
148/// Zero-banner dial values `(MOA, mrad)` for a solved zero angle, corrected by the
149/// elevation tracking correction factor (MBA-1358).
150///
151/// Direction: the stored CF is the published tall-target ratio `actual measured travel
152/// / dialed travel` (0.95 = the scope under-tracks by 5%). To obtain a true angular
153/// need `N` on a scope whose dialed unit delivers `CF` true units, the number set on
154/// the dial must be `N / CF` — so dial-unit OUTPUTS are DIVIDED by the CF.
155///
156/// Replicates the WASM terminal's historical banner conversions bit-for-bit at
157/// `elevation_cf == 1.0`: `deg * 60.0` (true MOA — deliberately NOT this module's locked
158/// printed-table `Moa = 3438.0` factor; see the MBA-724 note in the wasm banner sites)
159/// and `rad * 1000.0`, each divided by exactly `1.0` (a bit-exact no-op). Lives here,
160/// in an ungated module, so the emit rule is host-testable (`wasm.rs` is wasm32-gated)
161/// and shared by all three banner sites instead of being edited in parallel.
162pub fn zero_banner_dial_values(angle_deg: f64, angle_rad: f64, elevation_cf: f64) -> (f64, f64) {
163 (
164 angle_deg * 60.0 / elevation_cf,
165 angle_rad * 1000.0 / elevation_cf,
166 )
167}
168
169/// MBA-1358: the ONE accepted band for a scope tracking correction factor, shared by the
170/// native CLI and the WASM terminal so the two surfaces cannot drift: strictly between
171/// 0.5 and 1.5 (a real scope does not mistrack by 50% — values outside the band mean the
172/// tall-target test or a hand edit went wrong) and finite.
173pub fn tracking_cf_in_range(value: f64) -> bool {
174 value.is_finite() && value > 0.5 && value < 1.5
175}
176
177#[cfg(test)]
178mod tests {
179 use super::*;
180
181 // MBA-1358: the WASM zero-banner emit rule, host-tested here because wasm.rs is
182 // wasm32-gated (the drag_coefficient_json_value pattern).
183 #[test]
184 fn zero_banner_dial_values_divide_by_the_elevation_cf_and_are_exact_at_one() {
185 let deg = 0.0717_f64;
186 let rad = deg.to_radians();
187 // cf = 1.0 is bit-exact against the historical inline expressions.
188 let (moa, mrad) = zero_banner_dial_values(deg, rad, 1.0);
189 assert_eq!(moa.to_bits(), (deg * 60.0).to_bits());
190 assert_eq!(mrad.to_bits(), (rad * 1000.0).to_bits());
191 // an under-tracking scope (CF < 1) needs MORE dial: outputs divide by the CF.
192 let (moa_cf, mrad_cf) = zero_banner_dial_values(deg, rad, 0.95);
193 assert_eq!(moa_cf.to_bits(), (deg * 60.0 / 0.95).to_bits());
194 assert_eq!(mrad_cf.to_bits(), (rad * 1000.0 / 0.95).to_bits());
195 assert!(moa_cf > moa && mrad_cf > mrad);
196 }
197
198 #[test]
199 fn parse_click_value_accepts_suffixed_and_rejects_bare() {
200 let c = parse_click_value("0.25moa").unwrap();
201 assert!((c.size - 0.25).abs() < 1e-12);
202 assert!(matches!(c.base, ClickBase::Moa));
203 assert!(matches!(parse_click_value("0.1mil").unwrap().base, ClickBase::Mil));
204 assert!(matches!(parse_click_value("0.125smoa").unwrap().base, ClickBase::Smoa));
205 assert!(matches!(parse_click_value("0.125iphy").unwrap().base, ClickBase::Smoa));
206 for bad in ["0.25", "moa", "-0.1mil", "0mil", "0.1mils", ""] {
207 assert!(parse_click_value(bad).is_err(), "{bad:?} must be rejected");
208 }
209 // error message names the accepted suffixes
210 let e = parse_click_value("0.25").unwrap_err();
211 assert!(e.contains("mil") && e.contains("moa"), "{e}");
212 }
213
214 #[test]
215 fn clicks_round_to_nearest_whole_click() {
216 let quarter_moa = parse_click_value("0.25moa").unwrap();
217 // 2.6 TMOA of drop -> 10.4 clicks -> 10
218 let drop_yd = 2.6 / 3438.0 * 100.0;
219 assert_eq!(clicks_for(drop_yd, 100.0, &quarter_moa), 10);
220 // negative (wind left) rounds symmetrically
221 assert_eq!(clicks_for(-drop_yd, 100.0, &quarter_moa), -10);
222 // 10.5 clicks rounds away from zero
223 let drop_yd = 2.625 / 3438.0 * 100.0;
224 assert_eq!(clicks_for(drop_yd, 100.0, &quarter_moa), 11);
225 }
226
227 #[test]
228 fn quantize_reconstruction_identity_is_bit_exact() {
229 // residual is DEFINED as angle - clicks*size, so reconstruction is exact by construction.
230 let c = ClickValue { size: 0.25, base: ClickBase::Moa };
231 for angle in [0.0, 0.24, 0.25, 0.26, -std::f64::consts::PI, 7.4499999, 1234.567] {
232 let q = quantize_angle(angle, &c);
233 assert_eq!(q.clicks as f64 * c.size + q.residual, angle, "identity broke at {angle}");
234 assert!(q.residual.abs() <= c.size / 2.0 + f64::EPSILON, "residual beyond half-click");
235 }
236 }
237
238 #[test]
239 fn quantize_exact_multiples_have_residual_exactly_zero() {
240 let c = ClickValue { size: 0.1, base: ClickBase::Mil };
241 let q = quantize_angle(0.1 * 27.0, &c);
242 assert_eq!(q.clicks, 27);
243 assert_eq!(q.residual, 0.0); // bit-exact, not approx
244 }
245
246 #[test]
247 fn quantize_ties_round_away_from_zero_matching_clicks_for() {
248 let c = ClickValue { size: 0.5, base: ClickBase::Mil };
249 assert_eq!(quantize_angle(0.25, &c).clicks, 1); // f64::round: ties away from zero
250 assert_eq!(quantize_angle(-0.25, &c).clicks, -1);
251 }
252
253 #[test]
254 fn clicks_for_is_unchanged_and_delegates() {
255 // Historical behavior pinned: sub-1yd guard + rounding. Values chosen off any tie.
256 let c = ClickValue { size: 0.25, base: ClickBase::Moa };
257 assert_eq!(clicks_for(10.0, 0.5, &c), 0); // range_yd < 1.0 guard
258 let angle = (7.2 / 300.0) * adjustment_factor(ClickBase::Moa);
259 assert_eq!(clicks_for(7.2, 300.0, &c), (angle / 0.25_f64).round() as i64);
260 }
261
262 #[test]
263 fn click_size_mil_converts_via_the_locked_factor_table() {
264 let moa = ClickValue { size: 0.25, base: ClickBase::Moa };
265 // 0.25 MOA in mil via the LOCKED 3438 constant (MBA-724), not 3437.7467.
266 assert_eq!(click_size_mil(&moa), 0.25 * 1000.0 / 3438.0);
267 let mil = ClickValue { size: 0.1, base: ClickBase::Mil };
268 assert_eq!(click_size_mil(&mil), 0.1);
269 }
270
271 // MBA-1348: ClickValue's JSON wire form is the same suffixed string
272 // `parse_click_value` already parses, not a structural `{size, base}` object.
273 #[test]
274 fn click_value_json_is_pinned_to_the_suffixed_string() {
275 let quarter_moa = ClickValue { size: 0.25, base: ClickBase::Moa };
276 assert_eq!(serde_json::to_string(&quarter_moa).unwrap(), "\"0.25moa\"");
277
278 let tenth_mil = ClickValue { size: 0.1, base: ClickBase::Mil };
279 assert_eq!(serde_json::to_string(&tenth_mil).unwrap(), "\"0.1mil\"");
280
281 // A whole-number size prints without a trailing ".0" -- `f64`'s shortest Display.
282 let one_smoa = ClickValue { size: 1.0, base: ClickBase::Smoa };
283 assert_eq!(serde_json::to_string(&one_smoa).unwrap(), "\"1smoa\"");
284 }
285
286 #[test]
287 fn click_value_round_trips_through_json_including_smoa() {
288 let cases = [
289 ClickValue { size: 0.1, base: ClickBase::Mil },
290 ClickValue { size: 0.25, base: ClickBase::Moa },
291 ClickValue { size: 1.0, base: ClickBase::Smoa },
292 ClickValue { size: 0.125, base: ClickBase::Smoa },
293 ];
294 for click in cases {
295 let json = serde_json::to_string(&click).unwrap();
296 let parsed: ClickValue = serde_json::from_str(&json).unwrap();
297 assert_eq!(parsed, click, "round trip broke for {json}");
298 }
299 }
300
301 #[test]
302 fn click_value_deserialize_rejects_what_parse_click_value_rejects() {
303 // The Deserialize impl IS parse_click_value -- a non-positive or malformed
304 // string must fail to deserialize exactly as it fails to parse.
305 for bad in ["\"0mil\"", "\"-0.1mil\"", "\"0.25\"", "\"nonsense\""] {
306 assert!(
307 serde_json::from_str::<ClickValue>(bad).is_err(),
308 "{bad} must be rejected"
309 );
310 }
311 }
312}