/////////////////////////////////////////////////////////////////////////////
// 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 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 "Item.h"
PokeGen::PokeMod::Item::Item(const Pokemod* par, const unsigned _id) :
Object(_id, par),
name(""),
sellable(false),
type(UINT_MAX),
price(0),
description("")
{
}
PokeGen::PokeMod::Item::Item(const Pokemod* par, Ini& ini, const unsigned _id) :
Object(_id, par)
{
ImportIni(ini, _id);
}
bool PokeGen::PokeMod::Item::Validate()
{
pokemod->ValidationMsg(QString("---Item \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg);
if (name == "")
{
pokemod->ValidationMsg("Name is not defined");
isValid = false;
}
if (pokemod->GetItemTypeByID(type) == UINT_MAX)
{
pokemod->ValidationMsg("Invalid item type");
isValid = false;
}
if (GetEffectCount())
{
QMap idChecker;
for (QList::Iterator i = effects.begin(); i != effects.end(); ++i)
{
if (!i->IsValid())
isValid = false;
++idChecker[i->GetId()];
}
for (QMap::ConstIterator i = idChecker.begin(); i != idChecker.end(); ++i)
{
if (1 < i.value())
{
pokemod->ValidationMsg(QString("There are %1 effects with id %2").arg(i.value()).arg(i.key()));
isValid = false;
}
}
}
else
{
pokemod->ValidationMsg("There are no effects");
isValid = false;
}
return isValid;
}
void PokeGen::PokeMod::Item::ImportIni(Ini& ini, const unsigned _id)
{
if (_id == UINT_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);
effects.clear();
}
void PokeGen::PokeMod::Item::ExportIni(QFile& fout) const
{
Ini exItem("item");
exItem.AddField("id", id);
exItem.AddField("name", name);
exItem.AddField("sellable", sellable);
exItem.AddField("type", type);
exItem.AddField("price", price);
exItem.AddField("description", description);
exItem.Export(fout);
for (QList::ConstIterator i = effects.begin(); i != effects.end(); ++i)
i->ExportIni(fout, name);
}
void PokeGen::PokeMod::Item::SetName(const QString& n)
{
name = n;
}
void PokeGen::PokeMod::Item::SetSellable(const bool s)
{
sellable = s;
}
bool PokeGen::PokeMod::Item::SetType(const unsigned t)
{
if (pokemod->GetItemTypeByID(t) != UINT_MAX)
{
type = t;
return true;
}
return false;
}
void PokeGen::PokeMod::Item::SetPrice(const unsigned p)
{
price = p;
}
void PokeGen::PokeMod::Item::SetDescription(const QString& d)
{
description = d;
}
QString PokeGen::PokeMod::Item::GetName() const
{
return name;
}
bool PokeGen::PokeMod::Item::GetSellable() const
{
return sellable;
}
unsigned PokeGen::PokeMod::Item::GetType() const
{
return type;
}
unsigned PokeGen::PokeMod::Item::GetPrice() const
{
return price;
}
QString PokeGen::PokeMod::Item::GetDescription() const
{
return description;
}
const PokeGen::PokeMod::ItemEffect* PokeGen::PokeMod::Item::GetEffect(const unsigned i) const
{
if (i < GetEffectCount())
return &effects[i];
return NULL;
}
unsigned PokeGen::PokeMod::Item::GetEffectByID(const unsigned _id) const
{
for (unsigned i = 0; i < GetEffectCount(); ++i)
{
if (effects[i].GetId() == _id)
return i;
}
return UINT_MAX;
}
unsigned PokeGen::PokeMod::Item::GetEffectCount() const
{
return effects.size();
}
const PokeGen::PokeMod::ItemEffect* PokeGen::PokeMod::Item::NewEffect(Ini* const ini)
{
unsigned i = 0;
for (; (i < GetEffectCount()) && (GetEffectByID(i) != UINT_MAX); ++i)
;
ItemEffect newEffect(pokemod, i);
if (ini)
newEffect.ImportIni(*ini);
effects.append(newEffect);
return &effects[GetEffectCount() - 1];
}
bool PokeGen::PokeMod::Item::DeleteEffect(const unsigned i)
{
if (i < GetEffectCount())
{
effects.erase(effects.begin() + i);
return true;
}
return false;
}