From 73e3bf685868c9c925aa9abc0f2b6f1856787b27 Mon Sep 17 00:00:00 2001 From: Nick Condron Date: Tue, 21 Feb 2023 16:06:32 -0500 Subject: [PATCH 1/2] Parse misc item info --- peppi/src/model/enums/item.rs | 21 ++++++++++ peppi/src/model/item.rs | 12 +++++- peppi/src/serde/de.rs | 75 +++++++++++++++++++++-------------- peppi/src/serde/ser.rs | 6 ++- peppi/tests/peppi.rs | 23 +++++++++-- 5 files changed, 101 insertions(+), 36 deletions(-) diff --git a/peppi/src/model/enums/item.rs b/peppi/src/model/enums/item.rs index 7f69109..b36a27b 100644 --- a/peppi/src/model/enums/item.rs +++ b/peppi/src/model/enums/item.rs @@ -232,5 +232,26 @@ pseudo_enum!(Type: u16 { 236 => BIRDO_EGG, }); +pseudo_enum!(TurnipType: u8 { + 0 => SMILEY, + 1 => BORED, + 2 => SLEEPY, + 3 => SHOCKED, + 4 => LAUGHING, + 5 => WINK, + 6 => DOT_EYES, + 7 => STITCH_FACE, +}); + +pseudo_enum!(MissileType: u8 { + 0 => HOMING, + 1 => SUPER, +}); + +pseudo_enum!(ChargeLaunched: u8 { + 0 => CHARGING, + 1 => LAUNCHED, +}); + pseudo_enum!(State: u8 { }); diff --git a/peppi/src/model/item.rs b/peppi/src/model/item.rs index 42955f8..a566492 100644 --- a/peppi/src/model/item.rs +++ b/peppi/src/model/item.rs @@ -1,11 +1,19 @@ use serde::Serialize; use crate::model::{ - enums::item::{State, Type}, + enums::item::{ChargeLaunched, MissileType, State, TurnipType, Type}, primitives::{Direction, Port, Position, Velocity}, }; use peppi_derive::Arrow; +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Arrow)] +pub struct MiscInfo { + pub missile_type: MissileType, + pub turnip_type: TurnipType, + pub charge_launched: ChargeLaunched, + pub charge_power: u8, +} + #[derive(Clone, Copy, Debug, PartialEq, Serialize, Arrow)] pub struct Item { pub id: u32, @@ -26,7 +34,7 @@ pub struct Item { #[serde(skip_serializing_if = "Option::is_none")] #[slippi(version = "3.2")] - pub misc: Option<[u8; 4]>, + pub misc_info: Option, #[serde(skip_serializing_if = "Option::is_none")] #[slippi(version = "3.5")] diff --git a/peppi/src/serde/de.rs b/peppi/src/serde/de.rs index 8aed132..82ecdb9 100644 --- a/peppi/src/serde/de.rs +++ b/peppi/src/serde/de.rs @@ -24,7 +24,7 @@ use crate::{ }, frame::{self, Post, Pre}, game::{self, Netplay, Player, PlayerType, MAX_PLAYERS, NUM_PORTS}, - item::Item, + item::{Item, MiscInfo}, primitives::{Port, Position, Velocity}, slippi, triggers, }, @@ -512,39 +512,56 @@ fn frame_end(r: &mut &[u8]) -> Result> { } fn item(r: &mut &[u8]) -> Result> { - let id = FrameId::new(r.read_i32::()?); - trace!("Item Update: {:?}", id); + let frame_id = FrameId::new(r.read_i32::()?); + trace!("Item Update: {:?}", frame_id); let r#type = item::Type(r.read_u16::()?); + let state = item::State(r.read_u8()?); + let direction = { + let x = r.read_f32::()?; + if x == 0.0 { + None + } else { + Some(x.try_into()?) + } + }; + let velocity = Velocity { + x: r.read_f32::()?, + y: r.read_f32::()?, + }; + let position = Position { + x: r.read_f32::()?, + y: r.read_f32::()?, + }; + let damage = r.read_u16::()?; + let timer = r.read_f32::()?; + let id = r.read_u32::()?; + // v3.2 + let misc_info = if_more(r, |r| { + Ok(MiscInfo { + missile_type: item::MissileType(r.read_u8()?), + turnip_type: item::TurnipType(r.read_u8()?), + charge_launched: item::ChargeLaunched(r.read_u8()?), + charge_power: r.read_u8()?, + }) + })?; + // v3.6 + let owner = if_more(r, |r| Ok(Port::try_from(r.read_u8()?).ok()))?; + Ok(FrameEvent { - id: id, + id: frame_id, event: Item { - r#type: r#type, - state: item::State(r.read_u8()?), - direction: { - let x = r.read_f32::()?; - if x == 0.0 { - None - } else { - Some(x.try_into()?) - } - }, - velocity: Velocity { - x: r.read_f32::()?, - y: r.read_f32::()?, - }, - position: Position { - x: r.read_f32::()?, - y: r.read_f32::()?, - }, - damage: r.read_u16::()?, - timer: r.read_f32::()?, - id: r.read_u32::()?, + r#type, + state, + direction, + velocity, + position, + damage, + timer, + id, // v3.2 - misc: if_more(r, |r| { - Ok([r.read_u8()?, r.read_u8()?, r.read_u8()?, r.read_u8()?]) - })?, + misc_info, // v3.6 - owner: if_more(r, |r| Ok(Port::try_from(r.read_u8()?).ok()))?, + owner, }, }) } diff --git a/peppi/src/serde/ser.rs b/peppi/src/serde/ser.rs index a4999ad..e55bdcc 100644 --- a/peppi/src/serde/ser.rs +++ b/peppi/src/serde/ser.rs @@ -243,7 +243,11 @@ fn item(w: &mut W, i: &item::Item, ver: Version, frame_idx: i32) -> Re w.write_u32::(i.id)?; if ver.gte(3, 2) { - w.write_all(&i.misc.unwrap())?; + let misc = &i.misc_info.unwrap(); + w.write_u8(misc.missile_type.0)?; + w.write_u8(misc.turnip_type.0)?; + w.write_u8(misc.charge_launched.0)?; + w.write_u8(misc.charge_power)?; } if ver.gte(3, 6) { diff --git a/peppi/tests/peppi.rs b/peppi/tests/peppi.rs index 42f3efd..8b0ecee 100644 --- a/peppi/tests/peppi.rs +++ b/peppi/tests/peppi.rs @@ -24,7 +24,7 @@ use peppi::{ DashBack, End, EndMethod, Frames, Game, Language, Netplay, Player, PlayerType, Scene, ShieldDrop, Start, Ucf, }, - item::Item, + item::{Item, MiscInfo}, metadata::{self, Metadata}, primitives::{Direction, Port, Position, Velocity}, slippi::{Slippi, Version}, @@ -573,7 +573,12 @@ fn items() { timer: 140.0, r#type: item::Type::PEACH_TURNIP, velocity: Velocity { x: 0.0, y: 0.0 }, - misc: Some([5, 5, 5, 5]), + misc_info: Some(MiscInfo { + missile_type: item::MissileType(5), + turnip_type: item::TurnipType::WINK, + charge_launched: item::ChargeLaunched(5), + charge_power: 5, + }), owner: Some(Some(Port::P1)), } ); @@ -591,7 +596,12 @@ fn items() { timer: 140.0, r#type: item::Type::PEACH_TURNIP, velocity: Velocity { x: 0.0, y: 0.0 }, - misc: Some([5, 0, 5, 5]), + misc_info: Some(MiscInfo { + missile_type: item::MissileType(5), + turnip_type: item::TurnipType::SMILEY, + charge_launched: item::ChargeLaunched(5), + charge_power: 5, + }), owner: Some(Some(Port::P1)), } ); @@ -609,7 +619,12 @@ fn items() { timer: 140.0, r#type: item::Type::PEACH_TURNIP, velocity: Velocity { x: 0.0, y: 0.0 }, - misc: Some([5, 0, 5, 5]), + misc_info: Some(MiscInfo { + missile_type: item::MissileType(5), + turnip_type: item::TurnipType::SMILEY, + charge_launched: item::ChargeLaunched(5), + charge_power: 5, + }), owner: Some(Some(Port::P1)), } ); From 67480bb23d53761fd86f735979439315f51dd3b8 Mon Sep 17 00:00:00 2001 From: Nick Condron Date: Mon, 27 Mar 2023 19:54:02 -0400 Subject: [PATCH 2/2] Rename ChargeLaunched to ChargeState --- peppi/src/model/enums/item.rs | 2 +- peppi/src/model/item.rs | 4 ++-- peppi/src/serde/de.rs | 2 +- peppi/src/serde/ser.rs | 2 +- peppi/tests/peppi.rs | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/peppi/src/model/enums/item.rs b/peppi/src/model/enums/item.rs index b36a27b..a4e2e68 100644 --- a/peppi/src/model/enums/item.rs +++ b/peppi/src/model/enums/item.rs @@ -248,7 +248,7 @@ pseudo_enum!(MissileType: u8 { 1 => SUPER, }); -pseudo_enum!(ChargeLaunched: u8 { +pseudo_enum!(ChargeState: u8 { 0 => CHARGING, 1 => LAUNCHED, }); diff --git a/peppi/src/model/item.rs b/peppi/src/model/item.rs index a566492..5d54b3b 100644 --- a/peppi/src/model/item.rs +++ b/peppi/src/model/item.rs @@ -1,7 +1,7 @@ use serde::Serialize; use crate::model::{ - enums::item::{ChargeLaunched, MissileType, State, TurnipType, Type}, + enums::item::{ChargeState, MissileType, State, TurnipType, Type}, primitives::{Direction, Port, Position, Velocity}, }; use peppi_derive::Arrow; @@ -10,7 +10,7 @@ use peppi_derive::Arrow; pub struct MiscInfo { pub missile_type: MissileType, pub turnip_type: TurnipType, - pub charge_launched: ChargeLaunched, + pub charge_state: ChargeState, pub charge_power: u8, } diff --git a/peppi/src/serde/de.rs b/peppi/src/serde/de.rs index 82ecdb9..76a6641 100644 --- a/peppi/src/serde/de.rs +++ b/peppi/src/serde/de.rs @@ -540,7 +540,7 @@ fn item(r: &mut &[u8]) -> Result> { Ok(MiscInfo { missile_type: item::MissileType(r.read_u8()?), turnip_type: item::TurnipType(r.read_u8()?), - charge_launched: item::ChargeLaunched(r.read_u8()?), + charge_state: item::ChargeState(r.read_u8()?), charge_power: r.read_u8()?, }) })?; diff --git a/peppi/src/serde/ser.rs b/peppi/src/serde/ser.rs index e55bdcc..24db81b 100644 --- a/peppi/src/serde/ser.rs +++ b/peppi/src/serde/ser.rs @@ -246,7 +246,7 @@ fn item(w: &mut W, i: &item::Item, ver: Version, frame_idx: i32) -> Re let misc = &i.misc_info.unwrap(); w.write_u8(misc.missile_type.0)?; w.write_u8(misc.turnip_type.0)?; - w.write_u8(misc.charge_launched.0)?; + w.write_u8(misc.charge_state.0)?; w.write_u8(misc.charge_power)?; } diff --git a/peppi/tests/peppi.rs b/peppi/tests/peppi.rs index 8b0ecee..7a5d0fa 100644 --- a/peppi/tests/peppi.rs +++ b/peppi/tests/peppi.rs @@ -576,7 +576,7 @@ fn items() { misc_info: Some(MiscInfo { missile_type: item::MissileType(5), turnip_type: item::TurnipType::WINK, - charge_launched: item::ChargeLaunched(5), + charge_state: item::ChargeState(5), charge_power: 5, }), owner: Some(Some(Port::P1)), @@ -599,7 +599,7 @@ fn items() { misc_info: Some(MiscInfo { missile_type: item::MissileType(5), turnip_type: item::TurnipType::SMILEY, - charge_launched: item::ChargeLaunched(5), + charge_state: item::ChargeState(5), charge_power: 5, }), owner: Some(Some(Port::P1)), @@ -622,7 +622,7 @@ fn items() { misc_info: Some(MiscInfo { missile_type: item::MissileType(5), turnip_type: item::TurnipType::SMILEY, - charge_launched: item::ChargeLaunched(5), + charge_state: item::ChargeState(5), charge_power: 5, }), owner: Some(Some(Port::P1)),