fixed calculation for direction

This commit is contained in:
Christopher
2023-03-05 11:05:43 +13:00
parent 5542e2fe59
commit 2dba25c4ee
3 changed files with 9 additions and 7 deletions

View File

@@ -154,16 +154,17 @@ impl Pos {
self.0.abs_diff(other.0) + self.1.abs_diff(other.1)
}
pub fn dist(&self, other: Pos) -> f32 {
pub fn dist(&self, other: Pos) -> u8 {
let dx = other.0 as f32 - self.0 as f32;
let dy = other.1 as f32 - self.1 as f32;
(f32::powi(dx, 2) + f32::powi(dy, 2)).sqrt()
(f32::powi(dx, 2) + f32::powi(dy, 2)).sqrt() as u8
}
pub fn direction(&self, other: Pos) -> f32 {
// this is a replication of the original BASIC code
let dx = other.0 as f32 - self.0 as f32;
let dy = other.1 as f32 - self.1 as f32;
let dy = other.0 as f32 - self.0 as f32;
let dx = other.1 as f32 - self.1 as f32;
// note i actually use x,y, but the calculation assumes y,x so they're flipped above
let dx_dominant = dx.abs() > dy.abs();
let frac = if dx_dominant { dy / dx } else { -dx / dy };

View File

@@ -433,7 +433,7 @@ pub fn long_range_scan(galaxy: &Galaxy) -> Vec<Pos> {
stars = format!("{}", quadrant.stars.len());
}
print!(": {}{}{} ", klingons, stars, star_bases)
print!(": {}{}{} ", klingons, star_bases, stars)
}
println!(":");
println!("{:-^19}", "");
@@ -482,7 +482,7 @@ pub fn galaxy_scanned_map(galaxy: &Galaxy) {
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.as_ref().map_or("0", |_| "1"))
print!(" {}{}{} ", quadrant.klingons.len(), quadrant.star_base.as_ref().map_or("0", |_| "1"), quadrant.stars.len())
} else {
print!(" *** ");
}
@@ -589,7 +589,7 @@ pub fn starbase_report() {
println!("From Enterprise to Starbase:'")
}
pub fn direction_distance(dir: f32, dist: f32) {
pub fn direction_distance(dir: f32, dist: u8) {
println!(
"Direction = {dir}
Distance = {dist}"

View File

@@ -45,3 +45,4 @@ Started after movement and display of stats was finished (no energy management o
- [x] defeat due to time expired
- [x] intro instructions
- [x] victory
- [ ] switch from x,y to y,x