use glam::{Vec2, Vec3}; use serde::{Deserialize, Serialize}; use crate::sim::ai::Soldier; use crate::sim::ballistics::{BulletImpact, ExplosionEvent, Projectile}; use crate::sim::player::FpsPlayerState; use crate::world::nav::NavGrid; use crate::world::points::TacticalPointGraph; use crate::world::terrain::TerrainGrid; use crate::world::Faction; #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] pub enum ObjectiveKind { MoveToPosition, SuppressPosition { required_secs: f32 }, CallArtillery { required_strikes: u32 }, AssaultPosition { hold_secs: f32 }, ClearBuilding, DefendPosition { duration_secs: f32 }, } impl ObjectiveKind { pub fn tag(self) -> &'static str { match self { Self::MoveToPosition => "MOVE", Self::SuppressPosition { .. } => "SUPPRESS", Self::CallArtillery { .. } => "CALL ARTY [T]", Self::AssaultPosition { .. } => "ASSAULT", Self::ClearBuilding => "CLEAR BLDG", Self::DefendPosition { .. } => "DEFEND", } } pub fn marker_color(self) -> [f32; 4] { match self { Self::MoveToPosition => [0.28, 0.88, 1.0, 0.90], Self::SuppressPosition { .. } => [1.0, 0.78, 0.22, 0.90], Self::CallArtillery { .. } => [1.0, 0.42, 0.20, 0.92], Self::AssaultPosition { .. } => [0.42, 1.0, 0.56, 0.92], Self::ClearBuilding => [0.95, 0.88, 0.30, 0.92], Self::DefendPosition { .. } => [0.35, 0.72, 1.0, 0.92], } } } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ObjectiveStatus { Locked, Active, Completed, } #[derive(Clone, Debug)] pub struct MissionObjective { pub id: usize, pub kind: ObjectiveKind, pub title: String, pub brief: String, pub position: Vec3, pub radius: f32, pub progress: f32, pub timer_secs: f32, pub strikes_called: u32, pub visited: bool, pub hostiles_remaining: usize, pub status: ObjectiveStatus, } #[derive(Clone, Debug, Default)] pub struct MissionStepEvents { pub completed_stage: Option, pub trigger_counterattack: Option, pub operation_victory: bool, } #[derive(Clone, Debug)] pub struct MissionState { pub operation_name: String, pub friendly_faction: Faction, pub objectives: Vec, pub active_index: usize, pub completed: bool, pub toast_text: Option, pub toast_timer: f32, pub arty_cooldown: f32, pub counterattack_triggered: bool, } impl MissionState { pub fn from_point_graph(graph: &TacticalPointGraph) -> Self { let mut objs = Vec::with_capacity(graph.objectives.len()); for (idx, pt) in graph.objectives.iter().enumerate() { objs.push(MissionObjective { id: idx, kind: pt.suggested_kind, title: pt.name.clone(), brief: pt.brief.clone(), position: pt.position, radius: pt.radius, progress: 0.0, timer_secs: 0.0, strikes_called: 0, visited: false, hostiles_remaining: 0, status: if idx == 0 { ObjectiveStatus::Active } else { ObjectiveStatus::Locked }, }); } let first_title = objs .first() .map(|o| format!("OBJ 1/{}: {}", objs.len(), o.title)) .unwrap_or_else(|| graph.operation_name.clone()); Self { operation_name: graph.operation_name.clone(), friendly_faction: Faction::Federal, objectives: objs, active_index: 0, completed: false, toast_text: Some(first_title), toast_timer: 4.5, arty_cooldown: 0.0, counterattack_triggered: false, } } pub fn operation_cold_water(nav_grid: &NavGrid, terrain: &TerrainGrid) -> Self { let snap = |raw_x: f32, raw_z: f32| -> Vec3 { let p = nav_grid.nearest_walkable(Vec3::new(raw_x, 0.0, raw_z)); Vec3::new(p.x, nav_grid.surface_height(p.x, p.z, terrain), p.z) }; let mut objs = vec![ MissionObjective { id: 0, kind: ObjectiveKind::MoveToPosition, title: "West Bridgehead Staging".into(), brief: "Advance to the west bridge approach beside the knocked-out BTR-80.".into(), position: snap(-26.0, -6.0), radius: 12.0, progress: 0.0, timer_secs: 0.0, strikes_called: 0, visited: false, hostiles_remaining: 0, status: ObjectiveStatus::Active, }, MissionObjective { id: 1, kind: ObjectiveKind::SuppressPosition { required_secs: 6.0 }, title: "Suppress East Bank Sandbags".into(), brief: "Pin militia defenders overlooking the east bridge exit (fire into zone or press Y).".into(), position: snap(18.0, -12.0), radius: 18.0, progress: 0.0, timer_secs: 0.0, strikes_called: 0, visited: false, hostiles_remaining: 0, status: ObjectiveStatus::Locked, }, MissionObjective { id: 2, kind: ObjectiveKind::CallArtillery { required_strikes: 1 }, title: "Call 120mm Arty on North Ruins".into(), brief: "Aim toward the ruined northern apartment block and press [T] for a mortar barrage.".into(), position: snap(36.0, -28.0), radius: 26.0, progress: 0.0, timer_secs: 0.0, strikes_called: 0, visited: false, hostiles_remaining: 0, status: ObjectiveStatus::Locked, }, MissionObjective { id: 3, kind: ObjectiveKind::AssaultPosition { hold_secs: 4.0 }, title: "Assault East Bridgehead".into(), brief: "Cross the concrete arch bridge and seize the east bank road exit.".into(), position: snap(16.0, -4.0), radius: 16.0, progress: 0.0, timer_secs: 0.0, strikes_called: 0, visited: false, hostiles_remaining: 0, status: ObjectiveStatus::Locked, }, MissionObjective { id: 4, kind: ObjectiveKind::ClearBuilding, title: "Clear Northern Khrushchyovka".into(), brief: "Sweep the 3-story ruined apartment block and eliminate all hostiles.".into(), position: snap(36.0, -28.0), radius: 20.0, progress: 0.0, timer_secs: 0.0, strikes_called: 0, visited: false, hostiles_remaining: 0, status: ObjectiveStatus::Locked, }, MissionObjective { id: 5, kind: ObjectiveKind::DefendPosition { duration_secs: 25.0 }, title: "Defend East Town Crossroads".into(), brief: "Hold the crossroads against the militia counterattack from the eastern ridge.".into(), position: snap(42.0, 6.0), radius: 18.0, progress: 0.0, timer_secs: 0.0, strikes_called: 0, visited: false, hostiles_remaining: 0, status: ObjectiveStatus::Locked, }, ]; if let Some(first) = objs.first_mut() { first.status = ObjectiveStatus::Active; } Self { operation_name: "OPERATION COLD WATER".into(), friendly_faction: Faction::Federal, objectives: objs, active_index: 0, completed: false, toast_text: Some("OBJ 1/6: Advance to West Bridgehead Staging".into()), toast_timer: 4.5, arty_cooldown: 0.0, counterattack_triggered: false, } } pub fn active_objective(&self) -> Option<&MissionObjective> { if self.completed { None } else { self.objectives.get(self.active_index) } } pub fn notify_artillery_call(&mut self, target: Vec3) { if self.completed { return; } if let Some(obj) = self.objectives.get_mut(self.active_index) { if matches!(obj.kind, ObjectiveKind::CallArtillery { .. }) { let d_xz = Vec2::new(target.x - obj.position.x, target.z - obj.position.z).length(); if d_xz <= obj.radius + 10.0 { obj.strikes_called += 1; } } } } #[inline] fn horiz_dist(a: Vec3, b: Vec3) -> f32 { Vec2::new(a.x - b.x, a.z - b.z).length() } pub fn step( &mut self, dt: f32, soldiers: &[Soldier], player: &FpsPlayerState, projectiles: &[Projectile], impacts: &[BulletImpact], explosions: &[ExplosionEvent], ) -> MissionStepEvents { let mut events = MissionStepEvents::default(); self.arty_cooldown = (self.arty_cooldown - dt).max(0.0); if self.toast_timer > 0.0 { self.toast_timer = (self.toast_timer - dt).max(0.0); if self.toast_timer <= 0.0 { self.toast_text = None; } } if self.completed || self.active_index >= self.objectives.len() { return events; } let friendly_fac = self.friendly_faction; let player_alive = player.active && player.alive && player.faction == friendly_fac; let obj = &mut self.objectives[self.active_index]; obj.status = ObjectiveStatus::Active; let mut friendly_in = 0usize; let mut closest_friendly = f32::INFINITY; let mut hostiles_in = 0usize; let mut pinned_hostiles = 0usize; let mut total_hostiles_alive = 0usize; for s in soldiers { if !s.alive { continue; } let d = Self::horiz_dist(s.pos, obj.position); if s.faction == friendly_fac { closest_friendly = closest_friendly.min(d); if d <= obj.radius { friendly_in += 1; } } else { total_hostiles_alive += 1; if d <= obj.radius { hostiles_in += 1; if s.suppression >= 0.30 { pinned_hostiles += 1; } } } } let player_in = if player_alive { let dp = Self::horiz_dist(player.pos, obj.position); closest_friendly = closest_friendly.min(dp); dp <= obj.radius } else { false }; obj.hostiles_remaining = hostiles_in; let mut finished = false; match obj.kind { ObjectiveKind::MoveToPosition => { if player_in || friendly_in >= 2 { obj.progress = 1.0; finished = true; } else { let prox = if closest_friendly.is_finite() { (1.0 - (closest_friendly - obj.radius).max(0.0) / 80.0).clamp(0.0, 0.92) } else { 0.0 }; obj.progress = prox; } } ObjectiveKind::SuppressPosition { required_secs } => { let fire_into_zone = projectiles.iter().any(|p| { p.faction == friendly_fac && Self::horiz_dist(p.pos, obj.position) <= obj.radius + 4.0 }) || impacts.iter().any(|imp| { imp.faction == friendly_fac && Self::horiz_dist(imp.pos, obj.position) <= obj.radius + 4.0 }); if hostiles_in == 0 { // Prevent soft-lock if all defenders in the zone were already eliminated obj.timer_secs = required_secs; obj.progress = 1.0; finished = true; } else { if pinned_hostiles > 0 || fire_into_zone { let rate = if pinned_hostiles > 0 && fire_into_zone { 2.5 } else if fire_into_zone { 2.2 } else { 1.0 }; obj.timer_secs += dt * rate; } else { obj.timer_secs = (obj.timer_secs - dt * 0.25).max(0.0); } obj.progress = (obj.timer_secs / required_secs.max(0.1)).clamp(0.0, 1.0); finished = obj.timer_secs >= required_secs; } } ObjectiveKind::CallArtillery { required_strikes } => { for exp in explosions { if Self::horiz_dist(exp.pos, obj.position) <= obj.radius + 8.0 { obj.strikes_called = obj.strikes_called.max(required_strikes); } } obj.progress = (obj.strikes_called as f32 / required_strikes.max(1) as f32).clamp(0.0, 1.0); finished = obj.strikes_called >= required_strikes; } ObjectiveKind::AssaultPosition { hold_secs } => { let close_hostiles = soldiers .iter() .filter(|s| { s.alive && s.faction != friendly_fac && Self::horiz_dist(s.pos, obj.position) <= obj.radius * 0.85 }) .count(); obj.hostiles_remaining = close_hostiles; if (player_in || friendly_in > 0) && close_hostiles == 0 { obj.timer_secs += dt; } else { obj.timer_secs = (obj.timer_secs - dt * 0.25).max(0.0); } obj.progress = (obj.timer_secs / hold_secs.max(0.1)).clamp(0.0, 1.0); finished = obj.timer_secs >= hold_secs; } ObjectiveKind::ClearBuilding => { let reach_in = soldiers.iter().any(|s| { s.alive && s.faction == friendly_fac && Self::horiz_dist(s.pos, obj.position) <= obj.radius + 4.0 }) || (player_alive && Self::horiz_dist(player.pos, obj.position) <= obj.radius + 4.0); if reach_in { obj.visited = true; } let near_hostiles = soldiers .iter() .filter(|s| { s.alive && s.faction != friendly_fac && Self::horiz_dist(s.pos, obj.position) <= obj.radius + 2.0 }) .count(); obj.hostiles_remaining = near_hostiles; let clear_ratio = (1.0 - (near_hostiles as f32 / 5.0)).clamp(0.0, 1.0); obj.progress = (if obj.visited { 0.35 } else { 0.0 }) + 0.65 * clear_ratio; finished = obj.visited && near_hostiles == 0; } ObjectiveKind::DefendPosition { duration_secs } => { if !self.counterattack_triggered { self.counterattack_triggered = true; events.trigger_counterattack = Some(obj.position); } let defenders_in = soldiers.iter().any(|s| { s.alive && s.faction == friendly_fac && Self::horiz_dist(s.pos, obj.position) <= obj.radius + 2.0 }) || (player_alive && Self::horiz_dist(player.pos, obj.position) <= obj.radius + 2.0); if defenders_in { obj.timer_secs += dt; } obj.hostiles_remaining = total_hostiles_alive; obj.progress = (obj.timer_secs / duration_secs.max(0.1)).clamp(0.0, 1.0); finished = obj.timer_secs >= duration_secs || (total_hostiles_alive == 0 && obj.timer_secs >= 3.0); } } if finished { let done_title = obj.title.clone(); obj.progress = 1.0; obj.status = ObjectiveStatus::Completed; events.completed_stage = Some(done_title.clone()); self.active_index += 1; if self.active_index >= self.objectives.len() { self.completed = true; events.operation_victory = true; self.toast_text = Some("OPERATION COLD WATER COMPLETE — VALLEY SECURED".into()); self.toast_timer = 8.0; } else { let next_idx = self.active_index; self.objectives[next_idx].status = ObjectiveStatus::Active; let next_title = self.objectives[next_idx].title.clone(); if matches!(self.objectives[next_idx].kind, ObjectiveKind::DefendPosition { .. }) && !self.counterattack_triggered { self.counterattack_triggered = true; events.trigger_counterattack = Some(self.objectives[next_idx].position); } self.toast_text = Some(format!( "COMPLETE: {done_title} | NEXT ({}/{}): {next_title}", next_idx + 1, self.objectives.len() )); self.toast_timer = 5.0; } } events } /// Dynamically injects a Game Director objective (Arma Reforger GM tasking). pub fn add_gm_objective( &mut self, kind: ObjectiveKind, title: impl Into, brief: impl Into, position: Vec3, radius: f32, activate_now: bool, ) -> usize { let title_s = title.into(); let id = self.objectives.len(); let should_activate = activate_now || self.completed || self.objectives.is_empty(); if should_activate && self.active_index < self.objectives.len() { let cur = self.active_index; if self.objectives[cur].status == ObjectiveStatus::Active { self.objectives[cur].status = ObjectiveStatus::Locked; } } self.objectives.push(MissionObjective { id, kind, title: title_s.clone(), brief: brief.into(), position, radius, progress: 0.0, timer_secs: 0.0, strikes_called: 0, visited: false, hostiles_remaining: 0, status: if should_activate { ObjectiveStatus::Active } else { ObjectiveStatus::Locked }, }); if should_activate { self.active_index = id; self.completed = false; self.toast_text = Some(format!("GM TASK: {title_s}")); self.toast_timer = 4.5; } id } }