PkmnLib.NET/PkmnLib.Dynamic/AI/RandomAI.cs

48 lines
1.4 KiB
C#
Raw Normal View History

2024-10-01 09:00:01 +00:00
using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.Models.Choices;
using PkmnLib.Static.Moves;
2024-10-01 09:00:01 +00:00
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.AI;
/// <summary>
/// An AI that makes random choices.
/// </summary>
public class RandomAI : PokemonAI
{
private IRandom _random;
2025-03-02 16:19:57 +00:00
2024-10-01 09:00:01 +00:00
/// <inheritdoc />
public RandomAI() : base("Random")
{
_random = new RandomImpl();
}
/// <inheritdoc />
public override ITurnChoice GetChoice(IBattle battle, IPokemon pokemon)
{
var moves = pokemon.Moves.WhereNotNull().Where(x => x.CurrentPp > 0).ToList();
2024-10-01 09:00:01 +00:00
while (moves.Count > 0)
{
var move = moves[_random.GetInt(moves.Count)];
var targets = GetValidTargetsForMove(pokemon, move).ToArray();
if (move.MoveData.Category is MoveCategory.Physical or MoveCategory.Special)
{
targets = targets.Where(x => x.side != pokemon.BattleData!.SideIndex).ToArray();
}
2024-10-01 09:00:01 +00:00
if (targets.Length == 0)
{
moves.Remove(move);
continue;
}
var target = targets[_random.GetInt(targets.Length)];
var moveTurnChoice = new MoveChoice(pokemon, move!, target.side, target.position);
if (battle.CanUse(moveTurnChoice))
{
return moveTurnChoice;
}
moves.Remove(move);
}
return new PassChoice(pokemon);
}
}