More defensive programming.
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
2020-08-30 13:14:33 +02:00
parent 32f75f4a47
commit 3233daf9ab
5 changed files with 60 additions and 25 deletions

View File

@@ -8,20 +8,27 @@ using namespace Battling;
class ChoiceCompare {
public:
explicit ChoiceCompare() {}
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 aPriority = dynamic_cast<const AttackTurnChoice*>(a.get())->GetPriority();
auto bPriority = dynamic_cast<const AttackTurnChoice*>(b.get())->GetPriority();
auto aAttack = std::dynamic_pointer_cast<AttackTurnChoice>(a);
auto bAttack = std::dynamic_pointer_cast<AttackTurnChoice>(b);
AssertNotNull(aAttack);
AssertNotNull(bAttack);
auto aPriority = aAttack->GetPriority();
auto bPriority = bAttack->GetPriority();
if (aPriority != bPriority)
return aPriority > bPriority;
}
auto aSpeed = a->GetUser()->GetBoostedStat(Library::Statistic::Speed);
auto bSpeed = b->GetUser()->GetBoostedStat(Library::Statistic::Speed);
auto aUser = a->GetUser();
auto bUser = b->GetUser();
AssertNotNull(aUser);
AssertNotNull(bUser);
auto aSpeed = aUser->GetBoostedStat(Library::Statistic::Speed);
auto bSpeed = bUser->GetBoostedStat(Library::Statistic::Speed);
if (aSpeed != bSpeed)
return aSpeed > bSpeed;
@@ -29,13 +36,14 @@ public:
}
};
void TurnOrdering::OrderChoices(std::vector<std::shared_ptr<BaseTurnChoice>>& vec,
[[maybe_unused]] ArbUt::Random& rand) {
for (auto item : vec) {
void TurnOrdering::OrderChoices(std::vector<std::shared_ptr<BaseTurnChoice>>& vec) {
for (const auto& item : vec) {
AssertNotNull(item);
if (item->GetKind() == TurnChoiceKind::Attack) {
auto attackChoice = static_cast<AttackTurnChoice*>(item.get());
auto attackChoice = std::dynamic_pointer_cast<AttackTurnChoice>(item);
AssertNotNull(attackChoice);
auto priority = attackChoice->GetPriority();
HOOK(ChangePriority, attackChoice, attackChoice, &priority);
HOOK(ChangePriority, attackChoice, attackChoice.get(), &priority);
attackChoice->SetPriority(priority);
}
}

View File

@@ -9,7 +9,7 @@
namespace CreatureLib::Battling {
class TurnOrdering {
public:
static void OrderChoices(std::vector<std::shared_ptr<BaseTurnChoice>>& vec, ArbUt::Random& rand);
static void OrderChoices(std::vector<std::shared_ptr<BaseTurnChoice>>& vec);
};
}

View File

@@ -54,12 +54,12 @@ void Battle::CheckChoicesSetAndRun() {
for (auto choice : side->GetChoices()) {
AssertNotNull(choice)
if (choice->GetKind() == TurnChoiceKind::Attack) {
auto attack = ((AttackTurnChoice*)choice.get())->GetAttack();
auto attack = std::static_pointer_cast<AttackTurnChoice>(choice);
uint8_t uses = 1;
// HOOK: change number of uses needed.
if (attack->GetRemainingUses() < uses) {
if (attack->GetAttack()->GetRemainingUses() < uses) {
choice = std::shared_ptr<BaseTurnChoice>(_library->GetMiscLibrary()->ReplacementAttack(
choice->GetUser().GetRaw(), ((AttackTurnChoice*)choice.get())->GetTarget()));
choice->GetUser().GetRaw(), attack->GetTarget()));
}
// HOOK: Check if we need to change the move
}
@@ -74,7 +74,7 @@ void Battle::CheckChoicesSetAndRun() {
}
_currentTurn++;
try {
TurnOrdering::OrderChoices(choices, _random.GetRNG());
TurnOrdering::OrderChoices(choices);
} catch (const std::exception& e) {
THROW("Exception during turn ordering: '" << e.what() << "'.")
}