/* * 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 . */ #include "Pokemod.h" #include "CoinListObject.h" const QStringList CoinListObject::TypeStr = QStringList() << "Item" << "Pokémon"; CoinListObject::CoinListObject(const Pokemod* pokemod, const int id) : Object("CoinListObject", pokemod, id), m_type(Item), m_object(INT_MAX), m_amount(1), m_cost(0) { } CoinListObject::CoinListObject(const Pokemod* pokemod, const CoinListObject& object, const int id) : Object("CoinListObject", pokemod, id) { *this = object; } CoinListObject::CoinListObject(const Pokemod* pokemod, const QString& fileName, const int id) : Object("CoinListObject", pokemod, id) { load(fileName, id); } bool CoinListObject::validate() const { bool valid = true; pokemod()->validationMsg(QString("------Object with id %1---").arg(id()), Pokemod::V_Msg); if (Item == m_type) { if (pokemod()->itemIndex(m_object) == INT_MAX) { pokemod()->validationMsg("Invalid item"); valid = false; } } else if (Species == m_type) { if (pokemod()->speciesIndex(m_object) == INT_MAX) { pokemod()->validationMsg("Invalid Species"); valid = false; } } else { pokemod()->validationMsg("Invalid type"); valid = false; } if (!m_amount || ((1 < m_amount) && (Species == m_type))) { pokemod()->validationMsg("Invalid amount"); valid = false; } return valid; } void CoinListObject::load(const QString& fileName, int id) throw(Exception) { Ini ini(fileName); if (id == INT_MAX) ini.getValue("id", id); setId(id); ini.getValue("type", m_type, Item); ini.getValue("object", m_object); ini.getValue("amount", m_amount, 1); ini.getValue("cost", m_cost, 0); } void CoinListObject::save(const QString& coinList) const throw(Exception) { Ini ini; ini.addField("id", id()); ini.addField("type", m_type); ini.addField("object", m_object); ini.addField("amount", m_amount); ini.addField("cost", m_cost); ini.save(QString("%1/coinlist/%2/item/%3.pini").arg(pokemod()->path()).arg(coinList).arg(id())); } void CoinListObject::setType(const int type) throw(BoundsException) { if (End <= type) throw(BoundsException(className(), "type")); m_type = type; m_object = INT_MAX; } void CoinListObject::setObject(const int object) throw(BoundsException) { if (((Item == m_type) && (pokemod()->itemIndex(object) == INT_MAX)) || ((Species == m_type) && (pokemod()->speciesIndex(object) == INT_MAX))) throw(BoundsException(className(), "object")); m_object = object; } void CoinListObject::setAmount(const int amount) throw(BoundsException) { if (!amount || ((Species == m_type) && (1 < amount))) throw(BoundsException(className(), "amount")); m_amount = amount; } void CoinListObject::setCost(const int cost) { m_cost = cost; } int CoinListObject::type() const { return m_type; } int CoinListObject::object() const { return m_object; } int CoinListObject::amount() const { return m_amount; } int CoinListObject::cost() const { return m_cost; } CoinListObject& CoinListObject::operator=(const CoinListObject& rhs) { if (this == &rhs) return *this; m_type = rhs.m_type; m_object = rhs.m_object; m_amount = rhs.m_amount; m_cost = rhs.m_cost; return *this; }