///////////////////////////////////////////////////////////////////////////// // Name: pokemod/Tile.cpp // Purpose: Define a tile for the map // Author: Ben Boeckel // Modified by: Ben Boeckel // Created: Thu May 31 2007 13:52:39 // Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions // Licence: // 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 // MERCHANTTile 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 . ///////////////////////////////////////////////////////////////////////////// #include "Pokemod.h" #include "Tile.h" const QStringList Tile::ForceStr = QStringList() << "None" << "Slip" << "Stop" << "Force" << "Push"; Tile::Tile(const Pokemod& par, const int _id) : Object("Tile", par, _id), name(""), pic(""), wildChance(1, 1), hmType(-1), under(-1), forceType(-1), forceDirection(-1) { for (int i = 0; i < D_End; ++i) from[i] = false; } Tile::Tile(const Pokemod& par, const Tile& t, const int _id) : Object("Tile", par, _id) { *this = t; } Tile::Tile(const Pokemod& par, const QString& fname, const int _id) : Object("Tile", par, _id), pic("") { load(fname, _id); } bool Tile::validate() const { bool valid = true; pokemod.validationMsg(QString("---Tile \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg); if (name == "") { pokemod.validationMsg("Name is not defined"); valid = false; } if (!QFile::exists(QString("%1/image/tile/%2.png").arg(pokemod.getPath()).arg(pic))) { pokemod.validationMsg("Cannot find tile image"); valid = false; } if ((hmType == HM_Waterfall) && (!from[D_Up] || !from[D_Down])) { pokemod.validationMsg("A waterfall tile must be accessible from above and below"); valid = false; } else if ((hmType == HM_Whirlpool) && ((under == id) || (pokemod.getTileIndex(under) == -1) || (pokemod.getTileByID(under).getHMType() != HM_Surf) || (pokemod.getTileByID(under).getHMType() != HM_Dive))) { pokemod.validationMsg("Invalid under tile"); valid = false; } else if ((hmType == HM_Whirlpool) && ((under == id) || (pokemod.getTileIndex(under) == -1) || (pokemod.getTileByID(under).getHMType() != HM_End))) { pokemod.validationMsg("Invalid under tile"); valid = false; } else if ((hmType == HM_RockClimb) && (!from[D_Up] || !from[D_Down])) { pokemod.validationMsg("A rock climb tile must be accessible from above and below"); valid = false; } if (forceType < End) { if (((forceType == Slip) || (forceType == Force) || (forceType == Push)) && (D_End <= forceDirection)) { pokemod.validationMsg("Invalid force direction"); valid = false; } } else { pokemod.validationMsg("Invalid force type"); valid = false; } return valid; } void Tile::load(const QString& fname, const int _id) throw(Exception) { Ini ini(fname); if (_id == -1) ini.getValue("id", id); else id = _id; int i; int j; ini.getValue("name", name); ini.getValue("pic", pic); ini.getValue("from-up", from[D_Up], false); ini.getValue("from-down", from[D_Down], false); ini.getValue("from-left", from[D_Left], false); ini.getValue("from-right", from[D_Right], false); ini.getValue("wildChance-n", i, 1); ini.getValue("wildChance-d", j, 1); wildChance.set(i, j); ini.getValue("hmType", hmType); ini.getValue("under", under); ini.getValue("forceType", forceType); ini.getValue("forceDirection", forceDirection); } void Tile::save() const throw(Exception) { Ini ini; ini.addField("id", id); ini.addField("name", name); ini.addField("pic", pic); ini.addField("from-up", from[D_Up]); ini.addField("from-down", from[D_Down]); ini.addField("from-left", from[D_Left]); ini.addField("from-right", from[D_Right]); ini.addField("wildChance-n", wildChance.getNum()); ini.addField("wildChance-d", wildChance.getDenom()); ini.addField("hmType", hmType); ini.addField("under", under); ini.addField("forceType", forceType); ini.addField("forceDirection", forceDirection); ini.save(QString("%1/tile/%2.pini").arg(pokemod.getPath()).arg(name)); } void Tile::setName(const QString& n) { name = n; } void Tile::setPic(const QString& p) throw(OpenException) { if (!QFile::exists(QString("%1/image/tile/%2.png").arg(pokemod.getPath()).arg(p))) throw(OpenException(className, QString("%1/image/tile/%2.png").arg(pokemod.getPath()).arg(p))); pic = p; } void Tile::setFrom(const int d, const bool f) throw(BoundsException) { if (D_End <= d) throw(BoundsException(className, "direction")); from[d] = f; } void Tile::setWildChance(const int n, const int d) throw(Exception) { wildChance.set(n, d); } void Tile::setWildChanceNumerator(const int n) throw(Exception) { wildChance.setNum(n); } void Tile::setWildChanceDenominator(const int d) throw(Exception) { wildChance.setDenom(d); } void Tile::setHMType(const int h) throw(BoundsException) { if (HM_End <= h) throw(BoundsException(className, "hmType")); hmType = h; under = -1; } void Tile::setUnder(const int u) throw(Exception) { if ((hmType != HM_Whirlpool) || (hmType != HM_Cut) || (hmType != HM_RockSmash)) throw(UnusedException(className, "under")); if ((u == id) || (pokemod.getTileIndex(u) == -1)) throw(BoundsException(className, "under")); under = u; } void Tile::setForceType(const int f) throw(BoundsException) { if (End <= f) throw(BoundsException(className, "forceType")); forceType = f; } void Tile::setForceDirection(const int f) throw(Exception) { if ((forceType == None) || (forceType == Stop)) throw(UnusedException(className, "forceDirection")); if (D_End <= f) throw(BoundsException(className, "forceDirection")); forceDirection = f; } QString Tile::getName() const { return name; } QString Tile::getPic() const { return pic; } bool Tile::getFrom(const int d) const throw(BoundsException) { if (D_End <= d) throw(BoundsException(className, "direction")); return from[d]; } Frac Tile::getWildChance() const { return wildChance; } int Tile::getHMType() const { return hmType; } int Tile::getUnder() const { return under; } int Tile::getForceType() const { return forceType; } int Tile::getForceDirection() const { return forceDirection; } Tile& Tile::operator=(const Tile& rhs) { if (this == &rhs) return *this; name = rhs.name; pic = rhs.pic; for (int i = 0; i < D_End; ++i) from[i] = rhs.from[i]; wildChance = rhs.wildChance; hmType = rhs.hmType; under = rhs.under; forceType = rhs.forceType; forceDirection = rhs.forceDirection; return *this; }