flatland_client_lib/
audio.rs1use crate::game::GameState;
7use crate::social::AudioCue;
8
9pub fn ui_overlay_signature(state: &GameState) -> u64 {
11 let mut s = 0u64;
12 macro_rules! bit {
13 ($n:expr, $cond:expr) => {
14 if $cond {
15 s |= 1u64 << $n;
16 }
17 };
18 }
19 bit!(0, state.show_stats);
20 bit!(1, state.show_inventory_menu);
21 bit!(2, state.show_craft_menu);
22 bit!(3, state.show_plot_build_menu);
23 bit!(4, state.show_shop_menu);
24 bit!(5, state.bank_panel.is_some());
25 bit!(6, state.storage_panel.is_some());
26 bit!(7, state.market_panel.is_some());
27 bit!(8, state.show_npc_verb_menu);
28 bit!(9, state.show_npc_chat);
29 bit!(10, state.player_verbs.open);
30 bit!(11, state.trade_ui.panel.is_some());
31 bit!(12, state.whisper_pouch_ui.open);
32 bit!(13, state.social_chat.input_focused);
33 bit!(14, state.social_chat.picking_stone);
34 bit!(15, state.show_move_picker);
35 bit!(16, state.show_grant_picker);
36 bit!(17, state.show_destroy_picker);
37 bit!(18, state.show_rename_prompt);
38 bit!(19, state.show_worker_rename);
39 bit!(20, state.show_loadout_menu);
40 bit!(21, state.show_keychain_menu);
41 bit!(22, state.show_rotation_editor);
42 bit!(23, state.show_quest_offer);
43 bit!(24, state.show_quest_menu);
44 bit!(25, state.show_workers_menu);
45 bit!(26, state.show_worker_give_picker);
46 bit!(27, state.show_worker_give_target_picker);
47 bit!(28, state.show_worker_take_picker);
48 bit!(29, state.show_worker_teach_picker);
49 bit!(30, state.worker_route_editor.is_some());
50 bit!(31, state.show_plant_menu);
51 bit!(32, state.show_farm_access);
52 bit!(33, state.show_equip_menu);
53 bit!(34, state.claim_mode.is_some());
54 bit!(35, state.relocate_mode.is_some());
55 s
56}
57
58const UI_BIT_SHOP: u64 = 1 << 4;
59const UI_BIT_STORAGE: u64 = 1 << 6;
60const UI_BIT_NPC: u64 = (1 << 8) | (1 << 9);
61
62fn cue_for_ui_open(opened: u64) -> Option<AudioCue> {
63 if opened == 0 {
64 return None;
65 }
66 if opened & UI_BIT_SHOP != 0 {
67 return Some(AudioCue::ShopOpen);
68 }
69 if opened & UI_BIT_STORAGE != 0 {
70 return Some(AudioCue::ChestOpen);
71 }
72 if opened & UI_BIT_NPC != 0 {
73 return Some(AudioCue::NpcGreet);
74 }
75 Some(AudioCue::UiOpen)
76}
77
78pub fn sync_ui_audio(state: &mut GameState) {
80 let sig = ui_overlay_signature(state);
81 let audio = &mut state.social_chat;
82 if !audio.audio_ui_bootstrapped {
83 audio.audio_ui_sig = sig;
84 audio.audio_ui_bootstrapped = true;
85 return;
86 }
87 let prev = audio.audio_ui_sig;
88 if sig == prev {
89 return;
90 }
91 let opened = sig & !prev;
92 let closed = prev & !sig;
93 if let Some(cue) = cue_for_ui_open(opened) {
94 audio.push_cue(cue);
95 }
96 if closed != 0 {
97 audio.push_cue(AudioCue::UiClose);
98 }
99 audio.audio_ui_sig = sig;
100}
101
102pub fn sync_world_audio(state: &mut GameState) {
104 let inside = state.effective_inside_building();
105 let audio = &mut state.social_chat;
106
107 if !audio.audio_world_bootstrapped {
108 audio.audio_inside_building = inside.clone();
109 for door in &state.doors {
110 audio.audio_door_open.insert(door.id.clone(), door.open);
111 audio.audio_door_locked.insert(door.id.clone(), door.locked);
112 }
113 for chest in &state.placed_containers {
114 audio
115 .audio_chest_locked
116 .insert(chest.id.clone(), chest.locked);
117 }
118 audio.audio_world_bootstrapped = true;
119 return;
120 }
121
122 match (&audio.audio_inside_building, &inside) {
123 (None, Some(_)) => audio.push_cue(AudioCue::BuildingEnter),
124 (Some(_), None) => audio.push_cue(AudioCue::BuildingExit),
125 _ => {}
126 }
127 audio.audio_inside_building = inside;
128
129 for door in &state.doors {
130 let id = &door.id;
131 if let Some(was_open) = audio.audio_door_open.get(id).copied() {
132 if !was_open && door.open {
133 audio.push_cue(AudioCue::DoorOpen);
134 } else if was_open && !door.open {
135 audio.push_cue(AudioCue::DoorClose);
136 }
137 }
138 if let Some(was_locked) = audio.audio_door_locked.get(id).copied() {
139 if !was_locked && door.locked {
140 audio.push_cue(AudioCue::DoorLock);
141 } else if was_locked && !door.locked {
142 audio.push_cue(AudioCue::DoorUnlock);
143 }
144 }
145 audio.audio_door_open.insert(id.clone(), door.open);
146 audio.audio_door_locked.insert(id.clone(), door.locked);
147 }
148
149 audio
151 .audio_door_open
152 .retain(|id, _| state.doors.iter().any(|d| &d.id == id));
153 audio
154 .audio_door_locked
155 .retain(|id, _| state.doors.iter().any(|d| &d.id == id));
156
157 for chest in &state.placed_containers {
158 let id = &chest.id;
159 if let Some(was_locked) = audio.audio_chest_locked.get(id).copied() {
160 if !was_locked && chest.locked {
161 audio.push_cue(AudioCue::ChestLock);
162 } else if was_locked && !chest.locked {
163 audio.push_cue(AudioCue::ChestUnlock);
164 }
165 }
166 audio.audio_chest_locked.insert(id.clone(), chest.locked);
167 }
168 audio
169 .audio_chest_locked
170 .retain(|id, _| state.placed_containers.iter().any(|c| &c.id == id));
171}
172
173pub fn sync_frame_audio(state: &mut GameState) {
175 sync_ui_audio(state);
176 sync_world_audio(state);
177}