/* * 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 "Dialog.h" #include "Map.h" #include "MapTrainerTeamMember.h" #include "Pokemod.h" #include "Rules.h" // Qt includes #include MapTrainer::MapTrainer(const MapTrainer& trainer) : Object("MapTrainer", trainer.parent(), trainer.id()) { *this = trainer; } MapTrainer::MapTrainer(const Map* parent, const int id) : Object("MapTrainer", parent, id), m_name(""), m_trainerClass(INT_MAX), m_coordinate(0, 0), m_sight(0), m_direction(INT_MAX), m_numberFight(1), m_appearFlag(0, 0), m_dialog(INT_MAX), m_leadTeamMember(INT_MAX) { } MapTrainer::MapTrainer(const MapTrainer& trainer, const Map* parent, const int id) : Object("MapTrainer", parent, id) { *this = trainer; } MapTrainer::MapTrainer(const QDomElement& xml, const Map* parent, const int id) : Object("MapTrainer", parent, id) { load(xml, id); } MapTrainer::~MapTrainer() { clear(); } void MapTrainer::validate() { if (m_name.isEmpty()) emit(error("Name is empty")); TEST(setTrainerClass, trainerClass); TEST(setCoordinate, coordinate); TEST(setDirection, direction); TEST(setNumberFight, numberFight); TEST(setDialog, dialog); TEST(setLeadTeamMember, leadTeamMember); if (!teamMemberCount()) emit(error("There are no team members")); QSet idChecker; foreach (MapTrainerTeamMember* teamMember, m_teamMember) { teamMember->validate(); if (idChecker.contains(teamMember->id())) emit(error(subclass("team member", teamMember->id()))); idChecker.insert(teamMember->id()); } } void MapTrainer::load(const QDomElement& xml, int id) { LOAD_ID(); LOAD(QString, name); LOAD(int, trainerClass); LOAD(Point, coordinate); LOAD(int, sight); LOAD(int, direction); LOAD(int, numberFight); LOAD(Flag, appearFlag); LOAD(int, dialog); LOAD_SUB(newTeamMember, MapTrainerTeamMember); LOAD(int, leadTeamMember); } QDomElement MapTrainer::save() const { SAVE_CREATE(); SAVE(QString, name); SAVE(int, trainerClass); SAVE(Point, coordinate); SAVE(int, sight); SAVE(int, direction); SAVE(int, numberFight); SAVE(Flag, appearFlag); SAVE(int, dialog); SAVE_SUB(MapTrainerTeamMember, teamMember); return xml; } void MapTrainer::setName(const QString& name) { m_name = name; emit(changed()); } void MapTrainer::setTrainerClass(const int trainerClass) { if (static_cast(pokemod())->trainerIndex(trainerClass) == INT_MAX) { emit(error(bounds("trainerClass"))); return; } m_trainerClass = trainerClass; emit(changed()); } void MapTrainer::setCoordinate(const Point& coordinate) { if ((static_cast(parent())->width() <= coordinate.x()) || (static_cast(parent())->height() <= coordinate.y())) { emit(error(bounds("coordinate"))); return; } m_coordinate = coordinate; emit(changed()); } void MapTrainer::setSight(const int sight) { m_sight = sight; emit(changed()); } void MapTrainer::setDirection(const int direction) { if (Pokemod::D_End_None <= direction) { emit(error(bounds("direction"))); return; } m_direction = direction; emit(changed()); } void MapTrainer::setNumberFight(const int numberFight) { if (!numberFight || (static_cast(pokemod())->rules()->maxFight() < numberFight)) { emit(error(bounds("numberFight"))); return; } m_numberFight = numberFight; emit(changed()); } void MapTrainer::setAppearFlag(const Flag& appearFlag) { m_appearFlag = appearFlag; emit(changed()); } void MapTrainer::setDialog(const int dialog) { if (static_cast(pokemod())->dialogIndex(dialog) == INT_MAX) { emit(error(bounds("dialog"))); return; } m_dialog = dialog; emit(changed()); } void MapTrainer::setLeadTeamMember(const int leadMember) { if (teamMemberCount() <= leadMember) { emit(error(bounds("leadTeamMember"))); return; } m_leadTeamMember = leadMember; emit(changed()); } QString MapTrainer::name() const { return m_name; } int MapTrainer::trainerClass() const { return m_trainerClass; } Point MapTrainer::coordinate() const { return m_coordinate; } int MapTrainer::sight() const { return m_sight; } int MapTrainer::direction() const { return m_direction; } int MapTrainer::numberFight() const { return m_numberFight; } Flag MapTrainer::appearFlag() const { return m_appearFlag; } int MapTrainer::dialog() const { return m_dialog; } int MapTrainer::leadTeamMember() const { return m_leadTeamMember; } const MapTrainerTeamMember* MapTrainer::teamMember(const int index) const { if (teamMemberCount() <= index) return NULL; return m_teamMember.at(index); } MapTrainerTeamMember* MapTrainer::teamMember(const int index) { if (teamMemberCount() <= index) return NULL; return m_teamMember[index]; } const MapTrainerTeamMember* MapTrainer::teamMemberById(const int id) const { return teamMember(teamMemberIndex(id)); } MapTrainerTeamMember* MapTrainer::teamMemberById(const int id) { return teamMember(teamMemberIndex(id)); } int MapTrainer::teamMemberIndex(const int id) const { for (int i = 0; i < teamMemberCount(); ++i) { if (m_teamMember[i]->id() == id) return i; } return INT_MAX; } int MapTrainer::teamMemberCount() const { return m_teamMember.size(); } MapTrainerTeamMember* MapTrainer::newTeamMember() { return newTeamMember(new MapTrainerTeamMember(this, newTeamMemberId())); } MapTrainerTeamMember* MapTrainer::newTeamMember(const QDomElement& xml) { return newTeamMember(new MapTrainerTeamMember(xml, this, newTeamMemberId())); } MapTrainerTeamMember* MapTrainer::newTeamMember(const MapTrainerTeamMember& teamMember) { return newTeamMember(new MapTrainerTeamMember(teamMember, this, newTeamMemberId())); } MapTrainerTeamMember* MapTrainer::newTeamMember(MapTrainerTeamMember* teamMember) { m_teamMember.append(teamMember); return teamMember; } void MapTrainer::deleteTeamMember(const int index) { if (teamMemberCount() <= index) return; delete m_teamMember[index]; m_teamMember.removeAt(index); } void MapTrainer::deleteTeamMemberById(const int id) { deleteTeamMember(teamMemberIndex(id)); } int MapTrainer::newTeamMemberId() const { int i = 0; while ((i < teamMemberCount()) && (teamMemberIndex(i) != INT_MAX)) ++i; return i; } MapTrainer& MapTrainer::operator=(const MapTrainer& rhs) { if (this == &rhs) return *this; clear(); COPY(name); COPY(trainerClass); COPY(coordinate); COPY(sight); COPY(direction); COPY(numberFight); COPY(appearFlag); COPY(dialog); COPY(leadTeamMember); COPY_SUB(MapTrainerTeamMember, teamMember); return *this; } void MapTrainer::clear() { while (teamMemberCount()) deleteTeamMember(0); }