veloren_common/comp/body/
quadruped_small.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::QuadrupedSmall(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        Pig = 0,
53        Fox = 1,
54        Sheep = 2,
55        Boar = 3,
56        Jackalope = 4,
57        Skunk = 5,
58        Cat = 6,
59        Batfox = 7,
60        Raccoon = 8,
61        Quokka = 9,
62        Goat = 10,
63        Holladon = 11,
64        Hyena = 12,
65        Rabbit = 13,
66        Truffler = 14,
67        Frog = 15,
68        Rat = 16,
69        Axolotl = 17,
70        Gecko = 18,
71        Turtle = 19,
72        Squirrel = 20,
73        Fungome = 21,
74        Porcupine = 22,
75        Beaver = 23,
76        Hare = 24,
77        Dog = 25,
78        Seal = 26,
79        TreantSapling = 27,
80        MossySnail = 28,
81    }
82}
83
84/// Data representing per-species generic data.
85#[derive(Clone, Debug, Deserialize, Serialize)]
86pub struct AllSpecies<SpeciesMeta> {
87    pub pig: SpeciesMeta,
88    pub fox: SpeciesMeta,
89    pub sheep: SpeciesMeta,
90    pub boar: SpeciesMeta,
91    pub jackalope: SpeciesMeta,
92    pub skunk: SpeciesMeta,
93    pub cat: SpeciesMeta,
94    pub batfox: SpeciesMeta,
95    pub raccoon: SpeciesMeta,
96    pub quokka: SpeciesMeta,
97    pub holladon: SpeciesMeta,
98    pub hyena: SpeciesMeta,
99    pub rabbit: SpeciesMeta,
100    pub truffler: SpeciesMeta,
101    pub frog: SpeciesMeta,
102    pub rat: SpeciesMeta,
103    pub axolotl: SpeciesMeta,
104    pub gecko: SpeciesMeta,
105    pub turtle: SpeciesMeta,
106    pub squirrel: SpeciesMeta,
107    pub fungome: SpeciesMeta,
108    pub porcupine: SpeciesMeta,
109    pub beaver: SpeciesMeta,
110    pub hare: SpeciesMeta,
111    pub dog: SpeciesMeta,
112    pub goat: SpeciesMeta,
113    pub seal: SpeciesMeta,
114    pub treant_sapling: SpeciesMeta,
115    pub mossy_snail: SpeciesMeta,
116}
117
118impl<'a, SpeciesMeta> core::ops::Index<&'a Species> for AllSpecies<SpeciesMeta> {
119    type Output = SpeciesMeta;
120
121    #[inline]
122    fn index(&self, &index: &'a Species) -> &Self::Output {
123        match index {
124            Species::Pig => &self.pig,
125            Species::Fox => &self.fox,
126            Species::Sheep => &self.sheep,
127            Species::Boar => &self.boar,
128            Species::Jackalope => &self.jackalope,
129            Species::Skunk => &self.skunk,
130            Species::Cat => &self.cat,
131            Species::Batfox => &self.batfox,
132            Species::Raccoon => &self.raccoon,
133            Species::Quokka => &self.quokka,
134            Species::Holladon => &self.holladon,
135            Species::Hyena => &self.hyena,
136            Species::Rabbit => &self.rabbit,
137            Species::Truffler => &self.truffler,
138            Species::Frog => &self.frog,
139            Species::Rat => &self.rat,
140            Species::Axolotl => &self.axolotl,
141            Species::Gecko => &self.gecko,
142            Species::Turtle => &self.turtle,
143            Species::Squirrel => &self.squirrel,
144            Species::Fungome => &self.fungome,
145            Species::Porcupine => &self.porcupine,
146            Species::Beaver => &self.beaver,
147            Species::Hare => &self.hare,
148            Species::Dog => &self.dog,
149            Species::Goat => &self.goat,
150            Species::Seal => &self.seal,
151            Species::TreantSapling => &self.treant_sapling,
152            Species::MossySnail => &self.mossy_snail,
153        }
154    }
155}
156
157pub const ALL_SPECIES: [Species; Species::NUM_KINDS] = Species::ALL;
158
159impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
160    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
161    type Item = Species;
162
163    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
164}
165
166// Renaming any enum entries here (re-ordering is fine) will require a
167// database migration to ensure pets correctly de-serialize on player login.
168enum_iter! {
169    ~const_array(ALL)
170    #[derive(
171        Copy,
172        Clone,
173        Debug,
174        Display,
175        EnumString,
176        PartialEq,
177        Eq,
178        PartialOrd,
179        Ord,
180        Hash,
181        Serialize,
182        Deserialize,
183    )]
184    #[repr(u32)]
185    pub enum BodyType {
186        Female = 0,
187        Male = 1,
188    }
189}
190pub const ALL_BODY_TYPES: [BodyType; BodyType::NUM_KINDS] = BodyType::ALL;