GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/ea/equipbackend.h Lines: 6 31 19.4 %
Date: 2021-03-17 Branches: 1 22 4.5 %

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
#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
33
#include "gui/windows/inventorywindow.h"
34
35
namespace Ea
36
{
37
38
1
class EquipBackend final : public Equipment::Backend
39
{
40
    public:
41
        EquipBackend()
42
2
        {
43
2
            memset(mEquipment, -1, sizeof(mEquipment));
44
        }
45
46
        A_DELETE_COPY(EquipBackend)
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
60
77
        void clear() override final
61
        {
62
77
            Inventory *const inv = PlayerInfo::getInventory();
63
77
            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)
71
                        item->setEquipped(Equipped_false);
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)
91
                item->setEquipped(Equipped_false);
92
93
            // not checking index because it must be safe
94
            mEquipment[index] = inventoryIndex;
95
96
            item = inv->getItem(inventoryIndex);
97
            if (item != nullptr)
98
                item->setEquipped(Equipped_true);
99
100
            if (inventoryWindow != nullptr)
101
                inventoryWindow->updateButtons(nullptr);
102
        }
103
104
    private:
105
        int mEquipment[EQUIPMENT_SIZE];
106
};
107
108
}  // namespace Ea
109
110
#endif  // NET_EA_EQUIPBACKEND_H