Update for pkmnlib reworks

This commit is contained in:
Deukhoofd 2021-11-20 09:55:09 +01:00
parent d3e95dad61
commit 125db3bc5c
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
5 changed files with 98 additions and 10 deletions

61
BuildAbilities.cpp Normal file
View File

@ -0,0 +1,61 @@
#include "BuildAbilities.hpp"
#include "../../extern/json.hpp"
using json = nlohmann::json;
#define GET(o, objectKey, key) \
auto& _##objectKey = o[#objectKey]; \
if (_##objectKey.is_null()) { \
auto errorKeyFunc = [=]() { return key; }; \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << errorKeyFunc() << path \
<< "'\n"; \
return nullptr; \
}
CreatureLib::Library::TalentLibrary* BuildAbilities::Build(const std::string& path) {
std::ifstream fileStream(path.c_str());
if (fileStream.fail()) {
std::cout << "Failed to load Abilities data file at '" << path << "'\n";
return nullptr;
}
auto lib = new CreatureLib::Library::TalentLibrary();
json j;
fileStream >> j;
for (const auto& i : j.items()) {
if (i.key().starts_with("$")) {
continue;
}
auto& val = i.value();
auto _effect = val["effect"];
auto _parameters = val["parameters"];
ArbUt::StringView effectName = ""_cnc;
if (!_effect.empty()) {
effectName = ArbUt::StringView(_effect.get<std::string>().c_str(), _effect.get<std::string>().length());
}
ArbUt::List<CreatureLib::Library::EffectParameter*> realParameters;
if (!_parameters.empty()) {
for (auto& kv : _parameters.items()) {
auto& p = kv.value();
auto t = p.type();
switch (t) {
case json::value_t::boolean:
realParameters.Append(new CreatureLib::Library::EffectParameter(p.get<bool>()));
break;
case json::value_t::number_integer:
case json::value_t::number_unsigned:
realParameters.Append(new CreatureLib::Library::EffectParameter(p.get<int64_t>()));
break;
case json::value_t::number_float:
realParameters.Append(new CreatureLib::Library::EffectParameter(p.get<float>()));
break;
default: continue;
}
}
}
auto key = ArbUt::StringView(i.key().c_str(), i.key().length());
lib->Insert(key, new CreatureLib::Library::Talent(key, effectName, realParameters));
}
return lib;
}

11
BuildAbilities.hpp Normal file
View File

@ -0,0 +1,11 @@
#ifndef PKMNLIB_AI_BUILDABILITIES_HPP
#define PKMNLIB_AI_BUILDABILITIES_HPP
#include <CreatureLib/Library/TalentLibrary.hpp>
class BuildAbilities {
public:
static CreatureLib::Library::TalentLibrary* Build(const std::string& path);
};
#endif // PKMNLIB_AI_BUILDABILITIES_HPP

View File

@ -1,11 +1,13 @@
#include "BuildLibrary.hpp" #include "BuildLibrary.hpp"
#include <PkmnLib/ScriptResolving/AngelScript/AngelScriptResolver.hpp> #include <PkmnLib/ScriptResolving/AngelScript/AngelScriptResolver.hpp>
#include "BuildAbilities.hpp"
using namespace PkmnLib::Battling; using namespace PkmnLib::Battling;
size_t TypeTimeMs; size_t TypeTimeMs;
size_t NaturesTimeMs; size_t NaturesTimeMs;
size_t MovesTimeMs; size_t MovesTimeMs;
size_t AbilitiesTimeMs;
size_t SpeciesTimeMs; size_t SpeciesTimeMs;
size_t ItemsTimeMs; size_t ItemsTimeMs;
size_t GrowthRateTimeMs; size_t GrowthRateTimeMs;
@ -40,9 +42,12 @@ BattleLibrary* BuildLibrary::Build(const std::string& pathString,
MovesTimeMs = MovesTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count(); std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now(); begin = std::chrono::steady_clock::now();
auto* talentsLibraries = BuildAbilities::Build((path / "Abilities.json").generic_string());
AbilitiesTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
auto* speciesLibrary = auto* speciesLibrary = BuildSpecies::BuildLibrary((path / "Pokemon.json").generic_string(), typesLibrary,
BuildSpecies::BuildLibrary((path / "Pokemon.json").generic_string(), typesLibrary, movesLibrary); talentsLibraries, movesLibrary);
SpeciesTimeMs = SpeciesTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count(); std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now(); begin = std::chrono::steady_clock::now();
@ -64,8 +69,9 @@ BattleLibrary* BuildLibrary::Build(const std::string& pathString,
} }
auto settings = new PkmnLib::Library::LibrarySettings(100, 4, 4096); auto settings = new PkmnLib::Library::LibrarySettings(100, 4, 4096);
auto staticLibrary = new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary, auto staticLibrary =
growthRates, typesLibrary, natureLibrary); new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary, growthRates,
typesLibrary, talentsLibraries, natureLibrary);
begin = std::chrono::steady_clock::now(); begin = std::chrono::steady_clock::now();
auto scriptResolver = BattleLibrary::CreateScriptResolver(); auto scriptResolver = BattleLibrary::CreateScriptResolver();

View File

@ -13,6 +13,7 @@
PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string& path, PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string& path,
const CreatureLib::Library::TypeLibrary* types, const CreatureLib::Library::TypeLibrary* types,
const CreatureLib::Library::TalentLibrary* talentLibrary,
const PkmnLib::Library::MoveLibrary* moveLibrary) { const PkmnLib::Library::MoveLibrary* moveLibrary) {
std::ifstream fileStream(path.c_str()); std::ifstream fileStream(path.c_str());
if (fileStream.fail()) { if (fileStream.fail()) {
@ -50,7 +51,7 @@ PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string&
auto& defaultForme = _formes["default"]; auto& defaultForme = _formes["default"];
if (!defaultForme.is_null()) { if (!defaultForme.is_null()) {
auto forme = BuildForme("default", defaultForme, it.key(), path, types, moveLibrary); auto forme = BuildForme("default", defaultForme, it.key(), path, types, talentLibrary, moveLibrary);
species = new PkmnLib::Library::PokemonSpecies( species = new PkmnLib::Library::PokemonSpecies(
_id.get<uint16_t>(), ArbUt::StringView(_species.get<std::string>().c_str()), forme, _id.get<uint16_t>(), ArbUt::StringView(_species.get<std::string>().c_str()), forme,
_genderRatio.get<int8_t>() / static_cast<float>(100), _genderRatio.get<int8_t>() / static_cast<float>(100),
@ -62,7 +63,7 @@ PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string&
if (formeIt.key() == "default") { if (formeIt.key() == "default") {
continue; continue;
} }
auto forme = BuildForme(formeIt.key(), formeIt.value(), it.key(), path, types, moveLibrary); auto forme = BuildForme(formeIt.key(), formeIt.value(), it.key(), path, types, talentLibrary, moveLibrary);
if (forme == nullptr) if (forme == nullptr)
return nullptr; return nullptr;
if (species == nullptr) { if (species == nullptr) {
@ -110,6 +111,7 @@ inline void BuildLevelMove(const json& levelMoveObj, const PkmnLib::Library::Mov
const PkmnLib::Library::PokemonForme* BuildSpecies::BuildForme(const std::string& name, const json& forme, const PkmnLib::Library::PokemonForme* BuildSpecies::BuildForme(const std::string& name, const json& forme,
const std::string& baseKeyName, const std::string& path, const std::string& baseKeyName, const std::string& path,
const CreatureLib::Library::TypeLibrary* typeLibrary, const CreatureLib::Library::TypeLibrary* typeLibrary,
const CreatureLib::Library::TalentLibrary* talentLibrary,
const PkmnLib::Library::MoveLibrary* moveLibrary) { const PkmnLib::Library::MoveLibrary* moveLibrary) {
GET(forme, abilities, baseKeyName + " -> " + name); GET(forme, abilities, baseKeyName + " -> " + name);
GET(forme, hiddenAbilities, baseKeyName + " -> " + name); GET(forme, hiddenAbilities, baseKeyName + " -> " + name);
@ -128,16 +130,22 @@ const PkmnLib::Library::PokemonForme* BuildSpecies::BuildForme(const std::string
} }
auto stats = ParseStatistics(_baseStats); auto stats = ParseStatistics(_baseStats);
auto abilities = ArbUt::List<ArbUt::StringView>(_abilities.size()); auto abilities = ArbUt::List<ArbUt::BorrowedPtr<const CreatureLib::Library::Talent>>(_abilities.size());
for (const auto& ab : _abilities.items()) { for (const auto& ab : _abilities.items()) {
auto s = ab.value().get<std::string>(); auto s = ab.value().get<std::string>();
abilities.Append(ArbUt::StringView(s.c_str(), s.length())); auto tryAbility = talentLibrary->TryGet(ArbUt::StringView(s.c_str(), s.length()));
if (!tryAbility.has_value())
THROW("Unknown ability " << s);
abilities.Append(tryAbility.value());
} }
auto hiddenAbilities = ArbUt::List<ArbUt::StringView>(_hiddenAbilities.size()); auto hiddenAbilities = ArbUt::List<ArbUt::BorrowedPtr<const CreatureLib::Library::Talent>>(_hiddenAbilities.size());
for (const auto& ab : _hiddenAbilities.items()) { for (const auto& ab : _hiddenAbilities.items()) {
auto s = ab.value().get<std::string>(); auto s = ab.value().get<std::string>();
hiddenAbilities.Append(ArbUt::StringView(s.c_str(), s.length())); auto tryAbility = talentLibrary->TryGet(ArbUt::StringView(s.c_str(), s.length()));
if (!tryAbility.has_value())
THROW("Unknown ability " << s);
hiddenAbilities.Append(tryAbility.value());
} }
auto moves = new PkmnLib::Library::LearnableMoves(100); auto moves = new PkmnLib::Library::LearnableMoves(100);

View File

@ -11,11 +11,13 @@ class BuildSpecies {
static const PkmnLib::Library::PokemonForme* BuildForme(const std::string& name, const json& forme, static const PkmnLib::Library::PokemonForme* BuildForme(const std::string& name, const json& forme,
const std::string& baseKeyName, const std::string& path, const std::string& baseKeyName, const std::string& path,
const CreatureLib::Library::TypeLibrary* typeLibrary, const CreatureLib::Library::TypeLibrary* typeLibrary,
const CreatureLib::Library::TalentLibrary* talentLibrary,
const PkmnLib::Library::MoveLibrary* moveLibrary); const PkmnLib::Library::MoveLibrary* moveLibrary);
public: public:
static PkmnLib::Library::SpeciesLibrary* BuildLibrary(const std::string& path, static PkmnLib::Library::SpeciesLibrary* BuildLibrary(const std::string& path,
const CreatureLib::Library::TypeLibrary* types, const CreatureLib::Library::TypeLibrary* types,
const CreatureLib::Library::TalentLibrary* talentLibrary,
const PkmnLib::Library::MoveLibrary* moveLibrary); const PkmnLib::Library::MoveLibrary* moveLibrary);
}; };