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