Parse nature library and test natures.
This commit is contained in:
86
src/BuildData/BuildNatures.cpp
Normal file
86
src/BuildData/BuildNatures.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include "BuildNatures.hpp"
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
static CreatureLib::Core::Statistic ParseStatistic(const std::string& stat) {
|
||||
if (stat.empty())
|
||||
return static_cast<CreatureLib::Core::Statistic>(255);
|
||||
if (stat == "Attack")
|
||||
return CreatureLib::Core::Statistic::PhysicalAttack;
|
||||
if (stat == "Defense")
|
||||
return CreatureLib::Core::Statistic::PhysicalDefense;
|
||||
if (stat == "SpecialAttack")
|
||||
return CreatureLib::Core::Statistic::MagicalAttack;
|
||||
if (stat == "SpecialDefense")
|
||||
return CreatureLib::Core::Statistic::MagicalDefense;
|
||||
if (stat == "Speed")
|
||||
return CreatureLib::Core::Statistic::Speed;
|
||||
std::cout << "Invalid stat was given: '" << stat << "'.\n";
|
||||
return static_cast<CreatureLib::Core::Statistic>(254);
|
||||
}
|
||||
|
||||
PkmnLib::Library::NatureLibrary* BuildNatures::Build(const std::string& path) {
|
||||
std::ifstream file(path);
|
||||
if (file.fail()) {
|
||||
std::cout << "Failed to load Natures data file at '" << path << "'\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
if (!std::getline(file, line)) {
|
||||
std::cout << "Failed to read Natures 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 PkmnLib::Library::NatureLibrary();
|
||||
std::string substr;
|
||||
while (std::getline(file, line)) {
|
||||
size_t lastStart = 0;
|
||||
uint8_t current = 0;
|
||||
std::string natureName;
|
||||
std::string increasedStat;
|
||||
std::string decreasedStat;
|
||||
|
||||
for (size_t i = 0; i < line.length(); i++) {
|
||||
if (line[i] == divider) {
|
||||
switch (current) {
|
||||
case 0: natureName = line.substr(lastStart, i - lastStart); break;
|
||||
case 1: increasedStat = line.substr(lastStart, i - lastStart); break;
|
||||
case 2: decreasedStat = line.substr(lastStart, i - lastStart); break;
|
||||
default:
|
||||
std::cout << "Unknown field in nature data: '" << line.substr(lastStart, i - lastStart)
|
||||
<< "'.\n";
|
||||
return nullptr;
|
||||
}
|
||||
lastStart = i + 1;
|
||||
current++;
|
||||
}
|
||||
}
|
||||
if (current == 2)
|
||||
decreasedStat = line.substr(lastStart, line.length() - lastStart);
|
||||
|
||||
auto parsedIncreased = ParseStatistic(increasedStat);
|
||||
float increasedModifier = 1.1;
|
||||
if (parsedIncreased == 254)
|
||||
return nullptr;
|
||||
if (parsedIncreased == 255)
|
||||
increasedModifier = 1.0;
|
||||
|
||||
auto parsedDecreased = ParseStatistic(decreasedStat);
|
||||
float decreasedModifier = 0.9;
|
||||
if (parsedDecreased == 254)
|
||||
return nullptr;
|
||||
if (parsedDecreased == 255)
|
||||
decreasedModifier = 1.0;
|
||||
|
||||
std::cout << "Registered nature with name '" << natureName << "'.\n";
|
||||
library->LoadNature(natureName, PkmnLib::Library::Nature(parsedIncreased, parsedDecreased, increasedModifier,
|
||||
decreasedModifier));
|
||||
}
|
||||
return library;
|
||||
}
|
||||
10
src/BuildData/BuildNatures.hpp
Normal file
10
src/BuildData/BuildNatures.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef GEN7TESTS_BUILDNATURES_HPP
|
||||
#define GEN7TESTS_BUILDNATURES_HPP
|
||||
|
||||
#include <PkmnLib/Library/Natures/NatureLibrary.hpp>
|
||||
class BuildNatures {
|
||||
public:
|
||||
static PkmnLib::Library::NatureLibrary* Build(const std::string& path);
|
||||
};
|
||||
|
||||
#endif // GEN7TESTS_BUILDNATURES_HPP
|
||||
@@ -1 +1,70 @@
|
||||
#include "BuildTypes.hpp"
|
||||
CreatureLib::Library::TypeLibrary* BuildTypes::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;
|
||||
}
|
||||
|
||||
@@ -7,75 +7,7 @@
|
||||
|
||||
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;
|
||||
}
|
||||
static CreatureLib::Library::TypeLibrary* Build(const std::string& path);
|
||||
};
|
||||
|
||||
#endif // GEN7TESTS_BUILDTYPES_HPP
|
||||
|
||||
Reference in New Issue
Block a user