ManaPlus
Data Structures | Public Member Functions | Protected Member Functions | Protected Attributes
ShopItem Class Reference

#include <shopitem.h>

Inheritance diagram for ShopItem:
Item

Data Structures

struct  DuplicateItem
 

Public Member Functions

 ShopItem (const int inventoryIndex, const int id, const ItemTypeT type, const ItemColor color, const int quantity, const int price, const std::string &currency)
 
 ShopItem (const int id, const ItemTypeT type, const ItemColor color, const int price, const std::string &currency)
 
 ~ShopItem ()
 
void addDuplicate (const int inventoryIndex, const int quantity)
 
void addDuplicate ()
 
void update ()
 
int getCurrentQuantity () const
 
int getCurrentInvIndex () const
 
int sellCurrentDuplicate (const int quantity)
 
int getPrice () const
 
const std::string & getDisplayName () const
 
void setVisible (const bool b)
 
bool isVisible () const
 
void increaseUsedQuantity (const int amount)
 
int getUsedQuantity () const
 
void setDisabled (const bool b)
 
bool getDisabled () const
 
- Public Member Functions inherited from Item
 Item (const int id, const ItemTypeT type, const int quantity, const uint8_t refine, const ItemColor color, const Identified identified, const Damaged damaged, const Favorite favorite, const Equipm equipment, const Equipped equipped)
 
virtual ~Item ()
 
void setId (const int id, const ItemColor color)
 
int getId () const
 
ImagegetImage () const
 
void setQuantity (const int quantity)
 
void increaseQuantity (const int amount)
 
int getQuantity () const
 
void setEquipment (const Equipm equipment)
 
Equipm isEquipment () const
 
void setEquipped (const Equipped equipped)
 
Equipped isEquipped () const
 
void setRefine (const uint8_t refine)
 
uint8_t getRefine () const
 
void setInEquipment (const bool inEquipment)
 
bool isInEquipment () const
 
void setInvIndex (const int index)
 
int getInvIndex () const
 
const ItemInfogetInfo () const
 
std::string getName () const
 
bool isHaveTag (const int tagId) const
 
ItemColor getColor () const
 
void setColor (const ItemColor color)
 
const std::string & getDescription () const
 
void setIdentified (const Identified b)
 
Identified getIdentified () const
 
void setDamaged (const Damaged b)
 
Damaged getDamaged () const
 
void setFavorite (const Favorite b)
 
Favorite getFavorite () const
 
void setCard (const int index, const int id)
 
int getCard (const int index) const
 
void setCards (const int *const cards, const int size)
 
const int * getCards () const
 
void setOptions (const ItemOptionsList *const options)
 
const ItemOptionsListgetOptions () const
 
void setType (const ItemTypeT type)
 
ItemTypeT getType () const
 
void setTag (const int tag)
 
int getTag () const
 
void addCard (const int card)
 
void updateColor ()
 

Protected Member Functions

void updateDisplayName (const int quantity)
 

Protected Attributes

std::string mDisplayName
 
std::string mCurrency
 
std::stack< DuplicateItem * > mDuplicates
 
int mPrice
 
int mUsedQuantity
 
bool mShowQuantity
 
bool mVisible
 
bool mDisabled
 
- Protected Attributes inherited from Item
ImagemImage
 
std::string mDescription
 
std::map< int, int > mTags
 
int mCards [4]
 
const ItemOptionsListmOptions
 
uint8_t mRefine
 
int mInvIndex
 
ItemTypeT mType
 
Equipm mEquipment
 
Equipped mEquipped
 
bool mInEquipment
 
Identified mIdentified
 
Damaged mDamaged
 
Favorite mFavorite
 

Additional Inherited Members

- Static Public Member Functions inherited from Item
static ImagegetImage (const int id, const ItemColor color)
 
static bool isItem (const int id)
 
- Data Fields inherited from Item
int mId
 
ItemColor mColor
 
int mQuantity
 
int mTag
 

Detailed Description

Represents an item in a shop inventory. It can store quantity and inventory indices of duplicate entries in the shop as well.

Definition at line 37 of file shopitem.h.

Constructor & Destructor Documentation

◆ ShopItem() [1/2]

ShopItem::ShopItem ( const int  inventoryIndex,
const int  id,
const ItemTypeT  type,
const ItemColor  color,
const int  quantity,
const int  price,
const std::string &  currency 
)

Constructor. Creates a new ShopItem.

Parameters
inventoryIndexthe inventory index of the item
idthe id of the item
quantitynumber of available copies of the item
priceprice of the item

Definition at line 38 of file shopitem.cpp.

44  :
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 }
void setInvIndex(const int index)
Definition: item.h:159
Item(const int id, const ItemTypeT type, const int quantity, const uint8_t refine, const ItemColor color, const Identified identified, const Damaged damaged, const Favorite favorite, const Equipm equipment, const Equipped equipped)
Definition: item.cpp:46
bool mVisible
Definition: shopitem.h:178
int mUsedQuantity
Definition: shopitem.h:176
void updateDisplayName(const int quantity)
Definition: shopitem.cpp:100
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
int mPrice
Definition: shopitem.h:175
bool mDisabled
Definition: shopitem.h:179
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

References addDuplicate(), Item::setInvIndex(), and updateDisplayName().

◆ ShopItem() [2/2]

ShopItem::ShopItem ( const int  id,
const ItemTypeT  type,
const ItemColor  color,
const int  price,
const std::string &  currency 
)

Constructor. Creates a new ShopItem. Inventory index will be set to -1 and quantity to 0.

Parameters
idthe id of the item
priceprice of the item

Definition at line 65 of file shopitem.cpp.

69  :
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 }

References addDuplicate(), Item::setInvIndex(), and updateDisplayName().

◆ ~ShopItem()

ShopItem::~ShopItem ( )

Destructor.

Clear all remaining duplicates on Object destruction.

Definition at line 90 of file shopitem.cpp.

91 {
93  while (!mDuplicates.empty())
94  {
95  delete mDuplicates.top();
96  mDuplicates.pop();
97  }
98 }

References mDuplicates.

Member Function Documentation

◆ addDuplicate() [1/2]

void ShopItem::addDuplicate ( )

Add a duplicate. Id and price will be taken from this item. Needed for compatibility with ShopDuplicateItems (see) class documentation).

Definition at line 133 of file shopitem.cpp.

134 {
135  DuplicateItem *const di = new DuplicateItem;
136  di->inventoryIndex = -1;
137  di->quantity = 0;
138  mDuplicates.push(di);
139 }

References ShopItem::DuplicateItem::inventoryIndex, mDuplicates, and ShopItem::DuplicateItem::quantity.

Referenced by ShopItem().

◆ addDuplicate() [2/2]

void ShopItem::addDuplicate ( const int  inventoryIndex,
const int  quantity 
)

Add a duplicate. Id and price will be taken from this item.

Parameters
inventoryIndexthe inventory index of the item
quantitynumber of available copies of the item

Definition at line 124 of file shopitem.cpp.

125 {
126  DuplicateItem *const di = new DuplicateItem;
127  di->inventoryIndex = inventoryIndex;
128  di->quantity = quantity;
129  mDuplicates.push(di);
130  mQuantity += quantity;
131 }
int mQuantity
Definition: item.h:248

References ShopItem::DuplicateItem::inventoryIndex, mDuplicates, Item::mQuantity, and ShopItem::DuplicateItem::quantity.

Referenced by ShopItems::addItem2().

◆ getCurrentInvIndex()

int ShopItem::getCurrentInvIndex ( ) const
inline

Gets the inventory index of the currently topmost duplicate.

Returns
the inventory index of the currently topmost duplicate

Definition at line 108 of file shopitem.h.

109  {
110  return mDuplicates.empty() ? mInvIndex :
111  mDuplicates.top()->inventoryIndex;
112  }
int mInvIndex
Definition: item.h:258

References mDuplicates, and Item::mInvIndex.

Referenced by EAthena::BarterHandler::buyItems(), EAthena::NpcHandler::sellItems(), and NpcSellDialog::sellOneItem().

◆ getCurrentQuantity()

int ShopItem::getCurrentQuantity ( ) const
inline

Gets the quantity of the currently topmost duplicate.

Returns
the quantity of the currently topmost duplicate

Definition at line 98 of file shopitem.h.

99  {
100  return mDuplicates.empty() ? 0 : mDuplicates.top()->quantity;
101  }

References mDuplicates.

◆ getDisabled()

bool ShopItem::getDisabled ( ) const
inline

Definition at line 157 of file shopitem.h.

158  { return mDisabled; }

References mDisabled.

Referenced by ShopListBox::draw().

◆ getDisplayName()

const std::string& ShopItem::getDisplayName ( ) const
inline

Gets the display name for the item in the shop list.

Returns
the display name for the item in the shop list

Definition at line 140 of file shopitem.h.

141  { return mDisplayName; }

References mDisplayName.

◆ getPrice()

int ShopItem::getPrice ( ) const
inline

◆ getUsedQuantity()

int ShopItem::getUsedQuantity ( ) const
inline

◆ increaseUsedQuantity()

void ShopItem::increaseUsedQuantity ( const int  amount)

Definition at line 159 of file shopitem.cpp.

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 }

References Item::mQuantity, mShowQuantity, and mUsedQuantity.

Referenced by BuyDialog::action(), EAthena::VendingHandler::buyItems(), EAthena::CashShopHandler::buyItems(), EAthena::BarterHandler::buyItems(), EAthena::MarketHandler::buyItems(), EAthena::NpcHandler::buyItems(), EAthena::NpcHandler::sellItems(), and NpcSellDialog::sellManyItems().

◆ isVisible()

bool ShopItem::isVisible ( ) const
inline

Definition at line 146 of file shopitem.h.

147  { return mVisible; }

References mVisible.

Referenced by ShopItems::updateList().

◆ sellCurrentDuplicate()

int ShopItem::sellCurrentDuplicate ( const int  quantity)

Reduces the quantity of the topmost duplicate by the specified amount. Also reduces the total quantity of this DuplicateItem. Empty duplicates are automatically removed.

If the amount is bigger than the quantity of the current topmost, only sell as much as possible. Returns the amount actually sold (do not ignore the return value!)

Returns
the amount, that actually was sold.

Definition at line 141 of file shopitem.cpp.

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 }

References mDuplicates, Item::mQuantity, and ShopItem::DuplicateItem::quantity.

Referenced by NpcSellDialog::sellOneItem().

◆ setDisabled()

void ShopItem::setDisabled ( const bool  b)
inline

Definition at line 154 of file shopitem.h.

155  { mDisabled = b; }

References mDisabled.

Referenced by ShopWindow::showList().

◆ setVisible()

void ShopItem::setVisible ( const bool  b)
inline

Definition at line 143 of file shopitem.h.

144  { mVisible = b; }

References mVisible.

Referenced by BuyDialog::applyNameFilter().

◆ update()

void ShopItem::update ( )

◆ updateDisplayName()

void ShopItem::updateDisplayName ( const int  quantity)
protected

Definition at line 100 of file shopitem.cpp.

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 }
ItemColor mColor
Definition: item.h:247
std::string getName() const
Definition: item.cpp:140
const ItemInfo & getInfo() const
Definition: item.h:171
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

References UnitsDb::formatCurrency(), Item::getInfo(), Item::getName(), Net::getNetworkType(), Item::mColor, mCurrency, mDisplayName, mPrice, mShowQuantity, mUsedQuantity, ServerType::TMWATHENA, and Catch::toString().

Referenced by ShopItem(), and update().

Field Documentation

◆ mCurrency

std::string ShopItem::mCurrency
protected

Definition at line 164 of file shopitem.h.

Referenced by updateDisplayName().

◆ mDisabled

bool ShopItem::mDisabled
protected

Definition at line 179 of file shopitem.h.

Referenced by getDisabled(), and setDisabled().

◆ mDisplayName

std::string ShopItem::mDisplayName
protected

Definition at line 163 of file shopitem.h.

Referenced by getDisplayName(), and updateDisplayName().

◆ mDuplicates

std::stack<DuplicateItem*> ShopItem::mDuplicates
protected

◆ mPrice

int ShopItem::mPrice
protected

<– Stores duplicates

Definition at line 175 of file shopitem.h.

Referenced by getPrice(), and updateDisplayName().

◆ mShowQuantity

bool ShopItem::mShowQuantity
protected

Definition at line 177 of file shopitem.h.

Referenced by increaseUsedQuantity(), and updateDisplayName().

◆ mUsedQuantity

int ShopItem::mUsedQuantity
protected

Definition at line 176 of file shopitem.h.

Referenced by getUsedQuantity(), increaseUsedQuantity(), and updateDisplayName().

◆ mVisible

bool ShopItem::mVisible
protected

Definition at line 178 of file shopitem.h.

Referenced by isVisible(), and setVisible().


The documentation for this class was generated from the following files: