From fa556b253ab8078899aebd9b30b049b76e3632ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 5 Jul 2020 22:27:04 +0200 Subject: [PATCH] savestate: add Stats struct in IndividualPk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- savestate/src/species.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/savestate/src/species.rs b/savestate/src/species.rs index 8cf6a41..96c8089 100644 --- a/savestate/src/species.rs +++ b/savestate/src/species.rs @@ -166,11 +166,32 @@ impl TryFrom<&[u8; 10]> for EV { /// Displayed stats. These can be recalculated from IVs and EVs. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Stats { - hp: u32, - attack: u32, - defense: u32, - speed: u32, - special: u32, + hp: u16, + attack: u16, + defense: u16, + speed: u16, + special: u16, +} + +impl TryFrom<&[u8]> for Stats { + type Error = Error; + + fn try_from(data: &[u8]) -> Result { + let data: &[u8; 10] = data.try_into()?; + Ok(Stats::from(data)) + } +} + +impl From<&[u8; 10]> for Stats { + fn from(data: &[u8; 10]) -> Stats { + Stats { + hp: u16::from_be_bytes(data[0x0..0x2].try_into().unwrap()), + attack: u16::from_be_bytes(data[0x2..0x4].try_into().unwrap()), + defense: u16::from_be_bytes(data[0x4..0x6].try_into().unwrap()), + speed: u16::from_be_bytes(data[0x6..0x8].try_into().unwrap()), + special: u16::from_be_bytes(data[0x8..0xa].try_into().unwrap()), + } + } } /// Types are properties for Pokémons and their moves. @@ -501,6 +522,7 @@ const PK_XP_OFFSET: usize = 0xe; // Over 3 bytes const PK_OT_RANGE: Range = 0xc..0xe; const PK_MOVES_RANGE: Range = 0x8..0xc; const PK_PP_RANGE: Range = 0x1d..0x21; +const PK_STATS_RANGE: Range = 0x22..0x2c; #[derive(PartialEq)] pub struct IndividualPk { @@ -516,7 +538,7 @@ pub struct IndividualPk { pub xp: u32, pub iv: IV, pub ev: EV, - // pub stats: Stats, + pub stats: Stats, pub moves: Moves, } @@ -534,6 +556,7 @@ impl fmt::Debug for IndividualPk { .field("xp", &self.xp) .field("iv", &self.iv) .field("ev", &self.ev) + .field("stats", &self.stats) .field("moves", &self.moves) .finish_non_exhaustive() } @@ -582,6 +605,7 @@ impl TryFrom<(&[u8], PKString, PKString)> for IndividualPk { xp, iv: IV::try_from(&data[PK_IV_RANGE])?, ev: EV::try_from(&data[PK_EV_RANGE])?, + stats: Stats::try_from(&data[PK_STATS_RANGE])?, moves, }) }