author | alfadur |
Thu, 01 Nov 2018 02:55:22 +0300 | |
changeset 14052 | 9c817b2eedae |
parent 14051 | 8a0d69c16cad |
parent 14032 | 2869c2ccb1b8 |
child 14054 | 3185fb34f3b5 |
permissions | -rw-r--r-- |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
1 |
use itertools::Itertools; |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
2 |
|
14052 | 3 |
use integral_geometry::{Point, Size, Rect}; |
14026 | 4 |
use land2d::Land2D; |
5 |
use LandGenerationParameters; |
|
6 |
use LandGenerator; |
|
7 |
||
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
8 |
struct OutlinePoints { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
9 |
islands: Vec<Vec<Point>>, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
10 |
fill_points: Vec<Point>, |
14052 | 11 |
size: Size, |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
12 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
13 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
14 |
impl OutlinePoints { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
15 |
fn from_outline_template<I: Iterator<Item = u32>>( |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
16 |
outline_template: &OutlineTemplate, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
17 |
random_numbers: &mut I, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
18 |
) -> Self { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
19 |
Self { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
20 |
islands: outline_template |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
21 |
.islands |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
22 |
.iter() |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
23 |
.map(|i| { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
24 |
i.iter() |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
25 |
.zip(random_numbers.tuples()) |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
26 |
.map(|(rect, (rnd_a, rnd_b))| { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
27 |
Point::new( |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
28 |
rect.x + (rnd_a % rect.width) as i32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
29 |
rect.y + (rnd_b % rect.height) as i32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
30 |
) |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
31 |
}).collect() |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
32 |
}).collect(), |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
33 |
fill_points: outline_template.fill_points.clone(), |
14052 | 34 |
size: outline_template.size |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
35 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
36 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
37 |
|
14052 | 38 |
fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
39 |
self.islands.iter_mut() |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
40 |
.flat_map(|i| i.iter_mut()) |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
41 |
.chain(self.fill_points.iter_mut()) |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
42 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
43 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
44 |
fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
45 |
unimplemented!() |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
46 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
47 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
48 |
|
14026 | 49 |
struct OutlineTemplate { |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
50 |
islands: Vec<Vec<Rect>>, |
14026 | 51 |
fill_points: Vec<Point>, |
14032 | 52 |
size: Size, |
14026 | 53 |
can_flip: bool, |
54 |
can_invert: bool, |
|
55 |
can_mirror: bool, |
|
56 |
is_negative: bool, |
|
57 |
} |
|
58 |
||
59 |
struct TemplatedLandGenerator { |
|
60 |
outline_template: OutlineTemplate, |
|
61 |
} |
|
62 |
||
63 |
impl TemplatedLandGenerator { |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
64 |
pub fn new(outline_template: OutlineTemplate) -> Self { |
14026 | 65 |
Self { outline_template } |
66 |
} |
|
67 |
} |
|
68 |
||
69 |
impl LandGenerator for TemplatedLandGenerator { |
|
70 |
fn generate_land<T: Copy + PartialEq, I: Iterator<Item = u32>>( |
|
71 |
&self, |
|
72 |
parameters: LandGenerationParameters<T>, |
|
73 |
random_numbers: &mut I, |
|
74 |
) -> Land2D<T> { |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
75 |
let mut points = |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
76 |
OutlinePoints::from_outline_template(&self.outline_template, random_numbers); |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
77 |
|
14052 | 78 |
let mut land = Land2D::new(points.size, parameters.basic); |
14026 | 79 |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
80 |
let top_left = Point::new( |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
81 |
(land.width() - land.play_width() / 2) as i32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
82 |
(land.height() - land.play_height()) as i32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
83 |
); |
14026 | 84 |
|
14052 | 85 |
points.size = land.size(); |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
86 |
|
14052 | 87 |
points.iter_mut().for_each(|p| *p += top_left); |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
88 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
89 |
// mirror |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
90 |
if self.outline_template.can_mirror { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
91 |
if let Some(b) = random_numbers.next() { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
92 |
if b & 1 != 0 { |
14052 | 93 |
points.iter_mut().for_each(|p| p.x = land.width() as i32 - 1 - p.x); |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
94 |
} |
14026 | 95 |
} |
96 |
} |
|
97 |
||
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
98 |
// flip |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
99 |
if self.outline_template.can_flip { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
100 |
if let Some(b) = random_numbers.next() { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
101 |
if b & 1 != 0 { |
14052 | 102 |
points.iter_mut().for_each(|p| |
103 |
p.y = land.height() as i32 - 1 - p.y); |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
104 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
105 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
106 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
107 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
108 |
points.distort(random_numbers); |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
109 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
110 |
// draw_edge(points, land, parameters.zero) |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
111 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
112 |
for p in points.fill_points { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
113 |
land.fill(p, parameters.zero, parameters.zero) |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
114 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
115 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
116 |
// draw_edge(points, land, parameters.basic) |
14026 | 117 |
|
118 |
land |
|
119 |
} |
|
120 |
} |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
121 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
122 |
#[test()] |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
123 |
fn points_test() { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
124 |
let mut points = OutlinePoints { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
125 |
islands: vec![vec![]], |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
126 |
fill_points: vec![Point::new(1, 1)], |
14052 | 127 |
size: Size::square(100) |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
128 |
}; |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
129 |
|
14052 | 130 |
points.iter_mut().for_each(|p| p.x = 2); |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
131 |
assert_eq!(points.fill_points[0].x, 2); |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14026
diff
changeset
|
132 |
} |