#include "BuildItems.hpp" #include #include #include "../../extern/json.hpp" using json = nlohmann::json; #define GET(o, objectKey, key) \ auto _##objectKey = o[#objectKey]; \ if (_##objectKey.is_null()) { \ std::cout << "Failed to retrieve key '" << #objectKey << "' for object with value '" << key << "' in file '" \ << path << "'\n"; \ 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(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 (auto i : j.items()) { auto val = i.value(); GET(val, name, i); GET(val, itemType, i); GET(val, flags, i); GET(val, price, i); GET(val, flingPower, i); auto itemTypeStr = _itemType.get(); CreatureLib::Library::ItemCategory itemType = ParseItemCategory(itemTypeStr); if (static_cast(itemType) == 255) return nullptr; auto flags = std::unordered_set(); for (auto flagIndex : _flags.items()) { flags.insert(Arbutils::CaseInsensitiveConstString(flagIndex.value().get())); } auto item = new PkmnLib::Library::Item(Arbutils::CaseInsensitiveConstString(_name.get()), itemType, CreatureLib::Library::BattleItemCategory::None, _price.get(), flags, _flingPower.get()); lib->Insert(item->GetName(), item); } return lib; } #undef GET