Fully handle turn ordering
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
972af35ecf
commit
42c1623985
|
@ -1,18 +1,36 @@
|
||||||
#include "TurnOrdering.hpp"
|
#include "TurnOrdering.hpp"
|
||||||
|
#include "../TurnChoices/AttackTurnChoice.hpp"
|
||||||
|
#include "../Models/Creature.hpp"
|
||||||
|
#include "../Models/Battle.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#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 aKind = a->GetKind();
|
||||||
auto bKind = b->GetKind();
|
auto bKind = b->GetKind();
|
||||||
if (aKind != bKind)
|
if (aKind != bKind)
|
||||||
return aKind > bKind;
|
return aKind > bKind;
|
||||||
//FIXME: choices need to be ordered when they're the same type
|
if (aKind == TurnChoiceKind::Attack){
|
||||||
throw 1;
|
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) {
|
void TurnOrdering::OrderChoices(std::vector<const BaseTurnChoice *> &vec, Core::Random& rand) {
|
||||||
std::sort(vec.begin(), vec.end(), ___ChoiceOrderFunc);
|
auto comp = [&](const BaseTurnChoice * a,const BaseTurnChoice * b)-> bool {
|
||||||
|
return ___ChoiceOrderFunc(a,b,rand);
|
||||||
|
};
|
||||||
|
std::sort(vec.begin(), vec.end(), comp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,13 @@
|
||||||
#define CREATURELIB_TURNORDERING_HPP
|
#define CREATURELIB_TURNORDERING_HPP
|
||||||
|
|
||||||
#include "../TurnChoices/BaseTurnChoice.hpp"
|
#include "../TurnChoices/BaseTurnChoice.hpp"
|
||||||
|
#include "../../Core/Random.hpp"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace CreatureLib::Battling {
|
namespace CreatureLib::Battling {
|
||||||
class TurnOrdering {
|
class TurnOrdering {
|
||||||
public:
|
public:
|
||||||
static void OrderChoices(std::vector<const BaseTurnChoice*>& vec);
|
static void OrderChoices(std::vector<const BaseTurnChoice*>& vec, CreatureLib::Core::Random& rand);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include "../TurnChoices/AttackTurnChoice.hpp"
|
#include "../TurnChoices/AttackTurnChoice.hpp"
|
||||||
#include "../Flow/TurnOrdering.hpp"
|
#include "../Flow/TurnOrdering.hpp"
|
||||||
|
|
||||||
|
using namespace CreatureLib;
|
||||||
using namespace CreatureLib::Battling;
|
using namespace CreatureLib::Battling;
|
||||||
|
|
||||||
const BattleLibrary *Battle::GetLibrary() const {
|
const BattleLibrary *Battle::GetLibrary() const {
|
||||||
|
@ -39,7 +40,7 @@ void Battle::CheckChoicesSetAndRun() {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TurnOrdering::OrderChoices(choices);
|
TurnOrdering::OrderChoices(choices, _random);
|
||||||
auto choiceQueue = ChoiceQueue(choices);
|
auto choiceQueue = ChoiceQueue(choices);
|
||||||
this->_currentTurnQueue = &choiceQueue;
|
this->_currentTurnQueue = &choiceQueue;
|
||||||
TurnHandler::RunTurn(choiceQueue);
|
TurnHandler::RunTurn(choiceQueue);
|
||||||
|
@ -49,3 +50,7 @@ void Battle::CheckChoicesSetAndRun() {
|
||||||
ChoiceQueue* Battle::GetCurrentTurnQueue() const {
|
ChoiceQueue* Battle::GetCurrentTurnQueue() const {
|
||||||
return _currentTurnQueue;
|
return _currentTurnQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Core::Random &Battle::GetRandom(){
|
||||||
|
return _random;
|
||||||
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ namespace CreatureLib::Battling {
|
||||||
uint8_t _numberOfSides;
|
uint8_t _numberOfSides;
|
||||||
uint8_t _creaturesPerSide;
|
uint8_t _creaturesPerSide;
|
||||||
std::vector<BattleSide*> _sides;
|
std::vector<BattleSide*> _sides;
|
||||||
|
Core::Random _random;
|
||||||
|
|
||||||
ChoiceQueue* _currentTurnQueue = nullptr;
|
ChoiceQueue* _currentTurnQueue = nullptr;
|
||||||
public:
|
public:
|
||||||
|
@ -24,6 +25,7 @@ namespace CreatureLib::Battling {
|
||||||
void CheckChoicesSetAndRun();
|
void CheckChoicesSetAndRun();
|
||||||
|
|
||||||
ChoiceQueue* GetCurrentTurnQueue() const;
|
ChoiceQueue* GetCurrentTurnQueue() const;
|
||||||
|
Core::Random& GetRandom();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,11 @@ namespace CreatureLib::Battling{
|
||||||
TurnChoiceKind GetKind() const override {
|
TurnChoiceKind GetKind() const override {
|
||||||
return TurnChoiceKind ::Attack;
|
return TurnChoiceKind ::Attack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int8_t GetPriority() const{
|
||||||
|
//HOOK: Change priority
|
||||||
|
return _attack->GetAttack()->GetPriority();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,17 +5,19 @@
|
||||||
#include "../../src/Battling/TurnChoices/AttackTurnChoice.hpp"
|
#include "../../src/Battling/TurnChoices/AttackTurnChoice.hpp"
|
||||||
#include "../../src/Battling/Flow/TurnOrdering.hpp"
|
#include "../../src/Battling/Flow/TurnOrdering.hpp"
|
||||||
|
|
||||||
|
using namespace CreatureLib;
|
||||||
using namespace CreatureLib::Battling;
|
using namespace CreatureLib::Battling;
|
||||||
|
|
||||||
TEST_CASE( "Turn ordering: Attack before pass", "[Battling]" ) {
|
TEST_CASE( "Turn ordering: Attack before pass", "[Battling]" ) {
|
||||||
auto choice1 = new PassTurnChoice(nullptr);
|
auto choice1 = new PassTurnChoice(nullptr);
|
||||||
auto choice2 = new AttackTurnChoice(nullptr, nullptr);
|
auto choice2 = new AttackTurnChoice(nullptr, nullptr);
|
||||||
auto vec = std::vector<const BaseTurnChoice*>{choice1, choice2};
|
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[0] == choice2);
|
||||||
CHECK(vec[1] == choice1);
|
CHECK(vec[1] == choice1);
|
||||||
vec = std::vector<const BaseTurnChoice*>{choice2, choice1};
|
vec = std::vector<const BaseTurnChoice*>{choice2, choice1};
|
||||||
TurnOrdering::OrderChoices(vec);
|
TurnOrdering::OrderChoices(vec,rand);
|
||||||
CHECK(vec[0] == choice2);
|
CHECK(vec[0] == choice2);
|
||||||
CHECK(vec[1] == choice1);
|
CHECK(vec[1] == choice1);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue