summaryrefslogtreecommitdiffstats
path: root/pokemod/Item.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'pokemod/Item.cpp')
-rw-r--r--pokemod/Item.cpp303
1 files changed, 149 insertions, 154 deletions
diff --git a/pokemod/Item.cpp b/pokemod/Item.cpp
index f2a594d6..91d0ef3f 100644
--- a/pokemod/Item.cpp
+++ b/pokemod/Item.cpp
@@ -1,284 +1,279 @@
-/////////////////////////////////////////////////////////////////////////////
-// 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 <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/>.
+ */
+// Qt includes
#include <QDir>
-#include <QListIterator>
#include <QMap>
-#include <QMapIterator>
#include <QStringList>
-#include <QStringListIterator>
+// Pokemod includes
#include "Pokemod.h"
#include "ItemEffect.h"
+
+// Header include
#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* pokemod, const int id) :
+ Object("Item", pokemod, id),
+ m_name(""),
+ m_sellable(false),
+ m_type(INT_MAX),
+ m_price(0),
+ m_description("")
{
}
-Item::Item(const Pokemod* par, const Item& i, const int _id) :
- Object("Item", par, _id)
+Item::Item(const Pokemod* pokemod, const Item& item, const int id) :
+ Object("Item", pokemod, id)
{
- *this = i;
+ *this = item;
}
-Item::Item(const Pokemod* par, const QString& fname, const int _id) :
- Object("Item", par, _id)
+Item::Item(const Pokemod* pokemod, const QString& fileName, const int id) :
+ Object("Item", pokemod, id)
{
- load(fname, _id);
+ load(fileName, id);
}
Item::~Item()
{
- for (QListIterator<ItemEffect*> i(effects); i.hasNext(); )
- delete i.next();
+ foreach (ItemEffect* effect, m_effects)
+ delete effect;
}
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(QString("---Item \"%1\" with id %2---").arg(m_name).arg(id()), Pokemod::V_Msg);
+ if (m_name == "")
{
- pokemod->validationMsg("Name is not defined");
+ pokemod()->validationMsg("Name is not defined");
valid = false;
}
- if (pokemod->getItemTypeIndex(type) == -1)
+ if (pokemod()->itemTypeIndex(m_type) == INT_MAX)
{
- pokemod->validationMsg("Invalid item type");
+ pokemod()->validationMsg("Invalid item type");
valid = false;
}
- if (pokemod->getRules()->getMaxMoney() < price)
+ if (pokemod()->rules()->maxMoney() < m_price)
{
- pokemod->validationMsg("Invalid price");
+ pokemod()->validationMsg("Invalid price");
valid = false;
}
- if (getEffectCount())
+ if (effectCount())
{
- QMap<int, int> idChecker;
- for (QListIterator<ItemEffect*> i(effects); i.hasNext(); i.next())
+ QMap<int, bool> idChecker;
+ foreach (ItemEffect* effect, m_effects)
{
- if (!i.peekNext()->isValid())
+ if (!effect->isValid())
valid = false;
- ++idChecker[i.peekNext()->getId()];
- }
- for (QMapIterator<int, int> 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;
- }
+ if (idChecker[effect->id()])
+ pokemod()->validationMsg(QString("Duplicate effect with id %1").arg(effect->id()));
+ idChecker[effect->id()] = true;
}
}
else
{
- pokemod->validationMsg("There are no effects");
+ 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)
+void Item::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("sellable", sellable, false);
- ini.getValue("type", type);
- ini.getValue("price", price, 0);
- ini.getValue("description", description);
- QStringList path = pokemod->getPath().split('/');
+ setId(id);
+ ini.getValue("name", m_name);
+ ini.getValue("sellable", m_sellable, false);
+ ini.getValue("type", m_type);
+ ini.getValue("price", m_price, 0);
+ ini.getValue("description", m_description);
+ QStringList path = pokemod()->path().split('/');
path.removeLast();
QDir fdir(path.join("/"));
- effects.clear();
+ m_effects.clear();
if (fdir.cd("effect"))
{
- for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); )
- newEffect(i.next());
+ QStringList files(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name));
+ foreach (QString file, files)
+ newEffect(file);
}
}
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<ItemEffect*> i(effects); i.hasNext(); )
- i.next()->save(name);
+ ini.addField("id", id());
+ ini.addField("name", m_name);
+ ini.addField("sellable", m_sellable);
+ ini.addField("type", m_type);
+ ini.addField("price", m_price);
+ ini.addField("description", m_description);
+ ini.save(QString("%1/item/%2/data.pini").arg(pokemod()->path()).arg(m_name));
+ foreach (ItemEffect* effect, m_effects)
+ effect->save(m_name);
}
-void Item::setName(const QString& n)
+void Item::setName(const QString& name)
{
- name = n;
+ m_name = name;
}
-void Item::setSellable(const bool s)
+void Item::setSellable(const bool sellable)
{
- sellable = s;
+ m_sellable = sellable;
}
-void Item::setType(const int t) throw(BoundsException)
+void Item::setType(const int type) throw(BoundsException)
{
- if (pokemod->getItemTypeIndex(t) == -1)
- throw(BoundsException(className, "type"));
- type = t;
+ if (pokemod()->itemTypeIndex(type) == INT_MAX)
+ throw(BoundsException(className(), "type"));
+ m_type = type;
}
-void Item::setPrice(const int p) throw(BoundsException)
+void Item::setPrice(const int price) throw(BoundsException)
{
- if (pokemod->getRules()->getMaxMoney() < p)
- throw(BoundsException(className, "price"));
- price = p;
+ if (pokemod()->rules()->maxMoney() < price)
+ throw(BoundsException(className(), "price"));
+ m_price = price;
}
-void Item::setDescription(const QString& d)
+void Item::setDescription(const QString& description)
{
- description = d;
+ m_description = description;
}
-QString Item::getName() const
+QString Item::name() const
{
- return name;
+ return m_name;
}
-bool Item::getSellable() const
+bool Item::sellable() const
{
- return sellable;
+ return m_sellable;
}
-int Item::getType() const
+int Item::type() const
{
- return type;
+ return m_type;
}
-int Item::getPrice() const
+int Item::price() const
{
- return price;
+ return m_price;
}
-QString Item::getDescription() const
+QString Item::description() const
{
- return description;
+ return m_description;
}
-const ItemEffect* Item::getEffect(const int i) const throw(IndexException)
+const ItemEffect* Item::effect(const int index) const throw(IndexException)
{
- if (getEffectCount() <= i)
- throw(IndexException(className));
- return effects.at(i);
+ if (effectCount() <= index)
+ throw(IndexException(className()));
+ return m_effects.at(index);
}
-ItemEffect* Item::getEffectByID(const int i) throw(IndexException)
+ItemEffect* Item::effect(const int index) throw(IndexException)
{
- if (getEffectCount() <= i)
- throw(IndexException(className));
- return effects[i];
+ if (effectCount() <= index)
+ throw(IndexException(className()));
+ return m_effects[index];
}
-const ItemEffect* Item::getEffectByID(const int i) const throw(IndexException)
+const ItemEffect* Item::effectById(const int id) const throw(IndexException)
{
- return getEffect(getEffectIndex(i));
+ return effect(effectIndex(id));
}
-ItemEffect* Item::getEffect(const int i) throw(IndexException)
+ItemEffect* Item::effectById(const int id) throw(IndexException)
{
- return getEffect(getEffectIndex(i));
+ return effect(effectIndex(id));
}
-int Item::getEffectIndex(const int _id) const
+int Item::effectIndex(const int id) const
{
- for (int i = 0; i < getEffectCount(); ++i)
+ for (int i = 0; i < effectCount(); ++i)
{
- if (effects[i]->getId() == _id)
+ if (m_effects[i]->id() == id)
return i;
}
- return -1;
+ return INT_MAX;
}
-int Item::getEffectCount() const
+int Item::effectCount() const
{
- return effects.size();
+ return m_effects.size();
}
ItemEffect* Item::newEffect()
{
- effects.append(new ItemEffect(pokemod, getNewId()));
- return effects[getEffectCount() - 1];
+ m_effects.append(new ItemEffect(pokemod(), effectId()));
+ return m_effects[effectCount() - 1];
}
-ItemEffect* Item::newEffect(const QString& fname)
+ItemEffect* Item::newEffect(const QString& fileName)
{
- effects.append(new ItemEffect(pokemod, fname, getNewId()));
- return effects[getEffectCount() - 1];
+ m_effects.append(new ItemEffect(pokemod(), fileName, effectId()));
+ return m_effects[effectCount() - 1];
}
-ItemEffect* Item::newEffect(const ItemEffect& e)
+ItemEffect* Item::newEffect(const ItemEffect& effect)
{
- effects.append(new ItemEffect(pokemod, e, getNewId()));
- return effects[getEffectCount() - 1];
+ m_effects.append(new ItemEffect(pokemod(), effect, effectId()));
+ return m_effects[effectCount() - 1];
}
-void Item::deleteEffect(const int i) throw(IndexException)
+void Item::deleteEffect(const int index) throw(IndexException)
{
- if (getEffectCount() <= i)
- throw(IndexException(className));
- delete effects[i];
- effects.removeAt(i);
+ if (effectCount() <= index)
+ throw(IndexException(className()));
+ delete m_effects[index];
+ m_effects.removeAt(index);
+}
+
+void Item::deleteEffectById(const int id) throw(IndexException)
+{
+ deleteEffect(effectIndex(id));
+}
+
+int Item::effectId() const
+{
+ int i = 0;
+ while ((i < effectCount()) && (effectIndex(i) != INT_MAX))
+ ++i;
+ return 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(new ItemEffect(pokemod, *rhs.getEffect(i), rhs.getEffect(i)->getId()));
+ m_name = rhs.m_name;
+ m_sellable = rhs.m_sellable;
+ m_type = rhs.m_type;
+ m_price = rhs.m_price;
+ m_description = rhs.m_description;
+ m_effects.clear();
+ foreach (ItemEffect* effect, rhs.m_effects)
+ m_effects.append(new ItemEffect(pokemod(), *effect, effect->id()));
return *this;
}