diff --git a/84_Super_Star_Trek/rust/src/commands.rs b/84_Super_Star_Trek/rust/src/commands.rs index d55b08ff..3cc4aff5 100644 --- a/84_Super_Star_Trek/rust/src/commands.rs +++ b/84_Super_Star_Trek/rust/src/commands.rs @@ -2,25 +2,43 @@ use crate::{model::{Galaxy, Pos, COURSES, EndPosition}, view}; pub fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) { - let end = find_end_quadrant_sector(galaxy.enterprise.quadrant, galaxy.enterprise.sector, course, warp_speed); + let ship = &mut galaxy.enterprise; + + // todo account for being blocked + + let end = find_end_quadrant_sector(ship.quadrant, ship.sector, course, warp_speed); + + // todo account for engine damage + + if end.energy_cost > ship.total_energy { + view::insuffient_warp_energy(warp_speed); + return + } if end.hit_edge { view::hit_edge(&end); } + - if galaxy.enterprise.quadrant != end.quadrant { + if ship.quadrant != end.quadrant { view::enter_quadrant(&end.quadrant); if galaxy.quadrants[end.quadrant.as_index()].klingons.len() > 0 { view::condition_red(); - if galaxy.enterprise.shields <= 200 { + if ship.shields <= 200 { view::danger_shields(); } } } - galaxy.enterprise.quadrant = end.quadrant; - galaxy.enterprise.sector = end.sector; + ship.quadrant = end.quadrant; + ship.sector = end.sector; + + ship.total_energy = (ship.total_energy - end.energy_cost).max(0); + if ship.shields > ship.total_energy { + view::divert_energy_from_shields(); + ship.shields = ship.total_energy; + } view::short_range_scan(&galaxy) } @@ -58,8 +76,9 @@ fn find_end_quadrant_sector(start_quadrant: Pos, start_sector: Pos, course: u8, let quadrant = Pos((nx / 8) as u8, (ny / 8) as u8); let sector = Pos((nx % 8) as u8, (ny % 8) as u8); + let energy_cost = distance as u16 + 10; - EndPosition { quadrant, sector, hit_edge } + EndPosition { quadrant, sector, hit_edge, energy_cost } } pub fn move_klingons_and_fire(galaxy: &mut Galaxy) { diff --git a/84_Super_Star_Trek/rust/src/model.rs b/84_Super_Star_Trek/rust/src/model.rs index 9caa100a..103887f5 100644 --- a/84_Super_Star_Trek/rust/src/model.rs +++ b/84_Super_Star_Trek/rust/src/model.rs @@ -67,7 +67,8 @@ impl Enterprise { pub struct EndPosition { pub quadrant: Pos, pub sector: Pos, - pub hit_edge: bool + pub hit_edge: bool, + pub energy_cost: u16, } #[derive(PartialEq, Clone, Copy, Debug)] diff --git a/84_Super_Star_Trek/rust/src/view.rs b/84_Super_Star_Trek/rust/src/view.rs index 320cb4eb..f460eaa7 100644 --- a/84_Super_Star_Trek/rust/src/view.rs +++ b/84_Super_Star_Trek/rust/src/view.rs @@ -165,3 +165,12 @@ pub fn condition_red() { pub fn danger_shields() { println!(" SHIELDS DANGEROUSLY LOW ") } + +pub fn insuffient_warp_energy(warp_speed: f32) { + println!("Engineering reports, 'Insufficient energy available + for maneuvering at warp {warp_speed} !'") +} + +pub fn divert_energy_from_shields() { + println!("Shield Control supplies energy to complete the maneuver.") +}