summaryrefslogtreecommitdiff
path: root/src/resources/inventory/complexinventory.cpp
blob: 82490ac8b7258c4536605cd91eb0b0ac9c50d0b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 *  The ManaPlus Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2017  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "resources/inventory/complexinventory.h"

#include "logger.h"

#include "being/playerinfo.h"

#include "resources/item/complexitem.h"

#include "debug.h"

ComplexInventory::ComplexInventory(const InventoryTypeT type,
                                   const int size1) :
    Inventory(type, size1)
{
}

ComplexInventory::~ComplexInventory()
{
}

bool ComplexInventory::addVirtualItem(const Item *const item,
                                      int index,
                                      const int amount)
{
    if (!item || PlayerInfo::isItemProtected(item->getId()))
        return false;

    if (index >= 0 && index < CAST_S32(mSize))
    {
        ComplexItem *citem = nullptr;
        if (mItems[index] != nullptr)
        {
            const Item *const item2 = mItems[index];
            if (item->getId() != item2->getId() ||
                item->getColor() != item2->getColor())
            {   // not same id or color
                return false;
            }
            citem = dynamic_cast<ComplexItem*>(mItems[index]);
            if (!citem)
            {   // if in inventory not complex item, converting it to complex
                citem = new ComplexItem(item2->getId(),
                    item2->getType(),
                    item2->getQuantity(),
                    item2->getRefine(),
                    item2->getColor(),
                    item2->getIdentified(),
                    item2->getDamaged(),
                    item2->getFavorite(),
                    Equipm_false,
                    Equipped_false);
                citem->setTag(item2->getTag());
                delete mItems[index];
                mItems[index] = citem;
            }
        }
        else
        {
            citem = new ComplexItem(item->getId(),
                item->getType(),
                0,
                item->getRefine(),
                item->getColor(),
                item->getIdentified(),
                item->getDamaged(),
                item->getFavorite(),
                Equipm_false,
                Equipped_false);
            mItems[index] = citem;
        }
        citem->addChild(item, amount);
    }
    else
    {
        index = addItem(item->getId(),
            item->getType(),
            1,
            1,
            item->getColor(),
            item->getIdentified(),
            item->getDamaged(),
            item->getFavorite(),
            Equipm_false,
            Equipped_false);
    }
    if (index == -1)
        return false;

    Item *const item2 = getItem(index);
    if (item2)
        item2->setTag(item->getInvIndex());
    return true;
}

void ComplexInventory::setItem(const int index,
                               const int id,
                               const ItemTypeT type,
                               const int quantity,
                               const uint8_t refine,
                               const ItemColor color,
                               const Identified identified,
                               const Damaged damaged,
                               const Favorite favorite,
                               const Equipm equipment,
                               const Equipped equipped)
{
    if (index < 0 || index >= CAST_S32(mSize))
    {
        logger->log("Warning: invalid inventory index: %d", index);
        return;
    }

    const Item *const item1 = mItems[index];
    if (!item1 && id > 0)
    {
        ComplexItem *const item = new ComplexItem(id,
            type,
            quantity,
            refine,
            color,
            identified,
            damaged,
            favorite,
            equipment,
            equipped);
        item->setInvIndex(index);

        Item *const item2 = new Item(id,
            type,
            quantity,
            refine,
            color,
            identified,
            damaged,
            favorite,
            equipment,
            equipped);
        item2->setInvIndex(index);
        item->addChild(item2, quantity);

        mItems[index] = item;
        mUsed ++;
        distributeSlotsChangedEvent();
    }
}