From 1d18066bccdc2566de59384920677f78120b4a4d Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 7 Mar 2020 11:00:48 +0100 Subject: [PATCH] Made Battle take battleparties as a pointer instead of value for external use. --- src/Battling/Models/Battle.cpp | 4 +-- src/Battling/Models/Battle.hpp | 7 +++-- src/Battling/Models/BattleParty.hpp | 10 +++---- tests/Integration/BattleIntegrations.cpp | 36 ++++++++++++------------ 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index 197d4f8..869de53 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -90,8 +90,8 @@ void Battle::SwitchCreature(uint8_t sideIndex, uint8_t index, Creature* c) { bool Battle::CanSlotBeFilled(uint8_t side, uint8_t index) const { for (const auto& party : _parties) { - if (party.IsResponsibleForIndex(side, index)) { - if (party.HasCreaturesNotInField()) + if (party->IsResponsibleForIndex(side, index)) { + if (party->HasCreaturesNotInField()) return true; } } diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index 6d4d9f9..09bedbb 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -15,7 +15,7 @@ namespace CreatureLib::Battling { class Battle : public ScriptSource { const BattleLibrary* _library; - std::vector _parties; + std::vector _parties; bool _canFlee; uint8_t _numberOfSides; uint8_t _creaturesPerSide; @@ -29,7 +29,7 @@ namespace CreatureLib::Battling { ScriptSet _volatile; public: - Battle(const BattleLibrary* library, std::vector parties, bool canFlee = true, + Battle(const BattleLibrary* library, std::vector parties, bool canFlee = true, uint8_t numberOfSides = 2, uint8_t creaturesPerSide = 1) : _library(library), _parties(parties), _canFlee(canFlee), _numberOfSides(numberOfSides), _creaturesPerSide(creaturesPerSide) { @@ -43,6 +43,9 @@ namespace CreatureLib::Battling { for (auto s : _sides) { delete s; } + for (auto party : _parties) { + delete party; + } delete _currentTurnQueue; } diff --git a/src/Battling/Models/BattleParty.hpp b/src/Battling/Models/BattleParty.hpp index cf31a85..143e12e 100644 --- a/src/Battling/Models/BattleParty.hpp +++ b/src/Battling/Models/BattleParty.hpp @@ -13,10 +13,10 @@ namespace CreatureLib::Battling { BattleParty(CreatureParty* party, std::vector responsibleIndices) : _party(party), _responsibleIndices(responsibleIndices) {} - CreatureParty* GetParty() { return _party; } - const std::vector& GetResponsibleIndices() const { return _responsibleIndices; } + inline CreatureParty* GetParty() const { return _party; } + inline const std::vector& GetResponsibleIndices() const { return _responsibleIndices; } - bool IsResponsibleForIndex(const CreatureIndex& index) const { + inline bool IsResponsibleForIndex(const CreatureIndex& index) const { for (const auto& i : _responsibleIndices) { if (i == index) return true; @@ -24,7 +24,7 @@ namespace CreatureLib::Battling { return false; } - bool IsResponsibleForIndex(uint8_t side, uint8_t index) const { + inline bool IsResponsibleForIndex(uint8_t side, uint8_t index) const { for (const auto& i : _responsibleIndices) { if (i.GetSideIndex() == side && i.GetCreatureIndex() == index) return true; @@ -32,7 +32,7 @@ namespace CreatureLib::Battling { return false; } - bool HasCreaturesNotInField() const { + inline bool HasCreaturesNotInField() const { auto p = _party->GetParty(); for (const auto& creature : p) { if (creature == nullptr) diff --git a/tests/Integration/BattleIntegrations.cpp b/tests/Integration/BattleIntegrations.cpp index 906495e..6d5c2b9 100644 --- a/tests/Integration/BattleIntegrations.cpp +++ b/tests/Integration/BattleIntegrations.cpp @@ -25,11 +25,11 @@ TEST_CASE("Create Battle", "[Integrations]") { auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1}; - auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); + auto battleParty1 = new BattleParty(&party1, {CreatureIndex(0, 0)}); auto c2 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party2{c2}; - auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)}); + auto battleParty2 = new BattleParty(&party2, {CreatureIndex(1, 0)}); auto battle = Battle(library, {battleParty1, battleParty2}); } @@ -39,11 +39,11 @@ TEST_CASE("Use damaging move", "[Integrations]") { auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1}; - auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); + auto battleParty1 = new BattleParty(&party1, {CreatureIndex(0, 0)}); auto c2 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party2{c2}; - auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)}); + auto battleParty2 = new BattleParty(&party2, {CreatureIndex(1, 0)}); auto battle = Battle(library, {battleParty1, battleParty2}); @@ -61,11 +61,11 @@ TEST_CASE("Finish battle when all battle of one side have fainted", "[Integratio auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1}; - auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); + auto battleParty1 = new BattleParty(&party1, {CreatureIndex(0, 0)}); auto c2 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party2{c2}; - auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)}); + auto battleParty2 = new BattleParty(&party2, {CreatureIndex(1, 0)}); auto battle = Battle(library, {battleParty1, battleParty2}); @@ -96,11 +96,11 @@ TEST_CASE("When creature is dealt enough damage, faint it and mark battle as end auto c1 = CreateCreature(library, "testSpecies1", 100).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1}; - auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); + auto battleParty1 = new BattleParty(&party1, {CreatureIndex(0, 0)}); auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party2{c2}; - auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)}); + auto battleParty2 = new BattleParty(&party2, {CreatureIndex(1, 0)}); auto battle = Battle(library, {battleParty1, battleParty2}); @@ -125,13 +125,13 @@ TEST_CASE("When another creature is available on faint, make sure the battle has auto c1 = CreateCreature(library, "testSpecies1", 100).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1}; - auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); + auto battleParty1 = new BattleParty(&party1, {CreatureIndex(0, 0)}); auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); auto c3 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party2{c2, c3}; - auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)}); + auto battleParty2 = new BattleParty(&party2, {CreatureIndex(1, 0)}); auto battle = Battle(library, {battleParty1, battleParty2}); @@ -165,11 +165,11 @@ TEST_CASE("Switch Creature in", "[Integrations]") { auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1, c2}; - auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); + auto battleParty1 = new BattleParty(&party1, {CreatureIndex(0, 0)}); auto c3 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party2{c3}; - auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)}); + auto battleParty2 = new BattleParty(&party2, {CreatureIndex(1, 0)}); auto battle = Battle(library, {battleParty1, battleParty2}); @@ -191,11 +191,11 @@ TEST_CASE("Switch Creature in, but have attack aimed at it. Attack should hit ne auto c2 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1, c2}; - auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); + auto battleParty1 = new BattleParty(&party1, {CreatureIndex(0, 0)}); auto c3 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party2{c3}; - auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)}); + auto battleParty2 = new BattleParty(&party2, {CreatureIndex(1, 0)}); auto battle = Battle(library, {battleParty1, battleParty2}); @@ -216,11 +216,11 @@ TEST_CASE("Switch Creature in, mark as seen opponent for opponent", "[Integratio auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1, c2}; - auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); + auto battleParty1 = new BattleParty(&party1, {CreatureIndex(0, 0)}); auto c3 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party2{c3}; - auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)}); + auto battleParty2 = new BattleParty(&party2, {CreatureIndex(1, 0)}); auto battle = Battle(library, {battleParty1, battleParty2}); @@ -253,11 +253,11 @@ TEST_CASE("Flee Battle", "[Integrations]") { auto c1 = CreateCreature(library, "testSpecies1", 100).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party1{c1}; - auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)}); + auto battleParty1 = new BattleParty(&party1, {CreatureIndex(0, 0)}); auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create(); CreatureParty party2{c2}; - auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)}); + auto battleParty2 = new BattleParty(&party2, {CreatureIndex(1, 0)}); auto battle = Battle(library, {battleParty1, battleParty2});