GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/item/item.cpp Lines: 33 72 45.8 %
Date: 2017-11-29 Branches: 19 78 24.4 %

Line Branch Exec Source
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
33
#include "resources/item/itemoptionslist.h"
34
35
#include "resources/loaders/imageloader.h"
36
37
#include "net/net.h"
38
39
#include "utils/delete2.h"
40
41
#include "debug.h"
42
43
2
DragDrop dragDrop(nullptr, DragDropSource::Empty);
44
45
4
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
4
           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(),
63
    mOptions(nullptr),
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
12
    mFavorite(favorite)
73
{
74
4
    setId(id, color);
75
36
    for (int f = 0; f < maxCards; f ++)
76
16
        mCards[f] = 0;
77
4
}
78
79
20
Item::~Item()
80
{
81
4
    if (mImage != nullptr)
82
    {
83
4
        mImage->decRef();
84
4
        mImage = nullptr;
85
    }
86
4
    delete2(mOptions);
87
4
    dragDrop.clearItem(this);
88
8
}
89
90
4
void Item::setId(const int id,
91
                 const ItemColor color)
92
{
93
4
    mId = id;
94
4
    mColor = color;
95
96
    // Types 0 and 1 are not equippable items.
97

12
    mEquipment = fromBool(id && CAST_S32(getInfo().getType())
98
        >= 2, Equipm);
99
100
4
    if (mImage != nullptr)
101
        mImage->decRef();
102
103
4
    const ItemInfo &info = getInfo();
104
8
    mTags = info.getTags();
105
106
12
    const std::string dye = combineDye2(pathJoin(
107

24
        paths.getStringValue("itemIcons"), info.getDisplay().image),
108
16
        info.getDyeIconColorsString(color));
109
4
    mImage = Loader::getImage(dye);
110
111
4
    if (mImage == nullptr)
112
    {
113
        mImage = Theme::getImageFromTheme(paths.getValue("unknownItemFile",
114
            "unknown-item.png"));
115
    }
116
4
}
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);
130
    Image *image = Loader::getImage(combineDye2(pathJoin(paths.getStringValue(
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
2
std::string Item::getName() const
140
{
141
2
    const ItemInfo &info = ItemDB::get(mId);
142
#ifdef TMWA_SUPPORT
143
2
    if (Net::getNetworkType() == ServerType::TMWATHENA)
144
    {
145
        return info.getName();
146
    }
147
#endif  // TWMA_SUPPORT
148
2
    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
194
void Item::updateColor()
195
{
196
    if (Net::getNetworkType() != ServerType::TMWATHENA)
197
        setId(mId, ItemColorManager::getColorFromCards(&mCards[0]));
198

6
}