Compare commits
7 Commits
d3e95dad61
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
0b4806b354
|
|||
|
fd24ce63c1
|
|||
|
efca5ca06d
|
|||
|
e2edf6129d
|
|||
|
fe6d13eb7e
|
|||
|
045e863fc3
|
|||
|
125db3bc5c
|
68
BuildAbilities.cpp
Normal file
68
BuildAbilities.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "BuildAbilities.hpp"
|
||||
#include <fstream>
|
||||
#include "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;
|
||||
case json::value_t ::string: {
|
||||
auto s = p.get<std::string>();
|
||||
realParameters.Append(new CreatureLib::Library::EffectParameter(ArbUt::StringView(s.c_str(), s.length())));
|
||||
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
11
BuildAbilities.hpp
Normal 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
|
||||
@@ -83,6 +83,12 @@ PkmnLib::Library::ItemLibrary* BuildItems::Build(const std::string& path) {
|
||||
case json::value_t::number_float:
|
||||
parameters.Append(new CreatureLib::Library::EffectParameter(p.get<float>()));
|
||||
break;
|
||||
case json::value_t ::string: {
|
||||
auto s = p.get<std::string>();
|
||||
parameters.Append(
|
||||
new CreatureLib::Library::EffectParameter(ArbUt::StringView(s.c_str(), s.length())));
|
||||
break;
|
||||
}
|
||||
default: continue;
|
||||
}
|
||||
}
|
||||
@@ -91,9 +97,44 @@ PkmnLib::Library::ItemLibrary* BuildItems::Build(const std::string& path) {
|
||||
100, ArbUt::StringView(effectName.get<std::string>().c_str()), parameters);
|
||||
}
|
||||
|
||||
CreatureLib::Library::SecondaryEffect* battleEffect = nullptr;
|
||||
auto& battleEffectJson = val["battleEffect"];
|
||||
if (battleEffectJson != nullptr) {
|
||||
auto& effectName = battleEffectJson["name"];
|
||||
auto& parametersJson = battleEffectJson["parameters"];
|
||||
ArbUt::List<CreatureLib::Library::EffectParameter*> parameters;
|
||||
if (parametersJson != nullptr) {
|
||||
for (auto& kv : parametersJson.items()) {
|
||||
auto& p = kv.value();
|
||||
auto t = p.type();
|
||||
switch (t) {
|
||||
case json::value_t::boolean:
|
||||
parameters.Append(new CreatureLib::Library::EffectParameter(p.get<bool>()));
|
||||
break;
|
||||
case json::value_t::number_integer:
|
||||
case json::value_t::number_unsigned:
|
||||
parameters.Append(new CreatureLib::Library::EffectParameter(p.get<int64_t>()));
|
||||
break;
|
||||
case json::value_t::number_float:
|
||||
parameters.Append(new CreatureLib::Library::EffectParameter(p.get<float>()));
|
||||
break;
|
||||
case json::value_t ::string: {
|
||||
auto s = p.get<std::string>();
|
||||
parameters.Append(
|
||||
new CreatureLib::Library::EffectParameter(ArbUt::StringView(s.c_str(), s.length())));
|
||||
break;
|
||||
}
|
||||
default: continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
battleEffect = new CreatureLib::Library::SecondaryEffect(
|
||||
100, ArbUt::StringView(effectName.get<std::string>().c_str()), parameters);
|
||||
}
|
||||
|
||||
auto item = new PkmnLib::Library::Item(ArbUt::StringView(_name.get<std::string>().c_str()), itemType,
|
||||
CreatureLib::Library::BattleItemCategory::None, _price.get<int32_t>(),
|
||||
effect, flags, _flingPower.get<uint8_t>());
|
||||
effect, battleEffect, flags, _flingPower.get<uint8_t>());
|
||||
lib->Insert(item->GetName(), item);
|
||||
}
|
||||
return lib;
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#include "BuildLibrary.hpp"
|
||||
#include <PkmnLib/ScriptResolving/AngelScript/AngelScriptResolver.hpp>
|
||||
#include "BuildAbilities.hpp"
|
||||
|
||||
using namespace PkmnLib::Battling;
|
||||
|
||||
size_t TypeTimeMs;
|
||||
size_t NaturesTimeMs;
|
||||
size_t MovesTimeMs;
|
||||
size_t AbilitiesTimeMs;
|
||||
size_t SpeciesTimeMs;
|
||||
size_t ItemsTimeMs;
|
||||
size_t GrowthRateTimeMs;
|
||||
@@ -40,9 +42,12 @@ BattleLibrary* BuildLibrary::Build(const std::string& pathString,
|
||||
MovesTimeMs =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
|
||||
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 =
|
||||
BuildSpecies::BuildLibrary((path / "Pokemon.json").generic_string(), typesLibrary, movesLibrary);
|
||||
auto* speciesLibrary = BuildSpecies::BuildLibrary((path / "Pokemon.json").generic_string(), typesLibrary,
|
||||
talentsLibraries, movesLibrary);
|
||||
SpeciesTimeMs =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
|
||||
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 staticLibrary = new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary,
|
||||
growthRates, typesLibrary, natureLibrary);
|
||||
auto staticLibrary =
|
||||
new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary, growthRates,
|
||||
typesLibrary, talentsLibraries, natureLibrary);
|
||||
|
||||
begin = std::chrono::steady_clock::now();
|
||||
auto scriptResolver = BattleLibrary::CreateScriptResolver();
|
||||
@@ -89,7 +95,17 @@ BattleLibrary* BuildLibrary::Build(const std::string& pathString,
|
||||
continue;
|
||||
std::ifstream in(dirEntry.path());
|
||||
std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
asScriptResolver->CreateScript((const char*)dirEntry.path().c_str(), contents.c_str());
|
||||
const auto* p = dirEntry.path().c_str();
|
||||
// Windows defines paths as widechars, so we need to convert it to a normal human const char*
|
||||
#if WINDOWS
|
||||
std::mbstate_t state = std::mbstate_t();
|
||||
int textLen = 1 + std::wcsrtombs(NULL, &p, 0, &state);
|
||||
std::vector<char> storeTextBuffer(textLen);
|
||||
std::wcsrtombs(&storeTextBuffer[0], &p, textLen, &state);
|
||||
asScriptResolver->CreateScript(&storeTextBuffer[0], contents.c_str());
|
||||
#else
|
||||
asScriptResolver->CreateScript(p, contents.c_str());
|
||||
#endif
|
||||
}
|
||||
asScriptResolver->FinalizeModule();
|
||||
ScriptsTimeMs =
|
||||
|
||||
@@ -8,8 +8,8 @@ using json = nlohmann::json;
|
||||
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"; \
|
||||
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << errorKeyFunc() << path \
|
||||
<< "'\n"; \
|
||||
return nullptr; \
|
||||
}
|
||||
|
||||
@@ -89,6 +89,12 @@ PkmnLib::Library::MoveLibrary* BuildMoves::Build(const std::string& path,
|
||||
case json::value_t::number_float:
|
||||
parameters.Append(new CreatureLib::Library::EffectParameter(p.get<float>()));
|
||||
break;
|
||||
case json::value_t ::string: {
|
||||
auto s = p.get<std::string>();
|
||||
parameters.Append(new CreatureLib::Library::EffectParameter(
|
||||
ArbUt::StringView(s.c_str(), s.length())));
|
||||
break;
|
||||
}
|
||||
default: continue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string& path,
|
||||
const CreatureLib::Library::TypeLibrary* types,
|
||||
const CreatureLib::Library::TalentLibrary* talentLibrary,
|
||||
const PkmnLib::Library::MoveLibrary* moveLibrary) {
|
||||
std::ifstream fileStream(path.c_str());
|
||||
if (fileStream.fail()) {
|
||||
@@ -50,7 +51,7 @@ PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string&
|
||||
|
||||
auto& defaultForme = _formes["default"];
|
||||
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(
|
||||
_id.get<uint16_t>(), ArbUt::StringView(_species.get<std::string>().c_str()), forme,
|
||||
_genderRatio.get<int8_t>() / static_cast<float>(100),
|
||||
@@ -62,7 +63,7 @@ PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string&
|
||||
if (formeIt.key() == "default") {
|
||||
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)
|
||||
return 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 std::string& baseKeyName, const std::string& path,
|
||||
const CreatureLib::Library::TypeLibrary* typeLibrary,
|
||||
const CreatureLib::Library::TalentLibrary* talentLibrary,
|
||||
const PkmnLib::Library::MoveLibrary* moveLibrary) {
|
||||
GET(forme, abilities, 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 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()) {
|
||||
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()) {
|
||||
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);
|
||||
|
||||
@@ -11,11 +11,13 @@ class BuildSpecies {
|
||||
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 CreatureLib::Library::TalentLibrary* talentLibrary,
|
||||
const PkmnLib::Library::MoveLibrary* moveLibrary);
|
||||
|
||||
public:
|
||||
static PkmnLib::Library::SpeciesLibrary* BuildLibrary(const std::string& path,
|
||||
const CreatureLib::Library::TypeLibrary* types,
|
||||
const CreatureLib::Library::TalentLibrary* talentLibrary,
|
||||
const PkmnLib::Library::MoveLibrary* moveLibrary);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user