From 01f6e59cb6ae638f0c34b85b9dc1fb80f22cbed2 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 31 May 2020 19:01:45 +0200 Subject: [PATCH] Make battle class fields all smart pointers. --- CInterface/Battling/Battle.cpp | 4 +++- src/Battling/Flow/TurnHandler.cpp | 2 +- src/Battling/Flow/TurnHandler.hpp | 2 +- src/Battling/Models/Battle.cpp | 5 ++--- src/Battling/Models/Battle.hpp | 27 +++++++++------------------ 5 files changed, 16 insertions(+), 24 deletions(-) diff --git a/CInterface/Battling/Battle.cpp b/CInterface/Battling/Battle.cpp index c8cd8ee..390f74f 100644 --- a/CInterface/Battling/Battle.cpp +++ b/CInterface/Battling/Battle.cpp @@ -21,7 +21,9 @@ export uint8_t CreatureLib_Battle_TrySetChoice(bool& out, Battle* p, BaseTurnCho export bool CreatureLib_Battle_CanFlee(const Battle* p) { return p->CanFlee(); } export uint8_t CreatureLib_Battle_CheckChoicesSetAndRun(Battle* p) { Try(p->CheckChoicesSetAndRun();) } -export ChoiceQueue* CreatureLib_Battle_GetCurrentTurnQueue(const Battle* p) { return p->GetCurrentTurnQueue(); } +export ChoiceQueue* CreatureLib_Battle_GetCurrentTurnQueue(const Battle* p) { + return p->GetCurrentTurnQueue().operator->(); +} export BattleRandom* CreatureLib_Battle_GetRandom(Battle* p) { return p->GetRandom(); } export uint8_t CreatureLib_Battle_CreatureInField(bool& out, const Battle* p, Creature* c) { Try(out = p->CreatureInField(c);) diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 6c51fd0..737e007 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -5,7 +5,7 @@ using namespace CreatureLib::Battling; -void TurnHandler::RunTurn(ChoiceQueue* queue) { +void TurnHandler::RunTurn(ArbUt::BorrowedPtr queue) { AssertNotNull(queue) for (auto choice : queue->GetInnerQueue()) { HOOK(OnBeforeTurn, choice, choice); diff --git a/src/Battling/Flow/TurnHandler.hpp b/src/Battling/Flow/TurnHandler.hpp index 08bd401..589d2a2 100644 --- a/src/Battling/Flow/TurnHandler.hpp +++ b/src/Battling/Flow/TurnHandler.hpp @@ -20,7 +20,7 @@ namespace CreatureLib::Battling { static void ExecuteFleeChoice(ArbUt::BorrowedPtr choice); public: - static void RunTurn(ChoiceQueue* queue); + static void RunTurn(ArbUt::BorrowedPtr queue); }; } diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index 4cdf6a6..03ff92a 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -62,15 +62,14 @@ void Battle::CheckChoicesSetAndRun() { } _currentTurn++; TurnOrdering::OrderChoices(choices, _random.GetRNG()); - this->_currentTurnQueue = new ChoiceQueue(choices); + this->_currentTurnQueue.reset(new ChoiceQueue(choices)); TurnHandler::RunTurn(this->_currentTurnQueue); if (this->_currentTurnQueue->HasCompletedQueue) { - delete this->_currentTurnQueue; this->_currentTurnQueue = nullptr; } } -ChoiceQueue* Battle::GetCurrentTurnQueue() const noexcept { return _currentTurnQueue; } +ArbUt::BorrowedPtr Battle::GetCurrentTurnQueue() const noexcept { return _currentTurnQueue; } BattleRandom* Battle::GetRandom() noexcept { return &_random; } diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index ae2045c..8014bcc 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -18,13 +18,13 @@ namespace CreatureLib::Battling { class Battle : public ScriptSource { protected: ArbUt::BorrowedPtr _library; - ArbUt::List> _parties; + ArbUt::UniquePtrList _parties; bool _canFlee; uint8_t _numberOfSides; uint8_t _creaturesPerSide; - ArbUt::List _sides; + ArbUt::UniquePtrList _sides; BattleRandom _random; - ChoiceQueue* _currentTurnQueue = nullptr; + std::unique_ptr _currentTurnQueue = nullptr; bool _hasEnded = false; BattleResult _battleResult = BattleResult::Empty(); EventHook _eventHook = EventHook(); @@ -35,26 +35,17 @@ namespace CreatureLib::Battling { public: Battle(const BattleLibrary* library, ArbUt::List parties, bool canFlee = true, uint8_t numberOfSides = 2, uint8_t creaturesPerSide = 1) - : _library(library), _parties(parties.Count()), _canFlee(canFlee), _numberOfSides(numberOfSides), - _creaturesPerSide(creaturesPerSide) { + : _library(library), _parties(parties.GetStdList()), _canFlee(canFlee), _numberOfSides(numberOfSides), + _creaturesPerSide(creaturesPerSide), _sides(numberOfSides) { AssertNotNull(_library); AssertAllNotNull(parties); - for (size_t i = 0; i < parties.Count(); i++) { - _parties.GetStdList().push_back(std::unique_ptr(parties[i])); - } - _sides = ArbUt::List(numberOfSides); for (size_t i = 0; i < numberOfSides; i++) { _sides.Append(new BattleSide(i, this, creaturesPerSide)); } } - virtual ~Battle() { - for (auto s : _sides) { - delete s; - } - delete _currentTurnQueue; - } + virtual ~Battle() = default; [[nodiscard]] const ArbUt::BorrowedPtr& GetLibrary() const noexcept; [[nodiscard]] uint32_t GetCurrentTurn() const noexcept { return _currentTurn; } @@ -66,7 +57,7 @@ namespace CreatureLib::Battling { void CheckChoicesSetAndRun(); - [[nodiscard]] ChoiceQueue* GetCurrentTurnQueue() const noexcept; + [[nodiscard]] ArbUt::BorrowedPtr GetCurrentTurnQueue() const noexcept; BattleRandom* GetRandom() noexcept; bool CreatureInField(const Creature* creature) const; @@ -87,8 +78,8 @@ namespace CreatureLib::Battling { inline bool HasEnded() const noexcept { return _hasEnded; } inline const BattleResult& GetResult() const noexcept { return _battleResult; } - const ArbUt::List>& GetParties() const noexcept { return _parties; } - const ArbUt::List& GetSides() const noexcept { return _sides; } + const ArbUt::UniquePtrList& GetParties() const noexcept { return _parties; } + const ArbUt::UniquePtrList& GetSides() const noexcept { return _sides; } Script* GetVolatileScript(const ConstString& key) const { return _volatile.Get(key); } Script* GetVolatileScript(uint32_t keyHash) const noexcept { return _volatile.Get(keyHash); } void AddVolatileScript(const ConstString& key);