CreatureLib/src/Battling/Flow/TurnOrdering.cpp

49 lines
1.8 KiB
C++

#include "TurnOrdering.hpp"
#include "../Models/Battle.hpp"
#include "../TurnChoices/AttackTurnChoice.hpp"
using namespace CreatureLib;
using namespace Battling;
class ChoiceCompare {
public:
bool operator()(const std::shared_ptr<BaseTurnChoice>& a, const std::shared_ptr<BaseTurnChoice>& b) {
auto aKind = a->GetKind();
auto bKind = b->GetKind();
if (aKind != bKind)
return aKind > bKind;
if (aKind == TurnChoiceKind::Attack) {
auto aAttack = std::dynamic_pointer_cast<AttackTurnChoice>(a);
auto bAttack = std::dynamic_pointer_cast<AttackTurnChoice>(b);
EnsureNotNull(aAttack);
EnsureNotNull(bAttack);
auto aPriority = aAttack->GetPriority();
auto bPriority = bAttack->GetPriority();
if (aPriority != bPriority)
return aPriority > bPriority;
}
auto aUser = a->GetUser();
auto bUser = b->GetUser();
auto aSpeed = aUser->GetBoostedStat(Library::Statistic::Speed);
auto bSpeed = bUser->GetBoostedStat(Library::Statistic::Speed);
if (aSpeed != bSpeed)
return aSpeed > bSpeed;
return a->__GetRandomValue() > b->__GetRandomValue();
}
};
void TurnOrdering::OrderChoices(std::vector<std::shared_ptr<BaseTurnChoice>>& vec) {
for (const auto& item : vec) {
EnsureNotNull(item);
if (item->GetKind() == TurnChoiceKind::Attack) {
auto attackChoice = std::dynamic_pointer_cast<AttackTurnChoice>(item);
EnsureNotNull(attackChoice);
auto priority = attackChoice->GetPriority();
HOOK(ChangePriority, attackChoice, attackChoice.get(), &priority);
attackChoice->SetPriority(priority);
}
}
std::sort(vec.begin(), vec.end(), ChoiceCompare());
}