author | unC0Rr |
Wed, 15 Feb 2023 12:36:24 +0100 | |
branch | transitional_engine |
changeset 15925 | b0e8cc72bfef |
parent 15924 | 9502611bffc1 |
child 16024 | 3402b2185698 |
permissions | -rw-r--r-- |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
1 |
mod template; |
14151 | 2 |
pub mod theme; |
3 |
||
14710 | 4 |
use self::theme::Theme; |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
5 |
use crate::template::outline::TemplateCollectionDesc as OutlineTemplateCollectionDesc; |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
6 |
use crate::template::wavefront_collapse::TemplateCollectionDesc as WfcTemplateCollectionDesc; |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
7 |
|
14710 | 8 |
use land2d::Land2D; |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
9 |
use landgen::{ |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
10 |
outline_template_based::{ |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
11 |
outline_template::OutlineTemplate, template_based::TemplatedLandGenerator, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
12 |
}, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
13 |
wavefront_collapse::generator::{ |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
14 |
TemplateDescription as WfcTemplate, WavefrontCollapseLandGenerator, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
15 |
}, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
16 |
LandGenerationParameters, LandGenerator, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
17 |
}; |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
18 |
use rand::{seq::SliceRandom, Rng}; |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
19 |
|
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
20 |
use std::{borrow::Borrow, collections::hash_map::HashMap}; |
14160 | 21 |
use vec2d::Vec2D; |
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
22 |
|
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
23 |
#[derive(PartialEq, Eq, Hash, Clone, Debug)] |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
24 |
struct TemplateType(String); |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
25 |
|
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
26 |
impl Borrow<str> for TemplateType { |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
27 |
fn borrow(&self) -> &str { |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
28 |
self.0.as_str() |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
29 |
} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
30 |
} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
31 |
|
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
32 |
#[derive(Debug)] |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
33 |
pub struct MapGenerator<T> { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
34 |
pub(crate) templates: HashMap<TemplateType, Vec<T>>, |
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
35 |
} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
36 |
|
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
37 |
impl<T> MapGenerator<T> { |
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
38 |
pub fn new() -> Self { |
14710 | 39 |
Self { |
40 |
templates: HashMap::new(), |
|
41 |
} |
|
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
42 |
} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
43 |
|
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
44 |
pub fn get_template<R: Rng>(&self, template_type: &str, rng: &mut R) -> Option<&T> { |
14710 | 45 |
self.templates |
46 |
.get(template_type) |
|
15903
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
47 |
.and_then(|t| t.as_slice().choose(rng)) |
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
48 |
} |
14151 | 49 |
|
15903
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
50 |
pub fn make_texture<LandT>( |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
51 |
&self, |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
52 |
land: &Land2D<LandT>, |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
53 |
parameters: &LandGenerationParameters<LandT>, |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
54 |
theme: &Theme, |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
55 |
) -> Vec2D<u32> |
14710 | 56 |
where |
57 |
LandT: Copy + Default + PartialEq, |
|
58 |
{ |
|
15914 | 59 |
let mut texture = Vec2D::new(&land.size().size(), 0); |
14170 | 60 |
|
14160 | 61 |
if let Some(land_sprite) = theme.land_texture() { |
14710 | 62 |
for (row_index, (land_row, tex_row)) in land.rows().zip(texture.rows_mut()).enumerate() |
14160 | 63 |
{ |
64 |
let sprite_row = land_sprite.get_row(row_index % land_sprite.height()); |
|
65 |
let mut x_offset = 0; |
|
66 |
while sprite_row.len() < land.width() - x_offset { |
|
67 |
let copy_range = x_offset..x_offset + sprite_row.len(); |
|
68 |
tex_row_copy( |
|
15903
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
69 |
parameters.basic(), |
14160 | 70 |
&land_row[copy_range.clone()], |
71 |
&mut tex_row[copy_range], |
|
14710 | 72 |
sprite_row, |
14160 | 73 |
); |
74 |
||
75 |
x_offset += land_sprite.width() |
|
76 |
} |
|
14151 | 77 |
|
14160 | 78 |
if x_offset < land.width() { |
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
79 |
let final_range = x_offset..land.width(); |
14160 | 80 |
tex_row_copy( |
15903
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
81 |
parameters.basic(), |
14160 | 82 |
&land_row[final_range.clone()], |
83 |
&mut tex_row[final_range], |
|
14710 | 84 |
&sprite_row[..land.width() - x_offset], |
14160 | 85 |
); |
86 |
} |
|
87 |
} |
|
88 |
} |
|
14170 | 89 |
|
90 |
if let Some(border_sprite) = theme.border_texture() { |
|
91 |
assert!(border_sprite.height() <= 512); |
|
92 |
let border_width = (border_sprite.height() / 2) as u8; |
|
14175 | 93 |
let border_sprite = border_sprite.to_tiled(); |
14170 | 94 |
|
95 |
let mut offsets = vec![255u8; land.width()]; |
|
96 |
||
97 |
land_border_pass( |
|
15903
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
98 |
parameters.basic(), |
14170 | 99 |
land.rows().rev().zip(texture.rows_mut().rev()), |
100 |
&mut offsets, |
|
101 |
border_width, |
|
14710 | 102 |
|x, y| { |
103 |
border_sprite |
|
104 |
.get_pixel(x % border_sprite.width(), border_sprite.height() - 1 - y) |
|
105 |
}, |
|
14170 | 106 |
); |
107 |
||
108 |
offsets.iter_mut().for_each(|v| *v = 255); |
|
109 |
||
110 |
land_border_pass( |
|
15903
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
111 |
parameters.basic(), |
14170 | 112 |
land.rows().zip(texture.rows_mut()), |
113 |
&mut offsets, |
|
114 |
border_width, |
|
14710 | 115 |
|x, y| border_sprite.get_pixel(x % border_sprite.width(), y), |
14702 | 116 |
); |
117 |
} |
|
118 |
||
119 |
texture |
|
120 |
} |
|
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
121 |
} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
122 |
|
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
123 |
impl MapGenerator<OutlineTemplate> { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
124 |
pub fn import_yaml_templates(&mut self, text: &str) { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
125 |
let mut desc: OutlineTemplateCollectionDesc = serde_yaml::from_str(text).unwrap(); |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
126 |
let templates = std::mem::take(&mut desc.templates); |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
127 |
self.templates = desc |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
128 |
.template_types |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
129 |
.into_iter() |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
130 |
.map(|(size, indices)| { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
131 |
( |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
132 |
TemplateType(size), |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
133 |
indices.iter().map(|i| (&templates[*i]).into()).collect(), |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
134 |
) |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
135 |
}) |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
136 |
.collect(); |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
137 |
} |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
138 |
|
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
139 |
pub fn build_generator(&self, template: OutlineTemplate) -> impl LandGenerator { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
140 |
TemplatedLandGenerator::new(template) |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
141 |
} |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
142 |
} |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
143 |
|
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
144 |
impl MapGenerator<WfcTemplate> { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
145 |
pub fn import_yaml_templates(&mut self, text: &str) { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
146 |
let mut desc: WfcTemplateCollectionDesc = serde_yaml::from_str(text).unwrap(); |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
147 |
let templates = std::mem::take(&mut desc.templates); |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
148 |
self.templates = desc |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
149 |
.template_types |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
150 |
.into_iter() |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
151 |
.map(|(size, indices)| { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
152 |
( |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
153 |
TemplateType(size), |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
154 |
indices.iter().map(|i| (&templates[*i]).into()).collect(), |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
155 |
) |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
156 |
}) |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
157 |
.collect(); |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
158 |
} |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
159 |
|
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
160 |
pub fn build_generator(&self, template: WfcTemplate) -> impl LandGenerator { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
161 |
WavefrontCollapseLandGenerator::new(template) |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
162 |
} |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
163 |
} |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15921
diff
changeset
|
164 |
|
14170 | 165 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
166 |
struct Color(u32); |
|
167 |
||
168 |
impl Color { |
|
169 |
#[inline] |
|
170 |
fn red(self) -> u8 { |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
171 |
(self.0 & 0xFF) as u8 |
14170 | 172 |
} |
173 |
||
174 |
#[inline] |
|
175 |
fn green(self) -> u8 { |
|
176 |
(self.0 >> 8 & 0xFF) as u8 |
|
177 |
} |
|
178 |
||
179 |
#[inline] |
|
180 |
fn blue(self) -> u8 { |
|
181 |
(self.0 >> 16 & 0xFF) as u8 |
|
182 |
} |
|
183 |
||
184 |
#[inline] |
|
185 |
fn alpha(self) -> u8 { |
|
186 |
(self.0 >> 24 & 0xFF) as u8 |
|
187 |
} |
|
188 |
} |
|
189 |
||
190 |
#[inline] |
|
191 |
fn lerp(from: u8, to: u8, coef: u8) -> u8 { |
|
192 |
((from as u16 * (256 - coef as u16) + to as u16 * coef as u16) / 256) as u8 |
|
193 |
} |
|
194 |
||
195 |
#[inline] |
|
196 |
fn blend(source: u32, target: u32) -> u32 { |
|
197 |
let source = Color(source); |
|
198 |
let target = Color(target); |
|
199 |
let alpha = lerp(target.alpha(), 255, source.alpha()); |
|
200 |
let red = lerp(target.red(), source.red(), source.alpha()); |
|
201 |
let green = lerp(target.green(), source.green(), source.alpha()); |
|
202 |
let blue = lerp(target.blue(), source.blue(), source.alpha()); |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
203 |
(red as u32) | (green as u32) << 8 | (blue as u32) << 16 | (alpha as u32) << 24 |
14170 | 204 |
} |
205 |
||
15903
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
206 |
fn land_border_pass<'a, LandT, T, F>( |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
207 |
basic_value: LandT, |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
208 |
rows: T, |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
209 |
offsets: &mut [u8], |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
210 |
border_width: u8, |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
211 |
pixel_getter: F, |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
212 |
) where |
14710 | 213 |
LandT: Default + PartialEq + 'a, |
214 |
T: Iterator<Item = (&'a [LandT], &'a mut [u32])>, |
|
215 |
F: (Fn(usize, usize) -> u32), |
|
14170 | 216 |
{ |
217 |
for (land_row, tex_row) in rows { |
|
14710 | 218 |
for (x, ((land_v, tex_v), offset_v)) in land_row |
219 |
.iter() |
|
14170 | 220 |
.zip(tex_row.iter_mut()) |
221 |
.zip(offsets.iter_mut()) |
|
222 |
.enumerate() |
|
223 |
{ |
|
15903
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
224 |
*offset_v = if *land_v == basic_value { |
14170 | 225 |
if *offset_v < border_width { |
14710 | 226 |
*tex_v = blend(pixel_getter(x, *offset_v as usize), *tex_v) |
14170 | 227 |
} |
228 |
offset_v.saturating_add(1) |
|
229 |
} else { |
|
230 |
0 |
|
231 |
} |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
||
15903
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
236 |
fn tex_row_copy<LandT>( |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
237 |
basic_value: LandT, |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
238 |
land_row: &[LandT], |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
239 |
tex_row: &mut [u32], |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
240 |
sprite_row: &[u32], |
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
241 |
) where |
14710 | 242 |
LandT: Default + PartialEq, |
14702 | 243 |
{ |
14710 | 244 |
for ((land_v, tex_v), sprite_v) in land_row.iter().zip(tex_row.iter_mut()).zip(sprite_row) { |
15903
230dc46487ea
Update mapgen to take into account actual values for 'zero' and 'basic' colors
unC0Rr
parents:
15828
diff
changeset
|
245 |
*tex_v = if *land_v == basic_value { *sprite_v } else { 0 } |
14702 | 246 |
} |
247 |
} |
|
248 |
||
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
249 |
#[cfg(test)] |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
250 |
mod tests { |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
251 |
use crate::{MapGenerator, OutlineTemplate, TemplateType}; |
15924 | 252 |
use rand::thread_rng; |
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
253 |
|
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
254 |
#[test] |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
255 |
fn simple_load() { |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
256 |
let text = r#" |
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14127
diff
changeset
|
257 |
# comment |
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14127
diff
changeset
|
258 |
|
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
259 |
templates: |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
260 |
- |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
261 |
width: 3072 |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
262 |
height: 1424 |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
263 |
can_flip: false |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
264 |
can_invert: false |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
265 |
can_mirror: true |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
266 |
is_negative: false |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
267 |
put_girders: true |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
268 |
max_hedgehogs: 18 |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
269 |
outline_points: |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
270 |
- |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
271 |
- {x: 748, y: 1424, w: 1, h: 1} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
272 |
- {x: 636, y: 1252, w: 208, h: 72} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
273 |
- {x: 898, y: 1110, w: 308, h: 60} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
274 |
- {x: 1128, y: 1252, w: 434, h: 40} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
275 |
- {x: 1574, y: 1112, w: 332, h: 40} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
276 |
- {x: 1802, y: 1238, w: 226, h: 36} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
277 |
- {x: 1930, y: 1424, w: 1, h: 1} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
278 |
fill_points: |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
279 |
- {x: 1023, y: 0} |
14128
b04dac00e8e2
add command arguments to use a template from file into land_dump
alfadur
parents:
14127
diff
changeset
|
280 |
- {x: 1023, y: 0} |
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
281 |
|
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
282 |
template_types: |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
283 |
test: [0] |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
284 |
"#; |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
285 |
|
15924 | 286 |
let mut generator = MapGenerator::<OutlineTemplate>::new(); |
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
287 |
generator.import_yaml_templates(&text); |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
288 |
|
14710 | 289 |
assert!(generator |
290 |
.templates |
|
291 |
.contains_key(&TemplateType("test".to_string()))); |
|
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
292 |
|
15924 | 293 |
let template = generator.get_template("test", &mut thread_rng()).unwrap(); |
14127
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
294 |
|
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
295 |
assert_eq!(template.islands[0].len(), 7); |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
296 |
} |
0c5b9cfda9ab
add a higher level map generation lib to load yaml templates into somewhere
alfadur
parents:
diff
changeset
|
297 |
} |