Check if target of attack is a valid target for that attack.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2021-05-24 10:57:17 +02:00
parent 69dab061da
commit 8241a2d7b1
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 66 additions and 1 deletions

View File

@ -97,3 +97,57 @@ TargetList TargetResolver::ResolveTargets(const CreatureIndex& index, AttackTarg
}
THROW("Unknown attack target kind: '" << AttackTargetHelper::ToString(target) << "'.")
}
bool TargetResolver::IsValidTarget(const CreatureIndex& index, CreatureLib::Library::AttackTarget target,
const BorrowedPtr<Creature>& user) {
auto userIndex = user->GetBattleIndex();
switch (target) {
case AttackTarget::Adjacent:
case AttackTarget::AllAdjacent: {
auto diff = abs(index.GetCreatureIndex() - userIndex.GetCreatureIndex());
// If the difference is 0, ensure the move is not targeting self.
if (diff == 0) {
return index.GetSideIndex() != userIndex.GetSideIndex();
}
// Otherwise check if the difference is max 1.
return diff <= 1;
}
case AttackTarget::AdjacentAlly: {
if (index.GetSideIndex() != userIndex.GetSideIndex()) {
return false;
}
auto diff = abs(index.GetCreatureIndex() - userIndex.GetCreatureIndex());
return diff == 1;
}
case AttackTarget::AdjacentAllySelf: {
if (index.GetSideIndex() != userIndex.GetSideIndex()) {
return false;
}
auto diff = abs(index.GetCreatureIndex() - userIndex.GetCreatureIndex());
return diff <= 1;
}
case AttackTarget::AdjacentOpponent:
case AttackTarget::AllAdjacentOpponent: {
if (index.GetSideIndex() == userIndex.GetSideIndex()) {
return false;
}
auto diff = abs(index.GetCreatureIndex() - userIndex.GetCreatureIndex());
return diff <= 1;
}
case AttackTarget::All:
case AttackTarget::Any:
case AttackTarget::RandomOpponent: {
return true;
}
case AttackTarget::AllAlly: {
return index.GetSideIndex() == userIndex.GetSideIndex();
}
case AttackTarget::AllOpponent: {
return index.GetSideIndex() != userIndex.GetSideIndex();
}
case AttackTarget::Self: {
return index == userIndex;
}
}
THROW("Unknown attack target kind: '" << AttackTargetHelper::ToString(target) << "'.")
}

View File

@ -12,6 +12,9 @@ namespace CreatureLib::Battling {
public:
static TargetList ResolveTargets(const CreatureIndex& index, CreatureLib::Library::AttackTarget target,
const ArbUt::BorrowedPtr<Battle>& battle);
static bool IsValidTarget(const CreatureIndex& index, CreatureLib::Library::AttackTarget target,
const ArbUt::BorrowedPtr<Creature>& user);
};
}

View File

@ -1,5 +1,6 @@
#include "Battle.hpp"
#include "../EventHooks/EventDataClasses.hpp"
#include "../Flow/ResolveTarget.hpp"
#include "../Flow/TurnHandler.hpp"
#include "../Flow/TurnOrdering.hpp"
@ -10,8 +11,15 @@ const ArbUt::BorrowedPtr<const BattleLibrary>& Battle::GetLibrary() const noexce
bool Battle::CanUse(const ArbUt::BorrowedPtr<BaseTurnChoice>& choice) {
if (choice->GetKind() == TurnChoiceKind::Attack) {
auto attack = choice.ForceAs<AttackTurnChoice>();
// HOOK: change number of uses needed.
return choice.ForceAs<AttackTurnChoice>()->GetAttack()->GetRemainingUses() >= 1;
if (attack->GetAttack()->GetRemainingUses() < 1) {
return false;
}
if (!TargetResolver::IsValidTarget(attack->GetTarget(), attack->GetAttack()->GetAttack()->GetTarget(),
attack->GetUser())) {
return false;
}
}
return true;
}