42 lines
1.5 KiB
C++
42 lines
1.5 KiB
C++
#include "CaptureLibrary.hpp"
|
|
#include "../PkmnItemUseScript.hpp"
|
|
#include "../PkmnScriptHook.hpp"
|
|
|
|
namespace PkmnLib::Battling {
|
|
CaptureLibrary::CaptureResult CaptureLibrary::TryCatch(Pokemon* non_null pokemon, Library::Item* non_null catchItem,
|
|
ArbUt::Random* non_null random) const {
|
|
auto hpMax = pokemon->GetMaxHealth();
|
|
auto hpCurrent = pokemon->GetCurrentHealth();
|
|
auto rate = pokemon->GetSpecies()->GetCaptureRate();
|
|
|
|
u8 bonusBall = 1;
|
|
auto* itemScript =
|
|
dynamic_cast<PkmnItemUseScript*>(pokemon->GetLibrary()->GetScriptResolver()->LoadItemScript(catchItem));
|
|
itemScript->ModifyPokeballCatchBonus(pokemon, &bonusBall);
|
|
|
|
u8 bonusStatus = 1;
|
|
PKMN_HOOK(ModifyCaptureRateBonus, pokemon, pokemon, catchItem, &bonusStatus);
|
|
|
|
auto modifiedCatchRate = (((3.0 * hpMax) - (2.0 * hpCurrent)) * rate * bonusBall) / (3.0 * hpMax);
|
|
modifiedCatchRate *= bonusStatus;
|
|
|
|
auto shakeProbability = 65536 / pow((255.0 / modifiedCatchRate), 0.1875);
|
|
|
|
u8 shakes = 0;
|
|
if (modifiedCatchRate >= 255) {
|
|
shakes = 4;
|
|
}
|
|
|
|
// FIXME: Implement critical capture
|
|
|
|
for (; shakes < 4; shakes++) {
|
|
auto randomNumber = random->GetUnsigned(65536);
|
|
if (randomNumber >= shakeProbability) {
|
|
break;
|
|
}
|
|
}
|
|
auto success = shakes == 4;
|
|
return CaptureResult{.WasCaught = success, .Shakes = shakes, .WasCritical = false};
|
|
}
|
|
}
|