Use smart pointers for BattleSide.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-06-02 13:06:24 +02:00
parent 49e8ff055d
commit f898698f49
20 changed files with 86 additions and 107 deletions

View File

@@ -5,8 +5,8 @@ bool CreatureLib::Battling::ChoiceQueue::MoveCreatureChoiceNext(CreatureLib::Bat
AssertNotNull(creature)
// Find which index the creature choice is at.
size_t choiceIndex = SIZE_MAX;
for (size_t index = _current; index < _queue.Count(); index++) {
if (_queue.At(index)->GetUser() == creature) {
for (size_t index = _current; index < _queue.size(); index++) {
if (_queue[index]->GetUser() == creature) {
// If the creature choice is already next up, return.
if (index == _current)
return false;
@@ -17,15 +17,14 @@ bool CreatureLib::Battling::ChoiceQueue::MoveCreatureChoiceNext(CreatureLib::Bat
if (choiceIndex == SIZE_MAX)
return false;
auto& vec = _queue.GetStdList();
// Save the pointer to the creature choice.
auto creatureChoice = _queue[choiceIndex];
// Starting at the position before the current creature choice, and iterating backwards up till the current choice.
for (size_t i = choiceIndex - 1; i >= _current && i != SIZE_MAX; i--) {
// Place everything in one spot later.
vec[i + 1] = vec[i];
_queue[i + 1] = _queue[i];
}
// Set the creature choice up next.
vec[_current] = creatureChoice.GetRaw();
_queue[_current] = creatureChoice;
return true;
}

View File

@@ -8,25 +8,21 @@
namespace CreatureLib::Battling {
class ChoiceQueue {
ArbUt::UniquePtrList<BaseTurnChoice> _queue;
std::vector<std::shared_ptr<BaseTurnChoice>> _queue;
size_t _current = 0;
public:
bool HasCompletedQueue = false;
explicit ChoiceQueue(std::vector<BaseTurnChoice*> queue) : _queue(std::move(queue)) {}
explicit ChoiceQueue(std::vector<std::shared_ptr<BaseTurnChoice>> queue) : _queue(std::move(queue)) {}
ArbUt::BorrowedPtr<BaseTurnChoice> Dequeue() {
auto b = _queue[_current];
_current++;
return b;
}
const std::shared_ptr<BaseTurnChoice>& Dequeue() { return _queue[_current++]; }
ArbUt::BorrowedPtr<BaseTurnChoice> Peek() { return _queue[_current]; }
const std::shared_ptr<BaseTurnChoice>& Peek() { return _queue[_current]; }
[[nodiscard]] bool HasNext() const { return _current < _queue.Count(); }
[[nodiscard]] bool HasNext() const { return _current < _queue.size(); }
ArbUt::UniquePtrList<BaseTurnChoice>& GetInnerQueue() { return _queue; }
const std::vector<std::shared_ptr<BaseTurnChoice>>& GetInnerQueue() { return _queue; }
bool MoveCreatureChoiceNext(Creature* creature);
};

View File

@@ -8,11 +8,11 @@ using namespace CreatureLib::Battling;
void TurnHandler::RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue) {
AssertNotNull(queue)
for (auto choice : queue->GetInnerQueue()) {
HOOK(OnBeforeTurn, choice, choice);
HOOK(OnBeforeTurn, choice, choice.get());
}
while (queue->HasNext()) {
auto item = queue->Dequeue();
ExecuteChoice(item);
ExecuteChoice(item.get());
}
queue->HasCompletedQueue = true;
}
@@ -59,7 +59,7 @@ void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr<AttackTurnChoice> choic
// FIXME: Resolve all targets
auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget());
ArbUt::List<Creature*> targets = {target};
ArbUt::List<ArbUt::BorrowedPtr<Creature>> targets = {target};
auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
choice->MarkScriptAsTaken();
@@ -91,7 +91,7 @@ void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr<AttackTurnChoice> choic
HOOK_LOCAL(OnBeforeAttack, attack, &attack);
for (auto& t : attack.GetTargets()) {
HandleAttackForTarget(&attack, t);
HandleAttackForTarget(&attack, t.GetRaw());
}
}

View File

@@ -9,14 +9,14 @@ using namespace Battling;
class ChoiceCompare {
public:
explicit ChoiceCompare() {}
bool operator()(const BaseTurnChoice* a, const BaseTurnChoice* b) {
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)->GetPriority();
auto bPriority = dynamic_cast<const AttackTurnChoice*>(b)->GetPriority();
auto aPriority = dynamic_cast<const AttackTurnChoice*>(a.get())->GetPriority();
auto bPriority = dynamic_cast<const AttackTurnChoice*>(b.get())->GetPriority();
if (aPriority != bPriority)
return aPriority > bPriority;
}
@@ -29,6 +29,6 @@ public:
}
};
void TurnOrdering::OrderChoices(std::vector<BaseTurnChoice*>& vec, ArbUt::Random& rand) {
void TurnOrdering::OrderChoices(std::vector<std::shared_ptr<BaseTurnChoice>>& vec, ArbUt::Random& rand) {
std::sort(vec.begin(), vec.end(), ChoiceCompare());
}

View File

@@ -2,13 +2,14 @@
#define CREATURELIB_TURNORDERING_HPP
#include <Arbutils/Random.hpp>
#include <memory>
#include <vector>
#include "../TurnChoices/BaseTurnChoice.hpp"
namespace CreatureLib::Battling {
class TurnOrdering {
public:
static void OrderChoices(std::vector<BaseTurnChoice*>& vec, ArbUt::Random& rand);
static void OrderChoices(std::vector<std::shared_ptr<BaseTurnChoice>>& vec, ArbUt::Random& rand);
};
}