From f7b22dfb83a8eb6671e11d3a8dd7a8c293884054 Mon Sep 17 00:00:00 2001 From: C J Silverio Date: Wed, 20 Dec 2023 17:26:04 -0800 Subject: [PATCH] fix #94 Dropped/Sold items can't be removed from HUD (#95) tl;dr copy-paste considered harmful. Dijkstra is alleged to have provided that title for the essay, I have read. He was a real trickster figure. Plus, some efficiency tweaks for this code path. Added some comments explaining the problem with knowing when to advance a cycle in the drop/sell case. --- justfile | 2 +- src/controller/control.rs | 16 ++++++++++++---- src/controller/cycleentries.rs | 18 ++++++++++++++++++ src/controller/cycles.rs | 13 ++++--------- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/justfile b/justfile index fb9fd19e..4e86042b 100644 --- a/justfile +++ b/justfile @@ -3,7 +3,7 @@ set shell := ["bash", "-uc"] set dotenv-load := true SPRIGGIT := "~/bin/spriggit" -TESTMOD := "/mnt/g/MO2Skyrim/SoulsyHUD fomod test/" +TESTMOD := "/mnt/g/MO2Skyrim/Soulsy HUD dev version" # List available recipes. help: diff --git a/src/controller/control.rs b/src/controller/control.rs index 455df72c..69207bf4 100644 --- a/src/controller/control.rs +++ b/src/controller/control.rs @@ -226,25 +226,33 @@ impl Controller { } } } else { + // This entire code block is unlikely to execute because we are + // consistently getting the unequip message first. Unfortunately + // we have no idea at that time *why* the unequip event happened. if let Some(candidate) = self.visible.get_mut(&HudElement::Left) { if candidate.form_string() == *form_spec { candidate.set_count(new_count); + if new_count == 0 { + self.advance_hand_cycle(&CycleSlot::Left); + } } } if let Some(candidate) = self.visible.get_mut(&HudElement::Right) { if candidate.form_string() == *form_spec { candidate.set_count(new_count); + if new_count == 0 { + self.advance_hand_cycle(&CycleSlot::Right); + } } } } - - self.cycles - .remove_zero_count_items(form_spec.as_str(), &kind, new_count); - if new_count > 0 { return; } + self.cycles + .remove_zero_count_items(form_spec.as_str(), &kind); + // The count of the inventory item went to zero. We need to check // if we must equip/ready something else now. diff --git a/src/controller/cycleentries.rs b/src/controller/cycleentries.rs index fe07f12d..8c448e44 100644 --- a/src/controller/cycleentries.rs +++ b/src/controller/cycleentries.rs @@ -458,4 +458,22 @@ mod tests { let id = cycle.find_next_id(); assert_eq!(id, 1); } + + #[test] + fn filtering() { + use crate::data::item_cache::ItemCache; + let mut cache = ItemCache::new(); + let mut cycle = Vec::::new(); + + let item = cache.get(&"form-one".to_string()); + assert!(cycle.add(&item.form_string())); + let item2 = cache.get(&"form-two".to_string()); + assert!(cycle.add(&item2.form_string())); + let item3 = cache.get(&"form-three".to_string()); + cycle.add(&item3.form_string()); + assert_eq!(cycle.len(), 3); + + assert!(cycle.filter_id(&"form-two")); + assert_eq!(cycle.len(), 2); + } } diff --git a/src/controller/cycles.rs b/src/controller/cycles.rs index c334c8e1..b119499c 100644 --- a/src/controller/cycles.rs +++ b/src/controller/cycles.rs @@ -190,21 +190,16 @@ impl CycleData { } } - pub fn remove_zero_count_items(&mut self, form_spec: &str, kind: &BaseType, count: u32) { - // If count is zero, remove from any cycles it's in. - // If count is zero and item is equipped, advance the relevant cycle. <-- not happening erk - if count > 0 { - return; - } - + pub fn remove_zero_count_items(&mut self, form_spec: &str, kind: &BaseType) { if kind.is_utility() { self.utility.filter_id(form_spec); + return; } if kind.left_hand_ok() { - self.utility.filter_id(form_spec); + self.left.filter_id(form_spec); } if kind.right_hand_ok() { - self.utility.filter_id(form_spec); + self.right.filter_id(form_spec); } }