Adds helper functions to create Pokemon with more control.
continuous-integration/drone/push Build is failing Details
continuous-integration/drone/tag Build is failing Details

This commit is contained in:
Deukhoofd 2021-10-24 15:57:05 +02:00
parent 35527f68bd
commit 3b2e4f20ac
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 95 additions and 2 deletions

View File

@ -31,6 +31,21 @@ class BattleFunctions {
return p1; return p1;
} }
static CreatureLib::Battling::CreatureParty* CreateParty(CScriptArray* mons) {
auto* ctx = asGetActiveContext();
TestEnvironment* env = static_cast<TestEnvironment*>(ctx->GetUserData(684));
auto p1 = new PkmnLib::Battling::PokemonParty(mons->GetSize());
for (u32 i = 0; i < mons->GetSize(); ++i) {
auto s = *reinterpret_cast<PkmnLib::Battling::Pokemon**>(mons->At(i));
p1->SwapInto(i, s);
// Party becomes owner of mon, so take it from GC.
env->TakeOwnershipOfGarbage(s);
}
env->AddGarbage(p1);
return p1;
}
static PkmnLib::Battling::Battle* CreateSimpleBattle(u32 seed, const ArbUt::StringView& species1, static PkmnLib::Battling::Battle* CreateSimpleBattle(u32 seed, const ArbUt::StringView& species1,
const ArbUt::StringView& species2, u8 level) { const ArbUt::StringView& species2, u8 level) {
auto* ctx = asGetActiveContext(); auto* ctx = asGetActiveContext();
@ -152,6 +167,8 @@ public:
Ensure( Ensure(
engine->RegisterGlobalFunction("Party@ CreateSimpleParty(const array<constString>&in species, uint8 level)", engine->RegisterGlobalFunction("Party@ CreateSimpleParty(const array<constString>&in species, uint8 level)",
asFUNCTION(CreateSimpleParty), asCALL_CDECL) >= 0); asFUNCTION(CreateSimpleParty), asCALL_CDECL) >= 0);
Ensure(engine->RegisterGlobalFunction("Party@ CreateParty(const array<Pokemon@>&in mons)",
asFUNCTION(CreateParty), asCALL_CDECL) >= 0);
Ensure(engine->RegisterGlobalFunction("Battle@ CreateSimpleBattle(uint seed, const constString&in species1, " Ensure(engine->RegisterGlobalFunction("Battle@ CreateSimpleBattle(uint seed, const constString&in species1, "
"const constString&in species2, uint8 level)", "const constString&in species2, uint8 level)",
asFUNCTION(CreateSimpleBattle), asCALL_CDECL) >= 0); asFUNCTION(CreateSimpleBattle), asCALL_CDECL) >= 0);

View File

@ -0,0 +1,66 @@
#ifndef POKEMONSCRIPTTESTER_POKEMONBUILDER_HPP
#define POKEMONSCRIPTTESTER_POKEMONBUILDER_HPP
#include <PkmnLib/Battling/Pokemon/CreatePokemon.hpp>
#include <angelscript.h>
class PokemonBuilderRegistration {
public:
static PkmnLib::Battling::CreatePokemon* CreatePokemonBuilder(const ArbUt::StringView& species, uint8_t level) {
auto* ctx = asGetActiveContext();
TestEnvironment* env = static_cast<TestEnvironment*>(ctx->GetUserData(684));
auto lib = Globals::Library.GetValue();
auto builder = new PkmnLib::Battling::CreatePokemon(lib, species, level);
env->AddGarbage(builder);
return builder;
}
static PkmnLib::Battling::CreatePokemon& WithGenderWrapper(PkmnLib::Battling::CreatePokemon* builder, i32 gender) {
return builder->WithGender((CreatureLib::Library::Gender)gender);
}
static PkmnLib::Battling::Pokemon* BuildWrapper(PkmnLib::Battling::CreatePokemon* builder, u32 seed) {
auto rand = ArbUt::Random(seed);
auto p = builder->Build(rand);
auto* ctx = asGetActiveContext();
TestEnvironment* env = static_cast<TestEnvironment*>(ctx->GetUserData(684));
env->AddGarbage(p);
return p;
}
static void Register(AngelScriptResolver* resolver) {
auto engine = resolver->GetEngine();
Ensure(engine->RegisterObjectType("PokemonBuilder", 0, asOBJ_REF | asOBJ_NOCOUNT) >= 0);
Ensure(engine->RegisterGlobalFunction(
"PokemonBuilder@ CreatePokemonBuilder(const constString&in species, uint8 level)",
asFUNCTION(CreatePokemonBuilder), asCALL_CDECL) >= 0);
Ensure(engine->RegisterObjectMethod("PokemonBuilder", "PokemonBuilder@ WithForme(const constString&in forme)",
asMETHOD(PkmnLib::Battling::CreatePokemon, WithForme), asCALL_THISCALL));
Ensure(engine->RegisterObjectMethod("PokemonBuilder", "PokemonBuilder@ WithGender(Gender gender)",
asFUNCTION(WithGenderWrapper), asCALL_CDECL_OBJFIRST));
Ensure(engine->RegisterObjectMethod("PokemonBuilder", "PokemonBuilder@ IsShiny(bool value)",
asMETHOD(PkmnLib::Battling::CreatePokemon, IsShiny), asCALL_THISCALL));
Ensure(engine->RegisterObjectMethod("PokemonBuilder", "PokemonBuilder@ WithHeldItem(const constString&in item)",
asMETHOD(PkmnLib::Battling::CreatePokemon, WithHeldItem), asCALL_THISCALL));
Ensure(engine->RegisterObjectMethod(
"PokemonBuilder", "PokemonBuilder@ LearnMove(const constString&in move, MoveLearnMethod method)",
asMETHOD(PkmnLib::Battling::CreatePokemon, LearnMove), asCALL_THISCALL));
Ensure(engine->RegisterObjectMethod(
"PokemonBuilder",
"PokemonBuilder@ WithIndividualValues(uint8 hp,uint8 att,uint8 def,uint8 spa,uint8 spd,uint8 speed)",
asMETHOD(PkmnLib::Battling::CreatePokemon, WithIndividualValues), asCALL_THISCALL));
Ensure(engine->RegisterObjectMethod(
"PokemonBuilder",
"PokemonBuilder@ WithEffortValues(uint8 hp,uint8 att,uint8 def,uint8 spa,uint8 spd,uint8 speed)",
asMETHOD(PkmnLib::Battling::CreatePokemon, WithEffortValues), asCALL_THISCALL));
Ensure(engine->RegisterObjectMethod("PokemonBuilder", "PokemonBuilder@ WithNature(const constString&in nature)",
asMETHOD(PkmnLib::Battling::CreatePokemon, WithNature), asCALL_THISCALL));
Ensure(engine->RegisterObjectMethod("PokemonBuilder", "PokemonBuilder@ WithNickname(const string&in name)",
asMETHOD(PkmnLib::Battling::CreatePokemon, WithNickname), asCALL_THISCALL));
Ensure(engine->RegisterObjectMethod("PokemonBuilder", "Pokemon@ Build(uint seed = 0)", asFUNCTION(BuildWrapper),
asCALL_CDECL_OBJFIRST));
}
};
#endif // POKEMONSCRIPTTESTER_POKEMONBUILDER_HPP

View File

@ -6,6 +6,13 @@ struct TestEnvironment {
size_t FailedRequirements = 0; size_t FailedRequirements = 0;
template <typename T> void AddGarbage(T* data) { _garbage.Append(GarbageObject(data)); } template <typename T> void AddGarbage(T* data) { _garbage.Append(GarbageObject(data)); }
template <typename T> void TakeOwnershipOfGarbage(T* data) {
for (auto& g : _garbage) {
if (g.Ptr == data) {
g.Ptr = nullptr;
}
}
}
void CollectGarbage() { void CollectGarbage() {
for (auto& o : _garbage) { for (auto& o : _garbage) {

View File

@ -7,6 +7,7 @@
#include "Globals.hpp" #include "Globals.hpp"
#include "Tester/AngelScript/BattleFunctions.hpp" #include "Tester/AngelScript/BattleFunctions.hpp"
#include "Tester/AngelScript/MiscMockFunctions.hpp" #include "Tester/AngelScript/MiscMockFunctions.hpp"
#include "Tester/AngelScript/PokemonBuilder.hpp"
#include "Tester/AngelScript/TestFunctions.hpp" #include "Tester/AngelScript/TestFunctions.hpp"
#include "Tester/TestRunner.hpp" #include "Tester/TestRunner.hpp"
#include "git.h" #include "git.h"
@ -23,7 +24,8 @@ int main(int argc, char** argv) {
{"workdir"}); {"workdir"});
args::Flag logTimeFlag(parser, "time-log", "Whether to show time logging.", {'t', "time-log"}); args::Flag logTimeFlag(parser, "time-log", "Whether to show time logging.", {'t', "time-log"});
args::Flag forceColorFlag(parser, "force-color", "Whether to force color text output.", {'c', "force-color"}); args::Flag forceColorFlag(parser, "force-color", "Whether to force color text output.", {'c', "force-color"});
args::Flag waitForDebuggerFlag(parser, "wait-for-debugger", "Whether to wait until a debugger is connected or not.", {'w', "wait-for-debugger"}); args::Flag waitForDebuggerFlag(parser, "wait-for-debugger", "Whether to wait until a debugger is connected or not.",
{'w', "wait-for-debugger"});
try { try {
parser.ParseCLI(argc, argv); parser.ParseCLI(argc, argv);
} catch (args::Help&) { } catch (args::Help&) {
@ -56,6 +58,7 @@ int main(int argc, char** argv) {
TestFunctions::Register(scriptResolver); TestFunctions::Register(scriptResolver);
BattleFunctions::Register(scriptResolver); BattleFunctions::Register(scriptResolver);
MiscMockFunctions::Register(scriptResolver); MiscMockFunctions::Register(scriptResolver);
PokemonBuilderRegistration::Register(scriptResolver);
}; };
Globals::Library = BuildLibrary::Build("", initialize); Globals::Library = BuildLibrary::Build("", initialize);