summaryrefslogtreecommitdiffstats
path: root/pokemod/Ability.cpp
blob: 168dcdae1e43e3f0f5b6bbb081d5429bceaa971b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Header include
#include "Ability.h"

// Pokemod includes
#include "AbilityEffect.h"
#include "Pokemod.h"

// Qt includes
#include <QSet>

Ability::Ability(const Ability& ability) :
        Object("Ability", ability.parent(), ability.id())
{
    *this = ability;
}

Ability::Ability(const Pokemod* parent, const int id) :
        Object("Ability", parent, id),
        m_name("")
{
}

Ability::Ability(const Ability& ability, const Pokemod* parent, const int id) :
        Object("Ability", parent, id)
{
    *this = ability;
}

Ability::Ability(const QDomElement& xml, const Pokemod* parent, const int id) :
        Object("Ability", parent, id)
{
    load(xml, id);
}

Ability::~Ability()
{
    clear();
}

void Ability::validate()
{
    if (m_name.isEmpty())
        emit(error("Name is empty"));
    if (!effectCount())
        emit(error("No effects"));
    QSet<int> idChecker;
    foreach (AbilityEffect* effect, m_effects)
    {
        effect->validate();
        if (idChecker.contains(effect->id()))
            emit(error(subclass("effect", effect->id())));
        idChecker.insert(effect->id());
    }
}

void Ability::load(const QDomElement& xml, int id)
{
    LOAD_ID();
    LOAD(QString, name);
    LOAD_SUB(newEffect, AbilityEffect);
}

QDomElement Ability::save() const
{
    SAVE_CREATE();
    SAVE(QString, name);
    SAVE_SUB(AbilityEffect, effects);
    return xml;
}

void Ability::setName(const QString& name)
{
    m_name = name;
}

QString Ability::name() const
{
    return m_name;
}

const AbilityEffect* Ability::effect(const int index) const
{
    if (effectCount() <= index)
        return NULL;
    return m_effects.at(index);
}

AbilityEffect* Ability::effect(const int index)
{
    if (effectCount() <= index)
        return NULL;
    return m_effects[index];
}

const AbilityEffect* Ability::effectById(const int id) const
{
    return effect(effectIndex(id));
}

AbilityEffect* Ability::effectById(const int id)
{
    return effect(effectIndex(id));
}

int Ability::effectIndex(const int id) const
{
    for (int i = 0; i < effectCount(); ++i)
    {
        if (m_effects[id]->id() == id)
            return i;
    }
    return INT_MAX;
}

int Ability::effectCount() const
{
    return m_effects.size();
}

AbilityEffect* Ability::newEffect()
{
    return newEffect(new AbilityEffect(this, effectId()));
}

AbilityEffect* Ability::newEffect(const QDomElement& xml)
{
    return newEffect(new AbilityEffect(xml, this, effectId()));
}

AbilityEffect* Ability::newEffect(const AbilityEffect& effect)
{
    return newEffect(new AbilityEffect(effect, this, effectId()));
}

AbilityEffect* Ability::newEffect(AbilityEffect* effect)
{
    m_effects.append(effect);
    return effect;
}

void Ability::deleteEffect(const int index)
{
    if (effectCount() <= index)
        return;
    delete m_effects[index];
    m_effects.removeAt(index);
}

void Ability::deleteEffectById(const int id)
{
    deleteEffect(effectIndex(id));
}

int Ability::effectId() const
{
    int i = 0;
    while ((i < effectCount()) && (effectIndex(i) != INT_MAX))
        ++i;
    return i;
}

Ability& Ability::operator=(const Ability& rhs)
{
    if (this == &rhs)
        return *this;
    clear();
    COPY(name);
    COPY_SUB(AbilityEffect, effects);
    return *this;
}

void Ability::clear()
{
    while (effectCount())
        deleteEffect(0);
}