/* * Copyright 2007-2009 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" // Sigmod includes #include "Game.h" #include "Macros.h" #include "Map.h" #include "MapTrainerTeamMember.h" #include "Rules.h" // Qt includes #include using namespace Sigcore; using namespace Sigmod; MapTrainer::MapTrainer(const MapTrainer& trainer) : Object(trainer.parent(), trainer.id()), m_teamMembers(this) { *this = trainer; } MapTrainer::MapTrainer(const Map* parent, const int id) : Object(parent, id), m_name(""), m_trainerClass(INT_MAX), m_position(0, 0), m_numberFight(1), m_script("", ""), m_teamMembers(this) { } MapTrainer::MapTrainer(const MapTrainer& trainer, const Map* parent, const int id) : Object(parent, id), m_teamMembers(this) { *this = trainer; } MapTrainer::MapTrainer(const QDomElement& xml, const Map* parent, const int id) : Object(parent, id), m_teamMembers(this) { initXmlImport(xml, id); load(xml); } void MapTrainer::validate() { testBegin(); if (m_name.isEmpty()) emit(error("Name is empty")); TEST(trainerClass); TEST(position); TEST(numberFight); if (!m_leadTeamMember.size()) emit(error("There are no leading team members set")); else TEST_LIST(leadTeamMember); if (!m_teamMembers.count()) emit(error("There are no team members")); else if (m_teamMembers.count() < m_numberFight) emit(warning("There less team members than set to fight")); QSet idChecker; m_teamMembers.validate(); for (int i = 0; i < m_teamMembers.count(); ++i) { MapTrainerTeamMember* teamMember = m_teamMembers.at(i); testSubclassValue(teamMember, teamMember->id(), "ID value", &idChecker); } testEnd(); } void MapTrainer::load(const QDomElement& xml) { clear(); loadValue(xml.firstChildElement("name"), &m_name); loadValue(xml.firstChildElement("trainerClass"), &m_trainerClass); loadValue(xml.firstChildElement("position"), &m_position); loadValue(xml.firstChildElement("numberFight"), &m_numberFight); loadValue(xml.firstChildElement("script"), &m_script); loadList(xml.firstChildElement("leadTeamMember"), &m_leadTeamMember); m_teamMembers.importXml(xml, "MapTrainerTeamMember"); } QDomElement MapTrainer::save() const { QDomElement xml = initXmlExport(); xml.appendChild(saveValue("name", m_name)); xml.appendChild(saveValue("trainerClass", m_trainerClass)); xml.appendChild(saveValue("position", m_position)); xml.appendChild(saveValue("numberFight", m_numberFight)); xml.appendChild(saveValue("script", m_script)); xml.appendChild(saveList("leadTeamMember", m_leadTeamMember)); m_teamMembers.exportXml(&xml); return xml; } SETTER(MapTrainer, QString&, Name, name) SETTER(MapTrainer, int, TrainerClass, trainerClass) SETTER(MapTrainer, QPoint&, Position, position) SETTER(MapTrainer, int, NumberFight, numberFight) SETTER(MapTrainer, Script&, Script, script) SETTER_LIST_LIMIT(MapTrainer, LeadTeamMember, leadTeamMember, m_numberFight, "More lead team members than fighting") GETTER(MapTrainer, QString, name) GETTER(MapTrainer, int, trainerClass) GETTER(MapTrainer, QPoint, position) GETTER(MapTrainer, int, numberFight) GETTER(MapTrainer, Script, script) GETTER_LIST(MapTrainer, leadTeamMember) const MapTrainer::TeamMembers& MapTrainer::teamMembers() const { return m_teamMembers; } MapTrainer::TeamMembers& MapTrainer::teamMembers() { return m_teamMembers; } CHECK(MapTrainer, QString&, name) CHECK_INDEX(MapTrainer, int, trainerClass, game(), trainer) CHECK_BEGIN(MapTrainer, QPoint&, position) const Map* map = qobject_cast(parent()); TBOUNDS_MOD(position_x, 0, map->width() - 1, position.x()) TBOUNDS_MOD(position_y, 0, map->height() - 1, position.y()) CHECK_END() CHECK_BOUNDS(MapTrainer, int, numberFight, 1, game()->rules()->maxFight()) CHECK(MapTrainer, Script&, script) CHECK_BEGIN(MapTrainer, int, leadTeamMember) if (!teamMemberById(leadTeamMember)) { EBOUNDS_IDX(leadTeamMember); return false; } CHECK_END() MapTrainer& MapTrainer::operator=(const MapTrainer& rhs) { if (this == &rhs) return *this; clear(); m_name = rhs.m_name; m_trainerClass = rhs.m_trainerClass; m_position = rhs.m_position; m_numberFight = rhs.m_numberFight; m_script = rhs.m_script; m_leadTeamMember = rhs.m_leadTeamMember; m_teamMembers = rhs.m_teamMembers; return *this; } void MapTrainer::clear() { m_leadTeamMember.clear(); m_teamMembers.clear(); Object::clear(); }