Actual implementation of Angelscript hooks into battle library.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <Battling/ScriptHandling/Script.hpp>
|
||||
#define ANGELSCRIPT_DLL_LIBRARY_IMPORT
|
||||
#include <angelscript.h>
|
||||
#include <Core/Exceptions/NotImplementedException.hpp>
|
||||
#include "AngelScriptTypeInfo.hpp"
|
||||
#include "ContextPool.hpp"
|
||||
|
||||
@@ -19,25 +20,6 @@ public:
|
||||
|
||||
~AngelScriptScript() override { _obj->Release(); }
|
||||
|
||||
void InvokeMethod(const char* name) {
|
||||
auto func = _type->GetFunction(name);
|
||||
if (func == nullptr)
|
||||
return;
|
||||
auto ctx = _ctxPool->RequestContext();
|
||||
ctx->Prepare(func);
|
||||
ctx->SetObject(_obj);
|
||||
auto result = ctx->Execute();
|
||||
if (result != asEXECUTION_FINISHED) {
|
||||
if (result == asEXECUTION_EXCEPTION) {
|
||||
throw CreatureException(ctx->GetExceptionString());
|
||||
}
|
||||
throw CreatureException("Function failed.");
|
||||
}
|
||||
_ctxPool->ReturnContextToPool(ctx);
|
||||
}
|
||||
|
||||
asIScriptObject* GetScriptObject() { return _obj; }
|
||||
|
||||
asIScriptFunction* PrepareMethod(const char* name, asIScriptContext* ctx) {
|
||||
auto func = _type->GetFunction(name);
|
||||
ctx->Prepare(func);
|
||||
@@ -46,6 +28,89 @@ public:
|
||||
}
|
||||
|
||||
ContextPool* GetContextPool() { return _ctxPool; }
|
||||
|
||||
#define CALLHOOK(name, setup) \
|
||||
auto s = _type->Get##name(); \
|
||||
if (!s.Exists) \
|
||||
return; \
|
||||
auto ctx = _ctxPool->RequestContext(); \
|
||||
ctx->Prepare(s.Function); \
|
||||
ctx->SetObject(_obj); \
|
||||
setup; \
|
||||
auto scriptResult = ctx->Execute(); \
|
||||
if (scriptResult != 0) { \
|
||||
throw CreatureException("Script didn't finish properly; message " + std::to_string(scriptResult)); \
|
||||
} \
|
||||
_ctxPool->ReturnContextToPool(ctx);
|
||||
|
||||
void Stack() override { CALLHOOK(Stack, {}); }
|
||||
|
||||
void OnBeforeTurn(const CreatureLib::Battling::BaseTurnChoice* choice) override {
|
||||
throw NotImplementedException(); //TODO
|
||||
}
|
||||
|
||||
void ChangeAttack(CreatureLib::Battling::AttackTurnChoice* choice, std::string* outAttack) override {
|
||||
throw NotImplementedException(); //TODO
|
||||
}
|
||||
|
||||
void PreventAttack(CreatureLib::Battling::ExecutingAttack* attack, bool* outResult) override {
|
||||
CALLHOOK(PreventAttack, {
|
||||
ctx->SetArgObject(0, (void*)attack);
|
||||
ctx->SetArgAddress(1, outResult);
|
||||
})
|
||||
}
|
||||
|
||||
void FailAttack(CreatureLib::Battling::ExecutingAttack* attack, bool* outFailed) override {
|
||||
CALLHOOK(FailAttack, {
|
||||
ctx->SetArgObject(0, (void*)attack);
|
||||
ctx->SetArgAddress(1, outFailed);
|
||||
})
|
||||
}
|
||||
|
||||
void StopBeforeAttack(CreatureLib::Battling::ExecutingAttack* attack, bool* outResult) override {
|
||||
CALLHOOK(StopBeforeAttack, {
|
||||
ctx->SetArgObject(0, (void*)attack);
|
||||
ctx->SetArgAddress(1, outResult);
|
||||
})
|
||||
}
|
||||
|
||||
void OnBeforeAttack(CreatureLib::Battling::ExecutingAttack* attack) override { Script::OnBeforeAttack(attack); }
|
||||
void FailIncomingAttack(CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target,
|
||||
bool* outResult) override {
|
||||
Script::FailIncomingAttack(attack, target, outResult);
|
||||
}
|
||||
void IsInvulnerable(CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target,
|
||||
bool* outResult) override {
|
||||
Script::IsInvulnerable(attack, target, outResult);
|
||||
}
|
||||
void OnAttackMiss(CreatureLib::Battling::ExecutingAttack* attack,
|
||||
CreatureLib::Battling::Creature* target) override {
|
||||
Script::OnAttackMiss(attack, target);
|
||||
}
|
||||
void ChangeAttackType(CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target,
|
||||
uint8_t hitNumber, uint8_t* outType) override {
|
||||
Script::ChangeAttackType(attack, target, hitNumber, outType);
|
||||
}
|
||||
void OnStatusMove(const CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target,
|
||||
uint8_t hitNumber) override {
|
||||
Script::OnStatusMove(attack, target, hitNumber);
|
||||
}
|
||||
void PreventSecondaryEffects(const CreatureLib::Battling::ExecutingAttack* attack,
|
||||
CreatureLib::Battling::Creature* target, uint8_t hitNumber, bool* outResult) override {
|
||||
Script::PreventSecondaryEffects(attack, target, hitNumber, outResult);
|
||||
}
|
||||
void OnSecondaryEffect(const CreatureLib::Battling::ExecutingAttack* attack,
|
||||
CreatureLib::Battling::Creature* target, uint8_t hitNumber) override {
|
||||
Script::OnSecondaryEffect(attack, target, hitNumber);
|
||||
}
|
||||
void OnAfterHits(const CreatureLib::Battling::ExecutingAttack* attack,
|
||||
CreatureLib::Battling::Creature* target) override {
|
||||
Script::OnAfterHits(attack, target);
|
||||
}
|
||||
void PreventSelfSwitch(const CreatureLib::Battling::SwitchTurnChoice* choice, bool* outResult) override {
|
||||
Script::PreventSelfSwitch(choice, outResult);
|
||||
}
|
||||
};
|
||||
|
||||
#undef CALLHOOK
|
||||
#endif // PKMNLIB_ANGELSCRIPTSCRIPT_HPP
|
||||
|
||||
Reference in New Issue
Block a user