Merge branch 'ashirviskas/advanced_inventory_sorting' into 'master'

Inventory: Improved inventory sorting

See merge request veloren/veloren!3736
This commit is contained in:
Imbris 2023-01-01 01:14:47 +00:00
commit a123c7ad7f
6 changed files with 81 additions and 5 deletions

View File

@ -57,6 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improved shiny water shaders
- Tweaked armor stats
- Move bag icon to skillbar
- Improved inventory sorting by Category
### Removed

View File

@ -38,4 +38,5 @@ hud-bag-stun_res_desc =
Regenerates like Energy.
hud-bag-sort_by_name = Sort by Name
hud-bag-sort_by_quality = Sort by Quality
hud-bag-sort_by_category = Sort by Category
hud-bag-sort_by_category = Sort by Category
hud-bag-sort_by_tag = Sort by Tag

View File

@ -206,7 +206,7 @@ impl PartialOrd for Protection {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Armor {
pub kind: ArmorKind,
stats: StatsSource,
pub stats: StatsSource,
}
impl Armor {

View File

@ -305,6 +305,29 @@ impl ItemKind {
ItemKind::Tool(_) | ItemKind::Armor { .. } | ItemKind::Glider | ItemKind::Lantern(_)
)
}
// Used for inventory sorting, what comes before the first colon (:) is used as
// a broader category
pub fn get_itemkind_string(&self) -> String {
let result = match self {
// Using tool and toolkind to sort tools by kind
ItemKind::Tool(tool) => format!("Tool: {:?}", tool.kind),
ItemKind::ModularComponent(modular_component) => {
format!("ModularComponent: {:?}", modular_component.toolkind())
},
ItemKind::Lantern(lantern) => format!("Lantern: {:?}", lantern),
ItemKind::Armor(armor) => format!("Armor: {:?}", armor.stats),
ItemKind::Glider => "Glider:".to_string(),
ItemKind::Consumable { kind, .. } => {
format!("Consumable: {:?}", kind)
},
ItemKind::Throwable { kind } => format!("Throwable: {:?}", kind),
ItemKind::Utility { kind } => format!("Utility: {:?}", kind),
ItemKind::Ingredient { descriptor } => format!("Ingredient: {}", descriptor),
ItemKind::TagExamples { item_ids } => format!("TagExamples: {:?}", item_ids),
};
result
}
}
pub type ItemId = AtomicCell<Option<NonZeroU64>>;

View File

@ -1,7 +1,7 @@
use core::ops::Not;
use serde::{Deserialize, Serialize};
use specs::{Component, DerefFlaggedStorage};
use std::{convert::TryFrom, mem, ops::Range};
use std::{cmp::Ordering, convert::TryFrom, mem, ops::Range};
use tracing::{debug, trace, warn};
use vek::Vec3;
@ -54,6 +54,7 @@ pub enum Error {
pub enum InventorySortOrder {
Name,
Quality,
Category,
Tag,
}
@ -62,11 +63,20 @@ impl InventorySortOrder {
match self {
InventorySortOrder::Name => InventorySortOrder::Quality,
InventorySortOrder::Quality => InventorySortOrder::Tag,
InventorySortOrder::Tag => InventorySortOrder::Name,
InventorySortOrder::Tag => InventorySortOrder::Category,
InventorySortOrder::Category => InventorySortOrder::Name,
}
}
}
pub enum CustomOrder {
Name,
Quality,
KindPartial,
KindFull,
Tag,
}
/// Represents the Inventory of an entity. The inventory has 18 "built-in"
/// slots, with further slots being provided by items equipped in the Loadout
/// sub-struct. Inventory slots are indexed by `InvSlotId` which is
@ -137,6 +147,37 @@ impl Inventory {
)
}
/// If custom_order is empty, it will always return Ordering::Equal
pub fn order_by_custom(custom_order: &[CustomOrder], a: &Item, b: &Item) -> Ordering {
let mut order = custom_order.iter();
let a_quality = a.quality();
let b_quality = b.quality();
let a_kind = a.kind().get_itemkind_string();
let b_kind = b.kind().get_itemkind_string();
let mut cmp = Ordering::Equal;
while cmp == Ordering::Equal {
match order.next() {
Some(CustomOrder::KindFull) => cmp = Ord::cmp(&a_kind, &b_kind),
Some(CustomOrder::KindPartial) => {
cmp = Ord::cmp(
&a_kind.split_once(':').unwrap().0,
&b_kind.split_once(':').unwrap().0,
)
},
Some(CustomOrder::Quality) => cmp = Ord::cmp(&b_quality, &a_quality),
Some(CustomOrder::Name) => cmp = Ord::cmp(&a.name(), &b.name()),
Some(CustomOrder::Tag) => {
cmp = Ord::cmp(
&a.tags().first().map_or("", |tag| tag.name()),
&b.tags().first().map_or("", |tag| tag.name()),
)
},
_ => break,
}
}
cmp
}
/// Sorts the inventory using the next sort order
pub fn sort(&mut self) {
let sort_order = self.next_sort_order;
@ -146,6 +187,15 @@ impl Inventory {
InventorySortOrder::Name => Ord::cmp(&a.name(), &b.name()),
// Quality is sorted in reverse since we want high quality items first
InventorySortOrder::Quality => Ord::cmp(&b.quality(), &a.quality()),
InventorySortOrder::Category => {
let order = [
CustomOrder::KindPartial,
CustomOrder::Quality,
CustomOrder::KindFull,
CustomOrder::Name,
];
Self::order_by_custom(&order, a, b)
},
InventorySortOrder::Tag => Ord::cmp(
&a.tags().first().map_or("", |tag| tag.name()),
&b.tags().first().map_or("", |tag| tag.name()),

View File

@ -824,7 +824,8 @@ impl<'a> Widget for Bag<'a> {
&(match inventory.next_sort_order() {
InventorySortOrder::Name => i18n.get_msg("hud-bag-sort_by_name"),
InventorySortOrder::Quality => i18n.get_msg("hud-bag-sort_by_quality"),
InventorySortOrder::Tag => i18n.get_msg("hud-bag-sort_by_category"),
InventorySortOrder::Category => i18n.get_msg("hud-bag-sort_by_category"),
InventorySortOrder::Tag => i18n.get_msg("hud-bag-sort_by_tag"),
}),
"",
&tooltip,