Fully handle turn ordering
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-10-31 12:31:31 +01:00
parent 972af35ecf
commit 42c1623985
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
6 changed files with 43 additions and 10 deletions

View File

@ -1,18 +1,36 @@
#include "TurnOrdering.hpp"
#include "../TurnChoices/AttackTurnChoice.hpp"
#include "../Models/Creature.hpp"
#include "../Models/Battle.hpp"
#include <algorithm>
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<const AttackTurnChoice*>(a)->GetPriority();
auto bPriority = static_cast<const AttackTurnChoice*>(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<const BaseTurnChoice *> &vec) {
std::sort(vec.begin(), vec.end(), ___ChoiceOrderFunc);
void TurnOrdering::OrderChoices(std::vector<const BaseTurnChoice *> &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);
}

View File

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

View File

@ -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;
}

View File

@ -13,6 +13,7 @@ namespace CreatureLib::Battling {
uint8_t _numberOfSides;
uint8_t _creaturesPerSide;
std::vector<BattleSide*> _sides;
Core::Random _random;
ChoiceQueue* _currentTurnQueue = nullptr;
public:
@ -24,6 +25,7 @@ namespace CreatureLib::Battling {
void CheckChoicesSetAndRun();
ChoiceQueue* GetCurrentTurnQueue() const;
Core::Random& GetRandom();
};
}

View File

@ -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();
}
};
}

View File

@ -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<const BaseTurnChoice*>{choice1, choice2};
TurnOrdering::OrderChoices(vec);
auto rand = Core::Random();
TurnOrdering::OrderChoices(vec,rand);
CHECK(vec[0] == choice2);
CHECK(vec[1] == choice1);
vec = std::vector<const BaseTurnChoice*>{choice2, choice1};
TurnOrdering::OrderChoices(vec);
TurnOrdering::OrderChoices(vec,rand);
CHECK(vec[0] == choice2);
CHECK(vec[1] == choice1);