1 /*
2  *  The ManaPlus Client
3  *  Copyright (C) 2011-2019  The ManaPlus Developers
4  *  Copyright (C) 2019-2021  Andrei Karas
5  *
6  *  This file is part of The ManaPlus Client.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "resources/item/complexitem.h"
23 
24 #include "utils/dtor.h"
25 #include "utils/foreach.h"
26 
27 #include "debug.h"
28 
ComplexItem(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)29 ComplexItem::ComplexItem(const int id,
30                          const ItemTypeT type,
31                          const int quantity,
32                          const uint8_t refine,
33                          const ItemColor color,
34                          const Identified identified,
35                          const Damaged damaged,
36                          const Favorite favorite,
37                          const Equipm equipment,
38                          const Equipped equipped) :
39     Item(id,
40          type,
41          quantity,
42          refine,
43          color,
44          identified,
45          damaged,
46          favorite,
47          equipment,
48          equipped),
49     mChildItems()
50 {
51 }
52 
~ComplexItem()53 ComplexItem::~ComplexItem()
54 {
55     delete_all(mChildItems);
56     mChildItems.clear();
57 }
58 
addChild(const Item * const item,const int amount)59 void ComplexItem::addChild(const Item *const item,
60                            const int amount)
61 {
62     if (item == nullptr)
63         return;
64     increaseQuantity(amount);
65     Item *child = nullptr;
66     FOR_EACH (STD_VECTOR<Item*>::iterator, it, mChildItems)
67     {
68         Item *const item1 = *it;
69         if (item1->getId() == item->getId() &&
70             item1->getInvIndex() == item->getInvIndex() &&
71             item1->getTag() == item->getTag())
72         {
73             child = item1;
74             break;
75         }
76     }
77     if (child != nullptr)
78     {
79         child->increaseQuantity(amount);
80     }
81     else
82     {
83         child = new ComplexItem(item->getId(),
84             item->getType(),
85             amount,
86             item->getRefine(),
87             item->getColor(),
88             item->getIdentified(),
89             item->getDamaged(),
90             item->getFavorite(),
91             Equipm_false,
92             Equipped_false);
93         child->setTag(item->getTag());
94         child->setInvIndex(item->getInvIndex());
95         mChildItems.push_back(child);
96     }
97 }
98