/////////////////////////////////////////////////////////////////////////////
// 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 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 "MapTrainer.h"
PokeGen::PokeMod::MapTrainer::MapTrainer(const Pokemod* par, const unsigned _id) :
Object(_id, par),
name(""),
coordinate(0, 0),
skin(""),
sight(0),
direction(UINT_MAX),
numFight(1),
ai(""),
appearFlag(0, 0),
overworldDialog(UINT_MAX),
winDialog(UINT_MAX),
loseDialog(UINT_MAX),
leadPokemon(UINT_MAX)
{
}
PokeGen::PokeMod::MapTrainer::MapTrainer(const Pokemod* par, Ini& ini, const unsigned _id) :
Object(_id, par)
{
ImportIni(ini, _id);
}
bool PokeGen::PokeMod::MapTrainer::Validate()
{
pokemod->ValidationMsg(QString("------Trainer \"%1\" with id %2---").arg(name).arg(id), Pokemod::V_Msg);
if (name == "")
{
pokemod->ValidationMsg("Name is not defined");
isValid = false;
}
if (!QFile(pokemod->GetPath() + "skins/" + skin).exists())
{
pokemod->ValidationMsg("Skin could't be found");
isValid = false;
}
if (D_End_None <= direction)
{
pokemod->ValidationMsg("Invalid direction");
isValid = false;
}
if (!numFight || (pokemod->GetMaxFight() < numFight))
{
pokemod->ValidationMsg("Invalid number of Pokémon for a fight");
isValid = false;
}
if (!QFile(pokemod->GetPath() + "ai/" + ai).exists())
{
pokemod->ValidationMsg("AI file couldn\'t be found");
isValid = false;
}
if (pokemod->GetDialogByID(overworldDialog) == UINT_MAX)
{
pokemod->ValidationMsg("Invalid overworld dialog");
isValid = false;
}
if (pokemod->GetDialogByID(winDialog) == UINT_MAX)
{
pokemod->ValidationMsg("Invalid win dialog");
isValid = false;
}
if (pokemod->GetDialogByID(loseDialog) == UINT_MAX)
{
pokemod->ValidationMsg("Invalid lose dialog");
isValid = false;
}
if (GetPokemonCount() <= leadPokemon)
{
pokemod->ValidationMsg("Invalid lead Pokémon");
isValid = false;
}
if (GetPokemonCount())
{
QMap idChecker;
for (QList::Iterator i = team.begin(); i != team.end(); ++i)
{
if (!i->IsValid())
isValid = false;
++idChecker[i->GetId()];
}
for (QMap::ConstIterator i = idChecker.begin(); i != idChecker.end(); ++i)
{
if (1 < i.value())
{
pokemod->ValidationMsg(QString("There are %1 Pokémon with id %2").arg(i.value()).arg(i.key()));
isValid = false;
}
}
}
else
{
pokemod->ValidationMsg("There are no Pokémon");
isValid = false;
}
return isValid;
}
void PokeGen::PokeMod::MapTrainer::ImportIni(Ini& ini, const unsigned _id)
{
if (_id == UINT_MAX)
ini.GetValue("id", id);
else
id = _id;
unsigned i;
unsigned 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("leadPokemon", leadPokemon);
}
void PokeGen::PokeMod::MapTrainer::ExportIni(QFile& fout, const QString& map) const
{
Ini exMapTrainer("mapTrainer " + map);
exMapTrainer.AddField("id", id);
exMapTrainer.AddField("name", name);
exMapTrainer.AddField("name", name);
exMapTrainer.AddField("coordinate-x", coordinate.GetX());
exMapTrainer.AddField("coordinate-y", coordinate.GetY());
exMapTrainer.AddField("skin", skin);
exMapTrainer.AddField("sight", sight);
exMapTrainer.AddField("direction", direction);
exMapTrainer.AddField("numFight", numFight);
exMapTrainer.AddField("ai", ai);
exMapTrainer.AddField("appearFlag-f", appearFlag.GetFlag());
exMapTrainer.AddField("appearFlag-s", appearFlag.GetStatus());
exMapTrainer.AddField("overworldDialog", overworldDialog);
exMapTrainer.AddField("winDialog", winDialog);
exMapTrainer.AddField("loseDialog", loseDialog);
exMapTrainer.AddField("leadPokemon", leadPokemon);
exMapTrainer.Export(fout);
for (QList::ConstIterator i = team.begin(); i != team.end(); ++i)
i->ExportIni(fout, map, id);
}
void PokeGen::PokeMod::MapTrainer::SetName(const QString& n)
{
name = n;
}
void PokeGen::PokeMod::MapTrainer::SetCoordinate(const unsigned x, const unsigned y)
{
coordinate.Set(x, y);
}
void PokeGen::PokeMod::MapTrainer::SetCoordinateX(const unsigned x)
{
coordinate.SetX(x);
}
void PokeGen::PokeMod::MapTrainer::SetCoordinateY(const unsigned y)
{
coordinate.SetY(y);
}
bool PokeGen::PokeMod::MapTrainer::SetSkin(const QString& s)
{
int dirPos = s.lastIndexOf(PM_DEF_SEP);
int extPos = s.lastIndexOf('.');
if ((dirPos == -1) || (extPos == -1))
return false;
skin = s.mid(dirPos + 1, extPos - dirPos - 1);
QFile file(pokemod->GetPath() + "images/skins/" + skin + ".png");
if (file.exists() && !file.remove())
return false;
return QFile::copy(s, pokemod->GetPath() + "images/skins/" + skin + ".png");
}
void PokeGen::PokeMod::MapTrainer::SetSight(const unsigned s)
{
sight = s;
}
bool PokeGen::PokeMod::MapTrainer::SetDirection(const unsigned d)
{
if (d < D_End_None)
direction = d;
return (direction == d);
}
bool PokeGen::PokeMod::MapTrainer::SetNumFight(const unsigned n)
{
if (n && (n <= pokemod->GetMaxFight()))
numFight = n;
return (numFight == n);
}
bool PokeGen::PokeMod::MapTrainer::SetAI(const QString& a)
{
QFile file(pokemod->GetPath() + "ai/" + name + ".png");
if (file.exists() && file.remove())
return QFile::copy(a, pokemod->GetPath() + "ai/" + name + ".png");
return false;
}
void PokeGen::PokeMod::MapTrainer::SetAppearFlag(const unsigned f, const unsigned s)
{
appearFlag.Set(f, s);
}
void PokeGen::PokeMod::MapTrainer::SetAppearFlagFlag(const unsigned f)
{
appearFlag.SetFlag(f);
}
bool PokeGen::PokeMod::MapTrainer::SetAppearFlagStatus(const unsigned s)
{
if (s < Flag::End)
{
appearFlag.SetStatus(s);
return true;
}
return false;
}
bool PokeGen::PokeMod::MapTrainer::SetOverworldDialog(const unsigned o)
{
if (pokemod->GetDialogByID(o) != UINT_MAX)
overworldDialog = o;
return (overworldDialog == o);
}
bool PokeGen::PokeMod::MapTrainer::SetWinDialog(const unsigned w)
{
if (pokemod->GetDialogByID(w) != UINT_MAX)
winDialog = w;
return (winDialog == w);
}
bool PokeGen::PokeMod::MapTrainer::SetLoseDialog(const unsigned l)
{
if (pokemod->GetDialogByID(l) != UINT_MAX)
loseDialog = l;
return (loseDialog == l);
}
bool PokeGen::PokeMod::MapTrainer::SetLeadPokemon(const unsigned l)
{
if (l < GetPokemonCount())
{
leadPokemon = l;
return true;
}
return false;
}
QString PokeGen::PokeMod::MapTrainer::GetName() const
{
return name;
}
PokeGen::Point PokeGen::PokeMod::MapTrainer::GetCoordinate() const
{
return coordinate;
}
unsigned PokeGen::PokeMod::MapTrainer::GetCoordinateX() const
{
return coordinate.GetX();
}
unsigned PokeGen::PokeMod::MapTrainer::GetCoordinateY() const
{
return coordinate.GetY();
}
QString PokeGen::PokeMod::MapTrainer::GetSkin() const
{
return skin;
}
unsigned PokeGen::PokeMod::MapTrainer::GetSight() const
{
return sight;
}
unsigned PokeGen::PokeMod::MapTrainer::GetDirection() const
{
return direction;
}
unsigned PokeGen::PokeMod::MapTrainer::GetNumFight() const
{
return numFight;
}
QString PokeGen::PokeMod::MapTrainer::GetAI() const
{
return ai;
}
PokeGen::Flag PokeGen::PokeMod::MapTrainer::GetAppearFlag() const
{
return appearFlag;
}
unsigned PokeGen::PokeMod::MapTrainer::GetAppearFlagFlag() const
{
return appearFlag.GetFlag();
}
unsigned PokeGen::PokeMod::MapTrainer::GetAppearFlagStatus() const
{
return appearFlag.GetStatus();
}
unsigned PokeGen::PokeMod::MapTrainer::GetOverworldDialog() const
{
return overworldDialog;
}
unsigned PokeGen::PokeMod::MapTrainer::GetWinDialog() const
{
return winDialog;
}
unsigned PokeGen::PokeMod::MapTrainer::GetLoseDialog() const
{
return loseDialog;
}
unsigned PokeGen::PokeMod::MapTrainer::GetLeadPokemon() const
{
return leadPokemon;
}
const PokeGen::PokeMod::MapTrainerPokemon* PokeGen::PokeMod::MapTrainer::GetPokemon(const unsigned i) const
{
if (i < GetPokemonCount())
return &team[i];
return NULL;
}
unsigned PokeGen::PokeMod::MapTrainer::GetPokemonByID(const unsigned _id) const
{
for (unsigned i = 0; i < GetPokemonCount(); ++i)
{
if (team[i].GetId() == _id)
return i;
}
return UINT_MAX;
}
unsigned PokeGen::PokeMod::MapTrainer::GetPokemonCount() const
{
return team.size();
}
const PokeGen::PokeMod::MapTrainerPokemon* PokeGen::PokeMod::MapTrainer::NewPokemon(Ini* const ini)
{
unsigned i = 0;
for (; (i < GetPokemonCount()) && (GetPokemonByID(i) != UINT_MAX); ++i)
;
MapTrainerPokemon newPokemon(pokemod, i);
if (ini)
newPokemon.ImportIni(*ini);
team.append(newPokemon);
return &team[GetPokemonCount() - 1];
}
bool PokeGen::PokeMod::MapTrainer::DeletePokemon(const unsigned i)
{
if (i < GetPokemonCount())
{
team.erase(team.begin() + i);
return true;
}
return false;
}