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/character.rs | 1 - src/list.rs | 58 ++++++++++++++++++++++++++++++++++++-------------------- src/location.rs | 1 + 3 files changed, 38 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/character.rs b/src/character.rs index 8a3cb2f..0d19193 100644 --- a/src/character.rs +++ b/src/character.rs @@ -28,7 +28,6 @@ impl Character { } } - pub fn action(&mut self, free_spaces : Vec<(Location,usize)>) { if self.order == Orders::Wander as u8 { self.wander(free_spaces); 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, + } +} diff --git a/src/location.rs b/src/location.rs index a6ac623..509b551 100644 --- a/src/location.rs +++ b/src/location.rs @@ -2,6 +2,7 @@ pub struct Location(pub i32, pub i32); impl Location { + pub fn distance(&self, other: &Location) -> usize { (((self.0 - other.0).pow(2) + (self.1 - other.1).pow(2)) as f64).sqrt() as usize } -- cgit v1.2.3