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