Deal with setting targets
This commit is contained in:
61
test_runner/BuildData/BuildLibrary.hpp
Normal file
61
test_runner/BuildData/BuildLibrary.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#ifndef PKMNLIB_AI_BUILDLIBRARY_HPP
|
||||
#define PKMNLIB_AI_BUILDLIBRARY_HPP
|
||||
|
||||
#include <PkmnLib/Battling/Library/BattleLibrary.hpp>
|
||||
#include <filesystem>
|
||||
#include "BuildMoves.hpp"
|
||||
#include "BuildNatures.hpp"
|
||||
#include "BuildSpecies.hpp"
|
||||
#include "BuildTypes.hpp"
|
||||
#include "GrowthRatesBuilder.hpp"
|
||||
|
||||
class BuildLibrary {
|
||||
static PkmnLib::Library::TimeOfDay GetTime() { return PkmnLib::Library::TimeOfDay::Morning; }
|
||||
|
||||
public:
|
||||
static PkmnLib::Battling::BattleLibrary* Build(const std::string& pathString) {
|
||||
auto path = std::filesystem::path(pathString);
|
||||
auto* typesLibrary = BuildTypes::Build(path / "Types.csv");
|
||||
auto* natureLibrary = BuildNatures::Build(path / "Natures.csv");
|
||||
auto* movesLibrary = BuildMoves::Build(path / "Moves.json", typesLibrary);
|
||||
auto* speciesLibrary = BuildSpecies::BuildLibrary(path / "Pokemon.json", typesLibrary, movesLibrary);
|
||||
auto* itemsLibrary = BuildItems::Build(path / "Items.json");
|
||||
auto* growthRates = GrowthRatesBuilder::Build(path / "GrowthRates.json");
|
||||
auto scriptsPath = path / "Scripts";
|
||||
|
||||
if (typesLibrary == nullptr || speciesLibrary == nullptr || natureLibrary == nullptr ||
|
||||
movesLibrary == nullptr || itemsLibrary == nullptr || growthRates == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto settings = new PkmnLib::Library::LibrarySettings(100, 4, 4096);
|
||||
auto staticLibrary = new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary,
|
||||
growthRates, typesLibrary, natureLibrary);
|
||||
|
||||
auto scriptResolver = PkmnLib::Battling::BattleLibrary::CreateScriptResolver();
|
||||
|
||||
auto battleLib = new PkmnLib::Battling::BattleLibrary(
|
||||
staticLibrary, new PkmnLib::Battling::StatCalculator(), new PkmnLib::Battling::DamageLibrary(),
|
||||
new PkmnLib::Battling::ExperienceLibrary(), scriptResolver, new PkmnLib::Battling::MiscLibrary(GetTime));
|
||||
|
||||
scriptResolver->Initialize(battleLib);
|
||||
|
||||
auto asScriptResolver = dynamic_cast<AngelScriptResolver*>(scriptResolver);
|
||||
|
||||
for (const auto& dirEntry : std::filesystem::recursive_directory_iterator(scriptsPath)) {
|
||||
if (dirEntry.is_directory())
|
||||
continue;
|
||||
if (dirEntry.path().parent_path().stem() == "Interfaces")
|
||||
continue;
|
||||
if (dirEntry.path().extension() != ".as")
|
||||
continue;
|
||||
std::ifstream in(dirEntry.path());
|
||||
std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
asScriptResolver->CreateScript(dirEntry.path().c_str(), contents.c_str());
|
||||
}
|
||||
asScriptResolver->FinalizeModule();
|
||||
return battleLib;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // PKMNLIB_AI_BUILDLIBRARY_HPP
|
||||
@@ -70,7 +70,17 @@ u8 RunBattle(ArbUt::Random& rand, const PkmnLib::Battling::BattleLibrary* librar
|
||||
aiTwo->GetChoice(battle, static_cast<PkmnLib::Battling::Pokemon*>(battle->GetCreature(1, 0).GetValue()));
|
||||
EnsureNotNull(c1);
|
||||
EnsureNotNull(c2);
|
||||
Ensure(battle->TrySetChoice(c1));
|
||||
if (!battle->TrySetChoice(c1)) {
|
||||
if (c1->GetKind() == CreatureLib::Battling::TurnChoiceKind::Attack) {
|
||||
auto a = (CreatureLib::Battling::AttackTurnChoice*)c1;
|
||||
THROW("Failed to set move choice of move: '"
|
||||
<< a->GetAttack()->GetAttack()->GetName() << "' targeting side "
|
||||
<< (i32)a->GetTarget().GetSideIndex() << " and index " << (i32)a->GetTarget().GetCreatureIndex());
|
||||
} else {
|
||||
THROW("Failed to set choice of kind: "
|
||||
<< CreatureLib::Battling::TurnChoiceKindHelper::ToString(c1->GetKind()));
|
||||
}
|
||||
}
|
||||
Ensure(battle->TrySetChoice(c2));
|
||||
if (battle->GetCurrentTurn() >= 2000) {
|
||||
std::cout << ((CreatureLib::Battling::AttackTurnChoice*)c1)->GetAttack()->GetAttack()->GetName()
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "../extern/args.hpp"
|
||||
#include "AIResolver.hpp"
|
||||
#include "BuildData/BuildItems.hpp"
|
||||
#include "BuildData/BuildLibrary.hpp"
|
||||
#include "BuildData/BuildMoves.hpp"
|
||||
#include "BuildData/BuildNatures.hpp"
|
||||
#include "BuildData/BuildSpecies.hpp"
|
||||
@@ -11,20 +12,11 @@
|
||||
#include "BuildData/GrowthRatesBuilder.hpp"
|
||||
#include "Runner.hpp"
|
||||
|
||||
static const char* ScriptsPath = nullptr;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
args::ArgumentParser parser("PkmnLib AI Runner.", "");
|
||||
args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
|
||||
|
||||
std::string workingDirectory;
|
||||
std::string typesFile = "Types.csv";
|
||||
std::string naturesFile = "Natures.csv";
|
||||
std::string pokemonFile = "Pokemon.json";
|
||||
std::string moveFile = "Moves.json";
|
||||
std::string itemsFile = "Items.json";
|
||||
std::string growthRatesFile = "GrowthRates.json";
|
||||
std::string scriptsPath = "Scripts";
|
||||
|
||||
args::HelpFlag helpFlag(parser, "help", "Display this help menu", {'h', "help"});
|
||||
args::ValueFlag<std::string> workingDirFlag(parser, "Working Directory", "Which work directory to use.",
|
||||
@@ -52,47 +44,11 @@ int main(int argc, char** argv) {
|
||||
if (!workingDirectory.empty()) {
|
||||
chdir(std::filesystem::path(workingDirectory).c_str());
|
||||
}
|
||||
|
||||
auto* typesLibrary = BuildTypes::Build(typesFile);
|
||||
auto* natureLibrary = BuildNatures::Build(naturesFile);
|
||||
auto* movesLibrary = BuildMoves::Build(moveFile, typesLibrary);
|
||||
auto* speciesLibrary = BuildSpecies::BuildLibrary(pokemonFile, typesLibrary, movesLibrary);
|
||||
auto* itemsLibrary = BuildItems::Build(itemsFile);
|
||||
auto* growthRates = GrowthRatesBuilder::Build(growthRatesFile);
|
||||
ScriptsPath = scriptsPath.c_str();
|
||||
|
||||
if (typesLibrary == nullptr || speciesLibrary == nullptr || natureLibrary == nullptr || movesLibrary == nullptr ||
|
||||
itemsLibrary == nullptr || growthRates == nullptr) {
|
||||
auto battleLib = BuildLibrary::Build("");
|
||||
if (battleLib == nullptr) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto settings = new PkmnLib::Library::LibrarySettings(100, 4, 4096);
|
||||
auto staticLibrary = new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary,
|
||||
growthRates, typesLibrary, natureLibrary);
|
||||
|
||||
auto scriptResolver = PkmnLib::Battling::BattleLibrary::CreateScriptResolver();
|
||||
|
||||
auto battleLib = new PkmnLib::Battling::BattleLibrary(
|
||||
staticLibrary, new PkmnLib::Battling::StatCalculator(), new PkmnLib::Battling::DamageLibrary(),
|
||||
new PkmnLib::Battling::ExperienceLibrary(), scriptResolver, new PkmnLib::Battling::MiscLibrary());
|
||||
|
||||
scriptResolver->Initialize(battleLib);
|
||||
|
||||
auto asScriptResolver = dynamic_cast<AngelScriptResolver*>(scriptResolver);
|
||||
|
||||
for (const auto& dirEntry : std::filesystem::recursive_directory_iterator(ScriptsPath)) {
|
||||
if (dirEntry.is_directory())
|
||||
continue;
|
||||
if (dirEntry.path().parent_path().stem() == "Interfaces")
|
||||
continue;
|
||||
if (dirEntry.path().extension() != ".as")
|
||||
continue;
|
||||
std::ifstream in(dirEntry.path());
|
||||
std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
asScriptResolver->CreateScript(dirEntry.path().c_str(), contents.c_str());
|
||||
}
|
||||
asScriptResolver->FinalizeModule();
|
||||
|
||||
auto ai1 = AIResolver::Resolve(ArbUt::StringView(ai1Flag.Get().c_str(), ai1Flag.Get().size()));
|
||||
auto ai2 = AIResolver::Resolve(ArbUt::StringView(ai2Flag.Get().c_str(), ai1Flag.Get().size()));
|
||||
|
||||
@@ -100,7 +56,9 @@ int main(int argc, char** argv) {
|
||||
Runner::Run(battleLib, ai1, ai2, runsFlag.Get());
|
||||
} catch (ArbUt::Exception& e) {
|
||||
std::cout << std::endl;
|
||||
std::cout << "Exception with message: " << std::endl << e.what() << std::endl << e.GetStacktrace(10) << std::endl;
|
||||
std::cout << "Exception with message: " << std::endl
|
||||
<< e.what() << std::endl
|
||||
<< e.GetStacktrace(10) << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user