From 2e60096d57123f4496b6d8a9cee8c04964746e4b Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 22 Apr 2020 15:34:24 +0200 Subject: [PATCH] Implements missing Script functions. --- .../AngelScript/AngelScriptScript.hpp | 14 +++-- .../AngelScript/AngelScriptTypeInfo.hpp | 5 ++ .../TypeRegistry/BasicScriptClass.cpp | 7 ++- .../Battling/RegisterTurnChoices.cpp | 60 +++++++++++++------ .../Battling/RegisterTurnChoices.hpp | 1 + 5 files changed, 64 insertions(+), 23 deletions(-) diff --git a/src/ScriptResolving/AngelScript/AngelScriptScript.hpp b/src/ScriptResolving/AngelScript/AngelScriptScript.hpp index 9a6e77f..7c90646 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptScript.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptScript.hpp @@ -93,7 +93,7 @@ public: arr = GetEffectParameters(ctx, parameters); ctx->SetArgAddress(0, arr); }) - if (arr != nullptr){ + if (arr != nullptr) { arr->Release(); } } @@ -102,11 +102,14 @@ public: void OnRemove() override { CALL_HOOK(OnRemove, ); } void OnBeforeTurn(const CreatureLib::Battling::BaseTurnChoice* choice) override { - throw NotImplementedException(); // TODO + CALL_HOOK(OnBeforeTurn, { ctx->SetArgObject(0, (void*)choice); }) } void ChangeAttack(CreatureLib::Battling::AttackTurnChoice* choice, ConstString* outAttack) override { - throw NotImplementedException(); // TODO + CALL_HOOK(ChangeAttack, { + ctx->SetArgObject(0, (void*)choice); + ctx->SetArgAddress(0, outAttack); + }) } void PreventAttack(CreatureLib::Battling::ExecutingAttack* attack, bool* outResult) override { @@ -206,7 +209,10 @@ public: } void PreventSelfSwitch(const CreatureLib::Battling::SwitchTurnChoice* choice, bool* outResult) override { - throw NotImplementedException(); // TODO + CALL_HOOK(PreventSelfSwitch, { + ctx->SetArgObject(0, (void*)choice); + ctx->SetArgAddress(1, outResult); + }) } void ModifyEffectChance(const CreatureLib::Battling::ExecutingAttack* attack, diff --git a/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp b/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp index 1022636..37dcd38 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp @@ -82,6 +82,9 @@ public: SCRIPT_HOOK_FUNCTION(OnInitialize, "void OnInitialize(const array &in parameters)"); SCRIPT_HOOK_FUNCTION(Stack, "void Stack()"); SCRIPT_HOOK_FUNCTION(OnRemove, "void OnRemove()"); +SCRIPT_HOOK_FUNCTION(OnBeforeTurn, "void OnBeforeTurn(BaseTurnChoice@ choice)"); +SCRIPT_HOOK_FUNCTION(ChangeAttack, "void ChangeAttack(MoveTurnChoice@ choice, constString& changedMove)"); + SCRIPT_HOOK_FUNCTION(PreventAttack, "void PreventAttack(ExecutingMove@ attack, bool& result)"); SCRIPT_HOOK_FUNCTION(FailAttack, "void FailAttack(ExecutingMove@ attack, bool& result)"); SCRIPT_HOOK_FUNCTION(StopBeforeAttack, "void StopBeforeAttack(ExecutingMove@ attack, bool& result)"); @@ -101,6 +104,8 @@ public: SCRIPT_HOOK_FUNCTION(OnSecondaryEffect, "void OnSecondaryEffect(ExecutingMove@ attack, Pokemon@ target, uint8 hit)"); SCRIPT_HOOK_FUNCTION(OnAfterHits, "void OnAfterHits(ExecutingMove@ attack, Pokemon@ target)"); + SCRIPT_HOOK_FUNCTION(PreventSelfSwitch, "void PreventSelfSwitch(SwitchTurnChoice@ choice, bool& prevented)"); + SCRIPT_HOOK_FUNCTION(ModifyEffectChance, "void ModifyEffectChance(ExecutingMove@ attack, Pokemon@ target, float& chance)"); SCRIPT_HOOK_FUNCTION(ModifyIncomingEffectChance, diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp index 4c147a8..050e877 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp @@ -2,14 +2,16 @@ #include void BasicScriptClass::Register(asIScriptEngine* engine) { - // As far as I am aware at the moment you can't create an abstract class with virtual members through the application - // registry interface. As such, we just create it from string. + // As far as I am aware at the moment you can't create an abstract class with virtual members through the + // application registry interface. As such, we just create it from string. [[maybe_unused]] int r = engine->GetModuleByIndex(0)->AddScriptSection("PkmnScript", R"( shared abstract class PkmnScript { // CreatureLib methods void OnInitialize(const array &in parameters){}; void Stack(){}; void OnRemove(){}; + void OnBeforeTurn(BaseTurnChoice@ choice){}; + void ChangeAttack(MoveTurnChoice@ choice, constString& changedMove){}; void PreventAttack(ExecutingMove@ attack, bool& result){}; void FailAttack(ExecutingMove@ attack, bool& result){}; void StopBeforeAttack(ExecutingMove@ attack, bool& result){}; @@ -22,6 +24,7 @@ shared abstract class PkmnScript { void PreventSecondaryEffects(ExecutingMove@ attack, Pokemon@ target, uint8 hit, bool& outResult){}; void OnSecondaryEffect(ExecutingMove@ attack, Pokemon@ target, uint8 hit){}; void OnAfterHits(ExecutingMove@ attack, Pokemon@ target){}; + void PreventSelfSwitch(SwitchTurnChoice@ choice, bool& prevented){}; void ModifyEffectChance(ExecutingMove@ attack, Pokemon@ target, float& chance){}; void ModifyIncomingEffectChance(ExecutingMove@ attack, Pokemon@ target, float& chance){}; diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.cpp index 123e1e4..f10e92e 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.cpp @@ -1,63 +1,89 @@ #include "RegisterTurnChoices.hpp" #include #include -#include +#include #include "../RefCast.hpp" void RegisterTurnChoices::Register(asIScriptEngine* engine) { RegisterTurnChoiceKindEnum(engine); RegisterBaseTurnChoice(engine); RegisterMoveTurnChoice(engine); + RegisterSwitchTurnChoice(engine); } void RegisterTurnChoices::RegisterTurnChoiceKindEnum(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterEnum("TurnChoiceKind"); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterEnumValue("TurnChoiceKind", "Pass", (int)CreatureLib::Battling::TurnChoiceKind::Pass); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterEnumValue("TurnChoiceKind", "Attack", (int)CreatureLib::Battling::TurnChoiceKind::Attack); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterEnumValue("TurnChoiceKind", "Item", (int)CreatureLib::Battling::TurnChoiceKind::Item); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterEnumValue("TurnChoiceKind", "Switch", (int)CreatureLib::Battling::TurnChoiceKind::Switch); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterEnumValue("TurnChoiceKind", "Flee", (int)CreatureLib::Battling::TurnChoiceKind::Flee); - assert(r >= 0); + Assert(r >= 0); } void RegisterTurnChoices::RegisterBaseTurnChoice(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("BaseTurnChoice", 0, asOBJ_REF | asOBJ_NOCOUNT); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterObjectMethod("BaseTurnChoice", "TurnChoiceKind get_Kind() const property", asMETHOD(CreatureLib::Battling::BaseTurnChoice, GetKind), asCALL_THISCALL); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterObjectMethod("BaseTurnChoice", "Pokemon@ get_User() const property", asMETHOD(CreatureLib::Battling::BaseTurnChoice, GetUser), asCALL_THISCALL); - assert(r >= 0); + Assert(r >= 0); } void RegisterTurnChoices::RegisterMoveTurnChoice(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("MoveTurnChoice", 0, asOBJ_REF | asOBJ_NOCOUNT); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterObjectMethod("MoveTurnChoice", "TurnChoiceKind get_Kind() const property", asMETHOD(CreatureLib::Battling::AttackTurnChoice, GetKind), asCALL_THISCALL); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterObjectMethod("MoveTurnChoice", "Pokemon@ get_User() const property", asMETHOD(CreatureLib::Battling::AttackTurnChoice, GetUser), asCALL_THISCALL); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterObjectMethod("MoveTurnChoice", "LearnedMove@ get_Move() const property", asMETHOD(CreatureLib::Battling::AttackTurnChoice, GetAttack), asCALL_THISCALL); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterObjectMethod("MoveTurnChoice", "int8 get_Priority() const property", asMETHOD(CreatureLib::Battling::AttackTurnChoice, GetPriority), asCALL_THISCALL); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterObjectMethod( "BaseTurnChoice", "MoveTurnChoice@ opCast()", asFUNCTION((refCast)), asCALL_CDECL_OBJLAST); - assert(r >= 0); + Assert(r >= 0); r = engine->RegisterObjectMethod( "MoveTurnChoice", "BaseTurnChoice@ opImplCast()", asFUNCTION((refCast)), asCALL_CDECL_OBJLAST); - assert(r >= 0); + Assert(r >= 0); +} + +void RegisterTurnChoices::RegisterSwitchTurnChoice(asIScriptEngine* engine) { + [[maybe_unused]] int r = engine->RegisterObjectType("SwitchTurnChoice", 0, asOBJ_REF | asOBJ_NOCOUNT); + Assert(r >= 0); + r = engine->RegisterObjectMethod("SwitchTurnChoice", "TurnChoiceKind get_Kind() const property", + asMETHOD(CreatureLib::Battling::SwitchTurnChoice, GetKind), asCALL_THISCALL); + Assert(r >= 0); + r = engine->RegisterObjectMethod("SwitchTurnChoice", "Pokemon@ get_User() const property", + asMETHOD(CreatureLib::Battling::SwitchTurnChoice, GetUser), asCALL_THISCALL); + Assert(r >= 0); + r = engine->RegisterObjectMethod("SwitchTurnChoice", "Pokemon@ get_NewPokemon() const property", + asMETHOD(CreatureLib::Battling::SwitchTurnChoice, GetNewCreature), asCALL_THISCALL); + Assert(r >= 0); + + r = engine->RegisterObjectMethod( + "SwitchTurnChoice", "MoveTurnChoice@ opCast()", + asFUNCTION((refCast)), + asCALL_CDECL_OBJLAST); + Assert(r >= 0); + r = engine->RegisterObjectMethod( + "SwitchTurnChoice", "BaseTurnChoice@ opImplCast()", + asFUNCTION((refCast)), + asCALL_CDECL_OBJLAST); + Assert(r >= 0); } diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.hpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.hpp index 71cec61..f4e7ebb 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.hpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.hpp @@ -6,6 +6,7 @@ class RegisterTurnChoices { static void RegisterTurnChoiceKindEnum(asIScriptEngine* engine); static void RegisterBaseTurnChoice(asIScriptEngine* engine); static void RegisterMoveTurnChoice(asIScriptEngine* engine); + static void RegisterSwitchTurnChoice(asIScriptEngine* engine); public: static void Register(asIScriptEngine* engine); };