ManaPlus
iteminfo.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  *
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/iteminfo.h"
24 
26 
28 
30 
31 #include "resources/itemmenuitem.h"
32 
33 #include "resources/db/colordb.h"
34 #include "resources/db/itemdb.h"
35 
36 #include "utils/checkutils.h"
37 #include "utils/dtor.h"
38 #include "utils/stringutils.h"
39 
40 #include "debug.h"
41 
43  mDisplay(),
44  mMissile(),
45  mName(),
46  mNameEn(),
47  mDescription(),
48  mEffect(),
49  mUseButton(),
50  mUseButton2(),
51  mType(ItemDbType::UNUSABLE),
52  mWeight(0),
53  mView(0),
54  mId(0),
55  mIsRemoveSprites(false),
56  mSpriteToItemReplaceList(),
57  mAttackAction(SpriteAction::INVALID),
58  mSkyAttackAction(SpriteAction::INVALID),
59  mWaterAttackAction(SpriteAction::INVALID),
60  mRideAttackAction(SpriteAction::INVALID),
61  mAttackRange(0),
62  mMissileParticle(),
63  mAnimationFiles(),
64  mSounds(),
65  mTags(),
66  mColorsList(nullptr),
67  mIconColorsList(nullptr),
68  mInventoryMenu(),
69  mStorageMenu(),
70  mCartMenu(),
71  mColorsListName(),
72  mIconColorsListName(),
73  mCardColor(ItemColor_zero),
74  mHitEffectId(-1),
75  mCriticalHitEffectId(-1),
76  mMissEffectId(-1),
77  maxFloorOffsetX(mapTileSize),
78  maxFloorOffsetY(mapTileSize),
79  mPickupCursor(Cursor::CURSOR_POINTER),
80  mProtected(false)
81 {
82  for (int f = 0; f < 10; f ++)
83  {
84  mSpriteToItemReplaceMap[f] = nullptr;
85  mDrawBefore[f] = -1;
86  mDrawAfter[f] = -1;
87  mDrawPriority[f] = 0;
88  }
89 }
90 
92 {
95  for (int f = 0; f < 10; f ++)
96  mSpriteToItemReplaceMap[f] = nullptr;
97 }
98 
99 const std::string &ItemInfo::getSprite(const GenderT gender,
100  const BeingTypeId race) const
101 {
102  if (mView != 0)
103  {
104  // Forward the request to the item defining how to view this item
105  return ItemDB::get(mView).getSprite(gender, race);
106  }
107  static const std::string empty;
108  std::map<int, std::string>::const_iterator i =
109  mAnimationFiles.find(CAST_S32(gender) +
110  toInt(race, int) * 4);
111 
112  if (i != mAnimationFiles.end())
113  return i->second;
114  i = mAnimationFiles.find(CAST_S32(gender));
115  if (i != mAnimationFiles.end())
116  return i->second;
117  return empty;
118 }
119 
120 void ItemInfo::setAttackAction(const std::string &attackAction)
121 {
122  if (attackAction.empty())
123  mAttackAction = SpriteAction::ATTACK; // (Equal to unarmed animation)
124  else
125  mAttackAction = attackAction;
126 }
127 
128 void ItemInfo::setSkyAttackAction(const std::string &attackAction)
129 {
130  if (attackAction.empty())
132  else
133  mSkyAttackAction = attackAction;
134 }
135 
136 void ItemInfo::setWaterAttackAction(const std::string &attackAction)
137 {
138  if (attackAction.empty())
140  else
141  mWaterAttackAction = attackAction;
142 }
143 
144 void ItemInfo::setRideAttackAction(const std::string &attackAction)
145 {
146  if (attackAction.empty())
148  else
149  mRideAttackAction = attackAction;
150 }
151 
153  const std::string &filename, const int delay)
154 {
155  mSounds[event].push_back(SoundInfo(
156  filename,
157  delay));
158 }
159 
161 {
162  static const SoundInfo empty("", 0);
163  std::map<ItemSoundEvent::Type, SoundInfoVect>::const_iterator i;
164 
165  i = mSounds.find(event);
166 
167  if (i == mSounds.end())
168  return empty;
169  return (!i->second.empty()) ? i->second[CAST_U32(rand())
170  % i->second.size()] : empty;
171 }
172 
174  const int direction)
175 {
176  if (direction < 0 || direction >= 10)
177  return nullptr;
178 
179  SpriteToItemMap *spMap = mSpriteToItemReplaceMap[direction];
180 
181  if (spMap == nullptr)
182  {
183  spMap = new SpriteToItemMap;
184  mSpriteToItemReplaceMap[direction] = spMap;
185  mSpriteToItemReplaceList.push_back(spMap);
186  }
187 
188  SpriteToItemMap::iterator it = spMap->find(sprite);
189  if (it == spMap->end())
190  {
191  IntMap tmp;
192  (*mSpriteToItemReplaceMap[direction])[sprite] = tmp;
193  it = mSpriteToItemReplaceMap[direction]->find(sprite);
194  }
195  return &it->second;
196 }
197 
198 void ItemInfo::setColorsList(const std::string &name)
199 {
200  if (name.empty())
201  {
202  mColorsList = nullptr;
203  mColorsListName.clear();
204  }
205  else
206  {
209  }
210 }
211 
212 void ItemInfo::setIconColorsList(const std::string &name)
213 {
214  if (name.empty())
215  {
216  mIconColorsList = nullptr;
217  mIconColorsListName.clear();
218  }
219  else
220  {
223  }
224 }
225 
226 std::string ItemInfo::getDyeColorsString(const ItemColor color) const
227 {
228  if ((mColorsList == nullptr) || mColorsListName.empty())
229  return "";
230 
231  const std::map <ItemColor, ItemColorData>::const_iterator
232  it = mColorsList->find(color);
233  if (it == mColorsList->end())
234  return "";
235 
236  return it->second.color;
237 }
238 
239 std::string ItemInfo::getDyeIconColorsString(const ItemColor color) const
240 {
241  if ((mIconColorsList == nullptr) || mIconColorsListName.empty())
242  return "";
243 
244  const std::map <ItemColor, ItemColorData>::const_iterator
245  it = mIconColorsList->find(color);
246  if (it == mIconColorsList->end())
247  return "";
248 
249  return it->second.color;
250 }
251 
252 const std::string ItemInfo::getDescription(const ItemColor color) const
253 {
254  return replaceColors(mDescription, color);
255 }
256 
257 const std::string ItemInfo::getName(const ItemColor color) const
258 {
259  return replaceColors(mName, color);
260 }
261 
262 const std::string ItemInfo::getNameEn(const ItemColor color) const
263 {
264  return replaceColors(mNameEn, color);
265 }
266 
267 const std::string ItemInfo::replaceColors(std::string str,
268  const ItemColor color) const
269 {
270  std::string name;
271  if ((mColorsList != nullptr) && !mColorsListName.empty())
272  {
273  const std::map <ItemColor, ItemColorData>::const_iterator
274  it = mColorsList->find(color);
275  if (it == mColorsList->end())
276  name = "unknown";
277  else
278  name = it->second.name;
279  }
280  else
281  {
282  name = "unknown";
283  }
284 
285  str = replaceAll(str, "%color%", name);
286  if (!name.empty())
287  name[0] = CAST_S8(toupper(name[0]));
288 
289  return replaceAll(str, "%Color%", name);
290 }
291 
293  const
294 {
295  if (direction < 0 || direction >= 10)
296  return nullptr;
297 
298  const SpriteToItemMap *const spMap = mSpriteToItemReplaceMap[direction];
299  if (spMap != nullptr)
300  return spMap;
301  if (direction == SpriteDirection::UPLEFT
302  || direction == SpriteDirection::UPRIGHT)
303  {
305  }
306 
307  if (direction == SpriteDirection::DOWNLEFT
308  || direction == SpriteDirection::DOWNRIGHT)
309  {
311  }
312 
313  return nullptr;
314 }
315 
316 void ItemInfo::setSpriteOrder(int *const ptr,
317  const int direction,
318  const int n,
319  const int def)
320 {
321  switch (direction)
322  {
323  case -1:
324  {
325  for (int f = 0; f < 10; f ++)
326  {
327  if (ptr[f] == def)
328  ptr[f] = n;
329  }
330  return;
331  }
332  case -2:
333  {
334  ptr[SpriteDirection::DOWN] = n;
335  ptr[SpriteDirection::DOWNLEFT] = n;
337  return;
338  }
339  case -3:
340  {
341  ptr[SpriteDirection::UP] = n;
342  ptr[SpriteDirection::UPLEFT] = n;
343  ptr[SpriteDirection::UPRIGHT] = n;
344  return;
345  }
346  default:
347  break;
348  }
349  if (direction < 0 || direction >= 9)
350  return;
351 
352  if (direction == SpriteDirection::UP)
353  {
354  if (ptr[SpriteDirection::UPLEFT] == def)
355  ptr[SpriteDirection::UPLEFT] = n;
356  if (ptr[SpriteDirection::UPRIGHT] == def)
357  ptr[SpriteDirection::UPRIGHT] = n;
358  }
359  else if (direction == SpriteDirection::DOWN)
360  {
361  if (ptr[SpriteDirection::DOWNLEFT] == def)
362  ptr[SpriteDirection::DOWNLEFT] = n;
363  if (ptr[SpriteDirection::DOWNRIGHT] == def)
365  }
366  ptr[direction] = n;
367 }
368 
369 void ItemInfo::setDrawBefore(const int direction, const int n)
370 {
371  setSpriteOrder(&mDrawBefore[0], direction, n, -1);
372 }
373 
374 void ItemInfo::setDrawAfter(const int direction, const int n)
375 {
376  setSpriteOrder(&mDrawAfter[0], direction, n, -1);
377 }
378 
379 void ItemInfo::setDrawPriority(const int direction, const int n)
380 {
381  setSpriteOrder(&mDrawPriority[0], direction, n, 0);
382 }
383 
384 int ItemInfo::getDrawBefore(const int direction) const
385 {
386  if (direction < 0 || direction >= 10)
387  return -1;
388  return mDrawBefore[direction];
389 }
390 
391 int ItemInfo::getDrawAfter(const int direction) const
392 {
393  if (direction < 0 || direction >= 10)
394  return -1;
395  return mDrawAfter[direction];
396 }
397 
398 int ItemInfo::getDrawPriority(const int direction) const
399 {
400  if (direction < 0 || direction >= 10)
401  return 0;
402  return mDrawPriority[direction];
403 }
404 
405 void ItemInfo::setSprite(const std::string &animationFile,
406  const GenderT gender,
407  const int race)
408 {
409  mAnimationFiles[CAST_S32(gender) + race * 4] = animationFile;
410 }
411 
412 std::string ItemInfo::getColorName(const ItemColor idx) const
413 {
414  if (mColorsList == nullptr)
415  return std::string();
416 
417  const std::map <ItemColor, ItemColorData>::const_iterator
418  it = mColorsList->find(idx);
419  if (it == mColorsList->end())
420  {
421  reportAlways("Color %d for palette %s not found",
422  CAST_S32(idx),
423  mColorsListName.c_str())
424  return std::string();
425  }
426  return it->second.name;
427 }
428 
429 std::string ItemInfo::getColor(const ItemColor idx) const
430 {
431  if (mColorsList == nullptr)
432  return std::string();
433 
434  const std::map <ItemColor, ItemColorData>::const_iterator
435  it = mColorsList->find(idx);
436  if (it == mColorsList->end())
437  {
438  reportAlways("Color %d for palette %s not found",
439  CAST_S32(idx),
440  mColorsListName.c_str())
441  return std::string();
442  }
443  return it->second.color;
444 }
445 
446 std::string ItemInfo::getIconColorName(const ItemColor idx) const
447 {
448  if (mIconColorsList == nullptr)
449  return std::string();
450 
451  const std::map <ItemColor, ItemColorData>::const_iterator
452  it = mIconColorsList->find(idx);
453  if (it == mIconColorsList->end())
454  {
455  reportAlways("Color %d for palette %s not found",
456  CAST_S32(idx),
457  mColorsListName.c_str())
458  return std::string();
459  }
460  return it->second.name;
461 }
462 
463 std::string ItemInfo::getIconColor(const ItemColor idx) const
464 {
465  if (mIconColorsList == nullptr)
466  return std::string();
467 
468  const std::map <ItemColor, ItemColorData>::const_iterator
469  it = mIconColorsList->find(idx);
470  if (it == mIconColorsList->end())
471  {
472  reportAlways("Color %d for palette %s not found",
473  CAST_S32(idx),
474  mColorsListName.c_str())
475  return std::string();
476  }
477  return it->second.color;
478 }
479 
480 const std::string ItemInfo::getLink() const
481 {
482  return strprintf("[@@%d|%[email protected]@]", mId, mName.c_str());
483 }
ItemInfo::mColorsListName
std::string mColorsListName
Definition: iteminfo.h:385
ItemInfo::mDrawBefore
int mDrawBefore[10]
Definition: iteminfo.h:332
ItemInfo::mDrawPriority
int mDrawPriority[10]
Definition: iteminfo.h:334
ItemInfo::addReplaceSprite
IntMap * addReplaceSprite(const int sprite, const int direction)
Definition: iteminfo.cpp:173
ItemInfo::setAttackAction
void setAttackAction(const std::string &attackAction)
Definition: iteminfo.cpp:120
reportAlways
#define reportAlways(...)
Definition: checkutils.h:252
SpriteAction::ATTACKRIDE
static const std::string ATTACKRIDE("attackride")
SpriteAction::INVALID
static const std::string INVALID
Definition: spriteaction.h:71
ItemInfo::mWaterAttackAction
std::string mWaterAttackAction
Definition: iteminfo.h:367
ItemDbType
Definition: itemdbtype.h:28
SpriteDirection::DOWN
@ DOWN
Definition: spritedirection.h:32
ItemInfo::mSkyAttackAction
std::string mSkyAttackAction
Definition: iteminfo.h:366
Cursor::CURSOR_POINTER
@ CURSOR_POINTER
Definition: cursor.h:28
ItemInfo::mColorsList
const std::map< ItemColor, ItemColorData > * mColorsList
Definition: iteminfo.h:380
SpriteDirection::UPLEFT
@ UPLEFT
Definition: spritedirection.h:35
itemdb.h
SpriteAction::ATTACK
static const std::string ATTACK("attack")
ItemInfo::getDrawBefore
int getDrawBefore(const int direction) const
Definition: iteminfo.cpp:384
ItemInfo::setSkyAttackAction
void setSkyAttackAction(const std::string &attackAction)
Definition: iteminfo.cpp:128
map.h
ItemInfo::setWaterAttackAction
void setWaterAttackAction(const std::string &attackAction)
Definition: iteminfo.cpp:136
IntMap
std::map< int, int > IntMap
Definition: intmap.h:26
ItemInfo::mIconColorsListName
std::string mIconColorsListName
Definition: iteminfo.h:386
ItemInfo::mDescription
std::string mDescription
Definition: iteminfo.h:346
BeingTypeId
int BeingTypeId
Definition: beingtypeid.h:29
ItemInfo::setSpriteOrder
static void setSpriteOrder(int *const ptr, const int direction, const int n, const int def)
Definition: iteminfo.cpp:316
anonymous_namespace{itemdb.cpp}::mTags
StringIntMap mTags
Definition: itemdb.cpp:63
ItemSoundEvent::Type
Type
Definition: itemsoundevent.h:26
ItemInfo::setDrawPriority
void setDrawPriority(const int direction, const int n)
Definition: iteminfo.cpp:379
SpriteDirection::DOWNLEFT
@ DOWNLEFT
Definition: spritedirection.h:37
ItemInfo::mRideAttackAction
std::string mRideAttackAction
Definition: iteminfo.h:368
GenderT
Gender ::T GenderT
Definition: gender.h:35
ItemInfo::getIconColor
std::string getIconColor(const ItemColor idx) const
Definition: iteminfo.cpp:463
CAST_U32
#define CAST_U32
Definition: cast.h:30
iteminfo.h
ItemInfo::mDrawAfter
int mDrawAfter[10]
Definition: iteminfo.h:333
toInt
#define toInt(val, name)
Definition: intdefines.h:46
SpriteAction::ATTACKSKY
static const std::string ATTACKSKY("attacksky")
ItemInfo::setSprite
void setSprite(const std::string &animationFile, const GenderT gender, const int race)
Definition: iteminfo.cpp:405
ItemInfo::addSound
void addSound(const ItemSoundEvent::Type event, const std::string &filename, const int delay)
Definition: iteminfo.cpp:152
ItemInfo::getColorName
std::string getColorName(const ItemColor idx) const
Definition: iteminfo.cpp:412
replaceAll
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
Definition: stringutils.cpp:367
ItemInfo::setIconColorsList
void setIconColorsList(const std::string &name)
Definition: iteminfo.cpp:212
ItemInfo::mAnimationFiles
std::map< int, std::string > mAnimationFiles
Definition: iteminfo.h:375
ItemInfo::getSound
const SoundInfo & getSound(const ItemSoundEvent::Type event) const
Definition: iteminfo.cpp:160
ItemInfo::getLink
const std::string getLink() const
Definition: iteminfo.cpp:480
ItemInfo::mAttackAction
std::string mAttackAction
Definition: iteminfo.h:365
nullptr
#define nullptr
Definition: localconsts.h:44
SpriteDirection::DOWNRIGHT
@ DOWNRIGHT
Definition: spritedirection.h:38
checkutils.h
ItemInfo::mId
int mId
Definition: iteminfo.h:353
ItemInfo::getDyeColorsString
std::string getDyeColorsString(const ItemColor color) const
Definition: iteminfo.cpp:226
strprintf
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
anonymous_namespace{sounddb.cpp}::mSounds
std::vector< std::string > mSounds
Definition: sounddb.cpp:36
ItemInfo::setDrawBefore
void setDrawBefore(const int direction, const int n)
Definition: iteminfo.cpp:369
ItemInfo::mView
int mView
Definition: iteminfo.h:352
ItemInfo::mSounds
std::map< ItemSoundEvent::Type, SoundInfoVect > mSounds
Definition: iteminfo.h:378
CAST_S8
#define CAST_S8
Definition: cast.h:25
SpriteAction
Definition: spriteaction.h:37
SpriteDirection::UP
@ UP
Definition: spritedirection.h:31
SoundInfo
Definition: soundinfo.h:33
ItemInfo::getDyeIconColorsString
std::string getDyeIconColorsString(const ItemColor color) const
Definition: iteminfo.cpp:239
ItemInfo::mNameEn
std::string mNameEn
Definition: iteminfo.h:345
colordb.h
ItemInfo::getNameEn
const std::string & getNameEn() const
Definition: iteminfo.h:82
ItemInfo::setRideAttackAction
void setRideAttackAction(const std::string &attackAction)
Definition: iteminfo.cpp:144
itemmenuitem.h
ItemInfo::getSpriteToItemReplaceMap
const SpriteToItemMap * getSpriteToItemReplaceMap(const int directions) const
Definition: iteminfo.cpp:292
ItemInfo::getColor
std::string getColor(const ItemColor idx) const
Definition: iteminfo.cpp:429
spriteaction.h
ItemInfo::getName
const std::string & getName() const
Definition: iteminfo.h:73
ItemInfo::setDrawAfter
void setDrawAfter(const int direction, const int n)
Definition: iteminfo.cpp:374
ItemInfo::setColorsList
void setColorsList(const std::string &name)
Definition: iteminfo.cpp:198
ItemInfo::getSprite
const std::string & getSprite(const GenderT gender, const BeingTypeId race) const
Definition: iteminfo.cpp:99
ItemColor
uint16_t ItemColor
Definition: itemcolor.h:29
stringutils.h
ItemInfo::ItemInfo
ItemInfo()
Definition: iteminfo.cpp:42
ItemInfo::getDrawAfter
int getDrawAfter(const int direction) const
Definition: iteminfo.cpp:391
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
empty
std::string empty
Definition: podict.cpp:25
SpriteAction::ATTACKWATER
static const std::string ATTACKWATER("attackwater")
SpriteDirection::UPRIGHT
@ UPRIGHT
Definition: spritedirection.h:36
ItemColor_zero
const ItemColor ItemColor_zero
Definition: itemcolor.h:29
ItemInfo::replaceColors
const std::string replaceColors(std::string str, const ItemColor color) const
Definition: iteminfo.cpp:267
CAST_S32
#define CAST_S32
Definition: cast.h:29
ItemInfo::mIconColorsList
const std::map< ItemColor, ItemColorData > * mIconColorsList
Definition: iteminfo.h:381
ItemInfo::mSpriteToItemReplaceList
std::vector< SpriteToItemMap * > mSpriteToItemReplaceList
Definition: iteminfo.h:358
ItemInfo::getDescription
const std::string & getDescription() const
Definition: iteminfo.h:99
ItemInfo::getDrawPriority
int getDrawPriority(const int direction) const
Definition: iteminfo.cpp:398
ItemDB::get
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
ItemInfo::mSpriteToItemReplaceMap
SpriteToItemMap * mSpriteToItemReplaceMap[10]
Definition: iteminfo.h:356
dtor.h
ItemDbType::UNUSABLE
@ UNUSABLE
Definition: itemdbtype.h:30
ItemInfo::mName
std::string mName
Definition: iteminfo.h:344
ItemInfo::~ItemInfo
~ItemInfo()
Definition: iteminfo.cpp:91
ItemInfo::getIconColorName
std::string getIconColorName(const ItemColor idx) const
Definition: iteminfo.cpp:446
ColorDB::getColorsList
const std::map< ItemColor, ItemColorData > * getColorsList(const std::string &name)
Definition: colordb.cpp:218
SpriteToItemMap
std::map< int, IntMap > SpriteToItemMap
Definition: iteminfo.h:42
debug.h
ItemFieldType::name
const std::string name
Definition: itemfieldtype.h:40
Cursor
Definition: cursor.h:26
spritedirection.h
mapTileSize
static const int mapTileSize
Definition: map.h:26