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