author | alfadur |
Fri, 02 Nov 2018 02:31:01 +0300 | |
changeset 14072 | 3f21f27c6564 |
parent 14071 | 649ccb9f8cfd |
child 14074 | abb42ba345b6 |
permissions | -rw-r--r-- |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
1 |
use itertools::Itertools; |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
2 |
|
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
3 |
use integral_geometry::{Point, Rect, Size}; |
14031 | 4 |
use land2d::Land2D; |
5 |
use LandGenerationParameters; |
|
6 |
use LandGenerator; |
|
7 |
||
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
8 |
struct OutlinePoints { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
9 |
islands: Vec<Vec<Point>>, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
10 |
fill_points: Vec<Point>, |
14057 | 11 |
size: Size, |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
12 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
13 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
14 |
impl OutlinePoints { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
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:
14031
diff
changeset
|
16 |
outline_template: &OutlineTemplate, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
17 |
random_numbers: &mut I, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
18 |
) -> Self { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
19 |
Self { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
20 |
islands: outline_template |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
21 |
.islands |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
22 |
.iter() |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
23 |
.map(|i| { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
24 |
i.iter() |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
25 |
.zip(random_numbers.tuples()) |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
26 |
.map(|(rect, (rnd_a, rnd_b))| { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
27 |
Point::new( |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
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:
14031
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:
14031
diff
changeset
|
30 |
) |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
31 |
}).collect() |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
32 |
}).collect(), |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
33 |
fill_points: outline_template.fill_points.clone(), |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
34 |
size: outline_template.size, |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
35 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
36 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
37 |
|
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
38 |
fn total_len(&self) -> usize { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
39 |
self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len() |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
40 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
41 |
|
14057 | 42 |
fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
43 |
self.islands |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
44 |
.iter_mut() |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
45 |
.flat_map(|i| i.iter_mut()) |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
46 |
.chain(self.fill_points.iter_mut()) |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
47 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
48 |
|
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
49 |
fn divide_edge<I: Iterator<Item = u32>>( |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
50 |
&self, |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
51 |
start_point: Point, |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
52 |
end_point: Point, |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
53 |
random_numbers: &mut I, |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
54 |
) -> Option<Point> { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
55 |
None |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
56 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
57 |
|
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
58 |
fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
59 |
for is in 0..self.islands.len() { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
60 |
let mut i = 0; |
14072 | 61 |
let mut start_point = Point::zero(); |
62 |
let mut end_point = Point::zero(); |
|
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
63 |
|
14072 | 64 |
loop { |
65 |
{ |
|
66 |
let island = &self.islands[is]; |
|
67 |
if i < island.len() { |
|
68 |
start_point = island[i]; |
|
69 |
end_point = if i + 1 < island.len() { |
|
70 |
island[i + 1] |
|
71 |
} else { |
|
72 |
island[0] |
|
73 |
}; |
|
74 |
} else { |
|
75 |
break |
|
76 |
} |
|
77 |
} |
|
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
78 |
|
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
79 |
if let Some(new_point) = self.divide_edge(start_point, end_point, random_numbers) { |
14072 | 80 |
self.islands[is].insert(i + 1, new_point); |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
81 |
i += 2; |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
82 |
} else { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
83 |
i += 1; |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
84 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
85 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
86 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
87 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
88 |
|
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
89 |
fn bezierize(&mut self) { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
90 |
unimplemented!() |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
91 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
92 |
|
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
93 |
fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) { |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
94 |
loop { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
95 |
let old_len = self.total_len(); |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
96 |
self.divide_edges(random_numbers); |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
97 |
|
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
98 |
if self.total_len() != old_len { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
99 |
break; |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
100 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
101 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
102 |
|
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
103 |
self.bezierize(); |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
104 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
105 |
|
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
106 |
fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
107 |
for island in &self.islands { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
108 |
if island.len() > 1 { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
109 |
for i in 0..island.len() - 1 { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
110 |
land.draw_line(island[i], island[i + 1], value); |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
111 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
112 |
land.draw_line(island[island.len() - 1], island[0], value); |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
113 |
} |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
114 |
} |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
115 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
116 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
117 |
|
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
118 |
pub struct OutlineTemplate { |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
119 |
islands: Vec<Vec<Rect>>, |
14031 | 120 |
fill_points: Vec<Point>, |
14037 | 121 |
size: Size, |
14031 | 122 |
can_flip: bool, |
123 |
can_invert: bool, |
|
124 |
can_mirror: bool, |
|
125 |
is_negative: bool, |
|
126 |
} |
|
127 |
||
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
128 |
impl OutlineTemplate { |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
129 |
pub fn new(size: Size) -> Self { |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
130 |
OutlineTemplate { |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
131 |
size, |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
132 |
islands: Vec::new(), |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
133 |
fill_points: Vec::new(), |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
134 |
can_flip: false, |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
135 |
can_invert: false, |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
136 |
can_mirror: false, |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
137 |
is_negative: false, |
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
138 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
139 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
140 |
|
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
141 |
pub fn flippable(self) -> Self { |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
142 |
Self { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
143 |
can_flip: true, |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
144 |
..self |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
145 |
} |
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
146 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
147 |
|
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
148 |
pub fn mirrorable(self) -> Self { |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
149 |
Self { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
150 |
can_mirror: true, |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
151 |
..self |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
152 |
} |
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
153 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
154 |
|
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
155 |
pub fn invertable(self) -> Self { |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
156 |
Self { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
157 |
can_invert: true, |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
158 |
..self |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
159 |
} |
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
160 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
161 |
|
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
162 |
pub fn negative(self) -> Self { |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
163 |
Self { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
164 |
is_negative: true, |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
165 |
..self |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
166 |
} |
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
167 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
168 |
|
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
169 |
pub fn with_fill_points(self, fill_points: Vec<Point>) -> Self { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
170 |
Self { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
171 |
fill_points, |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
172 |
..self |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
173 |
} |
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
174 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
175 |
|
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
176 |
pub fn with_islands(self, islands: Vec<Vec<Rect>>) -> Self { |
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
177 |
Self { islands, ..self } |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
178 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
179 |
|
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
180 |
pub fn add_fill_points(mut self, points: &[Point]) -> Self { |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
181 |
self.fill_points.extend_from_slice(points); |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
182 |
self |
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
183 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
184 |
|
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
185 |
pub fn add_island(mut self, island: &[Rect]) -> Self { |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
186 |
self.islands.push(island.into()); |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
187 |
self |
14059
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
188 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
189 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
190 |
|
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14057
diff
changeset
|
191 |
pub struct TemplatedLandGenerator { |
14031 | 192 |
outline_template: OutlineTemplate, |
193 |
} |
|
194 |
||
195 |
impl TemplatedLandGenerator { |
|
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
196 |
pub fn new(outline_template: OutlineTemplate) -> Self { |
14031 | 197 |
Self { outline_template } |
198 |
} |
|
199 |
} |
|
200 |
||
201 |
impl LandGenerator for TemplatedLandGenerator { |
|
202 |
fn generate_land<T: Copy + PartialEq, I: Iterator<Item = u32>>( |
|
203 |
&self, |
|
204 |
parameters: LandGenerationParameters<T>, |
|
205 |
random_numbers: &mut I, |
|
206 |
) -> Land2D<T> { |
|
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
207 |
let mut points = |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
208 |
OutlinePoints::from_outline_template(&self.outline_template, random_numbers); |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
209 |
|
14057 | 210 |
let mut land = Land2D::new(points.size, parameters.basic); |
14031 | 211 |
|
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
212 |
let top_left = Point::new( |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
213 |
(land.width() - land.play_width() / 2) as i32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
214 |
(land.height() - land.play_height()) as i32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
215 |
); |
14031 | 216 |
|
14057 | 217 |
points.size = land.size(); |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
218 |
|
14057 | 219 |
points.iter_mut().for_each(|p| *p += top_left); |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
220 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
221 |
// mirror |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
222 |
if self.outline_template.can_mirror { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
223 |
if let Some(b) = random_numbers.next() { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
224 |
if b & 1 != 0 { |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
225 |
points |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
226 |
.iter_mut() |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
227 |
.for_each(|p| p.x = land.width() as i32 - 1 - p.x); |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
228 |
} |
14031 | 229 |
} |
230 |
} |
|
231 |
||
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
232 |
// flip |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
233 |
if self.outline_template.can_flip { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
234 |
if let Some(b) = random_numbers.next() { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
235 |
if b & 1 != 0 { |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
236 |
points |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
237 |
.iter_mut() |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
238 |
.for_each(|p| p.y = land.height() as i32 - 1 - p.y); |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
239 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
240 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
241 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
242 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
243 |
points.distort(random_numbers); |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
244 |
|
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
245 |
points.draw(&mut land, parameters.zero); |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
246 |
|
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
247 |
for p in &points.fill_points { |
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
248 |
land.fill(*p, parameters.zero, parameters.zero) |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
249 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
250 |
|
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
251 |
points.draw(&mut land, parameters.basic); |
14031 | 252 |
|
253 |
land |
|
254 |
} |
|
255 |
} |
|
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
256 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
257 |
#[test()] |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
258 |
fn points_test() { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
259 |
let mut points = OutlinePoints { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
260 |
islands: vec![vec![]], |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
261 |
fill_points: vec![Point::new(1, 1)], |
14071
649ccb9f8cfd
Commit broken code for divide_edges in hope for salvation
unc0rr
parents:
14059
diff
changeset
|
262 |
size: Size::square(100), |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
263 |
}; |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
264 |
|
14057 | 265 |
points.iter_mut().for_each(|p| p.x = 2); |
14056
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
266 |
assert_eq!(points.fill_points[0].x, 2); |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
14031
diff
changeset
|
267 |
} |