///////////////////////////////////////////////////////////////////////////// // 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 . ///////////////////////////////////////////////////////////////////////////// #include #include #include #include "Pokemod.h" #include "Store.h" Store::Store(const Pokemod& par, const unsigned _id) : Object(par, _id), name("") { } Store::Store(const Pokemod& par, const Store& s, const unsigned _id) : Object(par, _id) { *this = s; } Store::Store(const Pokemod& par, const QString& fname, const unsigned _id) : Object(par, _id) { load(fname, _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("Name is noe defiend"); valid = false; } if (!items.size()) { pokemod.validationMsg("There are no items"); valid = false; } QMap nameChecker; for (QListIterator i(items); i.hasNext(); i.next()) { if (pokemod.getItemIndex(i.peekNext()) == UINT_MAX) { pokemod.validationMsg("Invalid item"); valid = false; } ++nameChecker[i.peekNext()]; } for (QMapIterator i(nameChecker); i.hasNext(); i.next()) { if (1 < i.value()) { pokemod.validationMsg(QString("There are %1 of item %2").arg(i.value()).arg(i.key())); valid = false; } } return valid; } void Store::load(const QString& fname, const unsigned _id) throw(Exception) { Ini ini(fname); if (_id == UINT_MAX) ini.getValue("id", id); else id = _id; items.clear(); unsigned i; unsigned j; ini.getValue("name", name); ini.getValue("numItems", i, 0); for (unsigned k = 0; k < i; ++k) { ini.getValue(QString("item-%1").arg(k), j, UINT_MAX); if (j != UINT_MAX) 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 (unsigned i = 0; i < unsigned(items.size()); ++i) ini.addField(QString("items-%1").arg(i), items[i]); ini.save(QString("%1/store/%2.pini").arg(pokemod.getPath()).arg(name)); } void Store::setName(const QString& n) { name = n; } void Store::setItem(const unsigned itm, const bool it) throw(Exception) { if (pokemod.getItemIndex(itm) == UINT_MAX) throw(BoundsException("Store", "item")); for (QMutableListIterator i(items); i.hasNext(); ) { if (i.next() == itm) { if (it) throw(Exception("Store", "item already used")); else i.remove(); } } if (!it) throw(Exception("Store", "item wasn\'t being used anyway")); items.append(itm); } QString Store::getName() const { return name; } bool Store::getItem(const unsigned itm) const { for (QListIterator i(items); i.hasNext(); ) { if (i.next() == itm) return true; } return false; } Store& Store::operator=(const Store& rhs) { if (this == &rhs) return *this; name = rhs.name; items = rhs.items; return *this; }