///////////////////////////////////////////////////////////////////////////// // 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 int _id) : Object("Store", par, _id), name("") { } Store::Store(const Pokemod* par, const Store& s, const int _id) : Object("Store", par, _id) { *this = s; } Store::Store(const Pokemod* par, const QString& fname, const int _id) : Object("Store", 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()) == -1) { 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 int _id) throw(Exception) { Ini ini(fname); if (_id == -1) ini.getValue("id", id); else id = _id; items.clear(); int i; int j; ini.getValue("name", 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); } } 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)); } void Store::setName(const QString& n) { name = n; } void Store::setItem(const int itm, const bool it) throw(Exception) { if (pokemod->getItemIndex(itm) == -1) throw(BoundsException(className, "item")); for (QMutableListIterator i(items); i.hasNext(); ) { if (i.next() == itm) { if (!it) i.remove(); return; } } items.append(itm); } QString Store::getName() const { return name; } bool Store::getItem(const int 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; }