/* * 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" // Sigmod includes #include "Macros.h" #include "MapEffect.h" #include "MapTile.h" #include "MapTrainer.h" #include "MapWarp.h" #include "MapWildList.h" #include "Sigmod.h" // Qt includes #include const QStringList Sigmod::Map::TypeStr = QStringList() << "Outdoor" << "Dungeon" << "Building"; Sigmod::Map::Map(const Map& map) : Object(map.parent(), map.id()) { *this = map; } Sigmod::Map::Map(const Sigmod* parent, const int id) : Object(parent, id), m_name(""), m_flyWarp(-1), m_type(Outdoor), m_width(0), m_height(0) { } Sigmod::Map::Map(const Map& map, const Sigmod* parent, const int id) : Object(parent, id) { *this = map; } Sigmod::Map::Map(const QDomElement& xml, const Sigmod* parent, const int id) : Object(parent, id) { LOAD_ID(); load(xml); } Sigmod::Map::~Map() { clear(); } void Sigmod::Map::validate() { TEST_BEGIN(); if (m_name.isEmpty()) emit(error("Name is empty")); TEST(setFlyWarp, flyWarp); TEST(setType, type); if (!m_width) emit(error("Map has no width")); if (!m_height) emit(error("Map has no height")); 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 (!tileCount()) emit(warning("There are no tiles")); TEST_SUB_BEGIN(MapTile, tiles); TEST_SUB("tile", id); TEST_SUB_END(); idChecker.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 Sigmod::Map::load(const QDomElement& xml) { LOAD_BEGIN(); LOAD(name); LOAD(flyWarp); LOAD(type); LOAD(width); LOAD(height); LOAD_SUB(newEffect, MapEffect); LOAD_SUB(newTile, MapTile); LOAD_SUB(newTrainer, MapTrainer); LOAD_SUB(newWarp, MapWarp); LOAD_SUB(newWildList, MapWildList); } QDomElement Sigmod::Map::save() const { SAVE_CREATE(); SAVE(name); SAVE(flyWarp); SAVE(type); SAVE(width); SAVE(height); SAVE_SUB(MapEffect, effects); SAVE_SUB(MapTile, tiles); SAVE_SUB(MapTrainer, trainers); SAVE_SUB(MapWarp, warps); SAVE_SUB(MapWildList, wildLists); return xml; } void Sigmod::Map::setName(const QString& name) { CHECK(name); } void Sigmod::Map::setFlyWarp(const int flyWarp) { if ((flyWarp != -1) && !warpById(flyWarp)) emit(error(bounds("flyWarp"))); else CHECK(flyWarp); } void Sigmod::Map::setType(const Type type) { CHECK(type); } void Sigmod::Map::setWidth(const int width) { if (width <= 0) emit(error(bounds("width"))); else CHECK(width); } void Sigmod::Map::setHeight(const int height) { if (height <= 0) emit(error(bounds("height"))); else CHECK(height); } QString Sigmod::Map::name() const { return m_name; } int Sigmod::Map::flyWarp() const { return m_flyWarp; } Sigmod::Map::Type Sigmod::Map::type() const { return m_type; } int Sigmod::Map::width() const { return m_width; } int Sigmod::Map::height() const { return m_height; } const Sigmod::MapEffect* Sigmod::Map::effect(const int index) const { if (index < effectCount()) return m_effects.at(index); return NULL; } Sigmod::MapEffect* Sigmod::Map::effect(const int index) { if (index < effectCount()) return m_effects[index]; return NULL; } const Sigmod::MapEffect* Sigmod::Map::effectById(const int index) const { return effect(effectIndex(index)); } Sigmod::MapEffect* Sigmod::Map::effectById(const int index) { return effect(effectIndex(index)); } int Sigmod::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 Sigmod::Map::effectCount() const { return m_effects.size(); } Sigmod::MapEffect* Sigmod::Map::newEffect() { return newEffect(new MapEffect(this, newEffectId())); } Sigmod::MapEffect* Sigmod::Map::newEffect(const QDomElement& xml) { return newEffect(new MapEffect(xml, this, newEffectId())); } Sigmod::MapEffect* Sigmod::Map::newEffect(const MapEffect& effect) { return newEffect(new MapEffect(effect, this, newEffectId())); } Sigmod::MapEffect* Sigmod::Map::newEffect(MapEffect* effect) { m_effects.append(effect); return effect; } void Sigmod::Map::deleteEffect(const int index) { if (index < effectCount()) { delete m_effects[index]; m_effects.removeAt(index); } } void Sigmod::Map::deleteEffectById(const int id) { deleteEffect(effectIndex(id)); } int Sigmod::Map::newEffectId() const { int i = 0; while ((i < effectCount()) && (effectIndex(i) != INT_MAX)) ++i; return i; } const Sigmod::MapTile* Sigmod::Map::tile(const int index) const { if (index < tileCount()) return m_tiles.at(index); return NULL; } Sigmod::MapTile* Sigmod::Map::tile(const int index) { if (index < tileCount()) return m_tiles[index]; return NULL; } const Sigmod::MapTile* Sigmod::Map::tileById(const int index) const { return tile(tileIndex(index)); } Sigmod::MapTile* Sigmod::Map::tileById(const int index) { return tile(tileIndex(index)); } int Sigmod::Map::tileIndex(const int id) const { for (int i = 0; i < tileCount(); ++i) { if (m_tiles[i]->id() == id) return i; } return INT_MAX; } int Sigmod::Map::tileCount() const { return m_tiles.size(); } Sigmod::MapTile* Sigmod::Map::newTile() { return newTile(new MapTile(this, newTileId())); } Sigmod::MapTile* Sigmod::Map::newTile(const QDomElement& xml) { return newTile(new MapTile(xml, this, newTileId())); } Sigmod::MapTile* Sigmod::Map::newTile(const MapTile& tile) { return newTile(new MapTile(tile, this, newTileId())); } Sigmod::MapTile* Sigmod::Map::newTile(MapTile* tile) { m_tiles.append(tile); return tile; } void Sigmod::Map::deleteTile(const int index) { if (index < tileCount()) { delete m_tiles[index]; m_tiles.removeAt(index); } } void Sigmod::Map::deleteTileById(const int id) { deleteTile(tileIndex(id)); } int Sigmod::Map::newTileId() const { int i = 0; while ((i < tileCount()) && (tileIndex(i) != INT_MAX)) ++i; return i; } const Sigmod::MapTrainer* Sigmod::Map::trainer(const int index) const { if (index < trainerCount()) return m_trainers.at(index); return NULL; } Sigmod::MapTrainer* Sigmod::Map::trainer(const int index) { if (index < trainerCount()) return m_trainers[index]; return NULL; } const Sigmod::MapTrainer* Sigmod::Map::trainerById(const int id) const { return trainer(trainerIndex(id)); } Sigmod::MapTrainer* Sigmod::Map::trainerById(const int id) { return trainer(trainerIndex(id)); } int Sigmod::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 Sigmod::Map::trainerCount() const { return m_trainers.size(); } Sigmod::MapTrainer* Sigmod::Map::newTrainer() { return newTrainer(new MapTrainer(this, newTrainerId())); } Sigmod::MapTrainer* Sigmod::Map::newTrainer(const QDomElement& xml) { return newTrainer(new MapTrainer(xml, this, newTrainerId())); } Sigmod::MapTrainer* Sigmod::Map::newTrainer(const MapTrainer& trainer) { return newTrainer(new MapTrainer(trainer, this, newTrainerId())); } Sigmod::MapTrainer* Sigmod::Map::newTrainer(MapTrainer* trainer) { m_trainers.append(trainer); return trainer; } void Sigmod::Map::deleteTrainer(const int index) { if (index < trainerCount()) { delete m_trainers[index]; m_trainers.removeAt(index); } } void Sigmod::Map::deleteTrainerById(const int id) { deleteTrainer(trainerIndex(id)); } int Sigmod::Map::newTrainerId() const { int i = 0; while ((i < trainerCount()) && (trainerIndex(i) != INT_MAX)) ++i; return i; } const Sigmod::MapWarp* Sigmod::Map::warp(const int index) const { if (index < warpCount()) return m_warps.at(index); return NULL; } Sigmod::MapWarp* Sigmod::Map::warp(const int index) { if (index < warpCount()) return m_warps[index]; return NULL; } const Sigmod::MapWarp* Sigmod::Map::warpById(const int id) const { return warp(warpIndex(id)); } Sigmod::MapWarp* Sigmod::Map::warpById(const int id) { return warp(warpIndex(id)); } int Sigmod::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 Sigmod::Map::warpCount() const { return m_warps.size(); } Sigmod::MapWarp* Sigmod::Map::newWarp() { return newWarp(new MapWarp(this, newWarpId())); } Sigmod::MapWarp* Sigmod::Map::newWarp(const QDomElement& xml) { return newWarp(new MapWarp(xml, this, newWarpId())); } Sigmod::MapWarp* Sigmod::Map::newWarp(const MapWarp& warp) { return newWarp(new MapWarp(warp, this, newWarpId())); } Sigmod::MapWarp* Sigmod::Map::newWarp(MapWarp* warp) { m_warps.append(warp); return warp; } void Sigmod::Map::deleteWarp(const int index) { if (index < warpCount()) { delete m_warps[index]; m_warps.removeAt(index); } } void Sigmod::Map::deleteWarpById(const int id) { deleteWarp(warpIndex(id)); } int Sigmod::Map::newWarpId() const { int i = 0; while ((i < warpCount()) && (warpIndex(i) != INT_MAX)) ++i; return i; } const Sigmod::MapWildList* Sigmod::Map::wildList(const int index) const { if (index < wildListCount()) return m_wildLists.at(index); return NULL; } Sigmod::MapWildList* Sigmod::Map::wildList(const int index) { if (index < wildListCount()) return m_wildLists[index]; return NULL; } const Sigmod::MapWildList* Sigmod::Map::wildListById(const int id) const { return wildList(wildListIndex(id)); } Sigmod::MapWildList* Sigmod::Map::wildListById(const int id) { return wildList(wildListIndex(id)); } int Sigmod::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 Sigmod::Map::wildListCount() const { return m_wildLists.size(); } Sigmod::MapWildList* Sigmod::Map::newWildList() { return newWildList(new MapWildList(this, newWildListId())); } Sigmod::MapWildList* Sigmod::Map::newWildList(const QDomElement& xml) { return newWildList(new MapWildList(xml, this, newWildListId())); } Sigmod::MapWildList* Sigmod::Map::newWildList(const MapWildList& wildList) { return newWildList(new MapWildList(wildList, this, newWildListId())); } Sigmod::MapWildList* Sigmod::Map::newWildList(MapWildList* wildList) { m_wildLists.append(wildList); return wildList; } void Sigmod::Map::deleteWildList(const int index) { if (index < wildListCount()) { delete m_wildLists[index]; m_wildLists.removeAt(index); } } void Sigmod::Map::deleteWildListById(const int id) { deleteWildList(wildListIndex(id)); } int Sigmod::Map::newWildListId() const { int i = 0; while ((i < warpCount()) && (warpIndex(i) != INT_MAX)) ++i; return i; } Sigmod::Map& Sigmod::Map::operator=(const Map& rhs) { if (this == &rhs) return *this; clear(); COPY(name); COPY(flyWarp); COPY(type); COPY(width); COPY(height); COPY_SUB(MapEffect, effects); COPY_SUB(MapTile, tiles); COPY_SUB(MapTrainer, trainers); COPY_SUB(MapWarp, warps); COPY_SUB(MapWildList, wildLists); return *this; } void Sigmod::Map::clear() { qDeleteAll(m_effects); m_effects.clear(); qDeleteAll(m_tiles); m_tiles.clear(); qDeleteAll(m_trainers); m_trainers.clear(); qDeleteAll(m_warps); m_warps.clear(); qDeleteAll(m_wildLists); m_wildLists.clear(); }