diff --git a/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.cpp b/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.cpp index 9a2bac4..ef19205 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.cpp +++ b/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.cpp @@ -1,6 +1,31 @@ #include "AngelScriptItemUseScript.hpp" #include "AngelScriptResolver.hpp" +CScriptArray* AngelScriptItemUseScript::GetEffectParameters(const ArbUt::List& ls) { + asITypeInfo* t = _resolver->GetBaseType("array"_cnc); + CScriptArray* arr = CScriptArray::Create(t, ls.Count()); + for (size_t i = 0; i < ls.Count(); i++) { + arr->SetValue(i, (void**)&ls[i]); + } + return arr; +} + +void AngelScriptItemUseScript::OnInitialize( + const ArbUt::List& parameters) { + if (__OnInitialize.Exists) { + CScriptArray* arr = nullptr; + AngelScriptUtils::AngelscriptFunctionCall( + __OnInitialize.Function, _resolver->GetContextPool(), _scriptObject, ""_cnc, + [&]([[maybe_unused]] asIScriptContext* ctx) { + arr = GetEffectParameters(parameters); + ctx->SetArgAddress(0, arr); + }, + [&]([[maybe_unused]] asIScriptContext* ctx) {}); + + arr->Release(); + } +} + bool AngelScriptItemUseScript::IsItemUsable() const { if (!__IsItemUsable.Exists) { return CreatureLib::Battling::ItemUseScript::IsItemUsable(); diff --git a/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.hpp b/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.hpp index 72c70c9..c385389 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.hpp @@ -2,21 +2,23 @@ #define PKMNLIB_ANGELSCRIPTITEMUSESCRIPT_HPP #include +#include "../../../extern/angelscript_addons/scriptarray/scriptarray.h" #include "AngelScriptFunctionCall.hpp" class AngelScriptResolver; class AngelScriptItemUseScript final : public CreatureLib::Battling::ItemUseScript { public: - AngelScriptItemUseScript(asIScriptObject* scriptObject, const AngelScriptResolver* resolver) + AngelScriptItemUseScript(asIScriptObject* scriptObject, AngelScriptResolver* resolver) : _scriptObject(scriptObject), _resolver(resolver) {} - ~AngelScriptItemUseScript(){ - if (_scriptObject != nullptr){ + ~AngelScriptItemUseScript() { + if (_scriptObject != nullptr) { _scriptObject->Release(); } } + void OnInitialize(const ArbUt::List& parameters) override; [[nodiscard]] bool IsItemUsable() const override; [[nodiscard]] bool IsCreatureUseItem() const override; bool IsUseValidForCreature(CreatureLib::Battling::Creature* creature) const override; @@ -28,7 +30,7 @@ public: private: asIScriptObject* _scriptObject; - const AngelScriptResolver* _resolver; + AngelScriptResolver* _resolver; struct FunctionInfo { bool Exists = false; @@ -43,8 +45,11 @@ private: return FunctionInfo{.Exists = true, .Function = val}; } + CScriptArray* GetEffectParameters(const ArbUt::List& ls); + #define ITEM_USE_SCRIPT_HOOK_FUNCTION(name, decl) FunctionInfo __##name = Initialize(decl); + ITEM_USE_SCRIPT_HOOK_FUNCTION(OnInitialize, "void OnInitialize(const array &in parameters)"); ITEM_USE_SCRIPT_HOOK_FUNCTION(IsItemUsable, "bool IsItemUsable()"); ITEM_USE_SCRIPT_HOOK_FUNCTION(IsPokemonUseItem, "bool IsPokemonUseItem()"); ITEM_USE_SCRIPT_HOOK_FUNCTION(IsUseValidForPokemon, "bool IsUseValidForPokemon(Pokemon@ target)"); diff --git a/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp b/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp index 88eab46..5a44c43 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp +++ b/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp @@ -175,6 +175,7 @@ CreatureLib::Battling::ItemUseScript* AngelScriptResolver::LoadItemScript(const asIScriptObject* obj = *(asIScriptObject**)ctx->GetAddressOfReturnValue(); obj->AddRef(); auto scriptObject = new AngelScriptItemUseScript(obj, this); + scriptObject->OnInitialize(item->GetEffect()->GetParameters()); _itemUseScripts.Insert(item, scriptObject); _contextPool->ReturnContextToPool(ctx); return scriptObject; diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp index 6326a8d..23f4469 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp @@ -48,6 +48,7 @@ shared abstract class PkmnScript { Ensure(r >= 0); r = engine->GetModuleByIndex(0)->AddScriptSection("ItemUseScript", R"( shared abstract class ItemUseScript { + void OnInitialize(const array &in parameters){}; bool IsItemUsable() { return false; }; bool IsPokemonUseItem() { return false; }; bool IsUseValidForPokemon(Pokemon@ target) { return false; };