Use smart ArbUt pointers for returning scripts.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-05-14 15:09:58 +02:00
parent cd50b2c2e8
commit 0117cb9d64
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 15 additions and 11 deletions

View File

@ -367,8 +367,8 @@ void TurnHandler::ExecuteItemChoice(const ArbUt::BorrowedPtr<ItemTurnChoice>& ch
if (!battle.HasValue()) { if (!battle.HasValue()) {
return; return;
} }
auto* script = battle.GetValue()->GetLibrary()->GetScriptResolver()->LoadItemScript(choice->GetItem()); auto script = battle.GetValue()->GetLibrary()->GetScriptResolver()->LoadItemScript(choice->GetItem());
if (script == nullptr) { if (!script.HasValue()) {
return; return;
} }
const auto& targetIndex = choice->GetTarget(); const auto& targetIndex = choice->GetTarget();
@ -376,17 +376,17 @@ void TurnHandler::ExecuteItemChoice(const ArbUt::BorrowedPtr<ItemTurnChoice>& ch
if (targetIndex.has_value()) { if (targetIndex.has_value()) {
target = battle.GetValue()->GetCreature(targetIndex.value()); target = battle.GetValue()->GetCreature(targetIndex.value());
} }
auto isCreatureUseItem = script->IsCreatureUseItem(); auto isCreatureUseItem = script.GetValue()->IsCreatureUseItem();
if (isCreatureUseItem && !target.HasValue()) { if (isCreatureUseItem && !target.HasValue()) {
target = choice->GetUser(); target = choice->GetUser();
} }
if (isCreatureUseItem) { if (isCreatureUseItem) {
if (!script->IsUseValidForCreature(target.GetValue())) { if (!script.GetValue()->IsUseValidForCreature(target.GetValue())) {
return; return;
} }
script->OnCreatureUse(target.GetValue(), true); script.GetValue()->OnCreatureUse(target.GetValue(), true);
} else { } else {
script->OnUse(battle.GetValue()); script.GetValue()->OnUse(battle.GetValue());
} }
} }

View File

@ -365,7 +365,11 @@ namespace CreatureLib::Battling {
return false; 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(); auto isCreatureUseItem = script->IsCreatureUseItem();
if (isCreatureUseItem) { if (isCreatureUseItem) {
if (!script->IsUseValidForCreature(this)) { if (!script->IsUseValidForCreature(this)) {

View File

@ -18,13 +18,13 @@ namespace CreatureLib::Battling {
virtual ~ScriptResolver() = default; virtual ~ScriptResolver() = default;
virtual void Initialize([[maybe_unused]] BattleLibrary* non_null library){}; virtual void Initialize([[maybe_unused]] BattleLibrary* non_null library){};
virtual BattleScript* nullable LoadScript([[maybe_unused]] const ArbUt::OptionalBorrowedPtr<void>& owner, virtual ArbUt::OptionalUniquePtr<BattleScript>
[[maybe_unused]] ScriptCategory category, LoadScript([[maybe_unused]] const ArbUt::OptionalBorrowedPtr<void>& owner,
[[maybe_unused]] const ArbUt::StringView& scriptName) { [[maybe_unused]] ScriptCategory category, [[maybe_unused]] const ArbUt::StringView& scriptName) {
return nullptr; return nullptr;
}; };
virtual ItemUseScript* nullable virtual ArbUt::OptionalUniquePtr<ItemUseScript>
LoadItemScript([[maybe_unused]] const CreatureLib::Library::Item* non_null item) { LoadItemScript([[maybe_unused]] const CreatureLib::Library::Item* non_null item) {
return nullptr; return nullptr;
}; };