Mana
Loading...
Searching...
No Matches
inventory.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "inventory.h"
23#include "item.h"
24#include "log.h"
25
27#include "net/net.h"
28
29#include <algorithm>
30
31static bool slotUsed(const Item *item)
32{
33 return item && item->getId() >= 0 && item->getQuantity() > 0;
34}
35
37 : mType(type)
38{
39 mItems.resize(size == -1 ? Net::getInventoryHandler()->getSize(type) : size);
40}
41
42Inventory::~Inventory() = default;
43
44Item *Inventory::getItem(int index) const
45{
46 if (index < 0 || index >= getSize() || !mItems[index] || mItems[index]->getQuantity() <= 0)
47 return nullptr;
48
49 return mItems[index].get();
50}
51
52Item *Inventory::findItem(int itemId) const
53{
54 for (auto &item : mItems)
55 if (item && item->getId() == itemId)
56 return item.get();
57
58 return nullptr;
59}
60
61void Inventory::addItem(int id, int quantity)
62{
63 setItem(getFreeSlot(), id, quantity);
64}
65
66void Inventory::setItem(int index, int id, int quantity)
67{
68 if (index < 0 || index >= getSize())
69 {
70 Log::warn("Invalid inventory index: %d", index);
71 return;
72 }
73
74 if (id > 0)
75 {
76 if (!mItems[index])
77 {
78 auto item = std::make_unique<Item>(id, quantity);
79 item->setInvIndex(index);
80 mItems[index] = std::move(item);
81 mUsed++;
83 }
84 else
85 {
86 mItems[index]->setId(id);
87 mItems[index]->setQuantity(quantity);
88 }
89 }
90 else if (mItems[index])
91 {
92 removeItemAt(index);
93 }
94}
95
97{
98 for (auto &item : mItems)
99 item = nullptr;
100 mUsed = 0;
102}
103
105{
106 mItems[index] = nullptr;
107 if (mUsed > 0) {
108 mUsed--;
110 }
111}
112
113bool Inventory::contains(Item *item) const
114{
115 return std::any_of(mItems.begin(),
116 mItems.end(),
117 [id = item->getId()](auto &i) {
118 return i && i->getId() == id;
119 });
120}
121
123{
124 for (int i = 0; i < getSize(); i++)
125 if (!slotUsed(mItems[i].get()))
126 return i;
127
128 return -1;
129}
130
132{
133 for (int i = getSize() - 1; i >= 0; i--)
134 if (slotUsed(mItems[i].get()))
135 return i;
136
137 return -1;
138}
139
141{
142 mInventoryListeners.push_back(listener);
143}
144
146{
147 mInventoryListeners.remove(listener);
148}
149
151{
152 for (auto inventoryListener : mInventoryListeners)
153 inventoryListener->slotsChanged(this);
154}
void clear()
Reset all item slots.
Definition inventory.cpp:96
void setItem(int index, int id, int quantity)
Sets the item at the given position.
Definition inventory.cpp:66
void removeInventoryListener(InventoryListener *listener)
int mUsed
The number of slots in use.
Definition inventory.h:139
void addItem(int id, int quantity)
Adds a new item in a free slot.
Definition inventory.cpp:61
void distributeSlotsChangedEvent()
int getLastUsedSlot() const
Returns the index of the last occupied slot or 0 if none occupied.
std::vector< std::unique_ptr< Item > > mItems
The holder of items.
Definition inventory.h:138
std::list< InventoryListener * > mInventoryListeners
Definition inventory.h:133
Item * findItem(int itemId) const
Searches for the specified item by it's id.
Definition inventory.cpp:52
Item * getItem(int index) const
Returns the item at the specified index.
Definition inventory.cpp:44
void addInventoryListener(InventoryListener *listener)
Inventory(Type type, int size=-1)
Constructor.
Definition inventory.cpp:36
void removeItemAt(int index)
Remove the item at the specified index from the inventory.
int getFreeSlot() const
Returns id of next free slot or -1 if all occupied.
int getSize() const
Returns the size that this instance is configured for.
Definition inventory.h:67
bool contains(Item *item) const
Checks if the given item is in the inventory.
Represents one or more instances of a certain item type.
Definition item.h:35
int getQuantity() const
Returns the number of items.
Definition item.h:69
int getId() const
Returns the item id.
Definition item.h:49
void setInvIndex(int index)
Sets the inventory index of this item.
Definition item.h:89
void warn(const char *log_text,...) LOG_PRINTF_ATTR
InventoryHandler * getInventoryHandler()
Definition net.cpp:90