BuildData/BuildNatures.cpp

91 lines
3.3 KiB
C++

#include "BuildNatures.hpp"
#include <cstring>
#include <fstream>
#include <iostream>
static CreatureLib::Library::Statistic ParseStatistic(const std::string& stat) {
if (stat.empty())
return static_cast<CreatureLib::Library::Statistic>(255);
if (stat == "Attack")
return CreatureLib::Library::Statistic::PhysicalAttack;
if (stat == "Defense")
return CreatureLib::Library::Statistic::PhysicalDefense;
if (stat == "SpecialAttack")
return CreatureLib::Library::Statistic::MagicalAttack;
if (stat == "SpecialDefense")
return CreatureLib::Library::Statistic::MagicalDefense;
if (stat == "Speed")
return CreatureLib::Library::Statistic::Speed;
std::cout << "Invalid stat was given: '" << stat << "'.\n";
return static_cast<CreatureLib::Library::Statistic>(254);
}
const char PossibleSeparators[] = {',', ';', '|', '\t', ':'};
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 = ',';
for (const auto& s : PossibleSeparators) {
if (line.find(s) != std::string::npos) {
divider = s;
break;
}
}
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 (static_cast<int>(parsedIncreased) == 254)
return nullptr;
if (static_cast<int>(parsedIncreased) == 255)
increasedModifier = 1.0;
auto parsedDecreased = ParseStatistic(decreasedStat);
float decreasedModifier = 0.9;
if (static_cast<int>(parsedDecreased) == 254)
return nullptr;
if (static_cast<int>(parsedDecreased) == 255)
decreasedModifier = 1.0;
library->LoadNature(
ArbUt::StringView(natureName.c_str()),
new PkmnLib::Library::Nature(parsedIncreased, parsedDecreased, increasedModifier, decreasedModifier));
}
return library;
}