CreatureLib/src/Battling/Models/BattleSide.hpp

96 lines
3.3 KiB
C++

#ifndef CREATURELIB_BATTLESIDE_HPP
#define CREATURELIB_BATTLESIDE_HPP
#include "../TurnChoices/BaseTurnChoice.hpp"
#include "Creature.hpp"
namespace CreatureLib::Battling {
class BattleSide : public ScriptSource {
uint8_t _index;
uint8_t _creaturesPerSide;
ArbUt::List<ArbUt::OptionalBorrowedPtr<Creature>> _creatures;
ArbUt::List<std::shared_ptr<BaseTurnChoice>> _choices;
ArbUt::List<bool> _fillableSlots;
uint8_t _choicesSet = 0;
ScriptSet _volatile;
ArbUt::BorrowedPtr<Battle> _battle;
bool _hasFled = false;
public:
BattleSide(uint8_t index, ArbUt::BorrowedPtr<Battle> battle, uint8_t creaturesPerSide) noexcept
: _index(index), _creaturesPerSide(creaturesPerSide), _creatures(creaturesPerSide),
_choices(creaturesPerSide), _fillableSlots(creaturesPerSide), _battle(battle) {
for (size_t i = 0; i < creaturesPerSide; i++) {
_creatures.Append(nullptr);
_choices.Append(nullptr);
_fillableSlots.Append(true);
}
ResetChoices();
}
virtual ~BattleSide() = default;
[[nodiscard]] bool AllChoicesSet() const noexcept;
[[nodiscard]] const ArbUt::List<std::shared_ptr<BaseTurnChoice>>& GetChoices() const noexcept {
return _choices;
}
[[nodiscard]] bool AllPossibleSlotsFilled() const;
void SetChoice(BaseTurnChoice* choice);
void ResetChoices() noexcept;
void SetCreature(ArbUt::OptionalBorrowedPtr<Creature> creature, uint8_t index);
const ArbUt::OptionalBorrowedPtr<Creature>& GetCreature(uint8_t index) const;
bool CreatureOnSide(const ArbUt::BorrowedPtr<Creature>& creature) const;
size_t ScriptCount() const override;
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override;
const ArbUt::List<ArbUt::OptionalBorrowedPtr<Creature>>& GetCreatures() { return _creatures; }
uint8_t GetSideIndex() noexcept { return _index; }
uint8_t GetCreatureIndex(const ArbUt::BorrowedPtr<Creature>& c) {
for (size_t i = 0; i < _creatures.Count(); i++) {
if (!_creatures[i].HasValue())
continue;
if (_creatures[i].GetValue() == c.GetRaw())
return i;
}
THROW("Unable to find creature on field.");
}
void MarkSlotAsUnfillable(const ArbUt::BorrowedPtr<Creature>& creature) noexcept {
for (uint8_t i = 0; i < _creaturesPerSide; i++) {
if (!_creatures[i].HasValue())
continue;
if (_creatures[i].GetValue() == creature) {
_fillableSlots.At(i) = false;
return;
}
}
}
bool IsDefeated() noexcept {
for (auto b : _fillableSlots) {
if (b)
return false;
}
return true;
}
bool HasFled() noexcept { return _hasFled; }
void MarkAsFled() noexcept { _hasFled = true; }
uint8_t GetRandomCreatureIndex();
bool SwapPositions(u8 a, u8 b);
BattleSide* CloneWithoutCreatures();
};
}
#endif // CREATURELIB_BATTLESIDE_HPP