Gen7Tests/src/BuildData/BuildTypes.hpp

82 lines
2.8 KiB
C++

#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