From c40f063683a5edbe5f40e1ad9edf37d4401f720c Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 13 Feb 2022 13:40:33 +0100 Subject: [PATCH] Initial work on item use handling --- CInterface/Battling/ItemUseScript.cpp | 6 ++-- src/Battling/Flow/TurnHandler.cpp | 28 ++++++++++++++++++- src/Battling/Flow/TurnHandler.hpp | 2 ++ src/Battling/ScriptHandling/ItemUseScript.hpp | 5 ++-- src/Battling/TurnChoices/ItemTurnChoice.hpp | 25 +++++++++++++++++ 5 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 src/Battling/TurnChoices/ItemTurnChoice.hpp diff --git a/CInterface/Battling/ItemUseScript.cpp b/CInterface/Battling/ItemUseScript.cpp index 38e6967..a597893 100644 --- a/CInterface/Battling/ItemUseScript.cpp +++ b/CInterface/Battling/ItemUseScript.cpp @@ -14,7 +14,7 @@ export uint8_t CreatureLib_ItemUseScript_IsUseValidForCreature(ItemUseScript* p, export uint8_t CreatureLib_ItemUseScript_IsHoldable(ItemUseScript* p, uint8_t& out) { Try(out = p->IsHoldable()) } -export uint8_t CreatureLib_ItemUseScript_OnUse(ItemUseScript* p) { Try(p->OnUse()) } -export uint8_t CreatureLib_ItemUseScript_OnCreatureUse(ItemUseScript* p, Creature* creature) { - Try(p->OnCreatureUse(creature)) +export uint8_t CreatureLib_ItemUseScript_OnUse(ItemUseScript* p, Battle* battle) { Try(p->OnUse(battle)) } +export uint8_t CreatureLib_ItemUseScript_OnCreatureUse(ItemUseScript* p, Creature* creature, bool isBattle) { + Try(p->OnCreatureUse(creature, isBattle)) } \ No newline at end of file diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index cd06b82..102b9e9 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -79,7 +79,7 @@ void TurnHandler::ExecuteChoice(const ArbUt::BorrowedPtr& choice case TurnChoiceKind::Switch: return ExecuteSwitchChoice(choice.ForceAs()); case TurnChoiceKind::Flee: return ExecuteFleeChoice(choice.ForceAs()); - case TurnChoiceKind::Item: NOT_IMPLEMENTED; + case TurnChoiceKind::Item: return ExecuteItemChoice(choice.ForceAs()); } } @@ -361,3 +361,29 @@ void TurnHandler::ExecuteFleeChoice(const ArbUt::BorrowedPtr& ch battle.GetValue()->ValidateBattleState(); } } +void TurnHandler::ExecuteItemChoice(const ArbUt::BorrowedPtr& choice) { + auto user = choice->GetUser(); + auto battle = user->GetBattle(); + if (!battle.HasValue()) { + return; + } + auto* script = battle.GetValue()->GetLibrary()->GetScriptResolver()->LoadItemScript(choice->GetItem()); + auto targetIndex = choice->GetTarget(); + ArbUt::OptionalBorrowedPtr target = nullptr; + if (targetIndex.has_value()) { + target = battle.GetValue()->GetCreature(targetIndex.value()); + } + auto isCreatureUseItem = script->IsCreatureUseItem(); + if (isCreatureUseItem && !target.HasValue()) { + target = choice->GetUser(); + } + + if (isCreatureUseItem) { + if (!script->IsUseValidForCreature(target.GetValue())) { + return; + } + script->OnCreatureUse(target.GetValue(), true); + } else { + script->OnUse(battle.GetValue()); + } +} diff --git a/src/Battling/Flow/TurnHandler.hpp b/src/Battling/Flow/TurnHandler.hpp index 9067486..41bc93e 100644 --- a/src/Battling/Flow/TurnHandler.hpp +++ b/src/Battling/Flow/TurnHandler.hpp @@ -4,6 +4,7 @@ #include "../Models/ExecutingAttack.hpp" #include "../TurnChoices/AttackTurnChoice.hpp" #include "../TurnChoices/FleeTurnChoice.hpp" +#include "../TurnChoices/ItemTurnChoice.hpp" #include "../TurnChoices/SwitchTurnChoice.hpp" #include "ChoiceQueue.hpp" @@ -18,6 +19,7 @@ namespace CreatureLib::Battling { static void ExecuteSwitchChoice(const ArbUt::BorrowedPtr& choice); static void ExecuteFleeChoice(const ArbUt::BorrowedPtr& choice); + static void ExecuteItemChoice(const ArbUt::BorrowedPtr& choice); public: static void RunTurn(const ArbUt::BorrowedPtr& queue, const ArbUt::BorrowedPtr& battle); diff --git a/src/Battling/ScriptHandling/ItemUseScript.hpp b/src/Battling/ScriptHandling/ItemUseScript.hpp index f992fc1..89947ba 100644 --- a/src/Battling/ScriptHandling/ItemUseScript.hpp +++ b/src/Battling/ScriptHandling/ItemUseScript.hpp @@ -5,6 +5,7 @@ #include "../../Library/EffectParameter.hpp" namespace CreatureLib::Battling { class Creature; + class Battle; class ItemUseScript { public: @@ -22,8 +23,8 @@ namespace CreatureLib::Battling { /// @brief Can the item be held? [[nodiscard]] virtual bool IsHoldable() const { return false; } - virtual void OnUse() const {} - virtual void OnCreatureUse([[maybe_unused]] Creature* creature) const {} + virtual void OnUse([[maybe_unused]] Battle* battle) const {} + virtual void OnCreatureUse([[maybe_unused]] Creature* creature, [[maybe_unused]] bool isBattle) const {} }; } diff --git a/src/Battling/TurnChoices/ItemTurnChoice.hpp b/src/Battling/TurnChoices/ItemTurnChoice.hpp new file mode 100644 index 0000000..d19828e --- /dev/null +++ b/src/Battling/TurnChoices/ItemTurnChoice.hpp @@ -0,0 +1,25 @@ +#ifndef CREATURELIB_ITEMTURNCHOICE_HPP +#define CREATURELIB_ITEMTURNCHOICE_HPP + +#include "../../Library/Items/Item.hpp" +#include "../Models/CreatureIndex.hpp" +#include "BaseTurnChoice.hpp" +namespace CreatureLib::Battling { + class ItemTurnChoice : public BaseTurnChoice { + ArbUt::BorrowedPtr _item; + std::optional _target; + + public: + ItemTurnChoice(ArbUt::BorrowedPtr user, const ArbUt::BorrowedPtr& item, + const std::optional& target) + : BaseTurnChoice(user), _item(item), _target(target) {} + ~ItemTurnChoice() override = default; + + [[nodiscard]] TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind::Item; } + + [[nodiscard]] const ArbUt::BorrowedPtr& GetItem() const noexcept { return _item; } + [[nodiscard]] const std::optional& GetTarget() const noexcept { return _target; } + }; +} + +#endif // CREATURELIB_ITEMTURNCHOICE_HPP