|
@ -1,11 +1,13 @@ |
|
|
use std::cmp::Ordering;
|
|
|
use std::cmp::Ordering;
|
|
|
use thiserror::Error;
|
|
|
use thiserror::Error;
|
|
|
use libpso::character::character;//::InventoryItem;
|
|
|
use libpso::character::character;//::InventoryItem;
|
|
|
use crate::entity::item::{ItemEntityId, ItemDetail};
|
|
|
|
|
|
|
|
|
use crate::entity::item::{ItemEntityId, ItemDetail, ItemType};
|
|
|
use crate::entity::item::tool::Tool;
|
|
|
use crate::entity::item::tool::Tool;
|
|
|
use crate::ship::items::ClientItemId;
|
|
|
|
|
|
|
|
|
use crate::ship::items::{ClientItemId, BankItem, BankItemHandle};
|
|
|
use crate::ship::items::floor::{IndividualFloorItem, StackedFloorItem};
|
|
|
use crate::ship::items::floor::{IndividualFloorItem, StackedFloorItem};
|
|
|
|
|
|
|
|
|
|
|
|
const INVENTORY_CAPACITY: usize = 30;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
#[derive(Debug, Clone)]
|
|
|
pub struct InventorySlot(pub usize);
|
|
|
pub struct InventorySlot(pub usize);
|
|
@ -29,6 +31,15 @@ impl StackedInventoryItem { |
|
|
pub fn count(&self) -> usize {
|
|
|
pub fn count(&self) -> usize {
|
|
|
self.entity_ids.len()
|
|
|
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)]
|
|
|
#[derive(Debug, Clone)]
|
|
@ -68,6 +79,18 @@ impl InventoryItem { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
pub fn are_same_stackable_tool(&self, other_stacked_item: &StackedFloorItem) -> bool {
|
|
|
match self {
|
|
|
match self {
|
|
|
InventoryItem::Stacked(self_stacked_item) => {
|
|
|
InventoryItem::Stacked(self_stacked_item) => {
|
|
@ -78,6 +101,7 @@ impl InventoryItem { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TOOD: delete?
|
|
|
pub fn can_combine_stacks(&self, other_stacked_item: &StackedFloorItem) -> bool {
|
|
|
pub fn can_combine_stacks(&self, other_stacked_item: &StackedFloorItem) -> bool {
|
|
|
match self {
|
|
|
match self {
|
|
|
InventoryItem::Stacked(self_stacked_item) => {
|
|
|
InventoryItem::Stacked(self_stacked_item) => {
|
|
@ -90,6 +114,7 @@ impl InventoryItem { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// TODO: result
|
|
|
// TODO: result
|
|
|
|
|
|
// TOOD: delete?
|
|
|
pub fn combine_stacks(&mut self, other_stacked_item: &mut StackedFloorItem) {
|
|
|
pub fn combine_stacks(&mut self, other_stacked_item: &mut StackedFloorItem) {
|
|
|
match self {
|
|
|
match self {
|
|
|
InventoryItem::Stacked(self_stacked_item) => {
|
|
|
InventoryItem::Stacked(self_stacked_item) => {
|
|
@ -163,11 +188,46 @@ pub enum InventoryItemConsumeError { |
|
|
InvalidAmount,
|
|
|
InvalidAmount,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub struct ConsumedItem {
|
|
|
|
|
|
|
|
|
pub struct IndividualConsumedItem {
|
|
|
|
|
|
pub entity_id: ItemEntityId,
|
|
|
|
|
|
pub item: ItemDetail,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct StackedConsumedItem {
|
|
|
pub entity_ids: Vec<ItemEntityId>,
|
|
|
pub entity_ids: Vec<ItemEntityId>,
|
|
|
pub item: ItemDetail
|
|
|
|
|
|
|
|
|
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> {
|
|
|
pub struct InventoryItemHandle<'a> {
|
|
|
inventory: &'a mut CharacterInventory,
|
|
|
inventory: &'a mut CharacterInventory,
|
|
|
slot: usize,
|
|
|
slot: usize,
|
|
@ -175,37 +235,41 @@ pub struct InventoryItemHandle<'a> { |
|
|
|
|
|
|
|
|
impl<'a> InventoryItemHandle<'a> {
|
|
|
impl<'a> InventoryItemHandle<'a> {
|
|
|
pub fn item(&'a self) -> Option<&'a InventoryItem> {
|
|
|
pub fn item(&'a self) -> Option<&'a InventoryItem> {
|
|
|
self.inventory.0.get(self.slot)
|
|
|
|
|
|
|
|
|
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) {
|
|
|
pub fn remove_from_inventory(self) {
|
|
|
self.inventory.0.remove(self.slot);
|
|
|
|
|
|
|
|
|
self.inventory.items.remove(self.slot);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn consume(self, amount: usize) -> Result<ConsumedItem, InventoryItemConsumeError> {
|
|
|
pub fn consume(self, amount: usize) -> Result<ConsumedItem, InventoryItemConsumeError> {
|
|
|
enum RemoveMethod {
|
|
|
enum RemoveMethod {
|
|
|
EntireThing(ConsumedItem),
|
|
|
EntireThing(ConsumedItem),
|
|
|
Partial(ItemDetail),
|
|
|
|
|
|
|
|
|
Partial(Tool),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
let inventory_item = self.inventory.0.get(self.slot).ok_or(InventoryItemConsumeError::InconsistentState)?;
|
|
|
|
|
|
|
|
|
let inventory_item = self.inventory.items.get(self.slot).ok_or(InventoryItemConsumeError::InconsistentState)?;
|
|
|
let remove_method = match inventory_item {
|
|
|
let remove_method = match inventory_item {
|
|
|
InventoryItem::Individual(individual_inventory_item) => {
|
|
|
InventoryItem::Individual(individual_inventory_item) => {
|
|
|
RemoveMethod::EntireThing(ConsumedItem {
|
|
|
|
|
|
entity_ids: vec![individual_inventory_item.entity_id],
|
|
|
|
|
|
|
|
|
RemoveMethod::EntireThing(ConsumedItem::Individual(IndividualConsumedItem {
|
|
|
|
|
|
entity_id: individual_inventory_item.entity_id,
|
|
|
item: individual_inventory_item.item.clone()
|
|
|
item: individual_inventory_item.item.clone()
|
|
|
})
|
|
|
|
|
|
|
|
|
}))
|
|
|
},
|
|
|
},
|
|
|
InventoryItem::Stacked(stacked_inventory_item) => {
|
|
|
InventoryItem::Stacked(stacked_inventory_item) => {
|
|
|
match stacked_inventory_item.count().cmp(&amount) {
|
|
|
match stacked_inventory_item.count().cmp(&amount) {
|
|
|
Ordering::Equal => {
|
|
|
Ordering::Equal => {
|
|
|
RemoveMethod::EntireThing(ConsumedItem {
|
|
|
|
|
|
|
|
|
RemoveMethod::EntireThing(ConsumedItem::Stacked(StackedConsumedItem {
|
|
|
entity_ids: stacked_inventory_item.entity_ids.clone(),
|
|
|
entity_ids: stacked_inventory_item.entity_ids.clone(),
|
|
|
item: ItemDetail::Tool(stacked_inventory_item.tool),
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
tool: stacked_inventory_item.tool,
|
|
|
|
|
|
}))
|
|
|
},
|
|
|
},
|
|
|
Ordering::Greater => {
|
|
|
Ordering::Greater => {
|
|
|
RemoveMethod::Partial(ItemDetail::Tool(stacked_inventory_item.tool))
|
|
|
|
|
|
|
|
|
RemoveMethod::Partial(stacked_inventory_item.tool)
|
|
|
},
|
|
|
},
|
|
|
Ordering::Less => {
|
|
|
Ordering::Less => {
|
|
|
return Err(InventoryItemConsumeError::InvalidAmount)
|
|
|
return Err(InventoryItemConsumeError::InvalidAmount)
|
|
@ -216,11 +280,11 @@ impl<'a> InventoryItemHandle<'a> { |
|
|
|
|
|
|
|
|
match remove_method {
|
|
|
match remove_method {
|
|
|
RemoveMethod::EntireThing(consumed_item) => {
|
|
|
RemoveMethod::EntireThing(consumed_item) => {
|
|
|
self.inventory.0.remove(self.slot);
|
|
|
|
|
|
|
|
|
self.inventory.items.remove(self.slot);
|
|
|
Ok(consumed_item)
|
|
|
Ok(consumed_item)
|
|
|
},
|
|
|
},
|
|
|
RemoveMethod::Partial(item_detail) => {
|
|
|
|
|
|
let entity_ids = self.inventory.0.get_mut(self.slot)
|
|
|
|
|
|
|
|
|
RemoveMethod::Partial(tool) => {
|
|
|
|
|
|
let entity_ids = self.inventory.items.get_mut(self.slot)
|
|
|
.and_then(|item| {
|
|
|
.and_then(|item| {
|
|
|
if let InventoryItem::Stacked(stacked_inventory_item) = item {
|
|
|
if let InventoryItem::Stacked(stacked_inventory_item) = item {
|
|
|
Some(stacked_inventory_item.entity_ids.drain(..amount).collect::<Vec<_>>())
|
|
|
Some(stacked_inventory_item.entity_ids.drain(..amount).collect::<Vec<_>>())
|
|
@ -230,10 +294,10 @@ impl<'a> InventoryItemHandle<'a> { |
|
|
}
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
.ok_or(InventoryItemConsumeError::InvalidAmount)?;
|
|
|
.ok_or(InventoryItemConsumeError::InvalidAmount)?;
|
|
|
Ok(ConsumedItem {
|
|
|
|
|
|
|
|
|
Ok(ConsumedItem::Stacked(StackedConsumedItem {
|
|
|
entity_ids: entity_ids,
|
|
|
entity_ids: entity_ids,
|
|
|
item: item_detail,
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
tool: tool,
|
|
|
|
|
|
}))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -243,21 +307,28 @@ impl<'a> InventoryItemHandle<'a> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
#[derive(Debug)]
|
|
|
pub struct CharacterInventory(Vec<InventoryItem>);
|
|
|
|
|
|
|
|
|
pub struct CharacterInventory {
|
|
|
|
|
|
item_id_counter: u32,
|
|
|
|
|
|
items: Vec<InventoryItem>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
impl CharacterInventory {
|
|
|
impl CharacterInventory {
|
|
|
pub fn new(items: Vec<InventoryItem>) -> CharacterInventory {
|
|
|
pub fn new(items: Vec<InventoryItem>) -> CharacterInventory {
|
|
|
CharacterInventory(items)
|
|
|
|
|
|
|
|
|
CharacterInventory{
|
|
|
|
|
|
item_id_counter: 0,
|
|
|
|
|
|
items: items,
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn initialize_item_ids(&mut self, base_item_id: u32) {
|
|
|
pub fn initialize_item_ids(&mut self, base_item_id: u32) {
|
|
|
for (i, item) in self.0.iter_mut().enumerate() {
|
|
|
|
|
|
|
|
|
for (i, item) in self.items.iter_mut().enumerate() {
|
|
|
item.set_item_id(ClientItemId(base_item_id + i as u32));
|
|
|
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] {
|
|
|
pub fn as_client_inventory_items(&self) -> [character::InventoryItem; 30] {
|
|
|
self.0.iter()
|
|
|
|
|
|
|
|
|
self.items.iter()
|
|
|
.enumerate()
|
|
|
.enumerate()
|
|
|
.fold([character::InventoryItem::default(); 30], |mut inventory, (slot, item)| {
|
|
|
.fold([character::InventoryItem::default(); 30], |mut inventory, (slot, item)| {
|
|
|
let bytes = item.as_client_bytes();
|
|
|
let bytes = item.as_client_bytes();
|
|
@ -273,15 +344,15 @@ impl CharacterInventory { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn slot(&self, slot: usize) -> Option<&InventoryItem> {
|
|
|
pub fn slot(&self, slot: usize) -> Option<&InventoryItem> {
|
|
|
self.0.get(slot)
|
|
|
|
|
|
|
|
|
self.items.get(slot)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn count(&self) -> usize {
|
|
|
pub fn count(&self) -> usize {
|
|
|
self.0.len()
|
|
|
|
|
|
|
|
|
self.items.len()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn get_item_handle_by_id<'a>(&'a mut self, item_id: ClientItemId) -> Option<InventoryItemHandle<'a>> {
|
|
|
pub fn get_item_handle_by_id<'a>(&'a mut self, item_id: ClientItemId) -> Option<InventoryItemHandle<'a>> {
|
|
|
let (slot, _) = self.0.iter()
|
|
|
|
|
|
|
|
|
let (slot, _) = self.items.iter()
|
|
|
.enumerate()
|
|
|
.enumerate()
|
|
|
.filter(|(_, item)| {
|
|
|
.filter(|(_, item)| {
|
|
|
item.item_id() == item_id
|
|
|
item.item_id() == item_id
|
|
@ -294,7 +365,7 @@ impl CharacterInventory { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn get_item_by_id(&self, item_id: ClientItemId) -> Option<&InventoryItem> {
|
|
|
pub fn get_item_by_id(&self, item_id: ClientItemId) -> Option<&InventoryItem> {
|
|
|
self.0.iter()
|
|
|
|
|
|
|
|
|
self.items.iter()
|
|
|
.filter(|item| {
|
|
|
.filter(|item| {
|
|
|
item.item_id() == item_id
|
|
|
item.item_id() == item_id
|
|
|
})
|
|
|
})
|
|
@ -302,14 +373,14 @@ impl CharacterInventory { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn take_item_by_id(&mut self, item_id: ClientItemId) -> Option<InventoryItem> {
|
|
|
pub fn take_item_by_id(&mut self, item_id: ClientItemId) -> Option<InventoryItem> {
|
|
|
self.0
|
|
|
|
|
|
|
|
|
self.items
|
|
|
.drain_filter(|i| i.item_id() == item_id)
|
|
|
.drain_filter(|i| i.item_id() == item_id)
|
|
|
.nth(0)
|
|
|
.nth(0)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn add_item(&mut self, item: InventoryItem) -> Result<(), ()> { // TODO: errors
|
|
|
pub fn add_item(&mut self, item: InventoryItem) -> Result<(), ()> { // TODO: errors
|
|
|
// TODO: check slot conflict?
|
|
|
// TODO: check slot conflict?
|
|
|
self.0.push(item);
|
|
|
|
|
|
|
|
|
self.items.push(item);
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
@ -319,14 +390,14 @@ impl CharacterInventory { |
|
|
return None;
|
|
|
return None;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
self.0.push(InventoryItem::Individual(IndividualInventoryItem {
|
|
|
|
|
|
|
|
|
self.items.push(InventoryItem::Individual(IndividualInventoryItem {
|
|
|
entity_id: floor_item.entity_id,
|
|
|
entity_id: floor_item.entity_id,
|
|
|
item_id: floor_item.item_id,
|
|
|
item_id: floor_item.item_id,
|
|
|
item: floor_item.item.clone(),
|
|
|
item: floor_item.item.clone(),
|
|
|
equipped: false,
|
|
|
equipped: false,
|
|
|
}));
|
|
|
}));
|
|
|
|
|
|
|
|
|
if let Some(InventoryItem::Individual(new_item)) = self.0.last() {
|
|
|
|
|
|
|
|
|
if let Some(InventoryItem::Individual(new_item)) = self.items.last() {
|
|
|
Some((new_item, InventorySlot(self.count())))
|
|
|
Some((new_item, InventorySlot(self.count())))
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
else {
|
|
@ -336,7 +407,7 @@ impl CharacterInventory { |
|
|
|
|
|
|
|
|
// TODO: can be simplified using find instead of position
|
|
|
// TODO: can be simplified using find instead of position
|
|
|
pub fn pick_up_stacked_floor_item(&mut self, floor_item: &StackedFloorItem) -> Option<(&StackedInventoryItem, InventorySlot)> {
|
|
|
pub fn pick_up_stacked_floor_item(&mut self, floor_item: &StackedFloorItem) -> Option<(&StackedInventoryItem, InventorySlot)> {
|
|
|
let existing_stack_position = self.0.iter()
|
|
|
|
|
|
|
|
|
let existing_stack_position = self.items.iter()
|
|
|
.position(|inventory_item| {
|
|
|
.position(|inventory_item| {
|
|
|
if let InventoryItem::Stacked(stacked_inventory_item) = inventory_item {
|
|
|
if let InventoryItem::Stacked(stacked_inventory_item) = inventory_item {
|
|
|
if stacked_inventory_item.tool == floor_item.tool {
|
|
|
if stacked_inventory_item.tool == floor_item.tool {
|
|
@ -347,7 +418,7 @@ impl CharacterInventory { |
|
|
});
|
|
|
});
|
|
|
|
|
|
|
|
|
if let Some(existing_stack_position) = existing_stack_position {
|
|
|
if let Some(existing_stack_position) = existing_stack_position {
|
|
|
if let Some(InventoryItem::Stacked(stacked_item)) = self.0.get_mut(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() {
|
|
|
if stacked_item.count() + floor_item.count() <= stacked_item.tool.max_stack() {
|
|
|
stacked_item.entity_ids.append(&mut floor_item.entity_ids.clone());
|
|
|
stacked_item.entity_ids.append(&mut floor_item.entity_ids.clone());
|
|
|
Some((stacked_item, InventorySlot(existing_stack_position)))
|
|
|
Some((stacked_item, InventorySlot(existing_stack_position)))
|
|
@ -367,8 +438,8 @@ impl CharacterInventory { |
|
|
tool: floor_item.tool,
|
|
|
tool: floor_item.tool,
|
|
|
});
|
|
|
});
|
|
|
|
|
|
|
|
|
self.0.push(new_stacked_item);
|
|
|
|
|
|
if let Some(InventoryItem::Stacked(new_item)) = self.0.last() {
|
|
|
|
|
|
|
|
|
self.items.push(new_stacked_item);
|
|
|
|
|
|
if let Some(InventoryItem::Stacked(new_item)) = self.items.last() {
|
|
|
Some((new_item, InventorySlot(self.count())))
|
|
|
Some((new_item, InventorySlot(self.count())))
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
else {
|
|
@ -376,5 +447,69 @@ impl CharacterInventory { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
|
},
|
|
|
|
|
|
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()
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
(stacked_bank_item.count() == 0, slot)
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if remove {
|
|
|
|
|
|
bank_item.remove_from_bank();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.items.last().map(|item| {
|
|
|
|
|
|
(item, slot)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|