veloren_common/comp/body/
quadruped_low.rs

1use common_base::{enum_iter, struct_iter};
2use rand::{seq::SliceRandom, thread_rng};
3use serde::{Deserialize, Serialize};
4use strum::{Display, EnumString};
5
6struct_iter! {
7    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
8    pub struct Body {
9        pub species: Species,
10        pub body_type: BodyType,
11    }
12}
13
14impl Body {
15    pub fn random() -> Self {
16        let mut rng = thread_rng();
17        let species = *ALL_SPECIES.choose(&mut rng).unwrap();
18        Self::random_with(&mut rng, &species)
19    }
20
21    #[inline]
22    pub fn random_with(rng: &mut impl rand::Rng, &species: &Species) -> Self {
23        let body_type = *ALL_BODY_TYPES.choose(rng).unwrap();
24        Self { species, body_type }
25    }
26}
27
28impl From<Body> for super::Body {
29    fn from(body: Body) -> Self { super::Body::QuadrupedLow(body) }
30}
31
32// Renaming any enum entries here (re-ordering is fine) will require a
33// database migration to ensure pets correctly de-serialize on player login.
34enum_iter! {
35    ~const_array(ALL)
36    #[derive(
37        Copy,
38        Clone,
39        Debug,
40        Display,
41        EnumString,
42        PartialEq,
43        Eq,
44        PartialOrd,
45        Ord,
46        Hash,
47        Serialize,
48        Deserialize,
49    )]
50    #[repr(u32)]
51    pub enum Species {
52        Crocodile = 0,
53        Alligator = 1,
54        Salamander = 2,
55        Monitor = 3,
56        Asp = 4,
57        Tortoise = 5,
58        Pangolin = 6,
59        Maneater = 7,
60        Sandshark = 8,
61        Hakulaq = 9,
62        Lavadrake = 10,
63        Basilisk = 11,
64        Deadwood = 12,
65        Icedrake = 13,
66        SeaCrocodile = 14,
67        Dagon = 15,
68        Rocksnapper = 16,
69        Rootsnapper = 17,
70        Reefsnapper = 18,
71        Elbst = 19,
72        Mossdrake = 20,
73        Driggle = 21,
74        Snaretongue = 22,
75        Hydra = 23,
76    }
77}
78
79/// Data representing per-species generic data.
80#[derive(Clone, Debug, Deserialize, Serialize)]
81pub struct AllSpecies<SpeciesMeta> {
82    pub crocodile: SpeciesMeta,
83    pub sea_crocodile: SpeciesMeta,
84    pub alligator: SpeciesMeta,
85    pub salamander: SpeciesMeta,
86    pub elbst: SpeciesMeta,
87    pub monitor: SpeciesMeta,
88    pub asp: SpeciesMeta,
89    pub tortoise: SpeciesMeta,
90    pub rocksnapper: SpeciesMeta,
91    pub rootsnapper: SpeciesMeta,
92    pub reefsnapper: SpeciesMeta,
93    pub pangolin: SpeciesMeta,
94    pub maneater: SpeciesMeta,
95    pub sandshark: SpeciesMeta,
96    pub hakulaq: SpeciesMeta,
97    pub dagon: SpeciesMeta,
98    pub lavadrake: SpeciesMeta,
99    pub basilisk: SpeciesMeta,
100    pub deadwood: SpeciesMeta,
101    pub icedrake: SpeciesMeta,
102    pub mossdrake: SpeciesMeta,
103    pub driggle: SpeciesMeta,
104    pub snaretongue: SpeciesMeta,
105    pub hydra: SpeciesMeta,
106}
107
108impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
109    type Output = SpeciesMeta;
110
111    #[inline]
112    fn index(&self, &index: &'a Species) -> &Self::Output {
113        match index {
114            Species::Crocodile => &self.crocodile,
115            Species::SeaCrocodile => &self.sea_crocodile,
116            Species::Alligator => &self.alligator,
117            Species::Salamander => &self.salamander,
118            Species::Elbst => &self.elbst,
119            Species::Monitor => &self.monitor,
120            Species::Asp => &self.asp,
121            Species::Tortoise => &self.tortoise,
122            Species::Rocksnapper => &self.rocksnapper,
123            Species::Rootsnapper => &self.rootsnapper,
124            Species::Reefsnapper => &self.reefsnapper,
125            Species::Pangolin => &self.pangolin,
126            Species::Maneater => &self.maneater,
127            Species::Sandshark => &self.sandshark,
128            Species::Hakulaq => &self.hakulaq,
129            Species::Dagon => &self.dagon,
130            Species::Lavadrake => &self.lavadrake,
131            Species::Basilisk => &self.basilisk,
132            Species::Deadwood => &self.deadwood,
133            Species::Icedrake => &self.icedrake,
134            Species::Mossdrake => &self.mossdrake,
135            Species::Driggle => &self.driggle,
136            Species::Snaretongue => &self.snaretongue,
137            Species::Hydra => &self.hydra,
138        }
139    }
140}
141
142pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
143
144impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
145    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
146    type Item = Species;
147
148    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
149}
150
151// Renaming any enum entries here (re-ordering is fine) will require a
152// database migration to ensure pets correctly de-serialize on player login.
153enum_iter! {
154    ~const_array(ALL)
155    #[derive(
156        Copy,
157        Clone,
158        Debug,
159        Display,
160        EnumString,
161        PartialEq,
162        Eq,
163        PartialOrd,
164        Ord,
165        Hash,
166        Serialize,
167        Deserialize,
168    )]
169    #[repr(u32)]
170    pub enum BodyType {
171        Female = 0,
172        Male = 1,
173    }
174}
175pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;