Skip to content

Commit

Permalink
fix #94 Dropped/Sold items can't be removed from HUD (#95)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ceejbot authored Dec 21, 2023
1 parent f1139c6 commit f7b22df
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 12 additions & 4 deletions src/controller/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
18 changes: 18 additions & 0 deletions src/controller/cycleentries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>::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);
}
}
13 changes: 4 additions & 9 deletions src/controller/cycles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit f7b22df

Please sign in to comment.