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