From b036efaf18070f252ac4d0db6b8d31c1d0d20751 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 24 May 2021 10:31:15 +0200 Subject: [PATCH] Clean up TargetResolver. Signed-off-by: Deukhoofd --- src/Battling/Flow/ResolveTarget.cpp | 99 +++++++++++++++++++++++++++++ src/Battling/Flow/ResolveTarget.hpp | 96 +--------------------------- 2 files changed, 100 insertions(+), 95 deletions(-) diff --git a/src/Battling/Flow/ResolveTarget.cpp b/src/Battling/Flow/ResolveTarget.cpp index e69de29..eab79f7 100644 --- a/src/Battling/Flow/ResolveTarget.cpp +++ b/src/Battling/Flow/ResolveTarget.cpp @@ -0,0 +1,99 @@ +#include "ResolveTarget.hpp" +using namespace ArbUt; +using namespace CreatureLib::Library; +using namespace CreatureLib::Battling; + +static inline constexpr uint8_t GetOppositeSide(uint8_t v) { + if (v == 1) { + return 0; + } + return 1; +} + +static inline List> GetAll(const BorrowedPtr& battle) { + List> arr(battle->GetCreaturesPerSide() * battle->GetSides().Count()); + for (auto* side : battle->GetSides()) { + for (const auto& mon : side->GetCreatures()) { + arr.Append(mon); + } + } + return arr; +} + +static inline List> GetAllAdjacent(const CreatureIndex& index, + const BorrowedPtr& battle) { + auto left = index.GetCreatureIndex() - 1; + auto right = index.GetCreatureIndex() + 1; + if (left < 0 && right >= battle->GetCreaturesPerSide()) { + return {battle->GetCreature(index), + battle->GetCreature(GetOppositeSide(index.GetSideIndex()), index.GetCreatureIndex())}; + } + if (left >= 0) { + if (right < battle->GetCreaturesPerSide()) { + return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), left), + battle->GetCreature(index.GetSideIndex(), right), + battle->GetCreature(GetOppositeSide(index.GetSideIndex()), index.GetCreatureIndex())}; + } + return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), left), + battle->GetCreature(GetOppositeSide(index.GetSideIndex()), index.GetCreatureIndex())}; + } + return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), right), + battle->GetCreature(GetOppositeSide(index.GetSideIndex()), index.GetCreatureIndex())}; +} + +static inline List> GetAllAdjacentOpponent(const CreatureIndex& index, + const BorrowedPtr& battle) { + auto left = index.GetCreatureIndex() - 1; + auto right = index.GetCreatureIndex() + 1; + if (left < 0 && right >= battle->GetCreaturesPerSide()) { + return {battle->GetCreature(index)}; + } + if (left >= 0) { + if (right < battle->GetCreaturesPerSide()) { + return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), left), + battle->GetCreature(index.GetSideIndex(), right)}; + } + return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), left)}; + } + return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), right)}; +} + +List> TargetResolver::ResolveTargets(const CreatureIndex& index, AttackTarget target, + const BorrowedPtr& battle) { + switch (target) { + // Single targets should just return the mon at the index. + case AttackTarget::Adjacent: + case AttackTarget::AdjacentAlly: + case AttackTarget::AdjacentAllySelf: + case AttackTarget::AdjacentOpponent: + case AttackTarget::Any: + case AttackTarget::RandomOpponent: + case AttackTarget::Self: { + return {battle->GetCreature(index)}; + } + case AttackTarget::All: { + return GetAll(battle); + } + case AttackTarget::AllAdjacent: { + return GetAllAdjacent(index, battle); + } + case AttackTarget::AllAdjacentOpponent: { + return GetAllAdjacentOpponent(index, battle); + } + case AttackTarget::AllAlly: { + List> arr(battle->GetCreaturesPerSide()); + for (const auto& mon : battle->GetSides()[index.GetSideIndex()]->GetCreatures()) { + arr.Append(mon); + } + return arr; + } + case AttackTarget::AllOpponent: { + List> arr(battle->GetCreaturesPerSide()); + for (const auto& mon : battle->GetSides()[index.GetSideIndex()]->GetCreatures()) { + arr.Append(mon); + } + return List>(arr); + } + } + THROW("Unknown attack target kind: '" << AttackTargetHelper::ToString(target) << "'.") +} diff --git a/src/Battling/Flow/ResolveTarget.hpp b/src/Battling/Flow/ResolveTarget.hpp index 0414c8a..a0044bf 100644 --- a/src/Battling/Flow/ResolveTarget.hpp +++ b/src/Battling/Flow/ResolveTarget.hpp @@ -9,103 +9,9 @@ using namespace CreatureLib::Battling; namespace CreatureLib::Battling { class TargetResolver { public: - static inline constexpr uint8_t GetOppositeSide(uint8_t v) { - if (v == 1) - return 0; - return 1; - } - static ArbUt::List> ResolveTargets(const CreatureIndex& index, CreatureLib::Library::AttackTarget target, - const ArbUt::BorrowedPtr& battle) { - switch (target) { - case CreatureLib::Library::AttackTarget::Adjacent: { - return {battle->GetCreature(index)}; - }; - case CreatureLib::Library::AttackTarget::AdjacentAlly: { - return {battle->GetCreature(index)}; - }; - case CreatureLib::Library::AttackTarget::AdjacentAllySelf: { - return {battle->GetCreature(index)}; - }; - case CreatureLib::Library::AttackTarget::AdjacentOpponent: { - return {battle->GetCreature(index)}; - }; - case CreatureLib::Library::AttackTarget::All: { - ArbUt::OptionalBorrowedPtr - arr[battle->GetCreaturesPerSide() * battle->GetSides().Count()]; - size_t i = 0; - for (auto side : battle->GetSides()) { - for (auto mon : side->GetCreatures()) { - arr[i++] = mon; - } - } - return ArbUt::List>(arr, arr + i); - } - case CreatureLib::Library::AttackTarget::AllAdjacent: { - auto left = index.GetCreatureIndex() - 1; - auto right = index.GetCreatureIndex() + 1; - if (left < 0 && right >= battle->GetCreaturesPerSide()) { - return {battle->GetCreature(index), - battle->GetCreature(GetOppositeSide(index.GetSideIndex()), index.GetCreatureIndex())}; - } else if (left >= 0) { - if (right < battle->GetCreaturesPerSide()) { - return { - battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), left), - battle->GetCreature(index.GetSideIndex(), right), - battle->GetCreature(GetOppositeSide(index.GetSideIndex()), index.GetCreatureIndex())}; - } - return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), left), - battle->GetCreature(GetOppositeSide(index.GetSideIndex()), index.GetCreatureIndex())}; - } else { - return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), right), - battle->GetCreature(GetOppositeSide(index.GetSideIndex()), index.GetCreatureIndex())}; - } - } - case CreatureLib::Library::AttackTarget::AllAdjacentOpponent: { - auto left = index.GetCreatureIndex() - 1; - auto right = index.GetCreatureIndex() + 1; - if (left < 0 && right >= battle->GetCreaturesPerSide()) { - return {battle->GetCreature(index)}; - } else if (left >= 0) { - if (right < battle->GetCreaturesPerSide()) { - return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), left), - battle->GetCreature(index.GetSideIndex(), right)}; - } - return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), left)}; - } else { - return {battle->GetCreature(index), battle->GetCreature(index.GetSideIndex(), right)}; - } - } - case CreatureLib::Library::AttackTarget::AllAlly: { - ArbUt::OptionalBorrowedPtr arr[battle->GetCreaturesPerSide()]; - size_t i = 0; - for (auto mon : battle->GetSides()[index.GetSideIndex()]->GetCreatures()) { - arr[i++] = mon; - } - return ArbUt::List>(arr, arr + i); - }; - case CreatureLib::Library::AttackTarget::AllOpponent: { - ArbUt::OptionalBorrowedPtr arr[battle->GetCreaturesPerSide()]; - size_t i = 0; - for (auto mon : battle->GetSides()[index.GetSideIndex()]->GetCreatures()) { - arr[i++] = mon; - } - return ArbUt::List>(arr, arr + i); - }; - case CreatureLib::Library::AttackTarget::Any: { - return {battle->GetCreature(index)}; - }; - case CreatureLib::Library::AttackTarget::RandomOpponent: { - return {battle->GetCreature(index)}; - }; - case CreatureLib::Library::AttackTarget::Self: { - return {battle->GetCreature(index)}; - }; - } - THROW("Unknown attack target kind: '" << CreatureLib::Library::AttackTargetHelper::ToString(target) - << "'."); - } + const ArbUt::BorrowedPtr& battle); }; }