GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/item/shopitem.h Lines: 0 9 0.0 %
Date: 2017-11-29 Branches: 0 2 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_ITEM_SHOPITEM_H
24
#define RESOURCES_ITEM_SHOPITEM_H
25
26
#include "resources/item/item.h"
27
28
#include <stack>
29
30
#include "localconsts.h"
31
32
/**
33
 * Represents an item in a shop inventory. It can store quantity and inventory
34
 * indices of duplicate entries in the shop as well.
35
 */
36
class ShopItem final : public Item
37
{
38
    public:
39
        /**
40
         * Constructor. Creates a new ShopItem.
41
         *
42
         * @param inventoryIndex the inventory index of the item
43
         * @param id the id of the item
44
         * @param quantity number of available copies of the item
45
         * @param price price of the item
46
         */
47
        ShopItem(const int inventoryIndex,
48
                 const int id,
49
                 const ItemTypeT type,
50
                 const ItemColor color,
51
                 const int quantity,
52
                 const int price,
53
                 const std::string &currency);
54
55
        /**
56
         * Constructor. Creates a new ShopItem. Inventory index will be set to
57
         * -1 and quantity to 0.
58
         *
59
         * @param id the id of the item
60
         * @param price price of the item
61
         */
62
        ShopItem(const int id,
63
                 const ItemTypeT type,
64
                 const ItemColor color,
65
                 const int price,
66
                 const std::string &currency);
67
68
        A_DELETE_COPY(ShopItem)
69
70
        /**
71
         * Destructor.
72
         */
73
        ~ShopItem();
74
75
        /**
76
         * Add a duplicate. Id and price will be taken from this item.
77
         *
78
         * @param inventoryIndex the inventory index of the item
79
         * @param quantity number of available copies of the item
80
         */
81
        void addDuplicate(const int inventoryIndex, const int quantity);
82
83
        /**
84
         * Add a duplicate. Id and price will be taken from this item.
85
         * Needed for compatibility with ShopDuplicateItems (see) class
86
         * documentation).
87
         */
88
        void addDuplicate();
89
90
        void update();
91
92
        /**
93
         * Gets the quantity of the currently topmost duplicate.
94
         *
95
         * @return the quantity of the currently topmost duplicate
96
         */
97
        int getCurrentQuantity() const A_WARN_UNUSED
98
        {
99
            return mDuplicates.empty() ? 0 : mDuplicates.top()->quantity;
100
        }
101
102
        /**
103
         * Gets the inventory index of the currently topmost duplicate.
104
         *
105
         * @return the inventory index of the currently topmost duplicate
106
         */
107
        int getCurrentInvIndex() const A_WARN_UNUSED
108
        {
109
            return mDuplicates.empty() ? mInvIndex :
110
                   mDuplicates.top()->inventoryIndex;
111
        }
112
113
        /**
114
         * Reduces the quantity of the topmost duplicate by the specified
115
         * amount. Also reduces the total quantity of this DuplicateItem.
116
         * Empty duplicates are automatically removed.
117
         *
118
         * If the amount is bigger than the quantity of the current topmost,
119
         * only sell as much as possible. Returns the amount actually sold (do
120
         * not ignore the return value!)
121
         *
122
         * @return the amount, that actually was sold.
123
         */
124
        int sellCurrentDuplicate(const int quantity);
125
126
        /**
127
         * Gets the price of the item.
128
         *
129
         * @return the price of the item
130
         */
131
        int getPrice() const noexcept2 A_WARN_UNUSED
132
        { return mPrice; }
133
134
        /**
135
         * Gets the display name for the item in the shop list.
136
         *
137
         * @return the display name for the item in the shop list
138
         */
139
        const std::string &getDisplayName() const noexcept2 A_WARN_UNUSED
140
        { return mDisplayName; }
141
142
        void setVisible(const bool b) noexcept2
143
        { mVisible = b; }
144
145
        bool isVisible() const noexcept2 A_WARN_UNUSED
146
        { return mVisible; }
147
148
        void increaseUsedQuantity(const int amount);
149
150
        int getUsedQuantity() const noexcept2 A_WARN_UNUSED
151
        { return mUsedQuantity; }
152
153
        void setDisabled(const bool b) noexcept2
154
        { mDisabled = b; }
155
156
        bool getDisabled() const noexcept2 A_WARN_UNUSED
157
        { return mDisabled; }
158
159
    protected:
160
        void updateDisplayName(const int quantity);
161
162
        std::string mDisplayName;
163
        std::string mCurrency;
164
165
        /**
166
         * Struct to keep track of duplicates.
167
         */
168
        typedef struct
169
        {
170
            int inventoryIndex;
171
            int quantity;
172
        } DuplicateItem;
173
        std::stack<DuplicateItem*> mDuplicates; /** <-- Stores duplicates */
174
        int mPrice;
175
        int mUsedQuantity;
176
        bool mShowQuantity;
177
        bool mVisible;
178
        bool mDisabled;
179
};
180
181
#endif  // RESOURCES_ITEM_SHOPITEM_H