1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
extern crate pancurses;
extern crate pathfinding;
use self::pathfinding::astar;
use location::Location;
use character::Character;
use constants::Colors;
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 {
men.push(Character::new('@', Colors::BlueUnit as u8, Location(150,150+i)));
}
List {
men : men,
impassable_locations : impassable_locations,
}
}
pub fn action(&mut self) {
for i in 0..self.men.len() {
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() {
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,
}
}
pub fn give_destination(&mut self, destination : Location) {
for i in 0..self.men.len() {
self.men[i].give_destination(destination)
}
}
pub fn give_grid(&mut self, first_location : Location, second_location : Location) {
let mut index = 0;
for i in first_location.0..second_location.0 + 1 {
for j in first_location.1..second_location.1 + 1 {
if index < self.men.len() {
self.men[index].give_destination(Location(i,j));
index += 1;
}
}
}
for i in second_location.0..first_location.0 {
for j in second_location.1..first_location.1 {
if index < self.men.len() {
self.men[index].give_destination(Location(i,j));
index += 1;
}
}
}
for i in first_location.0..second_location.0 + 1 {
for j in second_location.1..first_location.1 {
if index < self.men.len() {
self.men[index].give_destination(Location(i,j));
index += 1;
}
}
}
for i in second_location.0..first_location.0 {
for j in first_location.1..second_location.1 + 1 {
if index < self.men.len() {
self.men[index].give_destination(Location(i,j));
index += 1;
}
}
}
}
fn get_free_locations(&mut self, location : Location) -> Vec<(Location, usize)> {
let mut potential_locations = location.neighbours(Vec::new());
potential_locations.retain(|potential_location| {
let mut keep = true;
for man in self.men.iter() {
if potential_location.0 == man.get_location() {
keep = false;
}
}
for impassable_location in self.impassable_locations.iter() {
if potential_location.0 == *impassable_location {
keep = false;
}
}
keep
});
potential_locations
}
fn get_all_impassable(&mut self) -> Vec<(Location, usize)> {
let mut impassable = Vec::new();
for man in self.men.iter() {
impassable.push((man.get_location(), 1));
}
for impassable_location in self.impassable_locations.iter() {
impassable.push((*impassable_location,1));
}
impassable
}
}
|