Validate movechoice target

This commit is contained in:
Deukhoofd 2024-12-27 15:53:11 +01:00
parent b4e8ad1cd2
commit b3529fa22f
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 60 additions and 4 deletions

View File

@ -236,7 +236,9 @@ public class BattleImpl : ScriptSource, IBattle
// TODO: Hook to change number of PP needed. // TODO: Hook to change number of PP needed.
if (moveChoice.ChosenMove.CurrentPp < 1) if (moveChoice.ChosenMove.CurrentPp < 1)
return false; return false;
// TODO: Validate target if (!TargetResolver.IsValidTarget(moveChoice.TargetSide, moveChoice.TargetPosition,
moveChoice.ChosenMove.MoveData.Target, moveChoice.User))
return false;
} }
return true; return true;

View File

@ -26,6 +26,60 @@ public static class TargetResolver
}; };
} }
/// <summary>
/// Validates whether a given target is valid for a move choice. Returns true if the target is valid.
/// </summary>
public static bool IsValidTarget(byte side, byte position, MoveTarget target, IPokemon user)
{
var userBattleData = user.BattleData;
if (userBattleData == null)
throw new ArgumentNullException(nameof(user.BattleData));
var userSide = userBattleData.SideIndex;
var userPosition = userBattleData.Position;
switch (target)
{
case MoveTarget.Adjacent:
case MoveTarget.AllAdjacent:
{
var diff = Math.Abs(position - userPosition);
if (diff == 0)
return userSide == side;
return diff <= 1;
}
case MoveTarget.AdjacentAlly:
{
if (userSide != side)
return false;
return Math.Abs(position - userPosition) == 1;
}
case MoveTarget.AdjacentAllySelf:
{
if (userSide != side)
return false;
return Math.Abs(position - userPosition) <= 1;
}
case MoveTarget.AdjacentOpponent:
case MoveTarget.AllAdjacentOpponent:
{
if (userSide == side)
return false;
return Math.Abs(position - userPosition) <= 1;
}
case MoveTarget.All:
case MoveTarget.Any:
case MoveTarget.RandomOpponent:
return true;
case MoveTarget.AllAlly:
return userSide == side;
case MoveTarget.AllOpponent:
return userSide != side;
case MoveTarget.SelfUse:
return userSide == side && userPosition == position;
}
throw new ArgumentOutOfRangeException(nameof(target), target, null);
}
private static IReadOnlyList<IPokemon?> GetAllTargets(IBattle battle) => private static IReadOnlyList<IPokemon?> GetAllTargets(IBattle battle) =>
battle.Sides.SelectMany(x => x.Pokemon).ToList(); battle.Sides.SelectMany(x => x.Pokemon).ToList();

View File

@ -9,10 +9,10 @@ public static class StaticHelpers
/// A function to get the current date and time. This can be replaced in cases where the date and time /// A function to get the current date and time. This can be replaced in cases where the date and time
/// may not be the same as the system time. /// may not be the same as the system time.
/// </summary> /// </summary>
public static Func<DateTime> DateTimeProvider { get; set; } = () => DateTime.Now; public static Func<DateTimeOffset> DateTimeProvider { get; set; } = () => DateTimeOffset.Now;
/// <summary> /// <summary>
/// Get the current date and time. /// Get the current date and time.
/// </summary> /// </summary>
public static DateTime GetCurrentDateTime() => DateTimeProvider(); public static DateTimeOffset GetCurrentDateTime() => DateTimeProvider();
} }

View File

@ -24,7 +24,7 @@ public class Gen7MiscLibrary : IMiscLibrary
/// <inheritdoc /> /// <inheritdoc />
public TimeOfDay GetTimeOfDay() public TimeOfDay GetTimeOfDay()
{ {
var time = StaticHelpers.GetCurrentDateTime(); var time = StaticHelpers.GetCurrentDateTime().LocalDateTime;
var hour = time.Hour; var hour = time.Hour;
return hour switch return hour switch
{ {