ManaPlus
shopitem.cpp
Go to the documentation of this file.
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 
25 
26 #include "resources/iteminfo.h"
27 
28 #include "utils/stringutils.h"
29 
30 #ifdef TMWA_SUPPORT
31 #include "net/net.h"
32 #endif // TMWA_SUPPORT
33 
34 #include "resources/db/unitsdb.h"
35 
36 #include "debug.h"
37 
38 ShopItem::ShopItem(const int inventoryIndex,
39  const int id,
40  const ItemTypeT type,
41  const ItemColor color,
42  const int quantity,
43  const int price,
44  const std::string &currency) :
45  Item(id, type, 0, 0, color,
51  mDisplayName(),
52  mCurrency(currency),
53  mDuplicates(),
54  mPrice(price),
55  mUsedQuantity(0),
56  mShowQuantity(true),
57  mVisible(true),
58  mDisabled(false)
59 {
60  updateDisplayName(quantity);
61  setInvIndex(inventoryIndex);
62  addDuplicate(inventoryIndex, quantity);
63 }
64 
65 ShopItem::ShopItem(const int id,
66  const ItemTypeT type,
67  const ItemColor color,
68  const int price,
69  const std::string &currency) :
70  Item(id, type, 0, 0, color,
76  mDisplayName(),
77  mCurrency(currency),
78  mDuplicates(),
79  mPrice(price),
80  mUsedQuantity(0),
81  mShowQuantity(false),
82  mVisible(true),
83  mDisabled(false)
84 {
86  setInvIndex(-1);
87  addDuplicate(-1, 0);
88 }
89 
91 {
93  while (!mDuplicates.empty())
94  {
95  delete mDuplicates.top();
96  mDuplicates.pop();
97  }
98 }
99 
100 void ShopItem::updateDisplayName(const int quantity)
101 {
102 #ifdef TMWA_SUPPORT
104  mDisplayName = std::string(getInfo().getName());
105  else
106 #endif // TMWA_SUPPORT
107  mDisplayName = std::string(getInfo().getName(mColor));
108  if (mPrice != 0)
109  {
110  mDisplayName.append(" (").append(
111  UnitsDb::formatCurrency(mCurrency, mPrice)).append(") ");
112  }
113  if (mShowQuantity && quantity > 1)
114  mDisplayName.append("[").append(toString(quantity)).append("]");
115  if (mUsedQuantity > 0)
116  mDisplayName.append(" +").append(toString(mUsedQuantity));
117 }
118 
120 {
122 }
123 
124 void ShopItem::addDuplicate(const int inventoryIndex, const int quantity)
125 {
126  DuplicateItem *const di = new DuplicateItem;
127  di->inventoryIndex = inventoryIndex;
128  di->quantity = quantity;
129  mDuplicates.push(di);
130  mQuantity += quantity;
131 }
132 
134 {
135  DuplicateItem *const di = new DuplicateItem;
136  di->inventoryIndex = -1;
137  di->quantity = 0;
138  mDuplicates.push(di);
139 }
140 
141 int ShopItem::sellCurrentDuplicate(const int quantity)
142 {
143  DuplicateItem* dupl = mDuplicates.top();
144  if (dupl == nullptr)
145  return 0;
146 
147  const int sellCount = quantity <= dupl->quantity
148  ? quantity : dupl->quantity;
149  dupl->quantity -= sellCount;
150  mQuantity -= sellCount;
151  if (dupl->quantity == 0)
152  {
153  delete dupl;
154  mDuplicates.pop();
155  }
156  return sellCount;
157 }
158 
159 void ShopItem::increaseUsedQuantity(const int amount)
160 {
161  if (mShowQuantity && (mQuantity != 0))
162  {
163  if (mQuantity < mUsedQuantity + amount ||
164  mUsedQuantity + amount < 0)
165  {
166  return;
167  }
168  }
169  else if (mUsedQuantity + amount < 0)
170  {
171  return;
172  }
173 
174  mUsedQuantity += amount;
175 }
Definition: item.h:50
int mQuantity
Definition: item.h:248
void setInvIndex(const int index)
Definition: item.h:159
ItemColor mColor
Definition: item.h:247
std::string getName() const
Definition: item.cpp:140
const ItemInfo & getInfo() const
Definition: item.h:171
~ShopItem()
Definition: shopitem.cpp:90
int mUsedQuantity
Definition: shopitem.h:176
void updateDisplayName(const int quantity)
Definition: shopitem.cpp:100
ShopItem(const int inventoryIndex, const int id, const ItemTypeT type, const ItemColor color, const int quantity, const int price, const std::string &currency)
Definition: shopitem.cpp:38
void increaseUsedQuantity(const int amount)
Definition: shopitem.cpp:159
bool mShowQuantity
Definition: shopitem.h:177
std::stack< DuplicateItem * > mDuplicates
Definition: shopitem.h:174
std::string mCurrency
Definition: shopitem.h:164
std::string mDisplayName
Definition: shopitem.h:163
void addDuplicate()
Definition: shopitem.cpp:133
void update()
Definition: shopitem.cpp:119
int mPrice
Definition: shopitem.h:175
int sellCurrentDuplicate(const int quantity)
Definition: shopitem.cpp:141
const bool Damaged_false
Definition: damaged.h:30
const bool Equipm_false
Definition: equipm.h:30
const bool Equipped_false
Definition: equipped.h:30
const bool Favorite_false
Definition: favorite.h:30
const bool Identified_true
Definition: identified.h:30
uint16_t ItemColor
Definition: itemcolor.h:30
ItemType ::T ItemTypeT
Definition: itemtype.h:43
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
ServerTypeT getNetworkType()
Definition: net.cpp:189
std::string formatCurrency(const int value)
Definition: unitsdb.cpp:324