Support for specific PkmnLib functions in Scripts, added ModifyCriticalStage function.

This commit is contained in:
Deukhoofd 2020-02-20 13:28:31 +01:00
parent 83a49eee6c
commit 19cfcc9e32
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
7 changed files with 106 additions and 18 deletions

View File

@ -0,0 +1,20 @@
#include "MiscLibrary.hpp"
#include <CreatureLib/Battling/Models/Battle.hpp>
#include "../PkmnScriptHook.hpp"
bool PkmnLib::Battling::MiscLibrary::IsCritical(CreatureLib::Battling::ExecutingAttack* attack,
CreatureLib::Battling::Creature* target, uint8_t hit) const {
uint8_t critStage = 0;
PKMN_HOOK(ModifyCriticalStage, attack, attack, target, hit, &critStage);
auto rand = target->GetBattle()->GetRandom();
switch (critStage){
case 0: return rand->Get(24) == 0;
case 1: return rand->Get(8) == 0;
case 2: return rand->Get(2) == 0;
default: return true;
}
}
bool PkmnLib::Battling::MiscLibrary::CanFlee(CreatureLib::Battling::FleeTurnChoice* switchChoice) const {
return CreatureLib::Battling::MiscLibrary::CanFlee(switchChoice);
}

View File

@ -0,0 +1,15 @@
#ifndef PKMNLIB_MISCLIBRARY_HPP
#define PKMNLIB_MISCLIBRARY_HPP
#include <CreatureLib/Battling/Library/MiscLibrary.hpp>
namespace PkmnLib::Battling{
class MiscLibrary : CreatureLib::Battling::MiscLibrary {
~MiscLibrary() override = default;
bool IsCritical(CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target,
uint8_t hit) const override;
bool CanFlee(CreatureLib::Battling::FleeTurnChoice* switchChoice) const override;
};
}
#endif // PKMNLIB_MISCLIBRARY_HPP

View File

@ -0,0 +1,16 @@
#ifndef PKMNLIB_PKMNSCRIPT_HPP
#define PKMNLIB_PKMNSCRIPT_HPP
#include <CreatureLib/Battling/ScriptHandling/Script.hpp>
#include "Pokemon/Pokemon.hpp"
namespace PkmnLib::Battling{
class PkmnScript : public CreatureLib::Battling::Script{
public:
PkmnScript(const std::string& name) : Script(name) {}
virtual void ModifyCriticalStage(CreatureLib::Battling::ExecutingAttack* attack,
CreatureLib::Battling::Creature* target, uint8_t hit, uint8_t* critStage){};
};
}
#endif // PKMNLIB_PKMNSCRIPT_HPP

View File

@ -0,0 +1,13 @@
#include "PkmnScript.hpp"
#define PKMN_HOOK(hookName, source, ...) \
{ \
auto aggregator = source->GetScriptIterator(); \
while (aggregator.HasNext()) { \
auto next = aggregator.GetNext(); \
if (next == nullptr) \
continue; \
auto castNext = dynamic_cast<PkmnLib::Battling::PkmnScript*>(next); \
castNext->hookName(__VA_ARGS__); \
} \
}

View File

@ -4,10 +4,11 @@
#define ANGELSCRIPT_DLL_LIBRARY_IMPORT #define ANGELSCRIPT_DLL_LIBRARY_IMPORT
#include <CreatureLib/Core/Exceptions/NotImplementedException.hpp> #include <CreatureLib/Core/Exceptions/NotImplementedException.hpp>
#include <angelscript.h> #include <angelscript.h>
#include "../../Battling/PkmnScript.hpp"
#include "AngelScriptTypeInfo.hpp" #include "AngelScriptTypeInfo.hpp"
#include "ContextPool.hpp" #include "ContextPool.hpp"
class AngelScriptScript : public CreatureLib::Battling::Script { class AngelScriptScript : public PkmnLib::Battling::PkmnScript {
private: private:
AngelScriptTypeInfo* _type = nullptr; AngelScriptTypeInfo* _type = nullptr;
ContextPool* _ctxPool = nullptr; ContextPool* _ctxPool = nullptr;
@ -16,7 +17,7 @@ private:
public: public:
AngelScriptScript(const std::string& name, AngelScriptTypeInfo* type, asIScriptObject* obj, ContextPool* ctxPool) AngelScriptScript(const std::string& name, AngelScriptTypeInfo* type, asIScriptObject* obj, ContextPool* ctxPool)
: CreatureLib::Battling::Script(name), _type(type), _ctxPool(ctxPool), _obj(obj) {} : PkmnLib::Battling::PkmnScript(name), _type(type), _ctxPool(ctxPool), _obj(obj) {}
~AngelScriptScript() override { _obj->Release(); } ~AngelScriptScript() override { _obj->Release(); }
@ -251,6 +252,20 @@ public:
ctx->SetArgAddress(3, damage); ctx->SetArgAddress(3, damage);
}) })
} }
////////////////////
// PkmnLib methods//
////////////////////
void ModifyCriticalStage(CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target,
uint8_t hit, uint8_t* critStage) override {
CALL_HOOK(ModifyCriticalStage, {
ctx->SetArgObject(0, (void*)attack);
ctx->SetArgObject(1, (void*)target);
ctx->SetArgByte(2, hit);
ctx->SetArgAddress(3, critStage);
})
}
}; };
#undef CALL_HOOK #undef CALL_HOOK

View File

@ -95,7 +95,8 @@ SCRIPT_HOOK_FUNCTION(ModifyIncomingEffectChance,
SCRIPT_HOOK_FUNCTION(OverrideBasePower, SCRIPT_HOOK_FUNCTION(OverrideBasePower,
"void OverrideBasePower(ExecutingMove@ attack, Pokemon@ target, uint8 hit, uint8& chance)"); "void OverrideBasePower(ExecutingMove@ attack, Pokemon@ target, uint8 hit, uint8& chance)");
SCRIPT_HOOK_FUNCTION(ChangeDamageStatsUser, SCRIPT_HOOK_FUNCTION(
ChangeDamageStatsUser,
"void ChangeDamageStatsUser(ExecutingMove@ attack, Pokemon@ target, uint8 hit, Pokemon@& user)"); "void ChangeDamageStatsUser(ExecutingMove@ attack, Pokemon@ target, uint8 hit, Pokemon@& user)");
SCRIPT_HOOK_FUNCTION(BypassDefensiveStat, SCRIPT_HOOK_FUNCTION(BypassDefensiveStat,
"void BypassDefensiveStat(ExecutingMove@ attack, Pokemon@ target, uint8 hit, bool& bypass)"); "void BypassDefensiveStat(ExecutingMove@ attack, Pokemon@ target, uint8 hit, bool& bypass)");
@ -103,11 +104,15 @@ SCRIPT_HOOK_FUNCTION(BypassOffensiveStat,
"void BypassOffensiveStat(ExecutingMove@ attack, Pokemon@ target, uint8 hit, bool& bypass)"); "void BypassOffensiveStat(ExecutingMove@ attack, Pokemon@ target, uint8 hit, bool& bypass)");
SCRIPT_HOOK_FUNCTION(ModifyStatModifier, SCRIPT_HOOK_FUNCTION(ModifyStatModifier,
"void ModifyStatModifier(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& modifier)"); "void ModifyStatModifier(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& modifier)");
SCRIPT_HOOK_FUNCTION(ModifyDamageModifier, SCRIPT_HOOK_FUNCTION(
ModifyDamageModifier,
"void ModifyDamageModifier(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& modifier)"); "void ModifyDamageModifier(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& modifier)");
SCRIPT_HOOK_FUNCTION(OverrideDamage, SCRIPT_HOOK_FUNCTION(OverrideDamage,
"void OverrideDamage(ExecutingMove@ attack, Pokemon@ target, uint8 hit, int& damage)"); "void OverrideDamage(ExecutingMove@ attack, Pokemon@ target, uint8 hit, int& damage)");
SCRIPT_HOOK_FUNCTION(
ModifyCriticalStage,
"void ModifyCriticalStage(ExecutingMove@ attack, Pokemon@ target, uint8 hit, uint8& critStage)");
}; };
#undef SCRIPT_HOOK_FUNCTION #undef SCRIPT_HOOK_FUNCTION

View File

@ -6,6 +6,7 @@ void BasicScriptClass::Register(asIScriptEngine* engine) {
// registry interface. As such, we just create it from string. // registry interface. As such, we just create it from string.
[[maybe_unused]] int r = engine->GetModuleByIndex(0)->AddScriptSection("PkmnScript", R"( [[maybe_unused]] int r = engine->GetModuleByIndex(0)->AddScriptSection("PkmnScript", R"(
shared abstract class PkmnScript { shared abstract class PkmnScript {
// CreatureLib methods
void Stack(){}; void Stack(){};
void OnRemove(){}; void OnRemove(){};
void PreventAttack(ExecutingMove@ attack, bool& result){}; void PreventAttack(ExecutingMove@ attack, bool& result){};
@ -31,6 +32,9 @@ shared abstract class PkmnScript {
void ModifyStatModifier(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& modifier){}; void ModifyStatModifier(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& modifier){};
void ModifyDamageModifier(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& modifier){}; void ModifyDamageModifier(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& modifier){};
void OverrideDamage(ExecutingMove@ attack, Pokemon@ target, uint8 hit, int& damage){}; void OverrideDamage(ExecutingMove@ attack, Pokemon@ target, uint8 hit, int& damage){};
// PkmnLib methods
void ModifyCriticalStage(ExecutingMove@ attack, Pokemon@ target, uint8 hit, uint8& critStage){};
} }
)"); )");
assert(r >= 0); assert(r >= 0);