Skip to content

Commit 2006c68

Browse files
committed
more accurate distances
1 parent 208fc9d commit 2006c68

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

navpointmatrixcli/src/cli.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub enum Command {
1515
ConnectedNodesFromNavpointMatrix(ConnectedNodesFromNavpointMatrixArgs),
1616
ConnectedNodesFromNavigationData(ConnectedNodesFromNavigationDataArgs),
1717
NavpointMatrix(BuildNavpointMatrixArgs),
18+
ShowPath(ShowPathArgs),
1819
}
1920

2021
#[derive(Args, Debug)]
@@ -67,3 +68,14 @@ pub enum ConnectedNodesMode {
6768
BresenhamLine,
6869
//P3,
6970
}
71+
72+
#[derive(Args, Debug)]
73+
pub struct ShowPathArgs {
74+
/// Source node index
75+
#[arg(value_name = "source index", required = true)]
76+
pub source_index: u16,
77+
78+
/// Destination node index
79+
#[arg(value_name = "destination index", required = true)]
80+
pub destination_index: u16,
81+
}

navpointmatrixcli/src/main.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clap::Parser;
2-
use cli::{BuildNavpointMatrixArgs, Cli, Command, ConnectedNodesFromNavigationDataArgs, ConnectedNodesFromNavpointMatrixArgs};
2+
use cli::{BuildNavpointMatrixArgs, Cli, Command, ConnectedNodesFromNavigationDataArgs, ConnectedNodesFromNavpointMatrixArgs, ShowPathArgs};
33
use connected_nodes::ConnectedNodes;
44
use p3_api::data::{navigation_matrix::NavigationMatrix, navigation_vector::NavigationVector, navpoint_matrix::NavpointMatrix};
55
use pathfinding::prelude::{build_path, dijkstra_all};
@@ -18,6 +18,7 @@ fn main() {
1818
Command::ConnectedNodesFromNavpointMatrix(args) => build_connected_nodes_from_navpoint_matrix(args),
1919
Command::ConnectedNodesFromNavigationData(args) => build_connected_nodes_from_navigation_data(args),
2020
Command::Test => test(),
21+
Command::ShowPath(show_path_args) => show_path(show_path_args),
2122
}
2223
}
2324

@@ -56,10 +57,26 @@ fn build_connected_nodes_from_navigation_data(args: ConnectedNodesFromNavigation
5657
fs::write(args.output_file, connected_nodes.serialize()).unwrap();
5758
}
5859

60+
fn show_path(args: ShowPathArgs) {
61+
let navpoint_matrix = NavpointMatrix::deserialize(&fs::read(r"C:\Users\Benni\Patrician 3_workbench\navdata\matrix_int.dat.orig").unwrap());
62+
let navigation_vector = NavigationVector::deserialize(&fs::read(r"C:\Users\Benni\Patrician 3_workbench\navdata\nav_vec.dat.orig").unwrap());
63+
let mut current = args.source_index;
64+
let current_point = navigation_vector.points[current as usize];
65+
let mut buf = format!("{current} {current_point:x?}");
66+
let distance = navpoint_matrix.get(current, args.destination_index, 350).distance;
67+
while current != args.destination_index {
68+
let cell = navpoint_matrix.get(current, args.destination_index, 350);
69+
current = cell.next;
70+
let current_point = navigation_vector.points[current as usize];
71+
buf.push_str(&format!(" -> {current} {current_point:x?}"));
72+
}
73+
println!("{buf} (declared distance: {distance})");
74+
}
75+
5976
fn test() {
6077
let start = Instant::now();
61-
let navigation_vector = NavigationVector::deserialize(&fs::read(r"C:\Users\Benni\Patrician 3_workbench\navdata\nav_vec.dat").unwrap());
62-
let original_navpoint_matrix = NavpointMatrix::deserialize(&fs::read(r"C:\Users\Benni\Patrician 3_workbench\navdata\matrix_int.dat").unwrap());
78+
let navigation_vector = NavigationVector::deserialize(&fs::read(r"C:\Users\Benni\Patrician 3_workbench\navdata\nav_vec.dat.orig").unwrap());
79+
let original_navpoint_matrix = NavpointMatrix::deserialize(&fs::read(r"C:\Users\Benni\Patrician 3_workbench\navdata\matrix_int.dat.orig").unwrap());
6380
let connected_nodes = ConnectedNodes::from_navpoint_matrix(&original_navpoint_matrix);
6481
let mut new_navpoint_matrix = NavpointMatrix::new(navigation_vector.length);
6582

@@ -99,7 +116,8 @@ fn test() {
99116
let orig_distance = original_navpoint_matrix.matrix[i].distance;
100117
let calculated_distance = new_navpoint_matrix.matrix[i].distance;
101118
if orig_distance != calculated_distance {
102-
println!("cell {i}: distance {orig_distance} != {calculated_distance}");
119+
let (source, destination) = new_navpoint_matrix.get_source_and_destination(i, navigation_vector.length);
120+
println!("cell {i}: distance {orig_distance} != {calculated_distance} ({source} -> {destination})");
103121
bad_distance_cells += 1;
104122
}
105123
}

p3-api/src/data/navigation_vector.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ops::Shl;
2+
13
#[derive(Debug)]
24
pub struct NavigationVector {
35
pub length: u16,
@@ -18,17 +20,18 @@ impl NavigationVector {
1820
}
1921

2022
pub fn get_path_length(&self, path: &[u16]) -> i32 {
21-
let mut distance = 0.0;
23+
let mut distance = 0;
2224
for i in 0..path.len() - 1 {
2325
let x1 = self.points[path[i] as usize].0 as i32;
2426
let y1 = self.points[path[i] as usize].1 as i32;
2527
let x2 = self.points[path[i + 1] as usize].0 as i32;
2628
let y2 = self.points[path[i + 1] as usize].1 as i32;
27-
let dx = (x2 - x1) as f64;
28-
let dy = (y2 - y1) as f64;
29-
distance += (dx * dx + dy * dy).sqrt();
29+
let dx = (x2 - x1) as i64;
30+
let dy = (y2 - y1) as i64;
31+
let squared = dx * dx + dy * dy;
32+
distance += ((squared.shl(32) as f64).sqrt() + 0.5) as i64;
3033
}
3134

32-
(distance * 65536.0).round() as i32
35+
(distance) as i32
3336
}
3437
}

p3-api/src/data/navpoint_matrix.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,19 @@ impl NavpointMatrix {
4646
pub fn set_next(&mut self, source: u16, destination: u16, next: u16, distance: i32, width: u16) {
4747
self.matrix[source as usize * width as usize + destination as usize] = NavpointMatrixCell { distance, next }
4848
}
49+
50+
pub fn get_next(&self, source: u16, destination: u16, width: u16) -> u16 {
51+
self.matrix[source as usize * width as usize + destination as usize].next
52+
}
53+
54+
pub fn get(&self, source: u16, destination: u16, width: u16) -> &NavpointMatrixCell {
55+
&self.matrix[source as usize * width as usize + destination as usize]
56+
}
57+
58+
pub fn get_source_and_destination(&self, cell: usize, width: u16) -> (u16, u16) {
59+
let width = width as usize;
60+
let destination = cell % width;
61+
let source = cell / width;
62+
(source as _, destination as _)
63+
}
4964
}

0 commit comments

Comments
 (0)