From 683f3ad08bd8fe8f8ac023418d5cea56c71caeb9 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 8 Aug 2020 12:01:05 +0200 Subject: [PATCH] Add Pokemon Status. --- CInterface/Battling/Pokemon.cpp | 8 +++--- src/Battling/EventHooks/PkmnEventKind.hpp | 2 +- src/Battling/EventHooks/StatusChangeEvent.hpp | 27 +++++++++++++++++++ src/Battling/PkmnScriptCategory.hpp | 2 +- src/Battling/Pokemon/CreatePokemon.cpp | 6 ++--- src/Battling/Pokemon/Pokemon.cpp | 20 ++++++++++++++ src/Battling/Pokemon/Pokemon.hpp | 11 ++++++-- .../AngelScript/AngelScriptResolver.cpp | 2 +- 8 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 src/Battling/EventHooks/StatusChangeEvent.hpp diff --git a/CInterface/Battling/Pokemon.cpp b/CInterface/Battling/Pokemon.cpp index 30a8c53..ec4d1d2 100644 --- a/CInterface/Battling/Pokemon.cpp +++ b/CInterface/Battling/Pokemon.cpp @@ -25,11 +25,13 @@ export void PkmnLib_Pokemon_Destruct(const Pokemon* p) { delete p; } SIMPLE_GET_FUNC(Pokemon, IsShiny, bool) SIMPLE_GET_FUNC_SMART_PTR(Pokemon, GetNature, const PkmnLib::Library::Nature*) -#undef SIMPLE_GET_FUNC - export uint8_t PkmnLib_Pokemon_GetIndividualValue(const Pokemon* p, CreatureLib::Library::Statistic stat) { return p->GetIndividualValue(stat); } export uint8_t PkmnLib_Pokemon_GetEffortValue(const Pokemon* p, CreatureLib::Library::Statistic stat) { return p->GetEffortValue(stat); -} \ No newline at end of file +} + +export uint8_t PkmnLib_Battle_SetStatus(Pokemon* p, const char* name) { Try(p->SetStatus(ArbUt::StringView(name))); }; +export uint8_t PkmnLib_Battle_ClearStatus(Pokemon* p) { Try(p->ClearStatus()); }; +export const char* PkmnLib_Battle_GetStatusName(Pokemon* p) { return p->GetStatusName().c_str(); } \ No newline at end of file diff --git a/src/Battling/EventHooks/PkmnEventKind.hpp b/src/Battling/EventHooks/PkmnEventKind.hpp index 399b450..b2c0a0c 100644 --- a/src/Battling/EventHooks/PkmnEventKind.hpp +++ b/src/Battling/EventHooks/PkmnEventKind.hpp @@ -3,6 +3,6 @@ #include #include -ENUM_WITH_START_VALUE(PkmnEventDataKind, uint8_t, 128, WeatherChange) +ENUM_WITH_START_VALUE(PkmnEventDataKind, uint8_t, 128, WeatherChange, StatusChange) #endif // PKMNLIB_PKMNEVENTKIND_HPP diff --git a/src/Battling/EventHooks/StatusChangeEvent.hpp b/src/Battling/EventHooks/StatusChangeEvent.hpp new file mode 100644 index 0000000..c989cc9 --- /dev/null +++ b/src/Battling/EventHooks/StatusChangeEvent.hpp @@ -0,0 +1,27 @@ +#ifndef PKMNLIB_STATUSCHANGEEVENT_HPP +#define PKMNLIB_STATUSCHANGEEVENT_HPP + +#include +#include +#include "../Pokemon/Pokemon.hpp" +#include "PkmnEventKind.hpp" + +namespace PkmnLib::Battling { + class StatusChangeEvent : public CreatureLib::Battling::EventData { + ArbUt::BorrowedPtr _pokemon; + ArbUt::StringView _statusName; + + public: + explicit StatusChangeEvent(const ArbUt::BorrowedPtr& pkmn, const ArbUt::StringView& name) + : _pokemon(pkmn), _statusName(name) {} + + [[nodiscard]] CreatureLib::Battling::EventDataKind GetKind() const noexcept override { + return static_cast(PkmnEventDataKind::StatusChange); + } + + const ArbUt::BorrowedPtr& GetPokemon() const noexcept { return _pokemon; } + const ArbUt::StringView& GetStatusName() const noexcept { return _statusName; } + }; +} + +#endif // PKMNLIB_STATUSCHANGEEVENT_HPP diff --git a/src/Battling/PkmnScriptCategory.hpp b/src/Battling/PkmnScriptCategory.hpp index c325d45..113ffad 100644 --- a/src/Battling/PkmnScriptCategory.hpp +++ b/src/Battling/PkmnScriptCategory.hpp @@ -3,6 +3,6 @@ #include #include -ENUM_WITH_START_VALUE(PkmnScriptCategory, uint8_t, ((uint8_t)ScriptCategoryHelper::Highest()) + 1, Weather) +ENUM_WITH_START_VALUE(PkmnScriptCategory, uint8_t, 128, Weather, Status) #endif // PKMNLIB_PKMNSCRIPTCATEGORY_HPP diff --git a/src/Battling/Pokemon/CreatePokemon.cpp b/src/Battling/Pokemon/CreatePokemon.cpp index 1a17f44..15898d2 100644 --- a/src/Battling/Pokemon/CreatePokemon.cpp +++ b/src/Battling/Pokemon/CreatePokemon.cpp @@ -50,7 +50,7 @@ namespace PkmnLib::Battling { } AssertNotNull(forme); CreatureLib::Library::TalentIndex ability; - if (this->_ability.Empty()) { + if (this->_ability.IsEmpty()) { ability = forme->GetRandomTalent(rand); } else { ability = forme->GetTalentIndex(this->_ability); @@ -64,7 +64,7 @@ namespace PkmnLib::Battling { gender = species->GetRandomGender(rand); } ArbUt::BorrowedPtr heldItem = nullptr; - if (!this->_heldItem.Empty()) { + if (!this->_heldItem.IsEmpty()) { if (!_library->GetItemLibrary()->TryGet(this->_heldItem, heldItem)) { THROW_CREATURE("Unknown Item: " << this->_heldItem.std_str()); } @@ -84,7 +84,7 @@ namespace PkmnLib::Battling { auto ivs = CreatureLib::Library::StatisticSet(_ivHp, _ivAttack, _ivDefense, _ivSpAtt, _ivSpDef, _ivSpeed); auto evs = CreatureLib::Library::StatisticSet(_evHp, _evAttack, _evDefense, _evSpAtt, _evSpDef, _evSpeed); - if (_nature.Empty()) { + if (_nature.IsEmpty()) { _nature = _library->GetNatureLibrary()->GetRandomNatureName(rand); } auto nature = _library->GetNatureLibrary()->GetNatureByName(_nature); diff --git a/src/Battling/Pokemon/Pokemon.cpp b/src/Battling/Pokemon/Pokemon.cpp index 0db5c23..b72a734 100644 --- a/src/Battling/Pokemon/Pokemon.cpp +++ b/src/Battling/Pokemon/Pokemon.cpp @@ -1,5 +1,7 @@ #include "Pokemon.hpp" #include +#include "../EventHooks/StatusChangeEvent.hpp" +#include "../PkmnScriptCategory.hpp" void PkmnLib::Battling::Pokemon::Evolve(ArbUt::BorrowedPtr mon, ArbUt::BorrowedPtr forme) { @@ -25,3 +27,21 @@ void PkmnLib::Battling::Pokemon::Evolve(ArbUt::BorrowedPtrOnRemove(); + } + _statusScript = std::unique_ptr( + _library->LoadScript(static_cast(PkmnScriptCategory::Status), name)); + if (_battle != nullptr) { + _battle->TriggerEventListener(this, name); + } +} +void PkmnLib::Battling::Pokemon::ClearStatus() { + _statusScript->OnRemove(); + _statusScript = nullptr; + if (_battle != nullptr) { + _battle->TriggerEventListener(this, ""_cnc); + } +} \ No newline at end of file diff --git a/src/Battling/Pokemon/Pokemon.hpp b/src/Battling/Pokemon/Pokemon.hpp index 18d8933..50087e5 100644 --- a/src/Battling/Pokemon/Pokemon.hpp +++ b/src/Battling/Pokemon/Pokemon.hpp @@ -14,8 +14,7 @@ namespace PkmnLib::Battling { CreatureLib::Library::StatisticSet _effortValues; ArbUt::BorrowedPtr _nature; - - ArbUt::BorrowedPtr GetLibrary() const { return _library.ForceAs(); } + std::unique_ptr _statusScript = nullptr; public: Pokemon(ArbUt::BorrowedPtr library, @@ -59,6 +58,14 @@ namespace PkmnLib::Battling { void Evolve(ArbUt::BorrowedPtr mon, ArbUt::BorrowedPtr forme); + + void SetStatus(const ArbUt::StringView& name); + void ClearStatus(); + const ArbUt::StringView& GetStatusName() noexcept { + if (_statusScript == nullptr) + return ArbUt::StringView::Empty(); + return _statusScript->GetName(); + } }; } diff --git a/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp b/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp index 156120f..f9de554 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp +++ b/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp @@ -173,7 +173,7 @@ void AngelScriptResolver::FinalizeModule() { effectName = ArbUt::StringView(val.c_str(), val.length()); } } - if (effectName.Empty()) { + if (effectName.IsEmpty()) { continue; } switch (metadataKind) {