diff --git a/84_Super_Star_Trek/rust/src/commands.rs b/84_Super_Star_Trek/rust/src/commands.rs index 264f7909..e87dd9c0 100644 --- a/84_Super_Star_Trek/rust/src/commands.rs +++ b/84_Super_Star_Trek/rust/src/commands.rs @@ -1,4 +1,4 @@ -use crate::model::{Galaxy, Pos, SectorStatus}; +use crate::model::{Galaxy, Pos, SectorStatus, COURSES}; pub fn short_range_scan(model: &Galaxy) { let quadrant = &model.quadrants[model.enterprise.sector.as_index()]; @@ -22,3 +22,49 @@ pub fn short_range_scan(model: &Galaxy) { } println!("{:-^33}", ""); } + +pub fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) { + let distance = (warp_speed * 8.0) as i8; + let galaxy_pos = galaxy.enterprise.quadrant * 8u8 + galaxy.enterprise.sector; + + let (dx, dy): (i8, i8) = COURSES[(course - 1) as usize]; + + let mut nx = (galaxy_pos.0 as i8) + dx * distance; + let mut ny = (galaxy_pos.1 as i8) + dy * distance; + + let mut hit_edge = false; + if nx < 0 { + nx = 0; + hit_edge = true; + } + if ny < 0 { + ny = 0; + hit_edge = true; + } + if nx >= 64 { + ny = 63; + hit_edge = true; + } + if nx >= 64 { + ny = 63; + hit_edge = true; + } + + let new_quadrant = Pos((nx / 8) as u8, (ny / 8) as u8); + let new_sector = Pos((nx % 8) as u8, (ny % 8) as u8); + + if hit_edge { + println!("Lt. Uhura report message from Starfleet Command: + 'Permission to attempt crossing of galactic perimeter + is hereby *Denied*. Shut down your engines.' + Chief Engineer Scott reports, 'Warp engines shut down + at sector {} of quadrant {}.'", new_quadrant, new_sector); + } + + galaxy.enterprise.quadrant = new_quadrant; + galaxy.enterprise.sector = new_sector; + + // if new_quadrant isnt old quadrant print intro + + short_range_scan(&galaxy) +} diff --git a/84_Super_Star_Trek/rust/src/main.rs b/84_Super_Star_Trek/rust/src/main.rs index a7265116..a9b5f4ce 100644 --- a/84_Super_Star_Trek/rust/src/main.rs +++ b/84_Super_Star_Trek/rust/src/main.rs @@ -1,8 +1,6 @@ use std::{io::{stdin, stdout, Write}, process::exit, str::FromStr}; -use model::{Galaxy, Pos}; - -use crate::model::DIRECTIONS; +use model::Galaxy; mod model; mod commands; @@ -12,7 +10,7 @@ fn main() { .expect("Error setting Ctrl-C handler"); let mut galaxy = Galaxy::generate_new(); - // init ops, starting state and notes + // init options, starting state and notes commands::short_range_scan(&galaxy); loop { @@ -21,18 +19,14 @@ fn main() { "NAV" => gather_dir_and_speed_then_move(&mut galaxy), _ => print_command_help() } - - // process the next command, based on it render something or update the galaxy or whatever - // this would be: read command, and based on it run dedicated function - // the function might get passed a mutable reference to the galaxy } } fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) { const BAD_NAV: &str = " Lt. Sulu reports, 'Incorrect course data, sir!'"; - let dir = prompt_value::("Course (1-9)?", 1, 9); - if dir.is_none() { + let course = prompt_value::("Course (1-9)?", 1, 9); + if course.is_none() { println!("{}", BAD_NAV); return; } @@ -43,48 +37,7 @@ fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) { return; } - let distance = (speed.unwrap() * 8.0) as i8; - let galaxy_pos = galaxy.enterprise.quadrant * 8u8 + galaxy.enterprise.sector; - - let (dx, dy): (i8, i8) = DIRECTIONS[(dir.unwrap() - 1) as usize]; - let mut nx = (galaxy_pos.0 as i8) + dx * distance; - let mut ny = (galaxy_pos.1 as i8) + dy * distance; - - let mut hit_edge = false; - if nx < 0 { - nx = 0; - hit_edge = true; - } - if ny < 0 { - ny = 0; - hit_edge = true; - } - if nx >= 64 { - ny = 63; - hit_edge = true; - } - if nx >= 64 { - ny = 63; - hit_edge = true; - } - - let new_quadrant = Pos((nx / 8) as u8, (ny / 8) as u8); - let new_sector = Pos((nx % 8) as u8, (ny % 8) as u8); - - if hit_edge { - println!("Lt. Uhura report message from Starfleet Command: - 'Permission to attempt crossing of galactic perimeter - is hereby *Denied*. Shut down your engines.' - Chief Engineer Scott reports, 'Warp engines shut down - at sector {} of quadrant {}.'", new_quadrant, new_sector); - } - - galaxy.enterprise.quadrant = new_quadrant; - galaxy.enterprise.sector = new_sector; - - // if new_quadrant isnt old quadrant print intro - - commands::short_range_scan(&galaxy) + commands::move_enterprise(course.unwrap(), speed.unwrap(), galaxy); } fn prompt(prompt_text: &str) -> String { diff --git a/84_Super_Star_Trek/rust/src/model.rs b/84_Super_Star_Trek/rust/src/model.rs index 2e097b9c..2ea85548 100644 --- a/84_Super_Star_Trek/rust/src/model.rs +++ b/84_Super_Star_Trek/rust/src/model.rs @@ -38,7 +38,7 @@ impl Display for Pos { } } -pub const DIRECTIONS : [(i8, i8); 8] = [ +pub const COURSES : [(i8, i8); 8] = [ (1, 0), (1, -1), (0, -1),