Fixes AI setting targets incorrectly.

This commit is contained in:
Deukhoofd 2021-05-24 12:21:07 +02:00
parent 37c9855926
commit c9b8e663f8
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 51 additions and 6 deletions

View File

@ -18,6 +18,10 @@ namespace PkmnLibAI {
if (!move.HasValue()) { if (!move.HasValue()) {
continue; continue;
} }
auto t = GetTargetIndex(battle, user, move);
if (!t.has_value() || t.value() != target) {
continue;
}
if (move.GetValue()->GetRemainingUses() <= 0) if (move.GetValue()->GetRemainingUses() <= 0)
continue; continue;
auto naiveDamage = move.GetValue()->GetMoveData()->GetBasePower() * auto naiveDamage = move.GetValue()->GetMoveData()->GetBasePower() *

View File

@ -1,6 +1,8 @@
#ifndef PKMNLIB_AI_POKEMONAI_HPP #ifndef PKMNLIB_AI_POKEMONAI_HPP
#define PKMNLIB_AI_POKEMONAI_HPP #define PKMNLIB_AI_POKEMONAI_HPP
#include <CreatureLib/Battling/TurnChoices/AttackTurnChoice.hpp>
#include <CreatureLib/Battling/TurnChoices/BaseTurnChoice.hpp> #include <CreatureLib/Battling/TurnChoices/BaseTurnChoice.hpp>
#include <CreatureLib/Library/Attacks/AttackTarget.hpp>
#include <PkmnLib/Battling/Battle/Battle.hpp> #include <PkmnLib/Battling/Battle/Battle.hpp>
#include <PkmnLib/Battling/Pokemon/Pokemon.hpp> #include <PkmnLib/Battling/Pokemon/Pokemon.hpp>
@ -24,6 +26,43 @@ namespace PkmnLibAI {
return CreatureLib::Battling::CreatureIndex(0, 0); return CreatureLib::Battling::CreatureIndex(0, 0);
} }
} }
std::optional<CreatureLib::Battling::CreatureIndex> GetTargetIndex(PkmnLib::Battling::Battle* battle,
PkmnLib::Battling::Pokemon* user,
PkmnLib::Battling::LearnedMove* move) {
auto moveTarget = move->GetMoveData()->GetTarget();
auto userIndex = user->GetBattleIndex();
switch (moveTarget) {
case CreatureLib::Library::AttackTarget::Adjacent:
case CreatureLib::Library::AttackTarget::AdjacentOpponent:
case CreatureLib::Library::AttackTarget::All:
case CreatureLib::Library::AttackTarget::AllAdjacent:
case CreatureLib::Library::AttackTarget::AllAdjacentOpponent:
case CreatureLib::Library::AttackTarget::AllOpponent:
case CreatureLib::Library::AttackTarget::Any:
case CreatureLib::Library::AttackTarget::RandomOpponent: {
return GetOppositeIndex(user);
}
case CreatureLib::Library::AttackTarget::AdjacentAlly: {
if (battle->GetCreaturesPerSide() < 2) {
return {};
};
if (userIndex.GetCreatureIndex() > 0) {
return CreatureLib::Battling::CreatureIndex(userIndex.GetSideIndex(),
userIndex.GetCreatureIndex() - 1);
}
return CreatureLib::Battling::CreatureIndex(userIndex.GetSideIndex(),
userIndex.GetCreatureIndex() + 1);
}
case CreatureLib::Library::AttackTarget::AdjacentAllySelf:
case CreatureLib::Library::AttackTarget::AllAlly:
case CreatureLib::Library::AttackTarget::Self: {
return userIndex;
}
}
THROW("Unknown attack target kind: '" << CreatureLib::Library::AttackTargetHelper::ToString(moveTarget)
<< "'.")
}
}; };
} }

View File

@ -15,22 +15,24 @@ namespace PkmnLibAI {
CreatureLib::Battling::BaseTurnChoice* GetChoice([[maybe_unused]] PkmnLib::Battling::Battle* battle, CreatureLib::Battling::BaseTurnChoice* GetChoice([[maybe_unused]] PkmnLib::Battling::Battle* battle,
[[maybe_unused]] PkmnLib::Battling::Pokemon* user) override { [[maybe_unused]] PkmnLib::Battling::Pokemon* user) override {
auto moves = user->GetMoves(); auto moves = user->GetMoves();
ArbUt::List<PkmnLib::Battling::LearnedMove*> validMoves; ArbUt::List<std::tuple<PkmnLib::Battling::LearnedMove*, CreatureLib::Battling::CreatureIndex>> validMoves;
for (auto move : moves) { for (auto move : moves) {
if (!move.HasValue()) { if (!move.HasValue()) {
continue; continue;
} }
if (move.GetValue()->GetRemainingUses() > 0) { auto target = GetTargetIndex(battle, user, move.GetValue());
validMoves.Append(move.GetValue()); if (move.GetValue()->GetRemainingUses() > 0 && target.has_value()) {
validMoves.Append(std::tuple<PkmnLib::Battling::LearnedMove*, CreatureLib::Battling::CreatureIndex>(
move.GetValue(), target.value()));
} }
} }
auto target = GetOppositeIndex(user);
if (validMoves.Count() == 0) { if (validMoves.Count() == 0) {
return battle->GetLibrary()->GetMiscLibrary()->ReplacementAttack(user, target); return battle->GetLibrary()->GetMiscLibrary()->ReplacementAttack(user, GetOppositeIndex(user));
} }
auto moveIndex = rand.Get(validMoves.Count()); auto moveIndex = rand.Get(validMoves.Count());
return new CreatureLib::Battling::AttackTurnChoice(user, validMoves[moveIndex], target); return new CreatureLib::Battling::AttackTurnChoice(user, std::get<0>(validMoves[moveIndex]),
std::get<1>(validMoves[moveIndex]));
} }
}; };
} }