CreatureLib/src/Battling/Models/BattleSide.hpp

96 lines
3.3 KiB
C++
Raw Normal View History

2019-10-17 12:33:25 +00:00
#ifndef CREATURELIB_BATTLESIDE_HPP
#define CREATURELIB_BATTLESIDE_HPP
#include "../TurnChoices/BaseTurnChoice.hpp"
#include "Creature.hpp"
2019-10-17 12:33:25 +00:00
namespace CreatureLib::Battling {
class BattleSide : public ScriptSource {
uint8_t _index;
uint8_t _creaturesPerSide;
ArbUt::List<ArbUt::OptionalBorrowedPtr<Creature>> _creatures;
2020-06-02 11:06:24 +00:00
ArbUt::List<std::shared_ptr<BaseTurnChoice>> _choices;
2020-05-26 16:31:06 +00:00
ArbUt::List<bool> _fillableSlots;
uint8_t _choicesSet = 0;
ScriptSet _volatile;
2020-06-02 11:06:24 +00:00
ArbUt::BorrowedPtr<Battle> _battle;
2019-12-15 10:52:10 +00:00
bool _hasFled = false;
public:
2020-06-02 11:06:24 +00:00
BattleSide(uint8_t index, ArbUt::BorrowedPtr<Battle> battle, uint8_t creaturesPerSide) noexcept
2020-03-22 12:42:26 +00:00
: _index(index), _creaturesPerSide(creaturesPerSide), _creatures(creaturesPerSide),
_choices(creaturesPerSide), _fillableSlots(creaturesPerSide), _battle(battle) {
for (size_t i = 0; i < creaturesPerSide; i++) {
2020-04-25 08:41:15 +00:00
_creatures.Append(nullptr);
_choices.Append(nullptr);
_fillableSlots.Append(true);
}
ResetChoices();
}
virtual ~BattleSide() = default;
2020-04-04 15:03:06 +00:00
[[nodiscard]] bool AllChoicesSet() const noexcept;
2020-06-02 11:06:24 +00:00
[[nodiscard]] const ArbUt::List<std::shared_ptr<BaseTurnChoice>>& GetChoices() const noexcept {
return _choices;
}
[[nodiscard]] bool AllPossibleSlotsFilled() const;
void SetChoice(BaseTurnChoice* choice);
2020-04-04 15:03:06 +00:00
void ResetChoices() noexcept;
void SetCreature(ArbUt::OptionalBorrowedPtr<Creature> creature, uint8_t index);
const ArbUt::OptionalBorrowedPtr<Creature>& GetCreature(uint8_t index) const;
2020-06-02 11:06:24 +00:00
bool CreatureOnSide(const ArbUt::BorrowedPtr<Creature>& creature) const;
2019-11-02 12:57:43 +00:00
size_t ScriptCount() const override;
2020-05-26 16:31:06 +00:00
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override;
const ArbUt::List<ArbUt::OptionalBorrowedPtr<Creature>>& GetCreatures() { return _creatures; }
2019-12-14 11:40:50 +00:00
2020-04-04 15:03:06 +00:00
uint8_t GetSideIndex() noexcept { return _index; }
2020-06-02 11:06:24 +00:00
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())
2019-12-07 21:52:43 +00:00
return i;
}
THROW("Unable to find creature on field.");
2019-12-07 21:52:43 +00:00
}
2020-06-02 11:06:24 +00:00
void MarkSlotAsUnfillable(const ArbUt::BorrowedPtr<Creature>& creature) noexcept {
2019-12-07 21:52:43 +00:00
for (uint8_t i = 0; i < _creaturesPerSide; i++) {
if (!_creatures[i].HasValue())
continue;
if (_creatures[i].GetValue() == creature) {
_fillableSlots.At(i) = false;
return;
}
}
}
2020-04-04 15:03:06 +00:00
bool IsDefeated() noexcept {
2019-12-07 21:52:43 +00:00
for (auto b : _fillableSlots) {
if (b)
return false;
}
return true;
}
2019-12-15 10:52:10 +00:00
2020-04-04 15:03:06 +00:00
bool HasFled() noexcept { return _hasFled; }
2019-12-15 10:52:10 +00:00
2020-04-04 15:03:06 +00:00
void MarkAsFled() noexcept { _hasFled = true; }
uint8_t GetRandomCreatureIndex();
bool SwapPositions(u8 a, u8 b);
BattleSide* CloneWithoutCreatures();
2019-10-17 12:33:25 +00:00
};
}
#endif // CREATURELIB_BATTLESIDE_HPP