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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use std::fs::File;
use std::io::{self, BufReader, BufRead, Write};
use std::collections::HashMap;
use stpsyr::types::*;
enum LookState {
Nothing,
Provinces,
Units
}
impl Stpsyr {
pub fn render_svg(&self, path: String) -> io::Result<()> {
let mut in_file = BufReader::new(File::open("data/standard.svg")?);
let mut out_file = File::create(path)?;
let mut look_state = LookState::Nothing;
let mut style_line = String::new();
let mut data_line = String::new();
let mut province_centers = HashMap::new();
for line in in_file.lines() {
let mut line = line?;
match look_state {
_ if line.trim() == "</g>" => look_state = LookState::Nothing,
LookState::Provinces if line.trim().starts_with("id=\"") => {
let quote_idx = line.find('"').unwrap() + 1;
let province = Province::from(line.chars().skip(quote_idx)
.take(3).collect::<String>());
let color = if let Some(p) = self.map.iter()
.find(|&r| r.province == province) {
if p.army_borders.is_empty() { "c5dfea" }
else if p.owner == Some(Power::from("Russia")) { "a87e9f" }
else if p.owner == Some(Power::from("Austria")) { "c48f85" }
else if p.owner == Some(Power::from("Turkey")) { "eaeaaf" }
else if p.owner == Some(Power::from("Italy")) { "a4c499" }
else if p.owner == Some(Power::from("France")) { "79afc6" }
else if p.owner == Some(Power::from("Germany")) { "a08a75" }
else if p.owner == Some(Power::from("England")) { "efc4e4" }
else { "e2c69e" }
} else { "000000" };
let hash_idx = style_line.find('#').unwrap() + 1;
write!(out_file, "{}{}",
style_line.drain(..hash_idx).collect::<String>(),
color)?;
style_line.drain(..6);
writeln!(out_file, "{}", style_line)?;
let mut points = vec![];
let mut mode = 'L';
let (mut last_x, mut last_y) = (0f32, 0f32);
for rule in data_line.split_whitespace().skip(1) {
if rule.len() <= 2 {
mode = rule.chars().next().unwrap();
} else {
match mode {
'H' => last_x = rule.parse().unwrap(),
'V' => last_y = rule.parse().unwrap(),
'L' => {
let mut rule = rule.split(',');
last_x = rule.next().unwrap().parse().unwrap();
last_y = rule.next().unwrap().parse().unwrap();
},
_ => unreachable!()
}
points.push((last_x, last_y));
}
}
let (first_x, first_y) = points[0];
points.push((first_x, first_y));
writeln!(out_file, "{}", data_line)?;
province_centers.insert(province, poly_center(points));
},
LookState::Provinces if line.trim().starts_with("d=\"") => {
data_line = line.clone();
line = String::new();
},
LookState::Provinces if line.trim().starts_with("style=\"fill") => {
style_line = line.clone();
line = String::new();
},
LookState::Units => if line.find('>').is_some() {
write!(out_file, "{}", line)?;
line = String::new();
for r in self.map.iter() {
if let Some(ref unit) = r.unit {
let &(x, y) = province_centers.get(&r.province).unwrap();
writeln!(out_file)?;
match unit.unit_type {
UnitType::Army => write!(out_file,
r#"<g style="fill:#006400" transform="translate({},{})">
<rect y="-0.2761367" x="-1.5930907" height="1.2319912" width="3.6534909" ry="1" rx="1" />
<rect y="-0.9558546" x="-0.6372381" height="0.9133727" width="1.8904694" />
<rect y="-0.7576384" x="-2.0604001" height="0.3257652" width="1.5718508" />
</g>"#,
x, y)?,
UnitType::Fleet => write!(out_file,
r#"<g style="fill:#0000ff" transform="translate({},{})">
<path d="M -0.58115325,-1.92628385 V 0.33837375 H 1.40624905 Z" />
<path d="M -1.68162975,0.69317735 H 1.68162975 L 0.85722605,1.92628385 H -0.85722605 Z" />
</g>"#,
x, y)?
}
}
}
},
_ => if line.trim() == "id=\"provinces\"" {
look_state = LookState::Provinces;
} else if line.trim() == "id=\"units\"" {
look_state = LookState::Units;
}
}
writeln!(out_file, "{}", line)?;
}
Ok(())
}
}
fn poly_center(verts: Vec<(f32, f32)>) -> (f32, f32) {
let (mut min_x, mut min_y, mut max_x, mut max_y) =
(9999999f32, 9999999f32, 0f32, 0f32);
for p in verts.iter() {
if p.0 < min_x { min_x = p.0; }
if p.0 > max_x { max_x = p.0; }
if p.1 < min_y { min_y = p.1; }
if p.1 > max_y { max_y = p.1; }
}
let n = 50;
while max_x - min_x > 2f32 && max_y - min_y > 2f32 {
let (mut best_x, mut best_y, mut best_dist) = (0f32, 0f32, 0f32);
let (dx, dy) = ((max_x - min_x) / (n as f32), (max_y - min_y) / (n as f32));
for i in 1..n {
for j in 1..n {
let (i, j) = (i as f32, j as f32);
let (test_x, test_y) = (min_x + i*dx, min_y + j*dy);
let test_dist = poly_distance(&verts, (test_x, test_y));
if test_dist > best_dist {
best_x = test_x;
best_y = test_y;
best_dist = test_dist;
}
}
}
min_x = best_x - dx;
max_x = best_x + dx;
min_y = best_y - dy;
max_y = best_y + dy;
}
((min_x + max_x) / 2f32, (min_y + max_y) / 2f32)
}
fn poly_distance(verts: &Vec<(f32, f32)>, p: (f32, f32)) -> f32 {
let mut dist = 9999999f32;
let mut inside = false;
for it in verts.windows(2) {
if ((it[0].1 > p.1) ^ (it[1].1 > p.1)) &&
p.0 < (it[1].0 - it[0].0) * (p.1 - it[0].1) / (it[1].1 - it[0].1)
+ it[0].0 { inside = !inside; }
let t = (((it[1].0 - it[0].0) * (p.0 - it[0].0) +
(it[1].1 - it[0].1) * (p.1 - it[0].1)) /
((it[0].0 - it[1].0).powi(2) +
(it[0].1 - it[1].1).powi(2))).max(0f32).min(1f32);
dist = dist.min(
(it[0].0 + t * (it[1].0 - it[0].0) - p.0).powi(2) +
(it[0].1 + t * (it[1].1 - it[0].1) - p.1).powi(2));
}
if inside { dist } else { 0f32 }
}