/* * 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 . */ // Qt includes #include // Pokemod includes #include "Pokemod.h" #include "MoveEffect.h" // Header include #include "Move.h" const QStringList Move::TargetStr = QStringList() << "Player" << "Enemy" << "All" << "Random"; const QStringList Move::ChoiceStr = QStringList() << "Player" << "Enemy" << "Random"; Move::Move(const Move& move) : Object("Move", move.pokemod(), move.id()) { *this = move; } Move::Move(const Pokemod* pokemod, const int id) : Object("Move", pokemod, id), m_name(""), m_accuracy(1, 1), m_power(0), m_type(INT_MAX), m_special(false), m_powerPoints(0), m_target(INT_MAX), m_targetChoice(INT_MAX), m_ignoreAccuracy(false), m_canFlinch(false), m_canRandom(false), m_canSnatch(false), m_sound(false), m_priority(1), m_description("") { } Move::Move(const Move& move, const Pokemod* pokemod, const int id) : Object("Move", pokemod, id) { *this = move; } Move::Move(const QDomElement& xml, const Pokemod* pokemod, const int id) : Object("Move", pokemod, id) { load(xml, id); } Move::~Move() { clear(); } bool Move::validate() const { bool valid = true; pokemod()->validationMsg(QString("---Move \"%1\" with id %2---").arg(m_name).arg(id()), Pokemod::V_Msg); if (m_name == "") { pokemod()->validationMsg("Name is not defined"); valid = ""; } if (pokemod()->typeIndex(m_type) == INT_MAX) { pokemod()->validationMsg("Invalid type"); valid = false; } if (!m_powerPoints) { pokemod()->validationMsg("Invalid number of power points"); valid = false; } if (T_End <= m_target) { pokemod()->validationMsg("Invalid target"); valid = false; } if (!m_target || (pokemod()->rules()->maxFight() * ((m_target == T_All) ? pokemod()->rules()->maxPlayers() : 1)) < m_numTargets) { pokemod()->validationMsg("Invalid number of targets"); valid = false; } if (C_End <= m_targetChoice) { pokemod()->validationMsg("Invalid target choice"); valid = false; } if (effectCount()) { QMap idChecker; foreach (MoveEffect* effect, m_effects) { if (!effect->isValid()) valid = false; if (idChecker[effect->id()]) pokemod()->validationMsg(QString("Duplicate effect with id %1").arg(effect->id())); idChecker[effect->id()] = true; } } else { pokemod()->validationMsg("There are no effects"); valid = false; } return valid; } void Move::load(const QDomElement& xml, int id) { LOAD_ID(); LOAD(QString, name); LOAD(Fraction, accuracy); LOAD(int, power); LOAD(int, type); LOAD(bool, special); LOAD(int, powerPoints); LOAD(int, target); LOAD(int, targetChoice); LOAD(bool, ignoreAccuracy); LOAD(bool, canFlinch); LOAD(bool, canRandom); LOAD(bool, canSnatch); LOAD(bool, sound); LOAD(QString, description); LOAD_SUB(newEffect, MoveEffect); } QDomElement Move::save() const { SAVE_CREATE(); SAVE(QString, name); SAVE(Fraction, accuracy); SAVE(int, power); SAVE(int, type); SAVE(bool, special); SAVE(int, powerPoints); SAVE(int, target); SAVE(int, numTargets); SAVE(int, targetChoice); SAVE(bool, ignoreAccuracy); SAVE(bool, canFlinch); SAVE(bool, canRandom); SAVE(bool, canSnatch); SAVE(bool, sound); SAVE(QString, description); SAVE_SUB(MoveEffect, effects); return xml; } void Move::setName(const QString& name) { m_name = name; } void Move::setAccuracy(const Fraction& accuracy) throw(BoundsException) { if (1 < accuracy) error("accuracy"); m_accuracy = accuracy; } void Move::setPower(const int power) { m_power = power; } void Move::setType(const int type) throw(BoundsException) { if (pokemod()->typeIndex(type) == INT_MAX) error("type"); m_type = type; } void Move::setSpecial(const bool special) { m_special = special; } void Move::setPowerPoints(const int powerPoints) throw(BoundsException) { if (!powerPoints) error("powerPoints"); m_powerPoints = powerPoints; } void Move::setTarget(const int target) throw(BoundsException) { if (T_End <= target) error("target"); m_target = target; } void Move::setNumTargets(const int numTargets) throw(BoundsException) { if (!numTargets || ((pokemod()->rules()->maxFight() * ((m_target == T_All) ? pokemod()->rules()->maxPlayers() : 1)) < numTargets)) error("numTargets"); m_numTargets = numTargets; } void Move::setTargetChoice(const int targetChoice) throw(BoundsException) { if (C_End <= targetChoice) error("targetChoice"); m_targetChoice = targetChoice; } void Move::setIgnoreAccuracy(const bool ignoreAccuracy) { m_ignoreAccuracy = ignoreAccuracy; } void Move::setCanFlinch(const bool canFlinch) { m_canFlinch = canFlinch; } void Move::setCanRandom(const bool canRandom) { m_canRandom = canRandom; } void Move::setCanSnatch(const bool canSnatch) { m_canSnatch = canSnatch; } void Move::setSound(const bool sound) { m_sound = sound; } void Move::setDescription(const QString& description) { m_description = description; } QString Move::name() const { return m_name; } Fraction Move::accuracy() const { return m_accuracy; } int Move::power() const { return m_power; } int Move::type() const { return m_type; } bool Move::special() const { return m_special; } int Move::powerPoints() const { return m_powerPoints; } int Move::target() const { return m_target; } int Move::numTargets() const { return m_numTargets; } int Move::targetChoice() const { return m_targetChoice; } bool Move::ignoreAccuracy() const { return m_ignoreAccuracy; } bool Move::canFlinch() const { return m_canFlinch; } bool Move::canRandom() const { return m_canRandom; } bool Move::canSnatch() const { return m_canSnatch; } bool Move::sound() const { return m_sound; } QString Move::description() const { return m_description; } const MoveEffect* Move::effect(const int index) const throw(IndexException) { if (effectCount() <= index) warning("effect"); return m_effects.at(index); } MoveEffect* Move::effect(const int index) throw(IndexException) { if (effectCount() <= index) error("effect"); return m_effects[index]; } const MoveEffect* Move::effectById(const int id) const throw(IndexException) { return effect(effectIndex(id)); } MoveEffect* Move::effectById(const int id) throw(IndexException) { return effect(effectIndex(id)); } int Move::effectIndex(const int id) const { for (int i = 0; i < effectCount(); ++i) { if (m_effects[i]->id() == id) return i; } return INT_MAX; } int Move::effectCount() const { return m_effects.size(); } MoveEffect* Move::newEffect() { m_effects.append(new MoveEffect(pokemod(), newEffectId())); return m_effects[effectCount() - 1]; } MoveEffect* Move::newEffect(const QDomElement& xml) { m_effects.append(new MoveEffect(xml, pokemod(), newEffectId())); return m_effects[effectCount() - 1]; } MoveEffect* Move::newEffect(const MoveEffect& effect) { m_effects.append(new MoveEffect(effect, pokemod(), newEffectId())); return m_effects[effectCount() - 1]; } void Move::deleteEffect(const int index) throw(IndexException) { if (effectCount() <= index) error("effect"); delete m_effects[index]; m_effects.removeAt(index); } void Move::deleteEffectById(const int id) throw(IndexException) { deleteEffect(effectIndex(id)); } int Move::newEffectId() const { int i = 0; while ((i < effectCount()) && (effectIndex(i) != INT_MAX)) ++i; return i; } Move& Move::operator=(const Move& rhs) { if (this == &rhs) return *this; clear(); COPY(name); COPY(accuracy); COPY(power); COPY(type); COPY(special); COPY(powerPoints); COPY(target); COPY(numTargets); COPY(targetChoice); COPY(ignoreAccuracy); COPY(canFlinch); COPY(canRandom); COPY(canSnatch); COPY(sound); COPY(description); COPY_SUB(MoveEffect, effects); return *this; }