1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use crate::{
    comp::{Collider, Density, Mass},
    consts::{AIR_DENSITY, WATER_DENSITY},
    make_case_elim,
    terrain::{Block, BlockKind, SpriteKind},
};
use rand::prelude::SliceRandom;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use vek::*;

pub const ALL_BODIES: [Body; 6] = [
    Body::DefaultAirship,
    Body::AirBalloon,
    Body::SailBoat,
    Body::Galleon,
    Body::Skiff,
    Body::Submarine,
];

pub const ALL_AIRSHIPS: [Body; 2] = [Body::DefaultAirship, Body::AirBalloon];
pub const ALL_SHIPS: [Body; 6] = [
    Body::SailBoat,
    Body::Galleon,
    Body::Skiff,
    Body::Submarine,
    Body::Carriage,
    Body::Cart,
];

make_case_elim!(
    body,
    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
    #[repr(u32)]
    pub enum Body {
        DefaultAirship = 0,
        AirBalloon = 1,
        SailBoat = 2,
        Galleon = 3,
        Volume = 4,
        Skiff = 5,
        Submarine = 6,
        Carriage = 7,
        Cart = 8,
    }
);

impl From<Body> for super::Body {
    fn from(body: Body) -> Self { super::Body::Ship(body) }
}

impl Body {
    pub fn random() -> Self {
        let mut rng = rand::thread_rng();
        Self::random_with(&mut rng)
    }

    pub fn random_with(rng: &mut impl rand::Rng) -> Self { *ALL_BODIES.choose(rng).unwrap() }

    pub fn random_airship_with(rng: &mut impl rand::Rng) -> Self {
        *ALL_AIRSHIPS.choose(rng).unwrap()
    }

    pub fn random_ship_with(rng: &mut impl rand::Rng) -> Self { *ALL_SHIPS.choose(rng).unwrap() }

    /// Return the structure manifest that this ship uses. `None` means that it
    /// should be derived from the collider.
    pub fn manifest_entry(&self) -> Option<&'static str> {
        match self {
            Body::DefaultAirship => Some("airship_human.structure"),
            Body::AirBalloon => Some("air_balloon.structure"),
            Body::SailBoat => Some("sail_boat.structure"),
            Body::Galleon => Some("galleon.structure"),
            Body::Skiff => Some("skiff.structure"),
            Body::Submarine => Some("submarine.structure"),
            Body::Carriage => Some("carriage.structure"),
            Body::Cart => Some("cart.structure"),
            Body::Volume => None,
        }
    }

    pub fn dimensions(&self) -> Vec3<f32> {
        match self {
            Body::DefaultAirship | Body::Volume => Vec3::new(25.0, 50.0, 40.0),
            Body::AirBalloon => Vec3::new(25.0, 50.0, 40.0),
            Body::SailBoat => Vec3::new(12.0, 32.0, 6.0),
            Body::Galleon => Vec3::new(14.0, 48.0, 10.0),
            Body::Skiff => Vec3::new(7.0, 15.0, 10.0),
            Body::Submarine => Vec3::new(2.0, 15.0, 8.0),
            Body::Carriage => Vec3::new(5.0, 12.0, 2.0),
            Body::Cart => Vec3::new(3.0, 6.0, 1.0),
        }
    }

    fn balloon_vol(&self) -> f32 {
        match self {
            Body::DefaultAirship | Body::AirBalloon | Body::Volume => {
                let spheroid_vol = |equat_d: f32, polar_d: f32| -> f32 {
                    (std::f32::consts::PI / 6.0) * equat_d.powi(2) * polar_d
                };
                let dim = self.dimensions();
                spheroid_vol(dim.z, dim.y)
            },
            _ => 0.0,
        }
    }

    fn hull_vol(&self) -> f32 {
        // height from bottom of keel to deck
        let deck_height = 10_f32;
        let dim = self.dimensions();
        (std::f32::consts::PI / 6.0) * (deck_height * 1.5).powi(2) * dim.y
    }

    pub fn hull_density(&self) -> Density {
        let oak_density = 600_f32;
        let ratio = 0.1;
        Density(ratio * oak_density + (1.0 - ratio) * AIR_DENSITY)
    }

    pub fn density(&self) -> Density {
        match self {
            Body::DefaultAirship | Body::AirBalloon | Body::Volume => Density(AIR_DENSITY),
            Body::Submarine => Density(WATER_DENSITY), // Neutrally buoyant
            Body::Carriage => Density(WATER_DENSITY * 0.5),
            Body::Cart => Density(500.0 / self.dimensions().product()), /* Carts get a constant */
            // mass
            _ => Density(AIR_DENSITY * 0.95 + WATER_DENSITY * 0.05), /* Most boats should be very
                                                                      * buoyant */
        }
    }

    pub fn mass(&self) -> Mass {
        if self.can_fly() {
            Mass((self.hull_vol() + self.balloon_vol()) * self.density().0)
        } else {
            Mass(self.density().0 * self.dimensions().product())
        }
    }

    pub fn can_fly(&self) -> bool {
        matches!(self, Body::DefaultAirship | Body::AirBalloon | Body::Volume)
    }

    pub fn flying_height(&self) -> f32 { if self.can_fly() { 200.0 } else { 0.0 } }

    pub fn has_water_thrust(&self) -> bool {
        matches!(self, Body::SailBoat | Body::Galleon | Body::Skiff)
    }

    pub fn has_wheels(&self) -> bool { matches!(self, Body::Carriage | Body::Cart) }

    pub fn make_collider(&self) -> Collider {
        match self.manifest_entry() {
            Some(manifest_entry) => Collider::Voxel {
                id: manifest_entry.to_string(),
            },
            None => {
                use rand::prelude::*;
                let sz = Vec3::broadcast(11);
                Collider::Volume(Arc::new(figuredata::VoxelCollider::from_fn(sz, |_pos| {
                    if thread_rng().gen_bool(0.25) {
                        Block::new(BlockKind::Rock, Rgb::new(255, 0, 0))
                    } else {
                        Block::air(SpriteKind::Empty)
                    }
                })))
            },
        }
    }

    /// Max speed in block/s
    pub fn get_speed(&self) -> f32 {
        match self {
            Body::DefaultAirship => 7.0,
            Body::AirBalloon => 8.0,
            Body::SailBoat => 5.0,
            Body::Galleon => 6.0,
            Body::Skiff => 6.0,
            Body::Submarine => 4.0,
            _ => 10.0,
        }
    }
}

/// Terrain is 11.0 scale relative to small-scale voxels,
/// airship scale is multiplied by 11 to reach terrain scale.
pub const AIRSHIP_SCALE: f32 = 11.0;

/// Duplicate of some of the things defined in `voxygen::scene::figure::load` to
/// avoid having to refactor all of that to `common` for using voxels as
/// collider geometry
pub mod figuredata {
    use crate::{
        assets::{self, AssetExt, AssetHandle, DotVoxAsset, Ron},
        figure::TerrainSegment,
        terrain::{
            block::{Block, BlockKind},
            sprite::SpriteKind,
            structure::load_base_structure,
        },
    };
    use hashbrown::HashMap;
    use lazy_static::lazy_static;
    use serde::{Deserialize, Serialize};
    use vek::{Rgb, Vec3};

    #[derive(Deserialize)]
    pub struct VoxSimple(pub String);

    #[derive(Deserialize)]
    pub struct ShipCentralSpec(pub HashMap<super::Body, SidedShipCentralVoxSpec>);

    #[derive(Deserialize)]
    pub enum DeBlock {
        Block(BlockKind),
        Air(SpriteKind, #[serde(default)] u8),
        Water(SpriteKind, #[serde(default)] u8),
    }

    impl DeBlock {
        fn to_block(&self, color: Rgb<u8>) -> Block {
            match *self {
                DeBlock::Block(block) => Block::new(block, color),
                DeBlock::Air(sprite, ori) => {
                    let block = Block::new(BlockKind::Air, color).with_sprite(sprite);
                    block.with_ori(ori).unwrap_or(block)
                },
                DeBlock::Water(sprite, ori) => {
                    let block = Block::new(BlockKind::Water, color).with_sprite(sprite);
                    block.with_ori(ori).unwrap_or(block)
                },
            }
        }
    }

    #[derive(Deserialize)]
    pub struct SidedShipCentralVoxSpec {
        pub bone0: ShipCentralSubSpec,
        pub bone1: ShipCentralSubSpec,
        pub bone2: ShipCentralSubSpec,
        pub bone3: ShipCentralSubSpec,

        // TODO: Use StructureBlock here instead. Which would require passing `IndexRef` and
        // `Calendar` when loading the voxel colliders, which wouldn't work while it's stored in a
        // static.
        #[serde(default)]
        pub custom_indices: HashMap<u8, DeBlock>,
    }

    #[derive(Deserialize)]
    pub struct ShipCentralSubSpec {
        pub offset: [f32; 3],
        pub central: VoxSimple,
        #[serde(default)]
        pub model_index: u32,
    }

    /// manual instead of through `make_vox_spec!` so that it can be in `common`
    #[derive(Clone)]
    pub struct ShipSpec {
        pub central: AssetHandle<Ron<ShipCentralSpec>>,
        pub colliders: HashMap<String, VoxelCollider>,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct VoxelCollider {
        pub(super) dyna: TerrainSegment,
        pub translation: Vec3<f32>,
        /// This value should be incremented every time the volume is mutated
        /// and can be used to keep track of volume changes.
        pub mut_count: usize,
    }

    impl VoxelCollider {
        pub fn from_fn<F: FnMut(Vec3<i32>) -> Block>(sz: Vec3<u32>, f: F) -> Self {
            Self {
                dyna: TerrainSegment::from_fn(sz, (), f),
                translation: -sz.map(|e| e as f32) / 2.0,
                mut_count: 0,
            }
        }

        pub fn volume(&self) -> &TerrainSegment { &self.dyna }
    }

    impl assets::Compound for ShipSpec {
        fn load(
            cache: assets::AnyCache,
            _: &assets::SharedString,
        ) -> Result<Self, assets::BoxedError> {
            let manifest: AssetHandle<Ron<ShipCentralSpec>> =
                AssetExt::load("common.manifests.ship_manifest")?;
            let mut colliders = HashMap::new();
            for (_, spec) in (manifest.read().0).0.iter() {
                for (index, bone) in [&spec.bone0, &spec.bone1, &spec.bone2, &spec.bone3]
                    .iter()
                    .enumerate()
                {
                    // TODO: Currently both client and server load models and manifests from
                    // "common.voxel.". In order to support CSG procedural airships, we probably
                    // need to load them in the server and sync them as an ECS resource.
                    let vox =
                        cache.load::<DotVoxAsset>(&["common.voxel.", &bone.central.0].concat())?;

                    let base_structure = load_base_structure(&vox.read().0, |col| col);
                    let dyna = base_structure.vol.map_into(|cell| {
                        if let Some(i) = cell {
                            let color = base_structure.palette[u8::from(i) as usize];
                            if let Some(block) = spec.custom_indices.get(&i.get())
                                && index == 0
                            {
                                block.to_block(color)
                            } else {
                                Block::new(BlockKind::Misc, color)
                            }
                        } else {
                            Block::empty()
                        }
                    });
                    let collider = VoxelCollider {
                        dyna,
                        translation: Vec3::from(bone.offset),
                        mut_count: 0,
                    };
                    colliders.insert(bone.central.0.clone(), collider);
                }
            }
            Ok(ShipSpec {
                central: manifest,
                colliders,
            })
        }
    }

    lazy_static! {
        // TODO: Load this from the ECS as a resource, and maybe make it more general than ships
        // (although figuring out how to keep the figure bones in sync with the terrain offsets seems
        // like a hard problem if they're not the same manifest)
        pub static ref VOXEL_COLLIDER_MANIFEST: AssetHandle<ShipSpec> = AssetExt::load_expect("common.manifests.ship_manifest");
    }

    #[test]
    fn test_ship_manifest_entries() {
        for body in super::ALL_BODIES {
            if let Some(entry) = body.manifest_entry() {
                assert!(
                    VOXEL_COLLIDER_MANIFEST
                        .read()
                        .colliders
                        .get(entry)
                        .is_some()
                );
            }
        }
    }
}