CreatureLib/src/Battling/Models/BattleSide.cpp

92 lines
2.9 KiB
C++
Raw Normal View History

2019-10-17 12:33:25 +00:00
#include "BattleSide.hpp"
#include <algorithm>
#include "../../Library/Exceptions/CreatureException.hpp"
#include "Battle.hpp"
using namespace CreatureLib::Battling;
2020-04-04 15:03:06 +00:00
bool BattleSide::AllChoicesSet() const noexcept { return _choicesSet == _creaturesPerSide; }
bool BattleSide::AllPossibleSlotsFilled() const {
2020-03-22 12:42:26 +00:00
AssertNotNull(_battle)
2020-07-28 15:22:43 +00:00
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;
}
}
2020-07-28 15:22:43 +00:00
} catch (const std::exception& e) {
THROW_CREATURE("Exception during AllPossibleSlotsFilled check: '" << e.what() << "'.");
}
return true;
}
2020-04-04 15:03:06 +00:00
void BattleSide::ResetChoices() noexcept {
_choicesSet = 0;
for (uint8_t i = 0; i < _creaturesPerSide; i++) {
_choices[i] = nullptr;
}
}
void BattleSide::SetChoice(BaseTurnChoice* choice) {
2020-03-22 12:42:26 +00:00
AssertNotNull(choice)
2020-07-28 15:22:43 +00:00
try {
for (size_t i = 0; i < _creatures.Count(); i++) {
auto& c = _creatures[i];
if (c == choice->GetUser()) {
_choices[i] = std::shared_ptr<BaseTurnChoice>(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> creature, uint8_t index) {
auto old = _creatures[index];
2019-12-14 11:40:50 +00:00
if (old != nullptr) {
old->SetOnBattleField(false);
}
_creatures[index] = creature;
if (creature != nullptr) {
creature->SetBattleData(_battle, this);
creature->SetOnBattleField(true);
}
2019-12-14 11:40:50 +00:00
if (_battle == nullptr)
return;
for (auto side : _battle->GetSides()) {
if (side == this)
continue;
for (auto c : side->GetCreatures()) {
if (c != nullptr) {
c->MarkOpponentAsSeen(creature);
2020-06-02 11:06:24 +00:00
creature->MarkOpponentAsSeen(c.GetRaw());
2019-12-14 11:40:50 +00:00
}
}
}
}
2019-11-02 12:57:43 +00:00
2020-06-02 11:06:24 +00:00
bool BattleSide::CreatureOnSide(const ArbUt::BorrowedPtr<Creature>& creature) const {
2020-03-22 12:42:26 +00:00
AssertNotNull(creature)
2019-11-02 12:57:43 +00:00
return std::find(_creatures.begin(), _creatures.end(), creature) != _creatures.end();
}
2020-06-02 11:06:24 +00:00
const ArbUt::BorrowedPtr<Creature>& BattleSide::GetCreature(uint8_t index) const { return _creatures[index]; }
2020-05-26 16:31:06 +00:00
void BattleSide::GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) {
2020-04-23 21:23:58 +00:00
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.
2020-03-22 12:42:26 +00:00
AssertNotNull(_battle)
return _battle->GetRandom()->Get(_creaturesPerSide);
}