veloren_common/comp/body/
object.rs

1use crate::{
2    comp::{Density, Mass},
3    consts::{AIR_DENSITY, IRON_DENSITY, WATER_DENSITY},
4};
5use common_base::enum_iter;
6use rand::{prelude::IndexedRandom, rng};
7use serde::{Deserialize, Serialize};
8use vek::Vec3;
9
10enum_iter! {
11    ~const_array(ALL)
12    #[derive(
13        Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
14    )]
15    #[repr(u32)]
16    pub enum Body {
17        Arrow = 0,
18        Bomb = 1,
19        Scarecrow = 2,
20        Pumpkin = 3,
21        Campfire = 4,
22        BoltFire = 5,
23        ArrowSnake = 6,
24        CampfireLit = 7,
25        BoltFireBig = 8,
26        TrainingDummy = 9,
27        FireworkBlue = 10,
28        FireworkGreen = 11,
29        FireworkPurple = 12,
30        FireworkRed = 13,
31        FireworkWhite = 14,
32        FireworkYellow = 15,
33        MultiArrow = 16,
34        BoltNature = 17,
35        ToughMeat = 18,
36        BeastMeat = 19,
37        Crossbow = 20,
38        ArrowTurret = 21,
39        ClayRocket = 22,
40        HaniwaSentry = 23,
41        SeaLantern = 24,
42        Snowball = 25,
43        Tornado = 26,
44        Apple = 27,
45        Hive = 28,
46        Coconut = 29,
47        SpitPoison = 30,
48        BoltIcicle = 31,
49        Dart = 32,
50        GnarlingTotemRed = 33,
51        GnarlingTotemGreen = 34,
52        GnarlingTotemWhite = 35,
53        DagonBomb = 36,
54        BarrelOrgan = 37,
55        IceBomb = 38,
56        SpectralSwordSmall = 39,
57        SpectralSwordLarge = 40,
58        LaserBeam = 41,
59        AdletSpear = 42,
60        AdletTrap = 43,
61        Flamethrower = 44,
62        Mine = 45,
63        LightningBolt = 46,
64        SpearIcicle = 47,
65        Portal = 48,
66        PortalActive = 49,
67        FieryTornado = 50,
68        FireRainDrop = 51,
69        ArrowClay = 52,
70        GrenadeClay = 53,
71        Pebble = 54,
72        LaserBeamSmall = 55,
73        TerracottaStatue = 56,
74        TerracottaDemolisherBomb = 57,
75        BoltBesieger = 58,
76        SurpriseEgg = 59,
77        BubbleBomb = 60,
78        IronPikeBomb = 61,
79        Lavathrower = 62,
80        PoisonBall = 63,
81        StrigoiHead = 64,
82        HarlequinDagger = 65,
83        BloodBomb = 66,
84        MinotaurAxe = 67,
85        BorealTrap = 68,
86        Crux = 69,
87        ArrowHeavy = 70,
88    }
89}
90
91impl Body {
92    pub fn random() -> Self {
93        let mut rng = rng();
94        *ALL_OBJECTS.choose(&mut rng).unwrap()
95    }
96}
97
98pub const ALL_OBJECTS: [Body; Body::NUM_KINDS] = Body::ALL;
99
100impl From<Body> for super::Body {
101    fn from(body: Body) -> Self { super::Body::Object(body) }
102}
103
104impl Body {
105    pub fn to_string(self) -> &'static str {
106        match self {
107            Body::Arrow => "arrow",
108            Body::Bomb => "bomb",
109            Body::Scarecrow => "scarecrow",
110            Body::Pumpkin => "pumpkin",
111            Body::Campfire => "campfire",
112            Body::CampfireLit => "campfire_lit",
113            Body::BoltFire => "bolt_fire",
114            Body::BoltFireBig => "bolt_fire_big",
115            Body::ArrowSnake => "arrow_snake",
116            Body::TrainingDummy => "training_dummy",
117            Body::FireworkBlue => "firework_blue",
118            Body::FireworkGreen => "firework_green",
119            Body::FireworkPurple => "firework_purple",
120            Body::FireworkRed => "firework_red",
121            Body::FireworkWhite => "firework_white",
122            Body::FireworkYellow => "firework_yellow",
123            Body::MultiArrow => "multi_arrow",
124            Body::BoltNature => "bolt_nature",
125            Body::ToughMeat => "tough_meat",
126            Body::BeastMeat => "beast_meat",
127            Body::Crossbow => "crossbow",
128            Body::ArrowTurret => "arrow_turret",
129            Body::ClayRocket => "clay_rocket",
130            Body::HaniwaSentry => "haniwa_sentry",
131            Body::SeaLantern => "sea_lantern",
132            Body::Snowball => "snowball",
133            Body::Tornado => "tornado",
134            Body::Apple => "apple",
135            Body::Hive => "hive",
136            Body::Coconut => "coconut",
137            Body::SpitPoison => "spit_poison",
138            Body::BoltIcicle => "bolt_icicle",
139            Body::Dart => "dart",
140            Body::GnarlingTotemRed => "gnarling_totem_red",
141            Body::GnarlingTotemGreen => "gnarling_totem_green",
142            Body::GnarlingTotemWhite => "gnarling_totem_white",
143            Body::DagonBomb => "dagon_bomb",
144            Body::TerracottaDemolisherBomb => "terracotta_demolisher_bomb",
145            Body::BarrelOrgan => "barrel_organ",
146            Body::IceBomb => "ice_bomb",
147            Body::SpectralSwordSmall => "spectral_sword_small",
148            Body::SpectralSwordLarge => "spectral_sword_large",
149            Body::LaserBeam => "laser_beam",
150            Body::LaserBeamSmall => "laser_beam_small",
151            Body::AdletSpear => "adlet_spear",
152            Body::AdletTrap => "adlet_trap",
153            Body::Flamethrower => "flamethrower",
154            Body::Lavathrower => "lavathrower",
155            Body::Mine => "mine",
156            Body::LightningBolt => "lightning_bolt",
157            Body::SpearIcicle => "spear_icicle",
158            Body::Portal => "portal",
159            Body::PortalActive => "portal_active",
160            Body::FieryTornado => "fiery_tornado",
161            Body::FireRainDrop => "fire_rain_drop",
162            Body::ArrowClay => "arrow_clay",
163            Body::GrenadeClay => "grenade_clay",
164            Body::Pebble => "pebble",
165            Body::TerracottaStatue => "terracotta_statue",
166            Body::BoltBesieger => "besieger_bolt",
167            Body::SurpriseEgg => "surprise_egg",
168            Body::BubbleBomb => "bubble_bomb",
169            Body::IronPikeBomb => "iron_pike_bomb",
170            Body::PoisonBall => "poison_ball",
171            Body::StrigoiHead => "strigoi_head",
172            Body::HarlequinDagger => "harlequin_dagger",
173            Body::BloodBomb => "blood_bomb",
174            Body::MinotaurAxe => "minotaur_axe",
175            Body::BorealTrap => "boreal_trap",
176            Body::Crux => "crux",
177            Body::ArrowHeavy => "heavy_arrow",
178        }
179    }
180
181    pub fn density(&self) -> Density {
182        let density = match self {
183            Body::Arrow
184            | Body::ArrowSnake
185            | Body::ArrowTurret
186            | Body::MultiArrow
187            | Body::ArrowClay
188            | Body::BoltBesieger
189            | Body::Dart
190            | Body::DagonBomb
191            | Body::TerracottaDemolisherBomb
192            | Body::SpectralSwordSmall
193            | Body::SpectralSwordLarge
194            | Body::AdletSpear
195            | Body::HarlequinDagger
196            | Body::AdletTrap
197            | Body::Flamethrower
198            | Body::Lavathrower
199            | Body::BorealTrap
200            | Body::BloodBomb
201            | Body::ArrowHeavy => 500.0,
202            Body::Bomb | Body::Mine | Body::SurpriseEgg => 2000.0, /* I have no idea what it's */
203            // supposed to be
204            Body::Scarecrow => 900.0,
205            Body::TrainingDummy => 2000.0,
206            Body::Snowball => 0.9 * WATER_DENSITY,
207            Body::Pebble => 1000.0,
208            Body::Crux => AIR_DENSITY,
209            // let them sink
210            _ => 1.1 * WATER_DENSITY,
211        };
212
213        Density(density)
214    }
215
216    pub fn mass(&self) -> Mass {
217        let m = match self {
218            // MultiArrow is one of several arrows, not several arrows combined
219            Body::Arrow | Body::ArrowSnake | Body::ArrowTurret | Body::MultiArrow | Body::Dart => {
220                0.003
221            },
222            Body::SpectralSwordSmall => 0.5,
223            Body::SpectralSwordLarge => 50.0,
224            Body::BoltFire
225            | Body::BoltFireBig
226            | Body::BoltNature
227            | Body::BoltIcicle
228            | Body::FireRainDrop
229            | Body::ArrowClay
230            | Body::Pebble
231            | Body::BubbleBomb
232            | Body::IronPikeBomb
233            | Body::BoltBesieger
234            | Body::PoisonBall
235            | Body::ArrowHeavy => 1.0,
236            Body::SpitPoison => 100.0,
237            Body::Bomb
238            | Body::DagonBomb
239            | Body::SurpriseEgg
240            | Body::TerracottaDemolisherBomb
241            | Body::BloodBomb => {
242                0.5 * IRON_DENSITY * std::f32::consts::PI / 6.0 * self.dimensions().x.powi(3)
243            },
244            Body::Campfire | Body::CampfireLit | Body::BarrelOrgan | Body::TerracottaStatue => {
245                300.0
246            },
247            Body::Crossbow => 200.0,
248            Body::Flamethrower | Body::Lavathrower => 200.0,
249            Body::FireworkBlue
250            | Body::FireworkGreen
251            | Body::FireworkPurple
252            | Body::FireworkRed
253            | Body::FireworkWhite
254            | Body::FireworkYellow => 1.0,
255            Body::ToughMeat => 50.0,
256            Body::BeastMeat => 50.0,
257            Body::Pumpkin | Body::StrigoiHead => 10.0,
258            Body::Scarecrow => 50.0,
259            Body::TrainingDummy => 60.0,
260            Body::ClayRocket | Body::GrenadeClay => 50.0,
261            Body::HaniwaSentry => 300.0,
262            Body::SeaLantern => 1000.0,
263            Body::MinotaurAxe => 100000.0,
264            Body::Snowball => 7360.0, // 2.5 m diamter
265            Body::Tornado | Body::FieryTornado => 50.0,
266            Body::Apple => 2.0,
267            Body::Hive => 2.0,
268            Body::Coconut => 2.0,
269            Body::GnarlingTotemRed | Body::GnarlingTotemGreen | Body::GnarlingTotemWhite => 100.0,
270            Body::IceBomb => 12298.0, // 2.5 m diamter but ice
271            Body::LaserBeam | Body::LaserBeamSmall => 80000.0,
272            Body::AdletSpear => 1.5,
273            Body::AdletTrap => 10.0,
274            Body::Mine => 100.0,
275            Body::HarlequinDagger => 1.5,
276            Body::BorealTrap => 10.0,
277            Body::LightningBolt | Body::SpearIcicle => 20000.0,
278            Body::Portal | Body::PortalActive => 10.0, // I dont know really
279            Body::Crux => 100.0,
280        };
281
282        Mass(m)
283    }
284
285    pub fn dimensions(&self) -> Vec3<f32> {
286        match self {
287            Body::Arrow
288            | Body::ArrowSnake
289            | Body::MultiArrow
290            | Body::ArrowTurret
291            | Body::ArrowClay
292            | Body::BoltBesieger
293            | Body::Dart
294            | Body::HarlequinDagger
295            | Body::AdletSpear => Vec3::new(0.01, 0.8, 0.01),
296            Body::AdletTrap => Vec3::new(1.0, 0.6, 0.3),
297            Body::BoltFire | Body::PoisonBall => Vec3::new(0.1, 0.1, 0.1),
298            Body::SpectralSwordSmall => Vec3::new(0.2, 0.9, 0.1),
299            Body::SpectralSwordLarge => Vec3::new(0.2, 1.5, 0.1),
300            Body::Crossbow => Vec3::new(3.0, 3.0, 1.5),
301            Body::Flamethrower => Vec3::new(3.0, 3.0, 2.5),
302            Body::Lavathrower => Vec3::new(3.0, 3.0, 2.0),
303            Body::HaniwaSentry => Vec3::new(0.8, 0.8, 1.4),
304            Body::SeaLantern => Vec3::new(0.8, 0.8, 1.4),
305            Body::Snowball => Vec3::broadcast(2.5),
306            Body::Tornado | Body::FieryTornado => Vec3::new(2.0, 2.0, 3.4),
307            Body::TrainingDummy => Vec3::new(1.5, 1.5, 3.0),
308            Body::BorealTrap => Vec3::new(1.0, 0.6, 0.3),
309            Body::GnarlingTotemRed | Body::GnarlingTotemGreen | Body::GnarlingTotemWhite => {
310                Vec3::new(0.8, 0.8, 1.4)
311            },
312            Body::BarrelOrgan => Vec3::new(4.0, 2.0, 3.0),
313            Body::TerracottaStatue => Vec3::new(5.0, 5.0, 5.0),
314            Body::IceBomb => Vec3::broadcast(2.5),
315            Body::LaserBeam => Vec3::new(8.0, 8.0, 8.0),
316            Body::LaserBeamSmall => Vec3::new(1.0, 1.0, 1.0),
317            Body::Mine => Vec3::new(0.8, 0.8, 0.5),
318            Body::LightningBolt | Body::SpearIcicle => Vec3::new(1.0, 1.0, 1.0),
319            Body::FireRainDrop => Vec3::new(0.01, 0.01, 0.02),
320            Body::Pebble => Vec3::new(0.4, 0.4, 0.4),
321            Body::MinotaurAxe => Vec3::new(5.0, 5.0, 5.0),
322            Body::Crux => Vec3::new(2.0, 2.0, 2.0),
323            Body::ArrowHeavy => Vec3::new(0.1, 0.9, 0.1),
324            // FIXME: this *must* be exhaustive match
325            _ => Vec3::broadcast(0.5),
326        }
327    }
328}