diff options
Diffstat (limited to 'pokemod/Map.cpp')
| -rw-r--r-- | pokemod/Map.cpp | 769 |
1 files changed, 367 insertions, 402 deletions
diff --git a/pokemod/Map.cpp b/pokemod/Map.cpp index e3adeff5..27f6f695 100644 --- a/pokemod/Map.cpp +++ b/pokemod/Map.cpp @@ -1,242 +1,183 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: pokemod/Map.cpp -// Purpose: Define a map for the game -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Mon May 28 21:45:48 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 <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// - +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +// Qt includes #include <QDir> #include <QMap> -#include <QMapIterator> -#include <QStringListIterator> +// Pokemod includes #include "Pokemod.h" #include "MapEffect.h" #include "MapTrainer.h" #include "MapWarp.h" #include "MapWildList.h" + +// Header include #include "Map.h" const QStringList Map::TypeStr = QStringList() << "Outdoor" << "Dungeon" << "Building"; -Map::Map(const Pokemod* par, const int _id) : - Object("Map", par, _id), - name(""), - flyWarp(-1), - type(-1) +Map::Map(const Pokemod* pokemod, const int id) : + Object("Map", pokemod, id), + m_name(""), + m_flyWarp(INT_MAX), + m_type(INT_MAX) { } -Map::Map(const Pokemod* par, const Map& m, const int _id) : - Object("Map", par, _id) +Map::Map(const Pokemod* pokemod, const Map& map, const int id) : + Object("Map", pokemod, id) { - *this = m; + *this = map; } -Map::Map(const Pokemod* par, const QString& fname, const int _id) : - Object("Map", par, _id) +Map::Map(const Pokemod* pokemod, const QString& fileName, const int id) : + Object("Map", pokemod, id) { - load(fname, _id); + load(fileName, id); } Map::~Map() { - for (QListIterator<MapEffect*> i(effects); i.hasNext(); ) - delete i.next(); - for (QListIterator<MapTrainer*> i(trainers); i.hasNext(); ) - delete i.next(); - for (QListIterator<MapWarp*> i(warps); i.hasNext(); ) - delete i.next(); - for (QListIterator<MapWildList*> i(wildLists); i.hasNext(); ) - delete i.next(); + foreach (MapEffect* effect, m_effects) + delete effect; + foreach (MapTrainer* trainer, m_trainers) + delete trainer; + foreach (MapWarp* warp, m_warps) + delete warp; + foreach (MapWildList* wildList, m_wildLists) + delete wildList; } bool Map::validate() const { bool valid = true; - pokemod->validationMsg(QString("---Map \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg); - if (name == "") + pokemod()->validationMsg(QString("---Map \"%1\" with id %2---").arg(m_name).arg(id()), Pokemod::V_Msg); + if (m_name == "") { - pokemod->validationMsg("name is not defined"); + pokemod()->validationMsg("name is not defined"); valid = false; } - if ((flyWarp != -1) && (getWarpIndex(flyWarp) == -1)) + if ((m_flyWarp != INT_MAX) && (warpIndex(m_flyWarp) == INT_MAX)) { - pokemod->validationMsg("Invalid fly destination warp"); + pokemod()->validationMsg("Invalid fly destination warp"); valid = false; } - if (type < End) + if (m_type < End) { - pokemod->validationMsg("Invalid type"); + pokemod()->validationMsg("Invalid type"); valid = false; } - QMap<int, int> idChecker; - QMap<QString, int> nameChecker; - if (getEffectCount()) + QMap<int, bool> idChecker; + if (effectCount()) { - for (QListIterator<MapEffect*> i(effects); i.hasNext(); i.next()) + foreach (MapEffect* effect, m_effects) { - if (!i.peekNext()->isValid()) - valid = false; - if (getWidth() <= i.peekNext()->getCoordinate().getX()) - { - pokemod->validationMsg("Invalid x coordinate"); - valid = false; - } - if (getHeight() <= i.peekNext()->getCoordinate().getY()) - { - pokemod->validationMsg("Invalid y coordinate"); + if (!effect->isValid()) valid = false; - } - ++idChecker[i.peekNext()->getId()]; - ++nameChecker[i.peekNext()->getName()]; - } - for (QMapIterator<int, int> i(idChecker); i.hasNext(); i.next()) - { - if (1 < i.value()) + if (idChecker[effect->id()]) + pokemod()->validationMsg(QString("Duplicate effect with id %1").arg(effect->id())); + idChecker[effect->id()] = true; + if (width() <= effect->coordinate().x()) { - pokemod->validationMsg(QString("There are %1 effects with id %2").arg(i.value()).arg(i.key())); + pokemod()->validationMsg("Invalid x coordinate"); valid = false; } - } - for (QMapIterator<QString, int> i(nameChecker); i.hasNext(); i.next()) - { - if (1 < i.value()) + if (height() <= effect->coordinate().y()) { - pokemod->validationMsg(QString("There are %1 effects with the name \"%2\"").arg(i.value()).arg(i.key())); + pokemod()->validationMsg("Invalid y coordinate"); valid = false; } } idChecker.clear(); - nameChecker.clear(); } else - pokemod->validationMsg("There are no effects", Pokemod::V_Warn); - if (getTrainerCount()) + pokemod()->validationMsg("There are no effects", Pokemod::V_Warn); + if (trainerCount()) { - for (QListIterator<MapTrainer*> i(trainers); i.hasNext(); i.next()) + foreach (MapTrainer* trainer, m_trainers) { - if (!i.peekNext()->isValid()) + if (!trainer->isValid()) valid = false; - if (getWidth() <= i.peekNext()->getCoordinate().getX()) + if (idChecker[trainer->id()]) + pokemod()->validationMsg(QString("Duplicate trainer with id %1").arg(trainer->id())); + idChecker[trainer->id()] = true; + if (width() <= trainer->coordinate().x()) { - pokemod->validationMsg("Invalid x coordinate"); + pokemod()->validationMsg("Invalid x coordinate"); valid = false; } - if (getHeight() <= i.peekNext()->getCoordinate().getY()) + if (height() <= trainer->coordinate().y()) { - pokemod->validationMsg("Invalid y coordinate"); - valid = false; - } - ++idChecker[i.peekNext()->getId()]; - ++nameChecker[i.peekNext()->getName()]; - } - for (QMapIterator<int, int> i(idChecker); i.hasNext(); i.next()) - { - if (1 < i.value()) - { - pokemod->validationMsg(QString("There are %1 trainers with id %2").arg(i.value()).arg(i.key())); - valid = false; - } - } - for (QMapIterator<QString, int> i(nameChecker); i.hasNext(); i.next()) - { - if (1 < i.value()) - { - pokemod->validationMsg(QString("There are %1 trainers with the name \"%2\"").arg(i.value()).arg(i.key())); + pokemod()->validationMsg("Invalid y coordinate"); valid = false; } } idChecker.clear(); - nameChecker.clear(); } else - pokemod->validationMsg("There are no trainers", Pokemod::V_Warn); - if (getWarpCount()) + pokemod()->validationMsg("There are no trainers", Pokemod::V_Warn); + if (warpCount()) { - for (QListIterator<MapWarp*> i(warps); i.hasNext(); i.next()) + foreach (MapWarp* warp, m_warps) { - if (!i.peekNext()->isValid()) - valid = false; - if (getWidth() <= i.peekNext()->getCoordinate().getX()) - { - pokemod->validationMsg("Invalid x coordinate"); - valid = false; - } - if (getHeight() <= i.peekNext()->getCoordinate().getY()) - { - pokemod->validationMsg("Invalid y coordinate"); + if (!warp->isValid()) valid = false; - } - ++idChecker[i.peekNext()->getId()]; - ++nameChecker[i.peekNext()->getName()]; - } - for (QMapIterator<int, int> i(idChecker); i.hasNext(); i.next()) - { - if (1 < i.value()) + if (idChecker[warp->id()]) + pokemod()->validationMsg(QString("Duplicate warp with id %1").arg(warp->id())); + idChecker[warp->id()] = true; + if (width() <= warp->coordinate().x()) { - pokemod->validationMsg(QString("There are %1 warps with id %2").arg(i.value()).arg(i.key())); + pokemod()->validationMsg("Invalid x coordinate"); valid = false; } - } - for (QMapIterator<QString, int> i(nameChecker); i.hasNext(); i.next()) - { - if (1 < i.value()) + if (height() <= warp->coordinate().y()) { - pokemod->validationMsg(QString("There are %1 warps with the name \"%2\"").arg(i.value()).arg(i.key())); + pokemod()->validationMsg("Invalid y coordinate"); valid = false; } } idChecker.clear(); - nameChecker.clear(); } else { - pokemod->validationMsg("There are no warps"); + pokemod()->validationMsg("There are no warps"); valid = false; } - if (getWildListCount()) + if (wildListCount()) { - for (QListIterator<MapWildList*> i(wildLists); i.hasNext(); i.next()) + foreach (MapWildList* wildList, m_wildLists) { - if (!i.peekNext()->isValid()) + if (!wildList->isValid()) valid = false; - ++idChecker[i.peekNext()->getId()]; + if (idChecker[wildList->id()]) + pokemod()->validationMsg(QString("Duplicate effect with id %1").arg(wildList->id())); + idChecker[wildList->id()] = true; } - for (QMapIterator<int, int> i(idChecker); i.hasNext(); i.next()) - { - if (1 < i.value()) - { - pokemod->validationMsg(QString("There are %1 wild lists with id %2").arg(i.value()).arg(i.key())); - valid = false; - } - } - idChecker.clear(); } else - pokemod->validationMsg("There are no effects", Pokemod::V_Warn); - for (int i = 0; i < getWidth(); ++i) + pokemod()->validationMsg("There are no effects", Pokemod::V_Warn); + for (int i = 0; i < width(); ++i) { - for (int j = 0; j < getHeight(); ++j) + for (int j = 0; j < height(); ++j) { - if (pokemod->getTileIndex(tiles(i, j)) == -1) + if (pokemod()->tileIndex(m_tiles(i, j)) == INT_MAX) { - pokemod->validationMsg(QString("Invalid tile at (%1, %2)").arg(i).arg(j)); + pokemod()->validationMsg(QString("Invalid tile at (%1, %2)").arg(i).arg(j)); valid = false; } } @@ -244,490 +185,514 @@ bool Map::validate() const return valid; } -int Map::getNewEffectId() const +void Map::load(const QString& fileName, int id) throw(Exception) { - int i = 0; - for (; (i < getEffectCount()) && (getEffectIndex(i) != -1); ++i) - ; - return i; -} - -int Map::getNewTrainerId() const -{ - int i = 0; - for (; (i < getTrainerCount()) && (getTrainerIndex(i) != -1); ++i) - ; - return i; -} - -int Map::getNewWarpId() const -{ - int i = 0; - for (; (i < getWarpCount()) && (getWarpIndex(i) != -1); ++i) - ; - return i; -} - -int Map::getNewWildListId() const -{ - int i = 0; - for (; (i < getWarpCount()) && (getWarpIndex(i) != -1); ++i) - ; - return i; -} - -void Map::load(const QString& fname, const int _id) throw(Exception) -{ - Ini ini(fname); - if (_id == -1) + Ini ini(fileName); + if (id == INT_MAX) ini.getValue("id", id); - else - id = _id; + setId(id); int i; int j; - ini.getValue("name", name); - ini.getValue("flyWarp", flyWarp); - ini.getValue("type", type); + ini.getValue("name", m_name); + ini.getValue("flyWarp", m_flyWarp); + ini.getValue("type", m_type); ini.getValue("width", i, 0); ini.getValue("height", j, 0); - tiles.resize(i, j, Frac(1, 1)); - for (int i = 0; i < getWidth(); ++i) + m_tiles.resize(i, j, Frac(1, 1)); + for (int i = 0; i < width(); ++i) { - for (int j = 0; j < getHeight(); ++j) - ini.getValue(QString("tiles-%1-%2").arg(i).arg(j), tiles(i, j)); + for (int j = 0; j < height(); ++j) + ini.getValue(QString("tiles-%1-%2").arg(i).arg(j), m_tiles(i, j)); } - QStringList path = pokemod->getPath().split('/'); + QStringList path = pokemod()->path().split('/'); path.removeLast(); QDir fdir(path.join("/")); - effects.clear(); + QStringList files; + m_effects.clear(); if (fdir.cd("effect")) { - for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); ) - newEffect(i.next()); + files = fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name); + foreach (QString file, files) + newEffect(file); fdir.cdUp(); } - trainers.clear(); + m_trainers.clear(); if (fdir.cd("trainer")) { - for (QStringListIterator i(fdir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)); i.hasNext(); ) - newTrainer(QString("%1/data.pini").arg(i.next())); + files = fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name); + foreach (QString file, files) + newTrainer(file); fdir.cdUp(); } - warps.clear(); + m_warps.clear(); if (fdir.cd("warp")) { - for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); ) - newWarp(i.next()); + files = fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name); + foreach (QString file, files) + newWarp(file); fdir.cdUp(); } - wildLists.clear(); + m_wildLists.clear(); if (fdir.cd("wildlist")) { - for (QStringListIterator i(fdir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)); i.hasNext(); ) - newWildList(QString("%1/data.pini").arg(i.next())); + files = fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name); + foreach (QString file, files) + newWildList(file); } } void Map::save() const throw(Exception) { Ini ini; - ini.addField("id", id); - ini.addField("name", name); - ini.addField("flyWarp", flyWarp); - ini.addField("type", type); - ini.addField("width", getWidth()); - ini.addField("height", getHeight()); - for (int i = 0; i < getWidth(); ++i) + ini.addField("id", id()); + ini.addField("name", m_name); + ini.addField("flyWarp", m_flyWarp); + ini.addField("type", m_type); + ini.addField("width", width()); + ini.addField("height", height()); + for (int i = 0; i < width(); ++i) { - for (int j = 0; j < getWidth(); ++j) - ini.addField(QString("tiles-%1-%2").arg(i).arg(j), tiles(i, j)); + for (int j = 0; j < width(); ++j) + ini.addField(QString("tiles-%1-%2").arg(i).arg(j), m_tiles(i, j)); } - ini.save(QString("%1/map/%2/data.pini").arg(pokemod->getPath()).arg(name)); - for (QListIterator<MapEffect*> i(effects); i.hasNext(); ) - i.next()->save(name); - for (QListIterator<MapTrainer*> i(trainers); i.hasNext(); ) - i.next()->save(name); - for (QListIterator<MapWarp*> i(warps); i.hasNext(); ) - i.next()->save(name); - for (QListIterator<MapWildList*> i(wildLists); i.hasNext(); ) - i.next()->save(name); + ini.save(QString("%1/map/%2/data.pini").arg(pokemod()->path()).arg(m_name)); + foreach (MapEffect* effect, m_effects) + effect->save(m_name); + foreach (MapTrainer* trainer, m_trainers) + trainer->save(m_name); + foreach (MapWarp* warp, m_warps) + warp->save(m_name); + foreach (MapWildList* wildList, m_wildLists) + wildList->save(m_name); } -void Map::setName(const QString& n) +void Map::setName(const QString& name) { - name = n; + m_name = name; } -void Map::setFlyWarp(const int f) throw(BoundsException) +void Map::setFlyWarp(const int warp) throw(BoundsException) { - if ((f != -1) && (getWarpIndex(f) == -1)) - throw(BoundsException(className, "warp")); - flyWarp = f; + if ((warp != INT_MAX) && (warpIndex(warp) == INT_MAX)) + throw(BoundsException(className(), "warp")); + m_flyWarp = warp; } -void Map::setType(const int t) throw(BoundsException) +void Map::setType(const int type) throw(BoundsException) { - if (End <= t) - throw(BoundsException(className, "type")); - type = t; + if (End <= type) + throw(BoundsException(className(), "type")); + m_type = type; } -QString Map::getName() const +QString Map::name() const { - return name; + return m_name; } -int Map::getFlyWarp() const +int Map::flyWarp() const { - return flyWarp; + return m_flyWarp; } -int Map::getType() const +int Map::type() const { - return type; + return m_type; } -void Map::setTile(int x, int y, int _id) throw(BoundsException) +void Map::setTile(int x, int y, int id) throw(BoundsException) { - if (pokemod->getTileIndex(_id) == -1) - throw(BoundsException(className, "tile")); - tiles(x, y) = _id; + if (pokemod()->tileIndex(id) == INT_MAX) + throw(BoundsException(className(), "tile")); + m_tiles(x, y) = id; } void Map::insertColumn(int x) { - tiles.insertCol(x, 0); + m_tiles.insertColumn(x, 0); } void Map::insertRow(int y) { - tiles.insertRow(y, 0); + m_tiles.insertRow(y, 0); } void Map::addColumn() { - tiles.addCol(0); + m_tiles.addColumn(0); } void Map::addRow() { - tiles.addRow(0); + m_tiles.addRow(0); } void Map::deleteColumn(int x) { - tiles.deleteCol(x); + m_tiles.deleteColumn(x); } void Map::deleteRow(int y) { - tiles.deleteRow(y); + m_tiles.deleteRow(y); } -const Matrix<int>* Map::getMap() const +const Matrix<int>* Map::map() const { - return &tiles; + return &m_tiles; } -Matrix<int>* Map::getMap() +Matrix<int>* Map::map() { - return &tiles; + return &m_tiles; } -int Map::getTile(int x, int y) const +int Map::tile(int x, int y) const { - return tiles(x, y); + return m_tiles(x, y); } -int Map::getWidth() const +int Map::width() const { - return tiles.getWidth(); + return m_tiles.width(); } -int Map::getHeight() const +int Map::height() const { - return tiles.getHeight(); + return m_tiles.height(); } -const MapEffect* Map::getEffect(const int i) const throw(IndexException) +const MapEffect* Map::effect(const int index) const throw(IndexException) { - if (getEffectCount() <= i) - throw(IndexException(className)); - return effects.at(i); + if (effectCount() <= index) + throw(IndexException(className())); + return m_effects.at(index); } -MapEffect* Map::getEffect(const int i) throw(IndexException) +MapEffect* Map::effect(const int index) throw(IndexException) { - if (getEffectCount() <= i) - throw(IndexException(className)); - return effects[i]; + if (effectCount() <= index) + throw(IndexException(className())); + return m_effects[index]; } -const MapEffect* Map::getEffectByID(const int i) const throw(IndexException) +const MapEffect* Map::effectById(const int index) const throw(IndexException) { - return getEffect(getEffectIndex(i)); + return effect(effectIndex(index)); } -MapEffect* Map::getEffectByID(const int i) throw(IndexException) +MapEffect* Map::effectById(const int index) throw(IndexException) { - return getEffect(getEffectIndex(i)); + return effect(effectIndex(index)); } -int Map::getEffectIndex(const int _id) const +int Map::effectIndex(const int id) const { - for (int i = 0; i < getEffectCount(); ++i) + for (int i = 0; i < effectCount(); ++i) { - if (effects[i]->getId() == _id) + if (m_effects[i]->id() == id) return i; } - return -1; + return INT_MAX; } -int Map::getEffectCount() const +int Map::effectCount() const { - return effects.size(); + return m_effects.size(); } MapEffect* Map::newEffect() { - effects.append(new MapEffect(pokemod, getNewEffectId())); - return effects[getEffectCount() - 1]; + m_effects.append(new MapEffect(pokemod(), newEffectId())); + return m_effects[effectCount() - 1]; +} + +MapEffect* Map::newEffect(const QString& fileName) +{ + m_effects.append(new MapEffect(pokemod(), fileName, newEffectId())); + return m_effects[effectCount() - 1]; } -MapEffect* Map::newEffect(const QString& fname) +MapEffect* Map::newEffect(const MapEffect& effect) { - effects.append(new MapEffect(pokemod, fname, getNewEffectId())); - return effects[getEffectCount() - 1]; + m_effects.append(new MapEffect(pokemod(), effect, newEffectId())); + return m_effects[effectCount() - 1]; } -MapEffect* Map::newEffect(const MapEffect& e) +void Map::deleteEffect(const int index) throw(IndexException) { - effects.append(new MapEffect(pokemod, e, getNewEffectId())); - return effects[getEffectCount() - 1]; + if (effectCount() <= index) + throw(IndexException(className())); + delete m_effects[index]; + m_effects.removeAt(index); } -void Map::deleteEffect(const int i) throw(IndexException) +void Map::deleteEffectById(const int id) throw(IndexException) { - if (getEffectCount() <= i) - throw(IndexException(className)); - delete effects[i]; - effects.removeAt(i); + deleteEffect(effectIndex(id)); } -const MapTrainer* Map::getTrainer(const int i) const throw(IndexException) +int Map::newEffectId() const { - if (getTrainerCount() <= i) - throw(IndexException(className)); - return trainers.at(i); + int i = 0; + while ((i < effectCount()) && (effectIndex(i) != INT_MAX)) + ++i; + return i; } -MapTrainer* Map::getTrainer(const int i) throw(IndexException) +const MapTrainer* Map::trainer(const int index) const throw(IndexException) { - if (getTrainerCount() <= i) - throw(IndexException(className)); - return trainers[i]; + if (trainerCount() <= index) + throw(IndexException(className())); + return m_trainers.at(index); } -const MapTrainer* Map::getTrainerByID(const int i) const throw(IndexException) +MapTrainer* Map::trainer(const int index) throw(IndexException) { - return getTrainer(getTrainerIndex(i)); + if (trainerCount() <= index) + throw(IndexException(className())); + return m_trainers[index]; } -MapTrainer* Map::getTrainerByID(const int i) throw(IndexException) +const MapTrainer* Map::trainerById(const int id) const throw(IndexException) { - return getTrainer(getTrainerIndex(i)); + return trainer(trainerIndex(id)); } -int Map::getTrainerIndex(const int _id) const +MapTrainer* Map::trainerById(const int id) throw(IndexException) { - for (int i = 0; i < getTrainerCount(); ++i) + return trainer(trainerIndex(id)); +} + +int Map::trainerIndex(const int id) const +{ + for (int i = 0; i < trainerCount(); ++i) { - if (trainers[i]->getId() == _id) + if (m_trainers[i]->id() == id) return i; } - return -1; + return INT_MAX; } -int Map::getTrainerCount() const +int Map::trainerCount() const { - return trainers.size(); + return m_trainers.size(); } MapTrainer* Map::newTrainer() { - trainers.append(new MapTrainer(pokemod, getNewTrainerId())); - return trainers[getTrainerCount() - 1]; + m_trainers.append(new MapTrainer(pokemod(), newTrainerId())); + return m_trainers[trainerCount() - 1]; } -MapTrainer* Map::newTrainer(const QString& fname) +MapTrainer* Map::newTrainer(const QString& fileName) { - trainers.append(new MapTrainer(pokemod, fname, getNewTrainerId())); - return trainers[getTrainerCount() - 1]; + m_trainers.append(new MapTrainer(pokemod(), fileName, newTrainerId())); + return m_trainers[trainerCount() - 1]; } -MapTrainer* Map::newTrainer(const MapTrainer& t) +MapTrainer* Map::newTrainer(const MapTrainer& trainer) { - trainers.append(new MapTrainer(pokemod, t, getNewTrainerId())); - return trainers[getTrainerCount() - 1]; + m_trainers.append(new MapTrainer(pokemod(), trainer, newTrainerId())); + return m_trainers[trainerCount() - 1]; } -void Map::deleteTrainer(const int i) throw(IndexException) +void Map::deleteTrainer(const int index) throw(IndexException) { - if (getTrainerCount() <= i) - throw(IndexException(className)); - delete trainers[i]; - trainers.removeAt(i); + if (trainerCount() <= index) + throw(IndexException(className())); + delete m_trainers[index]; + m_trainers.removeAt(index); } -const MapWarp* Map::getWarp(const int i) const throw(IndexException) +void Map::deleteTrainerById(const int id) throw(IndexException) { - if (getWarpCount() <= i) - throw(IndexException(className)); - return warps.at(i); + deleteTrainer(trainerIndex(id)); } -MapWarp* Map::getWarp(const int i) throw(IndexException) +int Map::newTrainerId() const { - if (getWarpCount() <= i) - throw(IndexException(className)); - return warps[i]; + int i = 0; + while ((i < trainerCount()) && (trainerIndex(i) != INT_MAX)) + ++i; + return i; } -const MapWarp* Map::getWarpByID(const int i) const throw(IndexException) +const MapWarp* Map::warp(const int index) const throw(IndexException) { - return getWarp(getWarpIndex(i)); + if (warpCount() <= index) + throw(IndexException(className())); + return m_warps.at(index); } -MapWarp* Map::getWarpByID(const int i) throw(IndexException) +MapWarp* Map::warp(const int index) throw(IndexException) { - return getWarp(getWarpIndex(i)); + if (warpCount() <= index) + throw(IndexException(className())); + return m_warps[index]; } -int Map::getWarpIndex(const int _id) const +const MapWarp* Map::warpById(const int id) const throw(IndexException) { - for (int i = 0; i < getWarpCount(); ++i) + return warp(warpIndex(id)); +} + +MapWarp* Map::warpById(const int id) throw(IndexException) +{ + return warp(warpIndex(id)); +} + +int Map::warpIndex(const int id) const +{ + for (int i = 0; i < warpCount(); ++i) { - if (warps[i]->getId() == _id) + if (m_warps[i]->id() == id) return i; } - return -1; + return INT_MAX; } -int Map::getWarpCount() const +int Map::warpCount() const { - return warps.size(); + return m_warps.size(); } MapWarp* Map::newWarp() { - warps.append(new MapWarp(pokemod, getNewWarpId())); - return warps[getWarpCount() - 1]; + m_warps.append(new MapWarp(pokemod(), newWarpId())); + return m_warps[warpCount() - 1]; +} + +MapWarp* Map::newWarp(const QString& fileName) +{ + m_warps.append(new MapWarp(pokemod(), fileName, newWarpId())); + return m_warps[warpCount() - 1]; +} + +MapWarp* Map::newWarp(const MapWarp& warp) +{ + m_warps.append(new MapWarp(pokemod(), warp, newWarpId())); + return m_warps[warpCount() - 1]; } -MapWarp* Map::newWarp(const QString& fname) +void Map::deleteWarp(const int index) throw(IndexException) { - warps.append(new MapWarp(pokemod, fname, getNewWarpId())); - return warps[getWarpCount() - 1]; + if (warpCount() <= index) + throw(IndexException(className())); + delete m_warps[index]; + m_warps.removeAt(index); } -MapWarp* Map::newWarp(const MapWarp& w) +void Map::deleteWarpById(const int id) throw(IndexException) { - warps.append(new MapWarp(pokemod, w, getNewWarpId())); - return warps[getWarpCount() - 1]; + deleteWarp(warpIndex(id)); } -void Map::deleteWarp(const int i) throw(IndexException) +int Map::newWarpId() const { - if (getWarpCount() <= i) - throw(IndexException(className)); - delete warps[i]; - warps.removeAt(i); + int i = 0; + while ((i < warpCount()) && (warpIndex(i) != INT_MAX)) + ++i; + return i; } -const MapWildList* Map::getWildList(const int i) const throw(IndexException) +const MapWildList* Map::wildList(const int index) const throw(IndexException) { - if (getWildListCount() <= i) - throw(IndexException(className)); - return wildLists.at(i); + if (wildListCount() <= index) + throw(IndexException(className())); + return m_wildLists.at(index); } -MapWildList* Map::getWildList(const int i) throw(IndexException) +MapWildList* Map::wildList(const int index) throw(IndexException) { - if (getWildListCount() <= i) - throw(IndexException(className)); - return wildLists[i]; + if (wildListCount() <= index) + throw(IndexException(className())); + return m_wildLists[index]; } -const MapWildList* Map::getWildListByID(const int i) const throw(IndexException) +const MapWildList* Map::wildListById(const int id) const throw(IndexException) { - return getWildList(getWildListIndex(i)); + return wildList(wildListIndex(id)); } -MapWildList* Map::getWildListByID(const int i) throw(IndexException) +MapWildList* Map::wildListById(const int id) throw(IndexException) { - return getWildList(getWildListIndex(i)); + return wildList(wildListIndex(id)); } -int Map::getWildListIndex(const int _id) const +int Map::wildListIndex(const int id) const { - for (int i = 0; i < getWildListCount(); ++i) + for (int i = 0; i < wildListCount(); ++i) { - if (wildLists[i]->getId() == _id) + if (m_wildLists[i]->id() == id) return i; } - return -1; + return INT_MAX; } -int Map::getWildListCount() const +int Map::wildListCount() const { - return wildLists.size(); + return m_wildLists.size(); } MapWildList* Map::newWildList() { - wildLists.append(new MapWildList(pokemod, getNewWildListId())); - return wildLists[getWildListCount() - 1]; + m_wildLists.append(new MapWildList(pokemod(), newWildListId())); + return m_wildLists[wildListCount() - 1]; +} + +MapWildList* Map::newWildList(const QString& fileName) +{ + m_wildLists.append(new MapWildList(pokemod(), fileName, newWildListId())); + return m_wildLists[wildListCount() - 1]; } -MapWildList* Map::newWildList(const QString& fname) +MapWildList* Map::newWildList(const MapWildList& wildList) { - wildLists.append(new MapWildList(pokemod, fname, getNewWildListId())); - return wildLists[getWildListCount() - 1]; + m_wildLists.append(new MapWildList(pokemod(), wildList, newWildListId())); + return m_wildLists[wildListCount() - 1]; } -MapWildList* Map::newWildList(const MapWildList& w) +void Map::deleteWildList(const int index) throw(IndexException) { - wildLists.append(new MapWildList(pokemod, w, getNewWildListId())); - return wildLists[getWildListCount() - 1]; + if (wildListCount() <= index) + throw(IndexException(className())); + delete m_wildLists[index]; + m_wildLists.removeAt(index); } -void Map::deleteWildList(const int i) throw(IndexException) +void Map::deleteWildListById(const int id) throw(IndexException) { - if (getWildListCount() <= i) - throw(IndexException(className)); - delete wildLists[i]; - wildLists.removeAt(i); + deleteWildList(wildListIndex(id)); +} + +int Map::newWildListId() const +{ + int i = 0; + while ((i < warpCount()) && (warpIndex(i) != INT_MAX)) + ++i; + return i; } Map& Map::operator=(const Map& rhs) { if (this == &rhs) return *this; - name = rhs.name; - flyWarp = rhs.flyWarp; - type = rhs.type; - tiles = rhs.tiles; - effects.clear(); - for (int i = 0; i < rhs.getEffectCount(); ++i) - effects.append(new MapEffect(pokemod, *rhs.getEffect(i), rhs.getEffect(i)->getId())); - trainers.clear(); - for (int i = 0; i < rhs.getTrainerCount(); ++i) - trainers.append(new MapTrainer(pokemod, *rhs.getTrainer(i), rhs.getTrainer(i)->getId())); - warps.clear(); - for (int i = 0; i < rhs.getWarpCount(); ++i) - warps.append(new MapWarp(pokemod, *rhs.getWarp(i), rhs.getWarp(i)->getId())); - wildLists.clear(); - for (int i = 0; i < rhs.getWildListCount(); ++i) - wildLists.append(new MapWildList(pokemod, *rhs.getWildList(i), rhs.getWildList(i)->getId())); + m_name = rhs.m_name; + m_flyWarp = rhs.m_flyWarp; + m_type = rhs.m_type; + m_tiles = rhs.m_tiles; + m_effects.clear(); + foreach (MapEffect* effect, rhs.m_effects) + m_effects.append(new MapEffect(pokemod(), *effect, effect->id())); + m_trainers.clear(); + foreach (MapTrainer* trainer, rhs.m_trainers) + m_trainers.append(new MapTrainer(pokemod(), *trainer, trainer->id())); + m_warps.clear(); + foreach (MapWarp* warp, rhs.m_warps) + m_warps.append(new MapWarp(pokemod(), *warp, warp->id())); + m_wildLists.clear(); + foreach (MapWildList* wildList, rhs.m_wildLists) + m_wildLists.append(new MapWildList(pokemod(), *wildList, wildList->id())); return *this; } |
