ManaPlus
item.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-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 #include "resources/item/item.h"
24 
25 #include "configuration.h"
26 #include "dragdrop.h"
27 #include "itemcolormanager.h"
28 
29 #include "gui/theme.h"
30 
31 #include "resources/iteminfo.h"
32 
34 
36 
37 #include "net/net.h"
38 
39 #include "utils/delete2.h"
40 
41 #include "debug.h"
42 
44 
45 Item::Item(const int id,
46  const ItemTypeT type,
47  const int quantity,
48  const uint8_t refine,
49  const ItemColor color,
50  const Identified identified,
51  const Damaged damaged,
52  const Favorite favorite,
53  const Equipm equipment,
54  const Equipped equipped) :
55  mId(0),
56  mColor(ItemColor_zero),
57  mQuantity(quantity),
58  mTag(0),
59  mImage(nullptr),
60  mDescription(),
61  mTags(),
62  mCards(),
64  mRefine(refine),
65  mInvIndex(0),
66  mType(type),
67  mEquipment(equipment),
68  mEquipped(equipped),
69  mInEquipment(false),
70  mIdentified(identified),
71  mDamaged(damaged),
72  mFavorite(favorite)
73 {
74  setId(id, color);
75  for (int f = 0; f < maxCards; f ++)
76  mCards[f] = 0;
77 }
78 
80 {
81  if (mImage != nullptr)
82  {
83  mImage->decRef();
84  mImage = nullptr;
85  }
87  dragDrop.clearItem(this);
88 }
89 
90 void Item::setId(const int id,
91  const ItemColor color)
92 {
93  mId = id;
94  mColor = color;
95 
96  // Types 0 and 1 are not equippable items.
98  >= 2, Equipm);
99 
100  if (mImage != nullptr)
101  mImage->decRef();
102 
103  const ItemInfo &info = getInfo();
104  mTags = info.getTags();
105 
106  const std::string dye = combineDye2(pathJoin(
107  paths.getStringValue("itemIcons"), info.getDisplay().image),
108  info.getDyeIconColorsString(color));
109  mImage = Loader::getImage(dye);
110 
111  if (mImage == nullptr)
112  {
113  mImage = Theme::getImageFromTheme(paths.getValue("unknownItemFile",
114  "unknown-item.png"));
115  }
116 }
117 
118 bool Item::isHaveTag(const int tagId) const
119 {
120  const std::map <int, int>::const_iterator it = mTags.find(tagId);
121  if (it == mTags.end())
122  return false;
123  return (*it).second > 0;
124 }
125 
126 Image *Item::getImage(const int id,
127  const ItemColor color)
128 {
129  const ItemInfo &info = ItemDB::get(id);
131  "itemIcons"), info.getDisplay().image),
132  info.getDyeIconColorsString(color)));
133 
134  if (image == nullptr)
135  image = Theme::getImageFromTheme("unknown-item.png");
136  return image;
137 }
138 
139 std::string Item::getName() const
140 {
141  const ItemInfo &info = ItemDB::get(mId);
142 #ifdef TMWA_SUPPORT
144  {
145  return info.getName();
146  }
147 #endif // TWMA_SUPPORT
148  return info.getName(mColor);
149 }
150 
151 void Item::setCard(const int index, const int id)
152 {
153  if (index < 0 || index >= maxCards)
154  return;
155  mCards[index] = id;
156 }
157 
158 int Item::getCard(const int index) const
159 {
160  if (index < 0 || index >= maxCards)
161  return 0;
162  return mCards[index];
163 }
164 
165 void Item::setCards(const int *const cards, const int size)
166 {
167  if (size < 0 || (cards == nullptr))
168  return;
169  int sz = size;
170  if (sz > maxCards)
171  sz = maxCards;
172  for (int f = 0; f < sz; f ++)
173  mCards[f] = cards[f];
174 }
175 
176 void Item::addCard(const int card)
177 {
178  for (int f = 0; f < maxCards; f ++)
179  {
180  if (mCards[f] == 0)
181  {
182  mCards[f] = card;
183  return;
184  }
185  }
186 }
187 
188 void Item::setOptions(const ItemOptionsList *const options)
189 {
190  delete2(mOptions);
191  mOptions = ItemOptionsList::copy(options);
192 }
193 
195 {
198 }
ItemOptionDb::OptionInfos mOptions
std::string getStringValue(const std::string &key) const
const ItemInfo & get(const int id)
Definition: itemdb.cpp:814
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:45
void setCards(const int *const cards, const int size)
Definition: item.cpp:165
static ItemColor getColorFromCards(const int *const cards)
void setId(const int id, const ItemColor color)
Definition: item.cpp:90
std::string pathJoin(std::string str1, const std::string &str2)
int mId
Definition: item.h:245
int getCard(const int index) const
Definition: item.cpp:158
void setOptions(const ItemOptionsList *const options)
Definition: item.cpp:188
uint16_t ItemColor
Definition: itemcolor.h:29
const ItemColor ItemColor_zero
Definition: itemcolor.h:29
void addCard(const int card)
Definition: item.cpp:176
const std::string & getName() const
Definition: iteminfo.h:73
#define delete2(var)
Definition: delete2.h:24
const ItemOptionsList * mOptions
Definition: item.h:255
Image * mImage
Definition: item.h:251
#define CAST_S32
Definition: cast.h:29
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:85
#define fromBool(val, name)
Definition: booldefines.h:48
bool info(InputEvent &event)
Definition: commands.cpp:56
void clearItem(const Item *const item)
Definition: dragdrop.h:227
virtual ~Item()
Definition: item.cpp:79
void setCard(const int index, const int id)
Definition: item.cpp:151
static Image * getImageFromTheme(const std::string &path)
Definition: theme.cpp:654
const IntMap & getTags() const
Definition: iteminfo.h:222
#define nullptr
Definition: localconsts.h:44
const SpriteDisplay & getDisplay() const
Definition: iteminfo.h:93
const ItemInfo & getInfo() const
Definition: item.h:170
std::string getDyeIconColorsString(const ItemColor color) const
Definition: iteminfo.cpp:239
static ItemOptionsList * copy(const ItemOptionsList *const options0)
ItemType ::T ItemTypeT
Definition: itemtype.h:42
Equipm mEquipment
Definition: item.h:259
std::string getName() const
Definition: item.cpp:139
ServerTypeT getNetworkType()
Definition: net.cpp:178
Configuration paths
DragDrop dragDrop(0, DragDropSource::Empty)
std::string getValue(const std::string &key, const std::string &deflt) const
ItemColor mColor
Definition: item.h:246
void updateColor()
Definition: item.cpp:194
Image * getImage() const
Definition: item.h:86
std::map< int, int > mTags
Definition: item.h:253
Equipment * mEquipment
Definition: playerinfo.cpp:63
int mCards[4]
Definition: item.h:254
ItemTypeT getType() const
Definition: item.h:224
Definition: image.h:61
std::string combineDye2(std::string file, const std::string &dye)
bool isHaveTag(const int tagId) const
Definition: item.cpp:118
void decRef()
Definition: image.cpp:521
#define maxCards
Definition: cards.h:24
std::string image
Definition: spritedisplay.h:44