CreatureLib/src/Battling/Models/Battle.cpp

93 lines
3.0 KiB
C++

#include "Battle.hpp"
#include "../../Core/Exceptions/NotImplementedException.hpp"
#include "../Flow/TurnHandler.hpp"
#include "../Flow/TurnOrdering.hpp"
using namespace CreatureLib;
using namespace CreatureLib::Battling;
const BattleLibrary* Battle::GetLibrary() const { return _library; }
bool Battle::CanUse(const BaseTurnChoice* choice) {
if (choice->GetKind() == TurnChoiceKind::Attack) {
// HOOK: change number of uses needed.
return dynamic_cast<const AttackTurnChoice*>(choice)->GetAttack()->GetRemainingUses() > 1;
}
return true;
}
bool Battle::TrySetChoice(BaseTurnChoice* choice) {
if (!CanUse(choice))
return false;
choice->GetUser()->GetBattleSide()->SetChoice(choice);
CheckChoicesSetAndRun();
return true;
}
void Battle::CheckChoicesSetAndRun() {
for (auto side : _sides) {
if (!side->AllChoicesSet()) {
return;
}
}
auto choices = std::vector<BaseTurnChoice*>(_numberOfSides * _creaturesPerSide);
auto i = 0;
for (auto side : _sides) {
for (BaseTurnChoice* choice : side->GetChoices()) {
if (choice->GetKind() == TurnChoiceKind::Attack) {
auto attack = dynamic_cast<const AttackTurnChoice*>(choice)->GetAttack();
uint8_t uses = 1;
// HOOK: change number of uses needed.
if (attack->GetRemainingUses() < uses) {
// TODO: Implement default move
throw NotImplementedException("Not enough remaining uses, change to default move.");
}
// HOOK: Check if we need to change the move
}
choices[i] = choice;
i++;
}
side->ResetChoices();
}
TurnOrdering::OrderChoices(choices, _random);
this->_currentTurnQueue = new ChoiceQueue(choices);
TurnHandler::RunTurn(this, this->_currentTurnQueue);
if (this->_currentTurnQueue->HasCompletedQueue) {
delete this->_currentTurnQueue;
this->_currentTurnQueue = nullptr;
}
}
ChoiceQueue* Battle::GetCurrentTurnQueue() const { return _currentTurnQueue; }
Core::Random& Battle::GetRandom() { return _random; }
bool Battle::CreatureInField(const Creature* creature) const {
for (auto s : _sides) {
if (s->CreatureOnSide(creature))
return true;
}
return false;
}
bool Battle::HasRecalledSlots() const { return _numberOfRecalledSlots > 0; }
void Battle::ForceRecall(uint8_t side, uint8_t index) {
_numberOfRecalledSlots++;
// TODO: Switch out.
}
void Battle::FillRecall(uint8_t side, uint8_t, Creature* c) {
_numberOfRecalledSlots--;
// TODO: switch in.
if (_numberOfRecalledSlots == 0 && _currentTurnQueue != nullptr) {
TurnHandler::RunTurn(this, _currentTurnQueue);
}
}
void Battle::GetActiveScripts(std::vector<ScriptWrapper>& scripts) { scripts.emplace_back(&_volatile); }
void Battle::SwitchCreature(uint8_t sideIndex, uint8_t index, Creature* c) {
auto side = this->_sides[sideIndex];
side->SetCreature(c, index);
}