GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/inventory/complexinventory.cpp Lines: 6 58 10.3 %
Date: 2017-11-29 Branches: 0 36 0.0 %

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/inventory/complexinventory.h"
24
25
#include "logger.h"
26
27
#include "being/playerinfo.h"
28
29
#include "utils/cast.h"
30
31
#include "resources/item/complexitem.h"
32
33
#include "debug.h"
34
35
2
ComplexInventory::ComplexInventory(const InventoryTypeT type,
36
2
                                   const int size1) :
37
2
    Inventory(type, size1)
38
{
39
2
}
40
41
2
ComplexInventory::~ComplexInventory()
42
{
43
2
}
44
45
bool ComplexInventory::addVirtualItem(const Item *const item,
46
                                      int index,
47
                                      const int amount)
48
{
49
    if ((item == nullptr) || PlayerInfo::isItemProtected(item->getId()))
50
        return false;
51
52
    if (index >= 0 && index < CAST_S32(mSize))
53
    {
54
        ComplexItem *citem = nullptr;
55
        if (mItems[index] != nullptr)
56
        {
57
            const Item *const item2 = mItems[index];
58
            if (item->getId() != item2->getId() ||
59
                item->getColor() != item2->getColor())
60
            {   // not same id or color
61
                return false;
62
            }
63
            citem = dynamic_cast<ComplexItem*>(mItems[index]);
64
            if (citem == nullptr)
65
            {   // if in inventory not complex item, converting it to complex
66
                citem = new ComplexItem(item2->getId(),
67
                    item2->getType(),
68
                    item2->getQuantity(),
69
                    item2->getRefine(),
70
                    item2->getColor(),
71
                    item2->getIdentified(),
72
                    item2->getDamaged(),
73
                    item2->getFavorite(),
74
                    Equipm_false,
75
                    Equipped_false);
76
                citem->setTag(item2->getTag());
77
                delete mItems[index];
78
                mItems[index] = citem;
79
            }
80
        }
81
        else
82
        {
83
            citem = new ComplexItem(item->getId(),
84
                item->getType(),
85
                0,
86
                item->getRefine(),
87
                item->getColor(),
88
                item->getIdentified(),
89
                item->getDamaged(),
90
                item->getFavorite(),
91
                Equipm_false,
92
                Equipped_false);
93
            mItems[index] = citem;
94
        }
95
        citem->addChild(item, amount);
96
    }
97
    else
98
    {
99
        index = addItem(item->getId(),
100
            item->getType(),
101
            1,
102
            1,
103
            item->getColor(),
104
            item->getIdentified(),
105
            item->getDamaged(),
106
            item->getFavorite(),
107
            Equipm_false,
108
            Equipped_false);
109
    }
110
    if (index == -1)
111
        return false;
112
113
    Item *const item2 = getItem(index);
114
    if (item2 != nullptr)
115
        item2->setTag(item->getInvIndex());
116
    return true;
117
}
118
119
void ComplexInventory::setItem(const int index,
120
                               const int id,
121
                               const ItemTypeT type,
122
                               const int quantity,
123
                               const uint8_t refine,
124
                               const ItemColor color,
125
                               const Identified identified,
126
                               const Damaged damaged,
127
                               const Favorite favorite,
128
                               const Equipm equipment,
129
                               const Equipped equipped)
130
{
131
    if (index < 0 || index >= CAST_S32(mSize))
132
    {
133
        logger->log("Warning: invalid inventory index: %d", index);
134
        return;
135
    }
136
137
    const Item *const item1 = mItems[index];
138
    if ((item1 == nullptr) && id > 0)
139
    {
140
        ComplexItem *const item = new ComplexItem(id,
141
            type,
142
            quantity,
143
            refine,
144
            color,
145
            identified,
146
            damaged,
147
            favorite,
148
            equipment,
149
            equipped);
150
        item->setInvIndex(index);
151
152
        Item *const item2 = new Item(id,
153
            type,
154
            quantity,
155
            refine,
156
            color,
157
            identified,
158
            damaged,
159
            favorite,
160
            equipment,
161
            equipped);
162
        item2->setInvIndex(index);
163
        item->addChild(item2, quantity);
164
        delete item2;
165
166
        mItems[index] = item;
167
        mUsed ++;
168
        distributeSlotsChangedEvent();
169
    }
170
}