Used ClangFormat style guide I'm happy with.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -5,31 +5,26 @@
|
||||
#include <vector>
|
||||
#include "../TurnChoices/BaseTurnChoice.hpp"
|
||||
|
||||
namespace CreatureLib::Battling{
|
||||
namespace CreatureLib::Battling {
|
||||
class ChoiceQueue {
|
||||
std::vector<BaseTurnChoice*> _queue;
|
||||
size_t _current = 0;
|
||||
|
||||
public:
|
||||
bool HasCompletedQueue = false;
|
||||
|
||||
explicit ChoiceQueue(std::vector<BaseTurnChoice*> queue)
|
||||
:_queue(std::move(queue)){}
|
||||
explicit ChoiceQueue(std::vector<BaseTurnChoice*> queue) : _queue(std::move(queue)) {}
|
||||
|
||||
BaseTurnChoice* Dequeue(){
|
||||
BaseTurnChoice* Dequeue() {
|
||||
auto b = _queue[_current];
|
||||
_current++;
|
||||
return b;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool HasNext() const{
|
||||
return _current < _queue.size();
|
||||
}
|
||||
[[nodiscard]] bool HasNext() const { return _current < _queue.size(); }
|
||||
|
||||
std::vector<BaseTurnChoice*>& GetInnerQueue(){
|
||||
return _queue;
|
||||
}
|
||||
std::vector<BaseTurnChoice*>& GetInnerQueue() { return _queue; }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif //CREATURELIB_CHOICEQUEUE_HPP
|
||||
#endif // CREATURELIB_CHOICEQUEUE_HPP
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
#include "TurnHandler.hpp"
|
||||
#include "../Models/Battle.hpp"
|
||||
#include "../../Core/Exceptions/NotImplementedException.hpp"
|
||||
#include "../Models/Battle.hpp"
|
||||
#include "../ScriptHandling/ScriptMacros.cpp"
|
||||
|
||||
using namespace CreatureLib::Battling;
|
||||
|
||||
void TurnHandler::RunTurn(Battle* battle, ChoiceQueue* queue) {
|
||||
for (auto choice: queue->GetInnerQueue()){
|
||||
for (auto choice : queue->GetInnerQueue()) {
|
||||
HOOK(OnBeforeTurn, choice, choice);
|
||||
}
|
||||
while (queue->HasNext()){
|
||||
if (!battle->HasRecalledSlots()){
|
||||
while (queue->HasNext()) {
|
||||
if (!battle->HasRecalledSlots()) {
|
||||
return;
|
||||
}
|
||||
auto item = queue->Dequeue();
|
||||
@@ -20,9 +20,8 @@ void TurnHandler::RunTurn(Battle* battle, ChoiceQueue* queue) {
|
||||
queue->HasCompletedQueue = true;
|
||||
}
|
||||
|
||||
void TurnHandler::ExecuteChoice(BaseTurnChoice *choice) {
|
||||
if (choice == nullptr)
|
||||
{
|
||||
void TurnHandler::ExecuteChoice(BaseTurnChoice* choice) {
|
||||
if (choice == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto choiceKind = choice->GetKind();
|
||||
@@ -31,35 +30,33 @@ void TurnHandler::ExecuteChoice(BaseTurnChoice *choice) {
|
||||
}
|
||||
auto user = choice->GetUser();
|
||||
// If the user is fainted, we don't want to execute its choice.
|
||||
if (user->IsFainted()){
|
||||
if (user->IsFainted()) {
|
||||
return;
|
||||
}
|
||||
auto battle = user->GetBattle();
|
||||
// If the user is not in the field, we don't want to execute its choice.
|
||||
if (!battle->CreatureInField(user)){
|
||||
if (!battle->CreatureInField(user)) {
|
||||
return;
|
||||
}
|
||||
// If the choice is not valid, we don't want to execute it.
|
||||
if (!battle->CanUse(choice)){
|
||||
if (!battle->CanUse(choice)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (choiceKind){
|
||||
switch (choiceKind) {
|
||||
case TurnChoiceKind::Pass: throw NotReachableException();
|
||||
case TurnChoiceKind::Attack:
|
||||
return ExecuteAttackChoice(dynamic_cast<AttackTurnChoice*>(choice));
|
||||
case TurnChoiceKind::Attack: return ExecuteAttackChoice(dynamic_cast<AttackTurnChoice*>(choice));
|
||||
case TurnChoiceKind::Item:
|
||||
case TurnChoiceKind::Switch:
|
||||
case TurnChoiceKind::RunAway:
|
||||
throw NotImplementedException();
|
||||
case TurnChoiceKind::RunAway: throw NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
void TurnHandler::ExecuteAttackChoice(AttackTurnChoice *choice) {
|
||||
void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) {
|
||||
auto attackName = choice->GetAttack()->GetAttack()->GetName();
|
||||
HOOK(ChangeAttack, choice, choice, attackName);
|
||||
if (attackName != choice->GetAttack()->GetAttack()->GetName()){
|
||||
//TODO: Change attack
|
||||
if (attackName != choice->GetAttack()->GetAttack()->GetName()) {
|
||||
// TODO: Change attack
|
||||
}
|
||||
|
||||
// FIXME: Resolve all targets
|
||||
@@ -69,56 +66,57 @@ void TurnHandler::ExecuteAttackChoice(AttackTurnChoice *choice) {
|
||||
auto attack = new ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
|
||||
bool prevented = false;
|
||||
HOOK(PreventAttack, attack, attack, prevented);
|
||||
if (prevented){
|
||||
if (prevented) {
|
||||
return;
|
||||
}
|
||||
|
||||
//HOOK: override targets
|
||||
// HOOK: override targets
|
||||
|
||||
if (!choice->GetAttack()->TryUse(1)){
|
||||
if (!choice->GetAttack()->TryUse(1)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//HOOK: check if attack fails
|
||||
// HOOK: check if attack fails
|
||||
bool fail = false;
|
||||
HOOK(FailAttack, attack, attack, fail);
|
||||
if (fail){
|
||||
//TODO: Fail handling.
|
||||
if (fail) {
|
||||
// TODO: Fail handling.
|
||||
return;
|
||||
}
|
||||
|
||||
HOOK(StopBeforeAttack, attack, attack);
|
||||
HOOK(OnBeforeAttack, attack, attack);
|
||||
|
||||
for (auto& kv: attack->GetTargets()){
|
||||
for (auto& kv : attack->GetTargets()) {
|
||||
HandleAttackForTarget(attack, kv.first, kv.second);
|
||||
}
|
||||
|
||||
//TODO: We currently delete this, but we probably want to store this in a log, so scripts can look it up.
|
||||
// TODO: We currently delete this, but we probably want to store this in a log, so scripts can look it up.
|
||||
delete attack;
|
||||
}
|
||||
|
||||
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature *target, const ExecutingAttack::TargetData &targetData) {
|
||||
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* target,
|
||||
const ExecutingAttack::TargetData& targetData) {
|
||||
auto user = attack->GetUser();
|
||||
|
||||
ScriptSource* targetSource = target;
|
||||
ScriptSource* userSource = attack;
|
||||
ScriptSource* targetSource = target;
|
||||
ScriptSource* userSource = attack;
|
||||
|
||||
bool fail = false;
|
||||
HOOK(FailIncomingAttack, targetSource, attack, target, fail);
|
||||
if (fail){
|
||||
//TODO: Fail handling.
|
||||
if (fail) {
|
||||
// TODO: Fail handling.
|
||||
return;
|
||||
}
|
||||
|
||||
bool invulnerable = fail;
|
||||
HOOK(IsInvulnerable, targetSource, attack, target, invulnerable);
|
||||
if (invulnerable){
|
||||
//TODO: We should probably do something when a target is invulnerable.
|
||||
if (invulnerable) {
|
||||
// TODO: We should probably do something when a target is invulnerable.
|
||||
return;
|
||||
}
|
||||
|
||||
if (!targetData.IsHit()){
|
||||
if (!targetData.IsHit()) {
|
||||
HOOK(OnAttackMiss, targetSource, attack, target);
|
||||
return;
|
||||
}
|
||||
@@ -129,11 +127,11 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature *targe
|
||||
auto attackData = attack->GetAttack()->GetAttack();
|
||||
auto library = user->GetBattle()->GetLibrary();
|
||||
auto dmgLibrary = library->GetDamageLibrary();
|
||||
for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++){
|
||||
if (user->IsFainted()){
|
||||
for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++) {
|
||||
if (user->IsFainted()) {
|
||||
break;
|
||||
}
|
||||
if (target->IsFainted()){
|
||||
if (target->IsFainted()) {
|
||||
// STOP, STOP! HE'S ALREADY DEAD ;_;
|
||||
break;
|
||||
}
|
||||
@@ -146,28 +144,27 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature *targe
|
||||
hit.SetBasePower(dmgLibrary->GetBasePower(attack, target, hitIndex));
|
||||
hit.SetDamage(dmgLibrary->GetDamage(attack, target, hitIndex));
|
||||
|
||||
if (attackData->GetCategory() == Library::AttackCategory::Status){
|
||||
if (attackData->GetCategory() == Library::AttackCategory::Status) {
|
||||
HOOK(OnStatusMove, userSource, attack, target, hitIndex);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
auto damage = hit.GetDamage();
|
||||
if (damage > target->GetCurrentHealth()){
|
||||
if (damage > target->GetCurrentHealth()) {
|
||||
damage = target->GetCurrentHealth();
|
||||
hit.SetDamage(damage);
|
||||
}
|
||||
if (damage > 0){
|
||||
if (damage > 0) {
|
||||
target->Damage(damage, DamageSource::AttackDamage);
|
||||
|
||||
bool preventSecondary = false;
|
||||
HOOK(PreventSecondaryEffects, targetSource, attack, target, hitIndex, preventSecondary);
|
||||
if (!preventSecondary){
|
||||
if (!preventSecondary) {
|
||||
HOOK(OnSecondaryEffect, userSource, attack, target, hitIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!user->IsFainted()){
|
||||
if (!user->IsFainted()) {
|
||||
HOOK(OnAfterHits, userSource, attack, target);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef CREATURELIB_TURNHANDLER_HPP
|
||||
#define CREATURELIB_TURNHANDLER_HPP
|
||||
|
||||
#include "ChoiceQueue.hpp"
|
||||
#include "../TurnChoices/AttackTurnChoice.hpp"
|
||||
#include "../Models/ExecutingAttack.hpp"
|
||||
#include "../TurnChoices/AttackTurnChoice.hpp"
|
||||
#include "ChoiceQueue.hpp"
|
||||
|
||||
namespace CreatureLib::Battling {
|
||||
class Battle;
|
||||
@@ -12,10 +12,12 @@ namespace CreatureLib::Battling {
|
||||
static void ExecuteChoice(BaseTurnChoice* choice);
|
||||
|
||||
static void ExecuteAttackChoice(AttackTurnChoice* choice);
|
||||
static void HandleAttackForTarget(ExecutingAttack* attack, Creature* target, const ExecutingAttack::TargetData& targetData);
|
||||
static void HandleAttackForTarget(ExecutingAttack* attack, Creature* target,
|
||||
const ExecutingAttack::TargetData& targetData);
|
||||
|
||||
public:
|
||||
static void RunTurn(Battle* battle, ChoiceQueue* queue);
|
||||
};
|
||||
}
|
||||
|
||||
#endif //CREATURELIB_TURNHANDLER_HPP
|
||||
#endif // CREATURELIB_TURNHANDLER_HPP
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
#include "TurnOrdering.hpp"
|
||||
#include "../TurnChoices/AttackTurnChoice.hpp"
|
||||
#include "../Models/Battle.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include "../Models/Battle.hpp"
|
||||
#include "../TurnChoices/AttackTurnChoice.hpp"
|
||||
|
||||
using namespace CreatureLib;
|
||||
using namespace Battling;
|
||||
|
||||
bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b, Core::Random& rand){
|
||||
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;
|
||||
if (aKind == TurnChoiceKind::Attack){
|
||||
if (aKind == TurnChoiceKind::Attack) {
|
||||
auto aPriority = dynamic_cast<const AttackTurnChoice*>(a)->GetPriority();
|
||||
auto bPriority = dynamic_cast<const AttackTurnChoice*>(b)->GetPriority();
|
||||
if (aPriority != bPriority)
|
||||
@@ -27,9 +26,9 @@ bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b, Core::
|
||||
return randomValue == 0;
|
||||
}
|
||||
|
||||
void TurnOrdering::OrderChoices(std::vector<BaseTurnChoice *> &vec, Core::Random& rand) {
|
||||
auto comp = [&](const BaseTurnChoice * a,const BaseTurnChoice * b)-> bool {
|
||||
return ___ChoiceOrderFunc(a,b,rand);
|
||||
void TurnOrdering::OrderChoices(std::vector<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);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef CREATURELIB_TURNORDERING_HPP
|
||||
#define CREATURELIB_TURNORDERING_HPP
|
||||
|
||||
#include "../TurnChoices/BaseTurnChoice.hpp"
|
||||
#include "../../Core/Random.hpp"
|
||||
#include <vector>
|
||||
#include "../../Core/Random.hpp"
|
||||
#include "../TurnChoices/BaseTurnChoice.hpp"
|
||||
|
||||
namespace CreatureLib::Battling {
|
||||
class TurnOrdering {
|
||||
@@ -12,4 +12,4 @@ namespace CreatureLib::Battling {
|
||||
};
|
||||
}
|
||||
|
||||
#endif //CREATURELIB_TURNORDERING_HPP
|
||||
#endif // CREATURELIB_TURNORDERING_HPP
|
||||
|
||||
Reference in New Issue
Block a user