/* * 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 "MapTrainerTeamMember.h" // Sigmod includes #include "Item.h" #include "Macros.h" #include "MapTrainer.h" #include "Rules.h" #include "Sigmod.h" #include "Species.h" #include "SpeciesMove.h" // Qt includes #include Sigmod::MapTrainerTeamMember::MapTrainerTeamMember(const MapTrainerTeamMember& teamMember) : Object(teamMember.parent(), teamMember.id()) { *this = teamMember; } Sigmod::MapTrainerTeamMember::MapTrainerTeamMember(const MapTrainer* parent, const int id) : Object(parent, id), m_species(INT_MAX), m_level(INT_MAX) { } Sigmod::MapTrainerTeamMember::MapTrainerTeamMember(const MapTrainerTeamMember& teamMember, const MapTrainer* parent, const int id) : Object(parent, id) { *this = teamMember; } Sigmod::MapTrainerTeamMember::MapTrainerTeamMember(const QDomElement& xml, const MapTrainer* parent, const int id) : Object(parent, id) { LOAD_ID(); load(xml); } void Sigmod::MapTrainerTeamMember::validate() { TEST_BEGIN(); TEST(species); TEST(level); if (sigmod()->rules()->maxAbilities() < m_ability.size()) emit(error("Too many abilities")); TEST_LIST(ability); if (sigmod()->rules()->maxHeldItems() < m_item.size()) emit(error("Too many held items")); TEST_LIST(item); if (sigmod()->rules()->maxMoves() < m_move.size()) emit(error("Too many moves")); TEST_LIST(move); if (sigmod()->rules()->maxNatures() < m_nature.size()) emit(error("Too many natures")); TEST_LIST(nature); TEST_END(); } void Sigmod::MapTrainerTeamMember::load(const QDomElement& xml) { LOAD_BEGIN(); LOAD(species); LOAD(level); LOAD_LIST(ability); LOAD_LIST(item); LOAD_LIST(move); LOAD_LIST(nature); } QDomElement Sigmod::MapTrainerTeamMember::save() const { SAVE_CREATE(); SAVE(species); SAVE(level); SAVE_LIST(ability); SAVE_LIST(item); SAVE_LIST(move); SAVE_LIST(nature); return xml; } SETTER(MapTrainerTeamMember, int, Species, species) SETTER(MapTrainerTeamMember, int, Level, level) SETTER_LIST_LIMIT(MapTrainerTeamMember, Ability, ability, sigmod()->rules()->maxAbilities(), "Cannot have anymore abilities") void Sigmod::MapTrainerTeamMember::setItem(const int item, const bool state) { if (itemCheck(item) && state && !m_item.contains(item)) { if (m_item.size() < sigmod()->rules()->maxHeldItems()) { if (checkWeight(item)) { m_item.append(item); emit(changed()); } else ERROR("Cannot carry that much weight"); } else ERROR("Cannot hold any more items"); } else if (m_item.contains(item)) { m_item.removeAll(item); emit(changed()); } } void Sigmod::MapTrainerTeamMember::setMove(const int move, const bool state) { if (moveCheck(move) && state && !m_move.contains(move)) { const Species* species = sigmod()->speciesById(move); for (int i = 0; i < species->moveCount(); ++i) { const SpeciesMove* speciesMove = species->move(i); if (speciesMove->move() == move) { if (m_move.size() < sigmod()->rules()->maxMoves()) { m_move.append(move); emit(changed()); } else ERROR("Cannot know any more moves"); } } ERROR("Cannot learn the move"); } else if (m_move.contains(move)) { m_move.removeAll(move); emit(changed()); } } SETTER_LIST_LIMIT(MapTrainerTeamMember, Nature, nature, sigmod()->rules()->maxNatures(), "Cannot have anymore natures") GETTER(MapTrainerTeamMember, int, species) GETTER(MapTrainerTeamMember, int, level) GETTER_LIST(MapTrainerTeamMember, ability) GETTER_LIST(MapTrainerTeamMember, item) GETTER_LIST(MapTrainerTeamMember, move) GETTER_LIST(MapTrainerTeamMember, nature) CHECK_INDEX(MapTrainerTeamMember, int, species, sigmod(), species) CHECK_BOUNDS(MapTrainerTeamMember, int, level, 1, sigmod()->rules()->maxLevel()) CHECK_INDEX(MapTrainerTeamMember, int, ability, sigmod(), ability) CHECK_INDEX(MapTrainerTeamMember, int, item, sigmod(), item) CHECK_INDEX(MapTrainerTeamMember, int, move, sigmod(), move) CHECK_INDEX(MapTrainerTeamMember, int, nature, sigmod(), nature) Sigmod::MapTrainerTeamMember& Sigmod::MapTrainerTeamMember::operator=(const MapTrainerTeamMember& rhs) { if (this == &rhs) return *this; COPY(species); COPY(level); COPY(ability); COPY(item); COPY(move); COPY(nature); return *this; } bool Sigmod::MapTrainerTeamMember::checkWeight(const int item) { const Species* species = sigmod()->speciesById(m_species); if (!species) return true; int totalWeight = sigmod()->itemById(item)->weight(); foreach (int itemId, m_item) { const Item* item = sigmod()->itemById(itemId); if (!item) totalWeight += item->weight(); } return (totalWeight <= species->maxHoldWeight()); }