/////////////////////////////////////////////////////////////////////////////
// Name: pokemod/MapWildList.cpp
// Purpose: Define a list of species that can be found on the map
// Author: Ben Boeckel
// Modified by: Ben Boeckel
// Created: Tue Mar 20 19:18:23 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"
#include "ItemEffect.h"
#include "MapWildList.h"
const QStringList MapWildList::ControlStr = QStringList() << "Grass" << "Surfing" << "Fishing" << "Dive" << "Headbutt" << "Rock Smash";
MapWildList::MapWildList(const Pokemod& par, const unsigned _id) :
Object("MapWildList", par, _id),
control(UINT_MAX),
value(INT_MAX),
scope(INT_MAX)
{
}
MapWildList::MapWildList(const Pokemod& par, const MapWildList& w, const unsigned _id) :
Object("MapWildList", par, _id)
{
*this = w;
}
MapWildList::MapWildList(const Pokemod& par, const QString& fname, const unsigned _id) :
Object("MapWildList", par, _id)
{
load(fname, _id);
}
bool MapWildList::validate() const
{
bool valid = true;
pokemod.validationMsg(QString("------Wild List with id %1---").arg(id), Pokemod::V_Msg);
if (End <= control)
{
pokemod.validationMsg("Invalid control");
valid = false;
}
else if (control == Fishing)
{
bool temp = false;
for (unsigned i = 0; (i < pokemod.getItemCount()) && !temp; ++i)
{
for (unsigned j = 0; (j < pokemod.getItem(i).getEffectCount()) && !temp; ++j)
{
if ((pokemod.getItem(i).getEffect(j).getEffect() == ItemEffect::E_Fish) && (unsigned(pokemod.getItem(i).getEffect(j).getVal2()) == value))
temp = true;
}
}
if (!temp)
{
pokemod.validationMsg("Invalid fishing value");
valid = false;
}
}
QMap idChecker;
for (QListIterator i(times); i.hasNext(); i.next())
{
if (pokemod.getTimeIndex(i.peekNext()) == UINT_MAX)
{
pokemod.validationMsg("Invalid time");
valid = false;
}
++idChecker[i.peekNext()];
}
for (QMapIterator i(idChecker); i.hasNext(); i.next())
{
if (1 < i.value())
{
pokemod.validationMsg(QString("There are %1 of time %2").arg(i.value()).arg(i.key()));
valid = false;
}
}
if (scope != UINT_MAX)
{
bool temp = false;
for (unsigned i = 0; (i < pokemod.getItemCount()) && !temp; ++i)
{
for (unsigned j = 0; (j < pokemod.getItem(i).getEffectCount()) && !temp; ++j)
{
if ((pokemod.getItem(i).getEffect(j).getEffect() == ItemEffect::E_Scope) && (pokemod.getItem(i).getEffect(j).getVal2() == scope))
temp = true;
}
}
if (!temp)
{
pokemod.validationMsg("Invalid scope");
valid = false;
}
}
return valid;
}
unsigned MapWildList::getNewId() const
{
unsigned i = 0;
for (; (i < getEncounterCount()) && (getEncounterIndex(i) != UINT_MAX); ++i)
;
return i;
}
void MapWildList::load(const QString& fname, const unsigned _id) throw(Exception)
{
Ini ini(fname);
if (_id == UINT_MAX)
ini.getValue("id", id);
else
id = _id;
unsigned i;
unsigned j;
times.clear();
ini.getValue("control", control);
ini.getValue("value", value);
ini.getValue("numTimes", i, 0);
for (unsigned k = 0; k < i; ++k)
{
ini.getValue(QString("time-%1").arg(k), j, UINT_MAX);
if (k != UINT_MAX)
times.append(j);
}
ini.getValue("scope", scope);
QStringList path = pokemod.getPath().split('/');
path.removeLast();
QDir fdir(path.join("/"));
encounters.clear();
if (fdir.cd("encounter"))
{
for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); )
newEncounter(i.next());
}
}
void MapWildList::save(const QString& map) const throw(Exception)
{
Ini ini;
ini.addField("id", id);
ini.addField("control", control);
ini.addField("value", value);
ini.addField("numTimes", times.size());
for (int i = 0; i < times.size(); ++i)
ini.addField(QString("time-%1").arg(i), times[i]);
ini.addField("scope", scope);
ini.save(QString("%1/map/%2/wildlist/%3/data.pini").arg(pokemod.getPath()).arg(map).arg(id));
for (QListIterator i(encounters); i.hasNext(); )
i.next().save(map, id);
}
void MapWildList::setControl(const unsigned c) throw(BoundsException)
{
if (End <= c)
throw(BoundsException(className, "control"));
control = c;
}
void MapWildList::setValue(const unsigned v)
{
value = v;
}
void MapWildList::setTime(const unsigned ts, const bool t) throw(Exception)
{
for (QMutableListIterator i(times); i.hasNext(); )
{
if (i.next() == ts)
{
if (t)
throw(Exception(className, "time already used"));
else
i.remove();
}
}
if (!t)
throw(Exception(className, "time wasn\'t being used anyway"));
times.append(ts);
}
void MapWildList::setScope(const unsigned s)
{
scope = s;
}
unsigned MapWildList::getControl() const
{
return control;
}
int MapWildList::getValue() const
{
return value;
}
bool MapWildList::getTime(const unsigned ts) const
{
for (QListIterator i(times); i.hasNext(); )
{
if (i.next() == ts)
return true;
}
return false;
}
int MapWildList::getScope() const
{
return scope;
}
const MapWildListEncounter& MapWildList::getEncounter(const unsigned i) const throw(IndexException)
{
if (getEncounterCount() <= i)
throw(IndexException(className));
return encounters.at(i);
}
MapWildListEncounter& MapWildList::getEncounter(const unsigned i) throw(IndexException)
{
if (getEncounterCount() <= i)
throw(IndexException(className));
return encounters[i];
}
const MapWildListEncounter& MapWildList::getEncounterByID(const unsigned i) const throw(IndexException)
{
return getEncounter(getEncounterIndex(i));
}
MapWildListEncounter& MapWildList::getEncounterByID(const unsigned i) throw(IndexException)
{
return getEncounter(getEncounterIndex(i));
}
unsigned MapWildList::getEncounterIndex(const unsigned _id) const
{
for (unsigned i = 0; i < getEncounterCount(); ++i)
{
if (encounters[i].getId() == _id)
return i;
}
return UINT_MAX;
}
unsigned MapWildList::getEncounterCount() const
{
return encounters.size();
}
MapWildListEncounter& MapWildList::newEncounter()
{
encounters.append(MapWildListEncounter(pokemod, getNewId()));
return encounters[getEncounterCount() - 1];
}
MapWildListEncounter& MapWildList::newEncounter(const QString& fname)
{
encounters.append(MapWildListEncounter(pokemod, fname, getNewId()));
return encounters[getEncounterCount() - 1];
}
MapWildListEncounter& MapWildList::newEncounter(const MapWildListEncounter& p)
{
encounters.append(MapWildListEncounter(pokemod, p, getNewId()));
return encounters[getEncounterCount() - 1];
}
void MapWildList::deleteEncounter(const unsigned i) throw(IndexException)
{
if (getEncounterCount() <= i)
throw(IndexException(className));
encounters.removeAt(i);
}
MapWildList& MapWildList::operator=(const MapWildList& rhs)
{
if (this == &rhs)
return *this;
control = rhs.control;
value = rhs.value;
times = rhs.times;
scope = rhs.scope;
encounters.clear();
for (unsigned i = 0; i < rhs.getEncounterCount(); ++i)
newEncounter(rhs.getEncounter(i));
return *this;
}