squad_ui.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. use egui::{Color32, RichText};
  2. use crate::sim::inventory::ItemKind;
  3. use crate::sim::orders::{AiAssistRequest, RulesOfEngagement, SquadFormation};
  4. use crate::sim::radio::{RadioEar, RadioState, FREQ_STEP_KHZ, MAX_FREQ_KHZ, MIN_FREQ_KHZ};
  5. use crate::sim::Simulation;
  6. #[derive(Clone, Debug)]
  7. pub struct TacticalWindowsState {
  8. pub show_squad: bool,
  9. pub show_radio: bool,
  10. pub show_inventory: bool,
  11. pub show_ai_bar: bool,
  12. pub new_squad_name: String,
  13. pub radio_loopback_test: bool,
  14. }
  15. impl Default for TacticalWindowsState {
  16. fn default() -> Self {
  17. Self {
  18. show_squad: false,
  19. show_radio: false,
  20. show_inventory: false,
  21. show_ai_bar: false,
  22. new_squad_name: "Alpha-2".to_string(),
  23. radio_loopback_test: false,
  24. }
  25. }
  26. }
  27. #[derive(Default)]
  28. pub struct TacticalWindowActions {
  29. pub radio_tuned: bool,
  30. pub create_squad_name: Option<String>,
  31. pub join_squad_id: Option<usize>,
  32. pub leave_squad: bool,
  33. pub recruit_ai_squad_id: Option<usize>,
  34. pub set_formation: Option<(usize, SquadFormation)>,
  35. pub set_roe: Option<(usize, RulesOfEngagement)>,
  36. pub set_squad_freq: Option<(usize, u32)>,
  37. pub deploy_rally: bool,
  38. pub ai_assist: Option<AiAssistRequest>,
  39. pub use_item: Option<ItemKind>,
  40. pub do_mag_check: bool,
  41. pub do_mag_repack: bool,
  42. }
  43. pub fn draw_tactical_windows(
  44. ctx: &egui::Context,
  45. sim: &mut Simulation,
  46. state: &mut TacticalWindowsState,
  47. mic_available: bool,
  48. ) -> TacticalWindowActions {
  49. let mut actions = TacticalWindowActions::default();
  50. if state.show_squad {
  51. draw_squad_manager_window(ctx, sim, state, &mut actions);
  52. }
  53. if state.show_radio {
  54. draw_radio_tuner_window(ctx, sim, state, mic_available, &mut actions);
  55. }
  56. if state.show_inventory {
  57. draw_inventory_window(ctx, sim, state, &mut actions);
  58. }
  59. if state.show_ai_bar {
  60. draw_ai_quick_bar(ctx, sim, state, &mut actions);
  61. }
  62. actions
  63. }
  64. fn draw_squad_manager_window(
  65. ctx: &egui::Context,
  66. sim: &mut Simulation,
  67. state: &mut TacticalWindowsState,
  68. actions: &mut TacticalWindowActions,
  69. ) {
  70. let my_sq = sim.player_squad_id();
  71. let my_faction = sim.player.faction;
  72. let mut open = state.show_squad;
  73. egui::Window::new("🛡 Squad Manager [U]")
  74. .open(&mut open)
  75. .default_width(370.0)
  76. .resizable(true)
  77. .show(ctx, |ui| {
  78. ui.horizontal(|ui| {
  79. ui.label("New Squad:");
  80. ui.text_edit_singleline(&mut state.new_squad_name);
  81. if ui.button("Create").clicked() {
  82. actions.create_squad_name = Some(state.new_squad_name.clone());
  83. }
  84. if my_sq.is_some() && ui.button("Leave").clicked() {
  85. actions.leave_squad = true;
  86. }
  87. });
  88. ui.separator();
  89. ui.label(RichText::new("Friendly Squads:").strong());
  90. egui::ScrollArea::vertical().max_height(155.0).show(ui, |ui| {
  91. for sq in &sim.squads {
  92. if sq.faction != my_faction {
  93. continue;
  94. }
  95. let alive_cnt = sq
  96. .member_ids
  97. .iter()
  98. .filter(|&&id| sim.soldiers.get(id).is_some_and(|s| s.alive))
  99. .count();
  100. let is_mine = my_sq == Some(sq.id);
  101. ui.horizontal(|ui| {
  102. let col = if is_mine {
  103. Color32::from_rgb(135, 240, 155)
  104. } else {
  105. Color32::from_rgb(220, 224, 230)
  106. };
  107. ui.colored_label(
  108. col,
  109. format!(
  110. "{} ({} alive) | {} | {} | {:.3} MHz",
  111. sq.name,
  112. alive_cnt,
  113. sq.formation.label(),
  114. sq.roe.label(),
  115. sq.radio_freq_khz as f32 / 1000.0
  116. ),
  117. );
  118. if !is_mine && !sq.locked && ui.small_button("Join").clicked() {
  119. actions.join_squad_id = Some(sq.id);
  120. }
  121. });
  122. }
  123. });
  124. if let Some(sq_id) = my_sq {
  125. if let Some(sq) = sim.squads.get_mut(sq_id) {
  126. ui.separator();
  127. ui.horizontal(|ui| {
  128. ui.strong(format!("Active Squad: {}", sq.name));
  129. ui.checkbox(&mut sq.locked, "Lock Squad");
  130. });
  131. ui.horizontal(|ui| {
  132. ui.label("Formation:");
  133. for &fmt in &SquadFormation::ALL {
  134. if ui.selectable_label(sq.formation == fmt, fmt.label()).clicked() {
  135. actions.set_formation = Some((sq_id, fmt));
  136. }
  137. }
  138. });
  139. ui.horizontal(|ui| {
  140. ui.label("ROE:");
  141. for &roe in &RulesOfEngagement::ALL {
  142. if ui.selectable_label(sq.roe == roe, roe.label()).clicked() {
  143. actions.set_roe = Some((sq_id, roe));
  144. }
  145. }
  146. });
  147. let freq = sq.radio_freq_khz;
  148. ui.horizontal(|ui| {
  149. if ui.button("Recruit Nearby AI (+4)").clicked() {
  150. actions.recruit_ai_squad_id = Some(sq_id);
  151. }
  152. if ui.button("Deploy Rally Point").clicked() {
  153. actions.deploy_rally = true;
  154. }
  155. if ui
  156. .button(format!("Sync Squad Net ({:.3} MHz)", freq as f32 / 1000.0))
  157. .clicked()
  158. {
  159. actions.set_squad_freq = Some((sq_id, sim.player.radio.active_freq_khz()));
  160. }
  161. });
  162. }
  163. } else {
  164. ui.separator();
  165. ui.weak("Create or join a friendly squad to manage formations, ROE & rally beacons.");
  166. }
  167. });
  168. state.show_squad = open;
  169. }
  170. fn draw_radio_tuner_window(
  171. ctx: &egui::Context,
  172. sim: &mut Simulation,
  173. state: &mut TacticalWindowsState,
  174. mic_available: bool,
  175. actions: &mut TacticalWindowActions,
  176. ) {
  177. let mut open = state.show_radio;
  178. egui::Window::new("📻 Tactical VHF Radio [N]")
  179. .open(&mut open)
  180. .default_width(350.0)
  181. .resizable(true)
  182. .show(ctx, |ui| {
  183. let radio = &mut sim.player.radio;
  184. ui.horizontal(|ui| {
  185. ui.strong(radio.radio_type.label());
  186. ui.separator();
  187. if ui.checkbox(&mut radio.encrypted, "Encrypt (Faction Key)").clicked() {
  188. actions.radio_tuned = true;
  189. }
  190. });
  191. ui.weak("Clear traffic can be heard across factions; Enable Encrypt to block eavesdropping.");
  192. ui.separator();
  193. // Primary channel
  194. ui.label(
  195. RichText::new(format!(
  196. "Primary Channel: {}",
  197. RadioState::format_freq_mhz(radio.primary_freq_khz)
  198. ))
  199. .size(16.0)
  200. .color(Color32::from_rgb(135, 235, 165)),
  201. );
  202. let mut mhz = radio.primary_freq_khz as f32 / 1000.0;
  203. if ui
  204. .add(
  205. egui::Slider::new(
  206. &mut mhz,
  207. (MIN_FREQ_KHZ as f32 / 1000.0)..=(MAX_FREQ_KHZ as f32 / 1000.0),
  208. )
  209. .step_by(FREQ_STEP_KHZ as f64 / 1000.0)
  210. .fixed_decimals(3)
  211. .text("MHz"),
  212. )
  213. .changed()
  214. {
  215. radio.tune_primary((mhz * 1000.0).round() as u32);
  216. actions.radio_tuned = true;
  217. }
  218. ui.horizontal(|ui| {
  219. for (lbl, delta) in [("-1 MHz", -1000), ("-25 kHz", -25), ("+25 kHz", 25), ("+1 MHz", 1000)] {
  220. if ui.small_button(lbl).clicked() {
  221. radio.step_primary(delta);
  222. actions.radio_tuned = true;
  223. }
  224. }
  225. });
  226. ui.horizontal(|ui| {
  227. ui.label("Ear Routing:");
  228. for &ear in &RadioEar::ALL {
  229. if ui.selectable_label(radio.primary_ear == ear, ear.label()).clicked() {
  230. radio.primary_ear = ear;
  231. actions.radio_tuned = true;
  232. }
  233. }
  234. });
  235. ui.horizontal(|ui| {
  236. ui.label("Presets:");
  237. let presets = radio.presets_khz;
  238. for (idx, p) in presets.iter().enumerate() {
  239. if ui
  240. .small_button(format!("Ch{} {:.2}", idx + 1, *p as f32 / 1000.0))
  241. .clicked()
  242. {
  243. radio.tune_primary(*p);
  244. actions.radio_tuned = true;
  245. }
  246. }
  247. });
  248. if radio.radio_type.supports_dual_watch() {
  249. ui.separator();
  250. let sec_khz = radio.secondary_freq_khz.unwrap_or(MIN_FREQ_KHZ);
  251. ui.label(
  252. RichText::new(format!(
  253. "Secondary Watch (Manpack): {}",
  254. radio
  255. .secondary_freq_khz
  256. .map_or_else(|| "OFF".into(), RadioState::format_freq_mhz)
  257. ))
  258. .color(Color32::from_rgb(160, 210, 250)),
  259. );
  260. ui.horizontal(|ui| {
  261. if ui.small_button("Command 68.0").clicked() {
  262. radio.tune_secondary(Some(68_000));
  263. actions.radio_tuned = true;
  264. }
  265. if ui.small_button("-25k").clicked() {
  266. radio.tune_secondary(Some(sec_khz.saturating_sub(25)));
  267. actions.radio_tuned = true;
  268. }
  269. if ui.small_button("+25k").clicked() {
  270. radio.tune_secondary(Some(sec_khz + 25));
  271. actions.radio_tuned = true;
  272. }
  273. if ui.small_button("Disable").clicked() {
  274. radio.tune_secondary(None);
  275. actions.radio_tuned = true;
  276. }
  277. });
  278. ui.horizontal(|ui| {
  279. ui.label("Secondary Ear:");
  280. for &ear in &RadioEar::ALL {
  281. if ui.selectable_label(radio.secondary_ear == ear, ear.label()).clicked() {
  282. radio.secondary_ear = ear;
  283. actions.radio_tuned = true;
  284. }
  285. }
  286. });
  287. }
  288. ui.separator();
  289. ui.horizontal(|ui| {
  290. let mic_col = if mic_available {
  291. Color32::from_rgb(120, 235, 145)
  292. } else {
  293. Color32::from_rgb(240, 175, 95)
  294. };
  295. ui.colored_label(
  296. mic_col,
  297. if mic_available { "Mic: Ready" } else { "Mic: Unavailable" },
  298. );
  299. ui.checkbox(&mut state.radio_loopback_test, "Loopback Mic Test (DSP)");
  300. });
  301. ui.weak("PTT Keys: Hold [CapsLock] Proximity (35m BVH occluded) | Hold [X] Radio TX");
  302. ui.separator();
  303. ui.strong("Comms & Radio Log:");
  304. egui::ScrollArea::vertical().max_height(130.0).show(ui, |ui| {
  305. for entry in sim.radio_log.iter().rev() {
  306. let prefix = if entry.is_proximity {
  307. "[PROX]".to_string()
  308. } else if let Some(f) = entry.freq_khz {
  309. format!("[{:.3}]", f as f32 / 1000.0)
  310. } else {
  311. "[NET]".to_string()
  312. };
  313. ui.label(format!("{prefix} {}: {}", entry.sender, entry.text));
  314. }
  315. });
  316. });
  317. state.show_radio = open;
  318. }
  319. fn draw_inventory_window(
  320. ctx: &egui::Context,
  321. sim: &mut Simulation,
  322. state: &mut TacticalWindowsState,
  323. actions: &mut TacticalWindowActions,
  324. ) {
  325. let mut open = state.show_inventory;
  326. egui::Window::new("🎒 Inventory, Kit & Magazines [I]")
  327. .open(&mut open)
  328. .default_width(360.0)
  329. .resizable(true)
  330. .show(ctx, |ui| {
  331. let p = &mut sim.player;
  332. let weight = p.inventory.total_weight_kg(&p.mag_banks);
  333. let spd_pct = (p.inventory.speed_multiplier(weight) * 100.0).round();
  334. ui.horizontal(|ui| {
  335. ui.strong(format!("Kit: {}", p.inventory.kit.label()));
  336. ui.separator();
  337. ui.label(format!("Load: {weight:.1} kg (Speed {spd_pct:.0}%)"));
  338. });
  339. if p.bleed_rate > 0.01 {
  340. ui.colored_label(
  341. Color32::from_rgb(245, 95, 85),
  342. format!("⚠ BLEEDING ({:.1} HP/s) — Use Field Bandage [7]!", p.bleed_rate),
  343. );
  344. }
  345. ui.separator();
  346. ui.strong("Field Gear & Consumables:");
  347. let items = p.inventory.items.clone();
  348. for slot in items {
  349. if slot.count == 0 {
  350. continue;
  351. }
  352. ui.horizontal(|ui| {
  353. ui.label(format!("{} x{}", slot.kind.label(), slot.count));
  354. if !matches!(slot.kind, ItemKind::RadioUnit(_)) && ui.small_button("Use").clicked() {
  355. actions.use_item = Some(slot.kind);
  356. }
  357. });
  358. }
  359. ui.separator();
  360. let (loaded_rounds, can_repack, chambered, cap, spares, w_name) = {
  361. let bank = p.current_mag_state();
  362. (
  363. bank.loaded_mag_rounds(),
  364. bank.can_repack(),
  365. bank.chambered,
  366. bank.capacity,
  367. bank.spare_mags.clone(),
  368. p.weapon.spec().name,
  369. )
  370. };
  371. ui.horizontal(|ui| {
  372. ui.strong(format!("Magazines ({w_name}):"));
  373. if ui.small_button("Mag Check [Ctrl+R]").clicked() {
  374. actions.do_mag_check = true;
  375. }
  376. if can_repack && ui.small_button("Repack [Shift+R]").clicked() {
  377. actions.do_mag_repack = true;
  378. }
  379. });
  380. ui.checkbox(&mut p.show_exact_ammo_hud, "Show Exact Round Counts on HUD");
  381. let chamber_str = if chambered { "+1 in chamber" } else { "chamber empty" };
  382. ui.label(format!("Loaded Mag: {loaded_rounds} / {cap} ({chamber_str})"));
  383. egui::ScrollArea::vertical().max_height(105.0).show(ui, |ui| {
  384. for (i, m) in spares.iter().enumerate() {
  385. let frac = m.fill_fraction();
  386. ui.horizontal(|ui| {
  387. ui.label(format!("Spare #{}:", i + 1));
  388. ui.add(
  389. egui::ProgressBar::new(frac)
  390. .desired_width(140.0)
  391. .text(format!("{}/{}", m.rounds, m.capacity)),
  392. );
  393. });
  394. }
  395. });
  396. });
  397. state.show_inventory = open;
  398. }
  399. fn draw_ai_quick_bar(
  400. ctx: &egui::Context,
  401. sim: &Simulation,
  402. state: &mut TacticalWindowsState,
  403. actions: &mut TacticalWindowActions,
  404. ) {
  405. let mut open = state.show_ai_bar;
  406. egui::Window::new("⚡ AI Squad Helpers [Z]")
  407. .open(&mut open)
  408. .anchor(egui::Align2::CENTER_BOTTOM, [0.0, -76.0])
  409. .title_bar(true)
  410. .resizable(false)
  411. .show(ctx, |ui| {
  412. ui.horizontal(|ui| {
  413. if ui.button("🏥 Call Medic").clicked() {
  414. actions.ai_assist = Some(AiAssistRequest::NeedMedic);
  415. }
  416. if ui.button("📦 Call Ammo").clicked() {
  417. actions.ai_assist = Some(AiAssistRequest::NeedAmmo);
  418. }
  419. if ui.button("🏃 Follow Me").clicked() {
  420. actions.ai_assist = Some(AiAssistRequest::FollowMe);
  421. }
  422. if ui.button("💨 Pop Smoke").clicked() {
  423. let target = sim.player.pos + glam::Vec3::new(sim.player.yaw.sin(), 0.0, -sim.player.yaw.cos()) * 14.0;
  424. actions.ai_assist = Some(AiAssistRequest::PopSmoke(target));
  425. }
  426. if ui.button("🚙 Mount Vehicle").clicked() {
  427. let vid = sim.player.in_vehicle.map(|(v, _)| v).unwrap_or(usize::MAX);
  428. actions.ai_assist = Some(AiAssistRequest::MountMyVehicle(vid));
  429. }
  430. if ui.button("⬇ Dismount All").clicked() {
  431. actions.ai_assist = Some(AiAssistRequest::DismountAll);
  432. }
  433. });
  434. });
  435. state.show_ai_bar = open;
  436. }