Check if target of attack is a valid target for that attack.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
@@ -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) << "'.")
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user