From f898698f495175d4f0be3fbf7037611485d04fe4 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 2 Jun 2020 13:06:24 +0200 Subject: [PATCH] Use smart pointers for BattleSide. --- CInterface/Battling/Battle.cpp | 2 +- CInterface/Battling/BattleSide.cpp | 4 +- CInterface/Battling/Creature.cpp | 2 +- CInterface/Battling/ExecutingAttack.cpp | 5 ++- src/Battling/Flow/ChoiceQueue.cpp | 9 ++--- src/Battling/Flow/ChoiceQueue.hpp | 16 +++----- src/Battling/Flow/TurnHandler.cpp | 8 ++-- src/Battling/Flow/TurnOrdering.cpp | 8 ++-- src/Battling/Flow/TurnOrdering.hpp | 3 +- src/Battling/Models/Battle.cpp | 8 ++-- src/Battling/Models/Battle.hpp | 8 ++-- src/Battling/Models/BattleRandom.hpp | 2 +- src/Battling/Models/BattleSide.cpp | 10 ++--- src/Battling/Models/BattleSide.hpp | 24 ++++++------ src/Battling/Models/Creature.cpp | 4 +- src/Battling/Models/Creature.hpp | 6 +-- src/Battling/Models/ExecutingAttack.hpp | 6 +-- tests/BattleTests/ChoiceQueueTests.cpp | 10 ++--- tests/BattleTests/ChoiceSetTests.cpp | 6 --- tests/BattleTests/TurnOrderTests.cpp | 52 ++++++++++--------------- 20 files changed, 86 insertions(+), 107 deletions(-) diff --git a/CInterface/Battling/Battle.cpp b/CInterface/Battling/Battle.cpp index 390f74f..0a73868 100644 --- a/CInterface/Battling/Battle.cpp +++ b/CInterface/Battling/Battle.cpp @@ -29,7 +29,7 @@ export uint8_t CreatureLib_Battle_CreatureInField(bool& out, const Battle* p, Cr Try(out = p->CreatureInField(c);) } export uint8_t CreatureLib_Battle_GetCreature(Creature*& out, const Battle* p, uint8_t side, uint8_t target) { - Try(out = p->GetCreature(side, target);) + Try(out = p->GetCreature(side, target).operator->();) } export uint8_t CreatureLib_Battle_ForceRecall(Battle* p, uint8_t side, uint8_t target) { diff --git a/CInterface/Battling/BattleSide.cpp b/CInterface/Battling/BattleSide.cpp index 3084049..a8b85ea 100644 --- a/CInterface/Battling/BattleSide.cpp +++ b/CInterface/Battling/BattleSide.cpp @@ -10,8 +10,6 @@ export void CreatureLib_BattleSide_Destruct(BattleSide* p) { delete p; } export bool CreatureLib_BattleSide_AllChoicesSet(BattleSide* p) { return p->AllChoicesSet(); } -export BaseTurnChoice* const* CreatureLib_BattleSide_GetChoices(BattleSide* p) { return p->GetChoices().RawData(); } - export uint8_t CreatureLib_BattleSide_AllPossibleSlotsFilled(bool& out, BattleSide* p) { Try(out = p->AllPossibleSlotsFilled()); } @@ -25,7 +23,7 @@ export uint8_t CreatureLib_BattleSide_SetCreature(BattleSide* p, Creature* creat } export uint8_t CreatureLib_BattleSide_GetCreature(Creature*& out, BattleSide* p, uint8_t index) { - Try(out = p->GetCreature(index)); + Try(out = p->GetCreature(index).GetRaw()); } export uint8_t CreatureLib_BattleSide_GetSideIndex(BattleSide* p) { return p->GetSideIndex(); } diff --git a/CInterface/Battling/Creature.cpp b/CInterface/Battling/Creature.cpp index 750ea41..5699b1b 100644 --- a/CInterface/Battling/Creature.cpp +++ b/CInterface/Battling/Creature.cpp @@ -43,7 +43,7 @@ export void CreatureLib_Creature_SetHeldItemFromItem(Creature* p, const Creature return p->SetHeldItem(ArbUt::BorrowedPtr(item)); } SIMPLE_GET_FUNC(Creature, GetCurrentHealth, uint32_t); -SIMPLE_GET_FUNC(Creature, GetBattle, Battle*); +SIMPLE_GET_FUNC_SMART_PTR(Creature, GetBattle, Battle*); SIMPLE_GET_FUNC(Creature, GetBattleSide, BattleSide*); SIMPLE_GET_FUNC(Creature, IsOnBattleField, bool); export const char* CreatureLib_Creature_GetNickname(Creature* p) { return p->GetNickname().c_str(); } diff --git a/CInterface/Battling/ExecutingAttack.cpp b/CInterface/Battling/ExecutingAttack.cpp index 55d9299..86923c0 100644 --- a/CInterface/Battling/ExecutingAttack.cpp +++ b/CInterface/Battling/ExecutingAttack.cpp @@ -5,8 +5,9 @@ using namespace CreatureLib::Battling; export uint8_t CreatureLib_ExecutingAttack_Construct(ExecutingAttack*& out, Creature* const* targets, size_t targetCount, uint8_t numberHits, Creature* user, LearnedAttack* attack, Script* script) { - Try(auto ls = ArbUt::List(targets, targets + targetCount); - out = new ExecutingAttack(ls, numberHits, user, attack, script);) + Try(auto ls = ArbUt::List>(targetCount); for (size_t i = 0; i < targetCount; i++) { + ls.Append(targets[i]); + } out = new ExecutingAttack(ls, numberHits, user, attack, script);) } export void CreatureLib_ExecutingAttack_Destruct(ExecutingAttack* p) { delete p; } diff --git a/src/Battling/Flow/ChoiceQueue.cpp b/src/Battling/Flow/ChoiceQueue.cpp index 88c1e0a..43b04ac 100644 --- a/src/Battling/Flow/ChoiceQueue.cpp +++ b/src/Battling/Flow/ChoiceQueue.cpp @@ -5,8 +5,8 @@ bool CreatureLib::Battling::ChoiceQueue::MoveCreatureChoiceNext(CreatureLib::Bat AssertNotNull(creature) // Find which index the creature choice is at. size_t choiceIndex = SIZE_MAX; - for (size_t index = _current; index < _queue.Count(); index++) { - if (_queue.At(index)->GetUser() == creature) { + for (size_t index = _current; index < _queue.size(); index++) { + if (_queue[index]->GetUser() == creature) { // If the creature choice is already next up, return. if (index == _current) return false; @@ -17,15 +17,14 @@ bool CreatureLib::Battling::ChoiceQueue::MoveCreatureChoiceNext(CreatureLib::Bat if (choiceIndex == SIZE_MAX) return false; - auto& vec = _queue.GetStdList(); // Save the pointer to the creature choice. auto creatureChoice = _queue[choiceIndex]; // Starting at the position before the current creature choice, and iterating backwards up till the current choice. for (size_t i = choiceIndex - 1; i >= _current && i != SIZE_MAX; i--) { // Place everything in one spot later. - vec[i + 1] = vec[i]; + _queue[i + 1] = _queue[i]; } // Set the creature choice up next. - vec[_current] = creatureChoice.GetRaw(); + _queue[_current] = creatureChoice; return true; } diff --git a/src/Battling/Flow/ChoiceQueue.hpp b/src/Battling/Flow/ChoiceQueue.hpp index a65e074..f2a859c 100644 --- a/src/Battling/Flow/ChoiceQueue.hpp +++ b/src/Battling/Flow/ChoiceQueue.hpp @@ -8,25 +8,21 @@ namespace CreatureLib::Battling { class ChoiceQueue { - ArbUt::UniquePtrList _queue; + std::vector> _queue; size_t _current = 0; public: bool HasCompletedQueue = false; - explicit ChoiceQueue(std::vector queue) : _queue(std::move(queue)) {} + explicit ChoiceQueue(std::vector> queue) : _queue(std::move(queue)) {} - ArbUt::BorrowedPtr Dequeue() { - auto b = _queue[_current]; - _current++; - return b; - } + const std::shared_ptr& Dequeue() { return _queue[_current++]; } - ArbUt::BorrowedPtr Peek() { return _queue[_current]; } + const std::shared_ptr& Peek() { return _queue[_current]; } - [[nodiscard]] bool HasNext() const { return _current < _queue.Count(); } + [[nodiscard]] bool HasNext() const { return _current < _queue.size(); } - ArbUt::UniquePtrList& GetInnerQueue() { return _queue; } + const std::vector>& GetInnerQueue() { return _queue; } bool MoveCreatureChoiceNext(Creature* creature); }; diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 737e007..1a4ad74 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -8,11 +8,11 @@ using namespace CreatureLib::Battling; void TurnHandler::RunTurn(ArbUt::BorrowedPtr queue) { AssertNotNull(queue) for (auto choice : queue->GetInnerQueue()) { - HOOK(OnBeforeTurn, choice, choice); + HOOK(OnBeforeTurn, choice, choice.get()); } while (queue->HasNext()) { auto item = queue->Dequeue(); - ExecuteChoice(item); + ExecuteChoice(item.get()); } queue->HasCompletedQueue = true; } @@ -59,7 +59,7 @@ void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr choic // FIXME: Resolve all targets auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget()); - ArbUt::List targets = {target}; + ArbUt::List> targets = {target}; auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript()); choice->MarkScriptAsTaken(); @@ -91,7 +91,7 @@ void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr choic HOOK_LOCAL(OnBeforeAttack, attack, &attack); for (auto& t : attack.GetTargets()) { - HandleAttackForTarget(&attack, t); + HandleAttackForTarget(&attack, t.GetRaw()); } } diff --git a/src/Battling/Flow/TurnOrdering.cpp b/src/Battling/Flow/TurnOrdering.cpp index 81c05fc..c99d9f0 100644 --- a/src/Battling/Flow/TurnOrdering.cpp +++ b/src/Battling/Flow/TurnOrdering.cpp @@ -9,14 +9,14 @@ using namespace Battling; class ChoiceCompare { public: explicit ChoiceCompare() {} - bool operator()(const BaseTurnChoice* a, const BaseTurnChoice* b) { + bool operator()(const std::shared_ptr& a, const std::shared_ptr& b) { auto aKind = a->GetKind(); auto bKind = b->GetKind(); if (aKind != bKind) return aKind > bKind; if (aKind == TurnChoiceKind::Attack) { - auto aPriority = dynamic_cast(a)->GetPriority(); - auto bPriority = dynamic_cast(b)->GetPriority(); + auto aPriority = dynamic_cast(a.get())->GetPriority(); + auto bPriority = dynamic_cast(b.get())->GetPriority(); if (aPriority != bPriority) return aPriority > bPriority; } @@ -29,6 +29,6 @@ public: } }; -void TurnOrdering::OrderChoices(std::vector& vec, ArbUt::Random& rand) { +void TurnOrdering::OrderChoices(std::vector>& vec, ArbUt::Random& rand) { std::sort(vec.begin(), vec.end(), ChoiceCompare()); } diff --git a/src/Battling/Flow/TurnOrdering.hpp b/src/Battling/Flow/TurnOrdering.hpp index cad76dc..61dc8c5 100644 --- a/src/Battling/Flow/TurnOrdering.hpp +++ b/src/Battling/Flow/TurnOrdering.hpp @@ -2,13 +2,14 @@ #define CREATURELIB_TURNORDERING_HPP #include +#include #include #include "../TurnChoices/BaseTurnChoice.hpp" namespace CreatureLib::Battling { class TurnOrdering { public: - static void OrderChoices(std::vector& vec, ArbUt::Random& rand); + static void OrderChoices(std::vector>& vec, ArbUt::Random& rand); }; } diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index 03ff92a..bed0e85 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -39,13 +39,13 @@ void Battle::CheckChoicesSetAndRun() { } } - auto choices = std::vector(_numberOfSides * _creaturesPerSide); + auto choices = std::vector>(_numberOfSides * _creaturesPerSide); auto i = 0; for (auto side : _sides) { - for (BaseTurnChoice* choice : side->GetChoices()) { + for (const auto& choice : side->GetChoices()) { AssertNotNull(choice) if (choice->GetKind() == TurnChoiceKind::Attack) { - auto attack = (dynamic_cast((choice)))->GetAttack(); + auto attack = ((AttackTurnChoice*)choice.get())->GetAttack(); uint8_t uses = 1; // HOOK: change number of uses needed. if (attack->GetRemainingUses() < uses) { @@ -73,7 +73,7 @@ ArbUt::BorrowedPtr Battle::GetCurrentTurnQueue() const noexcept { r BattleRandom* Battle::GetRandom() noexcept { return &_random; } -bool Battle::CreatureInField(const Creature* creature) const { +bool Battle::CreatureInField(ArbUt::BorrowedPtr creature) const { AssertNotNull(creature) for (auto s : _sides) { if (s->CreatureOnSide(creature)) diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index 8014bcc..e6b77e1 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -60,12 +60,14 @@ namespace CreatureLib::Battling { [[nodiscard]] ArbUt::BorrowedPtr GetCurrentTurnQueue() const noexcept; BattleRandom* GetRandom() noexcept; - bool CreatureInField(const Creature* creature) const; + bool CreatureInField(ArbUt::BorrowedPtr creature) const; - Creature* GetCreature(const CreatureIndex& target) const { + const ArbUt::BorrowedPtr& GetCreature(const CreatureIndex& target) const { return _sides[target.GetSideIndex()]->GetCreature(target.GetCreatureIndex()); } - Creature* GetCreature(uint8_t side, uint8_t target) const { return _sides[side]->GetCreature(target); } + const ArbUt::BorrowedPtr& GetCreature(uint8_t side, uint8_t target) const { + return _sides[side]->GetCreature(target); + } void ForceRecall(uint8_t side, uint8_t index); void SwitchCreature(uint8_t side, uint8_t index, Creature* c); diff --git a/src/Battling/Models/BattleRandom.hpp b/src/Battling/Models/BattleRandom.hpp index fc7b76d..2c72d8c 100644 --- a/src/Battling/Models/BattleRandom.hpp +++ b/src/Battling/Models/BattleRandom.hpp @@ -13,7 +13,7 @@ namespace CreatureLib::Battling { public: BattleRandom() noexcept : _random() {} - BattleRandom(uint_fast32_t seed) noexcept : _random(seed) {} + explicit BattleRandom(uint_fast32_t seed) noexcept : _random(seed) {} bool EffectChance(float chance, ExecutingAttack* attack, Creature* target); int32_t Get() noexcept { return _random.Get(); } diff --git a/src/Battling/Models/BattleSide.cpp b/src/Battling/Models/BattleSide.cpp index bc73b99..392038e 100644 --- a/src/Battling/Models/BattleSide.cpp +++ b/src/Battling/Models/BattleSide.cpp @@ -26,15 +26,13 @@ void BattleSide::ResetChoices() noexcept { } } -const ArbUt::List& BattleSide::GetChoices() const noexcept { return _choices; } - void BattleSide::SetChoice(BaseTurnChoice* choice) { AssertNotNull(choice) auto find = std::find(_creatures.begin(), _creatures.end(), choice->GetUser()); if (find == _creatures.end()) throw CreatureException("User not found"); uint8_t index = std::distance(_creatures.begin(), find); - _choices[index] = choice; + _choices[index] = std::shared_ptr(choice); _choicesSet++; } @@ -56,18 +54,18 @@ void BattleSide::SetCreature(Creature* creature, uint8_t index) { for (auto c : side->GetCreatures()) { if (c != nullptr) { c->MarkOpponentAsSeen(creature); - creature->MarkOpponentAsSeen(c); + creature->MarkOpponentAsSeen(c.GetRaw()); } } } } -bool BattleSide::CreatureOnSide(const Creature* creature) const { +bool BattleSide::CreatureOnSide(const ArbUt::BorrowedPtr& creature) const { AssertNotNull(creature) return std::find(_creatures.begin(), _creatures.end(), creature) != _creatures.end(); } -Creature* BattleSide::GetCreature(uint8_t index) const { return _creatures[index]; } +const ArbUt::BorrowedPtr& BattleSide::GetCreature(uint8_t index) const { return _creatures[index]; } void BattleSide::GetActiveScripts(ArbUt::List& scripts) { scripts.Append(ScriptWrapper::FromSet(&_volatile)); diff --git a/src/Battling/Models/BattleSide.hpp b/src/Battling/Models/BattleSide.hpp index 74919f1..6553bdc 100644 --- a/src/Battling/Models/BattleSide.hpp +++ b/src/Battling/Models/BattleSide.hpp @@ -11,16 +11,16 @@ namespace CreatureLib::Battling { class BattleSide : public ScriptSource { uint8_t _index; uint8_t _creaturesPerSide; - ArbUt::List _creatures; - ArbUt::List _choices; + ArbUt::List> _creatures; + ArbUt::List> _choices; ArbUt::List _fillableSlots; uint8_t _choicesSet = 0; ScriptSet _volatile; - Battle* _battle; + ArbUt::BorrowedPtr _battle; bool _hasFled = false; public: - BattleSide(uint8_t index, Battle* battle, uint8_t creaturesPerSide) noexcept + BattleSide(uint8_t index, ArbUt::BorrowedPtr battle, uint8_t creaturesPerSide) noexcept : _index(index), _creaturesPerSide(creaturesPerSide), _creatures(creaturesPerSide), _choices(creaturesPerSide), _fillableSlots(creaturesPerSide), _battle(battle) { for (size_t i = 0; i < creaturesPerSide; i++) { @@ -34,7 +34,9 @@ namespace CreatureLib::Battling { virtual ~BattleSide() = default; [[nodiscard]] bool AllChoicesSet() const noexcept; - [[nodiscard]] const ArbUt::List& GetChoices() const noexcept; + [[nodiscard]] const ArbUt::List>& GetChoices() const noexcept { + return _choices; + } [[nodiscard]] bool AllPossibleSlotsFilled() const; @@ -43,24 +45,24 @@ namespace CreatureLib::Battling { void SetCreature(Creature* creature, uint8_t index); - Creature* GetCreature(uint8_t index) const; - bool CreatureOnSide(const Creature* creature) const; + const ArbUt::BorrowedPtr& GetCreature(uint8_t index) const; + bool CreatureOnSide(const ArbUt::BorrowedPtr& creature) const; size_t ScriptCount() const override; void GetActiveScripts(ArbUt::List& scripts) override; - const ArbUt::List& GetCreatures() { return _creatures; } + const ArbUt::List>& GetCreatures() { return _creatures; } uint8_t GetSideIndex() noexcept { return _index; } - uint8_t GetCreatureIndex(Creature* c) { + uint8_t GetCreatureIndex(const ArbUt::BorrowedPtr& c) { for (size_t i = 0; i < _creatures.Count(); i++) { - if (c == _creatures[i]) + if (_creatures[i] == c) return i; } throw CreatureException("Unable to find creature on field."); } - void MarkSlotAsUnfillable(Creature* creature) noexcept { + void MarkSlotAsUnfillable(const ArbUt::BorrowedPtr& creature) noexcept { for (uint8_t i = 0; i < _creaturesPerSide; i++) { if (_creatures[i] == creature) { _fillableSlots.At(i) = false; diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 2fede93..c63c0a8 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -45,7 +45,7 @@ const ConstString& Battling::Creature::GetActiveTalent() const { return _variant->GetTalent(_talentIndex); } -void Battling::Creature::SetBattleData(Battling::Battle* battle, Battling::BattleSide* side) { +void Battling::Creature::SetBattleData(ArbUt::BorrowedPtr battle, BattleSide* side) { _battle = battle; _side = side; this->ResetActiveScripts(); @@ -85,7 +85,7 @@ void Battling::Creature::RecalculateBoostedStat(Library::Statistic stat) { // endregion -Battling::Battle* Battling::Creature::GetBattle() const { return _battle; } +const ArbUt::BorrowedPtr& Battling::Creature::GetBattle() const { return _battle; } Battling::BattleSide* Battling::Creature::GetBattleSide() const { return _side; } diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index f9c472b..2498bc4 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -41,7 +41,7 @@ namespace CreatureLib::Battling { Library::StatisticSet _flatStats; Library::StatisticSet _boostedStats; - Battle* _battle = nullptr; + ArbUt::BorrowedPtr _battle = nullptr; BattleSide* _side = nullptr; bool _onBattleField = false; @@ -101,8 +101,8 @@ namespace CreatureLib::Battling { inline uint32_t GetCurrentHealth() const noexcept { return _currentHealth; } - void SetBattleData(Battle* battle, BattleSide* side); - Battle* GetBattle() const; + void SetBattleData(ArbUt::BorrowedPtr battle, BattleSide* side); + const ArbUt::BorrowedPtr& GetBattle() const; BattleSide* GetBattleSide() const; void SetOnBattleField(bool value) { _onBattleField = value; } bool IsOnBattleField() const { return _onBattleField; } diff --git a/src/Battling/Models/ExecutingAttack.hpp b/src/Battling/Models/ExecutingAttack.hpp index 557f8b3..b3b49e5 100644 --- a/src/Battling/Models/ExecutingAttack.hpp +++ b/src/Battling/Models/ExecutingAttack.hpp @@ -36,7 +36,7 @@ namespace CreatureLib::Battling { }; private: - ArbUt::List _targets; + ArbUt::List> _targets; uint8_t _numberHits; HitData* _hits; Creature* _user; @@ -44,7 +44,7 @@ namespace CreatureLib::Battling { Script* _script; public: - ExecutingAttack(const ArbUt::List& targets, uint8_t numberHits, Creature* user, + ExecutingAttack(const ArbUt::List>& targets, uint8_t numberHits, Creature* user, const ArbUt::BorrowedPtr& attack, Script* script) : _targets(targets.Count()), _numberHits(numberHits), _hits(new HitData[targets.Count() * numberHits]), _user(user), _attack(attack), _script(script) { @@ -82,7 +82,7 @@ namespace CreatureLib::Battling { } bool IsCreatureTarget(Creature* creature) noexcept { return _targets.IndexOf(creature) != (size_t)-1; } - const ArbUt::List& GetTargets() noexcept { return _targets; } + const ArbUt::List>& GetTargets() noexcept { return _targets; } uint8_t GetNumberOfHits() const noexcept { return _numberHits; } Creature* GetUser() noexcept { return _user; } diff --git a/tests/BattleTests/ChoiceQueueTests.cpp b/tests/BattleTests/ChoiceQueueTests.cpp index c5efe8d..653632d 100644 --- a/tests/BattleTests/ChoiceQueueTests.cpp +++ b/tests/BattleTests/ChoiceQueueTests.cpp @@ -16,11 +16,11 @@ TEST_CASE("Move creature choice up next.", "[Battling]") { auto c3 = CreateCreature(lib, "testSpecies1"_cnc, 1).Create(); auto c4 = CreateCreature(lib, "testSpecies1"_cnc, 1).Create(); - std::vector choices = { - new PassTurnChoice(c1), - new PassTurnChoice(c2), - new PassTurnChoice(c3), - new PassTurnChoice(c4), + std::vector> choices = { + std::make_shared(c1), + std::make_shared(c2), + std::make_shared(c3), + std::make_shared(c4), }; auto choiceQueue = ChoiceQueue(choices); diff --git a/tests/BattleTests/ChoiceSetTests.cpp b/tests/BattleTests/ChoiceSetTests.cpp index 79274bd..ef3bebc 100644 --- a/tests/BattleTests/ChoiceSetTests.cpp +++ b/tests/BattleTests/ChoiceSetTests.cpp @@ -14,7 +14,6 @@ TEST_CASE("Set Choice one-sized side", "[Battling]") { side.SetCreature(c, 0); auto choice = new PassTurnChoice(c); side.SetChoice(choice); - delete choice; delete c; } @@ -26,7 +25,6 @@ TEST_CASE("Set Choice one-sized side, validate all choices set", "[Battling]") { REQUIRE_FALSE(side.AllChoicesSet()); side.SetChoice(choice); REQUIRE(side.AllChoicesSet()); - delete choice; delete c; } @@ -40,8 +38,6 @@ TEST_CASE("Set Choice two-sized side", "[Battling]") { auto choice2 = new PassTurnChoice(c2); side.SetChoice(choice1); side.SetChoice(choice2); - delete choice1; - delete choice2; delete c1; delete c2; } @@ -59,8 +55,6 @@ TEST_CASE("Set Choice two-sized side, validate all choices set", "[Battling]") { REQUIRE_FALSE(side.AllChoicesSet()); side.SetChoice(choice2); REQUIRE(side.AllChoicesSet()); - delete choice1; - delete choice2; delete c1; delete c2; } diff --git a/tests/BattleTests/TurnOrderTests.cpp b/tests/BattleTests/TurnOrderTests.cpp index d3d765f..b8b956a 100644 --- a/tests/BattleTests/TurnOrderTests.cpp +++ b/tests/BattleTests/TurnOrderTests.cpp @@ -13,40 +13,35 @@ TEST_CASE("Turn ordering: Attack before pass", "[Battling]") { auto lib = TestLibrary::Get(); auto learnedAttack = LearnedAttack(lib->GetAttackLibrary()->Get("standard"_cnc.GetHash()), AttackLearnMethod::Unknown); - auto choice1 = new PassTurnChoice(nullptr); - auto choice2 = new AttackTurnChoice(nullptr, &learnedAttack, CreatureIndex(0, 0)); - auto vec = std::vector{choice1, choice2}; + auto choice1 = std::make_shared(nullptr); + auto choice2 = std::make_shared(nullptr, &learnedAttack, CreatureIndex(0, 0)); + auto vec = std::vector>{choice1, choice2}; auto rand = ArbUt::Random(); TurnOrdering::OrderChoices(vec, rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); - vec = std::vector{choice2, choice1}; + vec = std::vector>{choice2, choice1}; TurnOrdering::OrderChoices(vec, rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); - - delete choice1; - delete choice2; } TEST_CASE("Turn ordering: High priority goes before no priority", "[Battling]") { const auto& l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("standard"_cnc.GetHash()), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("highPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); - auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); - auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0)); - auto vec = std::vector{choice1, choice2}; + auto choice1 = std::make_shared(nullptr, a1, CreatureIndex(0, 0)); + auto choice2 = std::make_shared(nullptr, a2, CreatureIndex(0, 0)); + auto vec = std::vector>{choice1, choice2}; auto rand = ArbUt::Random(); TurnOrdering::OrderChoices(vec, rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); - vec = std::vector{choice2, choice1}; + vec = std::vector>{choice2, choice1}; TurnOrdering::OrderChoices(vec, rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); - delete choice1; - delete choice2; delete a1; delete a2; } @@ -55,20 +50,17 @@ TEST_CASE("Turn ordering: Higher priority goes before high priority", "[Battling const auto& l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("highPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); - auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); - auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0)); - auto vec = std::vector{choice1, choice2}; + auto choice1 = std::make_shared(nullptr, a1, CreatureIndex(0, 0)); + auto choice2 = std::make_shared(nullptr, a2, CreatureIndex(0, 0)); + auto vec = std::vector>{choice1, choice2}; auto rand = ArbUt::Random(); TurnOrdering::OrderChoices(vec, rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); - vec = std::vector{choice2, choice1}; + vec = std::vector>{choice2, choice1}; TurnOrdering::OrderChoices(vec, rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); - - delete choice1; - delete choice2; delete a1; delete a2; } @@ -77,20 +69,18 @@ TEST_CASE("Turn ordering: High priority goes before low priority", "[Battling]") const auto& l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); - auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); - auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0)); - auto vec = std::vector{choice1, choice2}; + auto choice1 = std::make_shared(nullptr, a1, CreatureIndex(0, 0)); + auto choice2 = std::make_shared(nullptr, a2, CreatureIndex(0, 0)); + auto vec = std::vector>{choice1, choice2}; auto rand = ArbUt::Random(); TurnOrdering::OrderChoices(vec, rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); - vec = std::vector{choice2, choice1}; + vec = std::vector>{choice2, choice1}; TurnOrdering::OrderChoices(vec, rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); - delete choice1; - delete choice2; delete a1; delete a2; } @@ -99,20 +89,18 @@ TEST_CASE("Turn ordering: No priority goes before low priority", "[Battling]") { const auto& l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc.GetHash()), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("standard"_cnc.GetHash()), AttackLearnMethod::Unknown); - auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); - auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0)); - auto vec = std::vector{choice1, choice2}; + auto choice1 = std::make_shared(nullptr, a1, CreatureIndex(0, 0)); + auto choice2 = std::make_shared(nullptr, a2, CreatureIndex(0, 0)); + auto vec = std::vector>{choice1, choice2}; auto rand = ArbUt::Random(); TurnOrdering::OrderChoices(vec, rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); - vec = std::vector{choice2, choice1}; + vec = std::vector>{choice2, choice1}; TurnOrdering::OrderChoices(vec, rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); - delete choice1; - delete choice2; delete a1; delete a2; }