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

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2021-03-26 12:01:18 +01:00
parent cd3d665687
commit 9b7c271a20
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 39 additions and 1 deletions

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_IsDefeated(BattleSide* p) { return p->IsDefeated(); }
export bool CreatureLib_BattleSide_HasFled(BattleSide* p) { return p->HasFled(); } 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);)
}

View File

@ -90,3 +90,35 @@ uint8_t BattleSide::GetRandomCreatureIndex() {
// TODO: Consider adding parameter to only get index for available creatures. // TODO: Consider adding parameter to only get index for available creatures.
return _battle->GetRandom()->Get(_creaturesPerSide); 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;
}

View File

@ -85,6 +85,8 @@ namespace CreatureLib::Battling {
void MarkAsFled() noexcept { _hasFled = true; } void MarkAsFled() noexcept { _hasFled = true; }
uint8_t GetRandomCreatureIndex(); uint8_t GetRandomCreatureIndex();
bool SwapPositions(u8 a, u8 b);
}; };
} }