///////////////////////////////////////////////////////////////////////////// // Name: pokemod/MapTrainer.cpp // Purpose: Define a trainer for a map // Author: Ben Boeckel // Modified by: Ben Boeckel // Created: Fri June 1 2007 23:11:28 // 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 // 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 . ///////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include "Pokemod.h" #include "Dialog.h" #include "MapTrainer.h" MapTrainer::MapTrainer(const Pokemod* par, const int _id) : Object("MapTrainer", par, _id), name(""), coordinate(0, 0), skin(""), sight(0), direction(-1), numFight(1), ai(""), appearFlag(0, 0), overworldDialog(-1), winDialog(-1), loseDialog(-1), leadTeamMember(-1) { } MapTrainer::MapTrainer(const Pokemod* par, const MapTrainer& t, const int _id) : Object("MapTrainer", par, _id) { *this = t; } MapTrainer::MapTrainer(const Pokemod* par, const QString& fname, const int _id) : Object("MapTrainer", par, _id) { load(fname, _id); } bool MapTrainer::validate() const { bool valid = true; pokemod->validationMsg(QString("------Trainer \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg); if (name == "") { pokemod->validationMsg("Name is not defined"); valid = false; } if (!QFile::exists(getSkin())) { pokemod->validationMsg("Skin could't be found"); valid = false; } if (D_End_None <= direction) { pokemod->validationMsg("Invalid direction"); valid = false; } if (!numFight || (pokemod->getRules()->getMaxFight() < numFight)) { pokemod->validationMsg("Invalid number of Pokémon for a fight"); valid = false; } if (!QFile::exists(getAI())) { pokemod->validationMsg("AI file couldn\'t be found"); valid = false; } if (pokemod->getDialogIndex(overworldDialog) == -1) { pokemod->validationMsg("Invalid overworld dialog"); valid = false; } if (pokemod->getDialogIndex(winDialog) == -1) { pokemod->validationMsg("Invalid win dialog"); valid = false; } if (pokemod->getDialogIndex(loseDialog) == -1) { pokemod->validationMsg("Invalid lose dialog"); valid = false; } if (getTeamMemberCount() <= leadTeamMember) { pokemod->validationMsg("Invalid lead member"); valid = false; } if (getTeamMemberCount()) { QMap idChecker; for (QListIterator i(teamMembers); i.hasNext(); i.next()) { if (!i.peekNext().isValid()) valid = false; ++idChecker[i.peekNext().getId()]; } for (QMapIterator i(idChecker); i.hasNext(); i.next()) { if (1 < i.value()) { pokemod->validationMsg(QString("There are %1 team members with id %2").arg(i.value()).arg(i.key())); valid = false; } } } else { pokemod->validationMsg("There are no team members"); valid = false; } return valid; } int MapTrainer::getNewId() const { int i = 0; for (; i < getTeamMemberCount(); ++i) { if (getTeamMemberIndex(i) == -1) return i; } return -1; } void MapTrainer::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("coordinate-x", i, 0); ini.getValue("coordinate-y", j, 0); coordinate.set(i, j); ini.getValue("skin", skin); ini.getValue("sight", sight, 0); ini.getValue("direction", direction); ini.getValue("numFight", numFight, 1); ini.getValue("ai", ai); ini.getValue("appearFlag-f", i, 0); ini.getValue("appearFlag-s", j, 0); appearFlag.set(i, j); ini.getValue("overworldDialog", overworldDialog); ini.getValue("winDialog", winDialog); ini.getValue("loseDialog", loseDialog); ini.getValue("leadTeamMember", leadTeamMember); QStringList path = pokemod->getPath().split('/'); path.removeLast(); QDir fdir(path.join("/")); teamMembers.clear(); if (fdir.cd("team")) { for (QStringListIterator i(fdir.entryList(QStringList("*.pini"), QDir::Files, QDir::Name)); i.hasNext(); ) newTeamMember(i.next()); } } void MapTrainer::save(const QString& map) const throw(Exception) { Ini ini; ini.addField("id", id); ini.addField("name", name); ini.addField("name", name); ini.addField("coordinate-x", coordinate.getX()); ini.addField("coordinate-y", coordinate.getY()); ini.addField("skin", skin); ini.addField("sight", sight); ini.addField("direction", direction); ini.addField("numFight", numFight); ini.addField("ai", ai); ini.addField("appearFlag-f", appearFlag.getFlag()); ini.addField("appearFlag-s", appearFlag.getStatus()); ini.addField("overworldDialog", overworldDialog); ini.addField("winDialog", winDialog); ini.addField("loseDialog", loseDialog); ini.addField("leadTeamMember", leadTeamMember); ini.save(QString("%1/map/%2/trainer/%3/data.pini").arg(pokemod->getPath()).arg(map).arg(name)); for (QListIterator i(teamMembers); i.hasNext(); ) i.next().save(map, name); } void MapTrainer::setName(const QString& n) { name = n; } void MapTrainer::setCoordinate(const int x, const int y) { coordinate.set(x, y); } void MapTrainer::setCoordinateX(const int x) { coordinate.setX(x); } void MapTrainer::setCoordinateY(const int y) { coordinate.setY(y); } void MapTrainer::setSkin(const QString& s) throw(OpenException) { if (!QFile::exists(QString("%1/image/skin/%2.png").arg(pokemod->getPath()).arg(s))) throw(OpenException(className, QString("%1/image/skin/%2.png").arg(pokemod->getPath()).arg(s))); skin = s; } void MapTrainer::setSight(const int s) { sight = s; } void MapTrainer::setDirection(const int d) throw(BoundsException) { if (D_End_None <= d) throw(BoundsException(className, "direction")); direction = d; } void MapTrainer::setNumFight(const int n) throw(BoundsException) { if (!n || (pokemod->getRules()->getMaxFight() < n)) throw(BoundsException(className, "numFight")); numFight = n; } void MapTrainer::setAI(const QString& a) throw(OpenException) { if (!QFile::exists(QString("%1/ai/%2.pai").arg(pokemod->getPath()).arg(a))) throw(OpenException(className, QString("%1/ai/%2.pai").arg(pokemod->getPath()).arg(a))); ai = a; } void MapTrainer::setAppearFlag(const int f, const int s) { appearFlag.set(f, s); } void MapTrainer::setAppearFlagFlag(const int f) { appearFlag.setFlag(f); } void MapTrainer::setAppearFlagStatus(const int s) { appearFlag.setStatus(s); } void MapTrainer::setOverworldDialog(const int o) throw(BoundsException) { if (pokemod->getDialogIndex(o) == -1) throw(BoundsException(className, "overworldDialog")); overworldDialog = o; } void MapTrainer::setWinDialog(const int w) throw(BoundsException) { if (pokemod->getDialogIndex(w) == -1) throw(BoundsException(className, "winDialog")); winDialog = w; } void MapTrainer::setLoseDialog(const int l) throw(BoundsException) { if (pokemod->getDialogIndex(l) == -1) throw(BoundsException(className, "loseDialog")); loseDialog = l; } void MapTrainer::setLeadTeamMember(const int l) throw(BoundsException) { if (getTeamMemberCount() <= l) throw(BoundsException(className, "leadTeamMember")); leadTeamMember = l; } QString MapTrainer::getName() const { return name; } Point MapTrainer::getCoordinate() const { return coordinate; } QString MapTrainer::getSkin() const { return QString("%1/image/skin/%2.png").arg(pokemod->getPath()).arg(skin); } int MapTrainer::getSight() const { return sight; } int MapTrainer::getDirection() const { return direction; } int MapTrainer::getNumFight() const { return numFight; } QString MapTrainer::getAI() const { return QString("%1/ai/%2.pai").arg(pokemod->getPath()).arg(ai); } Flag MapTrainer::getAppearFlag() const { return appearFlag; } int MapTrainer::getOverworldDialog() const { return overworldDialog; } int MapTrainer::getWinDialog() const { return winDialog; } int MapTrainer::getLoseDialog() const { return loseDialog; } int MapTrainer::getLeadTeamMember() const { return leadTeamMember; } const MapTrainerTeamMember* MapTrainer::getTeamMember(const int i) const throw(IndexException) { if (getTeamMemberCount() <= i) throw(IndexException(className)); return &teamMembers.at(i); } MapTrainerTeamMember* MapTrainer::getTeamMember(const int i) throw(IndexException) { if (getTeamMemberCount() <= i) throw(IndexException(className)); return &teamMembers[i]; } const MapTrainerTeamMember* MapTrainer::getTeamMemberByID(const int i) const throw(IndexException) { return getTeamMember(getTeamMemberIndex(i)); } MapTrainerTeamMember* MapTrainer::getTeamMemberByID(const int i) throw(IndexException) { return getTeamMember(getTeamMemberIndex(i)); } int MapTrainer::getTeamMemberIndex(const int _id) const { for (int i = 0; i < getTeamMemberCount(); ++i) { if (teamMembers[i].getId() == _id) return i; } return -1; } int MapTrainer::getTeamMemberCount() const { return teamMembers.size(); } MapTrainerTeamMember* MapTrainer::newTeamMember() { teamMembers.append(MapTrainerTeamMember(pokemod, getNewId())); return &teamMembers[getTeamMemberCount() - 1]; } MapTrainerTeamMember* MapTrainer::newTeamMember(const QString& fname) { teamMembers.append(MapTrainerTeamMember(pokemod, fname, getNewId())); return &teamMembers[getTeamMemberCount() - 1]; } MapTrainerTeamMember* MapTrainer::newTeamMember(const MapTrainerTeamMember& p) { teamMembers.append(MapTrainerTeamMember(pokemod, p, getNewId())); return &teamMembers[getTeamMemberCount() - 1]; } void MapTrainer::deleteTeamMember(const int i) throw(IndexException) { if (getTeamMemberCount() <= i) throw(IndexException(className)); teamMembers.removeAt(i); } MapTrainer& MapTrainer::operator=(const MapTrainer& rhs) { if (this == &rhs) return *this; name = rhs.name; coordinate = rhs.coordinate; skin = rhs.skin; sight = rhs.sight; direction = rhs.direction; numFight = rhs.numFight; ai = rhs.ai; appearFlag = rhs.appearFlag; overworldDialog = rhs.overworldDialog; winDialog = rhs.winDialog; loseDialog = rhs.loseDialog; leadTeamMember = rhs.leadTeamMember; teamMembers.clear(); for (int i = 0; i < rhs.getTeamMemberCount(); ++i) teamMembers.append(MapTrainerTeamMember(pokemod, *rhs.getTeamMember(i), rhs.getTeamMember(i)->getId())); return *this; }