Loads of work on capturing pokemon
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-03-26 16:23:24 +01:00
parent 1b14f31bd7
commit 09638c8d14
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
12 changed files with 110 additions and 13 deletions

View File

@ -6,9 +6,12 @@ export uint8_t PkmnLib_BattleLibrary_Construct(BattleLibrary*& out, PkmnLib::Lib
StatCalculator* statCalculator, DamageLibrary* damageLibrary, StatCalculator* statCalculator, DamageLibrary* damageLibrary,
ExperienceLibrary* experienceLibrary, ExperienceLibrary* experienceLibrary,
PkmnLib::Battling::ScriptResolver* scriptResolver, PkmnLib::Battling::ScriptResolver* scriptResolver,
MiscLibrary* miscLibrary) { MiscLibrary* miscLibrary, CaptureLibrary* captureLibrary) {
Try(out = new BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, scriptResolver, Try(out = new BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, scriptResolver,
miscLibrary)); miscLibrary, captureLibrary));
} }
export void PkmnLib_BattleLibrary_Destruct(BattleLibrary* p) { delete p; } export void PkmnLib_BattleLibrary_Destruct(BattleLibrary* p) { delete p; }
export uint8_t PkmnLib_CaptureLibrary_Construct(CaptureLibrary*& out) { Try(out = new CaptureLibrary()); }
export void PkmnLib_CaptureLibrary_Destruct(CaptureLibrary* p) { delete p; }

View File

@ -1,3 +1,4 @@
#include "../../src/Battling/EventHooks/CaptureAttemptEvent.hpp"
#include "../../src/Battling/EventHooks/WeatherChangeEvent.hpp" #include "../../src/Battling/EventHooks/WeatherChangeEvent.hpp"
#include "../Core.hpp" #include "../Core.hpp"
using namespace PkmnLib::Battling; using namespace PkmnLib::Battling;
@ -5,4 +6,13 @@ using namespace PkmnLib::Battling;
export void PkmnLib_WeatherChangeEvent_Destruct(WeatherChangeEvent* p) { delete p; } export void PkmnLib_WeatherChangeEvent_Destruct(WeatherChangeEvent* p) { delete p; }
export const char* PkmnLib_WeatherChangeEvent_GetWeatherName(WeatherChangeEvent* p) { export const char* PkmnLib_WeatherChangeEvent_GetWeatherName(WeatherChangeEvent* p) {
return p->GetWeatherName().c_str(); return p->GetWeatherName().c_str();
}
export void PkmnLib_CaptureAttemptEvent_Destruct(CaptureAttemptEvent* p) { delete p; }
export const PkmnLib::Battling::Pokemon* PkmnLib_CaptureAttemptEvent_GetPokemon(CaptureAttemptEvent* p) {
return p->GetPokemon();
}
export const PkmnLib::Battling::CaptureLibrary::CaptureResult
PkmnLib_CaptureAttemptEvent_GetResult(CaptureAttemptEvent* p) {
return p->GetResult();
} }

View File

@ -30,6 +30,7 @@ PkmnLib_Pokemon_Construct(Pokemon*& out, const BattleLibrary* library, const Pkm
export void PkmnLib_Pokemon_Destruct(const Pokemon* p) { delete p; } export void PkmnLib_Pokemon_Destruct(const Pokemon* p) { delete p; }
SIMPLE_GET_FUNC(Pokemon, IsShiny, bool) SIMPLE_GET_FUNC(Pokemon, IsShiny, bool)
SIMPLE_GET_FUNC(Pokemon, WasCaught, bool)
SIMPLE_GET_FUNC_SMART_PTR(Pokemon, GetNature, const PkmnLib::Library::Nature*) SIMPLE_GET_FUNC_SMART_PTR(Pokemon, GetNature, const PkmnLib::Library::Nature*)
export u8 PkmnLib_Pokemon_GetIndividualValue(const Pokemon* p, CreatureLib::Library::Statistic stat) { export u8 PkmnLib_Pokemon_GetIndividualValue(const Pokemon* p, CreatureLib::Library::Statistic stat) {
@ -55,4 +56,8 @@ export void PkmnLib_Pokemon_SetIsEgg(Pokemon* p, bool value) { p->SetIsEgg(value
export u8 PkmnLib_Pokemon_Evolve(Pokemon* p, const PkmnLib::Library::PokemonSpecies* species, export u8 PkmnLib_Pokemon_Evolve(Pokemon* p, const PkmnLib::Library::PokemonSpecies* species,
const PkmnLib::Library::PokemonForme* forme) { const PkmnLib::Library::PokemonForme* forme) {
Try(p->Evolve(species, forme);) Try(p->Evolve(species, forme);)
}
export u8 PkmnLib_Pokemon_AttemptCapture(Pokemon* p, PkmnLib::Library::Item* non_null catchItem) {
Try(p->AttemptCapture(catchItem);)
} }

View File

@ -0,0 +1,26 @@
#ifndef PKMNLIB_CAPTUREATTEMPTEVENT_HPP
#define PKMNLIB_CAPTUREATTEMPTEVENT_HPP
#include <CreatureLib/Battling/EventHooks/Events/EventData.hpp>
#include "../Pokemon/Pokemon.hpp"
#include "Arbutils/Memory/Memory.hpp"
#include "PkmnEventKind.hpp"
namespace PkmnLib::Battling {
class CaptureAttemptEvent final : public CreatureLib::Battling::EventData {
ArbUt::BorrowedPtr<PkmnLib::Battling::Pokemon> _pokemon;
CaptureLibrary::CaptureResult _result;
public:
explicit CaptureAttemptEvent(PkmnLib::Battling::Pokemon* pokemon, const CaptureLibrary::CaptureResult& result)
: _pokemon(pokemon), _result(result) {}
[[nodiscard]] CreatureLib::Battling::EventDataKind GetKind() const noexcept override {
return static_cast<CreatureLib::Battling::EventDataKind>(PkmnEventDataKind::CaptureAttempt);
}
const ArbUt::BorrowedPtr<PkmnLib::Battling::Pokemon>& GetPokemon() const noexcept { return _pokemon; }
const CaptureLibrary::CaptureResult& GetResult() const noexcept { return _result; }
};
}
#endif // PKMNLIB_CAPTUREATTEMPTEVENT_HPP

View File

@ -2,6 +2,6 @@
#define PKMNLIB_PKMNEVENTKIND_HPP #define PKMNLIB_PKMNEVENTKIND_HPP
#include <CreatureLib/Battling/EventHooks/EventDataKind.hpp> #include <CreatureLib/Battling/EventHooks/EventDataKind.hpp>
ENUM_WITH_START_VALUE(PkmnEventDataKind, u8, 128, WeatherChange) ENUM_WITH_START_VALUE(PkmnEventDataKind, u8, 128, WeatherChange, CaptureAttempt)
#endif // PKMNLIB_PKMNEVENTKIND_HPP #endif // PKMNLIB_PKMNEVENTKIND_HPP

View File

@ -3,6 +3,7 @@
#include <CreatureLib/Battling/Library/BattleLibrary.hpp> #include <CreatureLib/Battling/Library/BattleLibrary.hpp>
#include "../../Library/PokemonLibrary.hpp" #include "../../Library/PokemonLibrary.hpp"
#include "CaptureLibrary.hpp"
#include "DamageLibrary.hpp" #include "DamageLibrary.hpp"
#include "ExperienceLibrary.hpp" #include "ExperienceLibrary.hpp"
#include "MiscLibrary.hpp" #include "MiscLibrary.hpp"
@ -11,13 +12,16 @@
namespace PkmnLib::Battling { namespace PkmnLib::Battling {
class BattleLibrary final : public CreatureLib::Battling::BattleLibrary { class BattleLibrary final : public CreatureLib::Battling::BattleLibrary {
std::unique_ptr<const CaptureLibrary> _captureLibrary;
public: public:
BattleLibrary(Library::PokemonLibrary* non_null staticLib, StatCalculator* non_null statCalculator, BattleLibrary(Library::PokemonLibrary* non_null staticLib, StatCalculator* non_null statCalculator,
DamageLibrary* non_null damageLibrary, DamageLibrary* non_null damageLibrary, ExperienceLibrary* non_null experienceLibrary,
PkmnLib::Battling::ExperienceLibrary* non_null experienceLibrary, ScriptResolver* non_null scriptResolver, MiscLibrary* non_null miscLibrary,
ScriptResolver* non_null scriptResolver, PkmnLib::Battling::MiscLibrary* non_null miscLibrary) CaptureLibrary* non_null captureLibrary)
: CreatureLib::Battling::BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, : CreatureLib::Battling::BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary,
scriptResolver, miscLibrary) {} scriptResolver, miscLibrary),
_captureLibrary(captureLibrary) {}
const std::unique_ptr<const Library::LibrarySettings>& GetSettings() const { const std::unique_ptr<const Library::LibrarySettings>& GetSettings() const {
return reinterpret_cast<const std::unique_ptr<const Library::LibrarySettings>&>(_staticLib->GetSettings()); return reinterpret_cast<const std::unique_ptr<const Library::LibrarySettings>&>(_staticLib->GetSettings());
@ -43,6 +47,9 @@ namespace PkmnLib::Battling {
const ArbUt::BorrowedPtr<const Library::NatureLibrary> GetNatureLibrary() const { const ArbUt::BorrowedPtr<const Library::NatureLibrary> GetNatureLibrary() const {
return GetStaticLib()->GetNatureLibrary(); return GetStaticLib()->GetNatureLibrary();
} }
[[nodiscard]] inline const ArbUt::BorrowedPtr<const CaptureLibrary> GetCaptureLibrary() const noexcept {
return _captureLibrary;
}
static PkmnLib::Battling::ScriptResolver* non_null CreateScriptResolver(); static PkmnLib::Battling::ScriptResolver* non_null CreateScriptResolver();
}; };

View File

@ -1,10 +1,11 @@
#include "CaptureLibrary.hpp" #include "CaptureLibrary.hpp"
#include "../PkmnItemUseScript.hpp" #include "../PkmnItemUseScript.hpp"
#include "../PkmnScriptHook.hpp" #include "../PkmnScriptHook.hpp"
#include "../Pokemon/Pokemon.hpp"
namespace PkmnLib::Battling { namespace PkmnLib::Battling {
CaptureLibrary::CaptureResult CaptureLibrary::TryCatch(Pokemon* non_null pokemon, Library::Item* non_null catchItem, CaptureLibrary::CaptureResult CaptureLibrary::TryCatch(Pokemon* non_null pokemon, Library::Item* non_null catchItem,
ArbUt::Random* non_null random) const { CreatureLib::Battling::BattleRandom* non_null random) const {
auto hpMax = pokemon->GetMaxHealth(); auto hpMax = pokemon->GetMaxHealth();
auto hpCurrent = pokemon->GetCurrentHealth(); auto hpCurrent = pokemon->GetCurrentHealth();
auto rate = pokemon->GetSpecies()->GetCaptureRate(); auto rate = pokemon->GetSpecies()->GetCaptureRate();

View File

@ -1,9 +1,13 @@
#ifndef PKMNLIB_CAPTURELIBRARY_HPP #ifndef PKMNLIB_CAPTURELIBRARY_HPP
#define PKMNLIB_CAPTURELIBRARY_HPP #define PKMNLIB_CAPTURELIBRARY_HPP
#include <CreatureLib/Battling/Models/BattleRandom.hpp>
#include <CreatureLib/Defines.hpp> #include <CreatureLib/Defines.hpp>
#include "../Pokemon/Pokemon.hpp" #include "../../Library/Items/Item.hpp"
namespace PkmnLib::Battling { namespace PkmnLib::Battling {
class Pokemon;
class CaptureLibrary { class CaptureLibrary {
public: public:
struct CaptureResult { struct CaptureResult {
@ -13,7 +17,7 @@ namespace PkmnLib::Battling {
}; };
CaptureResult TryCatch(Pokemon* non_null pokemon, Library::Item* non_null catchItem, CaptureResult TryCatch(Pokemon* non_null pokemon, Library::Item* non_null catchItem,
ArbUt::Random* non_null random) const; CreatureLib::Battling::BattleRandom* non_null random) const;
}; };
} }

View File

@ -1,5 +1,6 @@
#include "Pokemon.hpp" #include "Pokemon.hpp"
#include <CreatureLib/Battling/Models/Battle.hpp> #include <CreatureLib/Battling/Models/Battle.hpp>
#include "../EventHooks/CaptureAttemptEvent.hpp"
#include "../PkmnScriptCategory.hpp" #include "../PkmnScriptCategory.hpp"
void PkmnLib::Battling::Pokemon::Evolve(ArbUt::BorrowedPtr<const Library::PokemonSpecies> mon, void PkmnLib::Battling::Pokemon::Evolve(ArbUt::BorrowedPtr<const Library::PokemonSpecies> mon,
@ -71,5 +72,30 @@ bool PkmnLib::Battling::Pokemon::IsUsable() const noexcept {
if (IsEgg()) { if (IsEgg()) {
return false; return false;
} }
if (_wasCaught) {
return false;
}
return Creature::IsUsable(); return Creature::IsUsable();
} }
void PkmnLib::Battling::Pokemon::AttemptCapture(PkmnLib::Library::Item* catchItem) {
Ensure(_battleData.OnBattleField);
Ensure(_battleData.Battle.HasValue());
Ensure(_battleData.Side.HasValue());
Ensure(!IsFainted());
Ensure(IsUsable());
Ensure(!GetBattleSide().GetValue()->IsSlotUnfillabe(this)) auto captureLibrary =
GetLibrary().ForceAs<const BattleLibrary>()->GetCaptureLibrary();
auto result = captureLibrary->TryCatch(this, catchItem, _battleData.Battle.GetValue()->GetRandom());
_battleData.Battle.GetValue()->TriggerEventListener<CaptureAttemptEvent>(this, result);
if (result.WasCaught) {
// By marking the pokemon as caught, it becomes no longer usable for switch in.
_wasCaught = true;
// As the pokemon is caught now, we replace it with an empty space.
_battleData.Side.GetValue()->SetCreature(nullptr, _battleData.Index.GetCreatureIndex());
}
}
void PkmnLib::Battling::Pokemon::ClearBattleData() noexcept {
Creature::ClearBattleData();
_wasCaught = false;
}

View File

@ -4,6 +4,7 @@
#include <CreatureLib/Battling/Models/Creature.hpp> #include <CreatureLib/Battling/Models/Creature.hpp>
#include "../../Library/Statistic.hpp" #include "../../Library/Statistic.hpp"
#include "../Library/BattleLibrary.hpp" #include "../Library/BattleLibrary.hpp"
#include "../Library/CaptureLibrary.hpp"
#include "../PkmnScript.hpp" #include "../PkmnScript.hpp"
#include "LearnedMove.hpp" #include "LearnedMove.hpp"
@ -16,6 +17,7 @@ namespace PkmnLib::Battling {
ArbUt::BorrowedPtr<const PkmnLib::Library::Nature> _nature; ArbUt::BorrowedPtr<const PkmnLib::Library::Nature> _nature;
u8 _friendship = 0; u8 _friendship = 0;
bool _isEgg; bool _isEgg;
bool _wasCaught = {};
public: public:
Pokemon(ArbUt::BorrowedPtr<const BattleLibrary> library, Pokemon(ArbUt::BorrowedPtr<const BattleLibrary> library,
@ -81,6 +83,15 @@ namespace PkmnLib::Battling {
_friendship = newValue; _friendship = newValue;
} }
/// @brief Attempt to capture the Pokemon.
/// @param catchItem The item used to try and catch the Pokemon with (generally a pokeball).
/// @warning This requires the Pokemon to be on the battle field, not fainted, and usable (so not already caught
/// and not an egg).
void AttemptCapture(Library::Item* non_null catchItem);
inline bool WasCaught() const noexcept { return _wasCaught; }
void ClearBattleData() noexcept override;
bool IsUsable() const noexcept override; bool IsUsable() const noexcept override;
Creature* non_null Clone() const override; Creature* non_null Clone() const override;

View File

@ -6,7 +6,9 @@
#include "../TestLibrary/TestLibrary.hpp" #include "../TestLibrary/TestLibrary.hpp"
#define AS_CLASS(name, contents) \ #define AS_CLASS(name, contents) \
{ #name, "namespace Pokemon{ [Pokemon effect=" #name "] shared class " #name " : PkmnScript { " contents "}}" } { \
#name, "namespace Pokemon{ [Pokemon effect=" #name "] shared class " #name " : PkmnScript { " contents "}}" \
}
static std::unordered_map<const char*, const char*> _scripts = std::unordered_map<const char*, const char*>{ static std::unordered_map<const char*, const char*> _scripts = std::unordered_map<const char*, const char*>{
AS_CLASS(blankScript, ), AS_CLASS(blankScript, ),
@ -387,7 +389,8 @@ TEST_CASE("Add Volatile with return script function") {
auto resolver = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto resolver = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
auto mainLib = new PkmnLib::Battling::BattleLibrary( auto mainLib = new PkmnLib::Battling::BattleLibrary(
TestLibrary::BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(), TestLibrary::BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(),
new PkmnLib::Battling::ExperienceLibrary(), resolver, new PkmnLib::Battling::MiscLibrary(GetTime)); new PkmnLib::Battling::ExperienceLibrary(), resolver, new PkmnLib::Battling::MiscLibrary(GetTime),
new PkmnLib::Battling::CaptureLibrary());
resolver->Initialize(mainLib); resolver->Initialize(mainLib);
for (auto kv : _scripts) { for (auto kv : _scripts) {
resolver->CreateScript(kv.first, kv.second); resolver->CreateScript(kv.first, kv.second);

View File

@ -27,7 +27,8 @@ public:
auto scriptResolver = PkmnLib::Battling::BattleLibrary::CreateScriptResolver(); auto scriptResolver = PkmnLib::Battling::BattleLibrary::CreateScriptResolver();
auto lib = new PkmnLib::Battling::BattleLibrary( auto lib = new PkmnLib::Battling::BattleLibrary(
BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(false), BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(false),
new PkmnLib::Battling::ExperienceLibrary(), scriptResolver, new PkmnLib::Battling::MiscLibrary(GetTime)); new PkmnLib::Battling::ExperienceLibrary(), scriptResolver, new PkmnLib::Battling::MiscLibrary(GetTime),
new PkmnLib::Battling::CaptureLibrary());
scriptResolver->Initialize(lib); scriptResolver->Initialize(lib);
return lib; return lib;
} }