OpenEnroth 52af553
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
Inventory Class Reference

#include <Inventory.h>

Inheritance diagram for Inventory:
CharacterInventory ChestInventory

Classes

struct  InventoryRecord
 

Public Member Functions

 Inventory (Sizei gridSize, int capacity)
 
 Inventory ()
 
int size () const
 
int capacity () const
 
Sizei gridSize () const
 
Recti gridRect () const
 
auto entries (this auto &&self)
 
auto items (this auto &&self)
 
auto items (this auto &&self, ItemId itemId)
 
InventoryEntry entry (Pointi position)
 
InventoryConstEntry entry (Pointi position) const
 
InventoryEntry entry (ItemSlot slot)
 
InventoryConstEntry entry (ItemSlot slot) const
 
InventoryEntry entry (int index)
 
InventoryConstEntry entry (int index) const
 
bool canAdd (Pointi position, Sizei size) const
 
bool canAdd (Pointi position, const Item &item) const
 
bool canAdd (Sizei size) const
 
bool canAdd (const Item &item) const
 
InventoryEntry add (Pointi position, const Item &item)
 
InventoryEntry add (const Item &item)
 
InventoryEntry tryAdd (Pointi position, const Item &item)
 
InventoryEntry tryAdd (const Item &item)
 
bool canEquip (ItemSlot slot) const
 
InventoryEntry equip (ItemSlot slot, const Item &item)
 
InventoryEntry tryEquip (ItemSlot slot, const Item &item)
 
bool canStash () const
 
InventoryEntry stash (const Item &item)
 
InventoryEntry tryStash (const Item &item)
 
Item take (InventoryEntry entry)
 
std::optional< PointifindSpace (Sizei size) const
 
std::optional< PointifindSpace (const Item &item) const
 
InventoryEntry find (ItemId itemId)
 
InventoryConstEntry find (ItemId itemId) const
 
void clear ()
 

Static Public Attributes

static constexpr std::size_t MAX_ITEMS = 140
 

Private Member Functions

int findFreeIndex () const
 
bool isGridFree (Pointi position, Sizei size) const
 
InventoryEntry addAt (Pointi position, const Item &item, int index)
 
InventoryEntry equipAt (ItemSlot slot, const Item &item, int index)
 
InventoryEntry stashAt (const Item &item, int index)
 
void checkInvariants () const
 
auto availableRecords (this auto &&self)
 

Private Attributes

Sizei _gridSize
 
int _size = 0
 
int _capacity = 0
 
std::array< InventoryRecord, MAX_ITEMS_records
 
std::array< int, MAX_ITEMS_grid = {{}}
 
IndexedArray< int, ITEM_SLOT_FIRST_VALID, ITEM_SLOT_LAST_VALID_equipment = {{}}
 

Friends

class InventoryConstEntry
 
void snapshot (const ChestInventory &src, Chest_MM7 *dst)
 
void reconstruct (const Chest_MM7 &src, ChestInventory *dst, ContextTag< int > chestId)
 
void snapshot (const CharacterInventory &src, Character_MM7 *dst)
 
void reconstruct (const Character_MM7 &src, CharacterInventory *dst, ContextTag< int > characterIndex)
 

Detailed Description

Core container that stores items for both chests and characters.

Inventory stores items inside an array, so all items have indices. These indices are exposed through InventoryConstEntry::index and Inventory::entry(int). In practice there shouldn't be a lot of use cases where direct access to item indices is needed.

See also
ChestInventory
CharacterInventory

Constructor & Destructor Documentation

◆ Inventory() [1/2]

Inventory::Inventory ( Sizei  gridSize,
int  capacity 
)
explicit
Parameters
gridSizeSize of inventory's grid, WxH must be less or equal to MAX_ITEMS.
capacityInventory capacity, must be less or equal to MAX_ITEMS.

◆ Inventory() [2/2]

Inventory::Inventory ( )
inline

Constructs a 1x1 inventory that can hold MAX_ITEMS items.

Member Function Documentation

◆ add() [1/2]

InventoryEntry Inventory::add ( const Item item)

◆ add() [2/2]

InventoryEntry Inventory::add ( Pointi  position,
const Item item 
)

◆ addAt()

InventoryEntry Inventory::addAt ( Pointi  position,
const Item item,
int  index 
)
private

◆ availableRecords()

auto Inventory::availableRecords ( this auto &&  self)
inlineprivate

◆ canAdd() [1/4]

bool Inventory::canAdd ( const Item item) const
inline

◆ canAdd() [2/4]

bool Inventory::canAdd ( Pointi  position,
const Item item 
) const
inline

◆ canAdd() [3/4]

bool Inventory::canAdd ( Pointi  position,
Sizei  size 
) const

◆ canAdd() [4/4]

bool Inventory::canAdd ( Sizei  size) const

◆ canEquip()

bool Inventory::canEquip ( ItemSlot  slot) const

◆ canStash()

bool Inventory::canStash ( ) const

◆ capacity()

int Inventory::capacity ( ) const
inline
Returns
How many items can this inventory object hold.

◆ checkInvariants()

void Inventory::checkInvariants ( ) const
private

◆ clear()

void Inventory::clear ( )

◆ entries()

auto Inventory::entries ( this auto &&  self)
inline
Parameters
self*this.
Returns
A range of InventoryEntry or InventoryConstEntry objects for items in this inventory. Returned entries are never invalid.

◆ entry() [1/6]

InventoryEntry Inventory::entry ( int  index)
Parameters
indexEntry index to look up an item.
Returns
Inventory entry at index, or an invalid entry if index is out of bounds or if there is no item at index.

◆ entry() [2/6]

InventoryConstEntry Inventory::entry ( int  index) const
inline

◆ entry() [3/6]

InventoryEntry Inventory::entry ( ItemSlot  slot)
Parameters
slotEquipment slot to look up an item.
Returns
Inventory entry for an item equipped in slot, or an invalid entry if that slot is empty.

◆ entry() [4/6]

InventoryConstEntry Inventory::entry ( ItemSlot  slot) const
inline

◆ entry() [5/6]

InventoryEntry Inventory::entry ( Pointi  position)
Parameters
positionGrid position to look up an item.
Returns
Inventory entry at provided grid position, or an invalid entry if position is out of bounds or if the grid at position is empty.

◆ entry() [6/6]

InventoryConstEntry Inventory::entry ( Pointi  position) const
inline

◆ equip()

InventoryEntry Inventory::equip ( ItemSlot  slot,
const Item item 
)

◆ equipAt()

InventoryEntry Inventory::equipAt ( ItemSlot  slot,
const Item item,
int  index 
)
private

◆ find() [1/2]

InventoryEntry Inventory::find ( ItemId  itemId)

◆ find() [2/2]

InventoryConstEntry Inventory::find ( ItemId  itemId) const
inline

◆ findFreeIndex()

int Inventory::findFreeIndex ( ) const
private

◆ findSpace() [1/2]

std::optional< Pointi > Inventory::findSpace ( const Item item) const
inline

◆ findSpace() [2/2]

std::optional< Pointi > Inventory::findSpace ( Sizei  size) const

◆ gridRect()

Recti Inventory::gridRect ( ) const
inline
Returns
Rect for this inventory's grid, in cells. Mainly useful for checks like inventory.gridRect().contains(pos).

◆ gridSize()

Sizei Inventory::gridSize ( ) const
inline
Returns
Size of this inventory's grid, in cells.

◆ isGridFree()

bool Inventory::isGridFree ( Pointi  position,
Sizei  size 
) const
private

◆ items() [1/2]

auto Inventory::items ( this auto &&  self)
inline
Parameters
self*this.
Returns
A range of Item objects for items in this inventory. Returned items are never ITEM_NULL.

◆ items() [2/2]

auto Inventory::items ( this auto &&  self,
ItemId  itemId 
)
inline
Parameters
self*this.
itemIdItem id to filter for.
Returns
A range of Item objects for items in this inventory with the given itemId.

◆ size()

int Inventory::size ( ) const
inline
Returns
How many items are currently held in this inventory object.

◆ stash()

InventoryEntry Inventory::stash ( const Item item)

◆ stashAt()

InventoryEntry Inventory::stashAt ( const Item item,
int  index 
)
private

◆ take()

Item Inventory::take ( InventoryEntry  entry)

◆ tryAdd() [1/2]

InventoryEntry Inventory::tryAdd ( const Item item)

◆ tryAdd() [2/2]

InventoryEntry Inventory::tryAdd ( Pointi  position,
const Item item 
)

◆ tryEquip()

InventoryEntry Inventory::tryEquip ( ItemSlot  slot,
const Item item 
)

◆ tryStash()

InventoryEntry Inventory::tryStash ( const Item item)

Friends And Related Function Documentation

◆ InventoryConstEntry

friend class InventoryConstEntry
friend

◆ reconstruct [1/2]

void reconstruct ( const Character_MM7 src,
CharacterInventory dst,
ContextTag< int >  characterIndex 
)
friend

◆ reconstruct [2/2]

void reconstruct ( const Chest_MM7 src,
ChestInventory dst,
ContextTag< int >  chestId 
)
friend

◆ snapshot [1/2]

void snapshot ( const CharacterInventory src,
Character_MM7 dst 
)
friend

◆ snapshot [2/2]

void snapshot ( const ChestInventory src,
Chest_MM7 dst 
)
friend

Member Data Documentation

◆ _capacity

int Inventory::_capacity = 0
private

Max number of items that can be stuffed into this inventory.

◆ _equipment

IndexedArray<int, ITEM_SLOT_FIRST_VALID, ITEM_SLOT_LAST_VALID> Inventory::_equipment = {{}}
private

Equipment array. Positive number is an index into _records plus one. Zero means empty.

◆ _grid

std::array<int, MAX_ITEMS> Inventory::_grid = {{}}
private

Backpack grid. 0 means empty cell. Positive number is an index into _records plus one. Negative number is an index into the main slot in _grid minus one.

◆ _gridSize

Sizei Inventory::_gridSize
private

Inventory storage area size in cells.

◆ _records

std::array<InventoryRecord, MAX_ITEMS> Inventory::_records
private

All items in inventory. ITEM_NULL means the slot is empty.

◆ _size

int Inventory::_size = 0
private

Number of items in this inventory.

◆ MAX_ITEMS

constexpr std::size_t Inventory::MAX_ITEMS = 140
staticconstexpr

The documentation for this class was generated from the following files: