GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/beingequipbackend.cpp Lines: 1 28 3.6 %
Date: 2017-11-29 Branches: 0 26 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "beingequipbackend.h"
22
23
#include "being/being.h"
24
25
#include "net/inventoryhandler.h"
26
27
#include "resources/item/item.h"
28
29
#include "utils/delete2.h"
30
31
#include "debug.h"
32
33
BeingEquipBackend::BeingEquipBackend(Being *const being) :
34
    Equipment::Backend(),
35
    mEquipment()
36
{
37
    memset(mEquipment, 0, sizeof(mEquipment));
38
    if (being != nullptr)
39
    {
40
        const size_t sz = being->mSprites.size();
41
42
        for (size_t f = 0; f < sz; f ++)
43
        {
44
            const int idx = inventoryHandler->
45
                convertFromServerSlot(CAST_S32(f));
46
            const BeingSlot &slot = being->mSlots[f];
47
            const int id = slot.spriteId;
48
            if (id > 0 && idx >= 0 && idx < EQUIPMENT_SIZE)
49
            {
50
                Item *const item = new Item(id,
51
                    ItemType::Unknown,
52
                    1,
53
                    0,
54
                    slot.colorId,
55
                    Identified_true,
56
                    Damaged_false,
57
                    Favorite_false,
58
                    Equipm_true,
59
                    Equipped_true);
60
                const CardsList &cards = slot.cardsId;
61
                if (!cards.isEmpty())
62
                    item->setCards(&cards.cards[0], maxCards);
63
                mEquipment[idx] = item;
64
            }
65
        }
66
    }
67
}
68
69
BeingEquipBackend::~BeingEquipBackend()
70
{
71
    for (int i = 0; i < EQUIPMENT_SIZE; i++)
72
        delete2(mEquipment[i])
73
}
74
75
void BeingEquipBackend::clear()
76
{
77
    for (int i = 0; i < EQUIPMENT_SIZE; i++)
78
        delete2(mEquipment[i])
79
}
80
81
void BeingEquipBackend::setEquipment(const int index,
82
                                     const Item *const item)
83
{
84
    mEquipment[index] = item;
85
}
86
87
const Item *BeingEquipBackend::getEquipment(const int index) const
88
{
89
    if (index < 0 || index >= EQUIPMENT_SIZE)
90
        return nullptr;
91
    return mEquipment[index];
92
4
}