Mana
Loading...
Searching...
No Matches
shopitem.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 "shopitem.h"
23
24#include "units.h"
25
26#include "resources/iteminfo.h"
27
28ShopItem::ShopItem(int inventoryIndex, int id,
29 int quantity, int price) :
30 Item(id, 0),
31 mPrice(price)
32{
34 " (" + Units::formatCurrency(mPrice) + ")";
35 setInvIndex(inventoryIndex);
36 addDuplicate(inventoryIndex, quantity);
37}
38
39ShopItem::~ShopItem() = default;
40
41void ShopItem::addDuplicate(int inventoryIndex, int quantity)
42{
43 DuplicateItem &di = mDuplicates.emplace();
44 di.inventoryIndex = inventoryIndex;
45 di.quantity = quantity;
46 mQuantity += quantity;
47}
48
50{
51 DuplicateItem &dupl = mDuplicates.top();
52 int sellCount = quantity <= dupl.quantity ? quantity : dupl.quantity;
53 dupl.quantity -= sellCount;
54 mQuantity -= sellCount;
55 if (dupl.quantity == 0)
56 mDuplicates.pop();
57 return sellCount;
58}
std::string name
Definition iteminfo.h:113
Represents one or more instances of a certain item type.
Definition item.h:35
int mQuantity
Number of items.
Definition item.h:108
void setInvIndex(int index)
Sets the inventory index of this item.
Definition item.h:89
const ItemInfo & getInfo() const
Returns information about this item type.
Definition item.h:103
~ShopItem() override
ShopItem(int inventoryIndex, int id, int quantity, int price)
Constructor.
Definition shopitem.cpp:28
void addDuplicate(int inventoryIndex, int quantity)
Add a duplicate.
Definition shopitem.cpp:41
std::string mDisplayName
Definition shopitem.h:107
int sellCurrentDuplicate(int quantity)
Reduces the quantity of the topmost duplicate by the specified amount.
Definition shopitem.cpp:49
int mPrice
Definition shopitem.h:106
std::stack< DuplicateItem > mDuplicates
Definition shopitem.h:117
static std::string formatCurrency(int value)
Formats the given number in the correct currency format.
Definition units.cpp:216
Struct to keep track of duplicates.
Definition shopitem.h:113