summaryrefslogtreecommitdiffstats
path: root/pokemod/Move.cpp
blob: 6f8ad3231793f210e1988e7c886b9b77451db04b (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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/////////////////////////////////////////////////////////////////////////////
// Name:        pokemod/Move.cpp
// Purpose:     Define a move that can be learned
// Author:      Ben Boeckel
// Modified by: Ben Boeckel
// Created:     Sat May 26 2007 22:51:21
// Copyright:   ©2007-2008 Ben Boeckel and Nerdy Productions
// Licence:
//    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/>.
/////////////////////////////////////////////////////////////////////////////

#include <QDir>
#include <QListIterator>
#include <QMap>
#include <QMapIterator>
#include <QStringListIterator>

#include "Pokemod.h"
#include "MoveEffect.h"
#include "Move.h"

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

Move::Move(const Pokemod* par, const int _id) :
        Object("Move", par, _id),
        name(""),
        accuracy(1, 1),
        power(0),
        type(-1),
        special(false),
        powerPoints(0),
        target(-1),
        targetChoice(-1),
        ignoreAccuracy(false),
        canFlinch(false),
        canRandom(false),
        canSnatch(false),
        sound(false),
        description("")
{
}

Move::Move(const Pokemod* par, const Move& m, const int _id) :
        Object("Move", par, _id)
{
    *this = m;
}

Move::Move(const Pokemod* par, const QString& fname, const int _id) :
        Object("Move", par, _id)
{
    load(fname, _id);
}

Move::~Move()
{
    for (QListIterator<MoveEffect*> i(effects); i.hasNext(); )
        delete i.next();
}

bool Move::validate() const
{
    bool valid = true;
    pokemod->validationMsg(QString("---Move \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg);
    if (name == "")
    {
        pokemod->validationMsg("Name is not defined");
        valid = "";
    }
    if (pokemod->getTypeIndex(type) == -1)
    {
        pokemod->validationMsg("Invalid type");
        valid = false;
    }
    if (!powerPoints)
    {
        pokemod->validationMsg("Invalid number of power points");
        valid = false;
    }
    if (T_End <= target)
    {
        pokemod->validationMsg("Invalid target");
        valid = false;
    }
    if (!target || (pokemod->getRules()->getMaxFight() << (target == T_Both)) < numTargets)
    {
        pokemod->validationMsg("Invalid number of targets");
        valid = false;
    }
    if (C_End <= targetChoice)
    {
        pokemod->validationMsg("Invalid target choice");
        valid = false;
    }
    if (getEffectCount())
    {
        QMap<int, int> idChecker;
        for (QListIterator<MoveEffect*> i(effects); i.hasNext(); i.next())
        {
            if (!i.peekNext()->isValid())
                valid = false;
            ++idChecker[i.peekNext()->getId()];
        }
        for (QMapIterator<int, int> i(idChecker); i.hasNext(); i.next())
        {
            if (1 < i.value())
            {
                pokemod->validationMsg(QString("There are %1 effects with id %2").arg(i.value()).arg(i.key()));
                valid = false;
            }
        }
    }
    else
    {
        pokemod->validationMsg("There are no effects");
        valid = false;
    }
    return valid;
}

int Move::getNewId() const
{
    int i = 0;
    for (; (i < getEffectCount()) && (getEffectIndex(i) != -1); ++i)
        ;
    return i;
}

void Move::load(const QString& fname, const int _id) throw(Exception)
{
    Ini ini(fname);
    if (_id == -1)
        ini.getValue("id", id);
    else
        id = _id;
    int i;
    int j;
    ini.getValue("name", name);
    ini.getValue("accuracy-n", i, 1);
    ini.getValue("accuracy-d", j, 1);
    accuracy.set(i, j);
    ini.getValue("power", power, 0);
    ini.getValue("type", type);
    ini.getValue("special", special, false);
    ini.getValue("powerPoints", powerPoints, 1);
    ini.getValue("target", target);
    ini.getValue("numTargets", numTargets, 0);
    ini.getValue("targetChoice", targetChoice);
    ini.getValue("ignoreAccuracy", ignoreAccuracy, false);
    ini.getValue("canFlinch", canFlinch, false);
    ini.getValue("canRandom", canRandom, false);
    ini.getValue("canSnatch", canSnatch, false);
    ini.getValue("sound", sound, false);
    ini.getValue("description", description, "");
    QStringList path = pokemod->getPath().split('/');
    path.removeLast();
    QDir fdir(path.join("/"));
    effects.clear();
    if (fdir.cd("effect"))
    {
        for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); )
            newEffect(i.next());
    }
}

void Move::save() const throw(Exception)
{
    Ini ini;
    ini.addField("id", id);
    ini.addField("name", name);
    ini.addField("accuracy-n", accuracy.getNum());
    ini.addField("accuracy-d", accuracy.getDenom());
    ini.addField("power", power);
    ini.addField("type", type);
    ini.addField("special", special);
    ini.addField("powerPoints", powerPoints);
    ini.addField("target", target);
    ini.addField("numTargets", numTargets);
    ini.addField("targetChoice", targetChoice);
    ini.addField("ignoreAccuracy", ignoreAccuracy);
    ini.addField("canFlinch", canFlinch);
    ini.addField("canRandom", canRandom);
    ini.addField("canSnatch", canSnatch);
    ini.addField("sound", sound);
    ini.addField("description", description);
    ini.save(QString("%1/move/%2/data.pini").arg(pokemod->getPath()).arg(name));
    for (QListIterator<MoveEffect*> i(effects); i.hasNext(); )
        i.next()->save(name);
}

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

void Move::setAccuracy(const int n, const int d) throw(Exception)
{
    accuracy.set(n, d);
}

void Move::setAccuracyNum(const int n) throw(Exception)
{
    accuracy.setNum(n);
}

void Move::setAccuracyDenom(const int d) throw(Exception)
{
    accuracy.setDenom(d);
}

void Move::setType(const int t) throw(BoundsException)
{
    if (pokemod->getTypeIndex(t) == -1)
        throw(BoundsException(className, "type"));
    type = t;
}

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

void Move::setPowerPoints(const int p) throw(BoundsException)
{
    if (!p)
        throw(BoundsException(className, "powerPoints"));
    powerPoints = p;
}

void Move::setTarget(const int t) throw(BoundsException)
{
    if (T_End <= t)
        throw(BoundsException(className, "target"));
    target = t;
}

void Move::setNumTargets(const int n) throw(BoundsException)
{
    if (!n || ((pokemod->getRules()->getMaxFight() << 1) < n))
        throw(BoundsException(className, "numTargets"));
    numTargets = n;
}

void Move::setTargetChoice(const int t) throw(BoundsException)
{
    if (C_End <= t)
        throw(BoundsException(className, "targetChoice"));
    targetChoice = t;
}

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

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

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

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

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

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

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

Frac Move::getAccuracy() const
{
    return accuracy;
}

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

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

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

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

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

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

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

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

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

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

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

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

const MoveEffect* Move::getEffect(const int i) const throw(IndexException)
{
    if (getEffectCount() <= i)
        throw(IndexException(className));
    return effects.at(i);
}

MoveEffect* Move::getEffect(const int i) throw(IndexException)
{
    if (getEffectCount() <= i)
        throw(IndexException(className));
    return effects[i];
}

const MoveEffect* Move::getEffectByID(const int i) const throw(IndexException)
{
    return getEffect(getEffectIndex(i));
}

MoveEffect* Move::getEffectByID(const int i) throw(IndexException)
{
    return getEffect(getEffectIndex(i));
}

int Move::getEffectIndex(const int _id) const
{
    for (int i = 0; i < getEffectCount(); ++i)
    {
        if (effects[i]->getId() == _id)
            return i;
    }
    return -1;
}

int Move::getEffectCount() const
{
    return effects.size();
}

MoveEffect* Move::newEffect()
{
    effects.append(new MoveEffect(pokemod, getNewId()));
    return effects[getEffectCount() - 1];
}

MoveEffect* Move::newEffect(const QString& fname)
{
    effects.append(new MoveEffect(pokemod, fname, getNewId()));
    return effects[getEffectCount() - 1];
}

MoveEffect* Move::newEffect(const MoveEffect& e)
{
    effects.append(new MoveEffect(pokemod, e, getNewId()));
    return effects[getEffectCount() - 1];
}

void Move::deleteEffect(const int i) throw(IndexException)
{
    if (getEffectCount() <= i)
        throw(IndexException(className));
    delete effects[i];
    effects.removeAt(i);
}

Move& Move::operator=(const Move& rhs)
{
    if (this == &rhs)
        return *this;
    name = rhs.name;
    accuracy = rhs.accuracy;
    power = rhs.power;
    type = rhs.type;
    special = rhs.special;
    powerPoints = rhs.powerPoints;
    target = rhs.target;
    numTargets = rhs.numTargets;
    targetChoice = rhs.targetChoice;
    ignoreAccuracy = rhs.ignoreAccuracy;
    canFlinch = rhs.canFlinch;
    canRandom = rhs.canRandom;
    canSnatch = rhs.canSnatch;
    sound = rhs.sound;
    description = rhs.description;
    effects.clear();
    for (int i = 0; i < rhs.getEffectCount(); ++i)
        effects.append(new MoveEffect(pokemod, *rhs.getEffect(i), rhs.getEffect(i)->getId()));
    return *this;
}