summaryrefslogtreecommitdiffstats
path: root/pokemod/Move.cpp
blob: 723fd5af26520c3f9d658a3bdb6c62885d38959b (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * 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 "Move.h"

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

// Qt includes
#include <QSet>

const QStringList Move::TargetStr = QStringList() << "Player" << "Enemy" << "All" << "Random";
const QStringList Move::ChoiceStr = QStringList() << "Player" << "Enemy" << "Random";

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

Move::Move(const Pokemod* parent, const int id) :
        Object("Move", parent, id),
        m_name(""),
        m_accuracy(1, 1),
        m_power(0),
        m_type(INT_MAX),
        m_special(false),
        m_powerPoints(0),
        m_target(INT_MAX),
        m_targetChoice(INT_MAX),
        m_ignoreAccuracy(false),
        m_canFlinch(false),
        m_canRandom(false),
        m_canSnatch(false),
        m_sound(false),
        m_priority(1),
        m_description("")
{
}

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

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

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

void Move::validate()
{
    if (m_name.isEmpty())
        emit(error("Name is empty"));
    TEST(setType, type);
    TEST(setPowerPoints, powerPoints);
    TEST(setTarget, target);
    TEST(setNumTargets, numTargets);
    TEST(setTargetChoice, targetChoice);
    if (!effectCount())
        emit(warning("There are no effects"));
    QSet<int> idChecker;
    foreach (MoveEffect* effect, m_effects)
    {
        effect->validate();
        if (idChecker.contains(effect->id()))
            emit(error(subclass("effect", effect->id())));
        idChecker.insert(effect->id());
    }
}

void Move::load(const QDomElement& xml, int id)
{
    LOAD_ID();
    LOAD(QString, name);
    LOAD(Fraction, accuracy);
    LOAD(int, power);
    LOAD(int, type);
    LOAD(bool, special);
    LOAD(int, powerPoints);
    LOAD(int, target);
    LOAD(int, targetChoice);
    LOAD(bool, ignoreAccuracy);
    LOAD(bool, canFlinch);
    LOAD(bool, canRandom);
    LOAD(bool, canSnatch);
    LOAD(bool, sound);
    LOAD(QString, description);
    LOAD_SUB(newEffect, MoveEffect);
}

QDomElement Move::save() const
{
    SAVE_CREATE();
    SAVE(QString, name);
    SAVE(Fraction, accuracy);
    SAVE(int, power);
    SAVE(int, type);
    SAVE(bool, special);
    SAVE(int, powerPoints);
    SAVE(int, target);
    SAVE(int, numTargets);
    SAVE(int, targetChoice);
    SAVE(bool, ignoreAccuracy);
    SAVE(bool, canFlinch);
    SAVE(bool, canRandom);
    SAVE(bool, canSnatch);
    SAVE(bool, sound);
    SAVE(QString, description);
    SAVE_SUB(MoveEffect, effects);
    return xml;
}

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

void Move::setAccuracy(const Fraction& accuracy)
{
    if (1 < accuracy)
    {
        emit(error(bounds("accuracy")));
        return;
    }
    m_accuracy = accuracy;
}

void Move::setPower(const int power)
{
    m_power = power;
}

void Move::setType(const int type)
{
    if (static_cast<const Pokemod*>(pokemod())->typeIndex(type) == INT_MAX)
    {
        emit(error(bounds("type")));
        return;
    }
    m_type = type;
}

void Move::setSpecial(const bool special)
{
    m_special = special;
}

void Move::setPowerPoints(const int powerPoints)
{
    if (!powerPoints)
    {
        emit(error(bounds("powerPoints")));
        return;
    }
    m_powerPoints = powerPoints;
}

void Move::setTarget(const int target)
{
    if (T_End <= target)
    {
        emit(error(bounds("target")));
        return;
    }
    m_target = target;
}

void Move::setNumTargets(const int numTargets)
{
    if (!numTargets || ((static_cast<const Pokemod*>(pokemod())->rules()->maxFight() * ((m_target == T_All) ? static_cast<const Pokemod*>(pokemod())->rules()->maxPlayers() : 1)) < numTargets))
    {
        emit(error(bounds("numTargets")));
        return;
    }
    m_numTargets = numTargets;
}

void Move::setTargetChoice(const int targetChoice)
{
    if (C_End <= targetChoice)
    {
        emit(error(bounds("targetChoice")));
        return;
    }
    m_targetChoice = targetChoice;
}

void Move::setIgnoreAccuracy(const bool ignoreAccuracy)
{
    m_ignoreAccuracy = ignoreAccuracy;
}

void Move::setCanFlinch(const bool canFlinch)
{
    m_canFlinch = canFlinch;
}

void Move::setCanRandom(const bool canRandom)
{
    m_canRandom = canRandom;
}

void Move::setCanSnatch(const bool canSnatch)
{
    m_canSnatch = canSnatch;
}

void Move::setSound(const bool sound)
{
    m_sound = sound;
}

void Move::setDescription(const QString& description)
{
    m_description = description;
}

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

Fraction Move::accuracy() const
{
    return m_accuracy;
}

int Move::power() const
{
    return m_power;
}

int Move::type() const
{
    return m_type;
}

bool Move::special() const
{
    return m_special;
}

int Move::powerPoints() const
{
    return m_powerPoints;
}

int Move::target() const
{
    return m_target;
}

int Move::numTargets() const
{
    return m_numTargets;
}

int Move::targetChoice() const
{
    return m_targetChoice;
}

bool Move::ignoreAccuracy() const
{
    return m_ignoreAccuracy;
}

bool Move::canFlinch() const
{
    return m_canFlinch;
}

bool Move::canRandom() const
{
    return m_canRandom;
}

bool Move::canSnatch() const
{
    return m_canSnatch;
}

bool Move::sound() const
{
    return m_sound;
}

QString Move::description() const
{
    return m_description;
}

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

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

const MoveEffect* Move::effectById(const int id) const
{
    return effect(effectIndex(id));
}

MoveEffect* Move::effectById(const int id)
{
    return effect(effectIndex(id));
}

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

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

MoveEffect* Move::newEffect()
{
    return newEffect(new MoveEffect(this, newEffectId()));
}

MoveEffect* Move::newEffect(const QDomElement& xml)
{
    return newEffect(new MoveEffect(xml, this, newEffectId()));
}

MoveEffect* Move::newEffect(const MoveEffect& effect)
{
    return newEffect(new MoveEffect(effect, this, newEffectId()));
}

MoveEffect* Move::newEffect(MoveEffect* effect)
{
    m_effects.append(effect);
    return effect;
}

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

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

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

Move& Move::operator=(const Move& rhs)
{
    if (this == &rhs)
        return *this;
    clear();
    COPY(name);
    COPY(accuracy);
    COPY(power);
    COPY(type);
    COPY(special);
    COPY(powerPoints);
    COPY(target);
    COPY(numTargets);
    COPY(targetChoice);
    COPY(ignoreAccuracy);
    COPY(canFlinch);
    COPY(canRandom);
    COPY(canSnatch);
    COPY(sound);
    COPY(description);
    COPY_SUB(MoveEffect, effects);
    return *this;
}

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