//! `Platform` type.

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

/// 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(Clone, 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,
}

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

// EOF
