Implementation of Pokeballs
This commit is contained in:
53
Plugins/PkmnLib.Plugin.Gen7/Libraries/Gen7CaptureLibrary.cs
Normal file
53
Plugins/PkmnLib.Plugin.Gen7/Libraries/Gen7CaptureLibrary.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using PkmnLib.Dynamic.Libraries;
|
||||
using PkmnLib.Dynamic.Models;
|
||||
using PkmnLib.Dynamic.ScriptHandling;
|
||||
using PkmnLib.Dynamic.ScriptHandling.Registry;
|
||||
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));
|
||||
|
||||
var modifiedCatchRate =
|
||||
(((3.0 * maxHealth) - (2.0 * currentHealth)) * catchRate * bonusBall) / (3.0 * maxHealth);
|
||||
modifiedCatchRate *= bonusStatus;
|
||||
|
||||
var shakeProbability = 65536 / Math.Pow((255 / modifiedCatchRate), 0.1875);
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user