diff --git a/CInterface/Battling/BattleSide.cpp b/CInterface/Battling/BattleSide.cpp index 6e297c6..063b565 100644 --- a/CInterface/Battling/BattleSide.cpp +++ b/CInterface/Battling/BattleSide.cpp @@ -37,4 +37,8 @@ export uint8_t CreatureLib_BattleSide_MarkSlotAsUnfillable(BattleSide* p, Creatu export bool CreatureLib_BattleSide_IsDefeated(BattleSide* p) { return p->IsDefeated(); } export bool CreatureLib_BattleSide_HasFled(BattleSide* p) { return p->HasFled(); } -export void CreatureLib_BattleSide_MarkAsFled(BattleSide* p) { p->MarkAsFled(); } \ No newline at end of file +export void CreatureLib_BattleSide_MarkAsFled(BattleSide* p) { p->MarkAsFled(); } + +export u8 CreatureLib_BattleSide_SwapPositions(u8& out, BattleSide* p, u8 a, u8 b) { + Try(out = p->SwapPositions(a, b);) +} \ No newline at end of file diff --git a/src/Battling/Models/BattleSide.cpp b/src/Battling/Models/BattleSide.cpp index 2846e45..92b4c1b 100644 --- a/src/Battling/Models/BattleSide.cpp +++ b/src/Battling/Models/BattleSide.cpp @@ -90,3 +90,35 @@ uint8_t BattleSide::GetRandomCreatureIndex() { // TODO: Consider adding parameter to only get index for available creatures. return _battle->GetRandom()->Get(_creaturesPerSide); } +bool BattleSide::SwapPositions(u8 a, u8 b) { + // If out of range, don't allow swapping. + if (a >= _creaturesPerSide || b >= _creaturesPerSide) { + return false; + } + + // If the two indices are the same, don't allow swapping. + if (a == b) { + return false; + } + + // Fetch parties for the two indices. + BattleParty* partyA = nullptr; + BattleParty* partyB = nullptr; + for (auto* party : this->_battle->GetParties()) { + if (party->IsResponsibleForIndex(_index, a)) { + partyA = party; + } + if (party->IsResponsibleForIndex(_index, b)) { + partyB = party; + } + } + // Don't allow swapping if different parties are responsible for the indices. + if (partyA != partyB) { + return false; + } + + auto creatureA = _creatures[a]; + _creatures[a] = _creatures[b]; + _creatures[b] = creatureA; + return true; +} diff --git a/src/Battling/Models/BattleSide.hpp b/src/Battling/Models/BattleSide.hpp index b148d58..0522dd6 100644 --- a/src/Battling/Models/BattleSide.hpp +++ b/src/Battling/Models/BattleSide.hpp @@ -85,6 +85,8 @@ namespace CreatureLib::Battling { void MarkAsFled() noexcept { _hasFled = true; } uint8_t GetRandomCreatureIndex(); + + bool SwapPositions(u8 a, u8 b); }; }