From be7a5fe6bd309696c1988eae07bc17b66fd2128e Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 29 Oct 2021 21:40:10 +0200 Subject: [PATCH] More work on setting owner of a script. Signed-off-by: Deukhoofd --- CInterface/Battling/ScriptResolver.cpp | 6 +++--- src/Battling/Library/BattleLibrary.cpp | 5 +++-- src/Battling/Library/BattleLibrary.hpp | 3 ++- src/Battling/Models/Battle.cpp | 2 +- src/Battling/Models/BattleSide.cpp | 2 +- src/Battling/Models/Creature.cpp | 13 ++++++++----- src/Battling/ScriptHandling/ScriptResolver.hpp | 3 ++- src/Battling/TurnChoices/AttackTurnChoice.hpp | 2 +- 8 files changed, 21 insertions(+), 15 deletions(-) diff --git a/CInterface/Battling/ScriptResolver.cpp b/CInterface/Battling/ScriptResolver.cpp index f26cc86..85377c9 100644 --- a/CInterface/Battling/ScriptResolver.cpp +++ b/CInterface/Battling/ScriptResolver.cpp @@ -9,9 +9,9 @@ export void CreatureLib_ScriptResolver_Destruct(const ScriptResolver* p) { delet export uint8_t CreatureLib_ScriptResolver_Initialize(ScriptResolver* p, BattleLibrary* library) { Try(p->Initialize(library);) }; -export uint8_t CreatureLib_ScriptResolver_LoadScript(BattleScript*& out, ScriptResolver* p, ScriptCategory category, - const char* scriptName) { - Try(out = p->LoadScript(category, ArbUt::StringView(scriptName));) +export uint8_t CreatureLib_ScriptResolver_LoadScript(BattleScript*& out, ScriptResolver* p, void* owner, + ScriptCategory category, const char* scriptName) { + Try(out = p->LoadScript(owner, category, ArbUt::StringView(scriptName));) }; export uint8_t CreatureLib_ScriptResolver_LoadItemScript(ItemUseScript*& out, ScriptResolver* p, diff --git a/src/Battling/Library/BattleLibrary.cpp b/src/Battling/Library/BattleLibrary.cpp index c2033db..2f784ff 100644 --- a/src/Battling/Library/BattleLibrary.cpp +++ b/src/Battling/Library/BattleLibrary.cpp @@ -43,6 +43,7 @@ const std::unique_ptr& BattleLibrary::GetDamageLibrary() co const std::unique_ptr& BattleLibrary::GetMiscLibrary() const noexcept { return _miscLibrary; } -BattleScript* BattleLibrary::LoadScript(ScriptCategory category, const ArbUt::StringView& scriptName) const { - return _scriptResolver->LoadScript(category, scriptName); +BattleScript* BattleLibrary::LoadScript(const ArbUt::OptionalBorrowedPtr& owner, ScriptCategory category, + const ArbUt::StringView& scriptName) const { + return _scriptResolver->LoadScript(owner, category, scriptName); } \ No newline at end of file diff --git a/src/Battling/Library/BattleLibrary.hpp b/src/Battling/Library/BattleLibrary.hpp index 8602342..c236096 100644 --- a/src/Battling/Library/BattleLibrary.hpp +++ b/src/Battling/Library/BattleLibrary.hpp @@ -44,7 +44,8 @@ namespace CreatureLib::Battling { return _scriptResolver; } - [[nodiscard]] BattleScript* LoadScript(ScriptCategory category, const ArbUt::StringView& scriptName) const; + [[nodiscard]] BattleScript* LoadScript(const ArbUt::OptionalBorrowedPtr& owner, ScriptCategory category, + const ArbUt::StringView& scriptName) const; }; } diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index 38d8935..7b0900a 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -167,7 +167,7 @@ BattleScript* Battle::AddVolatileScript(const ArbUt::StringView& key) { script.GetValue()->Stack(); return script.GetValue(); } - script = _library->LoadScript(ScriptCategory::Battle, key); + script = _library->LoadScript(this, ScriptCategory::Battle, key); if (!script.HasValue()) { THROW("Invalid volatile script requested for battle: '" << key.c_str() << "'."); } diff --git a/src/Battling/Models/BattleSide.cpp b/src/Battling/Models/BattleSide.cpp index 3745b73..bf4fbcf 100644 --- a/src/Battling/Models/BattleSide.cpp +++ b/src/Battling/Models/BattleSide.cpp @@ -142,7 +142,7 @@ BattleScript* BattleSide::AddVolatileScript(const ArbUt::StringView& key) { script.GetValue()->Stack(); return script.GetValue(); } - script = _battle->GetLibrary()->LoadScript(ScriptCategory::Side, key); + script = _battle->GetLibrary()->LoadScript(this, ScriptCategory::Side, key); if (!script.HasValue()) { THROW("Invalid volatile script requested for battleside: '" << key.c_str() << "'."); } diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 35a810e..eda69a0 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -19,7 +19,8 @@ namespace CreatureLib::Battling { _uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)), _talentIndex(talent), _hasOverridenTalent(false), _attacks(attacks), _allowedExperienceGain(allowedExperienceGain) { - _activeTalent = std::unique_ptr(_library->LoadScript(ScriptCategory::Talent, GetActiveTalent())); + _activeTalent = + std::unique_ptr(_library->LoadScript(this, ScriptCategory::Talent, GetActiveTalent())); for (auto t : _variant->GetTypes()) { _types.push_back(t); } @@ -60,7 +61,8 @@ namespace CreatureLib::Battling { } // Grab the new active talent. - _activeTalent = std::unique_ptr(_library->LoadScript(ScriptCategory::Talent, GetActiveTalent())); + _activeTalent = + std::unique_ptr(_library->LoadScript(this, ScriptCategory::Talent, GetActiveTalent())); // We modify the health of the creature by the change in its max health. auto prevHealth = GetBoostedStat(CreatureLib::Library::Statistic::Health); @@ -214,7 +216,7 @@ namespace CreatureLib::Battling { _hasOverridenTalent = true; if (_activeTalent != nullptr) { _activeTalent->OnRemove(); - _activeTalent.reset(this->_library->LoadScript(ScriptCategory::Talent, talent)); + _activeTalent.reset(this->_library->LoadScript(this, ScriptCategory::Talent, talent)); } _overridenTalentName = talent; } @@ -295,7 +297,7 @@ namespace CreatureLib::Battling { script.GetValue()->Stack(); return script.GetValue(); } - script = this->_library->LoadScript(ScriptCategory::Creature, name); + script = this->_library->LoadScript(this, ScriptCategory::Creature, name); if (!script.HasValue()) { THROW("Invalid volatile script requested for creature: '" << name.c_str() << "'."); } @@ -384,7 +386,8 @@ void CreatureLib::Battling::Creature::SetStatus(const ArbUt::StringView& name) { if (_status != nullptr) { _status->OnRemove(); } - _status = std::unique_ptr(_library->LoadScript(ScriptCategory::Status, name)); + _status = + std::unique_ptr(_library->LoadScript(this, ScriptCategory::Status, name)); if (_battleData.Battle.HasValue()) { _battleData.Battle.GetValue()->TriggerEventListener(this, name); } diff --git a/src/Battling/ScriptHandling/ScriptResolver.hpp b/src/Battling/ScriptHandling/ScriptResolver.hpp index 87704f3..3a68e37 100644 --- a/src/Battling/ScriptHandling/ScriptResolver.hpp +++ b/src/Battling/ScriptHandling/ScriptResolver.hpp @@ -17,7 +17,8 @@ namespace CreatureLib::Battling { virtual ~ScriptResolver() = default; virtual void Initialize([[maybe_unused]] BattleLibrary* library){}; - virtual BattleScript* LoadScript([[maybe_unused]] ScriptCategory category, + virtual BattleScript* LoadScript([[maybe_unused]] const ArbUt::OptionalBorrowedPtr& owner, + [[maybe_unused]] ScriptCategory category, [[maybe_unused]] const ArbUt::StringView& scriptName) { return nullptr; }; diff --git a/src/Battling/TurnChoices/AttackTurnChoice.hpp b/src/Battling/TurnChoices/AttackTurnChoice.hpp index d6a910e..370f774 100644 --- a/src/Battling/TurnChoices/AttackTurnChoice.hpp +++ b/src/Battling/TurnChoices/AttackTurnChoice.hpp @@ -25,7 +25,7 @@ namespace CreatureLib::Battling { auto library = battle.GetValue()->GetLibrary(); auto& effect = attack->GetSecondaryEffect(); _attackScript = std::unique_ptr( - library->LoadScript(ScriptCategory::Attack, effect->GetEffectName())); + library->LoadScript(this, ScriptCategory::Attack, effect->GetEffectName())); if (_attackScript != nullptr) { _attackScript->OnInitialize(effect->GetParameters()); }