GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/inventory/inventory.h Lines: 4 4 100.0 %
Date: 2021-03-17 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#ifndef RESOURCES_INVENTORY_INVENTORY_H
25
#define RESOURCES_INVENTORY_INVENTORY_H
26
27
#include "enums/inventorytype.h"
28
29
#include "enums/resources/item/itemtype.h"
30
31
#include "enums/simpletypes/beingtypeid.h"
32
#include "enums/simpletypes/damaged.h"
33
#include "enums/simpletypes/equipm.h"
34
#include "enums/simpletypes/equipped.h"
35
#include "enums/simpletypes/favorite.h"
36
#include "enums/simpletypes/identified.h"
37
#include "enums/simpletypes/itemcolor.h"
38
39
#include "enums/being/gender.h"
40
41
#include "utils/intmap.h"
42
43
#include <list>
44
#include <string>
45
46
#include "localconsts.h"
47
48
class InventoryListener;
49
class Item;
50
51
struct ItemOptionsList;
52
53
class Inventory notfinal
54
{
55
    public:
56
        A_DELETE_COPY(Inventory)
57
58
        static const int NO_SLOT_INDEX = -1; /**< Slot has no index. */
59
60
        /**
61
         * Constructor.
62
         *
63
         * @param size the number of items that fit in the inventory
64
         */
65
        Inventory(const InventoryTypeT type,
66
                  const int size);
67
68
        /**
69
         * Destructor.
70
         */
71
        virtual ~Inventory();
72
73
        /**
74
         * Returns the size that this instance is configured for.
75
         */
76
        unsigned getSize() const noexcept2 A_WARN_UNUSED
77
4
        { return mSize; }
78
79
        /**
80
         * Returns the item at the specified index.
81
         */
82
        Item *getItem(const int index) const A_WARN_UNUSED;
83
84
        /**
85
         * Searches for the specified item by it's id.
86
         *
87
         * @param itemId The id of the item to be searched.
88
         * @param color The color of the item to be searched.
89
         * @return Item found on success, NULL on failure.
90
         */
91
        Item *findItem(const int itemId,
92
                       const ItemColor color) const A_WARN_UNUSED;
93
94
        /**
95
         * Adds a new item in a free slot.
96
         */
97
        int addItem(const int id,
98
                    const ItemTypeT type,
99
                    const int quantity,
100
                    const uint8_t refine,
101
                    const ItemColor color,
102
                    const Identified identified,
103
                    const Damaged damaged,
104
                    const Favorite favorite,
105
                    const Equipm equipment,
106
                    const Equipped equipped);
107
108
        /**
109
         * Sets the item at the given position.
110
         */
111
        virtual void setItem(const int index,
112
                             const int id,
113
                             const ItemTypeT type,
114
                             const int quantity,
115
                             const uint8_t refine,
116
                             const ItemColor color,
117
                             const Identified identified,
118
                             const Damaged damaged,
119
                             const Favorite favorite,
120
                             const Equipm equipment,
121
                             const Equipped equipped);
122
123
        void setCards(const int index,
124
                      const int *const cards,
125
                      const int size) const;
126
127
        void setOptions(const int index,
128
                        const ItemOptionsList *const options);
129
130
        void setTag(const int index,
131
                    const int tag);
132
133
        void moveItem(const int index1,
134
                      const int index2);
135
136
        /**
137
         * Remove a item from the inventory.
138
         */
139
        void removeItem(const int id);
140
141
        /**
142
         * Remove the item at the specified index from the inventory.
143
         */
144
        void removeItemAt(const int index);
145
146
        /**
147
         * Checks if the given item is in the inventory.
148
         */
149
        bool contains(const Item *const item) const A_WARN_UNUSED;
150
151
        /**
152
         * Returns id of next free slot or -1 if all occupied.
153
         */
154
        int getFreeSlot() const A_WARN_UNUSED;
155
156
        /**
157
         * Reset all item slots.
158
         */
159
        void clear();
160
161
        /**
162
         * Get the number of slots filled with an item
163
         */
164
        int getNumberOfSlotsUsed() const noexcept2 A_WARN_UNUSED
165
1
        { return mUsed; }
166
167
        /**
168
         * Returns the index of the last occupied slot or 0 if none occupied.
169
         */
170
        int getLastUsedSlot() const A_WARN_UNUSED;
171
172
        void addInventoyListener(InventoryListener *const listener);
173
174
        void removeInventoyListener(InventoryListener *const listener);
175
176
        InventoryTypeT getType() const noexcept2 A_WARN_UNUSED
177
7
        { return mType; }
178
179
        bool isMainInventory() const noexcept2 A_WARN_UNUSED
180
1
        { return mType == InventoryType::Inventory; }
181
182
        const Item *findItemBySprite(std::string spritePath,
183
                                     const GenderT gender,
184
                                     const BeingTypeId race)
185
                                     const A_WARN_UNUSED;
186
187
        std::string getName() const A_WARN_UNUSED;
188
189
        void resize(const unsigned int newSize);
190
191
        int findIndexByTag(const int tag) const;
192
193
        Item *findItemByTag(const int tag) const;
194
195
        virtual bool addVirtualItem(const Item *const item,
196
                                    int index,
197
                                    const int amount);
198
199
        void virtualRemove(Item *const item,
200
                           const int amount);
201
202
        void virtualRestore(const Item *const item,
203
                            const int amount);
204
205
        void restoreVirtuals();
206
207
    protected:
208
        typedef std::list<InventoryListener*> InventoryListenerList;
209
        InventoryListenerList mInventoryListeners;
210
211
        void distributeSlotsChangedEvent();
212
213
        IntMap mVirtualRemove;
214
        InventoryTypeT mType;
215
        unsigned mSize; /**< The max number of inventory items */
216
        Item **mItems;  /**< The holder of items */
217
        int mUsed;      /**< THe number of slots in use */
218
};
219
220
#endif  // RESOURCES_INVENTORY_INVENTORY_H