//! `SoundRand` type.

use crate::durandal::{err::*,
                      fixed::{Angle, Fixed}};
use bitflags::bitflags;

/// Reads a `bonk` chunk.
pub fn read(b: &[u8]) -> ResultS<(SoundRand, usize)>
{
   read_data! {
      endian: BIG, buf: b, size: 32, start: 0, data {
         let flags   = u16[0] flag SoundRandFlags;
         let index   = u16[2];
         let vol_nrm = u16[4];
         let vol_dta = u16[6];
         let prd_nrm = u16[8];
         let prd_dta = u16[10];
         let yaw_nrm = Angle[12];
         let yaw_dta = Angle[14];
         let pit_nrm = Fixed[16];
         let pit_dta = Fixed[20];
      }
   }

   Ok((SoundRand{flags, index, vol_nrm, vol_dta, prd_nrm, prd_dta, yaw_nrm,
                 yaw_dta, pit_nrm, pit_dta}, 32))
}

/// A randomly played sound definition.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub struct SoundRand
{
   pub flags:   SoundRandFlags,
   pub index:   u16,
   pub vol_nrm: u16,
   pub vol_dta: u16,
   pub prd_nrm: u16,
   pub prd_dta: u16,
   pub yaw_nrm: Angle,
   pub yaw_dta: Angle,
   pub pit_nrm: Fixed,
   pub pit_dta: Fixed,
}

bitflags! {
   /// Flags for `SoundRand`.
   #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
   pub struct SoundRandFlags: u16
   {
      const NO_DIRECTION = 1;
   }
}

// EOF
