///////////////////////////////////////////////////////////////////////////// // Name: pokemod/CoinList.cpp // Purpose: A specialty store where Pokémon and items can be gotten for // coins won through gambling // Author: Ben Boeckel // Modified by: Ben Boeckel // Created: Wed Feb 28 21:16:29 2007 // Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions // Licence: // 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 #include #include #include #include #include "Pokemod.h" #include "CoinListObject.h" #include "Item.h" #include "ItemEffect.h" #include "CoinList.h" CoinList::CoinList(const Pokemod* par, const int _id) : Object("CoinList", par, _id), name(""), value(0) { } CoinList::CoinList(const Pokemod* par, const CoinList& c, const int _id) : Object("CoinList", par, _id) { *this = c; } CoinList::CoinList(const Pokemod* par, const QString& fname, const int _id) : Object("CoinList", par, _id) { load(fname, _id); } CoinList::~CoinList() { for (QListIterator i(items); i.hasNext(); ) delete i.next(); } bool CoinList::validate() const { bool valid = true; pokemod->validationMsg(QString("---Coin List \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg); if (name == "") { pokemod->validationMsg("Name not defined"); valid = false; } bool ok = false; for (int i = 0; (i < pokemod->getItemCount()) && !ok; ++i) { const Item* it = pokemod->getItem(i); for (int j = 0; (j < it->getEffectCount()) && !ok; ++j) { const ItemEffect* e = it->getEffect(j); if (e->getEffect() == ItemEffect::E_CoinCase) ok = (e->getVal1() == value); } } if (!ok) { pokemod->validationMsg("No coin cases which hold the right kind of coin"); valid = false; } if (getItemCount()) { QMap idChecker; QMap itemChecker; QMap speciesChecker; for (QListIterator i(items); i.hasNext(); i.next()) { if (!i.peekNext()->isValid()) valid = false; ++idChecker[i.peekNext()->getId()]; if (i.peekNext()->getType() == CoinListObject::Item) ++itemChecker[i.peekNext()->getObject()]; else if (i.peekNext()->getType() == CoinListObject::Species) ++speciesChecker[i.peekNext()->getObject()]; } for (QMapIterator i(idChecker); i.hasNext(); i.next()) { if (1 < i.value()) { pokemod->validationMsg(QString("There are %1 objects with id %2").arg(i.value()).arg(i.key())); valid = false; } } for (QMapIterator i(itemChecker); i.hasNext(); i.next()) { if (1 < i.value()) { pokemod->validationMsg(QString("There are %1 items with the id %2").arg(i.value()).arg(i.key())); valid = false; } } for (QMapIterator i(speciesChecker); i.hasNext(); i.next()) { if (1 < i.value()) { pokemod->validationMsg(QString("There are %1 Pokémon with the id %2").arg(i.value()).arg(i.key())); valid = false; } } } else { pokemod->validationMsg("There are no objects"); valid = false; } return valid; } int CoinList::getNewId() const { int i = 0; for (; (i < getItemCount()) && (getItemIndex(i) != -1); ++i) ; return i; } void CoinList::load(const QString& fname, const int _id) throw(Exception) { Ini ini(fname); if (_id == -1) ini.getValue("id", id); else id = _id; ini.getValue("name", name); ini.getValue("value", value, 0); QStringList path = pokemod->getPath().split('/'); path.removeLast(); QDir fdir(path.join("/")); items.clear(); if (fdir.cd("item")) { for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); ) newItem(i.next()); } } void CoinList::save() const throw(Exception) { Ini ini; ini.addField("id", id); ini.addField("name", name); ini.addField("value", value); ini.save(QString("%1/coinlist/%2/data.pini").arg(pokemod->getPath()).arg(name)); for (QListIterator i(items); i.hasNext(); ) i.next()->save(name); } void CoinList::setName(const QString& n) { name = n; } void CoinList::setValue(const int v) throw(Exception) { for (int i = 0; (i < pokemod->getItemCount()); ++i) { const Item* it = pokemod->getItem(i); for (int j = 0; (j < it->getEffectCount()); ++j) { const ItemEffect* e = it->getEffect(j); if ((e->getEffect() == ItemEffect::E_CoinCase) && (e->getVal1() == v)) { value = v; return; } } } throw(Exception(className, "no compatable coin cases")); } QString CoinList::getName() const { return name; } int CoinList::getValue() const { return value; } const CoinListObject* CoinList::getItem(const int i) const throw(IndexException) { if (getItemCount() <= i) throw(IndexException(className)); return items.at(i); } CoinListObject* CoinList::getItem(const int i) throw(IndexException) { if (getItemCount() <= i) throw(IndexException(className)); return items[i]; } const CoinListObject* CoinList::getItemByID(const int i) const throw(IndexException) { return getItem(getItemIndex(i)); } CoinListObject* CoinList::getItemByID(const int i) throw(IndexException) { return getItem(getItemIndex(i)); } int CoinList::getItemIndex(const int _id) const { for (int i = 0; i < getItemCount(); ++i) { if (items[i]->getId() == _id) return i; } return -1; } int CoinList::getItemCount() const { return items.size(); } CoinListObject* CoinList::newItem() { items.append(new CoinListObject(pokemod, getNewId())); return items[getItemCount() - 1]; } CoinListObject* CoinList::newItem(const QString& fname) { items.append(new CoinListObject(pokemod, fname, getNewId())); return items[getItemCount() - 1]; } CoinListObject* CoinList::newItem(const CoinListObject& o) { items.append(new CoinListObject(pokemod, o, getNewId())); return items[getItemCount() - 1]; } void CoinList::deleteItem(const int i) throw(IndexException) { if (getItemCount() <= i) throw(IndexException(className)); delete items[i]; items.removeAt(i); } CoinList& CoinList::operator=(const CoinList& rhs) { if (this == &rhs) return *this; name = rhs.name; items.clear(); for (int i = 0; i < rhs.getItemCount(); ++i) items.append(new CoinListObject(pokemod, *rhs.getItem(i), rhs.getItem(i)->getId())); return *this; }