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
pub mod alpha;
pub mod aura;
pub mod breathe;
pub mod combomelee;
pub mod dash;
pub mod feed;
pub mod fly;
pub mod idle;
pub mod run;
pub mod selfbuff;
pub mod shockwave;
pub mod shoot;
pub mod stunned;
pub mod summon;
pub mod swim;

// Reexports
pub use self::{
    alpha::AlphaAnimation, aura::AuraAnimation, breathe::BreatheAnimation,
    combomelee::ComboAnimation, dash::DashAnimation, feed::FeedAnimation, fly::FlyAnimation,
    idle::IdleAnimation, run::RunAnimation, selfbuff::SelfBuffAnimation,
    shockwave::ShockwaveAnimation, shoot::ShootAnimation, stunned::StunnedAnimation,
    summon::SummonAnimation, swim::SwimAnimation,
};

use super::{make_bone, vek::*, FigureBoneData, Offsets, Skeleton};
use common::comp::{self};
use core::convert::TryFrom;

pub type Body = comp::bird_large::Body;

skeleton_impls!(struct BirdLargeSkeleton {
    + head,
    + beak,
    + neck,
    + chest,
    + tail_front,
    + tail_rear,
    + wing_in_l,
    + wing_in_r,
    + wing_mid_l,
    + wing_mid_r,
    + wing_out_l,
    + wing_out_r,
    + leg_l,
    + leg_r,
    + foot_l,
    + foot_r,
});

impl Skeleton for BirdLargeSkeleton {
    type Attr = SkeletonAttr;
    type Body = Body;

    const BONE_COUNT: usize = 16;
    #[cfg(feature = "use-dyn-lib")]
    const COMPUTE_FN: &'static [u8] = b"bird_large_compute_mats\0";

    #[cfg_attr(feature = "be-dyn-lib", export_name = "bird_large_compute_mats")]

    fn compute_matrices_inner(
        &self,
        base_mat: Mat4<f32>,
        buf: &mut [FigureBoneData; super::MAX_BONE_COUNT],
        body: Self::Body,
    ) -> Offsets {
        let base_mat = base_mat * Mat4::scaling_3d(SkeletonAttr::from(&body).scaler / 8.0);

        let chest_mat = base_mat * Mat4::<f32>::from(self.chest);
        let neck_mat = chest_mat * Mat4::<f32>::from(self.neck);
        let head_mat = neck_mat * Mat4::<f32>::from(self.head);
        let beak_mat = head_mat * Mat4::<f32>::from(self.beak);
        let tail_front_mat = chest_mat * Mat4::<f32>::from(self.tail_front);
        let tail_rear_mat = tail_front_mat * Mat4::<f32>::from(self.tail_rear);
        let wing_in_l_mat = chest_mat * Mat4::<f32>::from(self.wing_in_l);
        let wing_in_r_mat = chest_mat * Mat4::<f32>::from(self.wing_in_r);
        let wing_mid_l_mat = wing_in_l_mat * Mat4::<f32>::from(self.wing_mid_l);
        let wing_mid_r_mat = wing_in_r_mat * Mat4::<f32>::from(self.wing_mid_r);
        let wing_out_l_mat = wing_mid_l_mat * Mat4::<f32>::from(self.wing_out_l);
        let wing_out_r_mat = wing_mid_r_mat * Mat4::<f32>::from(self.wing_out_r);
        let leg_l_mat = base_mat * Mat4::<f32>::from(self.leg_l);
        let leg_r_mat = base_mat * Mat4::<f32>::from(self.leg_r);
        let foot_l_mat = leg_l_mat * Mat4::<f32>::from(self.foot_l);
        let foot_r_mat = leg_r_mat * Mat4::<f32>::from(self.foot_r);

        *(<&mut [_; Self::BONE_COUNT]>::try_from(&mut buf[0..Self::BONE_COUNT]).unwrap()) = [
            make_bone(head_mat),
            make_bone(beak_mat),
            make_bone(neck_mat),
            make_bone(chest_mat),
            make_bone(tail_front_mat),
            make_bone(tail_rear_mat),
            make_bone(wing_in_l_mat),
            make_bone(wing_in_r_mat),
            make_bone(wing_mid_l_mat),
            make_bone(wing_mid_r_mat),
            make_bone(wing_out_l_mat),
            make_bone(wing_out_r_mat),
            make_bone(leg_l_mat),
            make_bone(leg_r_mat),
            make_bone(foot_l_mat),
            make_bone(foot_r_mat),
        ];

        // Offset from the mounted bone's origin.
        let mount_position = (neck_mat * Vec4::from_point(mount_point(&body)))
            .homogenized()
            .xyz();
        // NOTE: We apply the ori from base_mat externally so we don't need to worry
        // about it here for now.
        let mount_orientation = self.neck.orientation;

        Offsets {
            viewpoint: Some((head_mat * Vec4::new(0.0, 3.0, 6.0, 1.0)).xyz()),
            mount_bone: Transform {
                position: mount_position,
                orientation: mount_orientation,
                scale: Vec3::one(),
            },
            ..Default::default()
        }
    }
}

pub struct SkeletonAttr {
    chest: (f32, f32),
    neck: (f32, f32),
    head: (f32, f32),
    beak: (f32, f32),
    tail_front: (f32, f32),
    tail_rear: (f32, f32),
    wing_in: (f32, f32, f32),
    wing_mid: (f32, f32, f32),
    wing_out: (f32, f32, f32),
    leg: (f32, f32, f32),
    foot: (f32, f32, f32),
    scaler: f32,
    feed: f32,
    wyvern: bool,
}

impl<'a> TryFrom<&'a comp::Body> for SkeletonAttr {
    type Error = ();

    fn try_from(body: &'a comp::Body) -> Result<Self, Self::Error> {
        match body {
            comp::Body::BirdLarge(body) => Ok(SkeletonAttr::from(body)),
            _ => Err(()),
        }
    }
}

impl Default for SkeletonAttr {
    fn default() -> Self {
        Self {
            chest: (0.0, 0.0),
            neck: (0.0, 0.0),
            head: (0.0, 0.0),
            beak: (0.0, 0.0),
            tail_front: (0.0, 0.0),
            tail_rear: (0.0, 0.0),
            wing_in: (0.0, 0.0, 0.0),
            wing_mid: (0.0, 0.0, 0.0),
            wing_out: (0.0, 0.0, 0.0),
            leg: (0.0, 0.0, 0.0),
            foot: (0.0, 0.0, 0.0),
            scaler: 0.0,
            feed: 0.0,
            wyvern: false,
        }
    }
}

impl<'a> From<&'a Body> for SkeletonAttr {
    fn from(body: &'a Body) -> Self {
        use comp::bird_large::Species::*;
        Self {
            chest: match (body.species, body.body_type) {
                (Phoenix, _) => (2.5, 16.0),
                (Cockatrice, _) => (2.5, 16.0),
                (Roc, _) => (2.5, 27.5),
                (FlameWyvern, _) => (2.5, 20.5),
                (CloudWyvern, _) => (2.5, 20.5),
                (FrostWyvern, _) => (2.5, 20.5),
                (SeaWyvern, _) => (2.5, 20.5),
                (WealdWyvern, _) => (2.5, 20.5),
            },
            neck: match (body.species, body.body_type) {
                (Phoenix, _) => (2.5, -5.5),
                (Cockatrice, _) => (5.0, -1.5),
                (Roc, _) => (9.5, -1.5),
                (FlameWyvern, _) => (11.0, -0.5),
                (CloudWyvern, _) => (11.0, -0.5),
                (FrostWyvern, _) => (11.0, -0.5),
                (SeaWyvern, _) => (11.0, -0.5),
                (WealdWyvern, _) => (11.0, -0.5),
            },
            head: match (body.species, body.body_type) {
                (Phoenix, _) => (6.0, 12.0),
                (Cockatrice, _) => (8.0, 4.5),
                (Roc, _) => (17.0, -3.5),
                (FlameWyvern, _) => (10.0, -1.5),
                (CloudWyvern, _) => (10.0, -1.5),
                (FrostWyvern, _) => (10.0, -1.5),
                (SeaWyvern, _) => (10.0, 2.5),
                (WealdWyvern, _) => (10.0, -1.5),
            },
            beak: match (body.species, body.body_type) {
                (Phoenix, _) => (5.0, 3.0),
                (Cockatrice, _) => (2.0, -3.0),
                (Roc, _) => (0.0, -3.0),
                (FlameWyvern, _) => (-3.0, 2.0),
                (CloudWyvern, _) => (-3.0, 2.0),
                (FrostWyvern, _) => (-3.0, 2.0),
                (SeaWyvern, _) => (-3.0, 2.0),
                (WealdWyvern, _) => (-3.0, 2.0),
            },
            tail_front: match (body.species, body.body_type) {
                (Phoenix, _) => (-9.5, -1.0),
                (Cockatrice, _) => (-5.0, -2.5),
                (Roc, _) => (-7.5, -3.5),
                (FlameWyvern, _) => (-10.0, -5.0),
                (CloudWyvern, _) => (-10.0, -5.0),
                (FrostWyvern, _) => (-10.0, -5.0),
                (SeaWyvern, _) => (-10.0, -5.0),
                (WealdWyvern, _) => (-10.0, -5.0),
            },
            tail_rear: match (body.species, body.body_type) {
                (Phoenix, _) => (-11.0, 0.0),
                (Cockatrice, _) => (-8.0, -3.0),
                (Roc, _) => (-8.0, -3.0),
                (FlameWyvern, _) => (-11.0, -1.0),
                (CloudWyvern, _) => (-11.0, -1.0),
                (FrostWyvern, _) => (-11.0, -1.0),
                (SeaWyvern, _) => (-11.0, -1.0),
                (WealdWyvern, _) => (-11.0, -1.0),
            },
            wing_in: match (body.species, body.body_type) {
                (Phoenix, _) => (3.0, 2.5, 2.0),
                (Cockatrice, _) => (3.5, 7.0, 3.5),
                (Roc, _) => (5.5, 7.5, -1.0),
                (FlameWyvern, _) => (6.5, 11.5, -2.0),
                (CloudWyvern, _) => (3.5, 11.5, -1.5),
                (FrostWyvern, _) => (5.0, 10.5, -1.5),
                (SeaWyvern, _) => (4.0, 11.5, -0.0),
                (WealdWyvern, _) => (5.0, 11.5, -1.0),
            },
            wing_mid: match (body.species, body.body_type) {
                (Phoenix, _) => (10.0, 1.0, 0.0),
                (Cockatrice, _) => (6.0, 0.0, 0.0),
                (Roc, _) => (12.0, 1.0, -0.5),
                (FlameWyvern, _) => (19.0, 11.5, 1.0),
                (CloudWyvern, _) => (19.0, 10.5, 1.0),
                (FrostWyvern, _) => (18.5, 11.5, 0.5),
                (SeaWyvern, _) => (19.0, 11.5, 0.5),
                (WealdWyvern, _) => (19.0, 11.5, 0.0),
            },
            wing_out: match (body.species, body.body_type) {
                (Phoenix, _) => (7.0, 2.0, 1.5),
                (Cockatrice, _) => (4.0, -1.0, 1.0),
                (Roc, _) => (10.0, -2.0, 0.0),
                (FlameWyvern, _) => (11.0, -1.0, 0.0),
                (CloudWyvern, _) => (11.0, -2.0, 0.0),
                (FrostWyvern, _) => (10.0, -1.5, 0.5),
                (SeaWyvern, _) => (12.0, -1.0, 0.0),
                (WealdWyvern, _) => (16.0, -4.0, -1.0),
            },
            leg: match (body.species, body.body_type) {
                (Phoenix, _) => (4.0, 1.5, 12.0),
                (Cockatrice, _) => (3.5, 2.5, 13.0),
                (Roc, _) => (5.5, -1.5, 17.5),
                (FlameWyvern, _) => (5.5, 2.0, 15.5),
                (CloudWyvern, _) => (5.5, 2.0, 15.5),
                (FrostWyvern, _) => (5.5, 2.0, 15.5),
                (SeaWyvern, _) => (5.5, 2.0, 15.5),
                (WealdWyvern, _) => (5.5, 2.0, 15.5),
            },
            foot: match (body.species, body.body_type) {
                (Phoenix, _) => (0.5, -0.5, -2.5),
                (Cockatrice, _) => (0.5, -3.0, -3.0),
                (Roc, _) => (2.5, -2.5, -5.5),
                (FlameWyvern, _) => (0.5, 0.0, -3.5),
                (CloudWyvern, _) => (0.5, 0.0, -3.5),
                (FrostWyvern, _) => (0.5, 0.0, -3.5),
                (SeaWyvern, _) => (0.5, 0.0, -3.5),
                (WealdWyvern, _) => (0.5, 0.0, -3.5),
            },
            scaler: match (body.species, body.body_type) {
                (Phoenix, _) => 1.0,
                (Cockatrice, _) => 1.0,
                (Roc, _) => 1.0,
                (FlameWyvern, _)
                | (CloudWyvern, _)
                | (FrostWyvern, _)
                | (SeaWyvern, _)
                | (WealdWyvern, _) => 1.0,
            },
            feed: match (body.species, body.body_type) {
                (Phoenix, _) => -0.65,
                (Cockatrice, _) => -0.5,
                (Roc, _) => -0.4,
                (FlameWyvern, _)
                | (CloudWyvern, _)
                | (FrostWyvern, _)
                | (SeaWyvern, _)
                | (WealdWyvern, _) => -0.65,
            },
            wyvern: matches!(
                (body.species, body.body_type),
                (FlameWyvern, _)
                    | (CloudWyvern, _)
                    | (FrostWyvern, _)
                    | (SeaWyvern, _)
                    | (WealdWyvern, _)
            ),
        }
    }
}

fn mount_point(body: &Body) -> Vec3<f32> {
    use comp::bird_large::Species::*;
    match (body.species, body.body_type) {
        (Phoenix, _) => (0.0, -2.0, 6.0),
        (Cockatrice, _) => (0.0, 0.0, 6.0),
        (Roc, _) => (0.0, 6.0, 3.0),
        (FlameWyvern, _) => (0.0, 0.0, 2.5),
        (FrostWyvern, _) => (0.0, 2.0, 3.0),
        (CloudWyvern, _) => (0.0, 1.0, 3.0),
        (SeaWyvern, _) => (0.0, -3.0, 4.0),
        (WealdWyvern, _) => (0.0, 0.0, 5.0),
    }
    .into()
}