Clean up TargetResolver.

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2021-05-24 10:31:15 +02:00
parent b48353822c
commit b036efaf18
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 100 additions and 95 deletions

View File

@ -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<OptionalBorrowedPtr<Creature>> GetAll(const BorrowedPtr<Battle>& battle) {
List<OptionalBorrowedPtr<Creature>> 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<OptionalBorrowedPtr<Creature>> GetAllAdjacent(const CreatureIndex& index,
const BorrowedPtr<Battle>& 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<OptionalBorrowedPtr<Creature>> GetAllAdjacentOpponent(const CreatureIndex& index,
const BorrowedPtr<Battle>& 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<OptionalBorrowedPtr<Creature>> TargetResolver::ResolveTargets(const CreatureIndex& index, AttackTarget target,
const BorrowedPtr<Battle>& 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<OptionalBorrowedPtr<Creature>> arr(battle->GetCreaturesPerSide());
for (const auto& mon : battle->GetSides()[index.GetSideIndex()]->GetCreatures()) {
arr.Append(mon);
}
return arr;
}
case AttackTarget::AllOpponent: {
List<OptionalBorrowedPtr<Creature>> arr(battle->GetCreaturesPerSide());
for (const auto& mon : battle->GetSides()[index.GetSideIndex()]->GetCreatures()) {
arr.Append(mon);
}
return List<OptionalBorrowedPtr<Creature>>(arr);
}
}
THROW("Unknown attack target kind: '" << AttackTargetHelper::ToString(target) << "'.")
}

View File

@ -9,103 +9,9 @@ using namespace CreatureLib::Battling;
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class TargetResolver { class TargetResolver {
public: public:
static inline constexpr uint8_t GetOppositeSide(uint8_t v) {
if (v == 1)
return 0;
return 1;
}
static ArbUt::List<ArbUt::OptionalBorrowedPtr<Creature>> static ArbUt::List<ArbUt::OptionalBorrowedPtr<Creature>>
ResolveTargets(const CreatureIndex& index, CreatureLib::Library::AttackTarget target, ResolveTargets(const CreatureIndex& index, CreatureLib::Library::AttackTarget target,
const ArbUt::BorrowedPtr<Battle>& battle) { const ArbUt::BorrowedPtr<Battle>& 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<Creature>
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<ArbUt::OptionalBorrowedPtr<Creature>>(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<Creature> arr[battle->GetCreaturesPerSide()];
size_t i = 0;
for (auto mon : battle->GetSides()[index.GetSideIndex()]->GetCreatures()) {
arr[i++] = mon;
}
return ArbUt::List<ArbUt::OptionalBorrowedPtr<Creature>>(arr, arr + i);
};
case CreatureLib::Library::AttackTarget::AllOpponent: {
ArbUt::OptionalBorrowedPtr<Creature> arr[battle->GetCreaturesPerSide()];
size_t i = 0;
for (auto mon : battle->GetSides()[index.GetSideIndex()]->GetCreatures()) {
arr[i++] = mon;
}
return ArbUt::List<ArbUt::OptionalBorrowedPtr<Creature>>(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)
<< "'.");
}
}; };
} }