From 0117cb9d643d8463f35e8496e1a97a32824d38f4 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 14 May 2022 15:09:58 +0200 Subject: [PATCH] Use smart ArbUt pointers for returning scripts. --- src/Battling/Flow/TurnHandler.cpp | 12 ++++++------ src/Battling/Models/Creature.cpp | 6 +++++- src/Battling/ScriptHandling/ScriptResolver.hpp | 8 ++++---- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index c45a1e1..568923e 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -367,8 +367,8 @@ void TurnHandler::ExecuteItemChoice(const ArbUt::BorrowedPtr& ch if (!battle.HasValue()) { return; } - auto* script = battle.GetValue()->GetLibrary()->GetScriptResolver()->LoadItemScript(choice->GetItem()); - if (script == nullptr) { + auto script = battle.GetValue()->GetLibrary()->GetScriptResolver()->LoadItemScript(choice->GetItem()); + if (!script.HasValue()) { return; } const auto& targetIndex = choice->GetTarget(); @@ -376,17 +376,17 @@ void TurnHandler::ExecuteItemChoice(const ArbUt::BorrowedPtr& ch if (targetIndex.has_value()) { target = battle.GetValue()->GetCreature(targetIndex.value()); } - auto isCreatureUseItem = script->IsCreatureUseItem(); + auto isCreatureUseItem = script.GetValue()->IsCreatureUseItem(); if (isCreatureUseItem && !target.HasValue()) { target = choice->GetUser(); } if (isCreatureUseItem) { - if (!script->IsUseValidForCreature(target.GetValue())) { + if (!script.GetValue()->IsUseValidForCreature(target.GetValue())) { return; } - script->OnCreatureUse(target.GetValue(), true); + script.GetValue()->OnCreatureUse(target.GetValue(), true); } else { - script->OnUse(battle.GetValue()); + script.GetValue()->OnUse(battle.GetValue()); } } diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 180485d..64d26fa 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -365,7 +365,11 @@ namespace CreatureLib::Battling { return false; } - auto* script = _library->GetScriptResolver()->LoadItemScript(_heldItem.GetValue()); + auto scriptOpt = _library->GetScriptResolver()->LoadItemScript(_heldItem.GetValue()); + if (!scriptOpt.HasValue()) { + return false; + } + auto script = scriptOpt.GetValue(); auto isCreatureUseItem = script->IsCreatureUseItem(); if (isCreatureUseItem) { if (!script->IsUseValidForCreature(this)) { diff --git a/src/Battling/ScriptHandling/ScriptResolver.hpp b/src/Battling/ScriptHandling/ScriptResolver.hpp index 831c77b..c862577 100644 --- a/src/Battling/ScriptHandling/ScriptResolver.hpp +++ b/src/Battling/ScriptHandling/ScriptResolver.hpp @@ -18,13 +18,13 @@ namespace CreatureLib::Battling { virtual ~ScriptResolver() = default; virtual void Initialize([[maybe_unused]] BattleLibrary* non_null library){}; - virtual BattleScript* nullable LoadScript([[maybe_unused]] const ArbUt::OptionalBorrowedPtr& owner, - [[maybe_unused]] ScriptCategory category, - [[maybe_unused]] const ArbUt::StringView& scriptName) { + virtual ArbUt::OptionalUniquePtr + LoadScript([[maybe_unused]] const ArbUt::OptionalBorrowedPtr& owner, + [[maybe_unused]] ScriptCategory category, [[maybe_unused]] const ArbUt::StringView& scriptName) { return nullptr; }; - virtual ItemUseScript* nullable + virtual ArbUt::OptionalUniquePtr LoadItemScript([[maybe_unused]] const CreatureLib::Library::Item* non_null item) { return nullptr; };