commonlibsse_ng\re\e/
ExtraHealth.rs

1//! # ExtraHealth
2//!
3//! Represents extra data for health values.
4//!
5//! Inherits from `BSExtraData` and includes the health value with padding for memory alignment.
6//!
7//! # Memory Layout:
8//! - `__base`: Base class `BSExtraData`
9//! - `health`: The health value (0x10)
10//! - `pad14`: Padding for alignment (0x14)
11
12use crate::re::BSExtraData::{BSExtraData, DerivedBSExtraData};
13use crate::re::ExtraDataType::ExtraDataType;
14use crate::re::NiMath::{ComparisonOptions, nearly_equal};
15use crate::re::offsets_rtti::RTTI_ExtraHealth;
16use crate::re::offsets_vtable::VTABLE_ExtraHealth;
17use crate::rel::id::VariantID;
18
19#[repr(C)]
20#[derive(Debug, PartialEq)]
21pub struct ExtraHealth {
22    /// Base class `BSExtraData`.
23    pub __base: BSExtraData,
24
25    /// The health value.
26    /// Offset: `0x10`
27    pub health: f32,
28
29    /// Padding to match C++ structure alignment.
30    /// Offset: `0x14`
31    pub pad14: u32,
32}
33
34const _: () = {
35    assert!(core::mem::offset_of!(ExtraHealth, __base) == 0x0);
36    assert!(core::mem::offset_of!(ExtraHealth, health) == 0x10);
37    assert!(core::mem::offset_of!(ExtraHealth, pad14) == 0x14);
38    assert!(core::mem::size_of::<ExtraHealth>() == 0x18);
39};
40
41impl Default for ExtraHealth {
42    #[inline]
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48impl DerivedBSExtraData for ExtraHealth {
49    #[inline]
50    fn get_extra_data(&self) -> &BSExtraData {
51        &self.__base
52    }
53
54    #[inline]
55    fn get_extra_data_type() -> ExtraDataType {
56        Self::EXTRA_DATA_TYPE
57    }
58}
59
60impl ExtraHealth {
61    /// Address & offset of the runtime type information (RTTI) identifier.
62    pub const RTTI: VariantID = RTTI_ExtraHealth;
63
64    /// Address & offset of the virtual function table.
65    pub const VTABLE: [VariantID; 1] = VTABLE_ExtraHealth;
66
67    /// The `ExtraDataType` value for health.
68    pub const EXTRA_DATA_TYPE: ExtraDataType = ExtraDataType::Health;
69
70    /// Creates a new `ExtraHealth` instance with default health (0.0).
71    #[inline]
72    pub const fn new() -> Self {
73        Self { __base: BSExtraData::new(), health: 0.0, pad14: 0 }
74    }
75
76    /// Creates a new `ExtraHealth` instance with a specific health value.
77    #[inline]
78    pub const fn from_health(health: f32) -> Self {
79        Self { __base: BSExtraData::new(), health, pad14: 0 }
80    }
81
82    /// Gets the extra data type, always returning `ExtraDataType::Health`.
83    #[inline]
84    pub const fn get_type(&self) -> ExtraDataType {
85        ExtraDataType::Health
86    }
87
88    /// Checks if this `ExtraHealth` is not equal to another.
89    #[inline]
90    pub const fn is_not_equal(&self, rhs: &Self) -> bool {
91        nearly_equal(self.health, rhs.health, ComparisonOptions::const_default())
92    }
93}
94
95/// The virtual function table for `ExtraHealth`.
96///
97/// This struct defines function pointers to simulate the C++ virtual functions.
98#[repr(C)]
99#[derive(Debug)]
100pub struct ExtraHealthVtbl {
101    /// Destructor function pointer.
102    pub CxxDrop: fn(this: &mut ExtraHealth),
103
104    /// Function pointer for retrieving the extra data type.
105    pub GetType: fn(this: &ExtraHealth) -> ExtraDataType,
106
107    /// Function pointer for equality check.
108    pub IsNotEqual: fn(this: &ExtraHealth, rhs: &ExtraHealth) -> bool,
109}
110
111impl Default for ExtraHealthVtbl {
112    #[inline]
113    fn default() -> Self {
114        Self::new()
115    }
116}
117
118impl ExtraHealthVtbl {
119    /// Creates a new default virtual table with stubbed functions.
120    pub const fn new() -> Self {
121        const fn CxxDrop(_this: &mut ExtraHealth) {}
122
123        const fn GetType(_this: &ExtraHealth) -> ExtraDataType {
124            ExtraHealth::EXTRA_DATA_TYPE
125        }
126
127        const fn IsNotEqual(this: &ExtraHealth, rhs: &ExtraHealth) -> bool {
128            nearly_equal(this.health, rhs.health, ComparisonOptions::const_default())
129        }
130
131        Self { CxxDrop, GetType, IsNotEqual }
132    }
133}