Initial support for ordering choices and getting them ready for execution.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
af611070c5
commit
972af35ecf
|
@ -0,0 +1 @@
|
||||||
|
#include "ChoiceQueue.hpp"
|
|
@ -0,0 +1,28 @@
|
||||||
|
#ifndef CREATURELIB_CHOICEQUEUE_HPP
|
||||||
|
#define CREATURELIB_CHOICEQUEUE_HPP
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "../TurnChoices/BaseTurnChoice.hpp"
|
||||||
|
|
||||||
|
namespace CreatureLib::Battling{
|
||||||
|
class ChoiceQueue {
|
||||||
|
std::vector<const BaseTurnChoice*> _queue;
|
||||||
|
size_t _current = 0;
|
||||||
|
public:
|
||||||
|
ChoiceQueue(const std::vector<const BaseTurnChoice*>& queue)
|
||||||
|
:_queue(queue){}
|
||||||
|
|
||||||
|
const BaseTurnChoice* Dequeue(){
|
||||||
|
auto b = _queue[_current];
|
||||||
|
_current++;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HasNext() const{
|
||||||
|
return _current < _queue.size();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //CREATURELIB_CHOICEQUEUE_HPP
|
|
@ -0,0 +1,5 @@
|
||||||
|
#include "TurnHandler.hpp"
|
||||||
|
|
||||||
|
void CreatureLib::Battling::TurnHandler::RunTurn(CreatureLib::Battling::ChoiceQueue &queue) {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
#ifndef CREATURELIB_TURNHANDLER_HPP
|
||||||
|
#define CREATURELIB_TURNHANDLER_HPP
|
||||||
|
|
||||||
|
#include "ChoiceQueue.hpp"
|
||||||
|
|
||||||
|
namespace CreatureLib::Battling {
|
||||||
|
class Battle;
|
||||||
|
|
||||||
|
class TurnHandler {
|
||||||
|
public:
|
||||||
|
static void RunTurn(ChoiceQueue& queue);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //CREATURELIB_TURNHANDLER_HPP
|
|
@ -0,0 +1,18 @@
|
||||||
|
#include "TurnOrdering.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace CreatureLib::Battling;
|
||||||
|
|
||||||
|
bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b){
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TurnOrdering::OrderChoices(std::vector<const BaseTurnChoice *> &vec) {
|
||||||
|
std::sort(vec.begin(), vec.end(), ___ChoiceOrderFunc);
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
#ifndef CREATURELIB_TURNORDERING_HPP
|
||||||
|
#define CREATURELIB_TURNORDERING_HPP
|
||||||
|
|
||||||
|
#include "../TurnChoices/BaseTurnChoice.hpp"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace CreatureLib::Battling {
|
||||||
|
class TurnOrdering {
|
||||||
|
public:
|
||||||
|
static void OrderChoices(std::vector<const BaseTurnChoice*>& vec);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //CREATURELIB_TURNORDERING_HPP
|
|
@ -1,11 +1,15 @@
|
||||||
#include "Battle.hpp"
|
#include "Battle.hpp"
|
||||||
|
#include "../Flow/TurnHandler.hpp"
|
||||||
#include "../TurnChoices/AttackTurnChoice.hpp"
|
#include "../TurnChoices/AttackTurnChoice.hpp"
|
||||||
|
#include "../Flow/TurnOrdering.hpp"
|
||||||
|
|
||||||
const CreatureLib::Battling::BattleLibrary *CreatureLib::Battling::Battle::GetLibrary() const {
|
using namespace CreatureLib::Battling;
|
||||||
|
|
||||||
|
const BattleLibrary *Battle::GetLibrary() const {
|
||||||
return _library;
|
return _library;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CreatureLib::Battling::Battle::CanUse(CreatureLib::Battling::BaseTurnChoice *choice) {
|
bool Battle::CanUse(BaseTurnChoice *choice) {
|
||||||
if (choice->GetKind() == TurnChoiceKind::Attack){
|
if (choice->GetKind() == TurnChoiceKind::Attack){
|
||||||
//HOOK: change number of uses needed.
|
//HOOK: change number of uses needed.
|
||||||
return static_cast<AttackTurnChoice*>(choice)->GetAttack()->GetRemainingUses() > 1;
|
return static_cast<AttackTurnChoice*>(choice)->GetAttack()->GetRemainingUses() > 1;
|
||||||
|
@ -13,9 +17,35 @@ bool CreatureLib::Battling::Battle::CanUse(CreatureLib::Battling::BaseTurnChoice
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CreatureLib::Battling::Battle::TrySetChoice(CreatureLib::Battling::BaseTurnChoice *choice) {
|
bool Battle::TrySetChoice(BaseTurnChoice *choice) {
|
||||||
if (!CanUse(choice))
|
if (!CanUse(choice))
|
||||||
return false;
|
return false;
|
||||||
choice->GetUser()->GetBattleSide()->SetChoice(choice);
|
choice->GetUser()->GetBattleSide()->SetChoice(choice);
|
||||||
|
CheckChoicesSetAndRun();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Battle::CheckChoicesSetAndRun() {
|
||||||
|
for (auto side: _sides){
|
||||||
|
if (!side->AllChoicesSet()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auto choices = std::vector<const BaseTurnChoice*>(_numberOfSides * _creaturesPerSide);
|
||||||
|
auto i = 0;
|
||||||
|
for (auto side: _sides){
|
||||||
|
for (auto choice: side->GetChoices()){
|
||||||
|
choices[i] = choice;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TurnOrdering::OrderChoices(choices);
|
||||||
|
auto choiceQueue = ChoiceQueue(choices);
|
||||||
|
this->_currentTurnQueue = &choiceQueue;
|
||||||
|
TurnHandler::RunTurn(choiceQueue);
|
||||||
|
this->_currentTurnQueue = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
ChoiceQueue* Battle::GetCurrentTurnQueue() const {
|
||||||
|
return _currentTurnQueue;
|
||||||
|
}
|
||||||
|
|
|
@ -5,16 +5,25 @@
|
||||||
#include "BattleSide.hpp"
|
#include "BattleSide.hpp"
|
||||||
#include "../Library/BattleLibrary.hpp"
|
#include "../Library/BattleLibrary.hpp"
|
||||||
#include "../TurnChoices/BaseTurnChoice.hpp"
|
#include "../TurnChoices/BaseTurnChoice.hpp"
|
||||||
|
#include "../Flow/ChoiceQueue.hpp"
|
||||||
|
|
||||||
namespace CreatureLib::Battling {
|
namespace CreatureLib::Battling {
|
||||||
class Battle {
|
class Battle {
|
||||||
const BattleLibrary* _library;
|
const BattleLibrary* _library;
|
||||||
|
uint8_t _numberOfSides;
|
||||||
|
uint8_t _creaturesPerSide;
|
||||||
std::vector<BattleSide*> _sides;
|
std::vector<BattleSide*> _sides;
|
||||||
|
|
||||||
|
ChoiceQueue* _currentTurnQueue = nullptr;
|
||||||
public:
|
public:
|
||||||
const BattleLibrary* GetLibrary() const;
|
const BattleLibrary* GetLibrary() const;
|
||||||
|
|
||||||
virtual bool CanUse(BaseTurnChoice* choice);
|
virtual bool CanUse(BaseTurnChoice* choice);
|
||||||
virtual bool TrySetChoice(BaseTurnChoice* choice);
|
virtual bool TrySetChoice(BaseTurnChoice* choice);
|
||||||
|
|
||||||
|
void CheckChoicesSetAndRun();
|
||||||
|
|
||||||
|
ChoiceQueue* GetCurrentTurnQueue() const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,11 +8,16 @@ namespace CreatureLib::Battling{
|
||||||
class AttackTurnChoice : public BaseTurnChoice {
|
class AttackTurnChoice : public BaseTurnChoice {
|
||||||
LearnedAttack* _attack;
|
LearnedAttack* _attack;
|
||||||
public:
|
public:
|
||||||
AttackTurnChoice(Creature* c) : BaseTurnChoice(c){}
|
AttackTurnChoice(Creature* c, LearnedAttack* attack)
|
||||||
|
: BaseTurnChoice(c), _attack(attack){}
|
||||||
|
|
||||||
inline LearnedAttack* GetAttack() const{
|
inline LearnedAttack* GetAttack() const{
|
||||||
return _attack;
|
return _attack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TurnChoiceKind GetKind() const override {
|
||||||
|
return TurnChoiceKind ::Attack;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
#include "TurnChoiceKind.hpp"
|
#include "TurnChoiceKind.hpp"
|
||||||
|
|
||||||
namespace CreatureLib::Battling{
|
namespace CreatureLib::Battling{
|
||||||
|
class Creature;
|
||||||
|
|
||||||
class BaseTurnChoice {
|
class BaseTurnChoice {
|
||||||
Creature* _user;
|
Creature* _user;
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
#ifdef TESTS_BUILD
|
||||||
|
|
||||||
|
#include "../TestLibrary/TestLibrary.cpp"
|
||||||
|
#include "../../src/Battling/TurnChoices/PassTurnChoice.hpp"
|
||||||
|
#include "../../src/Battling/TurnChoices/AttackTurnChoice.hpp"
|
||||||
|
#include "../../src/Battling/Flow/TurnOrdering.hpp"
|
||||||
|
|
||||||
|
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);
|
||||||
|
CHECK(vec[0] == choice2);
|
||||||
|
CHECK(vec[1] == choice1);
|
||||||
|
vec = std::vector<const BaseTurnChoice*>{choice2, choice1};
|
||||||
|
TurnOrdering::OrderChoices(vec);
|
||||||
|
CHECK(vec[0] == choice2);
|
||||||
|
CHECK(vec[1] == choice1);
|
||||||
|
|
||||||
|
delete choice1;
|
||||||
|
delete choice2;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue