Support for swapping two creatures on a side.
continuous-integration/drone/push Build is passing

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd committed 2021-03-26 12:01:18 +01:00
1 parent cd3d665687
commit 9b7c271a20
3 files changed
+39 -1

No files matched your search

+5 -1
View File
@@ -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(); }
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);)
}
+32
View File
@@ -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;
}
+2
View File
@@ -85,6 +85,8 @@ namespace CreatureLib::Battling {
void MarkAsFled() noexcept { _hasFled = true; }
uint8_t GetRandomCreatureIndex();
bool SwapPositions(u8 a, u8 b);
};
}