mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
basic damage to systems and restricted warp speed
This commit is contained in:
@@ -1,8 +1,20 @@
|
|||||||
use crate::{model::{Galaxy, Pos, COURSES, EndPosition}, view, input};
|
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<String>) {
|
pub fn get_amount_and_set_shields(galaxy: &mut Galaxy, provided: Vec<String>) {
|
||||||
|
|
||||||
// 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);
|
view::energy_available(galaxy.enterprise.total_energy);
|
||||||
let value = input::param_or_prompt_value(&provided, 0, "Number of units to shields", 0, i32::MAX);
|
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<String>) {
|
|||||||
view::shields_unchanged();
|
view::shields_unchanged();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let value = value.unwrap() as u16;
|
let value = value.unwrap() as u16;
|
||||||
if value > galaxy.enterprise.total_energy {
|
if value > galaxy.enterprise.total_energy {
|
||||||
view::ridiculous();
|
view::ridiculous();
|
||||||
@@ -29,17 +42,31 @@ pub fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy, provided: Vec<String>
|
|||||||
return;
|
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() {
|
if speed.is_none() {
|
||||||
view::bad_nav();
|
view::bad_nav();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let speed = speed.unwrap();
|
||||||
|
|
||||||
|
if speed > max_warp {
|
||||||
|
view::damaged_engines(max_warp, speed);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
move_klingons_and_fire(galaxy);
|
move_klingons_and_fire(galaxy);
|
||||||
if galaxy.enterprise.destroyed {
|
if galaxy.enterprise.destroyed {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
move_enterprise(course.unwrap(), speed.unwrap(), galaxy);
|
move_enterprise(course, speed, galaxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) {
|
fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) {
|
||||||
|
|||||||
@@ -26,9 +26,9 @@ fn main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
match command[0].to_uppercase().as_str() {
|
match command[0].to_uppercase().as_str() {
|
||||||
"SRS" => view::short_range_scan(&galaxy),
|
view::keys::SHORT_RANGE_SCAN => commands::perform_short_range_scan(&galaxy),
|
||||||
"NAV" => commands::gather_dir_and_speed_then_move(&mut galaxy, command[1..].into()),
|
view::keys::NAVIGATION => 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::SHIELD_CONTROL => commands::get_amount_and_set_shields(&mut galaxy, command[1..].into()),
|
||||||
_ => view::print_command_help()
|
_ => view::print_command_help()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use std::{ops::{Mul, Add}, fmt::Display};
|
use std::{ops::{Mul, Add}, fmt::Display, collections::HashMap};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ impl Klingon {
|
|||||||
|
|
||||||
pub struct Enterprise {
|
pub struct Enterprise {
|
||||||
pub destroyed: bool,
|
pub destroyed: bool,
|
||||||
pub damaged: bool, // later this could be by subsystem
|
pub damaged: HashMap<String, f32>,
|
||||||
pub quadrant: Pos,
|
pub quadrant: Pos,
|
||||||
pub sector: Pos,
|
pub sector: Pos,
|
||||||
pub photon_torpedoes: u8,
|
pub photon_torpedoes: u8,
|
||||||
@@ -147,7 +147,7 @@ impl Galaxy {
|
|||||||
quadrants: quadrants,
|
quadrants: quadrants,
|
||||||
enterprise: Enterprise {
|
enterprise: Enterprise {
|
||||||
destroyed: false,
|
destroyed: false,
|
||||||
damaged: false,
|
damaged: HashMap::new(),
|
||||||
quadrant: enterprise_quadrant,
|
quadrant: enterprise_quadrant,
|
||||||
sector: enterprise_sector,
|
sector: enterprise_sector,
|
||||||
photon_torpedoes: 28,
|
photon_torpedoes: 28,
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
use crate::model::{Galaxy, Pos, EndPosition, SectorStatus};
|
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() {
|
pub fn enterprise() {
|
||||||
println!("
|
println!("
|
||||||
|
|
||||||
@@ -82,7 +92,7 @@ pub fn short_range_scan(model: &Galaxy) {
|
|||||||
let mut condition = "GREEN";
|
let mut condition = "GREEN";
|
||||||
if quadrant.klingons.len() > 0 {
|
if quadrant.klingons.len() > 0 {
|
||||||
condition = "*RED*";
|
condition = "*RED*";
|
||||||
} else if model.enterprise.damaged {
|
} else if model.enterprise.damaged.len() > 0 {
|
||||||
condition = "YELLOW";
|
condition = "YELLOW";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,3 +205,16 @@ pub fn shields_set(value: u16) {
|
|||||||
pub fn shields_hit(shields: u16) {
|
pub fn shields_hit(shields: u16) {
|
||||||
println!(" <Shields down to {shields} units>")
|
println!(" <Shields down to {shields} units>")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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} !'")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user