//! `ObjectFreq` type.

use crate::durandal::err::*;
use bitflags::bitflags;

/// Reads a `plac` chunk.
pub fn read(b: &[u8]) -> ResultS<(ObjectFreq, usize)>
{
   read_data! {
      endian: BIG, buf: b, size: 12, start: 0, data {
         let flags   = u16[0] flag ObjectFreqFlags;
         let cnt_ini = u16[2];
         let cnt_min = u16[4];
         let cnt_max = u16[6];
         let cnt_rnd = u16[8];
         let chance  = u16[10];
      }
   }

   Ok((ObjectFreq{flags, cnt_ini, cnt_min, cnt_max, cnt_rnd, chance}, 12))
}

/// The difficulty definition for various object types.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub struct ObjectFreq
{
   pub flags:   ObjectFreqFlags,
   pub cnt_ini: u16,
   pub cnt_min: u16,
   pub cnt_max: u16,
   pub cnt_rnd: u16,
   pub chance:  u16,
}

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

// EOF
