/* * 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(setSpecies, species); TEST(setLevel, level); if (sigmod()->rules()->maxAbilities() < m_ability.size()) emit(error("Too many abilities")); TEST_LIST(setAbility, ability); if (sigmod()->rules()->maxHeldItems() < m_item.size()) emit(error("Too many held items")); TEST_LIST(setItem, item); if (sigmod()->rules()->maxMoves() < m_move.size()) emit(error("Too many moves")); TEST_LIST(setMove, move); if (sigmod()->rules()->maxNatures() < m_nature.size()) emit(error("Too many natures")); TEST_LIST(setNature, 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; } void Sigmod::MapTrainerTeamMember::setSpecies(const int species) { if (!sigmod()->speciesById(species)) emit(error(bounds("species", species))); else CHECK(species); } void Sigmod::MapTrainerTeamMember::setLevel(const int level) { if ((level <= 0) || (sigmod()->rules()->maxLevel() < level)) emit(error(bounds("level", 0, sigmod()->rules()->maxLevel(), level))); else CHECK(level); } void Sigmod::MapTrainerTeamMember::setAbility(const int ability, const bool state) { if (!sigmod()->abilityById(ability)) emit(error(bounds("ability", ability))); else if (state && !m_ability.contains(ability)) { if (m_ability.size() < sigmod()->rules()->maxAbilities()) { m_ability.append(ability); emit(changed()); } else emit(error("Cannot have any more abilities")); } else if (m_ability.contains(ability)) { m_ability.removeAll(ability); emit(changed()); } } void Sigmod::MapTrainerTeamMember::setItem(const int item, const bool state) { if (!sigmod()->itemById(item)) emit(error(bounds("item", item))); else if (state && !m_item.contains(item)) { if (m_item.size() < sigmod()->rules()->maxHeldItems()) { if (checkWeight(item)) { m_item.append(item); emit(changed()); } else emit(error("Cannot carry that much weight")); } else emit(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 (!sigmod()->moveById(move)) emit(error(bounds("move", move))); else if (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()); return; } else emit(error("Cannot know any more moves")); } } emit(error("Cannot learn the move")); } else if (m_move.contains(move)) { m_move.removeAll(move); emit(changed()); } } void Sigmod::MapTrainerTeamMember::setNature(const int nature, const bool state) { if (!sigmod()->natureById(nature)) emit(error(bounds("nature", nature))); else if (state && !m_nature.contains(nature)) { if (m_nature.size() < sigmod()->rules()->maxNatures()) { m_nature.append(nature); emit(changed()); } else emit(error("Cannot have any more natures")); } else if (m_nature.contains(nature)) { m_nature.removeAll(nature); emit(changed()); } } int Sigmod::MapTrainerTeamMember::species() const { return m_species; } int Sigmod::MapTrainerTeamMember::level() const { return m_level; } bool Sigmod::MapTrainerTeamMember::ability(const int ability) const { return m_ability.contains(ability); } QList Sigmod::MapTrainerTeamMember::abilities() const { return m_ability; } bool Sigmod::MapTrainerTeamMember::item(const int item) const { return m_item.contains(item); } QList Sigmod::MapTrainerTeamMember::items() const { return m_item; } bool Sigmod::MapTrainerTeamMember::move(const int move) const { return m_move.contains(move); } QList Sigmod::MapTrainerTeamMember::moves() const { return m_move; } bool Sigmod::MapTrainerTeamMember::nature(const int nature) const { return m_nature.contains(nature); } QList Sigmod::MapTrainerTeamMember::natures() const { return m_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()); }