Performance improvements, moves json.hpp into rest of the repo.

This commit is contained in:
Deukhoofd 2021-08-28 16:52:47 +02:00
parent 73d50ab98b
commit 4e11fdddb3
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
6 changed files with 26677 additions and 29 deletions

View File

@ -1,7 +1,7 @@
#include "BuildItems.hpp"
#include <fstream>
#include <iostream>
#include "../../extern/json.hpp"
#include "json.hpp"
using json = nlohmann::json;
#define GET(o, objectKey, key) \

View File

@ -1,7 +1,7 @@
#include "BuildMoves.hpp"
#include <fstream>
#include <iostream>
#include "../../extern/json.hpp"
#include "json.hpp"
using json = nlohmann::json;
#define GET(o, objectKey, key) \

View File

@ -3,7 +3,7 @@
#include <iostream>
#define GET(o, objectKey, key) \
auto& _##objectKey = o[#objectKey]; \
auto& _##objectKey = o.at(#objectKey); \
if (_##objectKey.is_null()) { \
auto errorKeyFunc = [=]() { return key; }; \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << errorKeyFunc() \
@ -90,13 +90,24 @@ PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string&
return lib;
}
static CreatureLib::Library::StatisticSet<uint16_t> ParseStatistics(json& json) {
return CreatureLib::Library::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>());
static CreatureLib::Library::StatisticSet<uint16_t> ParseStatistics(const json& json) {
return {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,
inline void BuildLevelMove(const json& levelMoveObj, const PkmnLib::Library::MoveLibrary* moveLibrary,
PkmnLib::Library::LearnableMoves* moves) {
auto levelMoveName = levelMoveObj.at("name").get<std::string>();
auto level = levelMoveObj.at("level").get<u8>();
auto move = moveLibrary->Get(ArbUt::StringView(levelMoveName.c_str(), levelMoveName.size()));
moves->AddLevelAttack(level, move.ForceAs<const CreatureLib::Library::AttackData>());
}
const PkmnLib::Library::PokemonForme* BuildSpecies::BuildForme(const std::string& name, const json& forme,
const std::string& baseKeyName, const std::string& path,
const CreatureLib::Library::TypeLibrary* typeLibrary,
const PkmnLib::Library::MoveLibrary* moveLibrary) {
@ -110,37 +121,34 @@ const PkmnLib::Library::PokemonForme* BuildSpecies::BuildForme(const std::string
GET(forme, baseExp, baseKeyName + " -> " + name);
GET(forme, moves, baseKeyName + " -> " + name);
auto typeStrings = _types.get<std::vector<std::string>>();
auto types = ArbUt::List<uint8_t>(typeStrings.size());
for (size_t i = 0; i < typeStrings.size(); i++) {
types.Append(typeLibrary->GetTypeId(ArbUt::StringView(typeStrings[i].c_str())));
auto types = ArbUt::List<uint8_t>(_types.size());
for (const auto& t : _types.items()) {
auto s = t.value().get<std::string>();
types.Append(typeLibrary->GetTypeId(ArbUt::StringView(s.c_str(), s.length())));
}
auto stats = ParseStatistics(_baseStats);
auto abilityStrings = _abilities.get<std::vector<std::string>>();
auto abilities = ArbUt::List<ArbUt::StringView>(abilityStrings.size());
for (size_t i = 0; i < abilityStrings.size(); i++) {
abilities.Append(ArbUt::StringView(abilityStrings[i].c_str()));
auto abilities = ArbUt::List<ArbUt::StringView>(_abilities.size());
for (const auto& ab : _abilities.items()) {
auto s = ab.value().get<std::string>();
abilities.Append(ArbUt::StringView(s.c_str(), s.length()));
}
auto hiddenAbilityStrings = _hiddenAbilities.get<std::vector<std::string>>();
auto hiddenAbilities = ArbUt::List<ArbUt::StringView>(hiddenAbilityStrings.size());
for (size_t i = 0; i < hiddenAbilityStrings.size(); i++) {
hiddenAbilities.Append(ArbUt::StringView(hiddenAbilityStrings[i].c_str()));
auto hiddenAbilities = ArbUt::List<ArbUt::StringView>(_hiddenAbilities.size());
for (const auto& ab : _hiddenAbilities.items()) {
auto s = ab.value().get<std::string>();
hiddenAbilities.Append(ArbUt::StringView(s.c_str(), s.length()));
}
auto moves = new PkmnLib::Library::LearnableMoves(100);
auto& movesJson = forme["moves"];
auto& levelMovesJson = movesJson["levelMoves"];
auto& levelMovesJson = movesJson.at("levelMoves");
for (auto& levelMoveObj : levelMovesJson) {
auto levelMoveName = levelMoveObj["name"].get<std::string>();
auto level = levelMoveObj["level"].get<u8>();
auto move = moveLibrary->Get(ArbUt::StringView(levelMoveName.c_str(), levelMoveName.size()));
moves->AddLevelAttack(level, move.ForceAs<const CreatureLib::Library::AttackData>());
BuildLevelMove(levelMoveObj, moveLibrary, moves);
}
return new PkmnLib::Library::PokemonForme(ArbUt::StringView(name.c_str()), _height.get<float>(),
_weight.get<float>(), _baseExp.get<uint32_t>(), types, stats, abilities,
hiddenAbilities, moves);
}
#undef GET

View File

@ -4,11 +4,11 @@
#include <CreatureLib/Library/TypeLibrary.hpp>
#include <PkmnLib/Library/PokemonLibrary.hpp>
#include <string>
#include "../../extern/json.hpp"
#include "json.hpp"
using json = nlohmann::json;
class BuildSpecies {
static const PkmnLib::Library::PokemonForme* BuildForme(const std::string& name, json& forme,
static const PkmnLib::Library::PokemonForme* BuildForme(const std::string& name, const json& forme,
const std::string& baseKeyName, const std::string& path,
const CreatureLib::Library::TypeLibrary* typeLibrary,
const PkmnLib::Library::MoveLibrary* moveLibrary);

View File

@ -1,7 +1,7 @@
#include "GrowthRatesBuilder.hpp"
#include <fstream>
#include <iostream>
#include "../../extern/json.hpp"
#include "json.hpp"
using json = nlohmann::json;
CreatureLib::Library::GrowthRateLibrary* GrowthRatesBuilder::Build(const std::string& path) {

26640
json.hpp Normal file

File diff suppressed because it is too large Load Diff