/* * 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" // Qt includes #include const QStringList Map::TypeStr = QStringList() << "Outdoor" << "Dungeon" << "Building"; Map::Map(const Map& map) : Object("Map", map.parent(), map.id()) { *this = map; } Map::Map(const Pokemod* parent, const int id) : Object("Map", parent, id), m_name(""), m_flyWarp(INT_MAX), m_type(INT_MAX) { } Map::Map(const Map& map, const Pokemod* parent, const int id) : Object("Map", parent, id) { *this = map; } Map::Map(const QDomElement& xml, const Pokemod* parent, const int id) : Object("Map", parent, id) { load(xml, id); } Map::~Map() { clear(); } void Map::validate() { // TODO: validate that every tile that can have a wild encounter has a list to go along with it 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")); foreach (MapEffect* effect, m_effects) { effect->validate(); if (idChecker.contains(effect->id())) emit(error(subclass("effect", effect->id()))); idChecker.insert(effect->id()); if (nameChecker.contains(effect->name())) emit(error(subclass("effect", effect->name()))); nameChecker.insert(effect->name()); } idChecker.clear(); nameChecker.clear(); if (!trainerCount()) emit(warning("There are no trainers")); foreach (MapTrainer* trainer, m_trainers) { trainer->validate(); if (idChecker.contains(trainer->id())) emit(error(subclass("trainer", trainer->id()))); idChecker.insert(trainer->id()); if (nameChecker.contains(trainer->name())) emit(error(subclass("trainer", trainer->name()))); nameChecker.insert(trainer->name()); } idChecker.clear(); nameChecker.clear(); if (!warpCount()) emit(error("There are no warps")); foreach (MapWarp* warp, m_warps) { warp->validate(); if (idChecker.contains(warp->id())) emit(error(subclass("warp", warp->id()))); idChecker.insert(warp->id()); if (nameChecker.contains(warp->name())) emit(error(subclass("warp", warp->name()))); nameChecker.insert(warp->name()); } idChecker.clear(); if (!wildListCount()) emit(error("There are no wild lists")); foreach (MapWildList* wildList, m_wildLists) { wildList->validate(); if (idChecker.contains(wildList->id())) emit(error(subclass("effect", wildList->id()))); idChecker.insert(wildList->id()); } } 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; emit(changed()); } void Map::setFlyWarp(const int warp) { if ((warp != INT_MAX) && (warpIndex(warp) == INT_MAX)) { emit(error(bounds("warp"))); return; } m_flyWarp = warp; emit(changed()); } void Map::setType(const int type) { if (End <= type) { emit(error(bounds("type"))); return; } m_type = type; emit(changed()); } QString Map::name() const { return m_name; } int Map::flyWarp() const { return m_flyWarp; } int Map::type() const { return m_type; } void 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 Map::insertColumn(const int column) { m_tile.insertColumn(column, 0); } void Map::insertRow(const int row) { m_tile.insertRow(row, 0); } void Map::addColumn() { m_tile.addColumn(0); } void Map::addRow() { m_tile.addRow(0); } void Map::deleteColumn(const int column) { m_tile.deleteColumn(column); } void Map::deleteRow(const int row) { m_tile.deleteRow(row); } const Matrix* Map::map() const { return &m_tile; } Matrix* Map::map() { return &m_tile; } int Map::tile(const int row, const int column) const { return m_tile.at(row, column); } int Map::width() const { return m_tile.width(); } int Map::height() const { return m_tile.height(); } Point Map::size() const { return m_tile.size(); } const MapEffect* Map::effect(const int index) const { if (effectCount() <= index) return NULL; return m_effects.at(index); } MapEffect* Map::effect(const int index) { if (effectCount() <= index) return NULL; return m_effects[index]; } const MapEffect* Map::effectById(const int index) const { return effect(effectIndex(index)); } MapEffect* Map::effectById(const int index) { 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() { return newEffect(new MapEffect(this, newEffectId())); } MapEffect* Map::newEffect(const QDomElement& xml) { return newEffect(new MapEffect(xml, this, newEffectId())); } MapEffect* Map::newEffect(const MapEffect& effect) { return newEffect(new MapEffect(effect, this, newEffectId())); } MapEffect* Map::newEffect(MapEffect* effect) { m_effects.append(effect); return effect; } void Map::deleteEffect(const int index) { if (effectCount() <= index) return; delete m_effects[index]; m_effects.removeAt(index); } void Map::deleteEffectById(const int id) { 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 { if (trainerCount() <= index) return NULL; return m_trainers.at(index); } MapTrainer* Map::trainer(const int index) { if (trainerCount() <= index) return NULL; return m_trainers[index]; } const MapTrainer* Map::trainerById(const int id) const { return trainer(trainerIndex(id)); } MapTrainer* Map::trainerById(const int id) { 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() { return newTrainer(new MapTrainer(this, newTrainerId())); } MapTrainer* Map::newTrainer(const QDomElement& xml) { return newTrainer(new MapTrainer(xml, this, newTrainerId())); } MapTrainer* Map::newTrainer(const MapTrainer& trainer) { return newTrainer(new MapTrainer(trainer, this, newTrainerId())); } MapTrainer* Map::newTrainer(MapTrainer* trainer) { m_trainers.append(trainer); return trainer; } void Map::deleteTrainer(const int index) { if (trainerCount() <= index) return; delete m_trainers[index]; m_trainers.removeAt(index); } void Map::deleteTrainerById(const int id) { 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 { if (warpCount() <= index) return NULL; return m_warps.at(index); } MapWarp* Map::warp(const int index) { if (warpCount() <= index) return NULL; return m_warps[index]; } const MapWarp* Map::warpById(const int id) const { return warp(warpIndex(id)); } MapWarp* Map::warpById(const int id) { 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() { return newWarp(new MapWarp(this, newWarpId())); } MapWarp* Map::newWarp(const QDomElement& xml) { return newWarp(new MapWarp(xml, this, newWarpId())); } MapWarp* Map::newWarp(const MapWarp& warp) { return newWarp(new MapWarp(warp, this, newWarpId())); } MapWarp* Map::newWarp(MapWarp* warp) { m_warps.append(warp); return warp; } void Map::deleteWarp(const int index) { if (warpCount() <= index) return; delete m_warps[index]; m_warps.removeAt(index); } void Map::deleteWarpById(const int id) { 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 { if (wildListCount() <= index) return NULL; return m_wildLists.at(index); } MapWildList* Map::wildList(const int index) { if (wildListCount() <= index) return NULL; return m_wildLists[index]; } const MapWildList* Map::wildListById(const int id) const { return wildList(wildListIndex(id)); } MapWildList* Map::wildListById(const int id) { 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() { return newWildList(new MapWildList(this, newWildListId())); } MapWildList* Map::newWildList(const QDomElement& xml) { return newWildList(new MapWildList(xml, this, newWildListId())); } MapWildList* Map::newWildList(const MapWildList& wildList) { return newWildList(new MapWildList(wildList, this, newWildListId())); } MapWildList* Map::newWildList(MapWildList* wildList) { m_wildLists.append(wildList); return wildList; } void Map::deleteWildList(const int index) { if (wildListCount() <= index) return; delete m_wildLists[index]; m_wildLists.removeAt(index); } void Map::deleteWildListById(const int id) { 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; } void Map::clear() { while (effectCount()) deleteEffect(0); while (trainerCount()) deleteTrainer(0); while (warpCount()) deleteWarp(0); while (wildListCount()) deleteWildList(0); }