From 1807e720dd3fcbed384b99dc579f25a8badc4613 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 10 Aug 2020 17:37:30 +0200 Subject: [PATCH] Support for packing party. Signed-off-by: Deukhoofd --- CInterface/Battling/CreatureParty.cpp | 1 + src/Battling/Models/CreatureParty.hpp | 23 +++++++++--- tests/BattleTests/CreaturePartyTests.cpp | 46 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 tests/BattleTests/CreaturePartyTests.cpp diff --git a/CInterface/Battling/CreatureParty.cpp b/CInterface/Battling/CreatureParty.cpp index 5d000a7..cb92900 100644 --- a/CInterface/Battling/CreatureParty.cpp +++ b/CInterface/Battling/CreatureParty.cpp @@ -14,6 +14,7 @@ export uint8_t CreatureLib_CreatureParty_GetAtIndex(Creature*& out, const Creatu } export uint8_t CreatureLib_CreatureParty_Switch(CreatureParty* p, size_t a, size_t b) { Try(p->Switch(a, b);) } +export uint8_t CreatureLib_CreatureParty_PackParty(CreatureParty* p) { Try(p->PackParty();) } export Creature* CreatureLib_CreatureParty_SwapInto(CreatureParty* p, size_t index, Creature* creature) { return p->SwapInto(index, creature); diff --git a/src/Battling/Models/CreatureParty.hpp b/src/Battling/Models/CreatureParty.hpp index fe1b744..8e2aca5 100644 --- a/src/Battling/Models/CreatureParty.hpp +++ b/src/Battling/Models/CreatureParty.hpp @@ -21,11 +21,7 @@ namespace CreatureLib::Battling { ArbUt::BorrowedPtr GetAtIndex(size_t index) const noexcept { return _party[index]; } - void Switch(size_t a, size_t b) noexcept { - auto ca = _party[a]; - _party[a] = _party[b]; - _party[b] = ca; - } + void Switch(size_t a, size_t b) noexcept { _party.Swap(a, b); } Creature* SwapInto(size_t index, Creature* creature) { auto p = _party.TakeOwnership(index); @@ -48,6 +44,23 @@ namespace CreatureLib::Battling { const ArbUt::UniquePtrList& GetParty() const noexcept { return _party; } size_t GetLength() const noexcept { return _party.Count(); } + + void PackParty() { + int32_t firstNil = -1; + for (size_t i = 0; i < _party.Count(); i++) { + if (_party[i] == nullptr) { + if (firstNil == -1) { + firstNil = i; + } + } else { + if (firstNil != -1) { + _party.Swap(firstNil, i); + i = firstNil; + firstNil = -1; + } + } + } + } }; } diff --git a/tests/BattleTests/CreaturePartyTests.cpp b/tests/BattleTests/CreaturePartyTests.cpp new file mode 100644 index 0000000..2e86839 --- /dev/null +++ b/tests/BattleTests/CreaturePartyTests.cpp @@ -0,0 +1,46 @@ +#ifdef TESTS_BUILD +#include "../../extern/catch.hpp" +#include "../../src/Battling/Models/CreateCreature.hpp" +#include "../../src/Battling/Models/CreatureParty.hpp" +#include "../TestLibrary/TestLibrary.hpp" + +using namespace CreatureLib::Battling; + +TEST_CASE("Pack party tests with single creature", "[Library]") { + auto library = TestLibrary::Get(); + auto creature = CreateCreature(library, "testSpecies1"_cnc, 1).Create(); + auto party = CreatureParty(6); + party.SwapInto(1, creature); + REQUIRE(party.GetAtIndex(1) == creature); + party.PackParty(); + REQUIRE(party.GetAtIndex(0) == creature); + REQUIRE(party.GetAtIndex(1) == nullptr); + + party.Switch(0, 5); + REQUIRE(party.GetAtIndex(0) == nullptr); + REQUIRE(party.GetAtIndex(5) == creature); + party.PackParty(); + REQUIRE(party.GetAtIndex(0) == creature); + REQUIRE(party.GetAtIndex(1) == nullptr); + REQUIRE(party.GetAtIndex(2) == nullptr); + REQUIRE(party.GetAtIndex(3) == nullptr); + REQUIRE(party.GetAtIndex(4) == nullptr); + REQUIRE(party.GetAtIndex(5) == nullptr); +} + +TEST_CASE("Pack party tests with two creatures", "[Library]") { + auto library = TestLibrary::Get(); + auto creature1 = CreateCreature(library, "testSpecies1"_cnc, 1).Create(); + auto creature2 = CreateCreature(library, "testSpecies1"_cnc, 1).Create(); + auto party = CreatureParty(6); + party.SwapInto(1, creature1); + party.SwapInto(5, creature2); + + REQUIRE(party.GetAtIndex(1) == creature1); + REQUIRE(party.GetAtIndex(5) == creature2); + party.PackParty(); + REQUIRE(party.GetAtIndex(0) == creature1); + REQUIRE(party.GetAtIndex(1) == creature2); +} + +#endif \ No newline at end of file