From b071b55a5868727078b1ebe1206fb9c088a19d7c Mon Sep 17 00:00:00 2001 From: hheik <4469778+hheik@users.noreply.github.com> Date: Sat, 1 Apr 2023 17:54:54 +0300 Subject: [PATCH] added ev display --- src/ivpeek/battle.rs | 42 ++++++++++++++++-------- src/ivpeek/memory_reader.rs | 2 +- src/ivpeek/ui.rs | 65 ++++++++++++++++++++++++++++++++++--- 3 files changed, 90 insertions(+), 19 deletions(-) diff --git a/src/ivpeek/battle.rs b/src/ivpeek/battle.rs index f8110a5..141b6c7 100644 --- a/src/ivpeek/battle.rs +++ b/src/ivpeek/battle.rs @@ -48,6 +48,7 @@ fn try_parse(data: &[u8], offset: usize) -> Option { return None; } let decrypted = decrypt_pokemon(encrypted).ok()?; + // println!("{}", debug_pokemon_data(&decrypted)); let pokemon = parse_pokemon(&decrypted).ok()?; Some(pokemon) } @@ -125,32 +126,47 @@ fn parse_pokemon(data: &[u8]) -> Result { instance.pokemon.national = read_short(data, offset_a + 0x00); let ivs = read_long(data, offset_b + 0x10); + instance.ivs.insert(BaseStat::Hp, ((ivs >> 0) & 31) as u8); instance .ivs - .insert(crate::pokemon::BaseStat::Hp, ((ivs >> 0) & 31) as u8); + .insert(BaseStat::Attack, ((ivs >> 5) & 31) as u8); instance .ivs - .insert(crate::pokemon::BaseStat::Attack, ((ivs >> 5) & 31) as u8); + .insert(BaseStat::Defense, ((ivs >> 10) & 31) as u8); instance .ivs - .insert(crate::pokemon::BaseStat::Defense, ((ivs >> 10) & 31) as u8); + .insert(BaseStat::Speed, ((ivs >> 15) & 31) as u8); instance .ivs - .insert(crate::pokemon::BaseStat::Speed, ((ivs >> 15) & 31) as u8); - instance.ivs.insert( - crate::pokemon::BaseStat::SpecialAttack, - ((ivs >> 20) & 31) as u8, - ); - instance.ivs.insert( - crate::pokemon::BaseStat::SpecialDefense, - ((ivs >> 25) & 31) as u8, - ); + .insert(BaseStat::SpecialAttack, ((ivs >> 20) & 31) as u8); + instance + .ivs + .insert(BaseStat::SpecialDefense, ((ivs >> 25) & 31) as u8); instance.nature = Some(Nature { pid: read_byte(data, offset_b + 0x19), ..Default::default() }); + instance + .evs + .insert(BaseStat::Hp, read_byte(data, offset_a + 0x10)); + instance + .evs + .insert(BaseStat::Attack, read_byte(data, offset_a + 0x11)); + instance + .evs + .insert(BaseStat::Defense, read_byte(data, offset_a + 0x12)); + instance + .evs + .insert(BaseStat::Speed, read_byte(data, offset_a + 0x13)); + instance + .evs + .insert(BaseStat::SpecialAttack, read_byte(data, offset_a + 0x14)); + instance + .evs + .insert(BaseStat::SpecialDefense, read_byte(data, offset_a + 0x15)); + Ok(instance) } @@ -232,7 +248,7 @@ fn debug_pokemon_data(data: &[u8]) -> String { _ => offset, }; if address % 4 == 0 { - result += format!("[ {:#06X} ]", address - offset).as_str(); + result += format!("[ {:#06X} :: {:#06X} ]", address, address - offset).as_str(); } result += format!(" {value:#04X}").as_str(); if address % 4 == 3 { diff --git a/src/ivpeek/memory_reader.rs b/src/ivpeek/memory_reader.rs index d0fa11b..81a8925 100644 --- a/src/ivpeek/memory_reader.rs +++ b/src/ivpeek/memory_reader.rs @@ -98,7 +98,7 @@ fn init_memory_reader(queue: Res) { let (tx, rx) = std::sync::mpsc::channel(); - let mut debouncer = new_debouncer(Duration::from_millis(1_000), None, tx) + let mut debouncer = new_debouncer(Duration::from_millis(200), None, tx) .expect("Could not create notify debouncer"); debouncer diff --git a/src/ivpeek/ui.rs b/src/ivpeek/ui.rs index 6e6f1df..c81da11 100644 --- a/src/ivpeek/ui.rs +++ b/src/ivpeek/ui.rs @@ -10,9 +10,9 @@ use super::inspector::{Inspector, PokemonInstance}; const BAR_HEIGHT: f32 = 12.0; const SPRITE_SIZE: f32 = 128.0; const SPRITE_PADDING: f32 = 32.0; -const BASE_STAT_WIDTH_MULT: f32 = 0.8; +const BASE_STAT_WIDTH_MULT: f32 = 0.5; const IV_WIDTH_MULT: f32 = 4.0; -// const EV_WIDTH_MULT: f32 = 1.0; +const EV_WIDTH_MULT: f32 = 0.25; lazy_static! { static ref BASE_STAT_COLOR_RANGES: Vec<(u8, egui::Color32)> = vec![ @@ -34,12 +34,13 @@ lazy_static! { static ref COLUMNS: Vec = vec![ 25.0, // Base stat 255.0 * BASE_STAT_WIDTH_MULT, // Base stat bar + 25.0, // EV value + 255.0 * EV_WIDTH_MULT, // EV bar 60.0, // Stat name 25.0, // IV value 32.0 * IV_WIDTH_MULT, // IV bar - // 40.0, // EV value - // 255.0 * EV_WIDTH_MULT, // EV bar ]; + static ref TRACK_COLOR: egui::Color32 = egui::Color32::from_rgb(48, 48, 48); } pub struct UiPlugin; @@ -131,7 +132,16 @@ fn render_pokemon_instance( 1 => { ui.label(egui::RichText::new("Base stat")); } + 2 => { + ui.label(egui::RichText::new("EV")); + } 3 => { + ui.label(egui::RichText::new(format!( + "{} / 510", + instance.evs.values().map(|v| *v as i32).sum::() + ))); + } + 5 => { ui.label(egui::RichText::new("IV")); } _ => (), @@ -184,6 +194,51 @@ fn render_pokemon_instance( ui.add(image); }); + // EV stat + ui.horizontal(|ui| { + if let Some(width) = column_size.next() { + ui.set_width(*width); + } + if let Some(ev) = instance.evs.get(&stat) { + ui.label(egui::RichText::new(format!("{ev}"))); + } else { + ui.label("-"); + } + }); + + // EV stat bar + ui.horizontal(|ui| { + if let Some(width) = column_size.next() { + ui.set_width(*width); + } + if let Some(ev) = instance.evs.get(&stat) { + let bar_length = *ev as f32 * EV_WIDTH_MULT; + let track_length = 255. * EV_WIDTH_MULT - bar_length; + let color = egui::Color32::LIGHT_GRAY; + + ui.horizontal(|ui| { + ui.spacing_mut().item_spacing = egui::Vec2::ZERO; + + // Bar + ui.add( + egui::Image::new( + ui_assets.bar_handle.1, + [bar_length, BAR_HEIGHT], + ) + .tint(color), + ); + // Track + ui.add( + egui::Image::new( + ui_assets.bar_handle.1, + [track_length, BAR_HEIGHT], + ) + .tint(TRACK_COLOR.clone()), + ); + }); + } + }); + // Stat name ui.horizontal(|ui| { if let Some(width) = column_size.next() { @@ -234,7 +289,7 @@ fn render_pokemon_instance( ui_assets.bar_handle.1, [track_length, BAR_HEIGHT], ) - .tint(egui::Color32::from_rgb(64, 64, 64)), + .tint(TRACK_COLOR.clone()), ); }); }