Support for packing party.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-08-10 17:37:30 +02:00
parent 3a75a40671
commit 1807e720dd
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 65 additions and 5 deletions

View File

@ -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);

View File

@ -21,11 +21,7 @@ namespace CreatureLib::Battling {
ArbUt::BorrowedPtr<Creature> 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<Creature>& 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;
}
}
}
}
};
}

View File

@ -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