diff --git a/84_Super_Star_Trek/rust/src/commands.rs b/84_Super_Star_Trek/rust/src/commands.rs index 3239b45e..0675ee4f 100644 --- a/84_Super_Star_Trek/rust/src/commands.rs +++ b/84_Super_Star_Trek/rust/src/commands.rs @@ -1,8 +1,20 @@ use crate::{model::{Galaxy, Pos, COURSES, EndPosition}, view, input}; +pub fn perform_short_range_scan(galaxy: &Galaxy) { + if galaxy.enterprise.damaged.contains_key(view::keys::SHORT_RANGE_SCAN) { + view::scanners_out(); + return; + } + + view::short_range_scan(&galaxy) +} + pub fn get_amount_and_set_shields(galaxy: &mut Galaxy, provided: Vec) { - // todo check for damaged module + if galaxy.enterprise.damaged.contains_key(view::keys::SHIELD_CONTROL) { + view::inoperable("Shield Control"); + return; + } view::energy_available(galaxy.enterprise.total_energy); let value = input::param_or_prompt_value(&provided, 0, "Number of units to shields", 0, i32::MAX); @@ -10,6 +22,7 @@ pub fn get_amount_and_set_shields(galaxy: &mut Galaxy, provided: Vec) { view::shields_unchanged(); return; } + let value = value.unwrap() as u16; if value > galaxy.enterprise.total_energy { view::ridiculous(); @@ -29,17 +42,31 @@ pub fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy, provided: Vec return; } - let speed = input::param_or_prompt_value(&provided, 1, "Warp Factor (0-8)?", 0.0, 8.0); + let course = course.unwrap(); + + let mut max_warp = 8.0; + if galaxy.enterprise.damaged.contains_key(view::keys::NAVIGATION) { + max_warp = 0.2; + } + + let speed = input::param_or_prompt_value(&provided, 1, format!("Warp Factor (0-{})?", max_warp).as_str(), 0.0, 8.0); if speed.is_none() { view::bad_nav(); return; } + + let speed = speed.unwrap(); + + if speed > max_warp { + view::damaged_engines(max_warp, speed); + return; + } move_klingons_and_fire(galaxy); if galaxy.enterprise.destroyed { return; } - move_enterprise(course.unwrap(), speed.unwrap(), galaxy); + move_enterprise(course, speed, galaxy); } fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) { diff --git a/84_Super_Star_Trek/rust/src/main.rs b/84_Super_Star_Trek/rust/src/main.rs index 1b9a0522..ecde0476 100644 --- a/84_Super_Star_Trek/rust/src/main.rs +++ b/84_Super_Star_Trek/rust/src/main.rs @@ -26,9 +26,9 @@ fn main() { continue; } match command[0].to_uppercase().as_str() { - "SRS" => view::short_range_scan(&galaxy), - "NAV" => commands::gather_dir_and_speed_then_move(&mut galaxy, command[1..].into()), - "SHE" => commands::get_amount_and_set_shields(&mut galaxy, command[1..].into()), + view::keys::SHORT_RANGE_SCAN => commands::perform_short_range_scan(&galaxy), + view::keys::NAVIGATION => commands::gather_dir_and_speed_then_move(&mut galaxy, command[1..].into()), + view::keys::SHIELD_CONTROL => commands::get_amount_and_set_shields(&mut galaxy, command[1..].into()), _ => view::print_command_help() } diff --git a/84_Super_Star_Trek/rust/src/model.rs b/84_Super_Star_Trek/rust/src/model.rs index 4f5a65ac..a078df05 100644 --- a/84_Super_Star_Trek/rust/src/model.rs +++ b/84_Super_Star_Trek/rust/src/model.rs @@ -1,4 +1,4 @@ -use std::{ops::{Mul, Add}, fmt::Display}; +use std::{ops::{Mul, Add}, fmt::Display, collections::HashMap}; use rand::Rng; @@ -37,7 +37,7 @@ impl Klingon { pub struct Enterprise { pub destroyed: bool, - pub damaged: bool, // later this could be by subsystem + pub damaged: HashMap, pub quadrant: Pos, pub sector: Pos, pub photon_torpedoes: u8, @@ -147,7 +147,7 @@ impl Galaxy { quadrants: quadrants, enterprise: Enterprise { destroyed: false, - damaged: false, + damaged: HashMap::new(), quadrant: enterprise_quadrant, sector: enterprise_sector, photon_torpedoes: 28, diff --git a/84_Super_Star_Trek/rust/src/view.rs b/84_Super_Star_Trek/rust/src/view.rs index 02c72815..68934549 100644 --- a/84_Super_Star_Trek/rust/src/view.rs +++ b/84_Super_Star_Trek/rust/src/view.rs @@ -1,5 +1,15 @@ use crate::model::{Galaxy, Pos, EndPosition, SectorStatus}; +pub mod keys { + pub const SHORT_RANGE_SCAN: &str = "SRS"; + pub const NAVIGATION: &str = "NAV"; + pub const SHIELD_CONTROL: &str = "SHE"; + + pub const ALL_SYSTEMS: [&str; 3] = [ + SHORT_RANGE_SCAN, NAVIGATION, SHIELD_CONTROL + ]; +} + pub fn enterprise() { println!(" @@ -82,7 +92,7 @@ pub fn short_range_scan(model: &Galaxy) { let mut condition = "GREEN"; if quadrant.klingons.len() > 0 { condition = "*RED*"; - } else if model.enterprise.damaged { + } else if model.enterprise.damaged.len() > 0 { condition = "YELLOW"; } @@ -195,3 +205,16 @@ pub fn shields_set(value: u16) { pub fn shields_hit(shields: u16) { println!(" ") } + +pub fn inoperable(arg: &str) { + println!("{} inoperable", arg) +} + +pub fn scanners_out() { + println!("*** Short Range Sensors are out ***") +} + +pub fn damaged_engines(max_warp: f32, warp_factor: f32) { + println!("Warp engines are damaged. Maximum speed = warp {max_warp} + Chief Engineer Scott reports, 'The engines won't take warp {warp_factor} !'") +}