diff options
Diffstat (limited to 'pokemod/Store.cpp')
| -rw-r--r-- | pokemod/Store.cpp | 162 |
1 files changed, 72 insertions, 90 deletions
diff --git a/pokemod/Store.cpp b/pokemod/Store.cpp index f715ef9b..94def03a 100644 --- a/pokemod/Store.cpp +++ b/pokemod/Store.cpp @@ -1,157 +1,139 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: pokemod/Store.cpp -// Purpose: Define a store that the player can shop at -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Tue Mar 20 18:31:50 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 <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 <http://www.gnu.org/licenses/>. + */ -#include <QListIterator> +// Qt includes #include <QMap> -#include <QMapIterator> +// Pokemod includes #include "Pokemod.h" + +// Header include #include "Store.h" -Store::Store(const Pokemod* par, const int _id) : - Object("Store", par, _id), - name("") +Store::Store(const Pokemod* pokemod, const int id) : + Object("Store", pokemod, id), + m_name("") { } -Store::Store(const Pokemod* par, const Store& s, const int _id) : - Object("Store", par, _id) +Store::Store(const Pokemod* pokemod, const Store& store, const int id) : + Object("Store", pokemod, id) { - *this = s; + *this = store; } -Store::Store(const Pokemod* par, const QString& fname, const int _id) : - Object("Store", par, _id) +Store::Store(const Pokemod* pokemod, const QString& fileName, const int id) : + Object("Store", pokemod, id) { - load(fname, _id); + load(fileName, id); } bool Store::validate() const { bool valid = true; - pokemod->validationMsg(QString("---Store \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg); - if (name == "") + pokemod()->validationMsg(QString("---Store \"%1\" with id %2---").arg(m_name).arg(id()), Pokemod::V_Msg); + if (m_name == "") { - pokemod->validationMsg("Name is noe defiend"); + pokemod()->validationMsg("Name is noe defiend"); valid = false; } - if (!items.size()) + if (!m_items.size()) { - pokemod->validationMsg("There are no items"); + pokemod()->validationMsg("There are no items"); valid = false; } - QMap<int, int> nameChecker; - for (QListIterator<int> i(items); i.hasNext(); i.next()) - { - if (pokemod->getItemIndex(i.peekNext()) == -1) - { - pokemod->validationMsg("Invalid item"); - valid = false; - } - ++nameChecker[i.peekNext()]; - } - for (QMapIterator<int, int> i(nameChecker); i.hasNext(); i.next()) + QMap<int, bool> valueChecker; + foreach (int item, m_items) { - if (1 < i.value()) + if (pokemod()->itemIndex(item) == INT_MAX) { - pokemod->validationMsg(QString("There are %1 of item %2").arg(i.value()).arg(i.key())); + pokemod()->validationMsg("Invalid item"); valid = false; } + if (valueChecker[item]) + pokemod()->validationMsg(QString("Duplicate of item %1").arg(item)); + valueChecker[item] = true; } return valid; } -void Store::load(const QString& fname, const int _id) throw(Exception) +void Store::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; - items.clear(); + setId(id); + m_items.clear(); int i; int j; - ini.getValue("name", name); + ini.getValue("name", m_name); ini.getValue("numItems", i, 0); for (int k = 0; k < i; ++k) { - ini.getValue(QString("item-%1").arg(k), j, -1); - if (j != -1) - items.append(j); + ini.getValue(QString("item-%1").arg(k), j); + if (!m_items.contains(j)) + m_items.append(j); } } void Store::save() const throw(Exception) { Ini ini; - ini.addField("id", id); - ini.addField("name", name); - ini.addField("numItems", items.size()); - for (int i = 0; i < items.size(); ++i) - ini.addField(QString("items-%1").arg(i), items[i]); - ini.save(QString("%1/store/%2.pini").arg(pokemod->getPath()).arg(name)); + ini.addField("id", id()); + ini.addField("name", m_name); + ini.addField("numItems", m_items.size()); + for (int i = 0; i < m_items.size(); ++i) + ini.addField(QString("items-%1").arg(i), m_items[i]); + ini.save(QString("%1/store/%2.pini").arg(pokemod()->path()).arg(m_name)); } -void Store::setName(const QString& n) +void Store::setName(const QString& name) { - name = n; + m_name = name; } -void Store::setItem(const int itm, const bool it) throw(Exception) +void Store::setItem(const int item, const bool state) throw(BoundsException) { - if (pokemod->getItemIndex(itm) == -1) - throw(BoundsException(className, "item")); - for (QMutableListIterator<int> i(items); i.hasNext(); ) + if (pokemod()->itemIndex(item) == INT_MAX) + throw(BoundsException(className(), "item")); + if (state) { - if (i.next() == itm) - { - if (!it) - i.remove(); - return; - } + if (!m_items.contains(item)) + m_items.append(item); } - items.append(itm); + else + m_items.removeAll(item); } -QString Store::getName() const +QString Store::name() const { - return name; + return m_name; } -bool Store::getItem(const int itm) const +bool Store::item(const int item) const { - for (QListIterator<int> i(items); i.hasNext(); ) - { - if (i.next() == itm) - return true; - } - return false; + return m_items.contains(item); } Store& Store::operator=(const Store& rhs) { if (this == &rhs) return *this; - name = rhs.name; - items = rhs.items; + m_name = rhs.m_name; + m_items = rhs.m_items; return *this; } |
