From 5672f2d2a7b0f246d60db486a862c4303638e2f1 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 12 Mar 2020 11:15:00 +0100 Subject: [PATCH] Support for attack when other attacks can't be used in MiscLibrary. --- CInterface/Battling/MiscLibrary.cpp | 12 ++++++-- src/Battling/Library/MiscLibrary.cpp | 29 +++++++++++++++++++ src/Battling/Library/MiscLibrary.hpp | 2 ++ src/Battling/TurnChoices/AttackTurnChoice.hpp | 21 +++++++++++++- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/CInterface/Battling/MiscLibrary.cpp b/CInterface/Battling/MiscLibrary.cpp index 75e45df..b0e21f0 100644 --- a/CInterface/Battling/MiscLibrary.cpp +++ b/CInterface/Battling/MiscLibrary.cpp @@ -6,7 +6,13 @@ export MiscLibrary* CreatureLib_MiscLibrary_Construct() { return new MiscLibrary export void CreatureLib_MiscLibrary_Destruct(const MiscLibrary* p) { delete p; } -export void CreatureLib_MiscLibrary_IsCritical(MiscLibrary* p, ExecutingAttack* attack, Creature* target, uint8_t hit) { - p->IsCritical(attack, target, hit); +export bool CreatureLib_MiscLibrary_IsCritical(MiscLibrary* p, ExecutingAttack* attack, Creature* target, uint8_t hit) { + return p->IsCritical(attack, target, hit); +}; +export bool CreatureLib_MiscLibrary_CanFlee(MiscLibrary* p, FleeTurnChoice* switchChoice) { + return p->CanFlee(switchChoice); +}; +export BaseTurnChoice* CreatureLib_MiscLibrary_ReplacementAttack(MiscLibrary* p, Creature* user, uint8_t sideTarget, + uint8_t creatureTarget) { + return p->ReplacementAttack(user, CreatureIndex(sideTarget, creatureTarget)); }; -export void CreatureLib_MiscLibrary_CanFlee(MiscLibrary* p, FleeTurnChoice* switchChoice) { p->CanFlee(switchChoice); }; \ No newline at end of file diff --git a/src/Battling/Library/MiscLibrary.cpp b/src/Battling/Library/MiscLibrary.cpp index 217c626..35459ee 100644 --- a/src/Battling/Library/MiscLibrary.cpp +++ b/src/Battling/Library/MiscLibrary.cpp @@ -1,9 +1,38 @@ #include "MiscLibrary.hpp" #include "../Models/Battle.hpp" +#include "../TurnChoices/AttackTurnChoice.hpp" bool CreatureLib::Battling::MiscLibrary::IsCritical(CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target, uint8_t hit) const { auto rand = target->GetBattle()->GetRandom(); return rand->Get(10) <= 0; } + +static CreatureLib::Battling::LearnedAttack* _replacementAttack = nullptr; +static CreatureLib::Library::AttackData* _replacementAttackData = nullptr; + +static CreatureLib::Library::AttackData* GetReplacementAttackData() { + if (_replacementAttackData == nullptr) { + _replacementAttackData = + new CreatureLib::Library::AttackData("replacement"_cnc, 0, CreatureLib::Library::AttackCategory::Physical, + 30, 255, 255, CreatureLib::Library::AttackTarget::Any, 0, {}); + } + return _replacementAttackData; +} + +static CreatureLib::Battling::LearnedAttack* GetReplacementAttack() { + if (_replacementAttack == nullptr) { + _replacementAttack = new CreatureLib::Battling::LearnedAttack( + GetReplacementAttackData(), CreatureLib::Battling::AttackLearnMethod::Unknown); + } + return _replacementAttack; +} + bool CreatureLib::Battling::MiscLibrary::CanFlee(FleeTurnChoice* switchChoice) const { return true; } +CreatureLib::Battling::BaseTurnChoice* +CreatureLib::Battling::MiscLibrary::ReplacementAttack(Creature* user, CreatureIndex target) const { + auto sideTarget = 0; + if (user->GetBattleSide()->GetSideIndex() == 0) + sideTarget = 1; + return new AttackTurnChoice(user, GetReplacementAttack(), target); +} diff --git a/src/Battling/Library/MiscLibrary.hpp b/src/Battling/Library/MiscLibrary.hpp index cd83bb0..b837e8a 100644 --- a/src/Battling/Library/MiscLibrary.hpp +++ b/src/Battling/Library/MiscLibrary.hpp @@ -1,6 +1,7 @@ #ifndef CREATURELIB_MISCLIBRARY_HPP #define CREATURELIB_MISCLIBRARY_HPP +#include "../Models/CreatureIndex.hpp" #include "../Models/ExecutingAttack.hpp" #include "../TurnChoices/FleeTurnChoice.hpp" @@ -10,6 +11,7 @@ namespace CreatureLib::Battling { virtual ~MiscLibrary() = default; virtual bool IsCritical(ExecutingAttack* attack, Creature* target, uint8_t hit) const; virtual bool CanFlee(FleeTurnChoice* switchChoice) const; + virtual BaseTurnChoice* ReplacementAttack(Creature* user, CreatureIndex target) const; }; } diff --git a/src/Battling/TurnChoices/AttackTurnChoice.hpp b/src/Battling/TurnChoices/AttackTurnChoice.hpp index 4cb95f8..127d521 100644 --- a/src/Battling/TurnChoices/AttackTurnChoice.hpp +++ b/src/Battling/TurnChoices/AttackTurnChoice.hpp @@ -1,8 +1,10 @@ #ifndef CREATURELIB_ATTACKTURNCHOICE_HPP #define CREATURELIB_ATTACKTURNCHOICE_HPP +#include "../Models/Battle.hpp" #include "../Models/CreatureIndex.hpp" #include "../Models/LearnedAttack.hpp" +#include "../ScriptHandling/ScriptCategory.hpp" #include "BaseTurnChoice.hpp" namespace CreatureLib::Battling { @@ -11,9 +13,26 @@ namespace CreatureLib::Battling { CreatureIndex _target; Script* _attackScript = nullptr; + void ResolveScript() { + if (_attackScript != nullptr) + return; + auto user = GetUser(); + if (user == nullptr) + return; + auto battle = user->GetBattle(); + if (battle != nullptr) { + auto library = battle->GetLibrary(); + _attackScript = library->LoadScript(ScriptCategory::Attack, _attack->GetAttack()->GetName()); + } + } + public: AttackTurnChoice(Creature* user, LearnedAttack* attack, const CreatureIndex& target) - : BaseTurnChoice(user), _attack(attack), _target(target) {} + : BaseTurnChoice(user), _attack(attack), _target(target) { + ResolveScript(); + } + AttackTurnChoice(Creature* user, LearnedAttack* attack, const CreatureIndex& target, Script* script) + : BaseTurnChoice(user), _attack(attack), _target(target), _attackScript(script) {} inline LearnedAttack* GetAttack() const { return _attack; }