summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Barrett <tombarrett@siu.edu>2017-11-24 05:03:02 -0600
committerTom Barrett <tombarrett@siu.edu>2017-11-24 05:03:02 -0600
commit007bb4def33ac4291f6d5b014ab57f2c760c1e3b (patch)
treef9fc43a8b452561a373c99a1be269051cf62bd16
parentae4e10f00eab3c9a7a9f5239392e67ef3c39fb1a (diff)
-added threaded path calculation
-rw-r--r--src/character.rs1
-rw-r--r--src/list.rs58
-rw-r--r--src/location.rs1
3 files changed, 38 insertions, 22 deletions
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<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,
+ }
+}
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
}