using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.Models.Choices;
///
/// The choice of a Pokémon to use a move.
///
public interface IMoveChoice : ITurnChoice
{
///
/// The move that is used.
///
ILearnedMove ChosenMove { get; }
///
/// The side the move is targeted at.
///
byte TargetSide { get; }
///
/// The position the move is targeted at.
///
byte TargetPosition { get; }
///
/// The priority of the move.
///
sbyte Priority { get; set; }
///
/// The underlying script of the move.
///
ScriptContainer Script { get; set; }
///
/// Additional data that can be used by scripts to store information about the choice.
///
Dictionary? AdditionalData { get; }
void SetAdditionalData(StringKey key, object? value);
///
/// Volatile effects that are applied to the move choice.
///
IScriptSet Volatile { get; }
}
///
[PublicAPI]
public class MoveChoice : TurnChoice, IMoveChoice
{
///
public MoveChoice(IPokemon user, ILearnedMove usedMove, byte targetSide, byte targetPosition) : base(user)
{
ChosenMove = usedMove;
TargetSide = targetSide;
TargetPosition = targetPosition;
Volatile = new ScriptSet(this);
var secondaryEffect = usedMove.MoveData.SecondaryEffect;
if (secondaryEffect != null)
{
if (user.Library.ScriptResolver.TryResolve(ScriptCategory.Move, secondaryEffect.Name,
secondaryEffect.Parameters, out var script))
{
Script.Set(script);
script.OnAddedToParent(this);
}
}
}
///
public ILearnedMove ChosenMove { get; }
///
public byte TargetSide { get; }
///
public byte TargetPosition { get; }
///
public sbyte Priority { get; set; }
///
public ScriptContainer Script { get; set; } = new();
///
public Dictionary? AdditionalData { get; private set; }
///
public void SetAdditionalData(StringKey key, object? value)
{
AdditionalData ??= new Dictionary();
AdditionalData[key] = value;
}
///
public IScriptSet Volatile { get; }
///
public override int ScriptCount => 2 + User.ScriptCount;
///
public override void GetOwnScripts(List> scripts)
{
scripts.Add(Volatile);
scripts.Add(Script);
}
///
public override void CollectScripts(List> scripts)
{
GetOwnScripts(scripts);
User.CollectScripts(scripts);
}
///
public override string ToString() =>
$"MoveChoice: {ChosenMove.MoveData.Name}, Target: {TargetSide}:{TargetPosition}, User: {User}";
protected bool Equals(MoveChoice other) =>
other.User == User && other.ChosenMove.Equals(ChosenMove) && other.TargetSide == TargetSide &&
other.TargetPosition == TargetPosition;
///
public override bool Equals(object? obj)
{
if (obj is null)
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj is not MoveChoice other)
return false;
return Equals(other);
}
///
public override int GetHashCode() =>
HashCode.Combine(User, ChosenMove, TargetSide, TargetPosition);
}