aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/audio/capture_voice.rs
blob: 86ecee63e462cb19e109588be2262c667616399f (plain) (blame)
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
use super::{GameAudio, VariationRng};

// iron3d+0x8ecc0 selects bank 3 for warbots and mobile command centres.
const ACKNOWLEDGEMENTS: [&str; 5] = [
    "VOICE_ACKNOWLEDGE_B",
    "VOICE_AFFIRMATIVE_B",
    "VOICE_OK_B",
    "VOICE_YES_SIR_B",
    "VOICE_EXECUTE_B",
];

pub(super) struct VehicleCaptureVoice {
    rng: VariationRng,
    previous: i32,
}

impl VehicleCaptureVoice {
    pub(super) fn new(seed: u32) -> Self {
        Self {
            rng: VariationRng::from_clock(seed),
            previous: -1,
        }
    }

    fn next(&mut self) -> &'static str {
        let index = self
            .rng
            .choose(ACKNOWLEDGEMENTS.len(), &mut self.previous)
            .expect("non-empty acknowledgement bank");
        ACKNOWLEDGEMENTS[index]
    }
}

impl GameAudio {
    pub(crate) fn queue_vehicle_capture_voice(&mut self) -> Result<String, String> {
        let symbol = self.vehicle_capture_voice.next();
        self.queue_game_voice(symbol)
    }
}

#[cfg(test)]
mod tests {
    use super::{VehicleCaptureVoice, ACKNOWLEDGEMENTS};

    #[test]
    fn capture_acknowledgements_follow_the_native_bank_order_and_skip_repeats() {
        let mut voice = VehicleCaptureVoice::new(0x156a_e3bf);
        let expected = [2, 3, 2, 3, 2, 4, 3, 4, 3, 0].map(|i| ACKNOWLEDGEMENTS[i]);
        let actual = std::array::from_fn::<_, 10, _>(|_| voice.next());
        assert_eq!(actual, expected);
    }
}