From 007bb4def33ac4291f6d5b014ab57f2c760c1e3b Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Fri, 24 Nov 2017 05:03:02 -0600 Subject: -added threaded path calculation --- src/list.rs | 58 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'src/list.rs') 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, impassable_locations : Vec, } impl List { + pub fn new(impassable_locations : Vec) -> 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> { - 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> { + 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, + } +} -- cgit v1.2.3