/* * 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 "Move.h" // Pokemod includes #include "MoveEffect.h" #include "Pokemod.h" // Qt includes #include 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.parent(), move.id()) { *this = move; } Move::Move(const Pokemod* parent, const int id) : Object("Move", parent, 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* parent, const int id) : Object("Move", parent, id) { *this = move; } Move::Move(const QDomElement& xml, const Pokemod* parent, const int id) : Object("Move", parent, id) { load(xml, id); } Move::~Move() { clear(); } void Move::validate() { if (m_name.isEmpty()) emit(error("Name is empty")); TEST(setType, type); TEST(setPowerPoints, powerPoints); TEST(setTarget, target); TEST(setNumTargets, numTargets); TEST(setTargetChoice, targetChoice); if (!effectCount()) emit(warning("There are no effects")); QSet idChecker; foreach (MoveEffect* effect, m_effects) { effect->validate(); if (idChecker.contains(effect->id())) emit(error(subclass("effect", effect->id()))); idChecker.insert(effect->id()); } } 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; emit(changed()); } void Move::setAccuracy(const Fraction& accuracy) { if (1 < accuracy) { emit(error(bounds("accuracy"))); return; } m_accuracy = accuracy; emit(changed()); } void Move::setPower(const int power) { m_power = power; emit(changed()); } void Move::setType(const int type) { if (static_cast(pokemod())->typeIndex(type) == INT_MAX) { emit(error(bounds("type"))); return; } m_type = type; emit(changed()); } void Move::setSpecial(const bool special) { m_special = special; emit(changed()); } void Move::setPowerPoints(const int powerPoints) { if (!powerPoints) { emit(error(bounds("powerPoints"))); return; } m_powerPoints = powerPoints; emit(changed()); } void Move::setTarget(const int target) { if (T_End <= target) { emit(error(bounds("target"))); return; } m_target = target; emit(changed()); } void Move::setNumTargets(const int numTargets) { if (!numTargets || ((static_cast(pokemod())->rules()->maxFight() * ((m_target == T_All) ? static_cast(pokemod())->rules()->maxPlayers() : 1)) < numTargets)) { emit(error(bounds("numTargets"))); return; } m_numTargets = numTargets; emit(changed()); } void Move::setTargetChoice(const int targetChoice) { if (C_End <= targetChoice) { emit(error(bounds("targetChoice"))); return; } m_targetChoice = targetChoice; emit(changed()); } void Move::setIgnoreAccuracy(const bool ignoreAccuracy) { m_ignoreAccuracy = ignoreAccuracy; emit(changed()); } void Move::setCanFlinch(const bool canFlinch) { m_canFlinch = canFlinch; emit(changed()); } void Move::setCanRandom(const bool canRandom) { m_canRandom = canRandom; emit(changed()); } void Move::setCanSnatch(const bool canSnatch) { m_canSnatch = canSnatch; emit(changed()); } void Move::setSound(const bool sound) { m_sound = sound; emit(changed()); } void Move::setDescription(const QString& description) { m_description = description; emit(changed()); } 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 { if (effectCount() <= index) return NULL; return m_effects.at(index); } MoveEffect* Move::effect(const int index) { if (effectCount() <= index) return NULL; return m_effects[index]; } const MoveEffect* Move::effectById(const int id) const { return effect(effectIndex(id)); } MoveEffect* Move::effectById(const int id) { 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() { return newEffect(new MoveEffect(this, newEffectId())); } MoveEffect* Move::newEffect(const QDomElement& xml) { return newEffect(new MoveEffect(xml, this, newEffectId())); } MoveEffect* Move::newEffect(const MoveEffect& effect) { return newEffect(new MoveEffect(effect, this, newEffectId())); } MoveEffect* Move::newEffect(MoveEffect* effect) { m_effects.append(effect); return effect; } void Move::deleteEffect(const int index) { if (effectCount() <= index) return; delete m_effects[index]; m_effects.removeAt(index); } void Move::deleteEffectById(const int id) { 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; } void Move::clear() { while (effectCount()) deleteEffect(0); }