PkmnLib.NET/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/ProtectionScript.cs

46 lines
1.4 KiB
C#
Raw Normal View History

2025-01-10 11:55:25 +00:00
using System;
using PkmnLib.Plugin.Gen7.Scripts.Pokemon;
namespace PkmnLib.Plugin.Gen7.Scripts.Moves;
2025-02-01 14:00:22 +00:00
[Script(ScriptCategory.Move, "protect")]
public class ProtectionScript : Script
2025-01-10 11:55:25 +00:00
{
2025-02-03 10:40:26 +00:00
protected virtual Script GetEffectScript() => new ProtectionEffectScript();
2025-02-01 14:00:22 +00:00
2025-01-10 11:55:25 +00:00
/// <inheritdoc />
public override void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
{
var battleData = target.BattleData;
if (battleData == null)
return;
// If the user goes last in the turn, the move will fail
if (battleData.Battle.ChoiceQueue is not null && !battleData.Battle.ChoiceQueue.HasNext())
{
move.GetHitData(target, hit).Fail();
return;
}
var failure = target.Volatile.Get<ProtectionFailureScript>();
if (failure == null)
{
failure = new ProtectionFailureScript();
target.Volatile.Add(failure);
}
var successive = failure.ProtectTurns;
// Each time, the chance of success is divided by 3.
var chance = 1.0 / Math.Pow(3, successive);
var random = target.BattleData!.Battle.Random.GetFloat();
if (random < chance)
{
failure.ProtectTurns++;
failure.UsedProtect = true;
target.Volatile.Add(GetEffectScript());
}
else
{
move.GetHitData(target, hit).Fail();
}
}
}