69 lines
2.3 KiB
C++
69 lines
2.3 KiB
C++
|
#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) {
|
||
|
auto val = library->RegisterType(ArbUt::StringView(substr.c_str()));
|
||
|
types.push_back(val);
|
||
|
} else {
|
||
|
hasSkippedFirst = true;
|
||
|
}
|
||
|
i++;
|
||
|
}
|
||
|
}
|
||
|
auto substr = line.substr(lastStart, line.length() - lastStart);
|
||
|
auto val = library->RegisterType(ArbUt::StringView(substr.c_str()));
|
||
|
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(ArbUt::StringView(substr.c_str()));
|
||
|
}
|
||
|
i++;
|
||
|
}
|
||
|
}
|
||
|
substr = line.substr(lastStart, line.length() - lastStart);
|
||
|
auto eff = std::atof(substr.c_str());
|
||
|
library->SetEffectiveness(attackingType, types[current], eff);
|
||
|
}
|
||
|
|
||
|
return library;
|
||
|
}
|