pinready 0.24.1

Cross-platform configurator and launcher supporting the adoption of Visual Pinball 10.8.1 — for testers and early adopters, not a replacement for a stable production setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/// Tilt/nudge sensitivity configuration state.
/// All three main parameters are 0–100% sliders for consistency.
#[derive(Debug, Clone)]
pub struct TiltConfig {
    /// Tilt sensitivity 0–100% — 0 insensitive (large angle to trigger tilt), 100 ultra-sensitive (tiny angle triggers).
    /// Written as PlumbThresholdAngle in INI over 0.15°..4°. VPX rev 6177
    /// (e4b5c623d) accepts up to 15° with a 4° default, but a board at 1 g never
    /// takes the plumb past ~1.45° and one at 4 g past ~5.7°: angles above 4°
    /// would only fill the slider with settings that never tilt, so the slider
    /// keeps the range real sensors reach and PinReady writes its own value.
    pub tilt_sensitivity_pct: f32,
    /// PlumbDamping: tilt plumb simulation damping (0..2; VPX default 0.2 since rev 6177,
    /// 1.0 before, which PinReady keeps writing). Replaces the older PlumbInertia.
    pub plumb_damping: f32,
    /// Nudge sensitivity 0–100% — written as `Mapping.Nudge0.Strength`
    /// (0..2, neutral at 1.0), *not* as the mapping's unit scale.
    pub nudge_scale_pct: f32,
    /// Accelerometer full-scale range in g (1, 2, 4 or 8) — written as the
    /// mapping's scale, converted to m/s². mjr recommends 1 g for a cabinet:
    /// "a 1G acceleration is pretty strong in this context".
    pub nudge_range_g: f32,
    /// Nudge deadzone 0–100% — movements below this are ignored (anti-noise)
    pub nudge_deadzone_pct: f32,
    /// Nudge sensor type — VPX `Mapping.Nudge0.Type` enum after the nudge-handler
    /// rewrite (10.8.1 rev 5277+): 0 = Game Controller, 1 = Intent Sensor,
    /// 2 = Cabinet Sensor. Default 1 (recommended for HID accelerometers like
    /// Pinscape — not high-frequency/noise-free enough for direct Cabinet Sensor).
    pub nudge_sensor_type: i32,
}

pub const TILT_ANGLE_MIN: f32 = 0.15;
pub const TILT_ANGLE_MAX: f32 = 4.0;
const TILT_ANGLE_RANGE: f32 = TILT_ANGLE_MAX - TILT_ANGLE_MIN;

/// VPX splits two things PinReady used to conflate. A mapping's `scale` is a
/// **unit conversion**: the accelerometer's physical range, so that a
/// full-scale reading becomes the right number of m/s² (VPX's own sensor page
/// offers 1/2/4/8 g, "which is what Pinscape boards propose"). Sensitivity —
/// how hard a given shake should hit the ball — has its own field,
/// `Mapping.Nudge0.Strength`, running 0..2 around a neutral 1.
///
/// Driving sensitivity through `scale` meant lying to the engine about what
/// the sensor is, and made a 4 g or 8 g board impossible to describe.
pub const GRAVITY: f32 = 9.806_65;
/// Slider percent → Strength, on VPX's own scale: its live UI shows this
/// field multiplied by 100, so neutral reads 100 % there. Using a 0-100 range
/// here would have shown 50 % for the very same setting — two numbers for one
/// value across two screens.
const NUDGE_STRENGTH_PER_PCT: f32 = 1.0 / 100.0;
/// Matching the 0..2 range of the underlying field.
const NUDGE_STRENGTH_MAX_PCT: f32 = 200.0;

impl Default for TiltConfig {
    fn default() -> Self {
        Self {
            // Cabinet-tuned defaults, in the order the sliders appear on the
            // tilt page: nudge 100 % (neutral, VPX's own default for
            // Strength), deadzone 3 %, tilt 75 % = 1.11°, within reach of a
            // 1 g board (VPX's own 4° default since rev 6177 is not). VPX
            // itself sets no deadzone at all; 3 % only swallows the sensor's
            // own noise.
            tilt_sensitivity_pct: 75.0,
            plumb_damping: 1.0,
            nudge_scale_pct: 100.0,
            nudge_range_g: 1.0,
            nudge_deadzone_pct: 3.0,
            nudge_sensor_type: 1,
        }
    }
}

/// The four full-scale ranges Pinscape boards expose and VPX's own sensor
/// page offers.
pub const SENSOR_RANGES_G: [f32; 4] = [1.0, 2.0, 4.0, 8.0];

/// Everything the tilt dial draws, in the one unit that lets a dead zone, an
/// intent threshold and a tilt angle be compared: acceleration as the sensor
/// reads it, in m/s².
///
/// Computing this runs the physics a few dozen times, so callers cache it
/// against [`TiltConfig::rings_key`] rather than recomputing per frame.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TiltRings {
    /// Shove needed to tilt. Infinite when no shove does.
    pub tilt: f32,
    /// Fixed intent threshold, referred back to the sensor: Strength is
    /// applied before the comparison, so raising it moves this inwards.
    /// `None` outside Intent mode, which has no such gate.
    pub intent: Option<f32>,
    /// Dead zone, the first gate the raw signal meets.
    pub deadzone: f32,
    /// Full scale — the rim, and the hard ceiling on everything above.
    pub full_scale: f32,
    /// Usable tilt window per entry of [`SENSOR_RANGES_G`]: the narrowest
    /// threshold the first shove through the filters already trips, and the
    /// widest one full scale still reaches.
    pub reach: [(f32, f32); 4],
    /// Strength, in percent, that would bring the tilt ring back inside full
    /// scale. `None` when it already is, or when even 200 % would not.
    pub strength_to_reach_pct: Option<f32>,
}

impl TiltRings {
    /// Whether the tilt threshold is out of the sensor's reach — the shove it
    /// needs exceeds what the sensor can report, so the table cannot tilt at
    /// all however hard the cabinet is shaken.
    pub fn tilt_out_of_reach(&self) -> bool {
        // Infinity is a legitimate answer here — no shove tilts at all — and
        // it must read as "out of reach", which a plain `>` gives.
        self.tilt.is_infinite() || self.tilt > self.full_scale
    }
}

impl TiltConfig {
    /// Sensor full scale in m/s² — a full-deflection reading.
    pub fn full_scale_ms2(&self) -> f32 {
        self.nudge_range_g * GRAVITY
    }

    /// The dead zone in the units it actually bites in. It is stored as a
    /// fraction of the axis, so the same percentage swallows eight times more
    /// real movement on an 8 g board than on a 1 g one — the one setting
    /// whose effect changes with the range.
    pub fn deadzone_ms2(&self) -> f32 {
        (self.nudge_deadzone_pct / 100.0) * self.full_scale_ms2()
    }

    /// `Mapping.NudgeN.Strength`, on VPX's own 0..2 scale.
    pub fn strength(&self) -> f32 {
        (self.nudge_scale_pct / 100.0).max(0.01)
    }

    /// The plumb threshold angle currently configured.
    pub fn threshold_deg(&self) -> f32 {
        Self::threshold_angle(self.tilt_sensitivity_pct)
    }

    /// Smallest shove that survives the filters and reaches the cabinet: the
    /// dead zone, and in Intent mode the fixed nudge threshold on top of it.
    fn floor_ms2(&self, range_g: f32) -> f32 {
        let deadzone = (self.nudge_deadzone_pct / 100.0) * range_g * GRAVITY;
        if self.nudge_sensor_type == 1 {
            // Referred back to the sensor: Strength multiplies before the
            // comparison, and a sideways nudge carries the 4/3 the intent
            // detector applies to its X axis.
            deadzone.max(crate::nudge_sim::INTENT_THRESHOLD_MS2 / (self.strength() * 4.0 / 3.0))
        } else {
            deadzone
        }
    }

    /// Run the physics and place every ring. A few dozen simulated shoves —
    /// milliseconds, but not per-frame work.
    pub fn rings(&self) -> TiltRings {
        let strength = self.strength();
        let full_scale = self.full_scale_ms2();
        let threshold = self.threshold_deg();
        let sim = |accel: f32| {
            crate::nudge_sim::peak_tilt_angle(
                accel,
                self.nudge_sensor_type,
                strength,
                crate::nudge_sim::DEFAULT_CAB_WEIGHT_KG,
                self.plumb_damping,
            )
        };
        let tilt = crate::nudge_sim::sensor_accel_for_tilt(
            threshold,
            self.nudge_sensor_type,
            strength,
            crate::nudge_sim::DEFAULT_CAB_WEIGHT_KG,
            self.plumb_damping,
        );
        let mut reach = [(TILT_ANGLE_MIN, TILT_ANGLE_MAX); 4];
        for (slot, range_g) in reach.iter_mut().zip(SENSOR_RANGES_G) {
            *slot = (
                sim(self.floor_ms2(range_g)).clamp(TILT_ANGLE_MIN, TILT_ANGLE_MAX),
                sim(range_g * GRAVITY).clamp(TILT_ANGLE_MIN, TILT_ANGLE_MAX),
            );
        }
        // The shove needed is inversely proportional to Strength, so the fix
        // is a division — aimed at 90 % of full scale, since a threshold
        // sitting exactly on the rim needs a perfect shove to reach.
        let strength_to_reach_pct = (tilt > full_scale && tilt.is_finite())
            .then(|| self.nudge_scale_pct * tilt / (0.9 * full_scale))
            .filter(|pct| *pct <= NUDGE_STRENGTH_MAX_PCT);
        TiltRings {
            tilt,
            intent: (self.nudge_sensor_type == 1)
                .then(|| crate::nudge_sim::INTENT_THRESHOLD_MS2 / (strength * 4.0 / 3.0)),
            deadzone: self.deadzone_ms2(),
            full_scale,
            reach,
            strength_to_reach_pct,
        }
    }

    /// Identifies the inputs [`rings`] depends on, so a cached result can be
    /// reused until one of them moves.
    ///
    /// [`rings`]: Self::rings
    pub fn rings_key(&self) -> u64 {
        let mut key = self.nudge_sensor_type as u64;
        for v in [
            self.tilt_sensitivity_pct,
            self.nudge_scale_pct,
            self.nudge_range_g,
            self.nudge_deadzone_pct,
            self.plumb_damping,
        ] {
            key = key.wrapping_mul(0x100_0000_01b3) ^ v.to_bits() as u64;
        }
        key
    }

    /// The plumb threshold angle a given sensitivity percentage writes.
    /// Inverted by nature: 100 % is the *smallest* angle, so the tilt trips
    /// soonest.
    pub fn threshold_angle(sensitivity_pct: f32) -> f32 {
        TILT_ANGLE_MAX - (sensitivity_pct / 100.0) * TILT_ANGLE_RANGE
    }

    pub fn load_from_config(&mut self, config: &crate::config::VpxConfig) {
        if let Some(v) = config.get_f32("Player", "PlumbThresholdAngle") {
            // Inverted mapping: small angle = high sensitivity, large angle = low sensitivity.
            // 0.15° → 100% (ultra-sensitive), 4° → 0% (insensitive).
            self.tilt_sensitivity_pct =
                ((TILT_ANGLE_MAX - v) / TILT_ANGLE_RANGE * 100.0).clamp(0.0, 100.0);
        }
        if let Some(v) = config.get_f32("Player", "PlumbDamping") {
            self.plumb_damping = v;
        }
        // Parse deadzone + scale from the accelerometer axis of the new nudge
        // sensor schema: "device;axis;type;deadZone;scale;limit".
        if let Some(mapping) = config.get("Input", "Mapping.Nudge0.AccX") {
            let parts: Vec<&str> = mapping.split(';').collect();
            if parts.len() >= 5 {
                if let Ok(dz) = parts[3].parse::<f32>() {
                    self.nudge_deadzone_pct = dz * 100.0;
                }
                if let Ok(s) = parts[4].parse::<f32>() {
                    // Snap to the nearest range VPX offers; anything else was
                    // written by hand and is best left readable as its
                    // closest neighbour.
                    let g = (s / GRAVITY).max(0.0);
                    self.nudge_range_g = [1.0, 2.0, 4.0, 8.0]
                        .into_iter()
                        .min_by(|a: &f32, b: &f32| (a - g).abs().total_cmp(&(b - g).abs()))
                        .unwrap_or(1.0);
                }
            }
        }
        if let Some(v) = config.get_i32("Input", "Mapping.Nudge0.Type") {
            self.nudge_sensor_type = v;
        }
        if let Some(v) = config.get_f32("Input", "Mapping.Nudge0.Strength") {
            self.nudge_scale_pct = (v / NUDGE_STRENGTH_PER_PCT).clamp(0.0, NUDGE_STRENGTH_MAX_PCT);
        }
    }

    pub fn save_to_config(&self, config: &mut crate::config::VpxConfig) {
        config.set_plumb_damping(self.plumb_damping);
        // Inverted mapping: 0% (insensitive) → 4°, 100% (ultra-sensitive) → 0.15°.
        config.set_plumb_threshold_angle(Self::threshold_angle(self.tilt_sensitivity_pct));
        // Update scale and deadZone on the accelerometer axes of the new nudge
        // sensor schema, and persist the sensor type.
        self.update_nudge_mapping(config, "Nudge0.AccX");
        self.update_nudge_mapping(config, "Nudge0.AccY");
        config.set_i32("Input", "Mapping.Nudge0.Type", self.nudge_sensor_type);
        config.set(
            "Input",
            "Mapping.Nudge0.Strength",
            &format!("{:.6}", self.nudge_scale_pct * NUDGE_STRENGTH_PER_PCT),
        );
    }

    fn update_nudge_mapping(&self, config: &mut crate::config::VpxConfig, key: &str) {
        let mapping_key = format!("Mapping.{key}");
        if let Some(mapping) = config.get("Input", &mapping_key) {
            let parts: Vec<&str> = mapping.split(';').collect();
            // Format: device;axis;type;deadZone;scale;limit
            if parts.len() >= 6 {
                let new_mapping = format!(
                    "{};{};{};{:.6};{:.6};{}",
                    parts[0],
                    parts[1],
                    parts[2],
                    self.nudge_deadzone_pct / 100.0,
                    self.nudge_range_g * GRAVITY,
                    parts[5]
                );
                config.set("Input", &mapping_key, &new_mapping);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::VpxConfig;
    use std::io::Write;
    use tempfile::NamedTempFile;

    fn config_from_str(content: &str) -> VpxConfig {
        let mut tmp = NamedTempFile::new().unwrap();
        tmp.write_all(content.as_bytes()).unwrap();
        VpxConfig::load(Some(tmp.path())).unwrap()
    }

    #[test]
    fn the_dial_orders_its_rings_the_way_the_filters_apply() {
        let tilt = TiltConfig::default();
        let rings = tilt.rings();
        assert!(rings.deadzone > 0.0);
        assert!(
            rings.deadzone < rings.intent.unwrap(),
            "the dead zone gates the raw signal before the intent threshold sees it"
        );
        assert!(
            rings.intent.unwrap() < rings.tilt,
            "clearing the nudge threshold must be easier than tilting"
        );
        // The defaults sit inside a 1 g board's reach, but only just — the
        // whole reason the page has to say so.
        assert!(!rings.tilt_out_of_reach(), "tilt at {}", rings.tilt);
        assert!(rings.tilt > 0.6 * rings.full_scale);
    }

    #[test]
    fn cabinet_mode_has_no_intent_ring() {
        let tilt = TiltConfig {
            nudge_sensor_type: 2,
            ..Default::default()
        };
        assert!(tilt.rings().intent.is_none());
    }

    #[test]
    fn a_wide_tilt_angle_falls_out_of_a_1g_sensor_reach() {
        let tilt = TiltConfig {
            tilt_sensitivity_pct: 0.0, // the widest angle, 4°
            ..Default::default()
        };
        let rings = tilt.rings();
        assert!(rings.tilt_out_of_reach(), "tilt at {}", rings.tilt);
        // 4° needs more than twice full scale at 1 g, so no strength within
        // VPX's 0..2 range brings it back — only a wider sensor range does.
        assert!(rings.strength_to_reach_pct.is_none());
        assert!(
            rings.reach[0].1 < 4.0 && rings.reach[2].1 >= 4.0,
            "a 1 g board should fall short where a 4 g one reaches: {:?}",
            rings.reach
        );
    }

    #[test]
    fn strength_is_offered_when_it_would_actually_help() {
        // Just out of reach: a threshold a stronger setting can recover.
        let tilt = TiltConfig {
            tilt_sensitivity_pct: 62.0,
            ..Default::default()
        };
        let rings = tilt.rings();
        assert!(rings.tilt_out_of_reach(), "tilt at {}", rings.tilt);
        let pct = rings
            .strength_to_reach_pct
            .expect("a reachable fix should be offered");
        assert!((100.0..=200.0).contains(&pct), "got {pct}");
        let fixed = TiltConfig {
            nudge_scale_pct: pct,
            ..tilt.clone()
        };
        assert!(
            !fixed.rings().tilt_out_of_reach(),
            "applying the offered strength should bring the tilt within reach"
        );
    }

    #[test]
    fn the_dead_zone_bites_in_proportion_to_the_range() {
        // The one setting whose real effect follows the range, which is why
        // the page prints it in m/s² next to its percentage.
        let narrow = TiltConfig::default();
        let wide = TiltConfig {
            nudge_range_g: 8.0,
            ..Default::default()
        };
        assert!((wide.deadzone_ms2() / narrow.deadzone_ms2() - 8.0).abs() < 0.01);
    }

    #[test]
    fn the_rings_cache_key_tracks_every_input_it_depends_on() {
        let base = TiltConfig::default();
        for changed in [
            TiltConfig {
                tilt_sensitivity_pct: 50.0,
                ..base.clone()
            },
            TiltConfig {
                nudge_scale_pct: 150.0,
                ..base.clone()
            },
            TiltConfig {
                nudge_range_g: 2.0,
                ..base.clone()
            },
            TiltConfig {
                nudge_deadzone_pct: 10.0,
                ..base.clone()
            },
            TiltConfig {
                plumb_damping: 0.5,
                ..base.clone()
            },
            TiltConfig {
                nudge_sensor_type: 2,
                ..base.clone()
            },
        ] {
            assert_ne!(base.rings_key(), changed.rings_key());
        }
    }

    #[test]
    fn default_values() {
        let tilt = TiltConfig::default();
        assert!((tilt.tilt_sensitivity_pct - 75.0).abs() < f32::EPSILON);
        assert!((tilt.plumb_damping - 1.0).abs() < f32::EPSILON);
        assert!((tilt.nudge_scale_pct - 100.0).abs() < f32::EPSILON);
        assert!((tilt.nudge_deadzone_pct - 3.0).abs() < f32::EPSILON);
    }

    #[test]
    fn load_from_config_reads_values() {
        // PlumbThresholdAngle=2.075 (mid range 0.15..4) → pct = (4 - 2.075)/3.85 * 100 = 50%
        let cfg = config_from_str(
            "[Player]\nPlumbThresholdAngle = 2.075\nPlumbDamping = 0.5\n\
             [Input]\nMapping.Nudge0.AccX = dev;512;A;0.1;9.80665;1.0\n",
        );
        let mut tilt = TiltConfig::default();
        tilt.load_from_config(&cfg);
        assert!((tilt.tilt_sensitivity_pct - 50.0).abs() < 0.1);
        assert!((tilt.plumb_damping - 0.5).abs() < f32::EPSILON);
        // The mapping declares 1 g; sensitivity is untouched by that and
        // keeps its default, since no Strength is present in this ini.
        assert!((tilt.nudge_range_g - 1.0).abs() < 0.01);
        assert!((tilt.nudge_scale_pct - 100.0).abs() < 0.1);
        assert!((tilt.nudge_deadzone_pct - 10.0).abs() < 0.1);
    }

    #[test]
    fn load_from_config_keeps_defaults_when_empty() {
        let cfg = config_from_str("");
        let mut tilt = TiltConfig::default();
        tilt.load_from_config(&cfg);
        assert!((tilt.tilt_sensitivity_pct - 75.0).abs() < f32::EPSILON);
        assert!((tilt.plumb_damping - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn load_from_config_inverted_sensitivity() {
        // 0.15° (min angle, ultra-sensitive) → 100%, 4° (max angle, insensitive) → 0%
        let ultra = config_from_str("[Player]\nPlumbThresholdAngle = 0.15\n");
        let mut t = TiltConfig::default();
        t.load_from_config(&ultra);
        assert!((t.tilt_sensitivity_pct - 100.0).abs() < 0.1);

        let insens = config_from_str("[Player]\nPlumbThresholdAngle = 4.0\n");
        let mut t = TiltConfig::default();
        t.load_from_config(&insens);
        assert!(t.tilt_sensitivity_pct.abs() < 0.1);
    }

    #[test]
    fn save_to_config_writes_values() {
        let mut cfg = config_from_str("");
        let tilt = TiltConfig {
            tilt_sensitivity_pct: 50.0, // mid → angle 2.075°
            plumb_damping: 0.7,
            nudge_scale_pct: 150.0,
            nudge_deadzone_pct: 20.0,
            ..Default::default()
        };
        tilt.save_to_config(&mut cfg);
        let angle = cfg.get_f32("Player", "PlumbThresholdAngle").unwrap();
        assert!((angle - 2.075).abs() < 0.01);
        let damping = cfg.get_f32("Player", "PlumbDamping").unwrap();
        assert!((damping - 0.7).abs() < f32::EPSILON);
    }

    #[test]
    fn save_to_config_inverted_sensitivity() {
        // pct=100 (ultra) → smallest angle, pct=0 (insensitive) → largest angle
        let mut cfg = config_from_str("");
        let ultra = TiltConfig {
            tilt_sensitivity_pct: 100.0,
            ..Default::default()
        };
        ultra.save_to_config(&mut cfg);
        let a = cfg.get_f32("Player", "PlumbThresholdAngle").unwrap();
        assert!((a - 0.15).abs() < 0.01, "expected 0.15, got {a}");

        let insens = TiltConfig {
            tilt_sensitivity_pct: 0.0,
            ..Default::default()
        };
        insens.save_to_config(&mut cfg);
        let a = cfg.get_f32("Player", "PlumbThresholdAngle").unwrap();
        assert!((a - 4.0).abs() < 0.01, "expected 4.0, got {a}");
    }

    #[test]
    fn save_roundtrip() {
        // PlumbThresholdAngle=3.0 → pct ≈ 28.57 → save back → 3.0
        let ini = "[Player]\nPlumbThresholdAngle = 3.0\nPlumbDamping = 0.8\n\
                    [Input]\nMapping.Nudge0.AccX = dev;512;A;0.05;0.3;1.0\nMapping.Nudge0.AccY = dev;513;A;0.05;0.3;1.0\n";
        let mut cfg = config_from_str(ini);
        let mut tilt = TiltConfig::default();
        tilt.load_from_config(&cfg);
        tilt.nudge_scale_pct = 200.0;
        tilt.nudge_range_g = 2.0;
        tilt.save_to_config(&mut cfg);

        // The mapping scale is the sensor range in m/s^2 …
        let mapping = cfg.get("Input", "Mapping.Nudge0.AccX").unwrap();
        assert!(
            mapping.contains("19.613300"),
            "expected the 2g range in: {mapping}"
        );
        // … and the slider rides Strength, 100 % being VPX's 2.0 ceiling.
        let strength = cfg.get_f32("Input", "Mapping.Nudge0.Strength").unwrap();
        assert!(
            (strength - 2.0).abs() < 0.001,
            "expected 2.0, got {strength}"
        );

        let angle = cfg.get_f32("Player", "PlumbThresholdAngle").unwrap();
        assert!((angle - 3.0).abs() < 0.01);
    }

    #[test]
    fn update_nudge_mapping_preserves_device_and_axis() {
        let ini = "[Input]\nMapping.Nudge0.AccX = SDLJoy_PSC004;512;A;0.000000;0.300000;1.000000\n";
        let mut cfg = config_from_str(ini);
        let tilt = TiltConfig {
            nudge_scale_pct: 50.0,
            ..Default::default()
        };
        tilt.save_to_config(&mut cfg);
        let mapping = cfg.get("Input", "Mapping.Nudge0.AccX").unwrap();
        assert!(mapping.starts_with("SDLJoy_PSC004;512;A;"));
        assert!(mapping.contains("9.806650"));
        assert!(mapping.ends_with(";1.000000"));
    }

    #[test]
    fn nudge_scale_parsed_from_mapping() {
        // A mapping written for a 4g board reads back as 4g, and the
        // sensitivity comes from Strength rather than from that scale.
        let cfg = config_from_str(
            "[Input]\nMapping.Nudge0.AccX = dev;512;A;0.000000;39.226600;1.000000\n\
             Mapping.Nudge0.Strength = 1.500000\n",
        );
        let mut tilt = TiltConfig::default();
        tilt.load_from_config(&cfg);
        assert!(
            (tilt.nudge_range_g - 4.0).abs() < 0.01,
            "got {}",
            tilt.nudge_range_g
        );
        assert!(
            (tilt.nudge_scale_pct - 150.0).abs() < 0.1,
            "got {}",
            tilt.nudge_scale_pct
        );
    }
}