objectives.rs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. use glam::{Vec2, Vec3};
  2. use serde::{Deserialize, Serialize};
  3. use crate::sim::ai::Soldier;
  4. use crate::sim::ballistics::{BulletImpact, ExplosionEvent, Projectile};
  5. use crate::sim::player::FpsPlayerState;
  6. use crate::world::nav::NavGrid;
  7. use crate::world::points::TacticalPointGraph;
  8. use crate::world::terrain::TerrainGrid;
  9. use crate::world::Faction;
  10. #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
  11. pub enum ObjectiveKind {
  12. MoveToPosition,
  13. SuppressPosition { required_secs: f32 },
  14. CallArtillery { required_strikes: u32 },
  15. AssaultPosition { hold_secs: f32 },
  16. ClearBuilding,
  17. DefendPosition { duration_secs: f32 },
  18. }
  19. impl ObjectiveKind {
  20. pub fn tag(self) -> &'static str {
  21. match self {
  22. Self::MoveToPosition => "MOVE",
  23. Self::SuppressPosition { .. } => "SUPPRESS",
  24. Self::CallArtillery { .. } => "CALL ARTY [T]",
  25. Self::AssaultPosition { .. } => "ASSAULT",
  26. Self::ClearBuilding => "CLEAR BLDG",
  27. Self::DefendPosition { .. } => "DEFEND",
  28. }
  29. }
  30. pub fn marker_color(self) -> [f32; 4] {
  31. match self {
  32. Self::MoveToPosition => [0.28, 0.88, 1.0, 0.90],
  33. Self::SuppressPosition { .. } => [1.0, 0.78, 0.22, 0.90],
  34. Self::CallArtillery { .. } => [1.0, 0.42, 0.20, 0.92],
  35. Self::AssaultPosition { .. } => [0.42, 1.0, 0.56, 0.92],
  36. Self::ClearBuilding => [0.95, 0.88, 0.30, 0.92],
  37. Self::DefendPosition { .. } => [0.35, 0.72, 1.0, 0.92],
  38. }
  39. }
  40. }
  41. #[derive(Clone, Copy, Debug, PartialEq, Eq)]
  42. pub enum ObjectiveStatus {
  43. Locked,
  44. Active,
  45. Completed,
  46. }
  47. #[derive(Clone, Debug)]
  48. pub struct MissionObjective {
  49. pub id: usize,
  50. pub kind: ObjectiveKind,
  51. pub title: String,
  52. pub brief: String,
  53. pub position: Vec3,
  54. pub radius: f32,
  55. pub progress: f32,
  56. pub timer_secs: f32,
  57. pub strikes_called: u32,
  58. pub visited: bool,
  59. pub hostiles_remaining: usize,
  60. pub status: ObjectiveStatus,
  61. }
  62. #[derive(Clone, Debug, Default)]
  63. pub struct MissionStepEvents {
  64. pub completed_stage: Option<String>,
  65. pub trigger_counterattack: Option<Vec3>,
  66. pub operation_victory: bool,
  67. }
  68. #[derive(Clone, Debug)]
  69. pub struct MissionState {
  70. pub operation_name: String,
  71. pub friendly_faction: Faction,
  72. pub objectives: Vec<MissionObjective>,
  73. pub active_index: usize,
  74. pub completed: bool,
  75. pub toast_text: Option<String>,
  76. pub toast_timer: f32,
  77. pub arty_cooldown: f32,
  78. pub counterattack_triggered: bool,
  79. }
  80. impl MissionState {
  81. pub fn from_point_graph(graph: &TacticalPointGraph) -> Self {
  82. let mut objs = Vec::with_capacity(graph.objectives.len());
  83. for (idx, pt) in graph.objectives.iter().enumerate() {
  84. objs.push(MissionObjective {
  85. id: idx,
  86. kind: pt.suggested_kind,
  87. title: pt.name.clone(),
  88. brief: pt.brief.clone(),
  89. position: pt.position,
  90. radius: pt.radius,
  91. progress: 0.0,
  92. timer_secs: 0.0,
  93. strikes_called: 0,
  94. visited: false,
  95. hostiles_remaining: 0,
  96. status: if idx == 0 {
  97. ObjectiveStatus::Active
  98. } else {
  99. ObjectiveStatus::Locked
  100. },
  101. });
  102. }
  103. let first_title = objs
  104. .first()
  105. .map(|o| format!("OBJ 1/{}: {}", objs.len(), o.title))
  106. .unwrap_or_else(|| graph.operation_name.clone());
  107. Self {
  108. operation_name: graph.operation_name.clone(),
  109. friendly_faction: Faction::Federal,
  110. objectives: objs,
  111. active_index: 0,
  112. completed: false,
  113. toast_text: Some(first_title),
  114. toast_timer: 4.5,
  115. arty_cooldown: 0.0,
  116. counterattack_triggered: false,
  117. }
  118. }
  119. pub fn operation_cold_water(nav_grid: &NavGrid, terrain: &TerrainGrid) -> Self {
  120. let snap = |raw_x: f32, raw_z: f32| -> Vec3 {
  121. let p = nav_grid.nearest_walkable(Vec3::new(raw_x, 0.0, raw_z));
  122. Vec3::new(p.x, nav_grid.surface_height(p.x, p.z, terrain), p.z)
  123. };
  124. let mut objs = vec![
  125. MissionObjective {
  126. id: 0,
  127. kind: ObjectiveKind::MoveToPosition,
  128. title: "West Bridgehead Staging".into(),
  129. brief: "Advance to the west bridge approach beside the knocked-out BTR-80.".into(),
  130. position: snap(-26.0, -6.0),
  131. radius: 12.0,
  132. progress: 0.0,
  133. timer_secs: 0.0,
  134. strikes_called: 0,
  135. visited: false,
  136. hostiles_remaining: 0,
  137. status: ObjectiveStatus::Active,
  138. },
  139. MissionObjective {
  140. id: 1,
  141. kind: ObjectiveKind::SuppressPosition { required_secs: 6.0 },
  142. title: "Suppress East Bank Sandbags".into(),
  143. brief: "Pin militia defenders overlooking the east bridge exit (fire into zone or press Y).".into(),
  144. position: snap(18.0, -12.0),
  145. radius: 18.0,
  146. progress: 0.0,
  147. timer_secs: 0.0,
  148. strikes_called: 0,
  149. visited: false,
  150. hostiles_remaining: 0,
  151. status: ObjectiveStatus::Locked,
  152. },
  153. MissionObjective {
  154. id: 2,
  155. kind: ObjectiveKind::CallArtillery { required_strikes: 1 },
  156. title: "Call 120mm Arty on North Ruins".into(),
  157. brief: "Aim toward the ruined northern apartment block and press [T] for a mortar barrage.".into(),
  158. position: snap(36.0, -28.0),
  159. radius: 26.0,
  160. progress: 0.0,
  161. timer_secs: 0.0,
  162. strikes_called: 0,
  163. visited: false,
  164. hostiles_remaining: 0,
  165. status: ObjectiveStatus::Locked,
  166. },
  167. MissionObjective {
  168. id: 3,
  169. kind: ObjectiveKind::AssaultPosition { hold_secs: 4.0 },
  170. title: "Assault East Bridgehead".into(),
  171. brief: "Cross the concrete arch bridge and seize the east bank road exit.".into(),
  172. position: snap(16.0, -4.0),
  173. radius: 16.0,
  174. progress: 0.0,
  175. timer_secs: 0.0,
  176. strikes_called: 0,
  177. visited: false,
  178. hostiles_remaining: 0,
  179. status: ObjectiveStatus::Locked,
  180. },
  181. MissionObjective {
  182. id: 4,
  183. kind: ObjectiveKind::ClearBuilding,
  184. title: "Clear Northern Khrushchyovka".into(),
  185. brief: "Sweep the 3-story ruined apartment block and eliminate all hostiles.".into(),
  186. position: snap(36.0, -28.0),
  187. radius: 20.0,
  188. progress: 0.0,
  189. timer_secs: 0.0,
  190. strikes_called: 0,
  191. visited: false,
  192. hostiles_remaining: 0,
  193. status: ObjectiveStatus::Locked,
  194. },
  195. MissionObjective {
  196. id: 5,
  197. kind: ObjectiveKind::DefendPosition { duration_secs: 25.0 },
  198. title: "Defend East Town Crossroads".into(),
  199. brief: "Hold the crossroads against the militia counterattack from the eastern ridge.".into(),
  200. position: snap(42.0, 6.0),
  201. radius: 18.0,
  202. progress: 0.0,
  203. timer_secs: 0.0,
  204. strikes_called: 0,
  205. visited: false,
  206. hostiles_remaining: 0,
  207. status: ObjectiveStatus::Locked,
  208. },
  209. ];
  210. if let Some(first) = objs.first_mut() {
  211. first.status = ObjectiveStatus::Active;
  212. }
  213. Self {
  214. operation_name: "OPERATION COLD WATER".into(),
  215. friendly_faction: Faction::Federal,
  216. objectives: objs,
  217. active_index: 0,
  218. completed: false,
  219. toast_text: Some("OBJ 1/6: Advance to West Bridgehead Staging".into()),
  220. toast_timer: 4.5,
  221. arty_cooldown: 0.0,
  222. counterattack_triggered: false,
  223. }
  224. }
  225. pub fn active_objective(&self) -> Option<&MissionObjective> {
  226. if self.completed {
  227. None
  228. } else {
  229. self.objectives.get(self.active_index)
  230. }
  231. }
  232. pub fn notify_artillery_call(&mut self, target: Vec3) {
  233. if self.completed {
  234. return;
  235. }
  236. if let Some(obj) = self.objectives.get_mut(self.active_index) {
  237. if matches!(obj.kind, ObjectiveKind::CallArtillery { .. }) {
  238. let d_xz = Vec2::new(target.x - obj.position.x, target.z - obj.position.z).length();
  239. if d_xz <= obj.radius + 10.0 {
  240. obj.strikes_called += 1;
  241. }
  242. }
  243. }
  244. }
  245. #[inline]
  246. fn horiz_dist(a: Vec3, b: Vec3) -> f32 {
  247. Vec2::new(a.x - b.x, a.z - b.z).length()
  248. }
  249. pub fn step(
  250. &mut self,
  251. dt: f32,
  252. soldiers: &[Soldier],
  253. player: &FpsPlayerState,
  254. projectiles: &[Projectile],
  255. impacts: &[BulletImpact],
  256. explosions: &[ExplosionEvent],
  257. ) -> MissionStepEvents {
  258. let mut events = MissionStepEvents::default();
  259. self.arty_cooldown = (self.arty_cooldown - dt).max(0.0);
  260. if self.toast_timer > 0.0 {
  261. self.toast_timer = (self.toast_timer - dt).max(0.0);
  262. if self.toast_timer <= 0.0 {
  263. self.toast_text = None;
  264. }
  265. }
  266. if self.completed || self.active_index >= self.objectives.len() {
  267. return events;
  268. }
  269. let friendly_fac = self.friendly_faction;
  270. let player_alive = player.active && player.alive && player.faction == friendly_fac;
  271. let obj = &mut self.objectives[self.active_index];
  272. obj.status = ObjectiveStatus::Active;
  273. let mut friendly_in = 0usize;
  274. let mut closest_friendly = f32::INFINITY;
  275. let mut hostiles_in = 0usize;
  276. let mut pinned_hostiles = 0usize;
  277. let mut total_hostiles_alive = 0usize;
  278. for s in soldiers {
  279. if !s.alive {
  280. continue;
  281. }
  282. let d = Self::horiz_dist(s.pos, obj.position);
  283. if s.faction == friendly_fac {
  284. closest_friendly = closest_friendly.min(d);
  285. if d <= obj.radius {
  286. friendly_in += 1;
  287. }
  288. } else {
  289. total_hostiles_alive += 1;
  290. if d <= obj.radius {
  291. hostiles_in += 1;
  292. if s.suppression >= 0.30 {
  293. pinned_hostiles += 1;
  294. }
  295. }
  296. }
  297. }
  298. let player_in = if player_alive {
  299. let dp = Self::horiz_dist(player.pos, obj.position);
  300. closest_friendly = closest_friendly.min(dp);
  301. dp <= obj.radius
  302. } else {
  303. false
  304. };
  305. obj.hostiles_remaining = hostiles_in;
  306. let mut finished = false;
  307. match obj.kind {
  308. ObjectiveKind::MoveToPosition => {
  309. if player_in || friendly_in >= 2 {
  310. obj.progress = 1.0;
  311. finished = true;
  312. } else {
  313. let prox = if closest_friendly.is_finite() {
  314. (1.0 - (closest_friendly - obj.radius).max(0.0) / 80.0).clamp(0.0, 0.92)
  315. } else {
  316. 0.0
  317. };
  318. obj.progress = prox;
  319. }
  320. }
  321. ObjectiveKind::SuppressPosition { required_secs } => {
  322. let fire_into_zone = projectiles.iter().any(|p| {
  323. p.faction == friendly_fac && Self::horiz_dist(p.pos, obj.position) <= obj.radius + 4.0
  324. }) || impacts.iter().any(|imp| {
  325. imp.faction == friendly_fac && Self::horiz_dist(imp.pos, obj.position) <= obj.radius + 4.0
  326. });
  327. if hostiles_in == 0 {
  328. // Prevent soft-lock if all defenders in the zone were already eliminated
  329. obj.timer_secs = required_secs;
  330. obj.progress = 1.0;
  331. finished = true;
  332. } else {
  333. if pinned_hostiles > 0 || fire_into_zone {
  334. let rate = if pinned_hostiles > 0 && fire_into_zone {
  335. 2.5
  336. } else if fire_into_zone {
  337. 2.2
  338. } else {
  339. 1.0
  340. };
  341. obj.timer_secs += dt * rate;
  342. } else {
  343. obj.timer_secs = (obj.timer_secs - dt * 0.25).max(0.0);
  344. }
  345. obj.progress = (obj.timer_secs / required_secs.max(0.1)).clamp(0.0, 1.0);
  346. finished = obj.timer_secs >= required_secs;
  347. }
  348. }
  349. ObjectiveKind::CallArtillery { required_strikes } => {
  350. for exp in explosions {
  351. if Self::horiz_dist(exp.pos, obj.position) <= obj.radius + 8.0 {
  352. obj.strikes_called = obj.strikes_called.max(required_strikes);
  353. }
  354. }
  355. obj.progress = (obj.strikes_called as f32 / required_strikes.max(1) as f32).clamp(0.0, 1.0);
  356. finished = obj.strikes_called >= required_strikes;
  357. }
  358. ObjectiveKind::AssaultPosition { hold_secs } => {
  359. let close_hostiles = soldiers
  360. .iter()
  361. .filter(|s| {
  362. s.alive && s.faction != friendly_fac && Self::horiz_dist(s.pos, obj.position) <= obj.radius * 0.85
  363. })
  364. .count();
  365. obj.hostiles_remaining = close_hostiles;
  366. if (player_in || friendly_in > 0) && close_hostiles == 0 {
  367. obj.timer_secs += dt;
  368. } else {
  369. obj.timer_secs = (obj.timer_secs - dt * 0.25).max(0.0);
  370. }
  371. obj.progress = (obj.timer_secs / hold_secs.max(0.1)).clamp(0.0, 1.0);
  372. finished = obj.timer_secs >= hold_secs;
  373. }
  374. ObjectiveKind::ClearBuilding => {
  375. let reach_in = soldiers.iter().any(|s| {
  376. s.alive && s.faction == friendly_fac && Self::horiz_dist(s.pos, obj.position) <= obj.radius + 4.0
  377. }) || (player_alive && Self::horiz_dist(player.pos, obj.position) <= obj.radius + 4.0);
  378. if reach_in {
  379. obj.visited = true;
  380. }
  381. let near_hostiles = soldiers
  382. .iter()
  383. .filter(|s| {
  384. s.alive && s.faction != friendly_fac && Self::horiz_dist(s.pos, obj.position) <= obj.radius + 2.0
  385. })
  386. .count();
  387. obj.hostiles_remaining = near_hostiles;
  388. let clear_ratio = (1.0 - (near_hostiles as f32 / 5.0)).clamp(0.0, 1.0);
  389. obj.progress = (if obj.visited { 0.35 } else { 0.0 }) + 0.65 * clear_ratio;
  390. finished = obj.visited && near_hostiles == 0;
  391. }
  392. ObjectiveKind::DefendPosition { duration_secs } => {
  393. if !self.counterattack_triggered {
  394. self.counterattack_triggered = true;
  395. events.trigger_counterattack = Some(obj.position);
  396. }
  397. let defenders_in = soldiers.iter().any(|s| {
  398. s.alive && s.faction == friendly_fac && Self::horiz_dist(s.pos, obj.position) <= obj.radius + 2.0
  399. }) || (player_alive && Self::horiz_dist(player.pos, obj.position) <= obj.radius + 2.0);
  400. if defenders_in {
  401. obj.timer_secs += dt;
  402. }
  403. obj.hostiles_remaining = total_hostiles_alive;
  404. obj.progress = (obj.timer_secs / duration_secs.max(0.1)).clamp(0.0, 1.0);
  405. finished = obj.timer_secs >= duration_secs || (total_hostiles_alive == 0 && obj.timer_secs >= 3.0);
  406. }
  407. }
  408. if finished {
  409. let done_title = obj.title.clone();
  410. obj.progress = 1.0;
  411. obj.status = ObjectiveStatus::Completed;
  412. events.completed_stage = Some(done_title.clone());
  413. self.active_index += 1;
  414. if self.active_index >= self.objectives.len() {
  415. self.completed = true;
  416. events.operation_victory = true;
  417. self.toast_text = Some("OPERATION COLD WATER COMPLETE — VALLEY SECURED".into());
  418. self.toast_timer = 8.0;
  419. } else {
  420. let next_idx = self.active_index;
  421. self.objectives[next_idx].status = ObjectiveStatus::Active;
  422. let next_title = self.objectives[next_idx].title.clone();
  423. if matches!(self.objectives[next_idx].kind, ObjectiveKind::DefendPosition { .. })
  424. && !self.counterattack_triggered
  425. {
  426. self.counterattack_triggered = true;
  427. events.trigger_counterattack = Some(self.objectives[next_idx].position);
  428. }
  429. self.toast_text = Some(format!(
  430. "COMPLETE: {done_title} | NEXT ({}/{}): {next_title}",
  431. next_idx + 1,
  432. self.objectives.len()
  433. ));
  434. self.toast_timer = 5.0;
  435. }
  436. }
  437. events
  438. }
  439. /// Dynamically injects a Game Director objective (Arma Reforger GM tasking).
  440. pub fn add_gm_objective(
  441. &mut self,
  442. kind: ObjectiveKind,
  443. title: impl Into<String>,
  444. brief: impl Into<String>,
  445. position: Vec3,
  446. radius: f32,
  447. activate_now: bool,
  448. ) -> usize {
  449. let title_s = title.into();
  450. let id = self.objectives.len();
  451. let should_activate = activate_now || self.completed || self.objectives.is_empty();
  452. if should_activate && self.active_index < self.objectives.len() {
  453. let cur = self.active_index;
  454. if self.objectives[cur].status == ObjectiveStatus::Active {
  455. self.objectives[cur].status = ObjectiveStatus::Locked;
  456. }
  457. }
  458. self.objectives.push(MissionObjective {
  459. id,
  460. kind,
  461. title: title_s.clone(),
  462. brief: brief.into(),
  463. position,
  464. radius,
  465. progress: 0.0,
  466. timer_secs: 0.0,
  467. strikes_called: 0,
  468. visited: false,
  469. hostiles_remaining: 0,
  470. status: if should_activate {
  471. ObjectiveStatus::Active
  472. } else {
  473. ObjectiveStatus::Locked
  474. },
  475. });
  476. if should_activate {
  477. self.active_index = id;
  478. self.completed = false;
  479. self.toast_text = Some(format!("GM TASK: {title_s}"));
  480. self.toast_timer = 4.5;
  481. }
  482. id
  483. }
  484. }