More handling of script hooks.

This commit is contained in:
Deukhoofd 2019-11-09 12:55:48 +01:00
parent ee14efe22e
commit 658672a246
4 changed files with 44 additions and 34 deletions

View File

@ -78,23 +78,33 @@ void TurnHandler::ExecuteAttackChoice(const AttackTurnChoice *choice) {
} }
void TurnHandler::HandleAttackForTarget(ExecutingAttack &attack, Creature *target, ExecutingAttack::TargetData &targetData) { void TurnHandler::HandleAttackForTarget(ExecutingAttack &attack, Creature *target, ExecutingAttack::TargetData &targetData) {
//HOOK: Check if attack fails on target auto user = attack.GetUser();
std::array<ScriptSource*, 1> sources = {target};
HOOK(OnIncomingAttackFails, sources, &attack, target);
//HOOK: Check if target is invulnerable std::array<ScriptSource*, 1> targetSources = {target};
HOOK(IsInvulnerable, sources, &attack, target); std::array<ScriptSource*, 1> userSources = {user};
bool fail = false;
HOOK(FailIncomingAttack, targetSources, &attack, target, fail);
if (fail){
//TODO: Fail handling.
return;
}
bool invulnerable = fail;
HOOK(IsInvulnerable, targetSources, &attack, target, invulnerable);
if (invulnerable){
//TODO: We should probably do something when a target is invulnerable.
return;
}
if (!targetData.IsHit()){ if (!targetData.IsHit()){
//HOOK: On attack miss. HOOK(OnAttackMiss, targetSources, &attack, target);
HOOK(OnAttackMiss, sources, &attack, target);
return; return;
} }
auto numHits = targetData.GetNumberOfHits(); auto numHits = targetData.GetNumberOfHits();
if (numHits == 0) if (numHits == 0)
return; return;
auto user = attack.GetUser();
auto attackData = attack.GetAttack()->GetAttack(); auto attackData = attack.GetAttack()->GetAttack();
auto library = user->GetBattle()->GetLibrary(); auto library = user->GetBattle()->GetLibrary();
auto dmgLibrary = library->GetDamageLibrary(); auto dmgLibrary = library->GetDamageLibrary();
@ -108,14 +118,16 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack &attack, Creature *targe
} }
auto hit = targetData.GetHit(hitIndex); auto hit = targetData.GetHit(hitIndex);
//HOOK: Change move type auto hitType = hit.GetType();
hit.SetEffectiveness(library->GetTypeLibrary()->GetEffectiveness(hit.GetType(), target->GetTypes())); HOOK(ChangeAttackType, targetSources, &attack, target, hitIndex, hitType);
hit.SetEffectiveness(library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes()));
hit.SetCritical(library->GetCriticalLibrary()->IsCritical(&attack, target, hitIndex)); hit.SetCritical(library->GetCriticalLibrary()->IsCritical(&attack, target, hitIndex));
hit.SetBasePower(dmgLibrary->GetBasePower(&attack, target, hitIndex)); hit.SetBasePower(dmgLibrary->GetBasePower(&attack, target, hitIndex));
hit.SetDamage(dmgLibrary->GetDamage(&attack, target, hitIndex)); hit.SetDamage(dmgLibrary->GetDamage(&attack, target, hitIndex));
std::array<ScriptSource*, 1> attackSource = {&attack};
if (attackData->GetCategory() == Library::AttackCategory::Status){ if (attackData->GetCategory() == Library::AttackCategory::Status){
//HOOK: Status attack HOOK(OnStatusMove, attackSource, &attack, target, hitIndex);
} }
else{ else{
auto damage = hit.GetDamage(); auto damage = hit.GetDamage();
@ -125,14 +137,17 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack &attack, Creature *targe
} }
if (damage > 0){ if (damage > 0){
target->Damage(damage, DamageSource::AttackDamage); target->Damage(damage, DamageSource::AttackDamage);
//HOOK: Prevent secondary effects
bool preventSecondary = false;
HOOK(PreventSecondaryEffects, targetSources, &attack, target, hitIndex, preventSecondary);
if (!preventSecondary){
//HOOK: On Move Hit //HOOK: On Move Hit
} }
} }
} }
}
if (!user->IsFainted()){ if (!user->IsFainted())
//HOOK: On After Hits HOOK(OnAfterHits, userSources, attack, target);
} }
} }

View File

@ -7,7 +7,7 @@
#include "Creature.hpp" #include "Creature.hpp"
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class ExecutingAttack { class ExecutingAttack : public ScriptSource {
public: public:
class HitData{ class HitData{
bool _critical = false; bool _critical = false;
@ -65,6 +65,7 @@ namespace CreatureLib::Battling {
std::unordered_map<Creature*, TargetData> _targets; std::unordered_map<Creature*, TargetData> _targets;
Creature* _user; Creature* _user;
LearnedAttack* _attack; LearnedAttack* _attack;
Script* _script;
public: public:
TargetData& GetAttackDataForTarget(Creature* creature){ TargetData& GetAttackDataForTarget(Creature* creature){
@ -86,6 +87,10 @@ namespace CreatureLib::Battling {
LearnedAttack* GetAttack(){ LearnedAttack* GetAttack(){
return _attack; return _attack;
} }
void GetActiveScripts(ScriptAggregator &aggr) override {
aggr.Add(_script);
}
}; };
} }

View File

@ -1,15 +0,0 @@
#ifndef CREATURELIB_HOOKS_HPP
#define CREATURELIB_HOOKS_HPP
#include <cstdint>
namespace CreatureLib::Battling{
enum class Hook : uint8_t{
IncomingAttackFails,
IsInvulnerable,
AttackMiss,
ChangeMove,
};
}
#endif //CREATURELIB_HOOKS_HPP

View File

@ -5,7 +5,6 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "Hooks.hpp"
namespace CreatureLib::Battling{ namespace CreatureLib::Battling{
class ExecutingAttack; class ExecutingAttack;
@ -24,9 +23,15 @@ namespace CreatureLib::Battling{
return _name; return _name;
} }
virtual void OnIncomingAttackFails(ExecutingAttack* attack, Creature* target){}; virtual void FailIncomingAttack(ExecutingAttack* attack, Creature* target, bool& result){};
virtual void IsInvulnerable(ExecutingAttack* attack, Creature* target){}; virtual void IsInvulnerable(ExecutingAttack* attack, Creature* target , bool& result){};
virtual void OnAttackMiss(ExecutingAttack* attack, Creature* target){}; virtual void OnAttackMiss(ExecutingAttack* attack, Creature* target){};
virtual void ChangeAttackType(ExecutingAttack* attack, Creature* target, uint8_t hitNumber, uint8_t& type){};
virtual void OnStatusMove(const ExecutingAttack* attack, Creature* target, uint8_t hitNumber){};
virtual void PreventSecondaryEffects(const ExecutingAttack* attack, Creature* target, uint8_t hitNumber, bool& result){};
virtual void OnSecondaryEffect(const ExecutingAttack* attack, Creature* target, uint8_t hitNumber){};
virtual void OnAfterHits(const ExecutingAttack* attack, Creature* target);
}; };
} }