#include "BuildSpecies.hpp" #include #include #define GET(o, objectKey, key) \ auto _##objectKey = o[#objectKey]; \ if (_##objectKey.is_null()) { \ std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << key << "' in file '" \ << path << "'\n"; \ return nullptr; \ } PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string& path, const CreatureLib::Library::TypeLibrary* types) { std::ifstream fileStream(path.c_str()); if (fileStream.fail()) { std::cout << "Failed to load Pokemon data file at '" << path << "'\n"; return nullptr; } auto lib = new PkmnLib::Library::SpeciesLibrary(); json j; fileStream >> j; for (json::iterator it = j.begin(); it != j.end(); ++it) { auto val = it.value(); GET(val, id, it.key()); GET(val, species, it.key()); GET(val, genderRatio, it.key()); GET(val, growthRate, it.key()); GET(val, baseHappiness, it.key()); GET(val, catchRate, it.key()); GET(val, color, it.key()); GET(val, genderDifference, it.key()); GET(val, eggGroups, it.key()); GET(val, eggCycles, it.key()); GET(val, tags, it.key()); GET(val, formes, it.key()); PkmnLib::Library::PokemonSpecies* species = nullptr; auto defaultForme = _formes["default"]; if (!defaultForme.is_null()) { auto forme = BuildForme("default", defaultForme, it.key(), path, types); species = new PkmnLib::Library::PokemonSpecies(_id.get(), _species.get(), forme, _genderRatio.get() / static_cast(100), _growthRate.get(), _catchRate.get(), _baseHappiness.get()); } for (json::iterator formeIt = _formes.begin(); formeIt != _formes.end(); ++formeIt) { if (formeIt.key() == "default") { continue; } auto forme = BuildForme(formeIt.key(), formeIt.value(), it.key(), path, types); if (forme == nullptr) return nullptr; if (species == nullptr) { species = new PkmnLib::Library::PokemonSpecies( _id.get(), _species.get(), forme, static_cast(_genderRatio.get()) / static_cast(100), _growthRate.get(), _catchRate.get(), _baseHappiness.get()); } else { species->SetVariant(formeIt.key(), forme); } } if (species == nullptr) { std::cout << "Pokemon with key '" << it.key() << "' does not have any formes.\n"; return nullptr; } lib->LoadSpecies(it.key(), species); } return lib; } static CreatureLib::Core::StatisticSet ParseStatistics(json& json) { return CreatureLib::Core::StatisticSet( json["hp"].get(), json["attack"].get(), json["defense"].get(), json["specialAttack"].get(), json["specialDefense"].get(), json["speed"].get()); } const PkmnLib::Library::PokemonForme* BuildSpecies::BuildForme(const std::string& name, json& forme, const std::string& baseKeyName, const std::string& path, const CreatureLib::Library::TypeLibrary* typeLibrary) { GET(forme, abilities, baseKeyName << " -> " << name); GET(forme, hiddenAbilities, baseKeyName << " -> " << name); GET(forme, baseStats, baseKeyName << " -> " << name); GET(forme, evReward, baseKeyName << " -> " << name); GET(forme, types, baseKeyName << " -> " << name); GET(forme, height, baseKeyName << " -> " << name); GET(forme, weight, baseKeyName << " -> " << name); GET(forme, baseExp, baseKeyName << " -> " << name); GET(forme, moves, baseKeyName << " -> " << name); auto typeStrings = _types.get>(); auto types = std::vector(typeStrings.size()); for (auto i = 0; i < typeStrings.size(); i++) { types[i] = typeLibrary->GetTypeId(typeStrings[i]); } auto stats = ParseStatistics(_baseStats); return new PkmnLib::Library::PokemonForme( name, _height.get(), _weight.get(), _baseExp.get(), types, stats, _abilities.get>(), _hiddenAbilities.get>(), nullptr); }