///////////////////////////////////////////////////////////////////////////// // Name: pokemod/Item.cpp // Purpose: Define an item // Author: Ben Boeckel // Modified by: Ben Boeckel // Created: Tue Mar 20 18: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 #include "Pokemod.h" #include "Item.h" Item::Item(const Pokemod* par, const int _id) : Object("Item", par, _id), name(""), sellable(false), type(-1), price(0), description("") { } Item::Item(const Pokemod* par, const Item& i, const int _id) : Object("Item", par, _id) { *this = i; } Item::Item(const Pokemod* par, const QString& fname, const int _id) : Object("Item", par, _id) { load(fname, _id); } bool Item::validate() const { bool valid = true; pokemod->validationMsg(QString("---Item \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg); if (name == "") { pokemod->validationMsg("Name is not defined"); valid = false; } if (pokemod->getItemTypeIndex(type) == -1) { pokemod->validationMsg("Invalid item type"); valid = false; } if (pokemod->getRules()->getMaxMoney() < price) { pokemod->validationMsg("Invalid price"); valid = false; } if (getEffectCount()) { QMap idChecker; for (QListIterator i(effects); i.hasNext(); i.next()) { if (!i.peekNext().isValid()) valid = false; ++idChecker[i.peekNext().getId()]; } for (QMapIterator i(idChecker); i.hasNext(); i.next()) { if (1 < i.value()) { pokemod->validationMsg(QString("There are %1 effects with id %2").arg(i.value()).arg(i.key())); valid = false; } } } else { pokemod->validationMsg("There are no effects"); valid = false; } return valid; } int Item::getNewId() const { int i = 0; for (; (i < getEffectCount()) && (getEffectIndex(i) != -1); ++i) ; return i; } void Item::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("sellable", sellable, false); ini.getValue("type", type); ini.getValue("price", price, 0); ini.getValue("description", description); QStringList path = pokemod->getPath().split('/'); path.removeLast(); QDir fdir(path.join("/")); effects.clear(); if (fdir.cd("effect")) { for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); ) newEffect(i.next()); } } void Item::save() const throw(Exception) { Ini ini; ini.addField("id", id); ini.addField("name", name); ini.addField("sellable", sellable); ini.addField("type", type); ini.addField("price", price); ini.addField("description", description); ini.save(QString("%1/item/%2/data.pini").arg(pokemod->getPath()).arg(name)); for (QListIterator i(effects); i.hasNext(); ) i.next().save(name); } void Item::setName(const QString& n) { name = n; } void Item::setSellable(const bool s) { sellable = s; } void Item::setType(const int t) throw(BoundsException) { if (pokemod->getItemTypeIndex(t) == -1) throw(BoundsException(className, "type")); type = t; } void Item::setPrice(const int p) throw(BoundsException) { if (pokemod->getRules()->getMaxMoney() < p) throw(BoundsException(className, "price")); price = p; } void Item::setDescription(const QString& d) { description = d; } QString Item::getName() const { return name; } bool Item::getSellable() const { return sellable; } int Item::getType() const { return type; } int Item::getPrice() const { return price; } QString Item::getDescription() const { return description; } const ItemEffect* Item::getEffect(const int i) const throw(IndexException) { if (getEffectCount() <= i) throw(IndexException(className)); return &effects.at(i); } ItemEffect* Item::getEffectByID(const int i) throw(IndexException) { if (getEffectCount() <= i) throw(IndexException(className)); return &effects[i]; } const ItemEffect* Item::getEffectByID(const int i) const throw(IndexException) { return getEffect(getEffectIndex(i)); } ItemEffect* Item::getEffect(const int i) throw(IndexException) { return getEffect(getEffectIndex(i)); } int Item::getEffectIndex(const int _id) const { for (int i = 0; i < getEffectCount(); ++i) { if (effects[i].getId() == _id) return i; } return -1; } int Item::getEffectCount() const { return effects.size(); } ItemEffect* Item::newEffect() { effects.append(ItemEffect(pokemod, getNewId())); return &effects[getEffectCount() - 1]; } ItemEffect* Item::newEffect(const QString& fname) { effects.append(ItemEffect(pokemod, fname, getNewId())); return &effects[getEffectCount() - 1]; } ItemEffect* Item::newEffect(const ItemEffect& e) { effects.append(ItemEffect(pokemod, e, getNewId())); return &effects[getEffectCount() - 1]; } void Item::deleteEffect(const int i) throw(IndexException) { if (getEffectCount() <= i) throw(IndexException(className)); effects.removeAt(i); } Item& Item::operator=(const Item& rhs) { if (this == &rhs) return *this; name = rhs.name; sellable = rhs.sellable; type = rhs.type; price = rhs.price; description = rhs.description; effects.clear(); for (int i = 0; i < rhs.getEffectCount(); ++i) effects.append(ItemEffect(pokemod, *rhs.getEffect(i), rhs.getEffect(i)->getId())); return *this; }