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
use stpsyr::types::*;
impl Stpsyr {
pub fn add_adjust(&mut self, owner: Power, province: Province, action: AdjustAction) {
match self.phase {
Phase::Builds => {},
_ => panic!("add_adjust called during non-build phase")
}
let delta =
if let Some(count) = self.sc_counts().get(&owner) { *count as i32 }
else { return; } -
if let Some(count) = self.unit_counts().get(&owner) { *count as i32 }
else { return; };
let mut dup = false;
let num = self.adjusts.iter()
.filter(|&&Adjust { owner: ref o, province: ref p, action: _ }|
if owner == *o && province == *p {
dup = true; true
} else { owner == *o }).count() as i32;
if dup || match action {
AdjustAction::Disband => delta >= 0 || -num == delta,
AdjustAction::Build { unit_type: _ } => delta <= 0 || num == delta
} { return; }
let region = self.map.iter().find(|r| r.province == province).unwrap();
if !match action {
AdjustAction::Disband => region.unit.as_ref()
.map_or(false, |u| u.owner == owner),
AdjustAction::Build { unit_type: t } => region.unit.is_none() &&
region.home_power.as_ref().map_or(false, |&ref p| *p == owner) &&
match t {
UnitType::Army => !region.army_borders.is_empty(),
UnitType::Fleet => !region.fleet_borders.is_empty()
}
} { return; }
self.adjusts.push(Adjust {
owner: owner,
province: province,
action: action
});
}
pub fn apply_adjusts(&mut self) {
for adjust in self.adjusts.iter() {
let region = self.map.iter_mut()
.find(|r| r.province == adjust.province).unwrap();
match adjust.action {
AdjustAction::Disband => region.unit = None,
AdjustAction::Build { unit_type } => region.unit = Some(Unit {
owner: adjust.owner.clone(), unit_type: unit_type
})
}
}
self.next_phase();
self.adjusts = vec![];
}
}