From d11722efdd8c87571660f7093709a245bb15c2b4 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 25 Aug 2021 21:17:44 +0200 Subject: [PATCH] Adds mock functions for a bunch more data. --- ...{BattleBuilder.hpp => BattleFunctions.hpp} | 10 +-- src/Tester/AngelScript/MiscMockFunctions.hpp | 74 +++++++++++++++++++ src/main.cpp | 8 +- 3 files changed, 84 insertions(+), 8 deletions(-) rename src/Tester/AngelScript/{BattleBuilder.hpp => BattleFunctions.hpp} (95%) create mode 100644 src/Tester/AngelScript/MiscMockFunctions.hpp diff --git a/src/Tester/AngelScript/BattleBuilder.hpp b/src/Tester/AngelScript/BattleFunctions.hpp similarity index 95% rename from src/Tester/AngelScript/BattleBuilder.hpp rename to src/Tester/AngelScript/BattleFunctions.hpp index 24a9bf7..cbb80dd 100644 --- a/src/Tester/AngelScript/BattleBuilder.hpp +++ b/src/Tester/AngelScript/BattleFunctions.hpp @@ -1,5 +1,5 @@ -#ifndef POKEMONSCRIPTTESTER_BATTLEBUILDER_HPP -#define POKEMONSCRIPTTESTER_BATTLEBUILDER_HPP +#ifndef POKEMONSCRIPTTESTER_BATTLEFUNCTIONS_HPP +#define POKEMONSCRIPTTESTER_BATTLEFUNCTIONS_HPP #include #include @@ -8,7 +8,7 @@ #include #include "../TestEnvironment.hpp" -class BattleBuilder { +class BattleFunctions { static PkmnLib::Battling::Battle* CreateSimpleBattle(u32 seed, const ArbUt::StringView& species1, const ArbUt::StringView& species2, u8 level) { auto* ctx = asGetActiveContext(); @@ -33,7 +33,7 @@ class BattleBuilder { .IsAllowedExperienceGain(false) .Build(); auto p2 = new CreatureLib::Battling::CreatureParty(1); - p1->SwapInto(0, mon2); + p2->SwapInto(0, mon2); auto battle = new PkmnLib::Battling::Battle( lib, @@ -107,4 +107,4 @@ public: } }; -#endif // POKEMONSCRIPTTESTER_BATTLEBUILDER_HPP +#endif // POKEMONSCRIPTTESTER_BATTLEFUNCTIONS_HPP diff --git a/src/Tester/AngelScript/MiscMockFunctions.hpp b/src/Tester/AngelScript/MiscMockFunctions.hpp new file mode 100644 index 0000000..aa85d73 --- /dev/null +++ b/src/Tester/AngelScript/MiscMockFunctions.hpp @@ -0,0 +1,74 @@ +#ifndef POKEMONSCRIPTTESTER_MISCMOCKFUNCTIONS_HPP +#define POKEMONSCRIPTTESTER_MISCMOCKFUNCTIONS_HPP + +#include + +class MiscMockFunctions { + static CScriptHandle CreateMoveScript(const ArbUt::StringView& name) { + auto script = Globals::Library.GetValue()->GetScriptResolver()->LoadScript(ScriptCategory::Attack, name); + if (script != nullptr) { + auto* ctx = asGetActiveContext(); + TestEnvironment* env = static_cast(ctx->GetUserData()); + env->AddGarbage(script); + } + auto p = dynamic_cast(script); + EnsureNotNull(p); + auto handle = CScriptHandle(p->GetRawAngelscriptObject(), p->GetRawAngelscriptObject()->GetObjectType()); + return handle; + } + + static CreatureLib::Battling::ExecutingAttack* CreateExecutingMove(const ArbUt::StringView& moveName, + PkmnLib::Battling::Pokemon* user, + PkmnLib::Battling::Pokemon* target) { + auto* ctx = asGetActiveContext(); + TestEnvironment* env = static_cast(ctx->GetUserData()); + + auto move = Globals::Library.GetValue()->GetMoveLibrary()->TryGet(moveName); + if (!move.has_value()) { + return {}; + } + auto learnedMove = + new PkmnLib::Battling::LearnedMove(move.value(), CreatureLib::Battling::AttackLearnMethod::Unknown); + env->AddGarbage(learnedMove); + + auto executingMove = new CreatureLib::Battling::ExecutingAttack( + {target}, 1, user, learnedMove, move.value().As(), nullptr); + env->AddGarbage(executingMove); + return executingMove; + } + + static CreatureLib::Battling::AttackTurnChoice* CreateMoveTurnChoice(const ArbUt::StringView& moveName, + PkmnLib::Battling::Pokemon* user, + u8 targetSide, u8 target) { + auto* ctx = asGetActiveContext(); + TestEnvironment* env = static_cast(ctx->GetUserData()); + + auto move = Globals::Library.GetValue()->GetMoveLibrary()->TryGet(moveName); + if (!move.has_value()) { + return {}; + } + auto learnedMove = + new PkmnLib::Battling::LearnedMove(move.value(), CreatureLib::Battling::AttackLearnMethod::Unknown); + env->AddGarbage(learnedMove); + + auto moveTurnChoice = new CreatureLib::Battling::AttackTurnChoice( + user, learnedMove, CreatureLib::Battling::CreatureIndex(targetSide, target)); + env->AddGarbage(moveTurnChoice); + return moveTurnChoice; + } + +public: + static void Register(AngelScriptResolver* scriptResolver) { + auto engine = scriptResolver->GetBuilder().GetEngine(); + Ensure(engine->RegisterGlobalFunction("ref@ CreateMoveScript(const constString&in name)", + asFUNCTION(CreateMoveScript), asCALL_CDECL) >= 0); + Ensure(engine->RegisterGlobalFunction( + "ExecutingMove@ CreateExecutingMove(const constString&in moveName, Pokemon@ user, Pokemon@ target)", + asFUNCTION(CreateExecutingMove), asCALL_CDECL) >= 0); + Ensure(engine->RegisterGlobalFunction("MoveTurnChoice@ CreateMoveTurnChoice(const constString&in moveName, " + "Pokemon@ user, uint8 targetSide, uint8 target)", + asFUNCTION(CreateMoveTurnChoice), asCALL_CDECL) >= 0); + } +}; + +#endif // POKEMONSCRIPTTESTER_MISCMOCKFUNCTIONS_HPP diff --git a/src/main.cpp b/src/main.cpp index 5799290..a72b3b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,8 @@ #include "../extern/args.hpp" #include "BuildData/BuildLibrary.hpp" #include "Globals.hpp" -#include "Tester/AngelScript/BattleBuilder.hpp" +#include "Tester/AngelScript/BattleFunctions.hpp" +#include "Tester/AngelScript/MiscMockFunctions.hpp" #include "Tester/AngelScript/TestFunctions.hpp" #include "Tester/TestRunner.hpp" @@ -41,9 +42,10 @@ int main(int argc, char** argv) { std::function initialize = [](PkmnLib::Battling::ScriptResolver* resolver) { auto* scriptResolver = dynamic_cast(resolver); - scriptResolver->DefineWord("TEST"); + scriptResolver->DefineWord("TESTS"); TestFunctions::Register(scriptResolver); - BattleBuilder::Register(scriptResolver); + BattleFunctions::Register(scriptResolver); + MiscMockFunctions::Register(scriptResolver); }; Globals::Library = BuildLibrary::Build("", initialize);