ManaPlus
complexinventory.cpp
Go to the documentation of this file.
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 
25 
26 #include "logger.h"
27 
28 #include "being/playerinfo.h"
29 
30 #include "utils/cast.h"
31 
33 
34 #include "debug.h"
35 
37  const int size1) :
38  Inventory(type, size1)
39 {
40 }
41 
43 {
44 }
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(),
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(),
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,
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 ++;
170  }
171 }
#define CAST_S32
Definition: cast.h:30
bool addVirtualItem(const Item *const item, int index, const int amount)
ComplexInventory(const InventoryTypeT type, const int size)
void 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)
void addChild(const Item *const item, const int amount)
Definition: complexitem.cpp:59
unsigned mSize
Definition: inventory.h:215
Item * getItem(const int index) const
Definition: inventory.cpp:83
int mUsed
Definition: inventory.h:217
void distributeSlotsChangedEvent()
Definition: inventory.cpp:312
int addItem(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)
Definition: inventory.cpp:115
Item ** mItems
Definition: inventory.h:216
Definition: item.h:50
uint8_t getRefine() const
Definition: item.h:141
int getTag() const
Definition: item.h:231
void setInvIndex(const int index)
Definition: item.h:159
int getQuantity() const
Definition: item.h:105
int getInvIndex() const
Definition: item.h:165
int getId() const
Definition: item.h:81
Favorite getFavorite() const
Definition: item.h:205
void setTag(const int tag)
Definition: item.h:228
ItemColor getColor() const
Definition: item.h:181
Identified getIdentified() const
Definition: item.h:193
ItemTypeT getType() const
Definition: item.h:225
Damaged getDamaged() const
Definition: item.h:199
void log(const char *const log_text,...)
Definition: logger.cpp:269
bool Damaged
Definition: damaged.h:30
const bool Equipm_false
Definition: equipm.h:30
bool Equipm
Definition: equipm.h:30
const bool Equipped_false
Definition: equipped.h:30
bool Equipped
Definition: equipped.h:30
bool Favorite
Definition: favorite.h:30
bool Identified
Definition: identified.h:30
InventoryType ::T InventoryTypeT
Definition: inventorytype.h:42
uint16_t ItemColor
Definition: itemcolor.h:30
ItemType ::T ItemTypeT
Definition: itemtype.h:43
Logger * logger
Definition: logger.cpp:89
bool isItemProtected(const int id)
Definition: playerinfo.cpp:515