veloren_common/comp/body/
quadruped_small.rs

1use crate::{make_case_elim, make_proj_elim};
2use rand::{seq::SliceRandom, thread_rng};
3use serde::{Deserialize, Serialize};
4use strum::{Display, EnumString};
5
6make_proj_elim!(
7    body,
8    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
9    pub struct Body {
10        pub species: Species,
11        pub body_type: BodyType,
12    }
13);
14
15impl Body {
16    pub fn random() -> Self {
17        let mut rng = thread_rng();
18        let species = *ALL_SPECIES.choose(&mut rng).unwrap();
19        Self::random_with(&mut rng, &species)
20    }
21
22    #[inline]
23    pub fn random_with(rng: &mut impl rand::Rng, &species: &Species) -> Self {
24        let body_type = *ALL_BODY_TYPES.choose(rng).unwrap();
25        Self { species, body_type }
26    }
27}
28
29impl From<Body> for super::Body {
30    fn from(body: Body) -> Self { super::Body::QuadrupedSmall(body) }
31}
32
33// Renaming any enum entries here (re-ordering is fine) will require a
34// database migration to ensure pets correctly de-serialize on player login.
35#[derive(
36    Copy,
37    Clone,
38    Debug,
39    Display,
40    EnumString,
41    PartialEq,
42    Eq,
43    PartialOrd,
44    Ord,
45    Hash,
46    Serialize,
47    Deserialize,
48)]
49#[repr(u32)]
50pub enum Species {
51    Pig = 0,
52    Fox = 1,
53    Sheep = 2,
54    Boar = 3,
55    Jackalope = 4,
56    Skunk = 5,
57    Cat = 6,
58    Batfox = 7,
59    Raccoon = 8,
60    Quokka = 9,
61    Goat = 10,
62    Holladon = 11,
63    Hyena = 12,
64    Rabbit = 13,
65    Truffler = 14,
66    Frog = 15,
67    Rat = 16,
68    Axolotl = 17,
69    Gecko = 18,
70    Turtle = 19,
71    Squirrel = 20,
72    Fungome = 21,
73    Porcupine = 22,
74    Beaver = 23,
75    Hare = 24,
76    Dog = 25,
77    Seal = 26,
78    TreantSapling = 27,
79    MossySnail = 28,
80}
81
82/// Data representing per-species generic data.
83///
84/// NOTE: Deliberately don't (yet?) implement serialize.
85#[derive(Clone, Debug, Deserialize)]
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; 29] = [
158    Species::Pig,
159    Species::Fox,
160    Species::Sheep,
161    Species::Boar,
162    Species::Jackalope,
163    Species::Skunk,
164    Species::Cat,
165    Species::Batfox,
166    Species::Raccoon,
167    Species::Quokka,
168    Species::Holladon,
169    Species::Hyena,
170    Species::Rabbit,
171    Species::Truffler,
172    Species::Frog,
173    Species::Rat,
174    Species::Axolotl,
175    Species::Gecko,
176    Species::Turtle,
177    Species::Squirrel,
178    Species::Fungome,
179    Species::Porcupine,
180    Species::Beaver,
181    Species::Hare,
182    Species::Dog,
183    Species::Goat,
184    Species::Seal,
185    Species::TreantSapling,
186    Species::MossySnail,
187];
188
189impl<'a, SpeciesMeta: 'a> IntoIterator for &'a AllSpecies<SpeciesMeta> {
190    type IntoIter = std::iter::Copied<std::slice::Iter<'static, Self::Item>>;
191    type Item = Species;
192
193    fn into_iter(self) -> Self::IntoIter { ALL_SPECIES.iter().copied() }
194}
195
196// Renaming any enum entries here (re-ordering is fine) will require a
197// database migration to ensure pets correctly de-serialize on player login.
198make_case_elim!(
199    body_type,
200    #[derive(
201        Copy,
202        Clone,
203        Debug,
204        Display,
205        EnumString,
206        PartialEq,
207        Eq,
208        PartialOrd,
209        Ord,
210        Hash,
211        Serialize,
212        Deserialize,
213    )]
214    #[repr(u32)]
215    pub enum BodyType {
216        Female = 0,
217        Male = 1,
218    }
219);
220
221pub const ALL_BODY_TYPES: [BodyType; 2] = [BodyType::Female, BodyType::Male];