diff --git a/src/Battling/Flow/TurnOrdering.cpp b/src/Battling/Flow/TurnOrdering.cpp index be77e70..e01ab5d 100644 --- a/src/Battling/Flow/TurnOrdering.cpp +++ b/src/Battling/Flow/TurnOrdering.cpp @@ -1,18 +1,36 @@ #include "TurnOrdering.hpp" +#include "../TurnChoices/AttackTurnChoice.hpp" +#include "../Models/Creature.hpp" +#include "../Models/Battle.hpp" #include -using namespace CreatureLib::Battling; +using namespace CreatureLib; +using namespace Battling; -bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b){ +bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b, Core::Random& rand){ auto aKind = a->GetKind(); auto bKind = b->GetKind(); if (aKind != bKind) return aKind > bKind; - //FIXME: choices need to be ordered when they're the same type - throw 1; + if (aKind == TurnChoiceKind::Attack){ + auto aPriority = static_cast(a)->GetPriority(); + auto bPriority = static_cast(b)->GetPriority(); + if (aPriority != bPriority) + return aPriority > bPriority; + } + auto aSpeed = a->GetUser()->GetBoostedStat(Core::Statistic::Speed); + auto bSpeed = b->GetUser()->GetBoostedStat(Core::Statistic::Speed); + if (aSpeed != bSpeed) + return aSpeed > bSpeed; + + auto randomValue = rand.Get(2); + return randomValue == 0; } -void TurnOrdering::OrderChoices(std::vector &vec) { - std::sort(vec.begin(), vec.end(), ___ChoiceOrderFunc); +void TurnOrdering::OrderChoices(std::vector &vec, Core::Random& rand) { + auto comp = [&](const BaseTurnChoice * a,const BaseTurnChoice * b)-> bool { + return ___ChoiceOrderFunc(a,b,rand); + }; + std::sort(vec.begin(), vec.end(), comp); } diff --git a/src/Battling/Flow/TurnOrdering.hpp b/src/Battling/Flow/TurnOrdering.hpp index 097965c..e760f92 100644 --- a/src/Battling/Flow/TurnOrdering.hpp +++ b/src/Battling/Flow/TurnOrdering.hpp @@ -2,12 +2,13 @@ #define CREATURELIB_TURNORDERING_HPP #include "../TurnChoices/BaseTurnChoice.hpp" +#include "../../Core/Random.hpp" #include namespace CreatureLib::Battling { class TurnOrdering { public: - static void OrderChoices(std::vector& vec); + static void OrderChoices(std::vector& vec, CreatureLib::Core::Random& rand); }; } diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index e9b88c5..0a5643c 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -3,6 +3,7 @@ #include "../TurnChoices/AttackTurnChoice.hpp" #include "../Flow/TurnOrdering.hpp" +using namespace CreatureLib; using namespace CreatureLib::Battling; const BattleLibrary *Battle::GetLibrary() const { @@ -39,7 +40,7 @@ void Battle::CheckChoicesSetAndRun() { i++; } } - TurnOrdering::OrderChoices(choices); + TurnOrdering::OrderChoices(choices, _random); auto choiceQueue = ChoiceQueue(choices); this->_currentTurnQueue = &choiceQueue; TurnHandler::RunTurn(choiceQueue); @@ -49,3 +50,7 @@ void Battle::CheckChoicesSetAndRun() { ChoiceQueue* Battle::GetCurrentTurnQueue() const { return _currentTurnQueue; } + +Core::Random &Battle::GetRandom(){ + return _random; +} diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index 4e514b6..781bcc5 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -13,6 +13,7 @@ namespace CreatureLib::Battling { uint8_t _numberOfSides; uint8_t _creaturesPerSide; std::vector _sides; + Core::Random _random; ChoiceQueue* _currentTurnQueue = nullptr; public: @@ -24,6 +25,7 @@ namespace CreatureLib::Battling { void CheckChoicesSetAndRun(); ChoiceQueue* GetCurrentTurnQueue() const; + Core::Random& GetRandom(); }; } diff --git a/src/Battling/TurnChoices/AttackTurnChoice.hpp b/src/Battling/TurnChoices/AttackTurnChoice.hpp index 96ff886..1efcc0c 100644 --- a/src/Battling/TurnChoices/AttackTurnChoice.hpp +++ b/src/Battling/TurnChoices/AttackTurnChoice.hpp @@ -18,6 +18,11 @@ namespace CreatureLib::Battling{ TurnChoiceKind GetKind() const override { return TurnChoiceKind ::Attack; } + + int8_t GetPriority() const{ + //HOOK: Change priority + return _attack->GetAttack()->GetPriority(); + } }; } diff --git a/tests/BattleTests/TurnOrderTests.cpp b/tests/BattleTests/TurnOrderTests.cpp index 21db5c1..fb4cdc1 100644 --- a/tests/BattleTests/TurnOrderTests.cpp +++ b/tests/BattleTests/TurnOrderTests.cpp @@ -5,17 +5,19 @@ #include "../../src/Battling/TurnChoices/AttackTurnChoice.hpp" #include "../../src/Battling/Flow/TurnOrdering.hpp" +using namespace CreatureLib; using namespace CreatureLib::Battling; TEST_CASE( "Turn ordering: Attack before pass", "[Battling]" ) { auto choice1 = new PassTurnChoice(nullptr); auto choice2 = new AttackTurnChoice(nullptr, nullptr); auto vec = std::vector{choice1, choice2}; - TurnOrdering::OrderChoices(vec); + auto rand = Core::Random(); + TurnOrdering::OrderChoices(vec,rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1); vec = std::vector{choice2, choice1}; - TurnOrdering::OrderChoices(vec); + TurnOrdering::OrderChoices(vec,rand); CHECK(vec[0] == choice2); CHECK(vec[1] == choice1);