diff options
author | Tom Barrett <tombarrett@siu.edu> | 2017-11-24 03:42:02 -0600 |
---|---|---|
committer | Tom Barrett <tombarrett@siu.edu> | 2017-11-24 03:42:02 -0600 |
commit | ae4e10f00eab3c9a7a9f5239392e67ef3c39fb1a (patch) | |
tree | e6ca958fe534adc91f898c3212a0f115122af9fa /src/list.rs | |
parent | a853a837b8cb991d8c55496401bef02275139c4e (diff) |
-made all character variables private
-moved path calculation to list level
-moved needs_path to a function
Diffstat (limited to 'src/list.rs')
-rw-r--r-- | src/list.rs | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/src/list.rs b/src/list.rs index 2340385..6292a2f 100644 --- a/src/list.rs +++ b/src/list.rs @@ -1,5 +1,8 @@ extern crate pancurses; +extern crate pathfinding; +use self::pathfinding::astar; + use location::Location; use character::Character; use constants::Colors; @@ -23,16 +26,40 @@ impl List { pub fn action(&mut self) { for i in 0..self.men.len() { - let location = self.men[i].location.clone(); + let location = self.men[i].get_location(); let free_locations = self.get_free_locations(location); self.men[i].action(free_locations); } let impassable = self.get_all_impassable(); for i in 0..self.men.len() { - if self.men[i].needs_path { - self.men[i].calculate_path(impassable.to_vec()); + if self.men[i].needs_path() { + let man = self.men[i].clone(); + let path = self.calculate_path(man, impassable.clone()); + self.men[i].give_path(path); + } + } + } + + fn calculate_path(&mut self, man : Character, impassable : Vec<(Location, usize)>) -> Option<Vec<Location>> { + let desired_location = man.get_desired_location(); + match desired_location { + Some(target) => { + let location = man.get_location(); + let result = astar(&location, + |l| l.neighbours(impassable.clone()), + |l| l.distance(&target), + |l| *l == target); + match result { + Some(mut result) => { + result.0.reverse(); + result.0.pop(); + Some(result.0) + } + None => None, + } } + None => None, } } @@ -84,7 +111,7 @@ impl List { potential_locations.retain(|potential_location| { let mut keep = true; for man in self.men.iter() { - if potential_location.0 == man.location { + if potential_location.0 == man.get_location() { keep = false; } } @@ -102,7 +129,7 @@ impl List { fn get_all_impassable(&mut self) -> Vec<(Location, usize)> { let mut impassable = Vec::new(); for man in self.men.iter() { - impassable.push((man.location, 1)); + impassable.push((man.get_location(), 1)); } for impassable_location in self.impassable_locations.iter() { impassable.push((*impassable_location,1)); |