summaryrefslogtreecommitdiffstats
path: root/pokemod/MapTrainer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'pokemod/MapTrainer.cpp')
-rw-r--r--pokemod/MapTrainer.cpp414
1 files changed, 193 insertions, 221 deletions
diff --git a/pokemod/MapTrainer.cpp b/pokemod/MapTrainer.cpp
index ae9e5585..dbcae301 100644
--- a/pokemod/MapTrainer.cpp
+++ b/pokemod/MapTrainer.cpp
@@ -1,393 +1,365 @@
-/////////////////////////////////////////////////////////////////////////////
-// 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/>.
-/////////////////////////////////////////////////////////////////////////////
-
+/*
+ * 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/>.
+ */
+
+// Qt includes
#include <QDir>
-#include <QListIterator>
#include <QMap>
-#include <QMapIterator>
#include <QStringList>
-#include <QStringListIterator>
+// Pokemod includes
#include "Pokemod.h"
#include "Dialog.h"
#include "MapTrainerTeamMember.h"
+
+// Header include
#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* pokemod, const int id) :
+ Object("MapTrainer", pokemod, id),
+ m_name(""),
+ m_trainerClass(INT_MAX),
+ m_coordinate(0, 0),
+ m_sight(0),
+ m_direction(INT_MAX),
+ m_numFight(1),
+ m_appearFlag(0, 0),
+ m_dialog(INT_MAX),
+ m_leadTeamMember(INT_MAX)
{
}
-MapTrainer::MapTrainer(const Pokemod* par, const MapTrainer& t, const int _id) :
- Object("MapTrainer", par, _id)
+MapTrainer::MapTrainer(const Pokemod* pokemod, const MapTrainer& trainer, const int id) :
+ Object("MapTrainer", pokemod, id)
{
- *this = t;
+ *this = trainer;
}
-MapTrainer::MapTrainer(const Pokemod* par, const QString& fname, const int _id) :
- Object("MapTrainer", par, _id)
+MapTrainer::MapTrainer(const Pokemod* pokemod, const QString& fileName, const int id) :
+ Object("MapTrainer", pokemod, id)
{
- load(fname, _id);
+ load(fileName, id);
}
MapTrainer::~MapTrainer()
{
- for (QListIterator<MapTrainerTeamMember*> i(teamMembers); i.hasNext(); )
- delete i.next();
+ foreach (MapTrainerTeamMember* teamMember, m_teamMembers)
+ delete teamMember;
}
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(QString("------Trainer \"%1\" with id %2---").arg(m_name).arg(id()), Pokemod::V_Msg);
+ if (m_name == "")
{
- pokemod->validationMsg("Name is not defined");
+ pokemod()->validationMsg("Name is not defined");
valid = false;
}
- if (pokemod->getTrainerIndex(trainerClass) == -1)
+ if (pokemod()->trainerIndex(m_trainerClass) == INT_MAX)
{
- pokemod->validationMsg("Invalid trainer class");
+ pokemod()->validationMsg("Invalid trainer class");
valid = false;
}
- if (Pokemod::D_End_None <= direction)
+ if (Pokemod::D_End_None <= m_direction)
{
- pokemod->validationMsg("Invalid direction");
+ pokemod()->validationMsg("Invalid direction");
valid = false;
}
- if (!numFight || (pokemod->getRules()->getMaxFight() < numFight))
+ if (!m_numFight || (pokemod()->rules()->maxFight() < m_numFight))
{
- pokemod->validationMsg("Invalid number of Pokémon for a fight");
+ pokemod()->validationMsg("Invalid number of Pokémon for a fight");
valid = false;
}
- if (pokemod->getDialogIndex(dialog) == -1)
+ if (pokemod()->dialogIndex(m_dialog) == INT_MAX)
{
- pokemod->validationMsg("Invalid dialog");
+ pokemod()->validationMsg("Invalid dialog");
valid = false;
}
- if (getTeamMemberCount() <= leadTeamMember)
+ if (teamMemberCount() <= m_leadTeamMember)
{
- pokemod->validationMsg("Invalid lead member");
+ pokemod()->validationMsg("Invalid lead member");
valid = false;
}
- if (getTeamMemberCount())
+ if (teamMemberCount())
{
- 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())
+ QMap<int, bool> idChecker;
+ foreach (MapTrainerTeamMember* teamMember, m_teamMembers)
{
- if (1 < i.value())
- {
- pokemod->validationMsg(QString("There are %1 team members with id %2").arg(i.value()).arg(i.key()));
+ if (!teamMember->isValid())
valid = false;
- }
+ if (idChecker[teamMember->id()])
+ pokemod()->validationMsg(QString("Duplicate team member with id %1").arg(teamMember->id()));
+ idChecker[teamMember->id()] = true;
}
}
else
{
- pokemod->validationMsg("There are no team members");
+ pokemod()->validationMsg("There are no team members");
valid = false;
}
return valid;
}
-int MapTrainer::getNewId() const
+void MapTrainer::load(const QString& fileName, int id) throw(Exception)
{
- 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 ini(fileName);
+ if (id == INT_MAX)
ini.getValue("id", id);
- else
- id = _id;
+ setId(id);
int i;
int j;
- ini.getValue("name", name);
- ini.getValue("trainerClass", trainerClass);
+ ini.getValue("name", m_name);
+ ini.getValue("trainerClass", m_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);
+ m_coordinate.set(i, j);
+ ini.getValue("sight", m_sight, 0);
+ ini.getValue("direction", m_direction);
+ ini.getValue("numFight", m_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('/');
+ m_appearFlag.set(i, j);
+ ini.getValue("dialog", m_dialog);
+ ini.getValue("leadTeamMember", m_leadTeamMember);
+ QStringList path = pokemod()->path().split('/');
path.removeLast();
QDir fdir(path.join("/"));
- teamMembers.clear();
+ m_teamMembers.clear();
if (fdir.cd("team"))
{
- for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); )
- newTeamMember(i.next());
+ QStringList files(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name));
+ foreach (QString file, files)
+ newTeamMember(file);
}
}
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);
+ ini.addField("id", id());
+ ini.addField("name", m_name);
+ ini.addField("trainerClass", m_trainerClass);
+ ini.addField("coordinate-x", m_coordinate.x());
+ ini.addField("coordinate-y", m_coordinate.y());
+ ini.addField("sight", m_sight);
+ ini.addField("direction", m_direction);
+ ini.addField("numFight", m_numFight);
+ ini.addField("appearFlag-f", m_appearFlag.flag());
+ ini.addField("appearFlag-s", m_appearFlag.status());
+ ini.addField("dialog", m_dialog);
+ ini.addField("leadTeamMember", m_leadTeamMember);
+ ini.save(QString("%1/map/%2/trainer/%3/data.pini").arg(pokemod()->path()).arg(map).arg(m_name));
+ foreach (MapTrainerTeamMember* teamMember, m_teamMembers)
+ teamMember->save(map, m_name);
}
-void MapTrainer::setName(const QString& n)
+void MapTrainer::setName(const QString& name)
{
- name = n;
+ m_name = name;
}
-void MapTrainer::setTrainerClass(const int t) throw(BoundsException)
+void MapTrainer::setTrainerClass(const int trainerClass) throw(BoundsException)
{
- if (pokemod->getTrainerIndex(t) == -1)
- throw(BoundsException(className, "trainerClass"));
- trainerClass = t;
+ if (pokemod()->trainerIndex(trainerClass) == INT_MAX)
+ throw(BoundsException(className(), "trainerClass"));
+ m_trainerClass = trainerClass;
}
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);
+ m_coordinate.set(x, y);
}
-void MapTrainer::setSight(const int s)
+void MapTrainer::setSight(const int sight)
{
- sight = s;
+ m_sight = sight;
}
-void MapTrainer::setDirection(const int d) throw(BoundsException)
+void MapTrainer::setDirection(const int direction) throw(BoundsException)
{
- if (Pokemod::D_End_None <= d)
- throw(BoundsException(className, "direction"));
- direction = d;
+ if (Pokemod::D_End_None <= direction)
+ throw(BoundsException(className(), "direction"));
+ m_direction = direction;
}
-void MapTrainer::setNumFight(const int n) throw(BoundsException)
+void MapTrainer::setNumFight(const int numFight) throw(BoundsException)
{
- if (!n || (pokemod->getRules()->getMaxFight() < n))
- throw(BoundsException(className, "numFight"));
- numFight = n;
+ if (!numFight || (pokemod()->rules()->maxFight() < numFight))
+ throw(BoundsException(className(), "numFight"));
+ m_numFight = numFight;
}
-void MapTrainer::setAppearFlag(const int f, const int s)
+void MapTrainer::setAppearFlag(const int flag, const int status)
{
- appearFlag.set(f, s);
+ m_appearFlag.set(flag, status);
}
-void MapTrainer::setAppearFlagFlag(const int f)
+void MapTrainer::setDialog(const int dialog) throw(BoundsException)
{
- appearFlag.setFlag(f);
+ if (pokemod()->dialogIndex(dialog) == INT_MAX)
+ throw(BoundsException(className(), "dialog"));
+ m_dialog = dialog;
}
-void MapTrainer::setAppearFlagStatus(const int s)
+void MapTrainer::setLeadTeamMember(const int leadMember) throw(BoundsException)
{
- appearFlag.setStatus(s);
+ if (teamMemberCount() <= leadMember)
+ throw(BoundsException(className(), "leadTeamMember"));
+ m_leadTeamMember = leadMember;
}
-void MapTrainer::setDialog(const int d) throw(BoundsException)
+QString MapTrainer::name() const
{
- if (pokemod->getDialogIndex(d) == -1)
- throw(BoundsException(className, "dialog"));
- dialog = d;
+ return m_name;
}
-void MapTrainer::setLeadTeamMember(const int l) throw(BoundsException)
+int MapTrainer::trainerClass() const
{
- if (getTeamMemberCount() <= l)
- throw(BoundsException(className, "leadTeamMember"));
- leadTeamMember = l;
+ return m_trainerClass;
}
-QString MapTrainer::getName() const
+Point MapTrainer::coordinate() const
{
- return name;
+ return m_coordinate;
}
-int MapTrainer::getTrainerClass() const
+int MapTrainer::sight() const
{
- return trainerClass;
+ return m_sight;
}
-Point MapTrainer::getCoordinate() const
+int MapTrainer::direction() const
{
- return coordinate;
+ return m_direction;
}
-int MapTrainer::getSight() const
+int MapTrainer::numFight() const
{
- return sight;
+ return m_numFight;
}
-int MapTrainer::getDirection() const
+Flag MapTrainer::appearFlag() const
{
- return direction;
+ return m_appearFlag;
}
-int MapTrainer::getNumFight() const
+int MapTrainer::dialog() const
{
- return numFight;
+ return m_dialog;
}
-Flag MapTrainer::getAppearFlag() const
+int MapTrainer::leadTeamMember() const
{
- return appearFlag;
+ return m_leadTeamMember;
}
-int MapTrainer::getDialog() const
+const MapTrainerTeamMember* MapTrainer::teamMember(const int index) const throw(IndexException)
{
- return dialog;
+ if (teamMemberCount() <= index)
+ throw(IndexException(className()));
+ return m_teamMembers.at(index);
}
-int MapTrainer::getLeadTeamMember() const
+MapTrainerTeamMember* MapTrainer::teamMember(const int index) throw(IndexException)
{
- return leadTeamMember;
+ if (teamMemberCount() <= index)
+ throw(IndexException(className()));
+ return m_teamMembers[index];
}
-const MapTrainerTeamMember* MapTrainer::getTeamMember(const int i) const throw(IndexException)
+const MapTrainerTeamMember* MapTrainer::teamMemberById(const int id) const throw(IndexException)
{
- if (getTeamMemberCount() <= i)
- throw(IndexException(className));
- return teamMembers.at(i);
+ return teamMember(teamMemberIndex(id));
}
-MapTrainerTeamMember* MapTrainer::getTeamMember(const int i) throw(IndexException)
+MapTrainerTeamMember* MapTrainer::teamMemberById(const int id) throw(IndexException)
{
- if (getTeamMemberCount() <= i)
- throw(IndexException(className));
- return teamMembers[i];
+ return teamMember(teamMemberIndex(id));
}
-const MapTrainerTeamMember* MapTrainer::getTeamMemberByID(const int i) const throw(IndexException)
+int MapTrainer::teamMemberIndex(const int id) const
{
- return getTeamMember(getTeamMemberIndex(i));
+ for (int i = 0; i < teamMemberCount(); ++i)
+ {
+ if (m_teamMembers[i]->id() == id)
+ return i;
+ }
+ return INT_MAX;
}
-MapTrainerTeamMember* MapTrainer::getTeamMemberByID(const int i) throw(IndexException)
+int MapTrainer::teamMemberCount() const
{
- return getTeamMember(getTeamMemberIndex(i));
+ return m_teamMembers.size();
}
-int MapTrainer::getTeamMemberIndex(const int _id) const
+MapTrainerTeamMember* MapTrainer::newTeamMember()
{
- for (int i = 0; i < getTeamMemberCount(); ++i)
- {
- if (teamMembers[i]->getId() == _id)
- return i;
- }
- return -1;
+ m_teamMembers.append(new MapTrainerTeamMember(pokemod(), newTeamMemberId()));
+ return m_teamMembers[teamMemberCount() - 1];
}
-int MapTrainer::getTeamMemberCount() const
+MapTrainerTeamMember* MapTrainer::newTeamMember(const QString& fileName)
{
- return teamMembers.size();
+ m_teamMembers.append(new MapTrainerTeamMember(pokemod(), fileName, newTeamMemberId()));
+ return m_teamMembers[teamMemberCount() - 1];
}
-MapTrainerTeamMember* MapTrainer::newTeamMember()
+MapTrainerTeamMember* MapTrainer::newTeamMember(const MapTrainerTeamMember& teamMember)
{
- teamMembers.append(new MapTrainerTeamMember(pokemod, getNewId()));
- return teamMembers[getTeamMemberCount() - 1];
+ m_teamMembers.append(new MapTrainerTeamMember(pokemod(), teamMember, newTeamMemberId()));
+ return m_teamMembers[teamMemberCount() - 1];
}
-MapTrainerTeamMember* MapTrainer::newTeamMember(const QString& fname)
+void MapTrainer::deleteTeamMember(const int index) throw(IndexException)
{
- teamMembers.append(new MapTrainerTeamMember(pokemod, fname, getNewId()));
- return teamMembers[getTeamMemberCount() - 1];
+ if (teamMemberCount() <= index)
+ throw(IndexException(className()));
+ delete m_teamMembers[index];
+ m_teamMembers.removeAt(index);
}
-MapTrainerTeamMember* MapTrainer::newTeamMember(const MapTrainerTeamMember& p)
+void MapTrainer::deleteTeamMemberById(const int id) throw(IndexException)
{
- teamMembers.append(new MapTrainerTeamMember(pokemod, p, getNewId()));
- return teamMembers[getTeamMemberCount() - 1];
+ deleteTeamMember(teamMemberIndex(id));
}
-void MapTrainer::deleteTeamMember(const int i) throw(IndexException)
+int MapTrainer::newTeamMemberId() const
{
- if (getTeamMemberCount() <= i)
- throw(IndexException(className));
- delete teamMembers[i];
- teamMembers.removeAt(i);
+ int i = 0;
+ while ((i < teamMemberCount()) && (teamMemberIndex(i) != INT_MAX))
+ ++i;
+ return 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()));
+ m_name = rhs.m_name;
+ m_trainerClass = rhs.m_trainerClass;
+ m_coordinate = rhs.m_coordinate;
+ m_sight = rhs.m_sight;
+ m_direction = rhs.m_direction;
+ m_numFight = rhs.m_numFight;
+ m_appearFlag = rhs.m_appearFlag;
+ m_dialog = rhs.m_dialog;
+ m_leadTeamMember = rhs.m_leadTeamMember;
+ m_teamMembers.clear();
+ foreach (MapTrainerTeamMember* teamMember, rhs.m_teamMembers)
+ m_teamMembers.append(new MapTrainerTeamMember(pokemod(), *teamMember, teamMember->id()));
return *this;
}