1use super::{BlockKind, StructureSprite};
2use crate::{
3 assets::{self, AssetExt, AssetHandle, BoxedError, DotVoxAsset},
4 make_case_elim,
5 vol::{BaseVol, ReadVol, SizedVol, WriteVol},
6 volumes::dyna::{Dyna, DynaError},
7};
8use common_i18n::Content;
9use dot_vox::DotVoxData;
10use hashbrown::HashMap;
11use serde::Deserialize;
12use std::{num::NonZeroU8, sync::Arc};
13use vek::*;
14
15use crate::terrain::SpriteCfg;
16
17make_case_elim!(
18 structure_block,
19 #[derive(Clone, PartialEq, Debug, Deserialize)]
20 #[repr(u8)]
21 pub enum StructureBlock {
22 None = 0,
23 Grass = 1,
24 TemperateLeaves = 2,
25 PineLeaves = 3,
26 Acacia = 4,
27 Mangrove = 5,
28 PalmLeavesInner = 6,
29 PalmLeavesOuter = 7,
30 Water = 8,
31 GreenSludge = 9,
32 Fruit = 10,
33 Coconut = 11,
34 MaybeChest = 12,
35 Hollow = 13,
36 Liana = 14,
37 Normal(color: Rgb<u8>) = 15,
38 Log = 16,
39 Filled(kind: BlockKind, color: Rgb<u8>) = 17,
40 Sprite(sprite: StructureSprite) = 18,
41 Chestnut = 19,
42 Baobab = 20,
43 BirchWood = 21,
44 FrostpineLeaves = 22,
45 EntitySpawner(entitykind: String, spawn_chance: f32) = 24,
48 Keyhole(consumes: String) = 25,
49 BoneKeyhole(consumes: String) = 26,
50 GlassKeyhole(consumes: String) = 27,
51 Sign(content: Content, ori: u8) = 28,
52 KeyholeBars(consumes: String) = 29,
53 HaniwaKeyhole(consumes: String) = 30,
54 TerracottaKeyhole(consumes: String) = 31,
55 SahaginKeyhole(consumes: String) = 32,
56 VampireKeyhole(consumes: String) = 33,
57 MyrmidonKeyhole(consumes: String) = 34,
58 MinotaurKeyhole(consumes: String) = 35,
59 MapleLeaves = 36,
60 CherryLeaves = 37,
61 AutumnLeaves = 38,
62 RedwoodWood = 39,
63 SpriteWithCfg(kind: StructureSprite, sprite_cfg: SpriteCfg) = 40,
64 }
65);
66
67#[expect(clippy::derivable_impls)]
69impl Default for StructureBlock {
70 fn default() -> Self { StructureBlock::None }
71}
72
73#[derive(Debug)]
74pub enum StructureError {
75 OutOfBounds,
76}
77
78#[derive(Clone, Debug)]
79pub struct Structure {
80 center: Vec3<i32>,
81 base: Arc<BaseStructure<StructureBlock>>,
82 custom_indices: [Option<StructureBlock>; 256],
83}
84
85#[derive(Debug)]
86pub(crate) struct BaseStructure<B> {
87 pub(crate) vol: Dyna<Option<NonZeroU8>, ()>,
88 pub(crate) palette: [B; 256],
89}
90
91pub struct StructuresGroup(Vec<Structure>);
92
93impl std::ops::Deref for StructuresGroup {
94 type Target = [Structure];
95
96 fn deref(&self) -> &[Structure] { &self.0 }
97}
98
99impl assets::Compound for StructuresGroup {
100 fn load(cache: assets::AnyCache, specifier: &assets::SharedString) -> Result<Self, BoxedError> {
101 let specs = cache.load::<StructuresGroupSpec>(specifier)?.read();
102
103 Ok(StructuresGroup(
104 specs
105 .0
106 .iter()
107 .map(|sp| {
108 let base = cache
109 .load::<Arc<BaseStructure<StructureBlock>>>(&sp.specifier)?
110 .cloned();
111 Ok(Structure {
112 center: Vec3::from(sp.center),
113 base,
114 custom_indices: {
115 let mut indices = std::array::from_fn(|_| None);
116 for (&idx, custom) in default_custom_indices()
117 .iter()
118 .chain(sp.custom_indices.iter())
119 {
120 indices[idx as usize] = Some(custom.clone());
121 }
122 indices
123 },
124 })
125 })
126 .collect::<Result<_, BoxedError>>()?,
127 ))
128 }
129}
130
131const STRUCTURE_MANIFESTS_DIR: &str = "world.manifests";
132impl Structure {
133 pub fn load_group(specifier: &str) -> AssetHandle<StructuresGroup> {
134 StructuresGroup::load_expect(&format!("{STRUCTURE_MANIFESTS_DIR}.{specifier}"))
135 }
136
137 #[must_use]
138 pub fn with_center(mut self, center: Vec3<i32>) -> Self {
139 self.center = center;
140 self
141 }
142
143 pub fn get_bounds(&self) -> Aabb<i32> {
144 Aabb {
145 min: -self.center,
146 max: self.base.vol.size().map(|e| e as i32) - self.center,
147 }
148 }
149}
150
151impl BaseVol for Structure {
152 type Error = StructureError;
153 type Vox = StructureBlock;
154}
155
156impl ReadVol for Structure {
157 #[inline(always)]
158 fn get(&self, pos: Vec3<i32>) -> Result<&Self::Vox, StructureError> {
159 match self.base.vol.get(pos + self.center) {
160 Ok(None) => Ok(&StructureBlock::None),
161 Ok(Some(index)) => match &self.custom_indices[index.get() as usize] {
162 Some(sb) => Ok(sb),
163 None => Ok(&self.base.palette[index.get() as usize]),
164 },
165 Err(DynaError::OutOfBounds) => Err(StructureError::OutOfBounds),
166 }
167 }
168}
169
170pub(crate) fn load_base_structure<B: Default>(
171 dot_vox_data: &DotVoxData,
172 mut to_block: impl FnMut(Rgb<u8>) -> B,
173) -> BaseStructure<B> {
174 let mut palette = std::array::from_fn(|_| B::default());
175 if let Some(model) = dot_vox_data.models.first() {
176 for (i, col) in dot_vox_data
177 .palette
178 .iter()
179 .map(|col| Rgb::new(col.r, col.g, col.b))
180 .enumerate()
181 {
182 palette[(i + 1).min(255)] = to_block(col);
183 }
184
185 let mut vol = Dyna::filled(
186 Vec3::new(model.size.x, model.size.y, model.size.z),
187 None,
188 (),
189 );
190
191 for voxel in &model.voxels {
192 let _ = vol.set(
193 Vec3::new(voxel.x, voxel.y, voxel.z).map(i32::from),
194 Some(NonZeroU8::new(voxel.i + 1).unwrap()),
195 );
196 }
197
198 BaseStructure { vol, palette }
199 } else {
200 BaseStructure {
201 vol: Dyna::filled(Vec3::zero(), None, ()),
202 palette,
203 }
204 }
205}
206
207impl assets::Compound for BaseStructure<StructureBlock> {
208 fn load(cache: assets::AnyCache, specifier: &assets::SharedString) -> Result<Self, BoxedError> {
209 let dot_vox_data = cache.load::<DotVoxAsset>(specifier)?.read();
210 let dot_vox_data = &dot_vox_data.0;
211
212 Ok(load_base_structure(dot_vox_data, |col| {
213 StructureBlock::Filled(BlockKind::Misc, col)
214 }))
215 }
216}
217
218#[derive(Clone, Deserialize)]
219struct StructureSpec {
220 specifier: String,
221 center: [i32; 3],
222 #[serde(default)]
223 custom_indices: HashMap<u8, StructureBlock>,
224}
225
226fn default_custom_indices() -> HashMap<u8, StructureBlock> {
227 let blocks: [_; 16] = [
228 Some(StructureBlock::TemperateLeaves),
229 Some(StructureBlock::PineLeaves),
230 None,
231 Some(StructureBlock::Water),
232 Some(StructureBlock::Acacia),
233 Some(StructureBlock::Mangrove),
234 Some(StructureBlock::GreenSludge),
235 Some(StructureBlock::Fruit),
236 Some(StructureBlock::Grass),
237 Some(StructureBlock::Liana),
238 Some(StructureBlock::MaybeChest),
239 Some(StructureBlock::Coconut),
240 None,
241 Some(StructureBlock::PalmLeavesOuter),
242 Some(StructureBlock::PalmLeavesInner),
243 Some(StructureBlock::Hollow),
244 ];
245
246 blocks
247 .iter()
248 .enumerate()
249 .filter_map(|(i, sb)| sb.as_ref().map(|sb| (i as u8 + 1, sb.clone())))
250 .collect()
251}
252
253#[derive(Clone, Deserialize)]
254struct StructuresGroupSpec(Vec<StructureSpec>);
255
256impl assets::Asset for StructuresGroupSpec {
257 type Loader = assets::RonLoader;
258
259 const EXTENSION: &'static str = "ron";
260}
261
262#[test]
263fn test_load_structures() {
264 let errors =
265 common_assets::load_rec_dir::<StructuresGroupSpec>("world.manifests.site_structures")
266 .expect("This should be able to load")
267 .read()
268 .ids()
269 .chain(
270 common_assets::load_rec_dir::<StructuresGroupSpec>("world.manifests.spots")
271 .expect("This should be able to load")
272 .read()
273 .ids(),
274 )
275 .chain(
276 common_assets::load_rec_dir::<StructuresGroupSpec>("world.manifests.spots_general")
277 .expect("This should be able to load")
278 .read()
279 .ids(),
280 )
281 .chain(
282 common_assets::load_rec_dir::<StructuresGroupSpec>("world.manifests.trees")
283 .expect("This should be able to load")
284 .read()
285 .ids(),
286 )
287 .chain(
288 common_assets::load_rec_dir::<StructuresGroupSpec>("world.manifests.shrubs")
289 .expect("This should be able to load")
290 .read()
291 .ids(),
292 )
293 .filter_map(|id| StructuresGroupSpec::load(id).err().map(|err| (id, err)))
294 .fold(None::<String>, |mut acc, (id, err)| {
295 use std::fmt::Write;
296
297 let s = acc.get_or_insert_default();
298 _ = writeln!(s, "{id}: {err}");
299
300 acc
301 });
302
303 if let Some(errors) = errors {
304 panic!("Failed to load the following structures:\n{errors}")
305 }
306}
307
308#[cfg(test)]
309mod tests {
310 use super::*;
311 use crate::{
312 generation::tests::validate_entity_config,
313 lottery::{LootSpec, tests::validate_loot_spec},
314 terrain::Block,
315 };
316 use std::ops::Deref;
317
318 pub fn validate_sprite_and_cfg(sprite: StructureSprite, sprite_cfg: &SpriteCfg) {
319 let SpriteCfg {
320 unlock: _,
322 content: _,
324 loot_table,
325 } = sprite_cfg;
326
327 let sprite = sprite
328 .get_block(Block::air)
329 .get_sprite()
330 .expect("This should have the sprite");
331
332 if let Some(loot_table) = loot_table.clone() {
333 if !sprite.is_defined_as_container() {
334 panic!(
335 r"
336Manifest contains a structure block with custom loot table for a sprite
337that isn't defined as container, you probably don't want that.
338
339If you want, add this sprite to `is_defined_as_container` list.
340Sprite in question: {sprite:?}
341"
342 );
343 }
344
345 validate_loot_spec(&LootSpec::LootTable(loot_table))
346 }
347 }
348
349 fn validate_structure_block(sb: &StructureBlock, id: &str) {
350 match sb {
351 StructureBlock::SpriteWithCfg(sprite, sprite_cfg) => {
352 std::panic::catch_unwind(|| validate_sprite_and_cfg(*sprite, sprite_cfg))
353 .unwrap_or_else(|_| {
354 panic!("failed to load structure_block in: {id}\n{sb:?}");
355 })
356 },
357 StructureBlock::EntitySpawner(entity_kind, _spawn_chance) => {
358 let config = &entity_kind;
359 std::panic::catch_unwind(|| validate_entity_config(config)).unwrap_or_else(|_| {
360 panic!("failed to load structure_block in: {id}\n{sb:?}");
361 })
362 },
363 StructureBlock::None
365 | StructureBlock::Grass
366 | StructureBlock::TemperateLeaves
367 | StructureBlock::PineLeaves
368 | StructureBlock::Acacia
369 | StructureBlock::Mangrove
370 | StructureBlock::PalmLeavesInner
371 | StructureBlock::PalmLeavesOuter
372 | StructureBlock::Water
373 | StructureBlock::GreenSludge
374 | StructureBlock::Fruit
375 | StructureBlock::Coconut
376 | StructureBlock::MaybeChest
377 | StructureBlock::Hollow
378 | StructureBlock::Liana
379 | StructureBlock::Normal { .. }
380 | StructureBlock::Log
381 | StructureBlock::Filled { .. }
382 | StructureBlock::Sprite { .. }
383 | StructureBlock::Chestnut
384 | StructureBlock::Baobab
385 | StructureBlock::BirchWood
386 | StructureBlock::FrostpineLeaves
387 | StructureBlock::MapleLeaves
388 | StructureBlock::CherryLeaves
389 | StructureBlock::RedwoodWood
390 | StructureBlock::AutumnLeaves => {},
391 StructureBlock::Keyhole { .. }
393 | StructureBlock::MyrmidonKeyhole { .. }
394 | StructureBlock::MinotaurKeyhole { .. }
395 | StructureBlock::SahaginKeyhole { .. }
396 | StructureBlock::VampireKeyhole { .. }
397 | StructureBlock::BoneKeyhole { .. }
398 | StructureBlock::GlassKeyhole { .. }
399 | StructureBlock::KeyholeBars { .. }
400 | StructureBlock::HaniwaKeyhole { .. }
401 | StructureBlock::TerracottaKeyhole { .. } => {},
402 StructureBlock::Sign { .. } => {},
404 }
405 }
406
407 #[test]
408 fn test_structure_manifests() {
409 let specs = assets::load_rec_dir::<StructuresGroupSpec>(STRUCTURE_MANIFESTS_DIR).unwrap();
410 for id in specs.read().ids() {
411 if id != "world.manifests.spots" {
413 let group = StructuresGroupSpec::load(id).unwrap_or_else(|e| {
414 panic!("failed to load: {id}\n{e:?}");
415 });
416 let StructuresGroupSpec(group) = group.read().deref().clone();
417 for StructureSpec {
418 specifier,
419 center: _center,
420 custom_indices,
421 } in group
422 {
423 BaseStructure::<StructureBlock>::load(&specifier).unwrap_or_else(|e| {
424 panic!("failed to load specifier for: {id}\n{e:?}");
425 });
426
427 for sb in custom_indices.values() {
428 validate_structure_block(sb, id);
429 }
430 }
431 }
432 }
433 }
434}