/* * 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 "Item.h" #include "ItemEffect.h" #include "Map.h" #include "MapWildListEncounter.h" #include "Pokemod.h" // Qt includes #include const QStringList MapWildList::ControlStr = QStringList() << "Grass" << "Surfing" << "Fishing" << "Dive" << "Headbutt" << "Rock Smash"; MapWildList::MapWildList(const MapWildList& wildList) : Object("MapWildList", wildList.parent(), wildList.id()) { *this = wildList; } MapWildList::MapWildList(const Map* parent, const int id) : Object("MapWildList", parent, id), m_control(INT_MAX), m_value(INT_MAX), m_scope(INT_MAX) { } MapWildList::MapWildList(const MapWildList& wildList, const Map* parent, const int id) : Object("MapWildList", parent, id) { *this = wildList; } MapWildList::MapWildList(const QDomElement& xml, const Map* parent, const int id) : Object("MapWildList", parent, id) { load(xml, id); } MapWildList::~MapWildList() { clear(); } void MapWildList::validate() { TEST(setControl, control); TEST(setValue, value); TEST_LIST(setTime, time); TEST(setScope, scope); 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 MapWildList::load(const QDomElement& xml, int id) { LOAD_ID(); LOAD(int, control); LOAD(int, value); LOAD_LIST(int, time); LOAD(int, scope); LOAD_SUB(newEncounter, MapWildListEncounter); } QDomElement MapWildList::save() const { SAVE_CREATE(); SAVE(int, control); SAVE(int, value); SAVE_LIST(int, time); SAVE(int, scope); SAVE_SUB(MapWildListEncounter, encounters); return xml; } void MapWildList::setControl(const int control) { if (End <= control) { emit(error(bounds("control"))); return; } m_control = control; m_value = INT_MAX; emit(changed()); } void MapWildList::setValue(const int value) { if (m_control != Fishing) { emit(warning(unused("value"))); return; } for (int i = 0; (i < static_cast(pokemod())->itemCount()); ++i) { const Item* item = static_cast(pokemod())->item(i); for (int j = 0; (j < item->effectCount()); ++j) { const ItemEffect* effect = item->effect(j); if ((effect->effect() == ItemEffect::E_Fish) && (effect->value2() == value)) { m_value = value; emit(changed()); return; } } } emit(error(bounds("value"))); } void MapWildList::setTime(const int time, const bool state) { if (static_cast(pokemod())->timeIndex(time) == INT_MAX) { emit(error(bounds("time"))); return; } if (state) { if (!m_time.contains(time)) { m_time.append(time); emit(changed()); } } else { m_time.removeAll(time); emit(changed()); } } void MapWildList::setScope(const int scope) { if (scope != INT_MAX) { for (int i = 0; (i < static_cast(pokemod())->itemCount()); ++i) { const Item* item = static_cast(pokemod())->item(i); for (int j = 0; (j < item->effectCount()); ++j) { const ItemEffect* effect = item->effect(j); if ((effect->effect() == ItemEffect::E_Scope) && (effect->value2() == scope)) { m_scope = scope; emit(changed()); return; } } } emit(error(bounds("value"))); } else { m_scope = scope; emit(changed()); } } int MapWildList::control() const { return m_control; } int MapWildList::value() const { return m_value; } bool MapWildList::time(const int time) const { return m_time.contains(time); } int MapWildList::scope() const { return m_scope; } const MapWildListEncounter* MapWildList::encounter(const int index) const { if (encounterCount() <= index) return NULL; return m_encounters.at(index); } MapWildListEncounter* MapWildList::encounter(const int index) { if (encounterCount() <= index) return NULL; return m_encounters[index]; } const MapWildListEncounter* MapWildList::encounterById(const int id) const { return encounter(encounterIndex(id)); } MapWildListEncounter* MapWildList::encounterById(const int id) { return encounter(encounterIndex(id)); } int 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 MapWildList::encounterCount() const { return m_encounters.size(); } MapWildListEncounter* MapWildList::newEncounter() { return newEncounter(new MapWildListEncounter(this, newEncounterId())); } MapWildListEncounter* MapWildList::newEncounter(const QDomElement& xml) { return newEncounter(new MapWildListEncounter(xml, this, newEncounterId())); } MapWildListEncounter* MapWildList::newEncounter(const MapWildListEncounter& encounter) { return newEncounter(new MapWildListEncounter(encounter, this, newEncounterId())); } MapWildListEncounter* MapWildList::newEncounter(MapWildListEncounter* encounter) { m_encounters.append(encounter); return encounter; } void MapWildList::deleteEncounter(const int index) { if (encounterCount() <= index) return; delete m_encounters[index]; m_encounters.removeAt(index); } void MapWildList::deleteEncounterById(const int id) { deleteEncounter(encounterIndex(id)); } int MapWildList::newEncounterId() const { int i = 0; while ((i < encounterCount()) && (encounterIndex(i) != INT_MAX)) ++i; return i; } MapWildList& MapWildList::operator=(const MapWildList& rhs) { if (this == &rhs) return *this; clear(); COPY(control); COPY(value); COPY(time); COPY(scope); COPY_SUB(MapWildListEncounter, encounters); return *this; } void MapWildList::clear() { while (encounterCount()) deleteEncounter(0); }