diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index a551207..9caa1a8 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -25,13 +25,28 @@ void TurnHandler::RunTurn(const ArbUt::BorrowedPtr& queue, const Ar << " with message"); } if (!battle->HasEnded()) { + ArbUt::List scripts; for (const auto& side : battle->GetSides()) { for (const auto& creature : side->GetCreatures()) { if (!creature.HasValue()) { continue; } - HOOK(OnEndTurn, creature.GetValue(), creature.GetValue()); + scripts.Clear(); + creature.GetValue()->GetOwnScripts(scripts); + for (auto& s : scripts) { + EXECUTE_SCRIPT_FUNC(s, OnEndTurn); + } } + scripts.Clear(); + side->GetOwnScripts(scripts); + for (auto& s : scripts) { + EXECUTE_SCRIPT_FUNC(s, OnEndTurn); + } + } + scripts.Clear(); + battle->GetOwnScripts(scripts); + for (auto& s : scripts) { + EXECUTE_SCRIPT_FUNC(s, OnEndTurn); } } queue->HasCompletedQueue = true; diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index de1e3fa..38d8935 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -113,9 +113,9 @@ bool Battle::CreatureInField(ArbUt::BorrowedPtr creature) const { void Battle::ForceRecall(uint8_t side, uint8_t index) { _sides[side]->SetCreature(nullptr, index); } size_t Battle::ScriptCount() const { return 1; } -void Battle::GetActiveScripts(ArbUt::List& scripts) { - scripts.Append(ScriptWrapper::FromSet(&_volatile)); -} +void Battle::GetActiveScripts(ArbUt::List& scripts) { GetOwnScripts(scripts); } + +void Battle::GetOwnScripts(ArbUt::List& scripts) { scripts.Append(ScriptWrapper::FromSet(&_volatile)); } void Battle::SwitchCreature(uint8_t sideIndex, uint8_t index, Creature* c) { auto side = this->_sides[sideIndex]; diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index f69a1a4..04ed601 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -79,6 +79,7 @@ namespace CreatureLib::Battling { size_t ScriptCount() const override; void GetActiveScripts(ArbUt::List& scripts) override; + void GetOwnScripts(ArbUt::List& scripts) override; void ValidateBattleState(); inline bool HasEnded() const noexcept { return _hasEnded; } diff --git a/src/Battling/Models/BattleSide.cpp b/src/Battling/Models/BattleSide.cpp index fb6025e..3745b73 100644 --- a/src/Battling/Models/BattleSide.cpp +++ b/src/Battling/Models/BattleSide.cpp @@ -82,9 +82,12 @@ bool BattleSide::CreatureOnSide(const ArbUt::BorrowedPtr& creature) co const ArbUt::OptionalBorrowedPtr& BattleSide::GetCreature(uint8_t index) const { return _creatures[index]; } void BattleSide::GetActiveScripts(ArbUt::List& scripts) { - scripts.Append(ScriptWrapper::FromSet(&_volatile)); + GetOwnScripts(scripts); _battle->GetActiveScripts(scripts); } +void BattleSide::GetOwnScripts(ArbUt::List& scripts) { + scripts.Append(ScriptWrapper::FromSet(&_volatile)); +} size_t BattleSide::ScriptCount() const { return _battle->ScriptCount() + 1; } uint8_t BattleSide::GetRandomCreatureIndex() { diff --git a/src/Battling/Models/BattleSide.hpp b/src/Battling/Models/BattleSide.hpp index 1cb5b43..3251875 100644 --- a/src/Battling/Models/BattleSide.hpp +++ b/src/Battling/Models/BattleSide.hpp @@ -50,6 +50,7 @@ namespace CreatureLib::Battling { size_t ScriptCount() const override; void GetActiveScripts(ArbUt::List& scripts) override; + void GetOwnScripts(ArbUt::List& scripts) override; const ArbUt::List>& GetCreatures() { return _creatures; } diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 103c714..35a810e 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -234,13 +234,17 @@ namespace CreatureLib::Battling { } void Creature::GetActiveScripts(ArbUt::List& scripts) { - scripts.Append(ScriptWrapper::FromScript(&_activeTalent)); - scripts.Append(ScriptWrapper::FromScript(&_status)); - scripts.Append(ScriptWrapper::FromSet(&_volatile)); + GetOwnScripts(scripts); if (_battleData.Side.HasValue()) { _battleData.Side.GetValue()->GetActiveScripts(scripts); } } + void Creature::GetOwnScripts(ArbUt::List& scripts) { + scripts.Append(ScriptWrapper::FromScript(&_activeTalent)); + scripts.Append(ScriptWrapper::FromScript(&_status)); + scripts.Append(ScriptWrapper::FromSet(&_volatile)); + } + void Creature::ClearVolatileScripts() { _volatile.Clear(); } void Creature::AddExperience(uint32_t amount) { auto maxLevel = _library->GetSettings()->GetMaximalLevel(); diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index 9d0cee0..f1d2999 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -148,6 +148,7 @@ namespace CreatureLib::Battling { size_t ScriptCount() const override; void GetActiveScripts(ArbUt::List& scripts) override; + void GetOwnScripts(ArbUt::List& scripts) override; void ClearVolatileScripts(); BattleScript* AddVolatileScript(const ArbUt::StringView& name); BattleScript* AddVolatileScript(BattleScript* script); diff --git a/src/Battling/Models/ExecutingAttack.hpp b/src/Battling/Models/ExecutingAttack.hpp index 8a687ab..adbe058 100644 --- a/src/Battling/Models/ExecutingAttack.hpp +++ b/src/Battling/Models/ExecutingAttack.hpp @@ -105,11 +105,13 @@ namespace CreatureLib::Battling { inline ArbUt::BorrowedPtr GetScript() const noexcept { return _script; } - protected: void GetActiveScripts(ArbUt::List& scripts) override { - scripts.Append(ScriptWrapper::FromScript(&_script)); + GetOwnScripts(scripts); _user->GetActiveScripts(scripts); } + void GetOwnScripts(ArbUt::List& scripts) override { + scripts.Append(ScriptWrapper::FromScript(&_script)); + } }; } diff --git a/src/Battling/ScriptHandling/BattleScript.hpp b/src/Battling/ScriptHandling/BattleScript.hpp index a8cbf22..c966c95 100644 --- a/src/Battling/ScriptHandling/BattleScript.hpp +++ b/src/Battling/ScriptHandling/BattleScript.hpp @@ -91,7 +91,7 @@ namespace CreatureLib::Battling { virtual void PreventOpponentRunAway([[maybe_unused]] const FleeTurnChoice* choice, [[maybe_unused]] bool* result){}; - virtual void OnEndTurn([[maybe_unused]] Creature* creature){}; + virtual void OnEndTurn(){}; }; } diff --git a/src/Battling/ScriptHandling/ScriptMacros.hpp b/src/Battling/ScriptHandling/ScriptMacros.hpp index bf1e563..5239859 100644 --- a/src/Battling/ScriptHandling/ScriptMacros.hpp +++ b/src/Battling/ScriptHandling/ScriptMacros.hpp @@ -16,3 +16,22 @@ THROW("Exception setting up script hook '" #hookName "': " << e.what()) \ } \ } + +#define EXECUTE_SCRIPT_FUNC(scriptWrapper, hookName, ...) \ + { \ + try { \ + if (scriptWrapper.HasValue()) { \ + if (scriptWrapper.IsSet()) { \ + for (const auto& sv : scriptWrapper.GetScriptSet()->GetIterator()) { \ + sv->hookName(__VA_ARGS__); \ + } \ + } else { \ + scriptWrapper.GetScript()->get()->hookName(__VA_ARGS__); \ + } \ + } \ + } catch (const ArbUt::Exception& e) { \ + throw e; \ + } catch (const std::exception& e) { \ + THROW("Exception setting up script hook '" #hookName "': " << e.what()) \ + } \ + } \ No newline at end of file diff --git a/src/Battling/ScriptHandling/ScriptSource.hpp b/src/Battling/ScriptHandling/ScriptSource.hpp index c340ac2..c9b9b69 100644 --- a/src/Battling/ScriptHandling/ScriptSource.hpp +++ b/src/Battling/ScriptHandling/ScriptSource.hpp @@ -12,7 +12,6 @@ namespace CreatureLib::Battling { ScriptAggregator _scriptsIterator; protected: - virtual void GetActiveScripts(ArbUt::List& scripts) = 0; void ResetActiveScripts() { _areScriptsInitialized = false; _scripts.Clear(); @@ -30,6 +29,9 @@ namespace CreatureLib::Battling { _scriptsIterator.Reset(); return _scriptsIterator; } + + virtual void GetActiveScripts(ArbUt::List& scripts) = 0; + virtual void GetOwnScripts(ArbUt::List& scripts) = 0; }; } diff --git a/src/Battling/TurnChoices/AttackTurnChoice.hpp b/src/Battling/TurnChoices/AttackTurnChoice.hpp index cafd30d..d6a910e 100644 --- a/src/Battling/TurnChoices/AttackTurnChoice.hpp +++ b/src/Battling/TurnChoices/AttackTurnChoice.hpp @@ -66,11 +66,13 @@ namespace CreatureLib::Battling { const std::unique_ptr& GetAttackScript() const noexcept { return _attackScript; } size_t ScriptCount() const override { return 1 + GetUser()->ScriptCount(); } - protected: void GetActiveScripts(ArbUt::List& scripts) override { - scripts.Append(ScriptWrapper::FromScript(&_attackScript)); + GetOwnScripts(scripts); GetUser()->GetActiveScripts(scripts); } + void GetOwnScripts(ArbUt::List& scripts) override { + scripts.Append(ScriptWrapper::FromScript(&_attackScript)); + } }; } diff --git a/src/Battling/TurnChoices/FleeTurnChoice.hpp b/src/Battling/TurnChoices/FleeTurnChoice.hpp index 2f0a54e..be5ee7d 100644 --- a/src/Battling/TurnChoices/FleeTurnChoice.hpp +++ b/src/Battling/TurnChoices/FleeTurnChoice.hpp @@ -14,6 +14,7 @@ namespace CreatureLib::Battling { protected: void GetActiveScripts(ArbUt::List& scripts) override { GetUser()->GetActiveScripts(scripts); } + void GetOwnScripts(ArbUt::List&) override {} }; } diff --git a/src/Battling/TurnChoices/PassTurnChoice.hpp b/src/Battling/TurnChoices/PassTurnChoice.hpp index b0b86fc..07b9ae3 100644 --- a/src/Battling/TurnChoices/PassTurnChoice.hpp +++ b/src/Battling/TurnChoices/PassTurnChoice.hpp @@ -13,6 +13,7 @@ namespace CreatureLib::Battling { protected: void GetActiveScripts(ArbUt::List& scripts) override { GetUser()->GetActiveScripts(scripts); } + void GetOwnScripts(ArbUt::List&) override {} }; } diff --git a/src/Battling/TurnChoices/SwitchTurnChoice.hpp b/src/Battling/TurnChoices/SwitchTurnChoice.hpp index f47f17e..63a8448 100644 --- a/src/Battling/TurnChoices/SwitchTurnChoice.hpp +++ b/src/Battling/TurnChoices/SwitchTurnChoice.hpp @@ -17,6 +17,7 @@ namespace CreatureLib::Battling { protected: void GetActiveScripts(ArbUt::List& scripts) override { GetUser()->GetActiveScripts(scripts); } + void GetOwnScripts(ArbUt::List&) override {} }; } diff --git a/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp b/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp index 569f8fa..2bb0eba 100644 --- a/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp +++ b/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp @@ -24,7 +24,8 @@ public: protected: size_t ScriptCount() const override { return 1; } - void GetActiveScripts(ArbUt::List& scripts) override { + void GetActiveScripts(ArbUt::List& scripts) override { GetOwnScripts(scripts); } + void GetOwnScripts(ArbUt::List& scripts) override { scripts.Append(ScriptWrapper::FromScript(&ScriptPtr)); } }; @@ -35,9 +36,8 @@ public: protected: size_t ScriptCount() const override { return 1; } - void GetActiveScripts(ArbUt::List& scripts) override { - scripts.Append(ScriptWrapper::FromSet(&Set)); - } + void GetActiveScripts(ArbUt::List& scripts) override { GetOwnScripts(scripts); } + void GetOwnScripts(ArbUt::List& scripts) override { scripts.Append(ScriptWrapper::FromSet(&Set)); } }; TEST_CASE("Script source with unset script ptr.") {