//! Orders issued by the player from the strategic warbot panel. use crate::{ strategic_roster::warbot_rows, unit_patrol::{UnitOrder, UnitPatrol}, Engine, StartingCockpit, }; use fparkan_terrain::NavigationQuery; use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle}; /// Where the warbot is to patrol. #[derive(Clone, Copy, Debug, PartialEq)] pub enum PatrolTarget { /// Fixed world coordinates. Point([f32; 2]), /// Live object whose position determines the patrol center. Object(ObjectHandle), } /// Tests a route waypoint using the selected warbot's chassis and the native areal flag. pub fn accepts_waypoint(engine: &Engine, actor: ObjectHandle, [x, y]: [f32; 2]) -> bool { if !x.is_finite() || !y.is_finite() { return false; } let Some(mission) = &engine.loaded else { return false; }; let Some(original) = identity_metadata(&engine.world, actor) .ok() .and_then(|meta| meta.original_id) else { return false; }; let terrain = &mission.terrain; let Some(areal) = terrain.locate_areal([x, y, 0.0]).ok().flatten() else { return false; }; // iron3d+0x76bc0 allows chassis selector 1 in any resolved areal. mission .unit_chassis_flags .get(&original.0) .is_some_and(|flags| flags.chassis_type == Some(1)) || terrain .areal_logic_flag(areal) .is_some_and(|flag| flag != 0) } /// Stops the selected warbot and clears its outstanding movement orders. pub fn stand(engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle) -> bool { issue(engine, cockpit, actor, UnitOrder::Stand) } /// Orders the selected warbot to search for hostile mobile units. pub fn search_and_destroy( engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle, ) -> bool { issue(engine, cockpit, actor, UnitOrder::SearchAndDestroy) } /// Sends the selected warbot to a friendly facility to replenish its supplies. pub fn recharge(engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle) -> bool { issue(engine, cockpit, actor, UnitOrder::Recharge) } /// Replaces the selected warbot's order with the confirmed sequence of world points. pub fn follow_route( engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle, points: &[[f32; 2]], ) -> bool { if points.is_empty() || !points .iter() .all(|&point| accepts_waypoint(engine, actor, point)) { return false; } issue(engine, cockpit, actor, UnitOrder::Route(points.to_vec())) } /// Whether an object may be used as the center of a patrol. pub fn accepts_patrol_object(engine: &Engine, target: ObjectHandle) -> bool { registration_sequence(&engine.world, target) .ok() .flatten() .is_some() && engine.loaded.as_ref().is_some_and(|mission| { !mission .control_device_tables .iter() .any(|table| table.handle == target && table.node_provider_depletion_marked) }) } /// Replaces the selected warbot's order with a patrol around a point or an object. pub fn patrol_at( engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle, target: PatrolTarget, ) -> bool { let valid = match target { PatrolTarget::Point(point) => accepts_waypoint(engine, actor, point), PatrolTarget::Object(target) => accepts_patrol_object(engine, target), }; valid && issue(engine, cockpit, actor, UnitOrder::StrategicPatrol(target)) } fn issue( engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle, order: UnitOrder, ) -> bool { if !warbot_rows(engine, cockpit) .iter() .any(|row| row.handle == actor && row.selectable) { return false; } let Some(mission) = engine.loaded.as_mut() else { return false; }; mission .unit_patrol .get_or_insert_with(UnitPatrol::scripted) .command_order(&engine.world, actor, order); true }