698 lines
24 KiB
Rust
698 lines
24 KiB
Rust
use std::cmp::Ordering;
|
|
use thiserror::Error;
|
|
use libpso::character::character;//::InventoryItem;
|
|
use crate::entity::character::CharacterEntityId;
|
|
use crate::entity::item::{ItemEntityId, ItemDetail, ItemEntity, ItemType, ItemLocation, InventoryEntity, InventoryItemEntity, EquippedEntity};
|
|
use crate::entity::item::tool::Tool;
|
|
use crate::entity::item::mag::Mag;
|
|
use crate::ship::items::{ClientItemId, BankItem, BankItemHandle};
|
|
use crate::ship::items::floor::{IndividualFloorItem, StackedFloorItem};
|
|
|
|
const INVENTORY_CAPACITY: usize = 30;
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct InventorySlot(pub usize);
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct IndividualInventoryItem {
|
|
pub entity_id: ItemEntityId,
|
|
pub item_id: ClientItemId,
|
|
pub item: ItemDetail,
|
|
pub equipped: bool,
|
|
}
|
|
|
|
impl IndividualInventoryItem {
|
|
pub fn mag(&self) -> Option<&Mag> {
|
|
match self.item {
|
|
ItemDetail::Mag(ref mag) => Some(mag),
|
|
_ => None
|
|
}
|
|
}
|
|
|
|
pub fn mag_mut(&mut self) -> Option<&mut Mag> {
|
|
match self.item {
|
|
ItemDetail::Mag(ref mut mag) => Some(mag),
|
|
_ => None
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct StackedInventoryItem {
|
|
pub entity_ids: Vec<ItemEntityId>,
|
|
pub item_id: ClientItemId,
|
|
pub tool: Tool,
|
|
}
|
|
|
|
impl StackedInventoryItem {
|
|
pub fn count(&self) -> usize {
|
|
self.entity_ids.len()
|
|
}
|
|
|
|
pub fn take_entity_ids(&mut self, amount: usize) -> Option<Vec<ItemEntityId>> {
|
|
if amount <= self.count() {
|
|
Some(self.entity_ids.drain(..amount).collect())
|
|
}
|
|
else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum InventoryItem {
|
|
Individual(IndividualInventoryItem),
|
|
Stacked(StackedInventoryItem),
|
|
}
|
|
|
|
#[derive(Error, Debug, Clone)]
|
|
#[error("")]
|
|
pub enum InventoryItemAddToError {
|
|
BothAreNotStacked,
|
|
DifferentTool,
|
|
ExceedsCapacity,
|
|
}
|
|
|
|
impl InventoryItem {
|
|
pub fn entity_ids(&self) -> Vec<ItemEntityId> {
|
|
match self {
|
|
InventoryItem::Individual(individual_inventory_item) => {
|
|
vec![individual_inventory_item.entity_id]
|
|
},
|
|
InventoryItem::Stacked(stacked_inventory_item) => {
|
|
stacked_inventory_item.entity_ids.clone()
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn item_id(&self) -> ClientItemId {
|
|
match self {
|
|
InventoryItem::Individual(individual_inventory_item) => {
|
|
individual_inventory_item.item_id
|
|
},
|
|
InventoryItem::Stacked(stacked_inventory_item) => {
|
|
stacked_inventory_item.item_id
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn set_item_id(&mut self, item_id: ClientItemId) {
|
|
match self {
|
|
InventoryItem::Individual(individual_inventory_item) => {
|
|
individual_inventory_item.item_id = item_id
|
|
},
|
|
InventoryItem::Stacked(stacked_inventory_item) => {
|
|
stacked_inventory_item.item_id = item_id
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn item_type(&self) -> ItemType {
|
|
match self {
|
|
InventoryItem::Individual(individual_inventory_item) => {
|
|
individual_inventory_item.item.item_type()
|
|
},
|
|
InventoryItem::Stacked(stacked_inventory_item) => {
|
|
ItemType::Tool(stacked_inventory_item.tool.tool)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TOOD: delete?
|
|
pub fn are_same_stackable_tool(&self, other_stacked_item: &StackedFloorItem) -> bool {
|
|
match self {
|
|
InventoryItem::Stacked(self_stacked_item) => {
|
|
self_stacked_item.tool == other_stacked_item.tool
|
|
&& self_stacked_item.tool.is_stackable() && other_stacked_item.tool.is_stackable()
|
|
},
|
|
_ => false
|
|
}
|
|
}
|
|
|
|
// TOOD: delete?
|
|
pub fn can_combine_stacks(&self, other_stacked_item: &StackedFloorItem) -> bool {
|
|
match self {
|
|
InventoryItem::Stacked(self_stacked_item) => {
|
|
self_stacked_item.tool == other_stacked_item.tool
|
|
&& self_stacked_item.tool.is_stackable() && other_stacked_item.tool.is_stackable()
|
|
&& self_stacked_item.count() + other_stacked_item.count() <= self_stacked_item.tool.max_stack()
|
|
},
|
|
_ => false
|
|
}
|
|
}
|
|
|
|
// TODO: result
|
|
// TOOD: delete?
|
|
pub fn combine_stacks(&mut self, other_stacked_item: &mut StackedFloorItem) {
|
|
match self {
|
|
InventoryItem::Stacked(self_stacked_item) => {
|
|
self_stacked_item.entity_ids.append(&mut other_stacked_item.entity_ids);
|
|
},
|
|
_ => {
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn equipped(&self) -> bool {
|
|
match self {
|
|
InventoryItem::Individual(individual_inventory_item) => {
|
|
individual_inventory_item.equipped
|
|
},
|
|
InventoryItem::Stacked(_) => {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn as_client_bytes(&self) -> [u8; 16] {
|
|
match self {
|
|
InventoryItem::Individual(item) => {
|
|
match &item.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(),
|
|
ItemDetail::Mag(m) => m.as_bytes(),
|
|
ItemDetail::ESWeapon(e) => e.as_bytes(),
|
|
}
|
|
},
|
|
InventoryItem::Stacked(item) => {
|
|
item.tool.as_stacked_bytes(item.entity_ids.len())
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn can_add_to(&mut self, stacked_floor_item: &StackedFloorItem) -> Result<(), InventoryItemAddToError> {
|
|
if let InventoryItem::Stacked(stacked_inventory_item) = self {
|
|
if stacked_floor_item.tool != stacked_inventory_item.tool {
|
|
return Err(InventoryItemAddToError::DifferentTool)
|
|
}
|
|
|
|
if stacked_floor_item.tool.tool.max_stack() < (stacked_floor_item.count() + stacked_inventory_item.count()) {
|
|
return Err(InventoryItemAddToError::ExceedsCapacity)
|
|
}
|
|
Ok(())
|
|
}
|
|
else {
|
|
Err(InventoryItemAddToError::BothAreNotStacked)
|
|
}
|
|
}
|
|
|
|
pub fn add_to(&mut self, mut stacked_floor_item: StackedFloorItem) -> Result<(), InventoryItemAddToError> {
|
|
self.can_add_to(&stacked_floor_item)?;
|
|
if let InventoryItem::Stacked(stacked_inventory_item) = self {
|
|
stacked_inventory_item.entity_ids.append(&mut stacked_floor_item.entity_ids);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub fn individual(&mut self) -> Option<&mut IndividualInventoryItem> {
|
|
match self {
|
|
InventoryItem::Individual(ref mut individual_inventory_item) => Some(individual_inventory_item),
|
|
_ => None
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Error, Debug, Clone)]
|
|
#[error("")]
|
|
pub enum InventoryItemConsumeError {
|
|
InconsistentState,
|
|
InvalidAmount,
|
|
}
|
|
|
|
pub struct IndividualConsumedItem {
|
|
pub entity_id: ItemEntityId,
|
|
pub item: ItemDetail,
|
|
}
|
|
|
|
pub struct StackedConsumedItem {
|
|
pub entity_ids: Vec<ItemEntityId>,
|
|
pub tool: Tool
|
|
}
|
|
|
|
pub enum ConsumedItem {
|
|
Individual(IndividualConsumedItem),
|
|
Stacked(StackedConsumedItem),
|
|
}
|
|
|
|
impl ConsumedItem {
|
|
pub fn entity_ids(&self) -> Vec<ItemEntityId> {
|
|
match self {
|
|
ConsumedItem::Individual(individual_consumed_item) => {
|
|
vec![individual_consumed_item.entity_id]
|
|
},
|
|
ConsumedItem::Stacked(stacked_consumed_item) => {
|
|
stacked_consumed_item.entity_ids.clone()
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn item(&self) -> ItemDetail {
|
|
match self {
|
|
ConsumedItem::Individual(individual_consumed_item) => {
|
|
individual_consumed_item.item.clone()
|
|
},
|
|
ConsumedItem::Stacked(stacked_consumed_item) => {
|
|
ItemDetail::Tool(stacked_consumed_item.tool)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
pub struct InventoryItemHandle<'a> {
|
|
inventory: &'a mut CharacterInventory,
|
|
slot: usize,
|
|
}
|
|
|
|
impl<'a> InventoryItemHandle<'a> {
|
|
pub fn item(&'a self) -> Option<&'a InventoryItem> {
|
|
self.inventory.items.get(self.slot)
|
|
}
|
|
|
|
pub fn item_mut(&mut self) -> Option<&mut InventoryItem> {
|
|
self.inventory.items.get_mut(self.slot)
|
|
}
|
|
|
|
pub fn remove_from_inventory(self) {
|
|
self.inventory.items.remove(self.slot);
|
|
}
|
|
|
|
pub fn consume(self, amount: usize) -> Result<ConsumedItem, InventoryItemConsumeError> {
|
|
enum RemoveMethod {
|
|
EntireThing(ConsumedItem),
|
|
Partial(Tool),
|
|
}
|
|
|
|
let inventory_item = self.inventory.items.get(self.slot).ok_or(InventoryItemConsumeError::InconsistentState)?;
|
|
let remove_method = match inventory_item {
|
|
InventoryItem::Individual(individual_inventory_item) => {
|
|
RemoveMethod::EntireThing(ConsumedItem::Individual(IndividualConsumedItem {
|
|
entity_id: individual_inventory_item.entity_id,
|
|
item: individual_inventory_item.item.clone()
|
|
}))
|
|
},
|
|
InventoryItem::Stacked(stacked_inventory_item) => {
|
|
match stacked_inventory_item.count().cmp(&amount) {
|
|
Ordering::Equal => {
|
|
RemoveMethod::EntireThing(ConsumedItem::Stacked(StackedConsumedItem {
|
|
entity_ids: stacked_inventory_item.entity_ids.clone(),
|
|
tool: stacked_inventory_item.tool,
|
|
}))
|
|
},
|
|
Ordering::Greater => {
|
|
RemoveMethod::Partial(stacked_inventory_item.tool)
|
|
},
|
|
Ordering::Less => {
|
|
return Err(InventoryItemConsumeError::InvalidAmount)
|
|
}
|
|
}
|
|
},
|
|
};
|
|
|
|
match remove_method {
|
|
RemoveMethod::EntireThing(consumed_item) => {
|
|
self.inventory.items.remove(self.slot);
|
|
Ok(consumed_item)
|
|
},
|
|
RemoveMethod::Partial(tool) => {
|
|
let entity_ids = self.inventory.items.get_mut(self.slot)
|
|
.and_then(|item| {
|
|
if let InventoryItem::Stacked(stacked_inventory_item) = item {
|
|
Some(stacked_inventory_item.entity_ids.drain(..amount).collect::<Vec<_>>())
|
|
}
|
|
else {
|
|
None
|
|
}
|
|
})
|
|
.ok_or(InventoryItemConsumeError::InvalidAmount)?;
|
|
Ok(ConsumedItem::Stacked(StackedConsumedItem {
|
|
entity_ids: entity_ids,
|
|
tool: tool,
|
|
}))
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn get_slot(&self) -> usize {
|
|
self.slot
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
pub struct CharacterInventory {
|
|
item_id_counter: u32,
|
|
items: Vec<InventoryItem>,
|
|
}
|
|
|
|
impl CharacterInventory {
|
|
pub fn new(items: Vec<InventoryItem>) -> CharacterInventory {
|
|
CharacterInventory{
|
|
item_id_counter: 0,
|
|
items: items,
|
|
}
|
|
}
|
|
|
|
pub fn initialize_item_ids(&mut self, base_item_id: u32) {
|
|
for (i, item) in self.items.iter_mut().enumerate() {
|
|
item.set_item_id(ClientItemId(base_item_id + i as u32));
|
|
}
|
|
self.item_id_counter = base_item_id + self.items.len() as u32 + 1;
|
|
}
|
|
|
|
pub fn as_client_inventory_items(&self) -> [character::InventoryItem; 30] {
|
|
self.items.iter()
|
|
.enumerate()
|
|
.fold([character::InventoryItem::default(); 30], |mut inventory, (slot, item)| {
|
|
let bytes = item.as_client_bytes();
|
|
inventory[slot].data1.copy_from_slice(&bytes[0..12]);
|
|
inventory[slot].data2.copy_from_slice(&bytes[12..16]);
|
|
inventory[slot].item_id = item.item_id().0;
|
|
// does this do anything?
|
|
inventory[slot].equipped = if item.equipped() { 1 } else { 0 };
|
|
// because this actually equips the item
|
|
inventory[slot].flags |= if item.equipped() { 8 } else { 0 };
|
|
inventory
|
|
})
|
|
}
|
|
|
|
pub fn slot(&self, slot: usize) -> Option<&InventoryItem> {
|
|
self.items.get(slot)
|
|
}
|
|
|
|
pub fn count(&self) -> usize {
|
|
self.items.len()
|
|
}
|
|
|
|
pub fn get_item_handle_by_id<'a>(&'a mut self, item_id: ClientItemId) -> Option<InventoryItemHandle<'a>> {
|
|
let (slot, _) = self.items.iter()
|
|
.enumerate()
|
|
.filter(|(_, item)| {
|
|
item.item_id() == item_id
|
|
})
|
|
.nth(0)?;
|
|
Some(InventoryItemHandle {
|
|
inventory: self,
|
|
slot: slot,
|
|
})
|
|
}
|
|
|
|
pub fn get_equipped_mag_handle<'a>(&'a mut self) -> Option<InventoryItemHandle<'a>> {
|
|
let (slot, _) = self.items.iter()
|
|
.enumerate()
|
|
.filter(|(_, item)| {
|
|
if let InventoryItem::Individual(individual_inventory_item) = item {
|
|
if let ItemDetail::Mag(_) = &individual_inventory_item.item {
|
|
return individual_inventory_item.equipped
|
|
}
|
|
}
|
|
false
|
|
})
|
|
.nth(0)?;
|
|
Some(InventoryItemHandle {
|
|
inventory: self,
|
|
slot: slot,
|
|
})
|
|
}
|
|
|
|
pub fn get_equipped_armor_handle<'a>(&'a mut self) -> Option<InventoryItemHandle<'a>> {
|
|
let (slot, _) = self.items.iter()
|
|
.enumerate()
|
|
.filter(|(_, item)| {
|
|
if let InventoryItem::Individual(individual_inventory_item) = item {
|
|
if let ItemDetail::Armor(_) = &individual_inventory_item.item {
|
|
return individual_inventory_item.equipped
|
|
}
|
|
}
|
|
false
|
|
})
|
|
.nth(0)?;
|
|
Some(InventoryItemHandle {
|
|
inventory: self,
|
|
slot: slot,
|
|
})
|
|
}
|
|
|
|
pub fn get_equipped_shield_handle<'a>(&'a mut self) -> Option<InventoryItemHandle<'a>> {
|
|
let (slot, _) = self.items.iter()
|
|
.enumerate()
|
|
.filter(|(_, item)| {
|
|
if let InventoryItem::Individual(individual_inventory_item) = item {
|
|
if let ItemDetail::Shield(_) = &individual_inventory_item.item {
|
|
return individual_inventory_item.equipped
|
|
}
|
|
}
|
|
false
|
|
})
|
|
.nth(0)?;
|
|
Some(InventoryItemHandle {
|
|
inventory: self,
|
|
slot: slot,
|
|
})
|
|
}
|
|
|
|
pub fn get_equipped_weapon_handle<'a>(&'a mut self) -> Option<InventoryItemHandle<'a>> {
|
|
let (slot, _) = self.items.iter()
|
|
.enumerate()
|
|
.filter(|(_, item)| {
|
|
if let InventoryItem::Individual(individual_inventory_item) = item {
|
|
if let ItemDetail::Weapon(_) = &individual_inventory_item.item {
|
|
return individual_inventory_item.equipped
|
|
}
|
|
}
|
|
false
|
|
})
|
|
.nth(0)?;
|
|
Some(InventoryItemHandle {
|
|
inventory: self,
|
|
slot: slot,
|
|
})
|
|
}
|
|
|
|
pub fn get_item_by_id(&self, item_id: ClientItemId) -> Option<&InventoryItem> {
|
|
self.items.iter()
|
|
.filter(|item| {
|
|
item.item_id() == item_id
|
|
})
|
|
.nth(0)
|
|
}
|
|
|
|
pub fn take_item_by_id(&mut self, item_id: ClientItemId) -> Option<InventoryItem> {
|
|
self.items
|
|
.drain_filter(|i| i.item_id() == item_id)
|
|
.nth(0)
|
|
}
|
|
|
|
pub fn add_item(&mut self, item: InventoryItem) -> Result<(), ()> { // TODO: errors
|
|
// TODO: check slot conflict?
|
|
self.items.push(item);
|
|
Ok(())
|
|
}
|
|
|
|
// TODO: should these pick up functions take floor_item as mut and remove the ids?
|
|
pub fn pick_up_individual_floor_item(&mut self, floor_item: &IndividualFloorItem) -> Option<(&IndividualInventoryItem, InventorySlot)> {
|
|
if self.count() >= 30 {
|
|
return None;
|
|
}
|
|
|
|
self.items.push(InventoryItem::Individual(IndividualInventoryItem {
|
|
entity_id: floor_item.entity_id,
|
|
item_id: floor_item.item_id,
|
|
item: floor_item.item.clone(),
|
|
equipped: false,
|
|
}));
|
|
|
|
if let Some(InventoryItem::Individual(new_item)) = self.items.last() {
|
|
Some((new_item, InventorySlot(self.count()-1)))
|
|
}
|
|
else {
|
|
None
|
|
}
|
|
}
|
|
|
|
// TODO: can be simplified using find instead of position
|
|
pub fn pick_up_stacked_floor_item(&mut self, floor_item: &StackedFloorItem) -> Option<(&StackedInventoryItem, InventorySlot)> {
|
|
let existing_stack_position = self.items.iter()
|
|
.position(|inventory_item| {
|
|
if let InventoryItem::Stacked(stacked_inventory_item) = inventory_item {
|
|
if stacked_inventory_item.tool == floor_item.tool {
|
|
return true
|
|
}
|
|
}
|
|
false
|
|
});
|
|
|
|
if let Some(existing_stack_position) = existing_stack_position {
|
|
if let Some(InventoryItem::Stacked(stacked_item)) = self.items.get_mut(existing_stack_position) {
|
|
if stacked_item.count() + floor_item.count() <= stacked_item.tool.max_stack() {
|
|
stacked_item.entity_ids.append(&mut floor_item.entity_ids.clone());
|
|
Some((stacked_item, InventorySlot(existing_stack_position)))
|
|
}
|
|
else {
|
|
None
|
|
}
|
|
}
|
|
else {
|
|
None
|
|
}
|
|
}
|
|
else {
|
|
let new_stacked_item = InventoryItem::Stacked(StackedInventoryItem {
|
|
entity_ids: floor_item.entity_ids.clone(),
|
|
item_id: floor_item.item_id,
|
|
tool: floor_item.tool,
|
|
});
|
|
|
|
self.items.push(new_stacked_item);
|
|
if let Some(InventoryItem::Stacked(new_item)) = self.items.last() {
|
|
Some((new_item, InventorySlot(self.count()-1)))
|
|
}
|
|
else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn withdraw_item(&mut self, mut bank_item: BankItemHandle, amount: usize) -> Option<(&InventoryItem, usize)> {
|
|
let (remove, slot) = match bank_item.item_mut()? {
|
|
BankItem::Individual(individual_bank_item) => {
|
|
if self.items.len() >= INVENTORY_CAPACITY {
|
|
return None
|
|
}
|
|
self.items.push(InventoryItem::Individual(IndividualInventoryItem {
|
|
entity_id: individual_bank_item.entity_id,
|
|
item_id: individual_bank_item.item_id,
|
|
item: individual_bank_item.item.clone(),
|
|
equipped: false,
|
|
}));
|
|
(true, self.count()-1)
|
|
},
|
|
BankItem::Stacked(stacked_bank_item) => {
|
|
let existing_inventory_item = self.items.iter_mut()
|
|
.enumerate()
|
|
.find_map(|(index, item)| {
|
|
if let InventoryItem::Stacked(stacked_inventory_item) = item {
|
|
if stacked_inventory_item.tool == stacked_inventory_item.tool {
|
|
return Some((index, stacked_inventory_item))
|
|
}
|
|
}
|
|
None
|
|
});
|
|
|
|
let slot = match existing_inventory_item {
|
|
Some((slot, stacked_inventory_item)) => {
|
|
if stacked_inventory_item.count() + stacked_bank_item.count() > stacked_bank_item.tool.max_stack() {
|
|
return None
|
|
}
|
|
|
|
let mut withdrawn_entity_ids = stacked_bank_item.take_entity_ids(amount)?;
|
|
stacked_inventory_item.entity_ids.append(&mut withdrawn_entity_ids);
|
|
slot
|
|
}
|
|
None => {
|
|
if self.items.len() >= INVENTORY_CAPACITY {
|
|
return None
|
|
}
|
|
let withdrawn_entity_ids = stacked_bank_item.take_entity_ids(amount)?;
|
|
|
|
self.item_id_counter += 1; // oh no
|
|
self.items.push(InventoryItem::Stacked(StackedInventoryItem {
|
|
entity_ids: withdrawn_entity_ids,
|
|
item_id: ClientItemId(self.item_id_counter),
|
|
tool: stacked_bank_item.tool,
|
|
}));
|
|
self.count()-1
|
|
}
|
|
};
|
|
(stacked_bank_item.count() == 0, slot)
|
|
}
|
|
};
|
|
|
|
if remove {
|
|
bank_item.remove_from_bank();
|
|
}
|
|
|
|
self.items.last().map(|item| {
|
|
(item, slot)
|
|
})
|
|
}
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &InventoryItem> {
|
|
self.items.iter()
|
|
}
|
|
|
|
pub fn items(&self) -> &Vec<InventoryItem> {
|
|
&self.items
|
|
}
|
|
|
|
pub fn set_items(&mut self, sorted_items: Vec<InventoryItem>) {
|
|
self.items = sorted_items;
|
|
}
|
|
|
|
pub fn as_inventory_entity(&self, character_id: &CharacterEntityId) -> InventoryEntity {
|
|
InventoryEntity {
|
|
items: self.items.iter()
|
|
.map(|item| {
|
|
match item {
|
|
InventoryItem::Individual(item) => {
|
|
InventoryItemEntity::Individual(ItemEntity {
|
|
id: item.entity_id,
|
|
location: ItemLocation::Inventory {
|
|
character_id: *character_id,
|
|
},
|
|
item: item.item.clone(),
|
|
})
|
|
},
|
|
InventoryItem::Stacked(items) => {
|
|
InventoryItemEntity::Stacked(items.entity_ids.iter()
|
|
.map(|id| {
|
|
ItemEntity {
|
|
id: *id,
|
|
location: ItemLocation::Inventory {
|
|
character_id: *character_id,
|
|
},
|
|
item: ItemDetail::Tool(items.tool)
|
|
}
|
|
})
|
|
.collect())
|
|
},
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
pub fn as_equipped_entity(&self) -> EquippedEntity {
|
|
self.items.iter()
|
|
.fold((EquippedEntity::default(), 0), |(mut equipped, mut unit_slot), item| {
|
|
if let InventoryItem::Individual(individual_item) = item {
|
|
if individual_item.equipped {
|
|
match individual_item.item {
|
|
ItemDetail::Weapon(_) => equipped.weapon = Some(individual_item.entity_id),
|
|
ItemDetail::Armor(_) => equipped.armor = Some(individual_item.entity_id),
|
|
ItemDetail::Shield(_) => equipped.shield = Some(individual_item.entity_id),
|
|
ItemDetail::Unit(_) => {
|
|
equipped.unit[unit_slot] = Some(individual_item.entity_id);
|
|
unit_slot += 1;
|
|
}
|
|
ItemDetail::Mag(_) => equipped.mag = Some(individual_item.entity_id),
|
|
_ => {},
|
|
}
|
|
}
|
|
}
|
|
(equipped, unit_slot)
|
|
}).0
|
|
}
|
|
}
|
|
|