2019-10-17 12:33:25 +00:00
|
|
|
#include "BattleSide.hpp"
|
2019-10-29 10:19:25 +00:00
|
|
|
#include <algorithm>
|
2020-03-22 09:11:53 +00:00
|
|
|
#include "../../Library/Exceptions/CreatureException.hpp"
|
2019-11-10 13:32:05 +00:00
|
|
|
#include "Battle.hpp"
|
2019-10-29 10:19:25 +00:00
|
|
|
|
|
|
|
using namespace CreatureLib::Battling;
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
bool BattleSide::AllChoicesSet() const { return _choicesSet == _creaturesPerSide; }
|
2019-10-29 10:19:25 +00:00
|
|
|
|
2019-12-07 11:13:12 +00:00
|
|
|
bool BattleSide::AllPossibleSlotsFilled() const {
|
2020-03-22 12:42:26 +00:00
|
|
|
AssertNotNull(_battle)
|
2020-03-22 18:21:40 +00:00
|
|
|
for (size_t i = 0; i < _creatures.Count(); i++) {
|
2019-12-07 11:13:12 +00:00
|
|
|
auto c = _creatures[i];
|
|
|
|
if (c == nullptr || c->IsFainted()) {
|
|
|
|
if (_battle->CanSlotBeFilled(_index, i))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:19:25 +00:00
|
|
|
void BattleSide::ResetChoices() {
|
2019-11-06 17:08:21 +00:00
|
|
|
_choicesSet = 0;
|
2019-11-28 11:55:22 +00:00
|
|
|
for (uint8_t i = 0; i < _creaturesPerSide; i++) {
|
2019-10-29 10:19:25 +00:00
|
|
|
_choices[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:21:40 +00:00
|
|
|
const List<BaseTurnChoice*>& BattleSide::GetChoices() const { return _choices; }
|
2019-10-29 10:19:25 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
void BattleSide::SetChoice(BaseTurnChoice* choice) {
|
2020-03-22 12:42:26 +00:00
|
|
|
AssertNotNull(choice)
|
2019-10-29 10:19:25 +00:00
|
|
|
auto find = std::find(_creatures.begin(), _creatures.end(), choice->GetUser());
|
2019-11-28 11:55:22 +00:00
|
|
|
if (find == _creatures.end())
|
2019-10-31 12:26:56 +00:00
|
|
|
throw CreatureException("User not found");
|
2019-11-28 11:55:22 +00:00
|
|
|
uint8_t index = std::distance(_creatures.begin(), find);
|
2019-10-29 10:19:25 +00:00
|
|
|
_choices[index] = choice;
|
2019-11-06 17:08:21 +00:00
|
|
|
_choicesSet++;
|
2019-10-29 10:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 11:56:41 +00:00
|
|
|
void BattleSide::SetCreature(Creature* creature, uint8_t index) {
|
2019-12-07 20:56:29 +00:00
|
|
|
auto old = _creatures[index];
|
2019-12-14 11:40:50 +00:00
|
|
|
if (old != nullptr) {
|
2019-12-07 20:56:29 +00:00
|
|
|
old->SetOnBattleField(false);
|
|
|
|
}
|
2019-12-05 11:56:41 +00:00
|
|
|
_creatures[index] = creature;
|
2020-03-25 18:07:36 +00:00
|
|
|
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);
|
|
|
|
creature->MarkOpponentAsSeen(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 11:56:41 +00:00
|
|
|
}
|
2019-11-02 12:57:43 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
bool BattleSide::CreatureOnSide(const 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();
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
Creature* BattleSide::GetCreature(uint8_t index) const { return _creatures[index]; }
|
2019-11-10 13:32:05 +00:00
|
|
|
|
2020-03-22 18:21:40 +00:00
|
|
|
void BattleSide::GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) {
|
|
|
|
scripts.Append(&_volatile);
|
2019-11-10 16:08:42 +00:00
|
|
|
_battle->GetActiveScripts(scripts);
|
2019-11-10 13:32:05 +00:00
|
|
|
}
|
2020-03-22 09:30:45 +00:00
|
|
|
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)
|
2020-03-22 09:30:45 +00:00
|
|
|
return _battle->GetRandom()->Get(_creaturesPerSide);
|
|
|
|
}
|