PkmnLib.NET/Plugins/PkmnLib.Plugin.Gen7/Libraries/Gen7CaptureLibrary.cs

49 lines
1.5 KiB
C#
Raw Normal View History

2025-01-10 10:58:23 +00:00
using System;
using PkmnLib.Dynamic.Libraries;
using PkmnLib.Static;
namespace PkmnLib.Plugin.Gen7.Libraries;
public class Gen7CaptureLibrary : ICaptureLibrary
{
/// <inheritdoc />
public CaptureResult TryCapture(IPokemon target, IItem captureItem, IBattleRandom random)
{
var maxHealth = target.BoostedStats.Hp;
var currentHealth = target.CurrentHealth;
var catchRate = target.Species.CaptureRate;
byte bonusBall = 1;
if (target.Library.ScriptResolver.TryResolveBattleItemScript(captureItem, out var script) &&
script is PokeballScript pokeballScript)
{
bonusBall = pokeballScript.GetCatchRate(target);
}
byte bonusStatus = 1;
target.RunScriptHook(x => x.ChangeCatchRateBonus(target, captureItem, ref bonusStatus));
2025-03-02 16:19:57 +00:00
var modifiedCatchRate = (3.0 * maxHealth - 2.0 * currentHealth) * catchRate * bonusBall / (3.0 * maxHealth);
2025-01-10 10:58:23 +00:00
modifiedCatchRate *= bonusStatus;
2025-03-02 16:19:57 +00:00
var shakeProbability = 65536 / Math.Pow(255 / modifiedCatchRate, 0.1875);
2025-01-10 10:58:23 +00:00
byte shakes = 0;
if (modifiedCatchRate >= 255)
{
shakes = 4;
}
else
{
// FIXME: Implement critical capture
for (var i = 0; i < 4; i++)
{
if (random.GetInt(0, 65536) < shakeProbability)
{
shakes++;
}
}
}
var success = shakes >= 3;
return new CaptureResult(success, shakes, false);
}
}