summaryrefslogtreecommitdiffstats
path: root/sigmod/MapTrainerTeamMember.cpp
blob: 3a72811e029c21c2c3b4603dafbe3ac8aebc5dee (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * 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 "MapTrainerTeamMember.h"

// Sigmod includes
#include "Item.h"
#include "Macros.h"
#include "MapTrainer.h"
#include "Rules.h"
#include "Sigmod.h"
#include "Species.h"
#include "SpeciesMove.h"

// Qt includes
#include <QtCore/QSet>

Sigmod::MapTrainerTeamMember::MapTrainerTeamMember(const MapTrainerTeamMember& teamMember) :
        Object(teamMember.parent(), teamMember.id())
{
    *this = teamMember;
}

Sigmod::MapTrainerTeamMember::MapTrainerTeamMember(const MapTrainer* parent, const int id) :
        Object(parent, id),
        m_species(INT_MAX),
        m_level(INT_MAX)
{
}

Sigmod::MapTrainerTeamMember::MapTrainerTeamMember(const MapTrainerTeamMember& teamMember, const MapTrainer* parent, const int id) :
        Object(parent, id)
{
    *this = teamMember;
}

Sigmod::MapTrainerTeamMember::MapTrainerTeamMember(const QDomElement& xml, const MapTrainer* parent, const int id) :
        Object(parent, id)
{
    LOAD_ID();
    load(xml);
}

void Sigmod::MapTrainerTeamMember::validate()
{
    TEST_BEGIN();
    TEST(setSpecies, species);
    TEST(setLevel, level);
    if (sigmod()->rules()->maxAbilities() < m_ability.size())
        emit(error("Too many abilities"));
    TEST_LIST(setAbility, ability);
    if (sigmod()->rules()->maxHeldItems() < m_item.size())
        emit(error("Too many held items"));
    TEST_LIST(setItem, item);
    if (sigmod()->rules()->maxMoves() < m_move.size())
        emit(error("Too many moves"));
    TEST_LIST(setMove, move);
    if (sigmod()->rules()->maxNatures() < m_nature.size())
        emit(error("Too many natures"));
    TEST_LIST(setNature, nature);
    TEST_END();
}

void Sigmod::MapTrainerTeamMember::load(const QDomElement& xml)
{
    LOAD_BEGIN();
    LOAD(species);
    LOAD(level);
    LOAD_LIST(ability);
    LOAD_LIST(item);
    LOAD_LIST(move);
    LOAD_LIST(nature);
}

QDomElement Sigmod::MapTrainerTeamMember::save() const
{
    SAVE_CREATE();
    SAVE(species);
    SAVE(level);
    SAVE_LIST(ability);
    SAVE_LIST(item);
    SAVE_LIST(move);
    SAVE_LIST(nature);
    return xml;
}

void Sigmod::MapTrainerTeamMember::setSpecies(const int species)
{
    if (!sigmod()->speciesById(species))
        emit(error(bounds("species")));
    else
        CHECK(species);
}

void Sigmod::MapTrainerTeamMember::setLevel(const int level)
{
    if (sigmod()->rules()->maxLevel() < level)
        emit(error(bounds("level")));
    else
        CHECK(level);
}

void Sigmod::MapTrainerTeamMember::setAbility(const int ability, const bool state)
{
    if (!sigmod()->abilityById(ability))
        emit(error(bounds("ability")));
    else if (state && !m_ability.contains(ability))
    {
        if (m_ability.size() < sigmod()->rules()->maxAbilities())
        {
            m_ability.append(ability);
            emit(changed());
        }
        else
            emit(error("Cannot have any more abilities"));
    }
    else if (m_ability.contains(ability))
    {
        m_ability.removeAll(ability);
        emit(changed());
    }
}

void Sigmod::MapTrainerTeamMember::setItem(const int item, const bool state)
{
    if (!sigmod()->itemById(item))
        emit(error(bounds("item")));
    else if (state && !m_item.contains(item))
    {
        if (m_item.size() < sigmod()->rules()->maxHeldItems())
        {
            if (checkWeight(item))
            {
                m_item.append(item);
                emit(changed());
            }
            else
                emit(error("Cannot carry that much weight"));
        }
        else
            emit(error("Cannot hold any more items"));
    }
    else if (m_item.contains(item))
    {
        m_item.removeAll(item);
        emit(changed());
    }
}

void Sigmod::MapTrainerTeamMember::setMove(const int move, const bool state)
{
    if (!sigmod()->moveById(move))
        emit(error(bounds("move")));
    else if (state && !m_move.contains(move))
    {
        const Species* species = sigmod()->speciesById(move);
        for (int i = 0; i < species->moveCount(); ++i)
        {
            const SpeciesMove* speciesMove = species->move(i);
            if (speciesMove->move() == move)
            {
                if (m_move.size() < sigmod()->rules()->maxMoves())
                {
                    m_move.append(move);
                    emit(changed());
                    return;
                }
                else
                    emit(error("Cannot know any more moves"));
            }
        }
        emit(error("Cannot learn the move"));
    }
    else if (m_move.contains(move))
    {
        m_move.removeAll(move);
        emit(changed());
    }
}

void Sigmod::MapTrainerTeamMember::setNature(const int nature, const bool state)
{
    if (!sigmod()->natureById(nature))
        emit(error(bounds("nature")));
    else if (state && !m_nature.contains(nature))
    {
        if (m_nature.size() < sigmod()->rules()->maxNatures())
        {
            m_nature.append(nature);
            emit(changed());
        }
        else
            emit(error("Cannot have any more natures"));
    }
    else if (m_nature.contains(nature))
    {
        m_nature.removeAll(nature);
        emit(changed());
    }
}

int Sigmod::MapTrainerTeamMember::species() const
{
    return m_species;
}

int Sigmod::MapTrainerTeamMember::level() const
{
    return m_level;
}

bool Sigmod::MapTrainerTeamMember::ability(const int ability) const
{
    return m_ability.contains(ability);
}

QList<int> Sigmod::MapTrainerTeamMember::abilities() const
{
    return m_ability;
}

bool Sigmod::MapTrainerTeamMember::item(const int item) const
{
    return m_item.contains(item);
}

QList<int> Sigmod::MapTrainerTeamMember::items() const
{
    return m_item;
}

bool Sigmod::MapTrainerTeamMember::move(const int move) const
{
    return m_move.contains(move);
}

QList<int> Sigmod::MapTrainerTeamMember::moves() const
{
    return m_move;
}

bool Sigmod::MapTrainerTeamMember::nature(const int nature) const
{
    return m_nature.contains(nature);
}

QList<int> Sigmod::MapTrainerTeamMember::natures() const
{
    return m_nature;
}

Sigmod::MapTrainerTeamMember& Sigmod::MapTrainerTeamMember::operator=(const MapTrainerTeamMember& rhs)
{
    if (this == &rhs)
        return *this;
    COPY(species);
    COPY(level);
    COPY(ability);
    COPY(item);
    COPY(move);
    COPY(nature);
    return *this;
}

bool Sigmod::MapTrainerTeamMember::checkWeight(const int item)
{
    const Species* species = sigmod()->speciesById(m_species);
    if (!species)
        return true;
    int totalWeight = sigmod()->itemById(item)->weight();
    foreach (int itemId, m_item)
    {
        const Item* item = sigmod()->itemById(itemId);
        if (!item)
            totalWeight += item->weight();
    }
    return (totalWeight <= species->maxHoldWeight());
}