/* * Copyright 2007-2008 Ben Boeckel * * 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 . */ // Header include #include "MapWildList.h" // Pokemod includes #include "Map.h" #include "MapWildListEncounter.h" // Qt includes #include Pokemod::MapWildList::MapWildList(const MapWildList& wildList) : Object("MapWildList", wildList.parent(), wildList.id()) { *this = wildList; } Pokemod::MapWildList::MapWildList(const Map* parent, const int id) : Object("MapWildList", parent, id), m_name("") { } Pokemod::MapWildList::MapWildList(const MapWildList& wildList, const Map* parent, const int id) : Object("MapWildList", parent, id) { *this = wildList; } Pokemod::MapWildList::MapWildList(const QDomElement& xml, const Map* parent, const int id) : Object("MapWildList", parent, id) { load(xml, id); } Pokemod::MapWildList::~MapWildList() { clear(); } void Pokemod::MapWildList::validate() { if (m_name.isEmpty()) emit(error("Name is empty")); if (!encounterCount()) emit(error("There are no encounters")); QSet idChecker; foreach (MapWildListEncounter* encounter, m_encounters) { encounter->validate(); if (idChecker.contains(encounter->id())) emit(error(subclass("encounter", encounter->id()))); idChecker.insert(encounter->id()); } } void Pokemod::MapWildList::load(const QDomElement& xml, int id) { LOAD_ID(); LOAD(QString, name); LOAD_SUB(newEncounter, MapWildListEncounter); } QDomElement Pokemod::MapWildList::save() const { SAVE_CREATE(); SAVE(QString, name); SAVE_SUB(MapWildListEncounter, encounters); return xml; } void Pokemod::MapWildList::setName(const QString& name) { CHECK(name); } QString Pokemod::MapWildList::name() const { return m_name; } const Pokemod::MapWildListEncounter* Pokemod::MapWildList::encounter(const int index) const { Q_ASSERT(index < encounterCount()); return m_encounters.at(index); } Pokemod::MapWildListEncounter* Pokemod::MapWildList::encounter(const int index) { Q_ASSERT(index < encounterCount()); return m_encounters[index]; } const Pokemod::MapWildListEncounter* Pokemod::MapWildList::encounterById(const int id) const { return encounter(encounterIndex(id)); } Pokemod::MapWildListEncounter* Pokemod::MapWildList::encounterById(const int id) { return encounter(encounterIndex(id)); } int Pokemod::MapWildList::encounterIndex(const int id) const { for (int i = 0; i < encounterCount(); ++i) { if (m_encounters[i]->id() == id) return i; } return INT_MAX; } int Pokemod::MapWildList::encounterCount() const { return m_encounters.size(); } Pokemod::MapWildListEncounter* Pokemod::MapWildList::newEncounter() { return newEncounter(new MapWildListEncounter(this, newEncounterId())); } Pokemod::MapWildListEncounter* Pokemod::MapWildList::newEncounter(const QDomElement& xml) { return newEncounter(new MapWildListEncounter(xml, this, newEncounterId())); } Pokemod::MapWildListEncounter* Pokemod::MapWildList::newEncounter(const MapWildListEncounter& encounter) { return newEncounter(new MapWildListEncounter(encounter, this, newEncounterId())); } Pokemod::MapWildListEncounter* Pokemod::MapWildList::newEncounter(MapWildListEncounter* encounter) { m_encounters.append(encounter); return encounter; } void Pokemod::MapWildList::deleteEncounter(const int index) { Q_ASSERT(index < encounterCount()); delete m_encounters[index]; m_encounters.removeAt(index); } void Pokemod::MapWildList::deleteEncounterById(const int id) { deleteEncounter(encounterIndex(id)); } int Pokemod::MapWildList::newEncounterId() const { int i = 0; while ((i < encounterCount()) && (encounterIndex(i) != INT_MAX)) ++i; return i; } Pokemod::MapWildList& Pokemod::MapWildList::operator=(const MapWildList& rhs) { if (this == &rhs) return *this; clear(); COPY(name); COPY_SUB(MapWildListEncounter, encounters); return *this; } void Pokemod::MapWildList::clear() { while (encounterCount()) deleteEncounter(0); }