2019-05-18 16:46:14 +00:00
|
|
|
pub mod item;
|
|
|
|
|
2019-07-25 12:46:54 +00:00
|
|
|
// Reexports
|
2020-03-14 15:40:29 +00:00
|
|
|
pub use item::{Consumable, DebugKind, Item, ItemKind, SwordKind, ToolData, ToolKind};
|
2019-07-25 12:46:54 +00:00
|
|
|
|
2019-10-22 18:18:40 +00:00
|
|
|
use crate::assets;
|
2020-03-04 10:09:48 +00:00
|
|
|
use specs::{Component, FlaggedStorage, HashMapStorage};
|
|
|
|
use specs_idvs::IDVStorage;
|
2019-10-31 04:47:17 +00:00
|
|
|
use std::ops::Not;
|
2019-05-18 16:46:14 +00:00
|
|
|
|
2020-03-10 20:50:04 +00:00
|
|
|
// The limit on distance between the entity and a collectible (squared)
|
|
|
|
pub const MAX_PICKUP_RANGE_SQR: f32 = 64.0;
|
|
|
|
|
2019-05-18 16:46:14 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
|
|
|
pub struct Inventory {
|
|
|
|
pub slots: Vec<Option<Item>>,
|
|
|
|
}
|
|
|
|
|
2019-10-31 04:47:17 +00:00
|
|
|
/// Errors which the methods on `Inventory` produce
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2020-02-01 20:39:39 +00:00
|
|
|
/// The inventory is full and items could not be added. The extra items have
|
|
|
|
/// been returned.
|
2019-10-31 04:47:17 +00:00
|
|
|
Full(Vec<Item>),
|
|
|
|
}
|
|
|
|
|
2019-05-18 16:46:14 +00:00
|
|
|
impl Inventory {
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn slots(&self) -> &[Option<Item>] { &self.slots }
|
2019-07-25 17:41:06 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn len(&self) -> usize { self.slots.len() }
|
2019-07-25 22:52:28 +00:00
|
|
|
|
2020-03-19 13:30:50 +00:00
|
|
|
/// Adds a new item to the first fitting group of the inventory or starts a
|
|
|
|
/// new group. Returns the item again if no space was found.
|
|
|
|
pub fn push(&mut self, item: Item) -> Option<Item> {
|
|
|
|
match item.kind {
|
|
|
|
ItemKind::Tool(_) | ItemKind::Armor { .. } => self.add_to_first_empty(item),
|
|
|
|
ItemKind::Utility {
|
|
|
|
kind: item_kind,
|
|
|
|
amount: new_amount,
|
|
|
|
} => {
|
|
|
|
for slot in &mut self.slots {
|
|
|
|
if slot
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| s.name() == item.name())
|
|
|
|
.unwrap_or(false)
|
|
|
|
&& slot
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| s.description() == item.description())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
if let Some(Item {
|
|
|
|
kind: ItemKind::Utility { kind, amount },
|
|
|
|
..
|
|
|
|
}) = slot
|
|
|
|
{
|
|
|
|
if item_kind == *kind {
|
|
|
|
*amount += new_amount;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// It didn't work
|
|
|
|
self.add_to_first_empty(item)
|
|
|
|
},
|
|
|
|
ItemKind::Consumable {
|
|
|
|
kind: item_kind,
|
|
|
|
amount: new_amount,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
for slot in &mut self.slots {
|
|
|
|
if slot
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| s.name() == item.name())
|
|
|
|
.unwrap_or(false)
|
|
|
|
&& slot
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| s.description() == item.description())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
if let Some(Item {
|
|
|
|
kind: ItemKind::Consumable { kind, amount, .. },
|
|
|
|
..
|
|
|
|
}) = slot
|
|
|
|
{
|
|
|
|
if item_kind == *kind {
|
|
|
|
*amount += new_amount;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// It didn't work
|
|
|
|
self.add_to_first_empty(item)
|
|
|
|
},
|
|
|
|
ItemKind::Ingredient {
|
|
|
|
kind: item_kind,
|
|
|
|
amount: new_amount,
|
|
|
|
} => {
|
|
|
|
for slot in &mut self.slots {
|
|
|
|
if slot
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| s.name() == item.name())
|
|
|
|
.unwrap_or(false)
|
|
|
|
&& slot
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| s.description() == item.description())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
if let Some(Item {
|
|
|
|
kind: ItemKind::Ingredient { kind, amount },
|
|
|
|
..
|
|
|
|
}) = slot
|
|
|
|
{
|
|
|
|
if item_kind == *kind {
|
|
|
|
*amount += new_amount;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// It didn't work
|
|
|
|
self.add_to_first_empty(item)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
/// Adds a new item to the first empty slot of the inventory. Returns the
|
|
|
|
/// item again if no free slot was found.
|
2020-03-19 13:30:50 +00:00
|
|
|
fn add_to_first_empty(&mut self, item: Item) -> Option<Item> {
|
2019-07-25 22:52:28 +00:00
|
|
|
match self.slots.iter_mut().find(|slot| slot.is_none()) {
|
|
|
|
Some(slot) => {
|
|
|
|
*slot = Some(item);
|
2019-08-30 19:02:18 +00:00
|
|
|
None
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-30 19:02:18 +00:00
|
|
|
None => Some(item),
|
2019-08-28 20:47:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
/// Add a series of items to inventory, returning any which do not fit as an
|
|
|
|
/// error.
|
2019-10-31 14:26:25 +00:00
|
|
|
pub fn push_all<I: Iterator<Item = Item>>(&mut self, mut items: I) -> Result<(), Error> {
|
2019-10-31 04:47:17 +00:00
|
|
|
// Vec doesn't allocate for zero elements so this should be cheap
|
|
|
|
let mut leftovers = Vec::new();
|
|
|
|
let mut slots = self.slots.iter_mut();
|
|
|
|
for item in &mut items {
|
|
|
|
if let Some(slot) = slots.find(|slot| slot.is_none()) {
|
|
|
|
slot.replace(item);
|
|
|
|
} else {
|
|
|
|
leftovers.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if leftovers.len() > 0 {
|
|
|
|
Err(Error::Full(leftovers))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 16:48:42 +00:00
|
|
|
/// Add a series of items to an inventory without giving duplicates.
|
|
|
|
/// (n * m complexity)
|
2019-11-07 01:57:05 +00:00
|
|
|
///
|
2020-02-01 20:39:39 +00:00
|
|
|
/// Error if inventory cannot contain the items (is full), returning the
|
|
|
|
/// un-added items. This is a lazy inefficient implementation, as it
|
|
|
|
/// iterates over the inventory more times than necessary (n^2) and with
|
|
|
|
/// the proper structure wouldn't need to iterate at all, but because
|
|
|
|
/// this should be fairly cold code, clarity has been favored over
|
|
|
|
/// efficiency.
|
2019-10-31 14:26:25 +00:00
|
|
|
pub fn push_all_unique<I: Iterator<Item = Item>>(&mut self, mut items: I) -> Result<(), Error> {
|
2019-10-31 04:47:17 +00:00
|
|
|
let mut leftovers = Vec::new();
|
|
|
|
for item in &mut items {
|
|
|
|
if self.contains(&item).not() {
|
2019-10-31 14:26:25 +00:00
|
|
|
self.push(item).map(|overflow| leftovers.push(overflow));
|
2019-10-31 04:47:17 +00:00
|
|
|
} // else drop item if it was already in
|
|
|
|
}
|
|
|
|
if leftovers.len() > 0 {
|
|
|
|
Err(Error::Full(leftovers))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
/// Replaces an item in a specific slot of the inventory. Returns the old
|
|
|
|
/// item or the same item again if that slot was not found.
|
2019-08-30 22:09:25 +00:00
|
|
|
pub fn insert(&mut self, cell: usize, item: Item) -> Result<Option<Item>, Item> {
|
2019-08-28 20:47:52 +00:00
|
|
|
match self.slots.get_mut(cell) {
|
|
|
|
Some(slot) => {
|
2019-08-30 22:09:25 +00:00
|
|
|
let old = slot.take();
|
|
|
|
*slot = Some(item);
|
|
|
|
Ok(old)
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-30 22:09:25 +00:00
|
|
|
None => Err(item),
|
2019-07-25 22:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn is_full(&self) -> bool { self.slots.iter().all(|slot| slot.is_some()) }
|
2019-09-25 22:53:43 +00:00
|
|
|
|
2019-10-31 04:47:17 +00:00
|
|
|
/// O(n) count the number of items in this inventory.
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn count(&self) -> usize { self.slots.iter().filter_map(|slot| slot.as_ref()).count() }
|
2019-10-31 04:47:17 +00:00
|
|
|
|
|
|
|
/// O(n) check if an item is in this inventory.
|
|
|
|
pub fn contains(&self, item: &Item) -> bool {
|
2019-11-06 22:57:54 +00:00
|
|
|
self.slots.iter().any(|slot| slot.as_ref() == Some(item))
|
2019-10-31 04:47:17 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 20:42:43 +00:00
|
|
|
/// Get content of a slot
|
|
|
|
pub fn get(&self, cell: usize) -> Option<&Item> {
|
|
|
|
self.slots.get(cell).and_then(Option::as_ref)
|
2019-05-18 16:46:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 20:42:43 +00:00
|
|
|
/// Swap the items inside of two slots
|
2019-07-25 22:52:28 +00:00
|
|
|
pub fn swap_slots(&mut self, a: usize, b: usize) {
|
|
|
|
if a.max(b) < self.slots.len() {
|
|
|
|
self.slots.swap(a, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:42:43 +00:00
|
|
|
/// Remove an item from the slot
|
2019-07-25 12:46:54 +00:00
|
|
|
pub fn remove(&mut self, cell: usize) -> Option<Item> {
|
|
|
|
self.slots.get_mut(cell).and_then(|item| item.take())
|
2019-05-18 16:46:14 +00:00
|
|
|
}
|
2020-03-19 13:30:50 +00:00
|
|
|
|
|
|
|
/// Remove just one item from the slot
|
|
|
|
pub fn take(&mut self, cell: usize) -> Option<Item> {
|
|
|
|
if let Some(Some(item)) = self.slots.get_mut(cell) {
|
|
|
|
let mut return_item = item.clone();
|
|
|
|
match &mut item.kind {
|
|
|
|
ItemKind::Tool(_) | ItemKind::Armor { .. } => self.remove(cell),
|
|
|
|
ItemKind::Utility { kind, amount } => {
|
|
|
|
if *amount <= 1 {
|
|
|
|
self.remove(cell)
|
|
|
|
} else {
|
|
|
|
*amount -= 1;
|
|
|
|
return_item.kind = ItemKind::Utility {
|
|
|
|
kind: *kind,
|
|
|
|
amount: 1,
|
|
|
|
};
|
|
|
|
Some(return_item)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ItemKind::Consumable {
|
|
|
|
kind,
|
|
|
|
amount,
|
|
|
|
effect,
|
|
|
|
} => {
|
|
|
|
if *amount <= 1 {
|
|
|
|
self.remove(cell)
|
|
|
|
} else {
|
|
|
|
*amount -= 1;
|
|
|
|
return_item.kind = ItemKind::Consumable {
|
|
|
|
kind: *kind,
|
|
|
|
effect: *effect,
|
|
|
|
amount: 1,
|
|
|
|
};
|
|
|
|
Some(return_item)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ItemKind::Ingredient { kind, amount } => {
|
|
|
|
if *amount <= 1 {
|
|
|
|
self.remove(cell)
|
|
|
|
} else {
|
|
|
|
*amount -= 1;
|
|
|
|
return_item.kind = ItemKind::Ingredient {
|
|
|
|
kind: *kind,
|
|
|
|
amount: 1,
|
|
|
|
};
|
|
|
|
Some(return_item)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 16:46:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 14:02:05 +00:00
|
|
|
impl Default for Inventory {
|
|
|
|
fn default() -> Inventory {
|
2019-08-26 18:05:13 +00:00
|
|
|
let mut inventory = Inventory {
|
2020-03-19 21:36:40 +00:00
|
|
|
slots: vec![None; 200],
|
2019-07-25 22:52:28 +00:00
|
|
|
};
|
2019-11-09 13:42:42 +00:00
|
|
|
inventory.push(assets::load_expect_cloned("common.items.cheese"));
|
|
|
|
inventory.push(assets::load_expect_cloned("common.items.apple"));
|
2019-08-26 18:05:13 +00:00
|
|
|
inventory
|
2019-07-25 14:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 16:46:14 +00:00
|
|
|
impl Component for Inventory {
|
2019-07-31 09:30:46 +00:00
|
|
|
type Storage = HashMapStorage<Self>;
|
2019-05-18 16:46:14 +00:00
|
|
|
}
|
2019-07-25 14:48:27 +00:00
|
|
|
|
2020-03-04 10:09:48 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum InventoryUpdateEvent {
|
|
|
|
Init,
|
|
|
|
Used,
|
|
|
|
Consumed(Consumable),
|
|
|
|
Gave,
|
|
|
|
Given,
|
|
|
|
Swapped,
|
|
|
|
Dropped,
|
|
|
|
Collected,
|
2020-03-11 10:30:59 +00:00
|
|
|
CollectFailed,
|
2020-03-04 10:09:48 +00:00
|
|
|
Possession,
|
|
|
|
Debug,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for InventoryUpdateEvent {
|
|
|
|
fn default() -> Self { Self::Init }
|
|
|
|
}
|
|
|
|
|
2019-07-25 14:48:27 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
|
2020-03-04 10:09:48 +00:00
|
|
|
pub struct InventoryUpdate {
|
|
|
|
event: InventoryUpdateEvent,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InventoryUpdate {
|
|
|
|
pub fn new(event: InventoryUpdateEvent) -> Self { Self { event } }
|
|
|
|
|
|
|
|
pub fn event(&self) -> InventoryUpdateEvent { self.event }
|
|
|
|
}
|
2019-07-25 14:48:27 +00:00
|
|
|
|
|
|
|
impl Component for InventoryUpdate {
|
2020-03-04 10:09:48 +00:00
|
|
|
type Storage = FlaggedStorage<Self, IDVStorage<Self>>;
|
2019-07-25 14:48:27 +00:00
|
|
|
}
|
2019-10-31 04:47:17 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
#[cfg(test)] mod test;
|