Build Item library.

This commit is contained in:
Deukhoofd 2020-02-13 11:50:01 +01:00
parent 1b45735326
commit dfa7e79ab2
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
6 changed files with 80 additions and 5 deletions

View File

@ -26,4 +26,4 @@ class PkmnLibConan(ConanFile):
self.copy("*.dll", "bin", "bin")
def requirements(self):
self.requires("PkmnLib/c91b1b09061f8e063aa37afe9ac2465479683f8e@pkmnlib/master")
self.requires("PkmnLib/eeedbdac030b9dc72b5e70b215f4fe7173a42f46@pkmnlib/master")

View File

@ -0,0 +1,56 @@
#include "BuildItems.hpp"
#include <fstream>
#include <iostream>
#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<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 Move 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<std::string>();
CreatureLib::Library::ItemCategory itemType = ParseItemCategory(itemTypeStr);
if (static_cast<int>(itemType) == 255) return nullptr;
auto item = new PkmnLib::Library::Item(
_name.get<std::string>(), itemType, CreatureLib::Library::BattleItemCategory::None, _price.get<int32_t>(),
_flags.get<std::unordered_set<std::string>>(), _flingPower.get<uint8_t>());
lib->LoadItem(item->GetName(), item);
}
return lib;
}
#undef GET

View File

@ -0,0 +1,10 @@
#ifndef GEN7TESTS_BUILDITEMS_HPP
#define GEN7TESTS_BUILDITEMS_HPP
#include <PkmnLib/Library/Items/ItemLibrary.hpp>
class BuildItems {
public:
static PkmnLib::Library::ItemLibrary* Build(const std::string& path);
};
#endif // GEN7TESTS_BUILDITEMS_HPP

View File

@ -67,3 +67,5 @@ PkmnLib::Library::MoveLibrary* BuildMoves::Build(const std::string& path,
return lib;
}
#undef GET

View File

@ -102,3 +102,5 @@ const PkmnLib::Library::PokemonForme* BuildSpecies::BuildForme(const std::string
name, _height.get<float>(), _weight.get<float>(), _baseExp.get<uint32_t>(), types, stats,
_abilities.get<std::vector<std::string>>(), _hiddenAbilities.get<std::vector<std::string>>(), nullptr);
}
#undef GET

View File

@ -1,5 +1,6 @@
#define CATCH_CONFIG_RUNNER
#include "../extern/catch.hpp"
#include "BuildData/BuildItems.hpp"
#include "BuildData/BuildMoves.hpp"
#include "BuildData/BuildNatures.hpp"
#include "BuildData/BuildSpecies.hpp"
@ -13,12 +14,14 @@ int main(int argc, char* argv[]) {
std::string naturesFile = "Natures.csv";
std::string pokemonFile = "Pokemon.json";
std::string moveFile = "Moves.json";
std::string itemsFile = "Items.json";
using namespace Catch::clara;
auto cli = session.cli() | Opt(pokemonFile, "Species")["--species"]("Which species file to load.") |
Opt(typesFile, "Types")["--types"]("Which Types file to load.") |
Opt(naturesFile, "Natures")["--natures"]("Which Natures file to load.");
Opt(moveFile, "Moves")["--moves"]("Which Moves file to load.");
Opt(naturesFile, "Natures")["--natures"]("Which Natures file to load.") |
Opt(moveFile, "Moves")["--moves"]("Which Moves file to load.") |
Opt(itemsFile, "Items")["--items"]("Which Items file to load.");
session.cli(cli);
@ -30,12 +33,14 @@ int main(int argc, char* argv[]) {
auto natureLibrary = BuildNatures::Build(naturesFile);
auto speciesLibrary = BuildSpecies::BuildLibrary(pokemonFile, typesLibrary);
auto movesLibrary = BuildMoves::Build(moveFile, typesLibrary);
auto itemsLibrary = BuildItems::Build(itemsFile);
if (typesLibrary == nullptr || speciesLibrary == nullptr || natureLibrary == nullptr || movesLibrary == nullptr)
if (typesLibrary == nullptr || speciesLibrary == nullptr || natureLibrary == nullptr || movesLibrary == nullptr ||
itemsLibrary == nullptr)
return 1;
auto settings = new PkmnLib::Library::LibrarySettings(100, 4, 4096);
auto library = new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, nullptr, nullptr,
auto library = new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary, nullptr,
typesLibrary, natureLibrary);
Library::SetStaticLib(library);