2021-08-22 10:10:26 +00:00
|
|
|
#include "BuildItems.hpp"
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2021-08-28 14:52:47 +00:00
|
|
|
#include "json.hpp"
|
2021-08-22 10:10:26 +00:00
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
|
|
#define GET(o, objectKey, key) \
|
2021-08-28 13:56:08 +00:00
|
|
|
auto& _##objectKey = o[#objectKey]; \
|
2021-08-22 10:10:26 +00:00
|
|
|
if (_##objectKey.is_null()) { \
|
2021-08-28 13:56:08 +00:00
|
|
|
auto errorKeyFunc = [=]() { return key; }; \
|
|
|
|
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << errorKeyFunc() << path \
|
|
|
|
<< "'\n"; \
|
2021-08-22 10:10:26 +00:00
|
|
|
return nullptr; \
|
|
|
|
}
|
|
|
|
|
|
|
|
static CreatureLib::Library::ItemCategory ParseItemCategory(std::string& in) {
|
|
|
|
std::transform(in.begin(), in.end(), in.end(), tolower);
|
|
|
|
if (in == "item")
|
|
|
|
return CreatureLib::Library::ItemCategory::MiscItem;
|
|
|
|
if (in == "medicine")
|
|
|
|
return CreatureLib::Library::ItemCategory::Medicine;
|
|
|
|
if (in == "berry")
|
|
|
|
return CreatureLib::Library::ItemCategory::Berry;
|
|
|
|
if (in == "mail")
|
|
|
|
return CreatureLib::Library::ItemCategory::Mail;
|
|
|
|
if (in == "key")
|
|
|
|
return CreatureLib::Library::ItemCategory::KeyItem;
|
|
|
|
if (in == "pokeball")
|
|
|
|
return CreatureLib::Library::ItemCategory::CaptureDevice;
|
|
|
|
if (in == "tm")
|
|
|
|
return CreatureLib::Library::ItemCategory::MoveLearner;
|
|
|
|
std::cout << "Unknown Item Type: '" << in << "'\n";
|
|
|
|
return static_cast<CreatureLib::Library::ItemCategory>(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
PkmnLib::Library::ItemLibrary* BuildItems::Build(const std::string& path) {
|
|
|
|
std::ifstream fileStream(path.c_str());
|
|
|
|
if (fileStream.fail()) {
|
|
|
|
std::cout << "Failed to load Items data file at '" << path << "'\n";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto lib = new PkmnLib::Library::ItemLibrary();
|
|
|
|
json j;
|
|
|
|
fileStream >> j;
|
|
|
|
for (const auto& i : j.items()) {
|
|
|
|
if (i.key().starts_with("$")) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-28 13:56:08 +00:00
|
|
|
auto& val = i.value();
|
2021-08-22 10:10:26 +00:00
|
|
|
GET(val, name, i);
|
|
|
|
GET(val, itemType, i);
|
|
|
|
GET(val, flags, i);
|
|
|
|
GET(val, price, i);
|
|
|
|
GET(val, flingPower, i);
|
|
|
|
auto itemTypeStr = _itemType.get<std::string>();
|
|
|
|
CreatureLib::Library::ItemCategory itemType = ParseItemCategory(itemTypeStr);
|
2021-08-29 10:14:27 +00:00
|
|
|
if (static_cast<i32>(itemType) == 255)
|
2021-08-22 10:10:26 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2021-08-29 10:14:27 +00:00
|
|
|
auto flags = std::unordered_set<u32>();
|
2021-08-28 13:56:08 +00:00
|
|
|
for (auto& flagIndex : _flags.items()) {
|
2021-08-22 10:10:26 +00:00
|
|
|
flags.insert(ArbUt::StringView(flagIndex.value().get<std::string>().c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureLib::Library::SecondaryEffect* effect = nullptr;
|
2021-08-28 13:56:08 +00:00
|
|
|
auto& effectJson = val["effect"];
|
2021-08-22 10:10:26 +00:00
|
|
|
if (effectJson != nullptr) {
|
2021-08-28 13:56:08 +00:00
|
|
|
auto& effectName = effectJson["name"];
|
|
|
|
auto& parametersJson = effectJson["parameters"];
|
2021-08-22 10:10:26 +00:00
|
|
|
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;
|
|
|
|
default: continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
effect = 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>());
|
|
|
|
lib->Insert(item->GetName(), item);
|
|
|
|
}
|
|
|
|
return lib;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef GET
|