#include "BattleSide.hpp" #include #include "../../Library/Exceptions/CreatureException.hpp" #include "Battle.hpp" using namespace CreatureLib::Battling; bool BattleSide::AllChoicesSet() const noexcept { return _choicesSet == _creaturesPerSide; } bool BattleSide::AllPossibleSlotsFilled() const { AssertNotNull(_battle) try { for (size_t i = 0; i < _creatures.Count(); i++) { auto c = _creatures[i]; if (c == nullptr || c->IsFainted()) { if (_battle->CanSlotBeFilled(_index, i)) return false; } } } catch (const std::exception& e) { THROW_CREATURE("Exception during AllPossibleSlotsFilled check: '" << e.what() << "'."); } return true; } void BattleSide::ResetChoices() noexcept { _choicesSet = 0; for (uint8_t i = 0; i < _creaturesPerSide; i++) { _choices[i] = nullptr; } } void BattleSide::SetChoice(BaseTurnChoice* choice) { AssertNotNull(choice) try { for (size_t i = 0; i < _creatures.Count(); i++) { auto& c = _creatures[i]; if (c == choice->GetUser()) { _choices[i] = std::shared_ptr(choice); _choicesSet++; return; } } } catch (const std::exception& e) { THROW_CREATURE("Error during setting choice: '" << e.what() << "'."); } THROW_CREATURE("User not found"); } void BattleSide::SetCreature(ArbUt::BorrowedPtr creature, uint8_t index) { auto old = _creatures[index]; if (old != nullptr) { old->SetOnBattleField(false); } _creatures[index] = creature; if (creature != nullptr) { creature->SetBattleData(_battle, this); creature->SetOnBattleField(true); } if (_battle == nullptr) return; for (auto side : _battle->GetSides()) { if (side == this) continue; for (auto c : side->GetCreatures()) { if (c != nullptr) { c->MarkOpponentAsSeen(creature); creature->MarkOpponentAsSeen(c.GetRaw()); } } } } bool BattleSide::CreatureOnSide(const ArbUt::BorrowedPtr& creature) const { AssertNotNull(creature) return std::find(_creatures.begin(), _creatures.end(), creature) != _creatures.end(); } const ArbUt::BorrowedPtr& BattleSide::GetCreature(uint8_t index) const { return _creatures[index]; } void BattleSide::GetActiveScripts(ArbUt::List& scripts) { scripts.Append(ScriptWrapper::FromSet(&_volatile)); _battle->GetActiveScripts(scripts); } size_t BattleSide::ScriptCount() const { return _battle->ScriptCount() + 1; } uint8_t BattleSide::GetRandomCreatureIndex() { // TODO: Consider adding parameter to only get index for available creatures. AssertNotNull(_battle) return _battle->GetRandom()->Get(_creaturesPerSide); }