Fixes GetPokemonIndex on BattleSide, adds unit test for it.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-03-28 16:16:47 +02:00
parent a8e71ddd0b
commit 515446bf20
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 88 additions and 3 deletions

View File

@ -2,6 +2,7 @@
#include <CreatureLib/Battling/Models/Battle.hpp> #include <CreatureLib/Battling/Models/Battle.hpp>
#include <CreatureLib/Battling/Models/BattleSide.hpp> #include <CreatureLib/Battling/Models/BattleSide.hpp>
#include "../../../../Battling/Battle/Battle.hpp" #include "../../../../Battling/Battle/Battle.hpp"
#include "../../../../Battling/Pokemon/Pokemon.hpp"
#include "../../AngelScriptScript.hpp" #include "../../AngelScriptScript.hpp"
#include "../HelperFile.hpp" #include "../HelperFile.hpp"
@ -56,6 +57,10 @@ static asIScriptObject* AddVolatileWrapper(PkmnLib::Battling::Battle* obj, const
return scriptObject->GetRawAngelscriptObject(); return scriptObject->GetRawAngelscriptObject();
} }
static u8 GetPokemonIndexWrapper(CreatureLib::Battling::BattleSide* obj, PkmnLib::Battling::Pokemon* pokemon) {
return obj->GetCreatureIndex(pokemon);
}
void RegisterBattleClass::RegisterBattleSide(asIScriptEngine* engine) { void RegisterBattleClass::RegisterBattleSide(asIScriptEngine* engine) {
int r = engine->RegisterObjectMethod( int r = engine->RegisterObjectMethod(
"BattleSide", "bool SwapPositions(uint8 a, uint8 b)", "BattleSide", "bool SwapPositions(uint8 a, uint8 b)",
@ -65,9 +70,7 @@ void RegisterBattleClass::RegisterBattleSide(asIScriptEngine* engine) {
asMETHOD(CreatureLib::Battling::BattleSide, GetSideIndex), asCALL_THISCALL); asMETHOD(CreatureLib::Battling::BattleSide, GetSideIndex), asCALL_THISCALL);
Ensure(r >= 0); Ensure(r >= 0);
r = engine->RegisterObjectMethod("BattleSide", "uint8 GetPokemonIndex(const Pokemon@ pokemon) const", r = engine->RegisterObjectMethod("BattleSide", "uint8 GetPokemonIndex(const Pokemon@ pokemon) const",
asMETHODPR(CreatureLib::Battling::BattleSide, GetCreatureIndex, asFUNCTION(GetPokemonIndexWrapper), asCALL_CDECL_OBJFIRST);
(const ArbUt::BorrowedPtr<CreatureLib::Battling::Creature>& c), u8),
asCALL_THISCALL);
Ensure(r >= 0); Ensure(r >= 0);
} }

View File

@ -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<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"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<AngelScriptResolver*>(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<AngelScriptScript*>(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