ManaPlus
itemshortcut.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2007-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 "configuration.h"
27 #include "spellmanager.h"
28 
29 #include "being/playerinfo.h"
30 
31 #include "const/spells.h"
32 
33 #include "const/resources/skill.h"
34 
36 
38 
39 #include "resources/item/item.h"
40 
41 #include "debug.h"
42 
44 
45 ItemShortcut::ItemShortcut(const size_t number) :
46  mItems(),
47  mItemColors(),
48  mItemData(),
49  mNumber(number),
50  mItemSelected(-1),
51  mItemColorSelected(ItemColor_one)
52 {
53  load();
54 }
55 
57 {
58  logger->log1("ItemShortcut::~ItemShortcut");
59 }
60 
62 {
63  std::string name;
64  std::string color;
65  std::string data;
67  return;
68 
69  const Configuration *cfg = &serverConfig;
70  if (mNumber != 0)
71  {
72  name = std::string("shortcut").append(
73  toString(CAST_S32(mNumber))).append("_");
74  color = std::string("shortcutColor").append(
75  toString(CAST_U32(mNumber))).append("_");
76  data = std::string("shortcutData").append(
77  toString(CAST_U32(mNumber))).append("_");
78  }
79  else
80  {
81  name = "shortcut";
82  color = "shortcutColor";
83  data = "shortcutData";
84  }
85  for (unsigned int i = 0; i < SHORTCUT_ITEMS; i++)
86  {
87  const int itemId = cfg->getValue(name + toString(i), -1);
88  const ItemColor itemColor = fromInt(
89  cfg->getValue(color + toString(i), 1),
90  ItemColor);
91 
92  mItems[i] = itemId;
93  mItemColors[i] = itemColor;
94  mItemData[i] = cfg->getValue(data + toString(i), std::string());
95  }
96 }
97 
98 void ItemShortcut::save() const
99 {
100  std::string name;
101  std::string color;
102  std::string data;
103  if (mNumber == SHORTCUT_AUTO_TAB)
104  return;
105  if (mNumber != 0)
106  {
107  name = std::string("shortcut").append(
108  toString(CAST_S32(mNumber))).append("_");
109  color = std::string("shortcutColor").append(
110  toString(CAST_U32(mNumber))).append("_");
111  data = std::string("shortcutData").append(
112  toString(CAST_U32(mNumber))).append("_");
113  }
114  else
115  {
116  name = "shortcut";
117  color = "shortcutColor";
118  data = "shortcutData";
119  }
120 
121  for (unsigned int i = 0; i < SHORTCUT_ITEMS; i++)
122  {
123  const int itemId = mItems[i] != 0 ? mItems[i] : -1;
124  if (itemId != -1)
125  {
126  const int itemColor = toInt(mItemColors[i], int);
127  const std::string itemData = mItemData[i];
128  serverConfig.setValue(name + toString(i), itemId);
129  serverConfig.setValue(color + toString(i), itemColor);
130  serverConfig.setValue(data + toString(i), itemData);
131  }
132  else
133  {
134  serverConfig.deleteKey(name + toString(i));
135  serverConfig.deleteKey(color + toString(i));
137  }
138  }
139 }
140 
142 {
143  for (size_t i = 0; i < SHORTCUT_ITEMS; i++)
144  {
145  mItems[i] = 0;
147  mItemData[i].clear();
148  }
149 }
150 
151 void ItemShortcut::useItem(const size_t index) const
152 {
153  const Inventory *const inv = PlayerInfo::getInventory();
154  if (inv == nullptr)
155  return;
156 
157  const int itemId = mItems[index];
158  const ItemColor itemColor = mItemColors[index];
159  if (itemId >= 0)
160  {
161  if (itemId < SPELL_MIN_ID)
162  {
163  const Item *const item = inv->findItem(itemId, itemColor);
164  if (item != nullptr && item->getQuantity() != 0)
166  }
167  else if (itemId < SKILL_MIN_ID && (spellManager != nullptr))
168  {
169  spellManager->useItem(itemId);
170  }
171  else if (skillDialog != nullptr)
172  {
173  skillDialog->useItem(itemId,
174  fromBool(config.getBoolValue("skillAutotarget"), AutoTarget),
175  toInt(itemColor, int),
176  mItemData[index]);
177  }
178  }
179 }
180 
181 void ItemShortcut::equipItem(const size_t index) const
182 {
183  const Inventory *const inv = PlayerInfo::getInventory();
184  if (inv == nullptr)
185  return;
186 
187  const int itemId = mItems[index];
188  if (itemId != 0)
189  {
190  const Item *const item = inv->findItem(itemId, mItemColors[index]);
191  if ((item != nullptr) && (item->getQuantity() != 0))
192  {
193  if (item->isEquipment() == Equipm_true)
194  {
195  if (item->isEquipped() == Equipped_false)
197  }
198  }
199  }
200 }
201 void ItemShortcut::unequipItem(const size_t index) const
202 {
203  const Inventory *const inv = PlayerInfo::getInventory();
204  if (inv == nullptr)
205  return;
206 
207  const int itemId = mItems[index];
208  if (itemId != 0)
209  {
210  const Item *const item = inv->findItem(itemId, mItemColors[index]);
211  if ((item != nullptr) && (item->getQuantity() != 0))
212  {
213  if (item->isEquipment() == Equipm_true)
214  {
215  if (item->isEquipped() == Equipped_true)
217  }
218  }
219  }
220 }
221 
222 void ItemShortcut::setItemSelected(const Item *const item)
223 {
224  if (item != nullptr)
225  {
226  mItemSelected = item->getId();
227  mItemColorSelected = item->getColor();
228  }
229  else
230  {
231  mItemSelected = -1;
233  }
234 }
235 
236 void ItemShortcut::setItem(const size_t index)
237 {
238  mItems[index] = mItemSelected;
240  save();
241 }
242 
243 void ItemShortcut::setItem(const size_t index,
244  const int item,
245  const ItemColor color)
246 {
247  mItems[index] = item;
248  mItemColors[index] = color;
249  save();
250 }
251 
252 void ItemShortcut::setItemFast(const size_t index,
253  const int item,
254  const ItemColor color)
255 {
256  mItems[index] = item;
257  mItemColors[index] = color;
258 }
259 
260 void ItemShortcut::swap(const size_t index1,
261  const size_t index2)
262 {
263  if (CAST_U32(index1) >= SHORTCUT_ITEMS ||
264  CAST_U32(index2) >= SHORTCUT_ITEMS)
265  {
266  return;
267  }
268 
269  const int tmpItem = mItems[index1];
270  mItems[index1] = mItems[index2];
271  mItems[index2] = tmpItem;
272  const ItemColor tmpColor = mItemColors[index1];
273  mItemColors[index1] = mItemColors[index2];
274  mItemColors[index2] = tmpColor;
275 
276  const std::string tmpData = mItemData[index1];
277  mItemData[index1] = mItemData[index2];
278  mItemData[index2] = tmpData;
279  save();
280 }
281 
283 {
284  for (size_t i = 0; i < SHORTCUT_ITEMS; i++)
285  {
286  if (mItems[i] < 0)
287  return i;
288  }
289  return SHORTCUT_ITEMS;
290 }
bool AutoTarget
Definition: autotarget.h:30
#define fromBool(val, name)
Definition: booldefines.h:49
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
std::string getValue(const std::string &key, const std::string &deflt) const
void deleteKey(const std::string &key)
bool getBoolValue(const std::string &key) const
void setValue(const std::string &key, const std::string &value)
Item * findItem(const int itemId, const ItemColor color) const
Definition: inventory.cpp:94
size_t getFreeIndex() const
void save() const
void setItemFast(const size_t index, const int item, const ItemColor color)
void setItemSelected(const int itemId)
Definition: itemshortcut.h:126
size_t mNumber
Definition: itemshortcut.h:177
ItemShortcut(const size_t number)
int mItems[SHORTCUT_ITEMS]
Definition: itemshortcut.h:174
void equipItem(const size_t index) const
ItemColor mItemColors[SHORTCUT_ITEMS]
Definition: itemshortcut.h:175
ItemColor mItemColorSelected
Definition: itemshortcut.h:179
void swap(const size_t index1, const size_t index2)
void setItem(const size_t index)
void useItem(const size_t index) const
std::string mItemData[SHORTCUT_ITEMS]
Definition: itemshortcut.h:176
void unequipItem(const size_t index) const
Definition: item.h:50
Equipm isEquipment() const
Definition: item.h:117
int getQuantity() const
Definition: item.h:105
Equipped isEquipped() const
Definition: item.h:129
int getId() const
Definition: item.h:81
ItemColor getColor() const
Definition: item.h:181
void log1(const char *const log_text)
Definition: logger.cpp:238
void useItem(const int itemId, const AutoTarget autoTarget, const int level, const std::string &data) const
void useItem(const int itemId) const
Configuration config
Configuration serverConfig
const size_t SHORTCUT_AUTO_TAB
Definition: itemshortcut.h:29
const unsigned int SHORTCUT_ITEMS
Definition: itemshortcut.h:27
const unsigned int SHORTCUT_TABS
Definition: itemshortcut.h:28
const bool Equipm_true
Definition: equipm.h:30
const bool Equipped_false
Definition: equipped.h:30
const bool Equipped_true
Definition: equipped.h:30
#define toInt(val, name)
Definition: intdefines.h:47
#define fromInt(val, name)
Definition: intdefines.h:46
const ItemColor ItemColor_one
Definition: itemcolor.h:30
uint16_t ItemColor
Definition: itemcolor.h:30
const ItemColor ItemColor_zero
Definition: itemcolor.h:30
ItemShortcut * itemShortcut[SHORTCUT_TABS]
Logger * logger
Definition: logger.cpp:89
uint32_t data
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
void useEquipItem(const Item *const item, const int16_t useType, const Sfx sfx)
Definition: playerinfo.cpp:262
Inventory * getInventory()
Definition: playerinfo.cpp:195
void equipItem(const Item *const item, const Sfx sfx)
Definition: playerinfo.cpp:238
void unequipItem(const Item *const item, const Sfx sfx)
Definition: playerinfo.cpp:246
const int SKILL_MIN_ID
Definition: skill.h:25
const bool Sfx_true
Definition: sfx.h:30
SkillDialog * skillDialog
Definition: skilldialog.cpp:66
SpellManager * spellManager
const int SPELL_MIN_ID
Definition: spells.h:27