veloren_common/terrain/
structure.rs

1use super::{BlockKind, SpriteKind};
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
15make_case_elim!(
16    structure_block,
17    #[derive(Clone, PartialEq, Debug, Deserialize)]
18    #[repr(u8)]
19    pub enum StructureBlock {
20        None = 0,
21        Grass = 1,
22        TemperateLeaves = 2,
23        PineLeaves = 3,
24        Acacia = 4,
25        Mangrove = 5,
26        PalmLeavesInner = 6,
27        PalmLeavesOuter = 7,
28        Water = 8,
29        GreenSludge = 9,
30        Fruit = 10,
31        Coconut = 11,
32        Chest = 12,
33        Hollow = 13,
34        Liana = 14,
35        Normal(color: Rgb<u8>) = 15,
36        Log = 16,
37        Filled(kind: BlockKind, color: Rgb<u8>) = 17,
38        Sprite(kind: SpriteKind) = 18,
39        Chestnut = 19,
40        Baobab = 20,
41        BirchWood = 21,
42        FrostpineLeaves = 22,
43        RotatedSprite(kind: SpriteKind, ori: u8) = 23,
44        EntitySpawner(entitykind: String, spawn_chance: f32) = 24,
45        Keyhole(consumes: String) = 25,
46        BoneKeyhole(consumes: String) = 26,
47        GlassKeyhole(consumes: String) = 27,
48        Sign(content: Content, ori: u8) = 28,
49        KeyholeBars(consumes: String) = 29,
50        HaniwaKeyhole(consumes: String) = 30,
51        TerracottaKeyhole(consumes: String) = 31,
52        SahaginKeyhole(consumes: String) = 32,
53        VampireKeyhole(consumes: String) = 33,
54        MyrmidonKeyhole(consumes: String) = 34,
55        MinotaurKeyhole(consumes: String) = 35,
56        MapleLeaves = 36,
57        CherryLeaves = 37,
58        AutumnLeaves = 38,
59        RedwoodWood = 39,
60    }
61);
62
63// We can't derive this because of the `make_case_elim` macro.
64#[expect(clippy::derivable_impls)]
65impl Default for StructureBlock {
66    fn default() -> Self { StructureBlock::None }
67}
68
69#[derive(Debug)]
70pub enum StructureError {
71    OutOfBounds,
72}
73
74#[derive(Clone, Debug)]
75pub struct Structure {
76    center: Vec3<i32>,
77    base: Arc<BaseStructure<StructureBlock>>,
78    custom_indices: [Option<StructureBlock>; 256],
79}
80
81#[derive(Debug)]
82pub(crate) struct BaseStructure<B> {
83    pub(crate) vol: Dyna<Option<NonZeroU8>, ()>,
84    pub(crate) palette: [B; 256],
85}
86
87pub struct StructuresGroup(Vec<Structure>);
88
89impl std::ops::Deref for StructuresGroup {
90    type Target = [Structure];
91
92    fn deref(&self) -> &[Structure] { &self.0 }
93}
94
95impl assets::Compound for StructuresGroup {
96    fn load(cache: assets::AnyCache, specifier: &assets::SharedString) -> Result<Self, BoxedError> {
97        let specs = cache.load::<StructuresGroupSpec>(specifier)?.read();
98
99        Ok(StructuresGroup(
100            specs
101                .0
102                .iter()
103                .map(|sp| {
104                    let base = cache
105                        .load::<Arc<BaseStructure<StructureBlock>>>(&sp.specifier)?
106                        .cloned();
107                    Ok(Structure {
108                        center: Vec3::from(sp.center),
109                        base,
110                        custom_indices: {
111                            let mut indices = std::array::from_fn(|_| None);
112                            for (&idx, custom) in default_custom_indices()
113                                .iter()
114                                .chain(sp.custom_indices.iter())
115                            {
116                                indices[idx as usize] = Some(custom.clone());
117                            }
118                            indices
119                        },
120                    })
121                })
122                .collect::<Result<_, BoxedError>>()?,
123        ))
124    }
125}
126
127impl Structure {
128    pub fn load_group(specifier: &str) -> AssetHandle<StructuresGroup> {
129        StructuresGroup::load_expect(&["world.manifests.", specifier].concat())
130    }
131
132    #[must_use]
133    pub fn with_center(mut self, center: Vec3<i32>) -> Self {
134        self.center = center;
135        self
136    }
137
138    pub fn get_bounds(&self) -> Aabb<i32> {
139        Aabb {
140            min: -self.center,
141            max: self.base.vol.size().map(|e| e as i32) - self.center,
142        }
143    }
144}
145
146impl BaseVol for Structure {
147    type Error = StructureError;
148    type Vox = StructureBlock;
149}
150
151impl ReadVol for Structure {
152    #[inline(always)]
153    fn get(&self, pos: Vec3<i32>) -> Result<&Self::Vox, StructureError> {
154        match self.base.vol.get(pos + self.center) {
155            Ok(None) => Ok(&StructureBlock::None),
156            Ok(Some(index)) => match &self.custom_indices[index.get() as usize] {
157                Some(sb) => Ok(sb),
158                None => Ok(&self.base.palette[index.get() as usize]),
159            },
160            Err(DynaError::OutOfBounds) => Err(StructureError::OutOfBounds),
161        }
162    }
163}
164
165pub(crate) fn load_base_structure<B: Default>(
166    dot_vox_data: &DotVoxData,
167    mut to_block: impl FnMut(Rgb<u8>) -> B,
168) -> BaseStructure<B> {
169    let mut palette = std::array::from_fn(|_| B::default());
170    if let Some(model) = dot_vox_data.models.first() {
171        for (i, col) in dot_vox_data
172            .palette
173            .iter()
174            .map(|col| Rgb::new(col.r, col.g, col.b))
175            .enumerate()
176        {
177            palette[(i + 1).min(255)] = to_block(col);
178        }
179
180        let mut vol = Dyna::filled(
181            Vec3::new(model.size.x, model.size.y, model.size.z),
182            None,
183            (),
184        );
185
186        for voxel in &model.voxels {
187            let _ = vol.set(
188                Vec3::new(voxel.x, voxel.y, voxel.z).map(i32::from),
189                Some(NonZeroU8::new(voxel.i + 1).unwrap()),
190            );
191        }
192
193        BaseStructure { vol, palette }
194    } else {
195        BaseStructure {
196            vol: Dyna::filled(Vec3::zero(), None, ()),
197            palette,
198        }
199    }
200}
201
202impl assets::Compound for BaseStructure<StructureBlock> {
203    fn load(cache: assets::AnyCache, specifier: &assets::SharedString) -> Result<Self, BoxedError> {
204        let dot_vox_data = cache.load::<DotVoxAsset>(specifier)?.read();
205        let dot_vox_data = &dot_vox_data.0;
206
207        Ok(load_base_structure(dot_vox_data, |col| {
208            StructureBlock::Filled(BlockKind::Misc, col)
209        }))
210    }
211}
212
213#[derive(Deserialize)]
214struct StructureSpec {
215    specifier: String,
216    center: [i32; 3],
217    #[serde(default)]
218    custom_indices: HashMap<u8, StructureBlock>,
219}
220
221fn default_custom_indices() -> HashMap<u8, StructureBlock> {
222    let blocks: [_; 16] = [
223        /* 1 */ Some(StructureBlock::TemperateLeaves),
224        /* 2 */ Some(StructureBlock::PineLeaves),
225        /* 3 */ None,
226        /* 4 */ Some(StructureBlock::Water),
227        /* 5 */ Some(StructureBlock::Acacia),
228        /* 6 */ Some(StructureBlock::Mangrove),
229        /* 7 */ Some(StructureBlock::GreenSludge),
230        /* 8 */ Some(StructureBlock::Fruit),
231        /* 9 */ Some(StructureBlock::Grass),
232        /* 10 */ Some(StructureBlock::Liana),
233        /* 11 */ Some(StructureBlock::Chest),
234        /* 12 */ Some(StructureBlock::Coconut),
235        /* 13 */ None,
236        /* 14 */ Some(StructureBlock::PalmLeavesOuter),
237        /* 15 */ Some(StructureBlock::PalmLeavesInner),
238        /* 16 */ Some(StructureBlock::Hollow),
239    ];
240
241    blocks
242        .iter()
243        .enumerate()
244        .filter_map(|(i, sb)| sb.as_ref().map(|sb| (i as u8 + 1, sb.clone())))
245        .collect()
246}
247
248#[derive(Deserialize)]
249struct StructuresGroupSpec(Vec<StructureSpec>);
250
251impl assets::Asset for StructuresGroupSpec {
252    type Loader = assets::RonLoader;
253
254    const EXTENSION: &'static str = "ron";
255}