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