GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/beingequipbackend.cpp Lines: 1 28 3.6 %
Date: 2021-03-17 Branches: 0 28 0.0 %

Line Branch Exec Source
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 "beingequipbackend.h"
23
24
#include "being/being.h"
25
26
#include "net/inventoryhandler.h"
27
28
#include "resources/item/item.h"
29
30
#include "utils/delete2.h"
31
32
#include "debug.h"
33
34
BeingEquipBackend::BeingEquipBackend(Being *const being) :
35
    Equipment::Backend(),
36
    mEquipment()
37
{
38
    memset(mEquipment, 0, sizeof(mEquipment));
39
    if (being != nullptr)
40
    {
41
        const size_t sz = being->mSprites.size();
42
43
        for (size_t f = 0; f < sz; f ++)
44
        {
45
            const int idx = inventoryHandler->
46
                convertFromServerSlot(CAST_S32(f));
47
            const BeingSlot &slot = being->mSlots[f];
48
            const int id = slot.spriteId;
49
            if (id > 0 && idx >= 0 && idx < EQUIPMENT_SIZE)
50
            {
51
                Item *const item = new Item(id,
52
                    ItemType::Unknown,
53
                    1,
54
                    0,
55
                    slot.colorId,
56
                    Identified_true,
57
                    Damaged_false,
58
                    Favorite_false,
59
                    Equipm_true,
60
                    Equipped_true);
61
                const CardsList &cards = slot.cardsId;
62
                if (!cards.isEmpty())
63
                    item->setCards(&cards.cards[0], maxCards);
64
                mEquipment[idx] = item;
65
            }
66
        }
67
    }
68
}
69
70
BeingEquipBackend::~BeingEquipBackend()
71
{
72
    for (int i = 0; i < EQUIPMENT_SIZE; i++)
73
        delete2(mEquipment[i])
74
}
75
76
void BeingEquipBackend::clear()
77
{
78
    for (int i = 0; i < EQUIPMENT_SIZE; i++)
79
        delete2(mEquipment[i])
80
}
81
82
void BeingEquipBackend::setEquipment(const int index,
83
                                     const Item *const item)
84
{
85
    mEquipment[index] = item;
86
}
87
88
const Item *BeingEquipBackend::getEquipment(const int index) const
89
{
90
    if (index < 0 || index >= EQUIPMENT_SIZE)
91
        return nullptr;
92
    return mEquipment[index];
93
2
}