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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
use std::cmp::Reverse;
use std::collections::HashSet;
use enum_map::EnumMap;
use rand::prelude::SliceRandom;
use rand::{Rng, seq::IndexedRandom};
use crate::map_parameters::MapParameters;
use crate::{
grid::Grid,
ruleset::{Ruleset, unique::Unique},
tile::Tile,
tile_component::*,
tile_map::{Layer, TileMap},
};
impl TileMap {
/// Generate natural wonders on the map.
///
/// This function is like to Civ6's natural wonder generation. We edit it to fit our game which is like Civ5.
pub fn place_natural_wonders(&mut self, map_parameters: &MapParameters, ruleset: &Ruleset) {
let grid = self.world_grid.grid;
// Get the number of natural wonders to place based on the world size
let num_natural_wonders = map_parameters.world_size_type_profile.num_natural_wonders;
// Collect the natural wonders and their possible tile locations
let mut natural_wonder_and_tile_list: EnumMap<NaturalWonder, Vec<Tile>> =
EnumMap::default();
let mut land_area_id_and_size: Vec<_> = self
.area_list
.iter()
.filter(|area| !area.is_water)
.map(|area| (area.id, area.size))
.collect();
// Sort by `area_size` in descending order
land_area_id_and_size.sort_by_key(|&(_, area_size)| (Reverse(area_size)));
/***** Tackle with the natural wonders which require 2 adjacent tiles *****/
// When a natural wonder requires occupying 2 adjacent tiles,
// we choose the current tile and one of its randomly selected adjacent tiles
// as the location for placing the wonder.
// This direction is the chosen adjacent tile's direction relative to the current tile.
//
// Notice: Now it is only used for `Great Barrier Reef`,
// in original game, neighbor_tile_direction is not randomly selected.
// it is always Direction::SouthEast.
let neighbor_tile_direction = *grid
.edge_direction_array()
.choose(&mut self.random_number_generator)
.expect("Failed to choose a random direction");
for tile in self.all_tiles() {
// If tile is a civilization start location, or a city state start location, or has natural wonder, then it cannot be chosen as the location for placing natural wonder.
if self.starting_tile_and_civilization.contains_key(&tile)
|| self.starting_tile_and_city_state.contains_key(&tile)
|| tile.natural_wonder(self).is_some()
{
continue;
}
for (natural_wonder, tile_list) in natural_wonder_and_tile_list.iter_mut() {
let natural_wonder_info = &ruleset.natural_wonders[natural_wonder.as_str()];
match natural_wonder {
NaturalWonder::GreatBarrierReef => {
if let Some(neighbor_tile) =
tile.neighbor_tile(neighbor_tile_direction, grid)
{
let mut all_neigbor_tiles = HashSet::new();
all_neigbor_tiles.extend(tile.neighbor_tiles(grid));
all_neigbor_tiles.extend(neighbor_tile.neighbor_tiles(grid));
// We only check neighbors of the current tile and the neighbor tile.
// So we remove them from the set of all neighbor tiles.
all_neigbor_tiles.remove(&tile);
all_neigbor_tiles.remove(&neighbor_tile);
// The tile should meet the following conditions:
// 1. All neighboring tiles exist
// 2. All neighboring tiles are water and not lake, not ice
// 3. At least 4 neighboring tiles are coast
if all_neigbor_tiles.len() == 8
&& all_neigbor_tiles.iter().all(|&tile| {
tile.terrain_type(self) == TerrainType::Water
&& tile.base_terrain(self) != BaseTerrain::Lake
&& tile.feature(self) != Some(Feature::Ice)
})
&& all_neigbor_tiles
.iter()
.filter(|tile| tile.base_terrain(self) == BaseTerrain::Coast)
.count()
>= 4
{
tile_list.push(tile);
}
}
}
_ => {
if tile.is_freshwater(self) != natural_wonder_info.is_fresh_water {
continue;
};
if !natural_wonder_info
.occurs_on_type
.contains(&tile.terrain_type(self))
|| !natural_wonder_info
.occurs_on_base
.contains(&tile.base_terrain(self))
{
continue;
}
let check_unique_conditions =
natural_wonder_info.uniques.iter().all(|unique| {
let unique = Unique::new(unique);
match unique.placeholder_text.as_str() {
"Must be adjacent to [] [] tiles" => {
let count = tile
.neighbor_tiles(grid)
.filter(|tile| {
self.matches_wonder_filter(
*tile,
unique.params[1].as_str(),
)
})
.count();
count == unique.params[0].parse::<usize>().unwrap()
}
"Must be adjacent to [] to [] [] tiles" => {
let count = tile
.neighbor_tiles(grid)
.filter(|tile| {
self.matches_wonder_filter(
*tile,
unique.params[2].as_str(),
)
})
.count();
count >= unique.params[0].parse::<usize>().unwrap()
&& count <= unique.params[1].parse::<usize>().unwrap()
}
"Must not be on [] largest landmasses" => {
// index is the ranking of the current landmass among all landmasses sorted by size from highest to lowest.
let index = unique.params[0].parse::<usize>().unwrap();
// Check if the tile isn't on the landmass with the given index
land_area_id_and_size
.get(index)
.is_none_or(|&(id, _)| id != tile.area_id(self))
}
"Must be on [] largest landmasses" => {
// index is the ranking of the current landmass among all landmasses sorted by size from highest to lowest.
let index = unique.params[0].parse::<usize>().unwrap();
// Check if the tile is on the landmass with the given index
land_area_id_and_size
.get(index)
.is_some_and(|&(id, _)| id == tile.area_id(self))
}
_ => true,
}
});
if check_unique_conditions {
tile_list.push(tile);
}
}
}
}
}
// Collect the natural wonders that can be placed
let mut selected_natural_wonder_list: Vec<_> = natural_wonder_and_tile_list
.iter()
.filter(|(_, tiles)| !tiles.is_empty())
.map(|(natural_wonder, _)| natural_wonder)
.collect();
// Sort the natural wonders by the number of tiles they can be placed
// In CIV5, the natural wonders with lesser number of tiles will be placed first.
selected_natural_wonder_list
.sort_by_key(|&natural_wonder| natural_wonder_and_tile_list[natural_wonder].len());
// Store current how many natural wonders have been placed
let mut num_placed_natural_wonders = 0;
// Store the tile where the natural wonder has been placed
let mut placed_natural_wonder_tiles: Vec<Tile> = Vec::new();
// start to place wonder
selected_natural_wonder_list
.into_iter()
.for_each(|natural_wonder| {
if num_placed_natural_wonders < num_natural_wonders {
let tile_list = &mut natural_wonder_and_tile_list[natural_wonder];
tile_list.shuffle(&mut self.random_number_generator);
for &tile in tile_list.iter() {
if self.layer_data[Layer::NaturalWonder][tile.index()] == 0 {
let natural_wonder_info =
&ruleset.natural_wonders[natural_wonder.as_str()];
// At first, we should remove feature from the tile
tile.clear_feature(self);
match natural_wonder {
NaturalWonder::GreatBarrierReef => {
// The neighbor tile absolutely exists because we have checked it before.
let neighbor_tile = tile
.neighbor_tile(neighbor_tile_direction, grid)
.expect("Neighbor tile does not exist");
// All related tiles should contain:
// 1. Current tile
// 2. Neighbor tile according to neighbor_tile_direction
// 3. All neighbor tiles of current tile and neighbor tile
let mut all_related_tiles = HashSet::new();
all_related_tiles.extend(tile.neighbor_tiles(grid));
all_related_tiles.extend(neighbor_tile.neighbor_tiles(grid));
all_related_tiles.into_iter().for_each(|tile| {
tile.set_terrain_type(self, TerrainType::Water);
tile.set_base_terrain(self, BaseTerrain::Coast);
});
// place the natural wonder on the candidate position and its adjacent tile
tile.set_natural_wonder(self, natural_wonder);
neighbor_tile.set_natural_wonder(self, natural_wonder);
// add the position of the placed natural wonder to the list of placed natural wonder positions
placed_natural_wonder_tiles.push(tile);
placed_natural_wonder_tiles.push(neighbor_tile);
}
NaturalWonder::RockOfGibraltar => {
tile.neighbor_tiles(grid).for_each(|neighbor_tile| {
if neighbor_tile.terrain_type(self) == TerrainType::Water {
neighbor_tile
.set_base_terrain(self, BaseTerrain::Coast);
} else {
neighbor_tile
.set_terrain_type(self, TerrainType::Mountain);
}
});
// Edit the choice tile's terrain_type to match the natural wonder
tile.set_terrain_type(self, TerrainType::Flatland);
// Edit the choice tile's base_terrain to match the natural wonder
tile.set_base_terrain(self, BaseTerrain::Grassland);
// place the natural wonder on the candidate position
tile.set_natural_wonder(self, natural_wonder);
// add the position of the placed natural wonder to the list of placed natural wonder positions
placed_natural_wonder_tiles.push(tile);
}
_ => {
// Edit the choice tile's terrain_type to match the natural wonder
if let Some(turn_into_terrain_type) =
natural_wonder_info.turns_into_type
{
tile.set_terrain_type(self, turn_into_terrain_type);
};
// Edit the choice tile's base_terrain to match the natural wonder
if let Some(turn_into_base_terrain) =
natural_wonder_info.turns_into_base
{
tile.set_base_terrain(self, turn_into_base_terrain);
}
// place the natural wonder on the candidate position
tile.set_natural_wonder(self, natural_wonder);
// add the position of the placed natural wonder to the list of placed natural wonder positions
placed_natural_wonder_tiles.push(tile);
}
}
self.place_impact_and_ripples(tile, Layer::NaturalWonder, u32::MAX);
num_placed_natural_wonders += 1;
break;
}
}
}
});
#[cfg(debug_assertions)]
if num_placed_natural_wonders < num_natural_wonders {
eprintln!(
"Could only place {} out of {} natural wonders on the map. Not enough valid locations for all natural wonders or not enough natural wonders available.",
num_placed_natural_wonders, num_natural_wonders
);
}
// If the natural wonder is not a lake, and it has water neighbors, then change the water neighbor tiles to lake or coast.
placed_natural_wonder_tiles.iter().for_each(|&tile| {
if tile.terrain_type(self) != TerrainType::Water
&& tile
.neighbor_tiles(grid)
.any(|neighbor_tile| neighbor_tile.terrain_type(self) == TerrainType::Water)
{
let water_neighbor_tiles: Vec<_> = tile
.neighbor_tiles(grid)
.filter(|&neighbor_tile| neighbor_tile.terrain_type(self) == TerrainType::Water)
.collect();
water_neighbor_tiles
.iter()
.for_each(|&water_neighbor_tile| {
// If the water neighbor tile has a lake neighbor, then change the water neighbor tile to a lake.
// Otherwise, change the water neighbor tile to a coast.
let has_lake_neighbor = water_neighbor_tile.neighbor_tiles(grid).any(
|neighbor_neighbor_tile| {
neighbor_neighbor_tile.base_terrain(self) == BaseTerrain::Lake
},
);
water_neighbor_tile.set_base_terrain(
self,
if has_lake_neighbor {
BaseTerrain::Lake
} else {
BaseTerrain::Coast
},
);
});
}
});
}
/// Generate natural wonders on the map.
///
/// This function is likely to Civ6's natural wonder generation. So we don't use this function for the current game which is more like Civ5.
pub fn generate_natural_wonders(&mut self, map_parameters: &MapParameters, ruleset: &Ruleset) {
let grid = self.world_grid.grid;
// Get the number of natural wonders to place based on the world size
let num_natural_wonders = map_parameters.world_size_type_profile.num_natural_wonders;
// Collect the natural wonders and their possible tile locations
let mut natural_wonder_and_tile_list: EnumMap<NaturalWonder, Vec<Tile>> =
EnumMap::default();
let mut land_area_id_and_size: Vec<_> = self
.area_list
.iter()
.filter(|area| !area.is_water)
.map(|area| (area.id, area.size))
.collect();
// Sort by `area_size` in descending order
land_area_id_and_size.sort_by_key(|&(_, area_size)| (Reverse(area_size)));
/***** Tackle with the natural wonders which require 2 adjacent tiles *****/
// When a natural wonder requires occupying 2 adjacent tiles,
// we choose the current tile and one of its randomly selected adjacent tiles
// as the location for placing the wonder.
// This direction is the chosen adjacent tile's direction relative to the current tile.
//
// Notice: Now it is only used for `Great Barrier Reef`,
// in original game, neighbor_tile_direction is not randomly selected.
// it is always Direction::SouthEast.
let neighbor_tile_direction = *grid
.edge_direction_array()
.choose(&mut self.random_number_generator)
.expect("Failed to choose a random direction");
for tile in self.all_tiles() {
// If tile is a civilization start location, or a city state start location, or has natural wonder, then it cannot be chosen as the location for placing natural wonder.
if self.starting_tile_and_civilization.contains_key(&tile)
|| self.starting_tile_and_city_state.contains_key(&tile)
|| tile.natural_wonder(self).is_some()
{
continue;
}
for (natural_wonder, tile_list) in natural_wonder_and_tile_list.iter_mut() {
let natural_wonder_info = &ruleset.natural_wonders[natural_wonder.as_str()];
match natural_wonder {
NaturalWonder::GreatBarrierReef => {
if let Some(neighbor_tile) =
tile.neighbor_tile(neighbor_tile_direction, grid)
{
let mut all_neigbor_tiles = HashSet::new();
all_neigbor_tiles.extend(tile.neighbor_tiles(grid));
all_neigbor_tiles.extend(neighbor_tile.neighbor_tiles(grid));
// We only check neighbors of the current tile and the neighbor tile.
// So we remove them from the set of all neighbor tiles.
all_neigbor_tiles.remove(&tile);
all_neigbor_tiles.remove(&neighbor_tile);
// The tile should meet the following conditions:
// 1. All neighboring tiles exist
// 2. All neighboring tiles are water and not lake, not ice
// 3. At least 4 neighboring tiles are coast
if all_neigbor_tiles.len() == 8
&& all_neigbor_tiles.iter().all(|&tile| {
tile.terrain_type(self) == TerrainType::Water
&& tile.base_terrain(self) != BaseTerrain::Lake
&& tile.feature(self) != Some(Feature::Ice)
})
&& all_neigbor_tiles
.iter()
.filter(|tile| tile.base_terrain(self) == BaseTerrain::Coast)
.count()
>= 4
{
tile_list.push(tile);
}
}
}
_ => {
if tile.is_freshwater(self) != natural_wonder_info.is_fresh_water {
continue;
};
if !natural_wonder_info
.occurs_on_type
.contains(&tile.terrain_type(self))
|| !natural_wonder_info
.occurs_on_base
.contains(&tile.base_terrain(self))
{
continue;
}
let check_unique_conditions =
natural_wonder_info.uniques.iter().all(|unique| {
let unique = Unique::new(unique);
match unique.placeholder_text.as_str() {
"Must be adjacent to [] [] tiles" => {
let count = tile
.neighbor_tiles(grid)
.filter(|tile| {
self.matches_wonder_filter(
*tile,
unique.params[1].as_str(),
)
})
.count();
count == unique.params[0].parse::<usize>().unwrap()
}
"Must be adjacent to [] to [] [] tiles" => {
let count = tile
.neighbor_tiles(grid)
.filter(|tile| {
self.matches_wonder_filter(
*tile,
unique.params[2].as_str(),
)
})
.count();
count >= unique.params[0].parse::<usize>().unwrap()
&& count <= unique.params[1].parse::<usize>().unwrap()
}
"Must not be on [] largest landmasses" => {
// index is the ranking of the current landmass among all landmasses sorted by size from highest to lowest.
let index = unique.params[0].parse::<usize>().unwrap();
// Check if the tile isn't on the landmass with the given index
land_area_id_and_size
.get(index)
.is_none_or(|&(id, _)| id != tile.area_id(self))
}
"Must be on [] largest landmasses" => {
// index is the ranking of the current landmass among all landmasses sorted by size from highest to lowest.
let index = unique.params[0].parse::<usize>().unwrap();
// Check if the tile is on the landmass with the given index
land_area_id_and_size
.get(index)
.is_some_and(|&(id, _)| id == tile.area_id(self))
}
_ => true,
}
});
if check_unique_conditions {
tile_list.push(tile);
}
}
}
}
}
// Get the natural wonders that can be placed
let mut selected_natural_wonder_list: Vec<_> = natural_wonder_and_tile_list
.iter()
.filter(|(_, tile_list)| !tile_list.is_empty())
.map(|(natural_wonder, _)| natural_wonder)
.collect();
// Shuffle the list that we can choose natural wonder randomly
// NOTICE: It is different from CIV5.
selected_natural_wonder_list.shuffle(&mut self.random_number_generator);
// Store current how many natural wonders have been placed
let mut num_placed_natural_wonders = 0;
// Store the tile where the natural wonder has been placed
let mut placed_natural_wonder_tiles: Vec<Tile> = Vec::new();
// start to place wonder
selected_natural_wonder_list
.into_iter()
.for_each(|natural_wonder| {
if num_placed_natural_wonders < num_natural_wonders {
// For every natural wonder, give a score to the position where the natural wonder can place.
// The score is related to the min value of the distance from the position to all the placed natural wonders
// If no natural wonder has placed, we choose the random place where the current natural wonder can place for the current natural wonder
// The score method start
let tile_list = &natural_wonder_and_tile_list[natural_wonder];
let tile_list_and_scores = tile_list
.iter()
.map(|&tile_x| {
let closest_natural_wonder_dist = placed_natural_wonder_tiles
.iter()
.map(|tile_y| grid.distance_to(tile_x.to_cell(), tile_y.to_cell()))
.min()
.unwrap_or(1000000);
let score = if closest_natural_wonder_dist <= 10 {
100 * closest_natural_wonder_dist
} else {
1000 + (closest_natural_wonder_dist - 10)
} + self.random_number_generator.random_range(0..100);
(tile_x, score)
})
.collect::<Vec<(Tile, i32)>>();
// The score method end
// choose the max score position as the candidate position for the current natural wonder
let max_score_tile = tile_list_and_scores
.into_iter()
.max_by_key(|&(_, score)| score)
.map(|(tile, _)| tile)
.unwrap();
if !placed_natural_wonder_tiles.contains(&max_score_tile) {
let natural_wonder_info = &ruleset.natural_wonders[natural_wonder.as_str()];
// At first, we should remove feature from the tile
max_score_tile.clear_feature(self);
match natural_wonder {
NaturalWonder::GreatBarrierReef => {
// The neighbor tile absolutely exists because we have checked it before.
let neighbor_tile = max_score_tile
.neighbor_tile(neighbor_tile_direction, grid)
.expect("Neighbor tile does not exist");
// All related tiles should contain:
// 1. Current tile (`max_score_tile``)
// 2. Neighbor tile according to `neighbor_tile_direction`
// 3. All neighbor tiles of current tile and neighbor tile
let mut all_related_tiles = HashSet::new();
all_related_tiles.extend(max_score_tile.neighbor_tiles(grid));
all_related_tiles.extend(neighbor_tile.neighbor_tiles(grid));
all_related_tiles.into_iter().for_each(|tile| {
tile.set_terrain_type(self, TerrainType::Water);
tile.set_base_terrain(self, BaseTerrain::Coast);
});
// place the natural wonder on the candidate position and its adjacent tile
max_score_tile.set_natural_wonder(self, natural_wonder);
neighbor_tile.set_natural_wonder(self, natural_wonder);
// add the position of the placed natural wonder to the list of placed natural wonder positions
placed_natural_wonder_tiles.push(max_score_tile);
placed_natural_wonder_tiles.push(neighbor_tile);
}
NaturalWonder::RockOfGibraltar => {
max_score_tile
.neighbor_tiles(grid)
.for_each(|neighbor_tile| {
if neighbor_tile.terrain_type(self) == TerrainType::Water {
neighbor_tile
.set_base_terrain(self, BaseTerrain::Coast);
} else {
neighbor_tile
.set_terrain_type(self, TerrainType::Mountain);
}
});
// Edit the choice tile's terrain_type to match the natural wonder
max_score_tile.set_terrain_type(self, TerrainType::Flatland);
// Edit the choice tile's base_terrain to match the natural wonder
max_score_tile.set_base_terrain(self, BaseTerrain::Grassland);
// place the natural wonder on the candidate position
max_score_tile.set_natural_wonder(self, natural_wonder);
// add the position of the placed natural wonder to the list of placed natural wonder positions
placed_natural_wonder_tiles.push(max_score_tile);
}
_ => {
// Edit the choice tile's terrain_type to match the natural wonder
if let Some(turn_into_terrain_type) =
natural_wonder_info.turns_into_type
{
max_score_tile.set_terrain_type(self, turn_into_terrain_type);
};
// Edit the choice tile's base_terrain to match the natural wonder
if let Some(turn_into_base_terrain) =
natural_wonder_info.turns_into_base
{
max_score_tile.set_base_terrain(self, turn_into_base_terrain);
}
// place the natural wonder on the candidate position
max_score_tile.set_natural_wonder(self, natural_wonder);
// add the position of the placed natural wonder to the list of placed natural wonder positions
placed_natural_wonder_tiles.push(max_score_tile);
}
}
num_placed_natural_wonders += 1;
}
}
});
#[cfg(debug_assertions)]
if num_placed_natural_wonders < num_natural_wonders {
eprintln!(
"Could only place {} out of {} natural wonders on the map. Not enough valid locations for all natural wonders or not enough natural wonders available.",
num_placed_natural_wonders, num_natural_wonders
);
}
// If the natural wonder is not a lake, and it has water neighbors, then change the water neighbor tiles to lake or coast.
placed_natural_wonder_tiles.iter().for_each(|&tile| {
if tile.terrain_type(self) != TerrainType::Water
&& tile
.neighbor_tiles(grid)
.any(|neighbor_tile| neighbor_tile.terrain_type(self) == TerrainType::Water)
{
let water_neighbor_tiles: Vec<_> = tile
.neighbor_tiles(grid)
.filter(|&neighbor_tile| neighbor_tile.terrain_type(self) == TerrainType::Water)
.collect();
water_neighbor_tiles
.iter()
.for_each(|&water_neighbor_tile| {
// If the water neighbor tile has a lake neighbor, then change the water neighbor tile to a lake.
// Otherwise, change the water neighbor tile to a coast.
let has_lake_neighbor = water_neighbor_tile.neighbor_tiles(grid).any(
|neighbor_neighbor_tile| {
neighbor_neighbor_tile.base_terrain(self) == BaseTerrain::Lake
},
);
water_neighbor_tile.set_base_terrain(
self,
if has_lake_neighbor {
BaseTerrain::Lake
} else {
BaseTerrain::Coast
},
);
});
}
});
}
fn matches_wonder_filter(&self, tile: Tile, filter: &str) -> bool {
let terrain_type = tile.terrain_type(self);
let base_terrain = tile.base_terrain(self);
let feature = tile.feature(self);
match filter {
"Elevated" => matches!(terrain_type, TerrainType::Mountain | TerrainType::Hill),
"Land" => terrain_type != TerrainType::Water,
_ => {
terrain_type.as_str() == filter
|| base_terrain.as_str() == filter
|| feature.is_some_and(|f| f.as_str() == filter)
}
}
}
}