/////////////////////////////////////////////////////////////////////////////
// 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 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 "Store.h"
PokeGen::PokeMod::Store::Store(const Pokemod* par, const unsigned _id) :
Object(_id, par),
name("")
{
}
PokeGen::PokeMod::Store::Store(const Pokemod* par, Ini& ini, const unsigned _id) :
Object(_id, par)
{
ImportIni(ini, _id);
}
bool PokeGen::PokeMod::Store::Validate()
{
pokemod->ValidationMsg(QString("---Store \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg);
if (name == "")
{
pokemod->ValidationMsg("Name is noe defiend");
isValid = false;
}
if (GetItemCount())
{
QMap idChecker;
for (QList::ConstIterator i = items.begin(); i != items.end(); ++i)
{
if (pokemod->GetItemByID(*i) == UINT_MAX)
{
pokemod->ValidationMsg("Invalid item");
isValid = false;
}
++idChecker[*i];
}
for (QMap::ConstIterator i = idChecker.begin(); i != idChecker.end(); ++i)
{
if (1 < i.value())
{
pokemod->ValidationMsg(QString("There are %1 items with id %2").arg(i.value()).arg(i.key()));
isValid = false;
}
}
}
else
{
pokemod->ValidationMsg("There are no items");
isValid = false;
}
return isValid;
}
void PokeGen::PokeMod::Store::ImportIni(Ini& ini, const unsigned _id)
{
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 PokeGen::PokeMod::Store::ExportIni(QFile& fout) const
{
Ini exStore("store");
exStore.AddField("id", id);
exStore.AddField("name", name);
exStore.AddField("numItems", items.size());
for (int i = 0; i < items.size(); ++i)
exStore.AddField(QString("items-%1").arg(i), items[i]);
exStore.Export(fout);
}
void PokeGen::PokeMod::Store::SetName(const QString& n)
{
name = n;
}
QString PokeGen::PokeMod::Store::GetName() const
{
return name;
}
unsigned PokeGen::PokeMod::Store::GetItem(const unsigned i) const
{
if (i < GetItemCount())
return items[i];
return UINT_MAX;
}
unsigned PokeGen::PokeMod::Store::GetItemByID(const unsigned _id) const
{
for (unsigned i = 0; i < GetItemCount(); ++i)
{
if (items[i] == _id)
return i;
}
return UINT_MAX;
}
unsigned PokeGen::PokeMod::Store::GetItemCount() const
{
return items.size();
}
bool PokeGen::PokeMod::Store::NewItem(const unsigned i)
{
if (pokemod->GetItemByID(i) != UINT_MAX)
{
items.append(i);
return true;
}
return false;
}
bool PokeGen::PokeMod::Store::DeleteItem(const unsigned i)
{
if (i < GetItemCount())
{
items.erase(items.begin() + i);
return true;
}
return false;
}