/* * 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 "Macros.h" #include "MapEffect.h" #include "MapTrainer.h" #include "MapWarp.h" #include "MapWildList.h" #include "Pokemod.h" // Qt includes #include const QStringList Pokemod::Map::TypeStr = QStringList() << "Outdoor" << "Dungeon" << "Building"; Pokemod::Map::Map(const Map& map) : Object("Map", map.parent(), map.id()) { *this = map; } Pokemod::Map::Map(const Pokemod* parent, const int id) : Object("Map", parent, id), m_name(""), m_flyWarp(-1), m_type(INT_MAX) { } Pokemod::Map::Map(const Map& map, const Pokemod* parent, const int id) : Object("Map", parent, id) { *this = map; } Pokemod::Map::Map(const QDomElement& xml, const Pokemod* parent, const int id) : Object("Map", parent, id) { LOAD_ID(); load(xml); } Pokemod::Map::~Map() { clear(); } void Pokemod::Map::validate() { TEST_BEGIN(); if (m_name.isEmpty()) emit(error("Name is empty")); TEST(setFlyWarp, flyWarp); TEST(setType, type); TEST_MATRIX(setTile, tile); QSet idChecker; QSet nameChecker; if (!effectCount()) emit(warning("There are no effects")); TEST_SUB_BEGIN(MapEffect, effects); TEST_SUB("effect", id); TEST_SUB("effect", name); TEST_SUB_END(); idChecker.clear(); nameChecker.clear(); if (!trainerCount()) emit(warning("There are no trainers")); TEST_SUB_BEGIN(MapTrainer, trainers); TEST_SUB("trainer", id); TEST_SUB("trainer", name); TEST_SUB_END(); idChecker.clear(); nameChecker.clear(); if (!warpCount()) emit(error("There are no warps")); TEST_SUB_BEGIN(MapWarp, warps); TEST_SUB("warp", id); TEST_SUB("warp", name); TEST_SUB_END(); idChecker.clear(); if (!wildListCount()) emit(warning("There are no wild lists")); TEST_SUB_BEGIN(MapWildList, wildLists); TEST_SUB("wild list", id); TEST_SUB_END(); TEST_END(); } void Pokemod::Map::load(const QDomElement& xml) { LOAD_BEGIN(); 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 Pokemod::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 Pokemod::Map::setName(const QString& name) { CHECK(name); } void Pokemod::Map::setFlyWarp(const int flyWarp) { if ((flyWarp != -1) && (warpIndex(flyWarp) == INT_MAX)) { emit(error(bounds("flyWarp"))); return; } CHECK(flyWarp); } void Pokemod::Map::setType(const int type) { if (End <= type) { emit(error(bounds("type"))); return; } CHECK(type); } QString Pokemod::Map::name() const { return m_name; } int Pokemod::Map::flyWarp() const { return m_flyWarp; } int Pokemod::Map::type() const { return m_type; } void Pokemod::Map::setTile(const int row, const int column, const int tile) { if (static_cast(pokemod())->tileIndex(tile) == INT_MAX) { emit(error(bounds("tile"))); return; } m_tile.set(row, column, tile); } void Pokemod::Map::insertColumn(const int column) { m_tile.insertColumn(column, 0); } void Pokemod::Map::insertRow(const int row) { m_tile.insertRow(row, 0); } void Pokemod::Map::addColumn() { m_tile.addColumn(0); } void Pokemod::Map::addRow() { m_tile.addRow(0); } void Pokemod::Map::deleteColumn(const int column) { m_tile.deleteColumn(column); } void Pokemod::Map::deleteRow(const int row) { m_tile.deleteRow(row); } const Pokemod::Matrix* Pokemod::Map::map() const { return &m_tile; } Pokemod::Matrix* Pokemod::Map::map() { return &m_tile; } int Pokemod::Map::tile(const int row, const int column) const { return m_tile.at(row, column); } int Pokemod::Map::width() const { return m_tile.width(); } int Pokemod::Map::height() const { return m_tile.height(); } QPoint Pokemod::Map::size() const { return m_tile.size(); } const Pokemod::MapEffect* Pokemod::Map::effect(const int index) const { Q_ASSERT(index < effectCount()); return m_effects.at(index); } Pokemod::MapEffect* Pokemod::Map::effect(const int index) { Q_ASSERT(index < effectCount()); return m_effects[index]; } const Pokemod::MapEffect* Pokemod::Map::effectById(const int index) const { return effect(effectIndex(index)); } Pokemod::MapEffect* Pokemod::Map::effectById(const int index) { return effect(effectIndex(index)); } int Pokemod::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 Pokemod::Map::effectCount() const { return m_effects.size(); } Pokemod::MapEffect* Pokemod::Map::newEffect() { return newEffect(new MapEffect(this, newEffectId())); } Pokemod::MapEffect* Pokemod::Map::newEffect(const QDomElement& xml) { return newEffect(new MapEffect(xml, this, newEffectId())); } Pokemod::MapEffect* Pokemod::Map::newEffect(const MapEffect& effect) { return newEffect(new MapEffect(effect, this, newEffectId())); } Pokemod::MapEffect* Pokemod::Map::newEffect(MapEffect* effect) { m_effects.append(effect); return effect; } void Pokemod::Map::deleteEffect(const int index) { Q_ASSERT(index < effectCount()); delete m_effects[index]; m_effects.removeAt(index); } void Pokemod::Map::deleteEffectById(const int id) { deleteEffect(effectIndex(id)); } int Pokemod::Map::newEffectId() const { int i = 0; while ((i < effectCount()) && (effectIndex(i) != INT_MAX)) ++i; return i; } const Pokemod::MapTrainer* Pokemod::Map::trainer(const int index) const { Q_ASSERT(index < trainerCount()); return m_trainers.at(index); } Pokemod::MapTrainer* Pokemod::Map::trainer(const int index) { Q_ASSERT(index < trainerCount()); return m_trainers[index]; } const Pokemod::MapTrainer* Pokemod::Map::trainerById(const int id) const { return trainer(trainerIndex(id)); } Pokemod::MapTrainer* Pokemod::Map::trainerById(const int id) { return trainer(trainerIndex(id)); } int Pokemod::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 Pokemod::Map::trainerCount() const { return m_trainers.size(); } Pokemod::MapTrainer* Pokemod::Map::newTrainer() { return newTrainer(new MapTrainer(this, newTrainerId())); } Pokemod::MapTrainer* Pokemod::Map::newTrainer(const QDomElement& xml) { return newTrainer(new MapTrainer(xml, this, newTrainerId())); } Pokemod::MapTrainer* Pokemod::Map::newTrainer(const MapTrainer& trainer) { return newTrainer(new MapTrainer(trainer, this, newTrainerId())); } Pokemod::MapTrainer* Pokemod::Map::newTrainer(MapTrainer* trainer) { m_trainers.append(trainer); return trainer; } void Pokemod::Map::deleteTrainer(const int index) { Q_ASSERT(index < trainerCount()); delete m_trainers[index]; m_trainers.removeAt(index); } void Pokemod::Map::deleteTrainerById(const int id) { deleteTrainer(trainerIndex(id)); } int Pokemod::Map::newTrainerId() const { int i = 0; while ((i < trainerCount()) && (trainerIndex(i) != INT_MAX)) ++i; return i; } const Pokemod::MapWarp* Pokemod::Map::warp(const int index) const { Q_ASSERT(index < warpCount()); return m_warps.at(index); } Pokemod::MapWarp* Pokemod::Map::warp(const int index) { Q_ASSERT(index < warpCount()); return m_warps[index]; } const Pokemod::MapWarp* Pokemod::Map::warpById(const int id) const { return warp(warpIndex(id)); } Pokemod::MapWarp* Pokemod::Map::warpById(const int id) { return warp(warpIndex(id)); } int Pokemod::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 Pokemod::Map::warpCount() const { return m_warps.size(); } Pokemod::MapWarp* Pokemod::Map::newWarp() { return newWarp(new MapWarp(this, newWarpId())); } Pokemod::MapWarp* Pokemod::Map::newWarp(const QDomElement& xml) { return newWarp(new MapWarp(xml, this, newWarpId())); } Pokemod::MapWarp* Pokemod::Map::newWarp(const MapWarp& warp) { return newWarp(new MapWarp(warp, this, newWarpId())); } Pokemod::MapWarp* Pokemod::Map::newWarp(MapWarp* warp) { m_warps.append(warp); return warp; } void Pokemod::Map::deleteWarp(const int index) { Q_ASSERT(index < warpCount()); delete m_warps[index]; m_warps.removeAt(index); } void Pokemod::Map::deleteWarpById(const int id) { deleteWarp(warpIndex(id)); } int Pokemod::Map::newWarpId() const { int i = 0; while ((i < warpCount()) && (warpIndex(i) != INT_MAX)) ++i; return i; } const Pokemod::MapWildList* Pokemod::Map::wildList(const int index) const { Q_ASSERT(index < wildListCount()); return m_wildLists.at(index); } Pokemod::MapWildList* Pokemod::Map::wildList(const int index) { Q_ASSERT(index < wildListCount()); return m_wildLists[index]; } const Pokemod::MapWildList* Pokemod::Map::wildListById(const int id) const { return wildList(wildListIndex(id)); } Pokemod::MapWildList* Pokemod::Map::wildListById(const int id) { return wildList(wildListIndex(id)); } int Pokemod::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 Pokemod::Map::wildListCount() const { return m_wildLists.size(); } Pokemod::MapWildList* Pokemod::Map::newWildList() { return newWildList(new MapWildList(this, newWildListId())); } Pokemod::MapWildList* Pokemod::Map::newWildList(const QDomElement& xml) { return newWildList(new MapWildList(xml, this, newWildListId())); } Pokemod::MapWildList* Pokemod::Map::newWildList(const MapWildList& wildList) { return newWildList(new MapWildList(wildList, this, newWildListId())); } Pokemod::MapWildList* Pokemod::Map::newWildList(MapWildList* wildList) { m_wildLists.append(wildList); return wildList; } void Pokemod::Map::deleteWildList(const int index) { Q_ASSERT(index < wildListCount()); delete m_wildLists[index]; m_wildLists.removeAt(index); } void Pokemod::Map::deleteWildListById(const int id) { deleteWildList(wildListIndex(id)); } int Pokemod::Map::newWildListId() const { int i = 0; while ((i < warpCount()) && (warpIndex(i) != INT_MAX)) ++i; return i; } Pokemod::Map& Pokemod::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; } void Pokemod::Map::clear() { while (effectCount()) deleteEffect(0); while (trainerCount()) deleteTrainer(0); while (warpCount()) deleteWarp(0); while (wildListCount()) deleteWildList(0); }