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
use std::collections::HashSet;
use stpsyr::types::*;
impl Stpsyr {
pub fn add_retreat(&mut self, owner: Power, province: Province, action: RetreatAction) {
match self.phase {
Phase::SpringRetreats | Phase::FallRetreats => {},
_ => panic!("add_retreat called during non-retreat phase")
};
let unit = if let Some(unit) = self.dislodged.iter().find(|&&(ref p, _)|
p == &province).map(|&(_, ref u)| u.clone()) { unit }
else { return; };
if unit.owner != owner { return; }
if match &action {
&RetreatAction::Move { ref to } => {
let r = self.get_region(&province).unwrap();
self.contested.contains(to) || !match unit.unit_type {
UnitType::Army => r.army_borders.clone(),
UnitType::Fleet => r.fleet_borders.clone().into_iter()
.filter(|p|
p.from_coast == r.province.coast &&
p.coast == to.coast)
.collect()
}.contains(&to)
},
_ => false
} { return; }
self.retreats.push(Retreat {
owner: owner,
province: province,
action: action
});
}
pub fn apply_retreats(&mut self) {
self.dislodged = vec![];
{
let (mut attempts, mut conflicts) = (HashSet::new(), HashSet::new());
for retreat in self.retreats.iter() {
match &retreat.action {
&RetreatAction::Move { ref to } => {
if attempts.contains(to) { conflicts.insert(to); }
else { attempts.insert(to); }
},
_ => {}
}
}
for retreat in self.retreats.iter() {
match &retreat.action {
&RetreatAction::Move { ref to } => {
if !conflicts.contains(to) {
let from_idx = self.map.iter()
.position(|r| r.province == retreat.province).unwrap();
let to_idx = self.map.iter()
.position(|r| r.province == *to).unwrap();
assert!(self.map[to_idx].unit.is_none());
self.map[to_idx].unit = self.map[from_idx].unit.clone();
}
},
&RetreatAction::Disband => {}
}
}
}
self.next_phase();
self.retreats = vec![];
}
}