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