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) {
//HOOK: Check if attack fails on target
std::array<ScriptSource*, 1> sources = {target};
HOOK(OnIncomingAttackFails, sources, &attack, target);
auto user = attack.GetUser();
//HOOK: Check if target is invulnerable
HOOK(IsInvulnerable, sources, &attack, target);
std::array<ScriptSource*, 1> targetSources = {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()){
//HOOK: On attack miss.
HOOK(OnAttackMiss, sources, &attack, target);
HOOK(OnAttackMiss, targetSources, &attack, target);
return;
}
auto numHits = targetData.GetNumberOfHits();
if (numHits == 0)
return;
auto user = attack.GetUser();
auto attackData = attack.GetAttack()->GetAttack();
auto library = user->GetBattle()->GetLibrary();
auto dmgLibrary = library->GetDamageLibrary();
@ -108,14 +118,16 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack &attack, Creature *targe
}
auto hit = targetData.GetHit(hitIndex);
//HOOK: Change move type
hit.SetEffectiveness(library->GetTypeLibrary()->GetEffectiveness(hit.GetType(), target->GetTypes()));
auto hitType = hit.GetType();
HOOK(ChangeAttackType, targetSources, &attack, target, hitIndex, hitType);
hit.SetEffectiveness(library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes()));
hit.SetCritical(library->GetCriticalLibrary()->IsCritical(&attack, target, hitIndex));
hit.SetBasePower(dmgLibrary->GetBasePower(&attack, target, hitIndex));
hit.SetDamage(dmgLibrary->GetDamage(&attack, target, hitIndex));
std::array<ScriptSource*, 1> attackSource = {&attack};
if (attackData->GetCategory() == Library::AttackCategory::Status){
//HOOK: Status attack
HOOK(OnStatusMove, attackSource, &attack, target, hitIndex);
}
else{
auto damage = hit.GetDamage();
@ -125,14 +137,17 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack &attack, Creature *targe
}
if (damage > 0){
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
}
}
}
}
if (!user->IsFainted()){
//HOOK: On After Hits
if (!user->IsFainted())
HOOK(OnAfterHits, userSources, attack, target);
}
}

View File

@ -7,7 +7,7 @@
#include "Creature.hpp"
namespace CreatureLib::Battling {
class ExecutingAttack {
class ExecutingAttack : public ScriptSource {
public:
class HitData{
bool _critical = false;
@ -65,6 +65,7 @@ namespace CreatureLib::Battling {
std::unordered_map<Creature*, TargetData> _targets;
Creature* _user;
LearnedAttack* _attack;
Script* _script;
public:
TargetData& GetAttackDataForTarget(Creature* creature){
@ -86,6 +87,10 @@ namespace CreatureLib::Battling {
LearnedAttack* GetAttack(){
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 <utility>
#include <vector>
#include "Hooks.hpp"
namespace CreatureLib::Battling{
class ExecutingAttack;
@ -24,9 +23,15 @@ namespace CreatureLib::Battling{
return _name;
}
virtual void OnIncomingAttackFails(ExecutingAttack* attack, Creature* target){};
virtual void IsInvulnerable(ExecutingAttack* attack, Creature* target){};
virtual void FailIncomingAttack(ExecutingAttack* attack, Creature* target, bool& result){};
virtual void IsInvulnerable(ExecutingAttack* attack, Creature* target , bool& result){};
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);
};
}