//! `Damage` type.

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

/// Reads a `Damage` object.
pub fn read(b: &[u8]) -> ResultS<Damage>
{
	read_data! {
		endian: BIG, buf: b, size: 12, start: 0, data {
			let dtype    = u16[0] enum DamageType;
			let flags    = u16[2] flag DamageFlags;
			let dmg_base = u16[4];
			let dmg_rand = u16[6];
			let scale    = Fixed[8];
		}
	}

	Ok(Damage{dtype, flags, dmg_base, dmg_rand, scale})
}

/// A damage definition.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub struct Damage {
	pub dtype:    DamageType,
	pub flags:    DamageFlags,
	pub dmg_base: u16,
	pub dmg_rand: u16,
	pub scale:    Fixed,
}

c_bitfield! {
	/// The composite type of damage taken by something.
	#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
	pub struct DamageTypeFlags: u32 {
		EXPLOSION          = 0,
		ELECTRICAL_STAFF   = 1,
		PROJECTILE         = 2,
		ABSORBED           = 3,
		FLAME              = 4,
		HOUND_CLAWS        = 5,
		ALIEN_PROJECTILE   = 6,
		HULK_SLAP          = 7,
		COMPILER_BOLT      = 8,
		FUSION_BOLT        = 9,
		HUNTER_BOLT        = 10,
		FIST               = 11,
		TELEPORTER         = 12,
		DEFENDER           = 13,
		YETI_CLAWS         = 14,
		YETI_PROJECTILE    = 15,
		CRUSHING           = 16,
		LAVA               = 17,
		SUFFOCATION        = 18,
		GOO                = 19,
		ENERGY_DRAIN       = 20,
		OXYGEN_DRAIN       = 21,
		HUMMER_BOLT        = 22,
		SHOTGUN_PROJECTILE = 23,
	}
}

c_bitfield! {
	/// Flags for `Damage`.
	#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
	pub struct DamageFlags: u16 {
		ALIEN = 0,
	}
}

c_enum! {
	/// A named type of damage taken by something.
	#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
	pub enum DamageType: u16 {
		Explosion         = 0,
		ElectricalStaff   = 1,
		Projectile        = 2,
		Absorbed          = 3,
		Flame             = 4,
		HoundClaws        = 5,
		AlienProjectile   = 6,
		HulkSlap          = 7,
		CompilerBolt      = 8,
		FusionBolt        = 9,
		HunterBolt        = 10,
		Fist              = 11,
		Teleporter        = 12,
		Defender          = 13,
		YetiClaws         = 14,
		YetiProjectile    = 15,
		Crushing          = 16,
		Lava              = 17,
		Suffocation       = 18,
		Goo               = 19,
		EnergyDrain       = 20,
		OxygenDrain       = 21,
		HummerBolt        = 22,
		ShotgunProjectile = 23,
		None              = 0xFFFF,
	}
}

// EOF
