Adds support for modifying volatile scripts after adding them.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2021-03-27 22:19:18 +01:00
parent 5cfa174396
commit e361507ec9
7 changed files with 91 additions and 14 deletions

View File

@@ -5,7 +5,7 @@
#include "../TestLibrary/TestLibrary.hpp"
#define AS_CLASS(name, contents) \
{ #name, "namespace Pokemon{ [Pokemon effect=" #name "] class " #name " : PkmnScript { " contents "}}" }
{ #name, "namespace Pokemon{ [Pokemon effect=" #name "] shared class " #name " : PkmnScript { " contents "}}" }
static std::unordered_map<const char*, const char*> _scripts = std::unordered_map<const char*, const char*>{
AS_CLASS(blankScript, ),
@@ -53,8 +53,11 @@ void StopBeforeAttack(ExecutingMove@ attack, bool& result) override{
R"(void IsInvulnerable(ExecutingMove@ attack, Pokemon@ target, bool& result) override{ result = !result; })"),
AS_CLASS(OnAttackMissScript,
"int value = 0; void OnAttackMiss(ExecutingMove@ attack, Pokemon@ target) override { value++; } "
"int GetValue() { return value; }"),
R"(
int value = 0;
void OnAttackMiss(ExecutingMove@ attack, Pokemon@ target) override { value++; }
int GetValue() { return value; }
)"),
AS_CLASS(
ChangeAttackTypeScript,
R"(void ChangeAttackType(ExecutingMove@ attack, Pokemon@ target, uint8 hit, uint8& outType) override{outType = 1; };)"),
@@ -64,15 +67,31 @@ void StopBeforeAttack(ExecutingMove@ attack, bool& result) override{
AS_CLASS(
PreventSecondaryEffectsScript,
R"(void PreventSecondaryEffects(ExecutingMove@ attack, Pokemon@ target, uint8 hit, bool& result) override{ result = !result; })"),
AS_CLASS(OnSecondaryEffectScript, "int value = 0; void OnSecondaryEffect(ExecutingMove@ attack, Pokemon@ target, "
"uint8 hit) override { value++; } "
"int GetValue() { return value; }"),
AS_CLASS(OnSecondaryEffectScript, R"(
int value = 0;
void OnSecondaryEffect(ExecutingMove@ attack, Pokemon@ target, uint8 hit) override {
value++;
}
int GetValue() { return value; })"),
AS_CLASS(OnAfterHitsScript,
"int value = 0; void OnAfterHits(ExecutingMove@ attack, Pokemon@ target) override { value++; } "
"int GetValue() { return value; }"),
AS_CLASS(throwScript,
R"(void PreventAttack(ExecutingMove@ attack, bool& result) override{ throw("test exception"); })"),
AS_CLASS(AddVolatileModMain,
R"(
void OnSecondaryEffect(ExecutingMove@ attack, Pokemon@ target, uint8 hit) override {
auto script = cast<AddVolatileModSecondary>(target.AddVolatile("AddVolatileModSecondary"));
script.value++;
};
)"),
AS_CLASS(AddVolatileModSecondary,
R"(
int value = 0;
int GetValue() { return value; }
)"),
};
static AngelScriptResolver* _resolverCache = nullptr;
@@ -354,4 +373,37 @@ TEST_CASE("Handle script exceptions.") {
throw ArbUt::Exception("Didn't throw");
}
TEST_CASE("Add Volatile with return script function") {
auto statCalc = new PkmnLib::Battling::StatCalculator();
auto resolver = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
auto mainLib = new PkmnLib::Battling::BattleLibrary(
TestLibrary::BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(),
new PkmnLib::Battling::ExperienceLibrary(), resolver, new PkmnLib::Battling::MiscLibrary());
resolver->Initialize(mainLib);
for (auto kv : _scripts) {
resolver->CreateScript(kv.first, kv.second);
}
resolver->FinalizeModule();
auto script = GetScript(mainLib, "AddVolatileModMain"_cnc);
auto mon = PkmnLib::Battling::CreatePokemon(mainLib, "testSpecies"_cnc, 30).Build();
script->OnSecondaryEffect(nullptr, mon, 0);
auto scriptObjOption = const_cast<CreatureLib::Battling::ScriptAggregator&>(mon->GetScriptIterator()).GetNextNotNull();
REQUIRE(scriptObjOption.has_value());
REQUIRE(scriptObjOption.value()->GetName() == "AddVolatileModSecondary"_cnc);
auto scriptObj = scriptObjOption.value();
auto script2 = scriptObj.As<AngelScriptScript>();
auto ctxPool = script2->GetContextPool();
auto ctx = ctxPool->RequestContext();
script2->PrepareMethod("GetValue"_cnc, ctx);
REQUIRE(ctx->Execute() == asEXECUTION_FINISHED);
REQUIRE(ctx->GetReturnDWord() == 1);
ctxPool->ReturnContextToPool(ctx);
delete script;
}
#endif