//! `Platform` type.

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

/// Reads a `plat` chunk.
pub fn read(b: &[u8]) -> ResultS<(Platform, usize)>
{
   read_data! {
      endian: BIG, buf: b, size: 32, start: 0, data {
         let ptype   = u16[0];
         let speed   = u16[2];
         let delay   = u16[4];
         let hei_max = Unit[6];
         let hei_min = Unit[8];
         let flags   = u32[10] flag PlatformFlags;
         let index   = u16[14];
         let tag     = u16[16];
      }
   }

   Ok((Platform{ptype, speed, delay, hei_min, hei_max, flags, index, tag}, 32))
}

/// Extra information for polygons with platforms.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub struct Platform
{
   pub ptype:   u16,
   pub speed:   u16,
   pub delay:   u16,
   pub hei_min: Unit,
   pub hei_max: Unit,
   pub flags:   PlatformFlags,
   pub index:   u16,
   pub tag:     u16,
}

bitflags! {
   /// Flags for `Platform`.
   #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
   pub struct PlatformFlags: u32
   {
      const INIT_ACTIVE           = 1;
      const INIT_EXTENDED         = 1 << 1;
      const STOP_AT_EACH_LEVEL    = 1 << 2;
      const STOP_AT_INIT_LEVEL    = 1 << 3;
      const START_ADJ_ON_STOP     = 1 << 4;
      const EXTENDS_FLOOR_TO_CEIL = 1 << 5;
      const COMES_FROM_FLOOR      = 1 << 6;
      const COMES_FROM_CEIL       = 1 << 7;
      const CAUSES_DAMAGE         = 1 << 8;
      const NO_ACTIVATE_PARENT    = 1 << 9;
      const ACTIVATES_ONCE        = 1 << 10;
      const ACTIVATES_LIGHT       = 1 << 11;
      const DEACTIVATES_LIGHT     = 1 << 12;
      const PLAYER_CONTROLS       = 1 << 13;
      const MONSTER_CONTROLS      = 1 << 14;
      const REVERSE_ON_OBSTRUCT   = 1 << 15;
      const NO_EXT_DEACTIVATION   = 1 << 16;
      const USE_POLYGON_HEIGHTS   = 1 << 17;
      const DELAYED_ACTIVATION    = 1 << 18;
      const START_ADJ_ON_START    = 1 << 19;
      const STOP_ADJ_ON_START     = 1 << 20;
      const STOP_ADJ_ON_STOP      = 1 << 21;
      const SLOW                  = 1 << 22;
      const START_AT_EACH_LEVEL   = 1 << 23;
      const LOCKED                = 1 << 24;
      const SECRET                = 1 << 25;
      const DOOR                  = 1 << 26;
   }
}

// EOF
