From 9a65bc6bb7c8da1dfa5b101579e52845c75848ef Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 31 Mar 2008 01:17:59 +0000 Subject: [FIX] Member variables now use m_ prefix [FIX] Lots of minor fixes git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@95 6ecfd1a5-f3ed-3746-8530-beee90d26b22 --- pokemod/CoinList.cpp | 293 ++++++++++++++++++++++++--------------------------- 1 file changed, 140 insertions(+), 153 deletions(-) (limited to 'pokemod/CoinList.cpp') diff --git a/pokemod/CoinList.cpp b/pokemod/CoinList.cpp index 53002fa0..c0e83a59 100644 --- a/pokemod/CoinList.cpp +++ b/pokemod/CoinList.cpp @@ -1,280 +1,267 @@ -///////////////////////////////////////////////////////////////////////////// -// 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 . -///////////////////////////////////////////////////////////////////////////// +/* + * 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 -#include #include #include -#include +// Pokemod includes #include "Pokemod.h" #include "CoinListObject.h" #include "Item.h" #include "ItemEffect.h" + +// Header include #include "CoinList.h" -CoinList::CoinList(const Pokemod* par, const int _id) : - Object("CoinList", par, _id), - name(""), - value(0) +CoinList::CoinList(const Pokemod* pokemod, const int id) : + Object("CoinList", pokemod, id), + m_name(""), + m_value(0) { } -CoinList::CoinList(const Pokemod* par, const CoinList& c, const int _id) : - Object("CoinList", par, _id) +CoinList::CoinList(const Pokemod* pokemod, const CoinList& coinList, const int id) : + Object("CoinList", pokemod, id) { - *this = c; + *this = coinList; } -CoinList::CoinList(const Pokemod* par, const QString& fname, const int _id) : - Object("CoinList", par, _id) +CoinList::CoinList(const Pokemod* pokemod, const QString& fileName, const int id) : + Object("CoinList", pokemod, id) { - load(fname, _id); + load(fileName, id); } CoinList::~CoinList() { - for (QListIterator i(items); i.hasNext(); ) - delete i.next(); + foreach (CoinListObject* object, m_objects) + delete object; } 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(QString("---Coin List \"%1\" with id %2---").arg(m_name).arg(id()), Pokemod::V_Msg); + if (m_name == "") { - pokemod->validationMsg("Name not defined"); + pokemod()->validationMsg("Name not defined"); valid = false; } bool ok = false; - for (int i = 0; (i < pokemod->getItemCount()) && !ok; ++i) + for (int i = 0; (i < pokemod()->itemCount()) && !ok; ++i) { - const Item* it = pokemod->getItem(i); - for (int j = 0; (j < it->getEffectCount()) && !ok; ++j) + const Item* item = pokemod()->item(i); + for (int j = 0; (j < item->effectCount()) && !ok; ++j) { - const ItemEffect* e = it->getEffect(j); - if (e->getEffect() == ItemEffect::E_CoinCase) - ok = (e->getVal1() == value); + const ItemEffect* effect = item->effect(j); + if (effect->effect() == ItemEffect::E_CoinCase) + ok = (effect->value1() == m_value); } } if (!ok) { - pokemod->validationMsg("No coin cases which hold the right kind of coin"); + pokemod()->validationMsg("No coin cases which hold the right kind of coin"); valid = false; } - if (getItemCount()) + if (objectCount()) { - QMap idChecker; - QMap itemChecker; - QMap speciesChecker; - for (QListIterator i(items); i.hasNext(); i.next()) + QMap idChecker; + QMap itemChecker; + QMap speciesChecker; + foreach (CoinListObject* object, m_objects) { - if (!i.peekNext()->isValid()) + if (!object->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()) + if (idChecker[object->id()]) + pokemod()->validationMsg(QString("Duplicate object with id %1").arg(object->id())); + idChecker[object->id()] = true; + if (object->type() == CoinListObject::Item) { - pokemod->validationMsg(QString("There are %1 objects with id %2").arg(i.value()).arg(i.key())); - valid = false; + if (itemChecker[object->object()]) + pokemod()->validationMsg(QString("Duplicate object with item %1").arg(object->id())); + itemChecker[object->object()] = true; } - } - for (QMapIterator i(itemChecker); i.hasNext(); i.next()) - { - if (1 < i.value()) + else if (object->type() == CoinListObject::Species) { - 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; + if (speciesChecker[object->object()]) + pokemod()->validationMsg(QString("Duplicate object with item %1").arg(object->id())); + speciesChecker[object->object()] = true; } } } else { - pokemod->validationMsg("There are no objects"); + 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) +void CoinList::load(const QString& fileName, int id) throw(Exception) { - Ini ini(fname); - if (_id == -1) + Ini ini(fileName); + if (id == INT_MAX) ini.getValue("id", id); - else - id = _id; - ini.getValue("name", name); - ini.getValue("value", value, 0); - QStringList path = pokemod->getPath().split('/'); + setId(id); + ini.getValue("name", m_name); + ini.getValue("value", m_value, 0); + QStringList path = pokemod()->path().split('/'); path.removeLast(); QDir fdir(path.join("/")); - items.clear(); + m_objects.clear(); if (fdir.cd("item")) { - for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); ) - newItem(i.next()); + QStringList files(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); + foreach (QString file, files) + newObject(file); } } 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); + ini.addField("id", id()); + ini.addField("name", m_name); + ini.addField("value", m_value); + ini.save(QString("%1/coinlist/%2/data.pini").arg(pokemod()->path()).arg(m_name)); + foreach (CoinListObject* object, m_objects) + object->save(m_name); } -void CoinList::setName(const QString& n) +void CoinList::setName(const QString& name) { - name = n; + m_name = name; } -void CoinList::setValue(const int v) throw(Exception) +void CoinList::setValue(const int value) throw(Exception) { - for (int i = 0; (i < pokemod->getItemCount()); ++i) + for (int i = 0; (i < pokemod()->itemCount()); ++i) { - const Item* it = pokemod->getItem(i); - for (int j = 0; (j < it->getEffectCount()); ++j) + const Item* item = pokemod()->item(i); + for (int j = 0; (j < item->effectCount()); ++j) { - const ItemEffect* e = it->getEffect(j); - if ((e->getEffect() == ItemEffect::E_CoinCase) && (e->getVal1() == v)) + const ItemEffect* effect = item->effect(j); + if ((effect->effect() == ItemEffect::E_CoinCase) && (effect->value1() == value)) { - value = v; + m_value = value; return; } } } - throw(Exception(className, "no compatable coin cases")); + throw(Exception(className(), "No compatable coin cases")); } -QString CoinList::getName() const +QString CoinList::name() const { - return name; + return m_name; } -int CoinList::getValue() const +int CoinList::value() const { - return value; + return m_value; } -const CoinListObject* CoinList::getItem(const int i) const throw(IndexException) +const CoinListObject* CoinList::object(const int index) const throw(IndexException) { - if (getItemCount() <= i) - throw(IndexException(className)); - return items.at(i); + if (objectCount() <= index) + throw(IndexException(className())); + return m_objects.at(index); } -CoinListObject* CoinList::getItem(const int i) throw(IndexException) +CoinListObject* CoinList::object(const int index) throw(IndexException) { - if (getItemCount() <= i) - throw(IndexException(className)); - return items[i]; + if (objectCount() <= index) + throw(IndexException(className())); + return m_objects[index]; } -const CoinListObject* CoinList::getItemByID(const int i) const throw(IndexException) +const CoinListObject* CoinList::objectById(const int id) const throw(IndexException) { - return getItem(getItemIndex(i)); + return object(objectIndex(id)); } -CoinListObject* CoinList::getItemByID(const int i) throw(IndexException) +CoinListObject* CoinList::objectById(const int id) throw(IndexException) { - return getItem(getItemIndex(i)); + return object(objectIndex(id)); } -int CoinList::getItemIndex(const int _id) const +int CoinList::objectIndex(const int id) const { - for (int i = 0; i < getItemCount(); ++i) + for (int i = 0; i < objectCount(); ++i) { - if (items[i]->getId() == _id) + if (m_objects[i]->id() == id) return i; } - return -1; + return INT_MAX; } -int CoinList::getItemCount() const +int CoinList::objectCount() const { - return items.size(); + return m_objects.size(); } -CoinListObject* CoinList::newItem() +CoinListObject* CoinList::newObject() { - items.append(new CoinListObject(pokemod, getNewId())); - return items[getItemCount() - 1]; + m_objects.append(new CoinListObject(pokemod(), objectId())); + return m_objects[objectCount() - 1]; } -CoinListObject* CoinList::newItem(const QString& fname) +CoinListObject* CoinList::newObject(const QString& fileName) { - items.append(new CoinListObject(pokemod, fname, getNewId())); - return items[getItemCount() - 1]; + m_objects.append(new CoinListObject(pokemod(), fileName, objectId())); + return m_objects[objectCount() - 1]; } -CoinListObject* CoinList::newItem(const CoinListObject& o) +CoinListObject* CoinList::newObject(const CoinListObject& object) { - items.append(new CoinListObject(pokemod, o, getNewId())); - return items[getItemCount() - 1]; + m_objects.append(new CoinListObject(pokemod(), object, objectId())); + return m_objects[objectCount() - 1]; } -void CoinList::deleteItem(const int i) throw(IndexException) +void CoinList::deleteObject(const int index) throw(IndexException) { - if (getItemCount() <= i) - throw(IndexException(className)); - delete items[i]; - items.removeAt(i); + if (objectCount() <= index) + throw(IndexException(className())); + delete m_objects[index]; + m_objects.removeAt(index); +} + +void CoinList::deleteObjectById(const int id) throw( IndexException ) +{ + deleteObject(objectIndex(id)); +} + +int CoinList::objectId() const +{ + int i = 0; + while ((i < objectCount()) && (objectIndex(i) != INT_MAX)) + ++i; + return 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())); + m_name = rhs.m_name; + m_objects.clear(); + foreach (CoinListObject* object, rhs.m_objects) + m_objects.append(new CoinListObject(pokemod(), *object, object->id())); return *this; } -- cgit