/* * 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 . */ // Qt includes #include #include // 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* pokemod, const int id) : Object("Map", pokemod, id), m_name(""), m_flyWarp(INT_MAX), m_type(INT_MAX) { } Map::Map(const Pokemod* pokemod, const Map& map, const int id) : Object("Map", pokemod, id) { *this = map; } Map::Map(const Pokemod* pokemod, const QString& fileName, const int id) : Object("Map", pokemod, id) { load(fileName, id); } Map::~Map() { 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(m_name).arg(id()), Pokemod::V_Msg); if (m_name == "") { pokemod()->validationMsg("name is not defined"); valid = false; } if ((m_flyWarp != INT_MAX) && (warpIndex(m_flyWarp) == INT_MAX)) { pokemod()->validationMsg("Invalid fly destination warp"); valid = false; } if (m_type < End) { pokemod()->validationMsg("Invalid type"); valid = false; } QMap idChecker; if (effectCount()) { foreach (MapEffect* effect, m_effects) { if (!effect->isValid()) valid = false; 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("Invalid x coordinate"); valid = false; } if (height() <= effect->coordinate().y()) { pokemod()->validationMsg("Invalid y coordinate"); valid = false; } } idChecker.clear(); } else pokemod()->validationMsg("There are no effects", Pokemod::V_Warn); if (trainerCount()) { foreach (MapTrainer* trainer, m_trainers) { if (!trainer->isValid()) valid = false; 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"); valid = false; } if (height() <= trainer->coordinate().y()) { pokemod()->validationMsg("Invalid y coordinate"); valid = false; } } idChecker.clear(); } else pokemod()->validationMsg("There are no trainers", Pokemod::V_Warn); if (warpCount()) { foreach (MapWarp* warp, m_warps) { if (!warp->isValid()) valid = false; 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("Invalid x coordinate"); valid = false; } if (height() <= warp->coordinate().y()) { pokemod()->validationMsg("Invalid y coordinate"); valid = false; } } idChecker.clear(); } else { pokemod()->validationMsg("There are no warps"); valid = false; } if (wildListCount()) { foreach (MapWildList* wildList, m_wildLists) { if (!wildList->isValid()) valid = false; if (idChecker[wildList->id()]) pokemod()->validationMsg(QString("Duplicate effect with id %1").arg(wildList->id())); idChecker[wildList->id()] = true; } } else pokemod()->validationMsg("There are no effects", Pokemod::V_Warn); for (int i = 0; i < width(); ++i) { for (int j = 0; j < height(); ++j) { if (pokemod()->tileIndex(m_tiles(i, j)) == INT_MAX) { pokemod()->validationMsg(QString("Invalid tile at (%1, %2)").arg(i).arg(j)); valid = false; } } } return valid; } void Map::load(const QString& fileName, int id) throw(Exception) { Ini ini(fileName); if (id == INT_MAX) ini.getValue("id", id); setId(id); int i; int j; 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); m_tiles.resize(i, j, Frac(1, 1)); for (int i = 0; i < width(); ++i) { for (int j = 0; j < height(); ++j) ini.getValue(QString("tiles-%1-%2").arg(i).arg(j), m_tiles(i, j)); } QStringList path = pokemod()->path().split('/'); path.removeLast(); QDir fdir(path.join("/")); QStringList files; m_effects.clear(); if (fdir.cd("effect")) { files = fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name); foreach (QString file, files) newEffect(file); fdir.cdUp(); } m_trainers.clear(); if (fdir.cd("trainer")) { files = fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name); foreach (QString file, files) newTrainer(file); fdir.cdUp(); } m_warps.clear(); if (fdir.cd("warp")) { files = fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name); foreach (QString file, files) newWarp(file); fdir.cdUp(); } m_wildLists.clear(); if (fdir.cd("wildlist")) { 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", 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 < 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()->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& name) { m_name = name; } void Map::setFlyWarp(const int warp) throw(BoundsException) { if ((warp != INT_MAX) && (warpIndex(warp) == INT_MAX)) throw(BoundsException(className(), "warp")); m_flyWarp = warp; } void Map::setType(const int type) throw(BoundsException) { if (End <= type) throw(BoundsException(className(), "type")); m_type = type; } QString Map::name() const { return m_name; } int Map::flyWarp() const { return m_flyWarp; } int Map::type() const { return m_type; } void Map::setTile(int x, int y, int id) throw(BoundsException) { if (pokemod()->tileIndex(id) == INT_MAX) throw(BoundsException(className(), "tile")); m_tiles(x, y) = id; } void Map::insertColumn(int x) { m_tiles.insertColumn(x, 0); } void Map::insertRow(int y) { m_tiles.insertRow(y, 0); } void Map::addColumn() { m_tiles.addColumn(0); } void Map::addRow() { m_tiles.addRow(0); } void Map::deleteColumn(int x) { m_tiles.deleteColumn(x); } void Map::deleteRow(int y) { m_tiles.deleteRow(y); } const Matrix* Map::map() const { return &m_tiles; } Matrix* Map::map() { return &m_tiles; } int Map::tile(int x, int y) const { return m_tiles(x, y); } int Map::width() const { return m_tiles.width(); } int Map::height() const { return m_tiles.height(); } const MapEffect* Map::effect(const int index) const throw(IndexException) { if (effectCount() <= index) throw(IndexException(className())); return m_effects.at(index); } MapEffect* Map::effect(const int index) throw(IndexException) { if (effectCount() <= index) throw(IndexException(className())); return m_effects[index]; } const MapEffect* Map::effectById(const int index) const throw(IndexException) { return effect(effectIndex(index)); } MapEffect* Map::effectById(const int index) throw(IndexException) { return effect(effectIndex(index)); } int Map::effectIndex(const int id) const { for (int i = 0; i < effectCount(); ++i) { if (m_effects[i]->id() == id) return i; } return INT_MAX; } int Map::effectCount() const { return m_effects.size(); } MapEffect* Map::newEffect() { 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 MapEffect& effect) { m_effects.append(new MapEffect(pokemod(), effect, newEffectId())); return m_effects[effectCount() - 1]; } void Map::deleteEffect(const int index) throw(IndexException) { if (effectCount() <= index) throw(IndexException(className())); delete m_effects[index]; m_effects.removeAt(index); } void Map::deleteEffectById(const int id) throw(IndexException) { deleteEffect(effectIndex(id)); } int Map::newEffectId() const { int i = 0; while ((i < effectCount()) && (effectIndex(i) != INT_MAX)) ++i; return i; } const MapTrainer* Map::trainer(const int index) const throw(IndexException) { if (trainerCount() <= index) throw(IndexException(className())); return m_trainers.at(index); } MapTrainer* Map::trainer(const int index) throw(IndexException) { if (trainerCount() <= index) throw(IndexException(className())); return m_trainers[index]; } const MapTrainer* Map::trainerById(const int id) const throw(IndexException) { return trainer(trainerIndex(id)); } MapTrainer* Map::trainerById(const int id) throw(IndexException) { return trainer(trainerIndex(id)); } int Map::trainerIndex(const int id) const { for (int i = 0; i < trainerCount(); ++i) { if (m_trainers[i]->id() == id) return i; } return INT_MAX; } int Map::trainerCount() const { return m_trainers.size(); } MapTrainer* Map::newTrainer() { m_trainers.append(new MapTrainer(pokemod(), newTrainerId())); return m_trainers[trainerCount() - 1]; } MapTrainer* Map::newTrainer(const QString& fileName) { m_trainers.append(new MapTrainer(pokemod(), fileName, newTrainerId())); return m_trainers[trainerCount() - 1]; } MapTrainer* Map::newTrainer(const MapTrainer& trainer) { m_trainers.append(new MapTrainer(pokemod(), trainer, newTrainerId())); return m_trainers[trainerCount() - 1]; } void Map::deleteTrainer(const int index) throw(IndexException) { if (trainerCount() <= index) throw(IndexException(className())); delete m_trainers[index]; m_trainers.removeAt(index); } void Map::deleteTrainerById(const int id) throw(IndexException) { deleteTrainer(trainerIndex(id)); } int Map::newTrainerId() const { int i = 0; while ((i < trainerCount()) && (trainerIndex(i) != INT_MAX)) ++i; return i; } const MapWarp* Map::warp(const int index) const throw(IndexException) { if (warpCount() <= index) throw(IndexException(className())); return m_warps.at(index); } MapWarp* Map::warp(const int index) throw(IndexException) { if (warpCount() <= index) throw(IndexException(className())); return m_warps[index]; } const MapWarp* Map::warpById(const int id) const throw(IndexException) { 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 (m_warps[i]->id() == id) return i; } return INT_MAX; } int Map::warpCount() const { return m_warps.size(); } MapWarp* Map::newWarp() { 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]; } void Map::deleteWarp(const int index) throw(IndexException) { if (warpCount() <= index) throw(IndexException(className())); delete m_warps[index]; m_warps.removeAt(index); } void Map::deleteWarpById(const int id) throw(IndexException) { deleteWarp(warpIndex(id)); } int Map::newWarpId() const { int i = 0; while ((i < warpCount()) && (warpIndex(i) != INT_MAX)) ++i; return i; } const MapWildList* Map::wildList(const int index) const throw(IndexException) { if (wildListCount() <= index) throw(IndexException(className())); return m_wildLists.at(index); } MapWildList* Map::wildList(const int index) throw(IndexException) { if (wildListCount() <= index) throw(IndexException(className())); return m_wildLists[index]; } const MapWildList* Map::wildListById(const int id) const throw(IndexException) { return wildList(wildListIndex(id)); } MapWildList* Map::wildListById(const int id) throw(IndexException) { return wildList(wildListIndex(id)); } int Map::wildListIndex(const int id) const { for (int i = 0; i < wildListCount(); ++i) { if (m_wildLists[i]->id() == id) return i; } return INT_MAX; } int Map::wildListCount() const { return m_wildLists.size(); } MapWildList* Map::newWildList() { 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 MapWildList& wildList) { m_wildLists.append(new MapWildList(pokemod(), wildList, newWildListId())); return m_wildLists[wildListCount() - 1]; } void Map::deleteWildList(const int index) throw(IndexException) { if (wildListCount() <= index) throw(IndexException(className())); delete m_wildLists[index]; m_wildLists.removeAt(index); } void Map::deleteWildListById(const int id) throw(IndexException) { 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; 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; }