GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/skill/skillinfo.cpp Lines: 0 66 0.0 %
Date: 2021-03-17 Branches: 0 56 0.0 %

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
#include "resources/skill/skillinfo.h"
25
26
#include "being/playerinfo.h"
27
28
#include "gui/models/skillmodel.h"
29
30
#include "utils/foreach.h"
31
#include "utils/stringutils.h"
32
33
#include "resources/skill/skilldata.h"
34
#include "resources/skill/skilltypelist.h"
35
36
#include "debug.h"
37
38
SkillInfo::SkillInfo() :
39
    skillLevel(),
40
    skillEffect(),
41
    useButton(),
42
    errorText(),
43
    castingAction(),
44
    castingRideAction(),
45
    castingSkyAction(),
46
    castingWaterAction(),
47
    dataMap(),
48
    model(nullptr),
49
    tab(nullptr),
50
    data(nullptr),
51
    level(0),
52
    customSelectedLevel(0),
53
    customOffsetX(0),
54
    customOffsetY(0),
55
    skillLevelWidth(0),
56
    id(0),
57
    range(0),
58
    sp(0),
59
    duration(0),
60
    durationTime(0),
61
    cooldown(0),
62
    x(0),
63
    y(0),
64
    type(SkillType::Unknown),
65
    owner(SkillOwner::Player),
66
    customCastType(CastType::Default),
67
    modifiable(Modifiable_false),
68
    visible(Visible_false),
69
    alwaysVisible(Visible_false),
70
    useTextParameter(false)
71
{
72
    dataMap[0] = new SkillData;
73
    data = dataMap[0];
74
}
75
76
SkillInfo::~SkillInfo()
77
{
78
    FOR_EACH (SkillDataMapIter, it, dataMap)
79
        delete (*it).second;
80
    dataMap.clear();
81
}
82
83
void SkillInfo::update()
84
{
85
    const int baseLevel = PlayerInfo::getSkillLevel(id);
86
    if (modifiable == Modifiable_false && baseLevel == 0)
87
    {
88
        if (visible == Visible_true)
89
        {
90
            visible = Visible_false;
91
            if (model != nullptr)
92
                model->updateVisibilities();
93
        }
94
        return;
95
    }
96
97
    const bool updateVisibility = (visible == Visible_false);
98
    visible = Visible_true;
99
100
    if (baseLevel == 0)
101
    {
102
        skillLevel.clear();
103
    }
104
    else
105
    {
106
        if (customSelectedLevel == 0)
107
        {
108
            // TRANSLATORS: skill level
109
            skillLevel = strprintf(_("Lvl: %d"), baseLevel);
110
        }
111
        else
112
        {
113
            // TRANSLATORS: skill level
114
            skillLevel = strprintf(_("Lvl: %d / %d"),
115
                customSelectedLevel,
116
                baseLevel);
117
        }
118
    }
119
120
    // TRANSLATORS: skill type
121
    const char *const typeStr = _("Type: %s");
122
123
    if (type == SkillType::Unknown)
124
    {
125
        // TRANSLATORS: Skill type
126
        skillEffect = strprintf(typeStr, _("Unknown"));
127
    }
128
    else
129
    {
130
        skillEffect.clear();
131
        for (size_t f = 0; f < skillTypeListSize; f ++)
132
        {
133
            const SkillTypeEntry &item = skillTypeList[f];
134
            if ((item.type & type) != 0)
135
            {
136
                if (!skillEffect.empty())
137
                    skillEffect.append(", ");
138
                skillEffect.append(strprintf(typeStr, item.name));
139
            }
140
        }
141
    }
142
    if (skillEffect.empty())
143
    {
144
        // TRANSLATORS: Skill type
145
        skillEffect = strprintf(typeStr, _("Unknown:"));
146
        skillEffect.append(" ").append(toString(CAST_S32(type)));
147
    }
148
149
    if (sp != 0)
150
    {
151
        // TRANSLATORS: skill mana
152
        skillEffect.append(strprintf(_(" / Mana: -%d"), sp));
153
    }
154
155
    if (range > 0)
156
    {
157
        if (!skillEffect.empty())
158
            skillEffect.append(" / ");
159
        // TRANSLATORS: skill range
160
        skillEffect.append(strprintf(_("Range: %d"), range));
161
    }
162
163
    level = baseLevel;
164
    if (customSelectedLevel > level)
165
        customSelectedLevel = level;
166
167
    skillLevelWidth = -1;
168
169
    if (updateVisibility && (model != nullptr))
170
        model->updateVisibilities();
171
172
    data = getData(level);
173
    if (data == nullptr)
174
        data = dataMap[0];
175
}
176
177
178
void SkillInfo::addData(const int level1, SkillData *const data1)
179
{
180
    dataMap[level1] = data1;
181
}
182
183
SkillData *SkillInfo::getData(const int level1) const
184
{
185
    const SkillDataMapCIter it = dataMap.find(level1);
186
    if (it == dataMap.end())
187
        return nullptr;
188
    return (*it).second;
189
}
190
191
SkillData *SkillInfo::getData1(const int lev) const
192
{
193
    const SkillDataMapCIter it = dataMap.find(lev);
194
    if (it == dataMap.end())
195
        return (*dataMap.begin()).second;
196
    return (*it).second;
197
}
198
199
std::string SkillInfo::toDataStr() const
200
{
201
    return strprintf("%d %d %d",
202
        CAST_S32(customCastType),
203
        customOffsetX,
204
        customOffsetY);
205
}