diff options
Diffstat (limited to 'sigmod/MapTrainer.cpp')
-rw-r--r-- | sigmod/MapTrainer.cpp | 277 |
1 files changed, 277 insertions, 0 deletions
diff --git a/sigmod/MapTrainer.cpp b/sigmod/MapTrainer.cpp new file mode 100644 index 00000000..4df1cbed --- /dev/null +++ b/sigmod/MapTrainer.cpp @@ -0,0 +1,277 @@ +/* + * 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 "MapTrainer.h" + +// Pokemod includes +#include "Macros.h" +#include "Map.h" +#include "MapTrainerTeamMember.h" +#include "Pokemod.h" +#include "Rules.h" + +// Qt includes +#include <QtCore/QSet> + +Pokemod::MapTrainer::MapTrainer(const MapTrainer& trainer) : + Object(trainer.parent(), trainer.id()) +{ + *this = trainer; +} + +Pokemod::MapTrainer::MapTrainer(const Map* parent, const int id) : + Object(parent, id), + m_name(""), + m_trainerClass(INT_MAX), + m_coordinate(0, 0), + m_numberFight(1), + m_script("", ""), + m_leadTeamMember(INT_MAX) +{ +} + +Pokemod::MapTrainer::MapTrainer(const MapTrainer& trainer, const Map* parent, const int id) : + Object(parent, id) +{ + *this = trainer; +} + +Pokemod::MapTrainer::MapTrainer(const QDomElement& xml, const Map* parent, const int id) : + Object(parent, id) +{ + LOAD_ID(); + load(xml); +} + +Pokemod::MapTrainer::~MapTrainer() +{ + clear(); +} + +void Pokemod::MapTrainer::validate() +{ + TEST_BEGIN(); + if (m_name.isEmpty()) + emit(error("Name is empty")); + TEST(setTrainerClass, trainerClass); + TEST(setCoordinate, coordinate); + TEST(setNumberFight, numberFight); + TEST(setLeadTeamMember, leadTeamMember); + if (!teamMemberCount()) + emit(error("There are no team members")); + QSet<int> idChecker; + TEST_SUB_BEGIN(MapTrainerTeamMember, teamMembers); + TEST_SUB("team member", id); + TEST_SUB_END(); + TEST_END(); +} + +void Pokemod::MapTrainer::load(const QDomElement& xml) +{ + LOAD_BEGIN(); + LOAD(name); + LOAD(trainerClass); + LOAD(coordinate); + LOAD(numberFight); + LOAD(script); + LOAD_SUB(newTeamMember, MapTrainerTeamMember); + LOAD(leadTeamMember); +} + +QDomElement Pokemod::MapTrainer::save() const +{ + SAVE_CREATE(); + SAVE(name); + SAVE(trainerClass); + SAVE(coordinate); + SAVE(numberFight); + SAVE(script); + SAVE_SUB(MapTrainerTeamMember, teamMembers); + return xml; +} + +void Pokemod::MapTrainer::setName(const QString& name) +{ + CHECK(name); +} + +void Pokemod::MapTrainer::setTrainerClass(const int trainerClass) +{ + if (qobject_cast<const Pokemod*>(pokemod())->trainerIndex(trainerClass) == INT_MAX) + emit(error(bounds("trainerClass"))); + else + CHECK(trainerClass); +} + +void Pokemod::MapTrainer::setCoordinate(const QPoint& coordinate) +{ + if ((qobject_cast<const Map*>(parent())->width() <= coordinate.x()) || (qobject_cast<const Map*>(parent())->height() <= coordinate.y())) + emit(error(bounds("coordinate"))); + else + CHECK(coordinate); +} + +void Pokemod::MapTrainer::setNumberFight(const int numberFight) +{ + if (!numberFight || (qobject_cast<const Pokemod*>(pokemod())->rules()->maxFight() < numberFight)) + emit(error(bounds("numberFight"))); + else + CHECK(numberFight); +} + +void Pokemod::MapTrainer::setScript(const Script& script) +{ + CHECK(script); +} + +void Pokemod::MapTrainer::setLeadTeamMember(const int leadTeamMember) +{ + if (teamMemberIndex(leadTeamMember) == INT_MAX) + emit(error(bounds("leadTeamMember"))); + else + CHECK(leadTeamMember); +} + +QString Pokemod::MapTrainer::name() const +{ + return m_name; +} + +int Pokemod::MapTrainer::trainerClass() const +{ + return m_trainerClass; +} + +QPoint Pokemod::MapTrainer::coordinate() const +{ + return m_coordinate; +} + +int Pokemod::MapTrainer::numberFight() const +{ + return m_numberFight; +} + +Pokemod::Script Pokemod::MapTrainer::script() const +{ + return m_script; +} + +int Pokemod::MapTrainer::leadTeamMember() const +{ + return m_leadTeamMember; +} + +const Pokemod::MapTrainerTeamMember* Pokemod::MapTrainer::teamMember(const int index) const +{ + Q_ASSERT(index < teamMemberCount()); + return m_teamMembers.at(index); +} + +Pokemod::MapTrainerTeamMember* Pokemod::MapTrainer::teamMember(const int index) +{ + Q_ASSERT(index < teamMemberCount()); + return m_teamMembers[index]; +} + +const Pokemod::MapTrainerTeamMember* Pokemod::MapTrainer::teamMemberById(const int id) const +{ + return teamMember(teamMemberIndex(id)); +} + +Pokemod::MapTrainerTeamMember* Pokemod::MapTrainer::teamMemberById(const int id) +{ + return teamMember(teamMemberIndex(id)); +} + +int Pokemod::MapTrainer::teamMemberIndex(const int id) const +{ + for (int i = 0; i < teamMemberCount(); ++i) + { + if (m_teamMembers[i]->id() == id) + return i; + } + return INT_MAX; +} + +int Pokemod::MapTrainer::teamMemberCount() const +{ + return m_teamMembers.size(); +} + +Pokemod::MapTrainerTeamMember* Pokemod::MapTrainer::newTeamMember() +{ + return newTeamMember(new MapTrainerTeamMember(this, newTeamMemberId())); +} + +Pokemod::MapTrainerTeamMember* Pokemod::MapTrainer::newTeamMember(const QDomElement& xml) +{ + return newTeamMember(new MapTrainerTeamMember(xml, this, newTeamMemberId())); +} + +Pokemod::MapTrainerTeamMember* Pokemod::MapTrainer::newTeamMember(const MapTrainerTeamMember& teamMember) +{ + return newTeamMember(new MapTrainerTeamMember(teamMember, this, newTeamMemberId())); +} + +Pokemod::MapTrainerTeamMember* Pokemod::MapTrainer::newTeamMember(MapTrainerTeamMember* teamMember) +{ + m_teamMembers.append(teamMember); + return teamMember; +} + +void Pokemod::MapTrainer::deleteTeamMember(const int index) +{ + Q_ASSERT(index < teamMemberCount()); + delete m_teamMembers[index]; + m_teamMembers.removeAt(index); +} + +void Pokemod::MapTrainer::deleteTeamMemberById(const int id) +{ + deleteTeamMember(teamMemberIndex(id)); +} + +int Pokemod::MapTrainer::newTeamMemberId() const +{ + int i = 0; + while ((i < teamMemberCount()) && (teamMemberIndex(i) != INT_MAX)) + ++i; + return i; +} + +Pokemod::MapTrainer& Pokemod::MapTrainer::operator=(const MapTrainer& rhs) +{ + if (this == &rhs) + return *this; + clear(); + COPY(name); + COPY(trainerClass); + COPY(coordinate); + COPY(numberFight); + COPY(script); + COPY(leadTeamMember); + COPY_SUB(MapTrainerTeamMember, teamMembers); + return *this; +} + +void Pokemod::MapTrainer::clear() +{ + while (teamMemberCount()) + deleteTeamMember(0); +} |