GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/ea/equipbackend.h Lines: 6 31 19.4 %
Date: 2017-11-29 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-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#ifndef NET_EA_EQUIPBACKEND_H
24
#define NET_EA_EQUIPBACKEND_H
25
26
#include "resources/item/item.h"
27
28
#include "being/playerinfo.h"
29
30
#include "const/equipment.h"
31
32
#include "gui/windows/inventorywindow.h"
33
34
namespace Ea
35
{
36
37
2
class EquipBackend final : public Equipment::Backend
38
{
39
    public:
40
        EquipBackend()
41
4
        {
42
4
            memset(mEquipment, -1, sizeof(mEquipment));
43
        }
44
45
        A_DELETE_COPY(EquipBackend)
46
47
        Item *getEquipment(const int index) const override final A_WARN_UNUSED
48
        {
49
            int invyIndex = mEquipment[index];
50
            if (invyIndex == -1)
51
                return nullptr;
52
53
            const Inventory *const inv = PlayerInfo::getInventory();
54
            if (inv != nullptr)
55
                return inv->getItem(invyIndex);
56
            return nullptr;
57
        }
58
59
152
        void clear() override final
60
        {
61
152
            Inventory *const inv = PlayerInfo::getInventory();
62
152
            if (inv == nullptr)
63
                return;
64
            for (int i = 0; i < EQUIPMENT_SIZE; i++)
65
            {
66
                if (mEquipment[i] != -1)
67
                {
68
                    Item* item = inv->getItem(i);
69
                    if (item != nullptr)
70
                        item->setEquipped(Equipped_false);
71
                }
72
73
                mEquipment[i] = -1;
74
            }
75
        }
76
77
        void setEquipment(const int index, const int inventoryIndex)
78
        {
79
            Inventory *const inv = PlayerInfo::getInventory();
80
            if (inv == nullptr)
81
                return;
82
83
            if (index < 0 || index >= EQUIPMENT_SIZE)
84
                return;
85
86
            // Unequip existing item
87
            Item *item = inv->getItem(mEquipment[index]);
88
89
            if (item != nullptr)
90
                item->setEquipped(Equipped_false);
91
92
            // not checking index because it must be safe
93
            mEquipment[index] = inventoryIndex;
94
95
            item = inv->getItem(inventoryIndex);
96
            if (item != nullptr)
97
                item->setEquipped(Equipped_true);
98
99
            if (inventoryWindow != nullptr)
100
                inventoryWindow->updateButtons();
101
        }
102
103
    private:
104
        int mEquipment[EQUIPMENT_SIZE];
105
};
106
107
}  // namespace Ea
108
109
#endif  // NET_EA_EQUIPBACKEND_H