summaryrefslogtreecommitdiffstats
path: root/pokemod/MapTrainer.cpp
blob: ae9e5585131484c2a8d943a799a2111fe8b874c0 (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
/////////////////////////////////////////////////////////////////////////////
// Name:        pokemod/MapTrainer.cpp
// Purpose:     Define a trainer for a map
// Author:      Ben Boeckel
// Modified by: Ben Boeckel
// Created:     Fri June 1 2007 23:11:28
// 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 <QStringList>
#include <QStringListIterator>

#include "Pokemod.h"
#include "Dialog.h"
#include "MapTrainerTeamMember.h"
#include "MapTrainer.h"

MapTrainer::MapTrainer(const Pokemod* par, const int _id) :
        Object("MapTrainer", par, _id),
        name(""),
        trainerClass(-1),
        coordinate(0, 0),
        sight(0),
        direction(-1),
        numFight(1),
        appearFlag(0, 0),
        dialog(-1),
        leadTeamMember(-1)
{
}

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

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

MapTrainer::~MapTrainer()
{
    for (QListIterator<MapTrainerTeamMember*> i(teamMembers); i.hasNext(); )
        delete i.next();
}

bool MapTrainer::validate() const
{
    bool valid = true;
    pokemod->validationMsg(QString("------Trainer \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg);
    if (name == "")
    {
        pokemod->validationMsg("Name is not defined");
        valid = false;
    }
    if (pokemod->getTrainerIndex(trainerClass) == -1)
    {
        pokemod->validationMsg("Invalid trainer class");
        valid = false;
    }
    if (Pokemod::D_End_None <= direction)
    {
        pokemod->validationMsg("Invalid direction");
        valid = false;
    }
    if (!numFight || (pokemod->getRules()->getMaxFight() < numFight))
    {
        pokemod->validationMsg("Invalid number of Pokémon for a fight");
        valid = false;
    }
    if (pokemod->getDialogIndex(dialog) == -1)
    {
        pokemod->validationMsg("Invalid dialog");
        valid = false;
    }
    if (getTeamMemberCount() <= leadTeamMember)
    {
        pokemod->validationMsg("Invalid lead member");
        valid = false;
    }
    if (getTeamMemberCount())
    {
        QMap<int, int> idChecker;
        for (QListIterator<MapTrainerTeamMember*> i(teamMembers); 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 team members with id %2").arg(i.value()).arg(i.key()));
                valid = false;
            }
        }
    }
    else
    {
        pokemod->validationMsg("There are no team members");
        valid = false;
    }
    return valid;
}

int MapTrainer::getNewId() const
{
    int i = 0;
    for (; i < getTeamMemberCount(); ++i)
    {
        if (getTeamMemberIndex(i) == -1)
            return i;
    }
    return -1;
}

void MapTrainer::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("trainerClass", trainerClass);
    ini.getValue("coordinate-x", i, 0);
    ini.getValue("coordinate-y", j, 0);
    coordinate.set(i, j);
    ini.getValue("sight", sight, 0);
    ini.getValue("direction", direction);
    ini.getValue("numFight", numFight, 1);
    ini.getValue("appearFlag-f", i, 0);
    ini.getValue("appearFlag-s", j, 0);
    appearFlag.set(i, j);
    ini.getValue("dialog", dialog);
    ini.getValue("leadTeamMember", leadTeamMember);
    QStringList path = pokemod->getPath().split('/');
    path.removeLast();
    QDir fdir(path.join("/"));
    teamMembers.clear();
    if (fdir.cd("team"))
    {
        for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); )
            newTeamMember(i.next());
    }
}

void MapTrainer::save(const QString& map) const throw(Exception)
{
    Ini ini;
    ini.addField("id", id);
    ini.addField("name", name);
    ini.addField("trainerClass", trainerClass);
    ini.addField("coordinate-x", coordinate.getX());
    ini.addField("coordinate-y", coordinate.getY());
    ini.addField("sight", sight);
    ini.addField("direction", direction);
    ini.addField("numFight", numFight);
    ini.addField("appearFlag-f", appearFlag.getFlag());
    ini.addField("appearFlag-s", appearFlag.getStatus());
    ini.addField("dialog", dialog);
    ini.addField("leadTeamMember", leadTeamMember);
    ini.save(QString("%1/map/%2/trainer/%3/data.pini").arg(pokemod->getPath()).arg(map).arg(name));
    for (QListIterator<MapTrainerTeamMember*> i(teamMembers); i.hasNext(); )
        i.next()->save(map, name);
}

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

void MapTrainer::setTrainerClass(const int t) throw(BoundsException)
{
    if (pokemod->getTrainerIndex(t) == -1)
        throw(BoundsException(className, "trainerClass"));
    trainerClass = t;
}

void MapTrainer::setCoordinate(const int x, const int y)
{
    coordinate.set(x, y);
}

void MapTrainer::setCoordinateX(const int x)
{
    coordinate.setX(x);
}

void MapTrainer::setCoordinateY(const int y)
{
    coordinate.setY(y);
}

void MapTrainer::setSight(const int s)
{
    sight = s;
}

void MapTrainer::setDirection(const int d) throw(BoundsException)
{
    if (Pokemod::D_End_None <= d)
        throw(BoundsException(className, "direction"));
    direction = d;
}

void MapTrainer::setNumFight(const int n) throw(BoundsException)
{
    if (!n || (pokemod->getRules()->getMaxFight() < n))
        throw(BoundsException(className, "numFight"));
    numFight = n;
}

void MapTrainer::setAppearFlag(const int f, const int s)
{
    appearFlag.set(f, s);
}

void MapTrainer::setAppearFlagFlag(const int f)
{
    appearFlag.setFlag(f);
}

void MapTrainer::setAppearFlagStatus(const int s)
{
    appearFlag.setStatus(s);
}

void MapTrainer::setDialog(const int d) throw(BoundsException)
{
    if (pokemod->getDialogIndex(d) == -1)
        throw(BoundsException(className, "dialog"));
    dialog = d;
}

void MapTrainer::setLeadTeamMember(const int l) throw(BoundsException)
{
    if (getTeamMemberCount() <= l)
        throw(BoundsException(className, "leadTeamMember"));
    leadTeamMember = l;
}

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

int MapTrainer::getTrainerClass() const
{
    return trainerClass;
}

Point MapTrainer::getCoordinate() const
{
    return coordinate;
}

int MapTrainer::getSight() const
{
    return sight;
}

int MapTrainer::getDirection() const
{
    return direction;
}

int MapTrainer::getNumFight() const
{
    return numFight;
}

Flag MapTrainer::getAppearFlag() const
{
    return appearFlag;
}

int MapTrainer::getDialog() const
{
    return dialog;
}

int MapTrainer::getLeadTeamMember() const
{
    return leadTeamMember;
}

const MapTrainerTeamMember* MapTrainer::getTeamMember(const int i) const throw(IndexException)
{
    if (getTeamMemberCount() <= i)
        throw(IndexException(className));
    return teamMembers.at(i);
}

MapTrainerTeamMember* MapTrainer::getTeamMember(const int i) throw(IndexException)
{
    if (getTeamMemberCount() <= i)
        throw(IndexException(className));
    return teamMembers[i];
}

const MapTrainerTeamMember* MapTrainer::getTeamMemberByID(const int i) const throw(IndexException)
{
    return getTeamMember(getTeamMemberIndex(i));
}

MapTrainerTeamMember* MapTrainer::getTeamMemberByID(const int i) throw(IndexException)
{
    return getTeamMember(getTeamMemberIndex(i));
}

int MapTrainer::getTeamMemberIndex(const int _id) const
{
    for (int i = 0; i < getTeamMemberCount(); ++i)
    {
        if (teamMembers[i]->getId() == _id)
            return i;
    }
    return -1;
}

int MapTrainer::getTeamMemberCount() const
{
    return teamMembers.size();
}

MapTrainerTeamMember* MapTrainer::newTeamMember()
{
    teamMembers.append(new MapTrainerTeamMember(pokemod, getNewId()));
    return teamMembers[getTeamMemberCount() - 1];
}

MapTrainerTeamMember* MapTrainer::newTeamMember(const QString& fname)
{
    teamMembers.append(new MapTrainerTeamMember(pokemod, fname, getNewId()));
    return teamMembers[getTeamMemberCount() - 1];
}

MapTrainerTeamMember* MapTrainer::newTeamMember(const MapTrainerTeamMember& p)
{
    teamMembers.append(new MapTrainerTeamMember(pokemod, p, getNewId()));
    return teamMembers[getTeamMemberCount() - 1];
}

void MapTrainer::deleteTeamMember(const int i) throw(IndexException)
{
    if (getTeamMemberCount() <= i)
        throw(IndexException(className));
    delete teamMembers[i];
    teamMembers.removeAt(i);
}

MapTrainer& MapTrainer::operator=(const MapTrainer& rhs)
{
    if (this == &rhs)
        return *this;
    name = rhs.name;
    trainerClass = rhs.trainerClass;
    coordinate = rhs.coordinate;
    sight = rhs.sight;
    direction = rhs.direction;
    numFight = rhs.numFight;
    appearFlag = rhs.appearFlag;
    dialog = rhs.dialog;
    leadTeamMember = rhs.leadTeamMember;
    teamMembers.clear();
    for (int i = 0; i < rhs.getTeamMemberCount(); ++i)
        teamMembers.append(new MapTrainerTeamMember(pokemod, *rhs.getTeamMember(i), rhs.getTeamMember(i)->getId()));
    return *this;
}