|
@ -1,244 +1,186 @@ |
|
|
use std::collections::HashMap;
|
|
|
use std::collections::HashMap;
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
|
|
|
|
|
|
|
|
use libpso::character::character::InventoryItem;
|
|
|
use libpso::character::character::InventoryItem;
|
|
|
|
|
|
|
|
|
use crate::entity::item::{Item, ItemDetail, ItemLocation};
|
|
|
|
|
|
|
|
|
use crate::entity::gateway::EntityGateway;
|
|
|
|
|
|
use crate::entity::character::Character;
|
|
|
|
|
|
use crate::entity::item::{Item, ItemId, ItemDetail, ItemLocation};
|
|
|
use crate::entity::item::weapon::Weapon;
|
|
|
use crate::entity::item::weapon::Weapon;
|
|
|
use crate::entity::item::armor::Armor;
|
|
|
use crate::entity::item::armor::Armor;
|
|
|
use crate::entity::item::shield::Shield;
|
|
|
use crate::entity::item::shield::Shield;
|
|
|
use crate::entity::item::unit::Unit;
|
|
|
use crate::entity::item::unit::Unit;
|
|
|
use crate::entity::item::tool::StackedTool;
|
|
|
|
|
|
|
|
|
use crate::entity::item::tool::Tool;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn are_items_same_type(itema: &Item, itemb: &Item) -> bool {
|
|
|
|
|
|
match (&itema.item, &itemb.item) {
|
|
|
|
|
|
(ItemDetail::Weapon(a), ItemDetail::Weapon(b)) => a.weapon == b.weapon,
|
|
|
|
|
|
(ItemDetail::Armor(a), ItemDetail::Armor(b)) => a.armor == b.armor,
|
|
|
|
|
|
(ItemDetail::Shield(a), ItemDetail::Shield(b)) => a.shield == b.shield,
|
|
|
|
|
|
(ItemDetail::Unit(a), ItemDetail::Unit(b)) => a.unit == b.unit,
|
|
|
|
|
|
(ItemDetail::Tool(a), ItemDetail::Tool(b)) => a.tool == b.tool,
|
|
|
|
|
|
_ => false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
pub struct ActiveItemId(u32);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Stacked(count, itemtype Vec<ItemId>)?
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
pub enum StackedItem {
|
|
|
pub enum StackedItem {
|
|
|
Individual(Item),
|
|
|
Individual(Item),
|
|
|
Stacked(Vec<Item>),
|
|
|
Stacked(Vec<Item>),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl StackedItem {
|
|
|
|
|
|
fn is_same_item_type(&self, item: &Item) -> bool {
|
|
|
|
|
|
match self {
|
|
|
|
|
|
StackedItem::Individual(i) => are_items_same_type(i, item),
|
|
|
|
|
|
StackedItem::Stacked(i) => i.iter().all(|k| are_items_same_type(k, item))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
|
|
|
|
|
|
pub struct ActiveItemId(u32);
|
|
|
|
|
|
|
|
|
fn index(&self) -> usize {
|
|
|
|
|
|
match self {
|
|
|
|
|
|
StackedItem::Individual(Item {location: ItemLocation::Inventory {index, ..}, ..}) => *index,
|
|
|
|
|
|
StackedItem::Stacked(items) => {
|
|
|
|
|
|
match items[0].location {
|
|
|
|
|
|
ItemLocation::Inventory {index, ..} => index,
|
|
|
|
|
|
_ => panic!()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => panic!()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn as_bytes(&self) -> [u8; 16] {
|
|
|
|
|
|
match self {
|
|
|
|
|
|
StackedItem::Individual(item) => {
|
|
|
|
|
|
item.item.as_bytes()
|
|
|
|
|
|
}
|
|
|
|
|
|
StackedItem::Stacked(items) => {
|
|
|
|
|
|
let count = items.len();
|
|
|
|
|
|
match &items[0].item {
|
|
|
|
|
|
ItemDetail::Tool(tool) => {
|
|
|
|
|
|
StackedTool {
|
|
|
|
|
|
tool: tool.tool,
|
|
|
|
|
|
count: count
|
|
|
|
|
|
}.as_bytes()
|
|
|
|
|
|
},
|
|
|
|
|
|
_ => panic!()
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
pub struct ActiveItem {
|
|
|
|
|
|
id: ActiveItemId,
|
|
|
|
|
|
item: StackedItem,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ActiveItem {
|
|
|
|
|
|
pub fn as_client_bytes(&self) -> [u8; 16] {
|
|
|
|
|
|
match &self.item {
|
|
|
|
|
|
StackedItem::Individual(i) => {
|
|
|
|
|
|
match &i.item {
|
|
|
|
|
|
ItemDetail::Weapon(w) => w.as_bytes(),
|
|
|
|
|
|
ItemDetail::Armor(a) => a.as_bytes(),
|
|
|
|
|
|
ItemDetail::Shield(s) => s.as_bytes(),
|
|
|
|
|
|
ItemDetail::Unit(u) => u.as_bytes(),
|
|
|
|
|
|
ItemDetail::Tool(t) => t.as_individual_bytes(),
|
|
|
|
|
|
ItemDetail::TechniqueDisk(d) => d.as_bytes(),
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
StackedItem::Stacked(i) => {
|
|
|
|
|
|
let len = i.len();
|
|
|
|
|
|
match &i[0].item {
|
|
|
|
|
|
ItemDetail::Tool(t) => t.as_stacked_bytes(len),
|
|
|
|
|
|
_ => panic!(),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ActiveInventory(Vec<ActiveItem>);
|
|
|
|
|
|
|
|
|
pub struct ItemActivator {
|
|
|
|
|
|
id: u32,
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ItemActivator {
|
|
|
|
|
|
pub fn new() -> ItemActivator {
|
|
|
|
|
|
ItemActivator {
|
|
|
|
|
|
id: 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
impl ActiveInventory {
|
|
|
|
|
|
pub fn as_client_inventory_items(&self) -> [InventoryItem; 30] {
|
|
|
|
|
|
self.0.iter()
|
|
|
|
|
|
.enumerate()
|
|
|
|
|
|
.fold([InventoryItem::default(); 30], |mut inventory, (index, item)| {
|
|
|
|
|
|
let bytes = item.as_client_bytes();
|
|
|
|
|
|
inventory[index].data1.copy_from_slice(&bytes[0..12]);
|
|
|
|
|
|
inventory[index].item_id = item.id.0;
|
|
|
|
|
|
|
|
|
|
|
|
// does this do anything?
|
|
|
|
|
|
inventory[index].equipped = match item.item {
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Weapon(Weapon {equipped: true, ..}), ..}) => 1,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Armor(Armor {equipped: true, ..}), ..}) => 1,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Shield(Shield {equipped: true, ..}), ..}) => 1,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Unit(Unit{equipped: true, ..}), ..}) => 1,
|
|
|
|
|
|
//StackedItem::Individual(Item {item: ItemDetail::Mag(Mag{equipped: true, ..}), ..}) => 1,
|
|
|
|
|
|
_ => 0,
|
|
|
|
|
|
};
|
|
|
|
|
|
// because this actually equips the item
|
|
|
|
|
|
inventory[index].flags |= match item.item {
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Weapon(Weapon {equipped: true, ..}), ..}) => 8,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Armor(Armor {equipped: true, ..}), ..}) => 8,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Shield(Shield {equipped: true, ..}), ..}) => 8,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Unit(Unit {equipped: true, ..}), ..}) => 8,
|
|
|
|
|
|
//StackedItem::Individual(Item {item: ItemDetail::Mag(Mag{equipped: true, ..}), ..}) => 8,
|
|
|
|
|
|
_ => 0,
|
|
|
|
|
|
};
|
|
|
|
|
|
inventory
|
|
|
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn activate_item(&mut self, item: StackedItem) -> ActiveItem {
|
|
|
|
|
|
self.id += 1;
|
|
|
|
|
|
ActiveItem {
|
|
|
|
|
|
id: ActiveItemId(self.id),
|
|
|
|
|
|
item: item,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
|
|
self.0.len()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
pub struct ActiveItem {
|
|
|
|
|
|
pub id: ActiveItemId,
|
|
|
|
|
|
pub item: StackedItem,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct StackedItemKey(Item);
|
|
|
|
|
|
|
|
|
|
|
|
impl Hash for StackedItemKey {
|
|
|
|
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
|
|
|
|
match &self.0.item {
|
|
|
|
|
|
ItemDetail::Weapon(w) => w.weapon.value().hash(hasher),
|
|
|
|
|
|
ItemDetail::Armor(a) => a.armor.value().hash(hasher),
|
|
|
|
|
|
ItemDetail::Shield(s) => s.shield.value().hash(hasher),
|
|
|
|
|
|
ItemDetail::Unit(u) => u.unit.value().hash(hasher),
|
|
|
|
|
|
ItemDetail::Tool(t) => t.tool.value().hash(hasher),
|
|
|
|
|
|
ItemDetail::TechniqueDisk(t) => t.tech.hash(hasher),
|
|
|
|
|
|
|
|
|
fn inventory_item_index(item: &StackedItem) -> usize {
|
|
|
|
|
|
match item {
|
|
|
|
|
|
StackedItem::Individual(i) => {
|
|
|
|
|
|
match i.location {
|
|
|
|
|
|
ItemLocation::Inventory{index: index, ..} => index,
|
|
|
|
|
|
_ => panic!()
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
StackedItem::Stacked(i) => {
|
|
|
|
|
|
match i[0].location {
|
|
|
|
|
|
ItemLocation::Inventory{index: index, ..} => index,
|
|
|
|
|
|
_ => panic!()
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl std::cmp::PartialEq for StackedItemKey {
|
|
|
|
|
|
fn eq(&self, other: &StackedItemKey) -> bool {
|
|
|
|
|
|
are_items_same_type(&self.0, &other.0)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Eq for StackedItemKey {}
|
|
|
|
|
|
|
|
|
fn stack_items(items: Vec<Item>) -> Vec<StackedItem> {
|
|
|
|
|
|
let mut stacks = HashMap::new();
|
|
|
|
|
|
|
|
|
pub fn stack_items(items: Vec<Item>) -> Vec<StackedItem> {
|
|
|
|
|
|
items.into_iter()
|
|
|
|
|
|
.fold(HashMap::new(), |mut stacked: HashMap<StackedItemKey, Vec<Item>>, item| {
|
|
|
|
|
|
stacked.entry(StackedItemKey(item.clone()))
|
|
|
|
|
|
.and_modify(|stack| stack.push(item.clone()))
|
|
|
|
|
|
.or_insert_with(|| {
|
|
|
|
|
|
vec![item]
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
for item in items {
|
|
|
|
|
|
stacks.entry(item.item.item_type()).or_insert(Vec::new()).push(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
stacked
|
|
|
|
|
|
})
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
.map(|(_k, v)| {
|
|
|
|
|
|
v
|
|
|
|
|
|
})
|
|
|
|
|
|
.fold(Vec::new(), |mut stacked, item| {
|
|
|
|
|
|
if item[0].item.is_stackable() {
|
|
|
|
|
|
stacked.push(StackedItem::Stacked(item))
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
stacked.append(&mut item.into_iter().map(|k| StackedItem::Individual(k)).collect())
|
|
|
|
|
|
|
|
|
stacks.into_iter()
|
|
|
|
|
|
.map(|(itype, items)| {
|
|
|
|
|
|
match items[0].item.is_stackable() {
|
|
|
|
|
|
true => {
|
|
|
|
|
|
vec![StackedItem::Stacked(items)]
|
|
|
|
|
|
},
|
|
|
|
|
|
false => {
|
|
|
|
|
|
items.into_iter().map(|i| {
|
|
|
|
|
|
StackedItem::Individual(i)
|
|
|
|
|
|
}).collect()
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
stacked
|
|
|
|
|
|
})
|
|
|
})
|
|
|
|
|
|
.flatten()
|
|
|
|
|
|
.collect()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ActiveBank([Option<ActiveItemId>; 200]);
|
|
|
|
|
|
|
|
|
pub enum InventoryAddError {
|
|
|
|
|
|
InventoryFull,
|
|
|
|
|
|
FullToolStack,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub enum InventoryRemoveError {
|
|
|
|
|
|
NoItemInInventory,
|
|
|
|
|
|
|
|
|
pub struct ActiveItemDatabase {
|
|
|
|
|
|
id: u32,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Inventory([Option<ActiveItem>; 30]);
|
|
|
|
|
|
|
|
|
|
|
|
impl Inventory {
|
|
|
|
|
|
pub fn new(items: Vec<ActiveItem>) -> Inventory {
|
|
|
|
|
|
items.into_iter()
|
|
|
|
|
|
.fold(Inventory([None; 30]), |mut inventory, item| {
|
|
|
|
|
|
let index = item.item.index();
|
|
|
|
|
|
inventory.0[index] = Some(item);
|
|
|
|
|
|
inventory
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
impl ActiveItemDatabase {
|
|
|
|
|
|
pub fn new() -> ActiveItemDatabase {
|
|
|
|
|
|
ActiveItemDatabase {
|
|
|
|
|
|
id: 0,
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
|
|
self.0.iter()
|
|
|
|
|
|
.filter(|k| k.is_some())
|
|
|
|
|
|
.count()
|
|
|
|
|
|
|
|
|
fn activate_item(&mut self, item: StackedItem) -> ActiveItem {
|
|
|
|
|
|
self.id += 1;
|
|
|
|
|
|
ActiveItem {
|
|
|
|
|
|
id: ActiveItemId(self.id),
|
|
|
|
|
|
item: item,
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn as_client_inventory_items(&self) -> [InventoryItem; 30] {
|
|
|
|
|
|
self.0.iter()
|
|
|
|
|
|
.enumerate()
|
|
|
|
|
|
.fold([InventoryItem::default(); 30], |mut inventory, (index, item)| {
|
|
|
|
|
|
if let Some(i) = item {
|
|
|
|
|
|
let bytes = i.item.as_bytes();
|
|
|
|
|
|
inventory[index].data1.copy_from_slice(&bytes[0..12]);
|
|
|
|
|
|
inventory[index].item_id = i.id.0;
|
|
|
|
|
|
|
|
|
// deactivate item
|
|
|
|
|
|
|
|
|
// does this do anything?
|
|
|
|
|
|
inventory[index].equipped = match i.item {
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Weapon(Weapon {equipped: true, ..}), ..}) => 1,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Armor(Armor {equipped: true, ..}), ..}) => 1,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Shield(Shield {equipped: true, ..}), ..}) => 1,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Unit(Unit{equipped: true, ..}), ..}) => 1,
|
|
|
|
|
|
_ => 0,
|
|
|
|
|
|
};
|
|
|
|
|
|
// because this actually equips the item
|
|
|
|
|
|
inventory[index].flags |= match i.item {
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Weapon(Weapon {equipped: true, ..}), ..}) => 8,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Armor(Armor {equipped: true, ..}), ..}) => 8,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Shield(Shield {equipped: true, ..}), ..}) => 8,
|
|
|
|
|
|
StackedItem::Individual(Item {item: ItemDetail::Unit(Unit {equipped: true, ..}), ..}) => 8,
|
|
|
|
|
|
_ => 0,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
pub fn get_character_inventory<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &Character) -> ActiveInventory {
|
|
|
|
|
|
let items = entity_gateway.get_items_by_character(&character);
|
|
|
|
|
|
let inventory_items = items.into_iter()
|
|
|
|
|
|
.filter(|item| {
|
|
|
|
|
|
match item.location {
|
|
|
|
|
|
ItemLocation::Inventory{..} => true,
|
|
|
|
|
|
_ => false,
|
|
|
}
|
|
|
}
|
|
|
inventory
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
}).collect();
|
|
|
|
|
|
let mut stacked = stack_items(inventory_items);
|
|
|
|
|
|
stacked.sort_by(|a, b| {
|
|
|
|
|
|
inventory_item_index(a).partial_cmp(&inventory_item_index(b)).unwrap()
|
|
|
|
|
|
});
|
|
|
|
|
|
let activated = stacked.into_iter().map(|i| self.activate_item(i));
|
|
|
|
|
|
ActiveInventory(activated.take(30).collect())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Bank {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn split_items_into_inventory_and_bank(items: Vec<Item>) -> (Vec<Item>, Vec<Item>) {
|
|
|
|
|
|
items.into_iter().partition(|item| {
|
|
|
|
|
|
match item.location {
|
|
|
|
|
|
ItemLocation::Inventory{..} => true,
|
|
|
|
|
|
ItemLocation::Bank{..} => false,
|
|
|
|
|
|
ItemLocation::Floor{..} => panic!("oh god what happened"),
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
mod test {
|
|
|
mod test {
|
|
|
use super::*;
|
|
|
use super::*;
|
|
|
use crate::entity::item;
|
|
|
use crate::entity::item;
|
|
|
use crate::entity::item::{Item, ItemDetail, ItemEntityId, ItemLocation, Tool};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use crate::entity::item::{Item, ItemDetail, ItemEntityId, ItemLocation};
|
|
|
#[test]
|
|
|
#[test]
|
|
|
fn test_stacked_items() {
|
|
|
|
|
|
|
|
|
fn test_stack_items() {
|
|
|
let item1 = Item {
|
|
|
let item1 = Item {
|
|
|
id: ItemEntityId(1),
|
|
|
id: ItemEntityId(1),
|
|
|
location: ItemLocation::Inventory {
|
|
|
location: ItemLocation::Inventory {
|
|
@ -344,7 +286,6 @@ mod test { |
|
|
tool: item::tool::ToolType::Monomate,
|
|
|
tool: item::tool::ToolType::Monomate,
|
|
|
})
|
|
|
})
|
|
|
};
|
|
|
};
|
|
|
|
|
|
|
|
|
let item_vec = vec![item1.clone(), item2.clone(), item3.clone(), item4.clone(), item5.clone(), item6.clone(), item7.clone(), item8.clone(), item9.clone()];
|
|
|
let item_vec = vec![item1.clone(), item2.clone(), item3.clone(), item4.clone(), item5.clone(), item6.clone(), item7.clone(), item8.clone(), item9.clone()];
|
|
|
|
|
|
|
|
|
let stacked = stack_items(item_vec);
|
|
|
let stacked = stack_items(item_vec);
|
|
|