Initial commit; Load Species and Types, test all type effectivenesses.

This commit is contained in:
2020-02-09 13:22:54 +01:00
commit bb5ae114c4
15 changed files with 41363 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
#include "BuildSpecies.hpp"
#include <fstream>
#include <iostream>
#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<uint16_t>(), _species.get<std::string>(), forme,
_genderRatio.get<int8_t>() / static_cast<float>(100),
_growthRate.get<std::string>(), _catchRate.get<uint8_t>(),
_baseHappiness.get<uint8_t>());
}
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<uint16_t>(), _species.get<std::string>(), forme,
static_cast<float>(_genderRatio.get<int8_t>()) / static_cast<float>(100),
_growthRate.get<std::string>(), _catchRate.get<uint8_t>(), _baseHappiness.get<uint8_t>());
} 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<uint16_t> ParseStatistics(json& json) {
return CreatureLib::Core::StatisticSet<uint16_t>(
json["hp"].get<uint16_t>(), json["attack"].get<uint16_t>(), json["defense"].get<uint16_t>(),
json["specialAttack"].get<uint16_t>(), json["specialDefense"].get<uint16_t>(), json["speed"].get<uint16_t>());
}
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<std::vector<std::string>>();
auto types = std::vector<uint8_t>(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<float>(), _weight.get<float>(), _baseExp.get<uint32_t>(), types, stats,
_abilities.get<std::vector<std::string>>(), _hiddenAbilities.get<std::vector<std::string>>(), nullptr);
}

View File

@@ -0,0 +1,19 @@
#ifndef GEN7TESTS_BUILDSPECIES_HPP
#define GEN7TESTS_BUILDSPECIES_HPP
#include <PkmnLib/Library/PokemonLibrary.hpp>
#include <CreatureLib/Library/TypeLibrary.hpp>
#include <string>
#include "../../extern/json.hpp"
using json = nlohmann::json;
class BuildSpecies {
static const PkmnLib::Library::PokemonForme* BuildForme(const std::string& name, json& forme,
const std::string& baseKeyName, const std::string& path,
const CreatureLib::Library::TypeLibrary* typeLibrary);
public:
static PkmnLib::Library::SpeciesLibrary* BuildLibrary(const std::string& path,
const CreatureLib::Library::TypeLibrary* types);
};
#endif // GEN7TESTS_BUILDSPECIES_HPP

View File

@@ -0,0 +1 @@
#include "BuildTypes.hpp"

View File

@@ -0,0 +1,81 @@
#ifndef GEN7TESTS_BUILDTYPES_HPP
#define GEN7TESTS_BUILDTYPES_HPP
#include <CreatureLib/Library/TypeLibrary.hpp>
#include <cstring>
#include <fstream>
#include <iostream>
class BuildTypes {
public:
static CreatureLib::Library::TypeLibrary* Build(const std::string& path) {
std::ifstream file(path);
if (file.fail()) {
std::cout << "Failed to load Types data file at '" << path << "'\n";
return nullptr;
}
std::string line;
if (!std::getline(file, line)) {
std::cout << "Failed to read Types data file at '" << path << "'\n";
return nullptr;
}
auto divider = ',';
if (strncmp(line.c_str(), "sep=", 4) == 0) {
divider = line[4];
std::getline(file, line);
}
auto library = new CreatureLib::Library::TypeLibrary();
bool hasSkippedFirst = false;
size_t lastStart = 0;
std::vector<uint8_t > types;
for (size_t i = 0; i < line.length(); i++) {
if (line[i] == divider) {
auto substr = line.substr(lastStart, i - lastStart);
lastStart = i + 1;
if (hasSkippedFirst) {
std::cout << "Registered type: " << substr << "\n";
auto val = library->RegisterType(substr);
types.push_back(val);
} else {
hasSkippedFirst = true;
}
i++;
}
}
auto substr = line.substr(lastStart, line.length() - lastStart);
std::cout << "Registered type: " << substr << "\n";
auto val = library->RegisterType(substr);
types.push_back(val);
while (std::getline(file, line)) {
uint8_t attackingType = 0;
bool gotType = false;
lastStart = 0;
int current = 0;
for (size_t i = 0; i < line.length(); i++) {
if (line[i] == divider) {
substr = line.substr(lastStart, i - lastStart);
lastStart = i + 1;
if (gotType) {
auto eff = std::atof(substr.c_str());
library->SetEffectiveness(attackingType, types[current], eff);
current++;
} else {
gotType = true;
attackingType = library->GetTypeId(substr);
}
i++;
}
}
substr = line.substr(lastStart, line.length() - lastStart);
auto eff = std::atof(substr.c_str());
library->SetEffectiveness(attackingType, types[current], eff);
}
return library;
}
};
#endif // GEN7TESTS_BUILDTYPES_HPP