diff options
author | Tom Barrett <tombarrett@siu.edu> | 2017-11-24 05:03:02 -0600 |
---|---|---|
committer | Tom Barrett <tombarrett@siu.edu> | 2017-11-24 05:03:02 -0600 |
commit | 007bb4def33ac4291f6d5b014ab57f2c760c1e3b (patch) | |
tree | f9fc43a8b452561a373c99a1be269051cf62bd16 /src/list.rs | |
parent | ae4e10f00eab3c9a7a9f5239392e67ef3c39fb1a (diff) |
-added threaded path calculation
Diffstat (limited to 'src/list.rs')
-rw-r--r-- | src/list.rs | 58 |
1 files changed, 37 insertions, 21 deletions
diff --git a/src/list.rs b/src/list.rs index 6292a2f..cba5c16 100644 --- a/src/list.rs +++ b/src/list.rs @@ -1,3 +1,5 @@ +use std::thread::spawn; + extern crate pancurses; extern crate pathfinding; @@ -7,12 +9,14 @@ use location::Location; use character::Character; use constants::Colors; +#[derive(Clone)] pub struct List{ pub men : Vec<Character>, impassable_locations : Vec<Location>, } impl List { + pub fn new(impassable_locations : Vec<Location>) -> List { let mut men = Vec::new(); for i in 0..10 { @@ -31,38 +35,28 @@ impl List { self.men[i].action(free_locations); } + let mut threads = vec!(); let impassable = self.get_all_impassable(); for i in 0..self.men.len() { 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); + let impassable_clone = impassable.clone(); + threads.push(spawn(move || { + (i, calculate_path(man, impassable_clone)) + })); } } - } - 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, - } + for thread in threads { + let data = thread.join(); + match data { + Ok(data) => self.men[data.0].give_path(data.1), + _ => (), } - None => None, } } + pub fn give_destination(&mut self, destination : Location) { for i in 0..self.men.len() { self.men[i].give_destination(destination) @@ -137,3 +131,25 @@ impl List { impassable } } + +fn calculate_path(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, + } +} |