From ab3cae61cee9964deebe20391dc090583ba84536 Mon Sep 17 00:00:00 2001 From: Zak Date: Fri, 10 Sep 2021 22:05:01 +0100 Subject: [PATCH] Fix ability points after changing character level/experience --- .../src/bl3_save/character_data.rs | 92 ++++++++++++------- .../src/views/item_editor/editor.rs | 2 +- bl3_save_edit_ui/src/views/item_editor/mod.rs | 2 +- .../src/views/manage_save/character/mod.rs | 3 +- 4 files changed, 60 insertions(+), 39 deletions(-) diff --git a/bl3_save_edit_core/src/bl3_save/character_data.rs b/bl3_save_edit_core/src/bl3_save/character_data.rs index 6477a0c..3cf2b7c 100644 --- a/bl3_save_edit_core/src/bl3_save/character_data.rs +++ b/bl3_save_edit_core/src/bl3_save/character_data.rs @@ -31,6 +31,8 @@ use crate::protos::oak_shared::{ }; use crate::vehicle_data::{VehicleName, VehicleStats}; +pub const MAX_CHARACTER_LEVEL: usize = 72; + #[derive(Derivative)] #[derivative(Debug, Clone, Default, Eq, PartialEq, Ord, PartialOrd)] pub struct CharacterData { @@ -367,25 +369,33 @@ impl CharacterData { } pub fn set_player_class(&mut self, player_class: PlayerClass) -> Result<()> { - let player_class_data = self - .character - .player_class_data - .as_mut() - .with_context(|| "failed to read Player Class data")?; + if player_class != self.player_class { + let player_class_data = self + .character + .player_class_data + .as_mut() + .with_context(|| "failed to read Player Class data")?; - player_class_data.player_class_path = player_class.get_serializations()[0].to_string(); + player_class_data.player_class_path = player_class.get_serializations()[0].to_string(); - let ability_data = self - .character - .ability_data - .as_mut() - .context("failed to read Player ability data")?; + let ability_data = self + .character + .ability_data + .as_mut() + .context("failed to read Player ability data")?; - if self.player_level > 2 { - ability_data.ability_points = self.player_level - 2; - } + //Reset our skill tree also + ability_data + .tree_item_list + .iter_mut() + .for_each(|ti| ti.points = 0); - self.player_class = player_class; + if self.player_level > 2 { + ability_data.ability_points = self.player_level - 2; + } + + self.player_class = player_class; + } Ok(()) } @@ -395,31 +405,43 @@ impl CharacterData { } pub fn set_player_level(&mut self, experience_points: i32) -> Result<()> { - self.player_level = experience_to_level(experience_points).with_context(|| { - format!( - "failed to set level for experience points: {}", - experience_points - ) - })?; + if experience_points != self.character.experience_points { + self.player_level = experience_to_level(experience_points).with_context(|| { + format!( + "failed to set level for experience points: {}", + experience_points + ) + })?; - self.character.experience_points = experience_points; + self.character.experience_points = experience_points; - self.set_game_stat(LEVEL_STAT, self.player_level); + self.set_game_stat(LEVEL_STAT, self.player_level); - let ability_data = self - .character - .ability_data - .as_mut() - .context("failed to read Player ability data")?; + let ability_data = self + .character + .ability_data + .as_mut() + .context("failed to read Player ability data")?; - //Unlock skill tree - if self.player_level > 1 && ability_data.tree_grade == 0 { - ability_data.tree_grade = 2; - } + //Reset existing skill tree + ability_data + .tree_item_list + .iter_mut() + .for_each(|ti| ti.points = 0); - for (challenge_level, challenge_obj) in LEVEL_CHALLENGES { - if self.player_level >= challenge_level { - self.unlock_challenge_obj(challenge_obj, 1, 0)?; + //Unlock skill tree + if self.player_level > 1 && ability_data.tree_grade == 0 { + ability_data.tree_grade = 2; + } + + if self.player_level > 2 { + ability_data.ability_points = self.player_level - 2; + } + + for (challenge_level, challenge_obj) in LEVEL_CHALLENGES { + if self.player_level >= challenge_level { + self.unlock_challenge_obj(challenge_obj, 1, 0)?; + } } } diff --git a/bl3_save_edit_ui/src/views/item_editor/editor.rs b/bl3_save_edit_ui/src/views/item_editor/editor.rs index 3714e79..7b03eb6 100644 --- a/bl3_save_edit_ui/src/views/item_editor/editor.rs +++ b/bl3_save_edit_ui/src/views/item_editor/editor.rs @@ -4,6 +4,7 @@ use iced::{ }; use bl3_save_edit_core::bl3_item::{BalancePart, Bl3Item, InvDataPart, ManufacturerPart}; +use bl3_save_edit_core::bl3_save::character_data::MAX_CHARACTER_LEVEL; use bl3_save_edit_core::resources::{ INVENTORY_BALANCE_PARTS, INVENTORY_INV_DATA_PARTS, INVENTORY_MANUFACTURER_PARTS, INVENTORY_PARTS_ALL_CATEGORIZED, INVENTORY_SERIAL_DB_PARTS_CATEGORIZED, @@ -15,7 +16,6 @@ use crate::resources::fonts::{JETBRAINS_MONO, JETBRAINS_MONO_BOLD}; use crate::views::item_editor::available_parts::AvailableParts; use crate::views::item_editor::current_parts::CurrentParts; use crate::views::item_editor::ItemEditorInteractionMessage; -use crate::views::manage_save::character::MAX_CHARACTER_LEVEL; use crate::views::InteractionExt; use crate::widgets::labelled_element::LabelledElement; use crate::widgets::number_input::NumberInput; diff --git a/bl3_save_edit_ui/src/views/item_editor/mod.rs b/bl3_save_edit_ui/src/views/item_editor/mod.rs index fad764d..503904a 100644 --- a/bl3_save_edit_ui/src/views/item_editor/mod.rs +++ b/bl3_save_edit_ui/src/views/item_editor/mod.rs @@ -12,6 +12,7 @@ use bl3_save_edit_core::bl3_item::{ MAX_BL3_ITEM_PARTS, }; use bl3_save_edit_core::bl3_profile::Bl3Profile; +use bl3_save_edit_core::bl3_save::character_data::MAX_CHARACTER_LEVEL; use bl3_save_edit_core::bl3_save::Bl3Save; use bl3_save_edit_core::resources::INVENTORY_SERIAL_DB; @@ -22,7 +23,6 @@ use crate::views::item_editor::available_parts::AvailablePartTypeIndex; use crate::views::item_editor::current_parts::CurrentPartTypeIndex; use crate::views::item_editor::item_editor_list_item::ItemEditorListItem; use crate::views::item_editor::parts_tab_bar::{AvailablePartType, CurrentPartType}; -use crate::views::manage_save::character::MAX_CHARACTER_LEVEL; use crate::views::InteractionExt; use crate::widgets::labelled_element::LabelledElement; use crate::widgets::notification::{Notification, NotificationSentiment}; diff --git a/bl3_save_edit_ui/src/views/manage_save/character/mod.rs b/bl3_save_edit_ui/src/views/manage_save/character/mod.rs index 8d71450..dcfc945 100644 --- a/bl3_save_edit_ui/src/views/manage_save/character/mod.rs +++ b/bl3_save_edit_ui/src/views/manage_save/character/mod.rs @@ -2,6 +2,7 @@ use iced::{ pick_list, text_input, tooltip, Align, Column, Container, Length, PickList, Row, Tooltip, }; +use bl3_save_edit_core::bl3_save::character_data::MAX_CHARACTER_LEVEL; use bl3_save_edit_core::bl3_save::player_class::PlayerClass; use bl3_save_edit_core::bl3_save::util::REQUIRED_XP_LIST; use bl3_save_edit_core::game_data::GameDataKv; @@ -24,8 +25,6 @@ mod gear; mod sdu; mod skins; -pub const MAX_CHARACTER_LEVEL: usize = 72; - #[derive(Debug, Default)] pub struct CharacterState { pub name_input: String,