/* * 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 "Map.h" // Pokemod includes #include "MapEffect.h" #include "MapTrainer.h" #include "MapWarp.h" #include "MapWildList.h" #include "Pokemod.h" const QStringList Map::TypeStr = QStringList() << "Outdoor" << "Dungeon" << "Building"; Map::Map(const Map& map) : Object("Map", map.parent(), map.id()) { *this = map; } Map::Map(const Object* parent, const int id) : Object("Map", parent, id), m_name(""), m_flyWarp(INT_MAX), m_type(INT_MAX) { } Map::Map(const Map& map, const Object* parent, const int id) : Object("Map", parent, id) { *this = map; } Map::Map(const QDomElement& xml, const Object* parent, const int id) : Object("Map", parent, id) { load(xml, id); } Map::~Map() { clear(); } bool Map::validate() const { // TODO: validate // bool valid = true; // static_cast(pokemod())->validationMsg(QString("---Map \"%1\" with id %2---").arg(m_name).arg(id()), Pokemod::V_Msg); // if (m_name == "") // { // static_cast(pokemod())->validationMsg("name is not defined"); // valid = false; // } // if ((m_flyWarp != INT_MAX) && (warpIndex(m_flyWarp) == INT_MAX)) // { // static_cast(pokemod())->validationMsg("Invalid fly destination warp"); // valid = false; // } // if (m_type < End) // { // static_cast(pokemod())->validationMsg("Invalid type"); // valid = false; // } // QMap idChecker; // if (effectCount()) // { // foreach (MapEffect* effect, m_effects) // { // if (!effect->isValid()) // valid = false; // if (idChecker[effect->id()]) // static_cast(pokemod())->validationMsg(QString("Duplicate effect with id %1").arg(effect->id())); // idChecker[effect->id()] = true; // if (width() <= effect->coordinate().x()) // { // static_cast(pokemod())->validationMsg("Invalid x coordinate"); // valid = false; // } // if (height() <= effect->coordinate().y()) // { // static_cast(pokemod())->validationMsg("Invalid y coordinate"); // valid = false; // } // } // idChecker.clear(); // } // else // static_cast(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()]) // static_cast(pokemod())->validationMsg(QString("Duplicate trainer with id %1").arg(trainer->id())); // idChecker[trainer->id()] = true; // if (width() <= trainer->coordinate().x()) // { // static_cast(pokemod())->validationMsg("Invalid x coordinate"); // valid = false; // } // if (height() <= trainer->coordinate().y()) // { // static_cast(pokemod())->validationMsg("Invalid y coordinate"); // valid = false; // } // } // idChecker.clear(); // } // else // static_cast(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()]) // static_cast(pokemod())->validationMsg(QString("Duplicate warp with id %1").arg(warp->id())); // idChecker[warp->id()] = true; // if (width() <= warp->coordinate().x()) // { // static_cast(pokemod())->validationMsg("Invalid x coordinate"); // valid = false; // } // if (height() <= warp->coordinate().y()) // { // static_cast(pokemod())->validationMsg("Invalid y coordinate"); // valid = false; // } // } // idChecker.clear(); // } // else // { // static_cast(pokemod())->validationMsg("There are no warps"); // valid = false; // } // if (wildListCount()) // { // foreach (MapWildList* wildList, m_wildLists) // { // if (!wildList->isValid()) // valid = false; // if (idChecker[wildList->id()]) // static_cast(pokemod())->validationMsg(QString("Duplicate effect with id %1").arg(wildList->id())); // idChecker[wildList->id()] = true; // } // } // else // static_cast(pokemod())->validationMsg("There are no effects", Pokemod::V_Warn); // for (int i = 0; i < width(); ++i) // { // for (int j = 0; j < height(); ++j) // { // if (static_cast(pokemod())->tileIndex(m_tile(i, j)) == INT_MAX) // { // static_cast(pokemod())->validationMsg(QString("Invalid tile at (%1, %2)").arg(i).arg(j)); // valid = false; // } // } // } // return valid; } void Map::load(const QDomElement& xml, int id) { LOAD_ID(); LOAD(QString, name); LOAD(int, flyWarp); LOAD(int, type); LOAD_MATRIX(setTile, Fraction, tile); LOAD_SUB(newEffect, MapEffect); LOAD_SUB(newTrainer, MapTrainer); LOAD_SUB(newWarp, MapWarp); LOAD_SUB(newWildList, MapWildList); } QDomElement Map::save() const { SAVE_CREATE(); SAVE(QString, name); SAVE(int, flyWarp); SAVE(int, type); SAVE_MATRIX(int, tile); SAVE_SUB(MapEffect, effects); SAVE_SUB(MapTrainer, trainers); SAVE_SUB(MapWarp, warps); SAVE_SUB(MapWildList, wildLists); return xml; } 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)) error("warp"); m_flyWarp = warp; } void Map::setType(const int type) throw(BoundsException) { if (End <= type) error("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 (static_cast(pokemod())->tileIndex(id) == INT_MAX) error("tile"); m_tile(x, y) = id; } void Map::insertColumn(int x) { m_tile.insertColumn(x, 0); } void Map::insertRow(int y) { m_tile.insertRow(y, 0); } void Map::addColumn() { m_tile.addColumn(0); } void Map::addRow() { m_tile.addRow(0); } void Map::deleteColumn(int x) { m_tile.deleteColumn(x); } void Map::deleteRow(int y) { m_tile.deleteRow(y); } const Matrix* Map::map() const { return &m_tile; } Matrix* Map::map() { return &m_tile; } int Map::tile(int x, int y) const { return m_tile(x, y); } int Map::width() const { return m_tile.width(); } int Map::height() const { return m_tile.height(); } const MapEffect* Map::effect(const int index) const throw(IndexException) { if (effectCount() <= index) warning("effect"); return m_effects.at(index); } MapEffect* Map::effect(const int index) throw(IndexException) { if (effectCount() <= index) error("effect"); 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(this, newEffectId())); return m_effects[effectCount() - 1]; } MapEffect* Map::newEffect(const QDomElement& xml) { m_effects.append(new MapEffect(xml, this, newEffectId())); return m_effects[effectCount() - 1]; } MapEffect* Map::newEffect(const MapEffect& effect) { m_effects.append(new MapEffect(effect, this, newEffectId())); return m_effects[effectCount() - 1]; } void Map::deleteEffect(const int index) throw(IndexException) { if (effectCount() <= index) error("effect"); 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) warning("trainer"); return m_trainers.at(index); } MapTrainer* Map::trainer(const int index) throw(IndexException) { if (trainerCount() <= index) error("trainer"); 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(this, newTrainerId())); return m_trainers[trainerCount() - 1]; } MapTrainer* Map::newTrainer(const QDomElement& xml) { m_trainers.append(new MapTrainer(xml, this, newTrainerId())); return m_trainers[trainerCount() - 1]; } MapTrainer* Map::newTrainer(const MapTrainer& trainer) { m_trainers.append(new MapTrainer(trainer, this, newTrainerId())); return m_trainers[trainerCount() - 1]; } void Map::deleteTrainer(const int index) throw(IndexException) { if (trainerCount() <= index) error("trainer"); 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) warning("warp"); return m_warps.at(index); } MapWarp* Map::warp(const int index) throw(IndexException) { if (warpCount() <= index) error("warp"); 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(this, newWarpId())); return m_warps[warpCount() - 1]; } MapWarp* Map::newWarp(const QDomElement& xml) { m_warps.append(new MapWarp(xml, this, newWarpId())); return m_warps[warpCount() - 1]; } MapWarp* Map::newWarp(const MapWarp& warp) { m_warps.append(new MapWarp(warp, this, newWarpId())); return m_warps[warpCount() - 1]; } void Map::deleteWarp(const int index) throw(IndexException) { if (warpCount() <= index) error("warp"); 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) warning("wild list"); return m_wildLists.at(index); } MapWildList* Map::wildList(const int index) throw(IndexException) { if (wildListCount() <= index) error("wild list"); 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(this, newWildListId())); return m_wildLists[wildListCount() - 1]; } MapWildList* Map::newWildList(const QDomElement& xml) { m_wildLists.append(new MapWildList(xml, this, newWildListId())); return m_wildLists[wildListCount() - 1]; } MapWildList* Map::newWildList(const MapWildList& wildList) { m_wildLists.append(new MapWildList(wildList, this, newWildListId())); return m_wildLists[wildListCount() - 1]; } void Map::deleteWildList(const int index) throw(IndexException) { if (wildListCount() <= index) error("wild list"); 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; clear(); COPY(name); COPY(flyWarp); COPY(type); COPY(tile); COPY_SUB(MapEffect, effects); COPY_SUB(MapTrainer, trainers); COPY_SUB(MapWarp, warps); COPY_SUB(MapWildList, wildLists); return *this; }