Adds initialization from parameters to ItemUseScript.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-03-07 20:03:07 +01:00
parent 090b77ff2b
commit 1540009912
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 36 additions and 4 deletions

View File

@ -1,6 +1,31 @@
#include "AngelScriptItemUseScript.hpp"
#include "AngelScriptResolver.hpp"
CScriptArray* AngelScriptItemUseScript::GetEffectParameters(const ArbUt::List<CreatureLib::Library::EffectParameter*>& ls) {
asITypeInfo* t = _resolver->GetBaseType("array<EffectParameter@>"_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<CreatureLib::Library::EffectParameter*>& 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();

View File

@ -2,13 +2,14 @@
#define PKMNLIB_ANGELSCRIPTITEMUSESCRIPT_HPP
#include <CreatureLib/Battling/ScriptHandling/ItemUseScript.hpp>
#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() {
@ -17,6 +18,7 @@ public:
}
}
void OnInitialize(const ArbUt::List<CreatureLib::Library::EffectParameter*>& 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<CreatureLib::Library::EffectParameter*>& ls);
#define ITEM_USE_SCRIPT_HOOK_FUNCTION(name, decl) FunctionInfo __##name = Initialize(decl);
ITEM_USE_SCRIPT_HOOK_FUNCTION(OnInitialize, "void OnInitialize(const array<EffectParameter@> &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)");

View File

@ -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;

View File

@ -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<EffectParameter@> &in parameters){};
bool IsItemUsable() { return false; };
bool IsPokemonUseItem() { return false; };
bool IsUseValidForPokemon(Pokemon@ target) { return false; };