veloren_common/terrain/
structure.rs

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, SpriteKind};
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        // NOTE: When adding set it equal to `23`.
46        // = 23,
47        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: SpriteKind, sprite_cfg: SpriteCfg) = 40,
64    }
65);
66
67// We can't derive this because of the `make_case_elim` macro.
68#[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        /* 1 */ Some(StructureBlock::TemperateLeaves),
229        /* 2 */ Some(StructureBlock::PineLeaves),
230        /* 3 */ None,
231        /* 4 */ Some(StructureBlock::Water),
232        /* 5 */ Some(StructureBlock::Acacia),
233        /* 6 */ Some(StructureBlock::Mangrove),
234        /* 7 */ Some(StructureBlock::GreenSludge),
235        /* 8 */ Some(StructureBlock::Fruit),
236        /* 9 */ Some(StructureBlock::Grass),
237        /* 10 */ Some(StructureBlock::Liana),
238        /* 11 */ Some(StructureBlock::MaybeChest),
239        /* 12 */ Some(StructureBlock::Coconut),
240        /* 13 */ None,
241        /* 14 */ Some(StructureBlock::PalmLeavesOuter),
242        /* 15 */ Some(StructureBlock::PalmLeavesInner),
243        /* 16 */ 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    };
315    use std::ops::Deref;
316
317    pub fn validate_sprite_and_cfg(sprite: SpriteKind, sprite_cfg: &SpriteCfg) {
318        let SpriteCfg {
319            // TODO: write validation for UnlockKind?
320            unlock: _,
321            // TODO: requires access to i18n for validation
322            content: _,
323            loot_table,
324        } = sprite_cfg;
325
326        if let Some(loot_table) = loot_table.clone() {
327            if !sprite.is_defined_as_container() {
328                panic!(
329                    r"
330Manifest contains a structure block with custom loot table for a sprite
331that isn't defined as container, you probably don't want that.
332
333If you want, add this sprite to `is_defined_as_container` list.
334Sprite in question: {sprite:?}
335"
336                );
337            }
338
339            validate_loot_spec(&LootSpec::LootTable(loot_table))
340        }
341    }
342
343    fn validate_structure_block(sb: &StructureBlock, id: &str) {
344        match sb {
345            StructureBlock::SpriteWithCfg(sprite, sprite_cfg) => {
346                std::panic::catch_unwind(|| validate_sprite_and_cfg(*sprite, sprite_cfg))
347                    .unwrap_or_else(|_| {
348                        panic!("failed to load structure_block in: {id}\n{sb:?}");
349                    })
350            },
351            StructureBlock::EntitySpawner(entity_kind, _spawn_chance) => {
352                let config = &entity_kind;
353                std::panic::catch_unwind(|| validate_entity_config(config)).unwrap_or_else(|_| {
354                    panic!("failed to load structure_block in: {id}\n{sb:?}");
355                })
356            },
357            // These probably can't fail
358            StructureBlock::None
359            | StructureBlock::Grass
360            | StructureBlock::TemperateLeaves
361            | StructureBlock::PineLeaves
362            | StructureBlock::Acacia
363            | StructureBlock::Mangrove
364            | StructureBlock::PalmLeavesInner
365            | StructureBlock::PalmLeavesOuter
366            | StructureBlock::Water
367            | StructureBlock::GreenSludge
368            | StructureBlock::Fruit
369            | StructureBlock::Coconut
370            | StructureBlock::MaybeChest
371            | StructureBlock::Hollow
372            | StructureBlock::Liana
373            | StructureBlock::Normal { .. }
374            | StructureBlock::Log
375            | StructureBlock::Filled { .. }
376            | StructureBlock::Sprite { .. }
377            | StructureBlock::Chestnut
378            | StructureBlock::Baobab
379            | StructureBlock::BirchWood
380            | StructureBlock::FrostpineLeaves
381            | StructureBlock::MapleLeaves
382            | StructureBlock::CherryLeaves
383            | StructureBlock::RedwoodWood
384            | StructureBlock::AutumnLeaves => {},
385            // TODO: ideally this should be tested as well
386            StructureBlock::Keyhole { .. }
387            | StructureBlock::MyrmidonKeyhole { .. }
388            | StructureBlock::MinotaurKeyhole { .. }
389            | StructureBlock::SahaginKeyhole { .. }
390            | StructureBlock::VampireKeyhole { .. }
391            | StructureBlock::BoneKeyhole { .. }
392            | StructureBlock::GlassKeyhole { .. }
393            | StructureBlock::KeyholeBars { .. }
394            | StructureBlock::HaniwaKeyhole { .. }
395            | StructureBlock::TerracottaKeyhole { .. } => {},
396            // TODO: requires access to i18n for validation
397            StructureBlock::Sign { .. } => {},
398        }
399    }
400
401    #[test]
402    fn test_structure_manifests() {
403        let specs = assets::load_rec_dir::<StructuresGroupSpec>(STRUCTURE_MANIFESTS_DIR).unwrap();
404        for id in specs.read().ids() {
405            // Ignore manifest file
406            if id != "world.manifests.spots" {
407                let group = StructuresGroupSpec::load(id).unwrap_or_else(|e| {
408                    panic!("failed to load: {id}\n{e:?}");
409                });
410                let StructuresGroupSpec(group) = group.read().deref().clone();
411                for StructureSpec {
412                    specifier,
413                    center: _center,
414                    custom_indices,
415                } in group
416                {
417                    BaseStructure::<StructureBlock>::load(&specifier).unwrap_or_else(|e| {
418                        panic!("failed to load specifier for: {id}\n{e:?}");
419                    });
420
421                    for sb in custom_indices.values() {
422                        validate_structure_block(sb, id);
423                    }
424                }
425            }
426        }
427    }
428}