ManaPlus
equipbackend.h
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 
24 #ifndef NET_EA_EQUIPBACKEND_H
25 #define NET_EA_EQUIPBACKEND_H
26 
27 #include "resources/item/item.h"
28 
29 #include "being/playerinfo.h"
30 
31 #include "const/equipment.h"
32 
34 
35 namespace Ea
36 {
37 
39 {
40  public:
42  {
43  memset(mEquipment, -1, sizeof(mEquipment));
44  }
45 
47 
48  Item *getEquipment(const int index) const override final A_WARN_UNUSED
49  {
50  int invyIndex = mEquipment[index];
51  if (invyIndex == -1)
52  return nullptr;
53 
54  const Inventory *const inv = PlayerInfo::getInventory();
55  if (inv != nullptr)
56  return inv->getItem(invyIndex);
57  return nullptr;
58  }
59 
61  {
62  Inventory *const inv = PlayerInfo::getInventory();
63  if (inv == nullptr)
64  return;
65  for (int i = 0; i < EQUIPMENT_SIZE; i++)
66  {
67  if (mEquipment[i] != -1)
68  {
69  Item* item = inv->getItem(i);
70  if (item != nullptr)
72  }
73 
74  mEquipment[i] = -1;
75  }
76  }
77 
78  void setEquipment(const int index, const int inventoryIndex)
79  {
80  Inventory *const inv = PlayerInfo::getInventory();
81  if (inv == nullptr)
82  return;
83 
84  if (index < 0 || index >= EQUIPMENT_SIZE)
85  return;
86 
87  // Unequip existing item
88  Item *item = inv->getItem(mEquipment[index]);
89 
90  if (item != nullptr)
92 
93  // not checking index because it must be safe
94  mEquipment[index] = inventoryIndex;
95 
96  item = inv->getItem(inventoryIndex);
97  if (item != nullptr)
99 
100  if (inventoryWindow != nullptr)
101  inventoryWindow->updateButtons(nullptr);
102  }
103 
104  private:
106 };
107 
108 } // namespace Ea
109 
110 #endif // NET_EA_EQUIPBACKEND_H
int mEquipment[EQUIPMENT_SIZE]
Definition: equipbackend.h:105
void setEquipment(const int index, const int inventoryIndex)
Definition: equipbackend.h:78
Item * getEquipment(const int index) const
Definition: equipbackend.h:48
void updateButtons(const Item *item)
Item * getItem(const int index) const
Definition: inventory.cpp:83
Definition: item.h:50
void setEquipped(const Equipped equipped)
Definition: item.h:123
static const int EQUIPMENT_SIZE
Definition: equipment.h:29
const bool Equipped_false
Definition: equipped.h:30
const bool Equipped_true
Definition: equipped.h:30
InventoryWindow * inventoryWindow
#define override
Definition: localconsts.h:47
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
Inventory * getInventory()
Definition: playerinfo.cpp:195