This commit is contained in:
282
PkmnLib.Dynamic/BattleFlow/MoveTurnExecutor.cs
Normal file
282
PkmnLib.Dynamic/BattleFlow/MoveTurnExecutor.cs
Normal file
@@ -0,0 +1,282 @@
|
||||
using PkmnLib.Dynamic.Events;
|
||||
using PkmnLib.Dynamic.Models;
|
||||
using PkmnLib.Dynamic.Models.Choices;
|
||||
using PkmnLib.Dynamic.ScriptHandling;
|
||||
using PkmnLib.Static;
|
||||
using PkmnLib.Static.Moves;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dynamic.BattleFlow;
|
||||
|
||||
/// <summary>
|
||||
/// Helper class for executing moves.
|
||||
/// </summary>
|
||||
public static class MoveTurnExecutor
|
||||
{
|
||||
internal static void ExecuteMoveChoice(IBattle battle, IMoveChoice moveChoice)
|
||||
{
|
||||
var chosenMove = moveChoice.ChosenMove;
|
||||
var useMove = chosenMove.MoveData;
|
||||
|
||||
var moveDataName = useMove.Name;
|
||||
moveChoice.RunScriptHook(x => x.ChangeMove(moveChoice, ref moveDataName));
|
||||
if (useMove.Name != moveDataName)
|
||||
{
|
||||
if (!battle.Library.StaticLibrary.Moves.TryGet(moveDataName, out useMove))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"The move was changed to '{moveDataName}' by a script, but this move does not exist.");
|
||||
}
|
||||
var secondaryEffect = useMove.SecondaryEffect;
|
||||
if (secondaryEffect != null)
|
||||
{
|
||||
if (moveChoice.User.Library.ScriptResolver.TryResolve(ScriptCategory.Move, secondaryEffect.Name,
|
||||
secondaryEffect.Parameters, out var script))
|
||||
{
|
||||
moveChoice.Script.Set(script);
|
||||
script.OnAddedToParent(moveChoice);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveChoice.Script.Clear();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
moveChoice.Script.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
var targetType = useMove.Target;
|
||||
var targets =
|
||||
TargetResolver.ResolveTargets(battle, moveChoice.TargetSide, moveChoice.TargetPosition, targetType);
|
||||
moveChoice.RunScriptHook(x => x.ChangeTargets(moveChoice, ref targets));
|
||||
if (targets.Count == 0)
|
||||
{
|
||||
moveChoice.Fail();
|
||||
return;
|
||||
}
|
||||
|
||||
var targetSide = battle.Sides[moveChoice.TargetSide];
|
||||
targetSide.RunScriptHook(x => x.ChangeIncomingTargets(moveChoice, ref targets));
|
||||
|
||||
byte numberOfHits = 1;
|
||||
moveChoice.RunScriptHook(x => x.ChangeNumberOfHits(moveChoice, ref numberOfHits));
|
||||
if (numberOfHits == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var executingMove = new ExecutingMoveImpl(targets, numberOfHits, chosenMove, useMove, moveChoice, battle);
|
||||
|
||||
var prevented = false;
|
||||
executingMove.RunScriptHook(x => x.PreventMove(executingMove, ref prevented));
|
||||
if (prevented)
|
||||
return;
|
||||
|
||||
byte ppUsed = 1;
|
||||
// TODO: Modify the PP used by the move.
|
||||
if (!executingMove.ChosenMove.TryUse(ppUsed))
|
||||
return;
|
||||
|
||||
battle.EventHook.Invoke(new MoveUseEvent(executingMove));
|
||||
|
||||
var failed = false;
|
||||
executingMove.RunScriptHook(x => x.FailMove(executingMove, ref failed));
|
||||
if (failed)
|
||||
{
|
||||
// TODO: fail handling
|
||||
executingMove.MoveChoice.Fail();
|
||||
return;
|
||||
}
|
||||
ExecuteMove(executingMove);
|
||||
|
||||
if (executingMove.Hits.All(x => x.HasFailed) || (executingMove.UseMove.Category != MoveCategory.Status &&
|
||||
executingMove.Hits.All(x => x.Damage == 0)))
|
||||
{
|
||||
executingMove.MoveChoice.Fail();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the move for its targets.
|
||||
/// </summary>
|
||||
/// <param name="executingMove"></param>
|
||||
public static void ExecuteMove(IExecutingMove executingMove)
|
||||
{
|
||||
var stopped = false;
|
||||
executingMove.RunScriptHook(x => x.StopBeforeMove(executingMove, ref stopped));
|
||||
if (stopped)
|
||||
return;
|
||||
|
||||
executingMove.RunScriptHook(x => x.OnBeforeMove(executingMove));
|
||||
foreach (var target in executingMove.Targets.WhereNotNull())
|
||||
{
|
||||
ExecuteMoveChoiceForTarget(executingMove.Battle, executingMove, target);
|
||||
}
|
||||
executingMove.RunScriptHook(x => x.OnAfterMove(executingMove));
|
||||
}
|
||||
|
||||
private static void ExecuteMoveChoiceForTarget(IBattle battle, IExecutingMove executingMove, IPokemon target)
|
||||
{
|
||||
var failed = false;
|
||||
target.RunScriptHook(x => x.FailIncomingMove(executingMove, target, ref failed));
|
||||
if (failed)
|
||||
{
|
||||
// TODO: fail handling
|
||||
return;
|
||||
}
|
||||
|
||||
var isInvulnerable = false;
|
||||
target.RunScriptHook(x => x.IsInvulnerableToMove(executingMove, target, ref isInvulnerable));
|
||||
if (isInvulnerable)
|
||||
{
|
||||
// TODO: event?
|
||||
return;
|
||||
}
|
||||
|
||||
var numberOfHits = executingMove.NumberOfHits;
|
||||
var targetHitStat = executingMove.GetTargetIndex(target) * numberOfHits;
|
||||
|
||||
for (byte i = 0; i < numberOfHits; i++)
|
||||
{
|
||||
if (battle.HasEnded)
|
||||
break;
|
||||
if (executingMove.User.IsFainted)
|
||||
break;
|
||||
if (target.IsFainted)
|
||||
break;
|
||||
|
||||
var hitIndex = i;
|
||||
executingMove.RunScriptHook(x => x.OnBeforeHit(executingMove, target, hitIndex));
|
||||
var hitData = (HitData)executingMove.GetDataFromRawIndex(targetHitStat + i);
|
||||
if (hitData.HasFailed)
|
||||
break;
|
||||
|
||||
var useMove = executingMove.UseMove;
|
||||
var hitType = (TypeIdentifier?)useMove.MoveType;
|
||||
executingMove.RunScriptHook(x => x.ChangeMoveType(executingMove, target, hitIndex, ref hitType));
|
||||
|
||||
hitData.Type = hitType;
|
||||
|
||||
var types = target.Types.ToList();
|
||||
executingMove.RunScriptHook(x => x.ChangeTypesForMove(executingMove, target, hitIndex, types));
|
||||
target.RunScriptHook(x => x.ChangeTypesForIncomingMove(executingMove, target, hitIndex, types));
|
||||
|
||||
var effectiveness = hitType == null
|
||||
? 1
|
||||
: battle.Library.StaticLibrary.Types.GetEffectiveness(hitType.Value, types);
|
||||
executingMove.RunScriptHook(x => x.ChangeEffectiveness(executingMove, target, hitIndex, ref effectiveness));
|
||||
target.RunScriptHook(x =>
|
||||
x.ChangeIncomingEffectiveness(executingMove, target, hitIndex, ref effectiveness));
|
||||
hitData.Effectiveness = effectiveness;
|
||||
|
||||
var blockCritical = false;
|
||||
executingMove.RunScriptHook(x => x.BlockCriticalHit(executingMove, target, hitIndex, ref blockCritical));
|
||||
target.RunScriptHook(x => x.BlockIncomingCriticalHit(executingMove, target, hitIndex, ref blockCritical));
|
||||
if (!blockCritical)
|
||||
{
|
||||
var critical = battle.Library.DamageCalculator.IsCritical(battle, executingMove, target, hitIndex);
|
||||
hitData.IsCritical = critical;
|
||||
}
|
||||
|
||||
var basePower = battle.Library.DamageCalculator.GetBasePower(executingMove, target, hitIndex, hitData);
|
||||
hitData.BasePower = basePower;
|
||||
|
||||
hitData.Damage = battle.Library.DamageCalculator.GetDamage(executingMove, target, hitIndex, hitData);
|
||||
|
||||
var accuracy = useMove.Accuracy;
|
||||
// If the accuracy is 255, the move should always hit, and as such we should not allow
|
||||
// modifying it.
|
||||
if (accuracy != 255)
|
||||
{
|
||||
accuracy = battle.Library.StatCalculator.CalculateModifiedAccuracy(executingMove, target, hitIndex,
|
||||
accuracy);
|
||||
}
|
||||
|
||||
if (accuracy < 100 && battle.Random.GetInt(100) >= accuracy)
|
||||
{
|
||||
executingMove.RunScriptHook(x => x.OnMoveMiss(executingMove, target));
|
||||
battle.EventHook.Invoke(new MoveMissEvent(executingMove));
|
||||
break;
|
||||
}
|
||||
|
||||
var blockIncomingHit = false;
|
||||
target.RunScriptHook(x => x.BlockIncomingHit(executingMove, target, hitIndex, ref blockIncomingHit));
|
||||
executingMove.RunScriptHook(x => x.BlockOutgoingHit(executingMove, target, hitIndex, ref blockIncomingHit));
|
||||
if (blockIncomingHit)
|
||||
break;
|
||||
if (executingMove.GetHitData(target, hitIndex).HasFailed)
|
||||
break;
|
||||
var category = useMove.Category;
|
||||
executingMove.RunScriptHook(x => x.ChangeCategory(executingMove, target, hitIndex, ref category));
|
||||
if (category == MoveCategory.Status)
|
||||
{
|
||||
var secondaryEffect = useMove.SecondaryEffect;
|
||||
if (secondaryEffect != null)
|
||||
{
|
||||
var chance = secondaryEffect.Chance;
|
||||
if (chance < 0 || battle.Random.EffectChance(chance, executingMove, target, hitIndex))
|
||||
{
|
||||
executingMove.RunScriptHook(x => x.OnSecondaryEffect(executingMove, target, hitIndex));
|
||||
}
|
||||
}
|
||||
}
|
||||
// else if the move is a physical or special move, we should apply the damage.
|
||||
else
|
||||
{
|
||||
var currentHealth = target.CurrentHealth;
|
||||
if (hitData.Damage > currentHealth)
|
||||
{
|
||||
hitData.Damage = currentHealth;
|
||||
}
|
||||
var damage = hitData.Damage;
|
||||
if (damage > 0)
|
||||
{
|
||||
var hitEventBatch = new EventBatchId();
|
||||
battle.EventHook.Invoke(new MoveHitEvent(executingMove, hitData, target)
|
||||
{
|
||||
BatchId = hitEventBatch,
|
||||
});
|
||||
target.Damage(damage, DamageSource.MoveDamage, hitEventBatch);
|
||||
if (!target.IsFainted)
|
||||
target.RunScriptHook(x => x.OnIncomingHit(executingMove, target, hitIndex));
|
||||
else
|
||||
executingMove.RunScriptHook(x => x.OnOpponentFaints(executingMove, target, hitIndex));
|
||||
|
||||
if (!target.IsFainted)
|
||||
{
|
||||
var secondaryEffect = useMove.SecondaryEffect;
|
||||
if (secondaryEffect != null)
|
||||
{
|
||||
var preventSecondary = false;
|
||||
target.RunScriptHook(x =>
|
||||
x.PreventSecondaryEffect(executingMove, target, hitIndex, ref preventSecondary));
|
||||
|
||||
if (!preventSecondary)
|
||||
{
|
||||
var chance = secondaryEffect.Chance;
|
||||
if (chance < 0 || battle.Random.EffectChance(chance, executingMove, target, hitIndex))
|
||||
{
|
||||
executingMove.RunScriptHook(x =>
|
||||
x.OnSecondaryEffect(executingMove, target, hitIndex));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (numberOfHits == 0)
|
||||
{
|
||||
target.RunScriptHook(x => x.OnMoveMiss(executingMove, target));
|
||||
battle.EventHook.Invoke(new MoveMissEvent(executingMove));
|
||||
}
|
||||
|
||||
if (!executingMove.User.IsFainted)
|
||||
{
|
||||
executingMove.RunScriptHook(x => x.OnAfterHits(executingMove, target));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user