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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use rand;
use self::rand::seq::SliceRandom;
use self::rand::Rng;
use crate::board::{Board, Pos};
use crate::land::LandKind;
use crate::slope::SlopeGen;
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::rc::Rc;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
struct Connection<'a> {
from: &'a Pos,
to: &'a Pos,
}
pub struct LargeBoardGen<'a, R: Rng> {
rng: &'a mut R,
width: usize,
height: usize,
max_towns: usize,
num_tops: usize,
town_min_cost: usize,
conn_max_cost: usize,
down_rate: u8,
}
// This warning is raised only on wasm32 target since Pos contains usize fields.
// usize field have 64bits length on x86_64 but have 32bits on wasm32.
// So using `self` argument is efficient on wasm32, but not on x86_64.
#[allow(clippy::trivially_copy_pass_by_ref)]
impl<'a, R: Rng> LargeBoardGen<'a, R> {
pub fn new<'b: 'a>(rng: &'b mut R, width: usize, height: usize) -> Self {
let max_towns = rng.gen_range(10, 16);
let num_tops = width * height / 2048 + rng.gen_range(0, 4);
let average_len = (width + height) / 2;
let town_min_cost = average_len.checked_div(max_towns).unwrap_or(average_len);
let conn_max_cost = average_len / 2;
let down_rate = 6; // Set smaller down rate for larger map
LargeBoardGen {
rng,
height,
width,
max_towns,
num_tops,
town_min_cost,
conn_max_cost,
down_rate,
}
}
#[inline]
fn land_kind(altitude: u8) -> LandKind {
match altitude {
0..=40 => LandKind::DeepSea,
41..=55 => LandKind::Sea,
56..=70 => LandKind::Plain,
71..=80 => LandKind::Forest,
81..=90 => LandKind::Mountain,
91..=99 => LandKind::Highland,
_ => unreachable!(),
}
}
#[allow(clippy::needless_range_loop)]
fn towns(&mut self, altitudes: &[Vec<u8>]) -> HashSet<Pos> {
#[inline]
fn land_fitness(kind: LandKind) -> u8 {
match kind {
LandKind::DeepSea => 0,
LandKind::Sea => 16,
LandKind::Plain => 8,
LandKind::Forest => 4,
LandKind::Mountain => 2,
LandKind::Highland => 1,
_ => unreachable!(),
}
}
// Initialize fitness
let mut fitness = Vec::with_capacity(self.height);
for y in 0..self.height {
let mut row = Vec::with_capacity(self.width);
for x in 0..self.width {
row.push(land_fitness(Self::land_kind(altitudes[y][x])))
}
fitness.push(row);
}
// Cells at edges of map, (0, y), (x, 0), (MAX, y), (x, MAX), never become towns
fn convo_3times(fitness: &mut Vec<Vec<u8>>) {
for _ in 0..3 {
for y in 1..fitness.len() - 1 {
for x in 1..fitness[y].len() - 1 {
let mut sum = 0 as i32;
for y in &[y - 1, y, y + 1] {
for x in &[x - 1, x, x + 1] {
sum += i32::from(fitness[*y][*x]);
}
}
fitness[y][x] = (sum / 9) as u8;
}
}
}
}
convo_3times(&mut fitness);
for y in 0..self.height {
for x in 0..self.width {
if y == 0
|| x == 0
|| y == self.height - 1
|| x == self.width - 1
|| Self::land_kind(altitudes[y][x]) != LandKind::Plain
{
fitness[y][x] = 0;
}
}
}
convo_3times(&mut fitness);
let mut min_fitness = 0;
for row in fitness.iter() {
for f in row.iter() {
if *f > min_fitness {
min_fitness = *f;
}
}
}
let min_fitness = min_fitness * 9 / 10; // * 0.9
let mut candidates = Vec::new();
for y in 1..fitness.len() - 1 {
for x in 1..fitness[y].len() - 1 {
if fitness[y][x] >= min_fitness {
candidates.push(Pos { x, y });
}
}
}
candidates.as_mut_slice().shuffle(&mut self.rng);
let mut towns = HashSet::with_capacity(self.max_towns);
for c in candidates.iter() {
if towns.len() == self.max_towns {
break;
}
if towns
.iter()
.all(|p: &Pos| p.move_cost(c) > self.town_min_cost)
{
towns.insert(*c);
}
}
towns
}
// Get shortest path of the connection using Dijkstra's algorithm
fn shortest_path<'b>(&self, conn: &Connection<'b>, altitudes: &[Vec<u8>]) -> Vec<Pos> {
#[inline]
fn land_cost(kind: LandKind) -> usize {
match kind {
LandKind::DeepSea => 64,
LandKind::Sea => 32,
LandKind::Plain => 1,
LandKind::Forest => 4,
LandKind::Mountain => 8,
LandKind::Highland => 16,
_ => unreachable!(),
}
}
#[derive(Clone)]
enum Route {
Cons(Pos, Rc<Route>),
Nil,
}
struct Vert {
cost: usize,
pos: Pos,
prev: Route,
}
// Note: Vert is ordered by cost for priority queue
impl PartialEq for Vert {
fn eq(&self, rhs: &Vert) -> bool {
self.cost == rhs.cost
}
}
impl Eq for Vert {}
impl Ord for Vert {
fn cmp(&self, rhs: &Vert) -> Ordering {
rhs.cost.cmp(&self.cost)
}
}
impl PartialOrd for Vert {
fn partial_cmp(&self, rhs: &Vert) -> Option<Ordering> {
Some(self.cmp(rhs))
}
}
// Map node => cost
let mut costs = HashMap::new();
costs.insert(*conn.from, 0);
let mut state = BinaryHeap::new();
state.push(Vert {
cost: 0,
pos: *conn.from,
prev: Route::Nil,
});
while let Some(Vert { cost, pos, prev }) = state.pop() {
if &pos == conn.to {
// Collect list as Vec<Pos>
// Note: Start node and goal node are not included since they are town
let mut verts = Vec::new();
let mut route = &prev;
while let Route::Cons(pos, ref prev) = route {
verts.push(*pos);
route = prev;
}
return verts;
}
// Note: OK to create an Rc pointer in advance since at least one point iterated in below loop is valid.
let prev = Rc::new(prev);
let Pos { x, y } = pos;
for pair in &[
(Some(x), y.checked_sub(1)),
(x.checked_sub(1), Some(y)),
(x.checked_add(1), Some(y)),
(Some(x), y.checked_add(1)),
] {
let (x, y) = match pair {
(Some(x), Some(y)) if *x < self.width && *y < self.height => (*x, *y),
_ => continue,
};
if let Route::Cons(pos, ..) = *prev {
if pos.x == x && pos.y == y {
// Going back to previous position never happens
continue;
}
}
let cost = cost + land_cost(Self::land_kind(altitudes[y][x]));
let pos = Pos { x, y };
if let Some(c) = costs.get(&pos) {
if cost >= *c {
continue;
}
}
costs.insert(pos, cost);
state.push(Vert {
cost,
pos,
prev: Route::Cons(pos, prev.clone()),
});
}
}
// Connection unreachable
Vec::new()
}
// Get all cells of paths
fn paths(&mut self, towns: &HashSet<Pos>, altitudes: &[Vec<u8>]) -> HashSet<Pos> {
towns
.iter()
.map(|town| {
let mut near_towns = towns
.iter()
.filter_map(|t| {
if t == town {
return None;
}
let cost = t.move_cost(town);
if cost > self.conn_max_cost {
return None;
}
Some((cost, t))
})
.collect::<Vec<_>>();
near_towns.sort_unstable_by_key(|(cost, _)| *cost);
let mut dirs = HashSet::new();
near_towns.into_iter().filter_map(move |(_, near)| {
// On wasm32, usize has 32bits length so converting from usize to f64 is lossless.
// For conversion between usize and f64 using `as`, clippy complains that using
// f64::from instead is safer. This is true on wasm32, but on x86_64, conversion
// between usize and f64 is not lossless so f64::from(usize) is not implemented.
#[allow(clippy::cast_lossless)]
let angle =
(town.y as f64 - near.y as f64).atan2(town.x as f64 - near.x as f64);
let dir = (angle / 45.0) as usize;
for dir in &[dir, (dir + 1) % 8, (dir + 7) % 8] {
if dirs.contains(dir) {
return None;
}
}
dirs.insert(dir);
Some(Connection {
from: &town,
to: &near,
})
})
})
.flatten()
.filter({
// Dedup connections (from-to pairs)
let mut saw = HashMap::new();
move |conn: &Connection<'_>| {
if let Some(to) = saw.get(conn.from) {
if to == &conn.to {
return false;
}
}
if let Some(from) = saw.get(conn.to) {
if from == &conn.from {
return false;
}
}
saw.insert(conn.from, conn.to);
true
}
})
.map(|conn| self.shortest_path(&conn, altitudes))
.flatten()
.collect()
}
pub fn gen(&mut self) -> Board<'static> {
let mut slope = SlopeGen::new(
self.rng,
self.width,
self.height,
self.down_rate,
self.num_tops,
);
slope.gen();
let altitudes = slope.altitudes;
let tops = slope.tops;
let towns = self.towns(&altitudes);
let paths = self.paths(&towns, &altitudes);
Board::build(self.width, self.height, |w, h| {
let alt = altitudes[h][w];
let p = Pos { x: w, y: h };
if tops.contains(&p) {
LandKind::Top.preset(alt)
} else if towns.contains(&p) {
LandKind::Town.preset(alt)
} else if paths.contains(&p) {
LandKind::Path.preset(alt)
} else {
Self::land_kind(alt).preset(alt)
}
})
}
}