CreatureLib/src/Battling/Flow/TurnOrdering.cpp

44 lines
1.6 KiB
C++
Raw Normal View History

#include "TurnOrdering.hpp"
#include <algorithm>
#include "../Models/Battle.hpp"
#include "../TurnChoices/AttackTurnChoice.hpp"
2019-10-31 11:31:31 +00:00
using namespace CreatureLib;
using namespace Battling;
2020-04-22 19:20:07 +00:00
class ChoiceCompare {
public:
explicit ChoiceCompare() {}
2020-06-02 11:06:24 +00:00
bool operator()(const std::shared_ptr<BaseTurnChoice>& a, const std::shared_ptr<BaseTurnChoice>& b) {
2020-04-22 19:20:07 +00:00
auto aKind = a->GetKind();
auto bKind = b->GetKind();
if (aKind != bKind)
return aKind > bKind;
if (aKind == TurnChoiceKind::Attack) {
2020-06-02 11:06:24 +00:00
auto aPriority = dynamic_cast<const AttackTurnChoice*>(a.get())->GetPriority();
auto bPriority = dynamic_cast<const AttackTurnChoice*>(b.get())->GetPriority();
2020-04-22 19:20:07 +00:00
if (aPriority != bPriority)
return aPriority > bPriority;
}
auto aSpeed = a->GetUser()->GetBoostedStat(Library::Statistic::Speed);
auto bSpeed = b->GetUser()->GetBoostedStat(Library::Statistic::Speed);
if (aSpeed != bSpeed)
return aSpeed > bSpeed;
2020-04-22 21:09:54 +00:00
return a->__GetRandomValue() > b->__GetRandomValue();
2020-04-22 19:20:07 +00:00
}
};
2020-07-17 11:12:21 +00:00
void TurnOrdering::OrderChoices(std::vector<std::shared_ptr<BaseTurnChoice>>& vec,
[[maybe_unused]] ArbUt::Random& rand) {
for (auto item : vec) {
if (item->GetKind() == TurnChoiceKind::Attack) {
auto attackChoice = static_cast<AttackTurnChoice*>(item.get());
auto priority = attackChoice->GetPriority();
HOOK(ChangePriority, attackChoice, attackChoice, &priority);
attackChoice->SetPriority(priority);
}
}
std::sort(vec.begin(), vec.end(), ChoiceCompare());
}