//! `Monster` type.

use crate::durandal::{bin::OptU16, err::*, fixed::Unit};
use std::convert::TryFrom;

/// Reads a `Trigger` object.
pub fn read(b: &[u8]) -> ResultS<Trigger>
{
   read_data! {
      endian: BIG, buf: b, size: 36, start: 0, data {
         let magazine    = u16[0];
         let typ_ammo    = OptU16[2];
         let tic_round   = OptU16[4];
         let tic_recover = u16[6];
         let tic_charge  = u16[8];
         let recoil      = Unit[10];
         let snd_fire    = OptU16[12];
         let snd_click   = OptU16[14];
         let snd_charge  = OptU16[16];
         let snd_casing  = OptU16[18];
         let snd_reload  = OptU16[20];
         let snd_charged = OptU16[22];
         let typ_proj    = u16[24];
         let theta       = u16[26];
         let dx          = i16[28];
         let dz          = i16[30];
         let typ_casing  = u16[32] enum CasingType;
         let burst       = u16[34];
      }
   }

   Ok(Trigger{burst, dx, dz, magazine, recoil, snd_casing, snd_charge,
              snd_charged, snd_click, snd_fire, snd_reload, theta, tic_charge,
              tic_recover, tic_round, typ_ammo, typ_casing, typ_proj})
}

/// The definition of one of two triggers for a weapon.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub struct Trigger
{
   pub burst:       u16,
   pub dx:          i16,
   pub dz:          i16,
   pub magazine:    u16,
   pub recoil:      Unit,
   pub snd_casing:  OptU16,
   pub snd_charge:  OptU16,
   pub snd_charged: OptU16,
   pub snd_click:   OptU16,
   pub snd_fire:    OptU16,
   pub snd_reload:  OptU16,
   pub theta:       u16,
   pub tic_charge:  u16,
   pub tic_recover: u16,
   pub tic_round:   OptU16,
   pub typ_ammo:    OptU16,
   pub typ_casing:  CasingType,
   pub typ_proj:    u16,
}

c_enum! {
   /// A bullet shell casing emitted by a weapon.
   #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
   #[derive(Debug)]
   pub enum CasingType: u16
   {
      Rifle       = 0,
      Pistol      = 1,
      PistolLeft  = 2,
      PistolRight = 3,
      SMG         = 4,
      None        = 0xFFFF,
   }
}

// EOF
