/* * Copyright 2007-2008 Ben Boeckel * * 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 . */ // 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 Pokemod::MapTrainer::MapTrainer(const MapTrainer& trainer) : Object("MapTrainer", trainer.parent(), trainer.id()) { *this = trainer; } Pokemod::MapTrainer::MapTrainer(const Map* parent, const int id) : Object("MapTrainer", 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("MapTrainer", parent, id) { *this = trainer; } Pokemod::MapTrainer::MapTrainer(const QDomElement& xml, const Map* parent, const int id) : Object("MapTrainer", 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 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(QString, name); LOAD(int, trainerClass); LOAD(QPoint, coordinate); LOAD(int, numberFight); LOAD(Script, script); LOAD_SUB(newTeamMember, MapTrainerTeamMember); LOAD(int, leadTeamMember); } QDomElement Pokemod::MapTrainer::save() const { SAVE_CREATE(); SAVE(QString, name); SAVE(int, trainerClass); SAVE(QPoint, coordinate); SAVE(int, numberFight); SAVE(Script, script); SAVE_SUB(MapTrainerTeamMember, teamMembers); return xml; } void Pokemod::MapTrainer::setName(const QString& name) { CHECK(name); } void Pokemod::MapTrainer::setTrainerClass(const int trainerClass) { if (static_cast(pokemod())->trainerIndex(trainerClass) == INT_MAX) { emit(error(bounds("trainerClass"))); return; } CHECK(trainerClass); } void Pokemod::MapTrainer::setCoordinate(const QPoint& coordinate) { if ((static_cast(parent())->width() <= coordinate.x()) || (static_cast(parent())->height() <= coordinate.y())) { emit(error(bounds("coordinate"))); return; } CHECK(coordinate); } void Pokemod::MapTrainer::setNumberFight(const int numberFight) { if (!numberFight || (static_cast(pokemod())->rules()->maxFight() < numberFight)) { emit(error(bounds("numberFight"))); return; } CHECK(numberFight); } void Pokemod::MapTrainer::setScript(const Script& script) { CHECK(script); } void Pokemod::MapTrainer::setLeadTeamMember(const int leadTeamMember) { if (teamMemberCount() <= leadTeamMember) { emit(error(bounds("leadTeamMember"))); return; } 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); }