implemented scan history

This commit is contained in:
Christopher
2023-03-02 20:29:08 +13:00
parent 134b17f77e
commit 765fa23901
4 changed files with 46 additions and 8 deletions

View File

@@ -143,6 +143,7 @@ fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) {
if ship.quadrant != end.quadrant {
view::enter_quadrant(&end.quadrant);
galaxy.scanned.insert(end.quadrant);
if galaxy.quadrants[end.quadrant.as_index()].klingons.len() > 0 {
view::condition_red();
@@ -216,13 +217,16 @@ pub fn display_damage_control(enterprise: &Enterprise) {
println!();
}
pub fn perform_long_range_scan(galaxy: &Galaxy) {
pub fn perform_long_range_scan(galaxy: &mut Galaxy) {
if galaxy.enterprise.damaged.contains_key(model::systems::LONG_RANGE_SCAN) {
view::inoperable(&systems::name_for(systems::LONG_RANGE_SCAN));
return;
}
view::long_range_scan(galaxy);
let seen = view::long_range_scan(galaxy);
for pos in seen {
galaxy.scanned.insert(pos);
}
}
pub fn access_computer(galaxy: &Galaxy, provided: Vec<String>) {
@@ -243,6 +247,7 @@ pub fn access_computer(galaxy: &Galaxy, provided: Vec<String>) {
}
match operation {
0 => view::galaxy_scanned_map(galaxy),
5 => view::galaxy_region_map(),
_ => todo!() // todo implement others
}

View File

@@ -30,7 +30,7 @@ fn main() {
model::systems::WARP_ENGINES => commands::gather_dir_and_speed_then_move(&mut galaxy, command[1..].into()),
model::systems::SHIELD_CONTROL => commands::get_amount_and_set_shields(&mut galaxy, command[1..].into()),
model::systems::DAMAGE_CONTROL => commands::display_damage_control(&galaxy.enterprise),
model::systems::LONG_RANGE_SCAN => commands::perform_long_range_scan(&galaxy),
model::systems::LONG_RANGE_SCAN => commands::perform_long_range_scan(&mut galaxy),
model::systems::COMPUTER => commands::access_computer(&galaxy, command[1..].into()),
_ => view::print_command_help()
}

View File

@@ -1,4 +1,4 @@
use std::{ops::{Mul, Add}, fmt::Display, collections::HashMap};
use std::{ops::{Mul, Add}, fmt::Display, collections::{HashMap, HashSet}};
use rand::Rng;
@@ -8,6 +8,7 @@ pub struct Galaxy {
pub stardate: f32,
pub final_stardate: f32,
pub quadrants: Vec<Quadrant>,
pub scanned: HashSet<Pos>,
pub enterprise: Enterprise
}
@@ -136,7 +137,7 @@ pub struct EndPosition {
pub energy_cost: u16,
}
#[derive(PartialEq, Clone, Copy, Debug)]
#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq)]
pub struct Pos(pub u8, pub u8);
impl Pos {
@@ -206,10 +207,14 @@ impl Galaxy {
let enterprise_sector = quadrants[enterprise_quadrant.as_index()].find_empty_sector();
let stardate = rng.gen_range(20..=40) as f32 * 100.0;
let mut scanned = HashSet::new();
scanned.insert(enterprise_quadrant);
Galaxy {
stardate,
final_stardate: stardate + rng.gen_range(25..=35) as f32,
quadrants: quadrants,
scanned: scanned,
enterprise: Enterprise {
destroyed: false,
damaged: HashMap::new(),

View File

@@ -174,7 +174,7 @@ const SUB_REGION_NAMES: [&str; 4] = ["I", "II", "III", "IV"];
fn quadrant_name(quadrant: &Pos) -> String {
format!("{} {}",
REGION_NAMES[((quadrant.0 << 1) + (quadrant.1 >> 2)) as usize],
REGION_NAMES[((quadrant.1 << 1) + (quadrant.0 >> 2)) as usize],
SUB_REGION_NAMES[(quadrant.1 % 4) as usize])
}
@@ -342,11 +342,13 @@ pub fn system_repair_completed(name: String) {
println!(" {name} repair completed.")
}
pub fn long_range_scan(galaxy: &Galaxy) {
pub fn long_range_scan(galaxy: &Galaxy) -> Vec<Pos> {
let cx = galaxy.enterprise.quadrant.0 as i8;
let cy = galaxy.enterprise.quadrant.1 as i8;
let mut seen = Vec::new();
println!("Long range scan for quadrant {}", galaxy.enterprise.quadrant);
println!("{:-^19}", "");
for y in cy - 1..=cy + 1 {
@@ -356,7 +358,10 @@ pub fn long_range_scan(galaxy: &Galaxy) {
let mut stars = "*".into();
if y >= 0 && y < 8 && x >= 0 && x < 8 {
let quadrant = &galaxy.quadrants[Pos(x as u8, y as u8).as_index()];
let pos = Pos(x as u8, y as u8);
seen.push(pos);
let quadrant = &galaxy.quadrants[pos.as_index()];
klingons = format!("{}", quadrant.klingons.len());
star_bases = quadrant.star_base.map_or("0", |_| "1");
stars = format!("{}", quadrant.stars.len());
@@ -367,6 +372,8 @@ pub fn long_range_scan(galaxy: &Galaxy) {
println!(":");
println!("{:-^19}", "");
}
seen
}
pub fn stranded() {
@@ -397,3 +404,24 @@ pub fn galaxy_region_map() {
----- ----- ----- ----- ----- ----- ----- -----", (i/2)+1, REGION_NAMES[i], REGION_NAMES[i+1]);
}
}
pub(crate) fn galaxy_scanned_map(galaxy: &Galaxy) {
println!(
"Computer record of galaxy for quadrant {}
1 2 3 4 5 6 7 8
----- ----- ----- ----- ----- ----- ----- -----", galaxy.enterprise.quadrant);
for y in 0..8 {
print!("{} ", y+1);
for x in 0..8 {
let pos = Pos(x, y);
if galaxy.scanned.contains(&pos) {
let quadrant = &galaxy.quadrants[pos.as_index()];
print!(" {}{}{} ", quadrant.klingons.len(), quadrant.stars.len(), quadrant.star_base.map_or("0", |_| "1"))
} else {
print!(" *** ");
}
}
println!(
"\n ----- ----- ----- ----- ----- ----- ----- -----")
}
}