diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp index 1005daf..ffd8f10 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp @@ -2,6 +2,7 @@ #include #include #include "../../../../Battling/Battle/Battle.hpp" +#include "../../../../Battling/Pokemon/Pokemon.hpp" #include "../../AngelScriptScript.hpp" #include "../HelperFile.hpp" @@ -56,6 +57,10 @@ static asIScriptObject* AddVolatileWrapper(PkmnLib::Battling::Battle* obj, const return scriptObject->GetRawAngelscriptObject(); } +static u8 GetPokemonIndexWrapper(CreatureLib::Battling::BattleSide* obj, PkmnLib::Battling::Pokemon* pokemon) { + return obj->GetCreatureIndex(pokemon); +} + void RegisterBattleClass::RegisterBattleSide(asIScriptEngine* engine) { int r = engine->RegisterObjectMethod( "BattleSide", "bool SwapPositions(uint8 a, uint8 b)", @@ -65,9 +70,7 @@ void RegisterBattleClass::RegisterBattleSide(asIScriptEngine* engine) { asMETHOD(CreatureLib::Battling::BattleSide, GetSideIndex), asCALL_THISCALL); Ensure(r >= 0); r = engine->RegisterObjectMethod("BattleSide", "uint8 GetPokemonIndex(const Pokemon@ pokemon) const", - asMETHODPR(CreatureLib::Battling::BattleSide, GetCreatureIndex, - (const ArbUt::BorrowedPtr& c), u8), - asCALL_THISCALL); + asFUNCTION(GetPokemonIndexWrapper), asCALL_CDECL_OBJFIRST); Ensure(r >= 0); } diff --git a/tests/ScriptTests/ScriptTypeTests/Battle/BattleTests.cpp b/tests/ScriptTests/ScriptTypeTests/Battle/BattleTests.cpp new file mode 100644 index 0000000..b1928c3 --- /dev/null +++ b/tests/ScriptTests/ScriptTypeTests/Battle/BattleTests.cpp @@ -0,0 +1,82 @@ +#ifdef TESTS_BUILD +#include "../../../../extern/doctest.hpp" +#include "../../../../src/Battling/Battle/Battle.hpp" +#include "../../../../src/Battling/Pokemon/CreatePokemon.hpp" +#include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp" +#include "../../../TestLibrary/TestLibrary.hpp" + +static std::unordered_map _scripts = + std::unordered_map{{"testScript1", R"( +namespace Pokemon{ +[Pokemon effect=testScript1] +class testScript1 : PkmnScript { + bool testGetPokemonIndex(BattleSide@ b, const Pokemon@ pokemon){ return b.GetPokemonIndex(pokemon) == 0; } +}} +)"}}; + +struct ScriptData { + AngelScriptScript* Script = nullptr; + AngelScriptResolver* Resolver = nullptr; + asIScriptFunction* Func = nullptr; + asIScriptContext* Context = nullptr; + + ~ScriptData() { + Script->GetContextPool()->ReturnContextToPool(Context); + delete Script; + } +}; + +static AngelScriptResolver* _resolverCache = nullptr; +static AngelScriptResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) { + if (_resolverCache == nullptr) { + _resolverCache = dynamic_cast(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); + _resolverCache->Initialize(mainLib); + _resolverCache->CreateScript("testScript1", _scripts["testScript1"]); + _resolverCache->FinalizeModule(); + } + return _resolverCache; +} + +static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const ArbUt::StringView& funcName) { + auto lib = GetScriptResolver(mainLib); + auto s = lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc); + auto script = dynamic_cast(s); + REQUIRE(script != nullptr); + + auto ctxPool = script->GetContextPool(); + auto ctx = ctxPool->RequestContext(); + + auto func = script->PrepareMethod(funcName, ctx); + REQUIRE(func != nullptr); + + return {.Script = script, .Resolver = lib, .Func = func, .Context = ctx}; +} + +TEST_CASE("Validate Battle Side GetPokemonIndex") { + auto mainLib = TestLibrary::GetLibrary(); + + auto mon = PkmnLib::Battling::CreatePokemon(mainLib, "testSpecies"_cnc, 30).Build(); + auto mon2 = PkmnLib::Battling::CreatePokemon(mainLib, "testSpecies"_cnc, 30).Build(); + + auto userParty = new CreatureLib::Battling::CreatureParty({mon}); + auto targetParty = new CreatureLib::Battling::CreatureParty({mon2}); + auto battle = new PkmnLib::Battling::Battle( + mainLib, { + new CreatureLib::Battling::BattleParty(userParty, {CreatureLib::Battling::CreatureIndex(0, 0)}), + new CreatureLib::Battling::BattleParty(targetParty, {CreatureLib::Battling::CreatureIndex(1, 0)}), + }); + + battle->SwitchCreature(0, 0, mon); + battle->SwitchCreature(1, 0, mon2); + + auto data = GetScript(mainLib, "testGetPokemonIndex"_cnc); + data.Context->SetArgObject(0, battle->GetSides()[0]); + data.Context->SetArgObject(1, mon); + + REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED); + REQUIRE((bool)data.Context->GetReturnWord()); + delete mon; + delete mon2; +} + +#endif \ No newline at end of file