aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/registry_holder/float_provider.rs
blob: c7d56bf9ffc05d72435275a040d0821ecfc913f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::ops::Range;

use azalea_registry::builtin::FloatProviderKind;
use simdnbt::{
    DeserializeError, FromNbtTag,
    borrow::{NbtCompound, NbtTag},
};

use crate::registry_holder::get_in_compound;

#[derive(Clone, Debug)]
pub enum FloatProvider {
    Constant(f32),
    Uniform {
        range: Range<f32>,
    },
    ClampedNormal {
        mean: f32,
        deviation: f32,
        min: f32,
        max: f32,
    },
    Trapezoid {
        min: f32,
        max: f32,
        plateau: f32,
    },
}
impl FromNbtTag for FloatProvider {
    fn from_nbt_tag(tag: NbtTag) -> Option<Self> {
        if let Some(f) = tag.float() {
            return Some(Self::Constant(f));
        }
        if let Some(c) = tag.compound() {
            return Self::from_compound(c).ok();
        }
        None
    }
}
impl FloatProvider {
    fn from_compound(nbt: NbtCompound) -> Result<Self, DeserializeError> {
        let kind = get_in_compound(&nbt, "type")?;
        match kind {
            FloatProviderKind::Constant => Ok(Self::Constant(get_in_compound(&nbt, "value")?)),
            FloatProviderKind::Uniform => {
                let min_inclusive = get_in_compound(&nbt, "min_inclusive")?;
                let max_exclusive = get_in_compound(&nbt, "max_exclusive")?;
                Ok(Self::Uniform {
                    range: min_inclusive..max_exclusive,
                })
            }
            FloatProviderKind::ClampedNormal => {
                let mean = get_in_compound(&nbt, "mean")?;
                let deviation = get_in_compound(&nbt, "deviation")?;
                let min = get_in_compound(&nbt, "min")?;
                let max = get_in_compound(&nbt, "max")?;
                Ok(Self::ClampedNormal {
                    mean,
                    deviation,
                    min,
                    max,
                })
            }
            FloatProviderKind::Trapezoid => {
                let min = get_in_compound(&nbt, "min")?;
                let max = get_in_compound(&nbt, "max")?;
                let plateau = get_in_compound(&nbt, "plateau")?;
                Ok(Self::Trapezoid { min, max, plateau })
            }
        }
    }
}