GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/shortcut/itemshortcut.cpp Lines: 0 128 0.0 %
Date: 2021-03-17 Branches: 0 188 0.0 %

Line Branch Exec Source
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
24
#include "gui/shortcut/itemshortcut.h"
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
35
#include "gui/windows/skilldialog.h"
36
37
#include "resources/inventory/inventory.h"
38
39
#include "resources/item/item.h"
40
41
#include "debug.h"
42
43
ItemShortcut *itemShortcut[SHORTCUT_TABS];
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
56
ItemShortcut::~ItemShortcut()
57
{
58
    logger->log1("ItemShortcut::~ItemShortcut");
59
}
60
61
void ItemShortcut::load()
62
{
63
    std::string name;
64
    std::string color;
65
    std::string data;
66
    if (mNumber == SHORTCUT_AUTO_TAB)
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));
136
            serverConfig.deleteKey(data + toString(i));
137
        }
138
    }
139
}
140
141
void ItemShortcut::clear()
142
{
143
    for (size_t i = 0; i < SHORTCUT_ITEMS; i++)
144
    {
145
        mItems[i] = 0;
146
        mItemColors[i] = ItemColor_zero;
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)
165
                PlayerInfo::useEquipItem(item, 0, Sfx_true);
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)
196
                    PlayerInfo::equipItem(item, Sfx_true);
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)
216
                    PlayerInfo::unequipItem(item, Sfx_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;
232
        mItemColorSelected = ItemColor_one;
233
    }
234
}
235
236
void ItemShortcut::setItem(const size_t index)
237
{
238
    mItems[index] = mItemSelected;
239
    mItemColors[index] = mItemColorSelected;
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
282
size_t ItemShortcut::getFreeIndex() const
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
}