veloren_common/comp/body/
biped_small.rs

1use common_base::{enum_iter, struct_iter};
2use rand::{seq::SliceRandom, thread_rng};
3use serde::{Deserialize, Serialize};
4
5struct_iter! {
6    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
7    pub struct Body {
8        pub species: Species,
9        pub body_type: BodyType,
10    }
11}
12
13impl Body {
14    pub fn random() -> Self {
15        let mut rng = thread_rng();
16        let species = *ALL_SPECIES.choose(&mut rng).unwrap();
17        Self::random_with(&mut rng, &species)
18    }
19
20    #[inline]
21    pub fn random_with(rng: &mut impl rand::Rng, &species: &Species) -> Self {
22        let body_type = *ALL_BODY_TYPES.choose(rng).unwrap();
23        Self { species, body_type }
24    }
25}
26
27impl From<Body> for super::Body {
28    fn from(body: Body) -> Self { super::Body::BipedSmall(body) }
29}
30
31enum_iter! {
32    ~const_array(ALL)
33    #[derive(
34        Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
35    #[repr(u32)]
36    pub enum Species {
37        Gnome = 0,
38        Sahagin = 1,
39        Adlet = 2,
40        Gnarling = 3,
41        Mandragora = 4,
42        Kappa = 5,
43        Cactid = 6,
44        Gnoll = 7,
45        Haniwa = 8,
46        Myrmidon = 9,
47        Husk = 10,
48        Boreal = 11,
49        Bushly = 12,
50        Irrwurz = 13,
51        IronDwarf = 14,
52        Flamekeeper = 15,
53        ShamanicSpirit = 16,
54        Jiangshi = 17,
55        TreasureEgg = 18,
56        GnarlingChieftain = 19,
57        BloodmoonHeiress = 20,
58        Bloodservant = 21,
59        Harlequin = 22,
60        GoblinThug = 23,
61        GoblinChucker = 24,
62        GoblinRuffian = 25,
63        GreenLegoom = 26,
64        OchreLegoom = 27,
65        PurpleLegoom = 28,
66        RedLegoom = 29,
67    }
68}
69
70/// Data representing per-species generic data.
71#[derive(Clone, Debug, Deserialize, Serialize)]
72pub struct AllSpecies<SpeciesMeta> {
73    pub gnome: SpeciesMeta,
74    pub sahagin: SpeciesMeta,
75    pub adlet: SpeciesMeta,
76    pub gnarling: SpeciesMeta,
77    pub mandragora: SpeciesMeta,
78    pub kappa: SpeciesMeta,
79    pub cactid: SpeciesMeta,
80    pub gnoll: SpeciesMeta,
81    pub haniwa: SpeciesMeta,
82    pub myrmidon: SpeciesMeta,
83    pub husk: SpeciesMeta,
84    pub boreal: SpeciesMeta,
85    pub bushly: SpeciesMeta,
86    pub irrwurz: SpeciesMeta,
87    pub iron_dwarf: SpeciesMeta,
88    pub flamekeeper: SpeciesMeta,
89    pub shamanic_spirit: SpeciesMeta,
90    pub jiangshi: SpeciesMeta,
91    pub treasure_egg: SpeciesMeta,
92    pub gnarling_chieftain: SpeciesMeta,
93    pub bloodmoon_heiress: SpeciesMeta,
94    pub bloodservant: SpeciesMeta,
95    pub harlequin: SpeciesMeta,
96    pub goblin_thug: SpeciesMeta,
97    pub goblin_chucker: SpeciesMeta,
98    pub goblin_ruffian: SpeciesMeta,
99    pub green_legoom: SpeciesMeta,
100    pub ochre_legoom: SpeciesMeta,
101    pub purple_legoom: SpeciesMeta,
102    pub red_legoom: SpeciesMeta,
103}
104
105impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
106    type Output = SpeciesMeta;
107
108    #[inline]
109    fn index(&self, &index: &'a Species) -> &Self::Output {
110        match index {
111            Species::Gnome => &self.gnome,
112            Species::Sahagin => &self.sahagin,
113            Species::Adlet => &self.adlet,
114            Species::Gnarling => &self.gnarling,
115            Species::Mandragora => &self.mandragora,
116            Species::Kappa => &self.kappa,
117            Species::Cactid => &self.cactid,
118            Species::Gnoll => &self.gnoll,
119            Species::Haniwa => &self.haniwa,
120            Species::Myrmidon => &self.myrmidon,
121            Species::Husk => &self.husk,
122            Species::Boreal => &self.boreal,
123            Species::Bushly => &self.bushly,
124            Species::Irrwurz => &self.irrwurz,
125            Species::IronDwarf => &self.iron_dwarf,
126            Species::Flamekeeper => &self.flamekeeper,
127            Species::ShamanicSpirit => &self.shamanic_spirit,
128            Species::Jiangshi => &self.jiangshi,
129            Species::TreasureEgg => &self.treasure_egg,
130            Species::GnarlingChieftain => &self.gnarling_chieftain,
131            Species::BloodmoonHeiress => &self.bloodmoon_heiress,
132            Species::Bloodservant => &self.bloodservant,
133            Species::Harlequin => &self.harlequin,
134            Species::GoblinThug => &self.goblin_thug,
135            Species::GoblinChucker => &self.goblin_chucker,
136            Species::GoblinRuffian => &self.goblin_ruffian,
137            Species::GreenLegoom => &self.green_legoom,
138            Species::OchreLegoom => &self.ochre_legoom,
139            Species::PurpleLegoom => &self.purple_legoom,
140            Species::RedLegoom => &self.red_legoom,
141        }
142    }
143}
144
145pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
146
147impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
148    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
149    type Item = Species;
150
151    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
152}
153
154enum_iter! {
155    ~const_array(ALL)
156    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
157    #[repr(u32)]
158    pub enum BodyType {
159        Female = 0,
160        Male = 1,
161    }
162}
163pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;