Skip to main content

unbanished_game/behaviour/
sounds.rs

1//! The sound events a character's animation fires (`AnimAudioEvent`): a hammer on an anvil, an
2//! axe, a chicken. Each one fired draws from the game's random numbers, so they are part of the
3//! simulation even though they only make a sound. See docs/AUDIO.md.
4
5use crate::random::Random;
6
7// Runtime MeshInstance::Update (0x180103090) calls AnimationState::Update (0x1801003b0) with its
8// last argument true, so the first layer's events fire on every update (ApplyEvents,
9// 0x1800fd230). The event's slot +0x40 is FUN_14008d310: probability +0x28, effect +0x20, at the
10// entity's transform (+0x10 is its position).
11/// The sound events the characters' clips can fire, and the sounds that passed their draw.
12#[derive(Debug, Clone, Default)]
13pub struct AnimationSounds {
14    /// Each `AnimAudioEvent` the clips name: the event resource, its SoundEffect and its
15    /// probability.
16    pub events: Vec<(String, Option<String>, f32)>,
17    /// The sounds to play since the caller last took them: position and SoundEffect.
18    pub fired: Vec<([f32; 3], String)>,
19    /// How many templates [`AnimationSounds::learn`] has read.
20    templates_seen: usize,
21}
22
23impl AnimationSounds {
24    /// Take in the sound events of templates loaded since the last call (the animals' clips name
25    /// theirs through their templates).
26    pub fn learn(&mut self, templates: &[crate::nature::seeding::Template]) {
27        for t in templates.iter().skip(self.templates_seen) {
28            for e in &t.audio_events {
29                if !self.events.iter().any(|(r, _, _)| *r == e.0) {
30                    self.events.push(e.clone());
31                }
32            }
33        }
34        self.templates_seen = templates.len();
35    }
36
37    /// For each event a character's clip fired, in order: one `RandFloat` from the game's
38    /// generator against the event's probability; when it passes, its sound plays at the
39    /// character. An event that is not a sound draws nothing.
40    ///
41    /// The draws come with each entity's model update in the every-iteration phase, just before
42    /// its character runs, as in the original.
43    ///
44    /// Deviation: the original runs that phase entity by entity in its set's order (by address,
45    /// step 3 of issue #22). The port runs one kind at a time (citizens, animals, boats,
46    /// nomads), each in its own order, and the trees' draws come with the natural resources.
47    ///
48    /// Original: `FUN_14008d310`, called through `MeshInstance::Update` from the
49    /// ModelComponent's every-iteration update (`FUN_140129e30`).
50    pub fn fire(&mut self, resources: &[String], position: [f32; 3], rng: &mut Random) {
51        for r in resources {
52            let Some((_, effect, probability)) = self.events.iter().find(|(e, _, _)| e == r) else {
53                continue;
54            };
55            if rng.rand_float() <= *probability {
56                if let Some(effect) = effect {
57                    self.fired.push((position, effect.clone()));
58                }
59            }
60        }
61    }
62}