/////////////////////////////////////////////////////////////////////////////
// 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 "MapWildListEncounter.h"
#include "MapWildList.h"
const QStringList MapWildList::ControlStr = QStringList() << "Grass" << "Surfing" << "Fishing" << "Dive" << "Headbutt" << "Rock Smash";
MapWildList::MapWildList(const Pokemod* par, const int _id) :
Object("MapWildList", par, _id),
control(-1),
value(-1),
scope(-1)
{
}
MapWildList::MapWildList(const Pokemod* par, const MapWildList& w, const int _id) :
Object("MapWildList", par, _id)
{
*this = w;
}
MapWildList::MapWildList(const Pokemod* par, const QString& fname, const int _id) :
Object("MapWildList", par, _id)
{
load(fname, _id);
}
MapWildList::~MapWildList()
{
for (QListIterator i(encounters); i.hasNext(); )
delete i.next();
}
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 ok = false;
for (int i = 0; (i < pokemod->getItemCount()) && !ok; ++i)
{
const Item* it = pokemod->getItem(i);
for (int j = 0; (j < it->getEffectCount()) && !ok; ++j)
{
const ItemEffect* e = it->getEffect(j);
ok = ((e->getEffect() == ItemEffect::E_Fish) && (e->getVal2() == value));
}
}
if (!ok)
{
pokemod->validationMsg("Invalid fishing value");
valid = false;
}
}
QMap idChecker;
for (QListIterator i(times); i.hasNext(); i.next())
{
if (pokemod->getTimeIndex(i.peekNext()) == -1)
{
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 != -1)
{
bool ok = false;
for (int i = 0; (i < pokemod->getItemCount()) && !ok; ++i)
{
const Item* it = pokemod->getItem(i);
for (int j = 0; (j < it->getEffectCount()) && !ok; ++j)
{
const ItemEffect* e = it->getEffect(j);
ok = ((e->getEffect() == ItemEffect::E_Scope) && (e->getVal2() == scope));
}
}
if (!ok)
{
pokemod->validationMsg("Invalid scope");
valid = false;
}
}
// TODO: MapWildList Encounter validation
return valid;
}
int MapWildList::getNewId() const
{
int i = 0;
for (; (i < getEncounterCount()) && (getEncounterIndex(i) != -1); ++i)
;
return i;
}
void MapWildList::load(const QString& fname, const int _id) throw(Exception)
{
Ini ini(fname);
if (_id == -1)
ini.getValue("id", id);
else
id = _id;
int i;
int j;
times.clear();
ini.getValue("control", control);
ini.getValue("value", value);
ini.getValue("numTimes", i, 0);
for (int k = 0; k < i; ++k)
{
ini.getValue(QString("time-%1").arg(k), j);
if (k != -1)
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 int c) throw(BoundsException)
{
if (End <= c)
throw(BoundsException(className, "control"));
control = c;
value = -1;
}
void MapWildList::setValue(const int v) throw(Exception)
{
if (control != Fishing)
throw(UnusedException(className, "value"));
bool ok = false;
for (int i = 0; (i < pokemod->getItemCount()) && !ok; ++i)
{
const Item* it = pokemod->getItem(i);
for (int j = 0; (j < it->getEffectCount()) && !ok; ++j)
{
const ItemEffect* e = it->getEffect(j);
ok = ((e->getEffect() == ItemEffect::E_Fish) && (e->getVal2() == value));
}
}
if (!ok)
throw(BoundsException(className, "value"));
value = v;
}
void MapWildList::setTime(const int 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 int s) throw(BoundsException)
{
if (s != -1)
{
bool ok = false;
for (int i = 0; (i < pokemod->getItemCount()) && !ok; ++i)
{
const Item* it = pokemod->getItem(i);
for (int j = 0; (j < it->getEffectCount()) && !ok; ++j)
{
const ItemEffect* e = it->getEffect(j);
ok = ((e->getEffect() == ItemEffect::E_Scope) && (e->getVal2() == s));
}
}
if (!ok)
throw(BoundsException(className, "scope"));
}
scope = s;
}
int MapWildList::getControl() const
{
return control;
}
int MapWildList::getValue() const
{
return value;
}
bool MapWildList::getTime(const int 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 int i) const throw(IndexException)
{
if (getEncounterCount() <= i)
throw(IndexException(className));
return encounters.at(i);
}
MapWildListEncounter* MapWildList::getEncounter(const int i) throw(IndexException)
{
if (getEncounterCount() <= i)
throw(IndexException(className));
return encounters[i];
}
const MapWildListEncounter* MapWildList::getEncounterByID(const int i) const throw(IndexException)
{
return getEncounter(getEncounterIndex(i));
}
MapWildListEncounter* MapWildList::getEncounterByID(const int i) throw(IndexException)
{
return getEncounter(getEncounterIndex(i));
}
int MapWildList::getEncounterIndex(const int _id) const
{
for (int i = 0; i < getEncounterCount(); ++i)
{
if (encounters[i]->getId() == _id)
return i;
}
return -1;
}
int MapWildList::getEncounterCount() const
{
return encounters.size();
}
MapWildListEncounter* MapWildList::newEncounter()
{
encounters.append(new MapWildListEncounter(pokemod, getNewId()));
return encounters[getEncounterCount() - 1];
}
MapWildListEncounter* MapWildList::newEncounter(const QString& fname)
{
encounters.append(new MapWildListEncounter(pokemod, fname, getNewId()));
return encounters[getEncounterCount() - 1];
}
MapWildListEncounter* MapWildList::newEncounter(const MapWildListEncounter& p)
{
encounters.append(new MapWildListEncounter(pokemod, p, getNewId()));
return encounters[getEncounterCount() - 1];
}
void MapWildList::deleteEncounter(const int i) throw(IndexException)
{
if (getEncounterCount() <= i)
throw(IndexException(className));
delete encounters[i];
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 (int i = 0; i < rhs.getEncounterCount(); ++i)
encounters.append(new MapWildListEncounter(pokemod, *rhs.getEncounter(i), rhs.getEncounter(i)->getId()));
return *this;
}