From 6def68cf729341fefc01f0b63427e11706aa91be Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 8 May 2021 12:12:36 +0200 Subject: [PATCH] Adds Angelscript wrappers for Party. --- CMakeLists.txt.angelscript.in | 7 ++++ src/Battling/Pokemon/Pokemon.cpp | 19 +++++----- src/Battling/Pokemon/PokemonParty.hpp | 2 +- .../AngelScript/AngelScriptResolver.cpp | 2 ++ .../Battling/RegisterBattleClass.cpp | 19 +++++++++- .../TypeRegistry/Battling/RegisterParty.cpp | 36 +++++++++++++++++++ .../TypeRegistry/Battling/RegisterParty.hpp | 13 +++++++ 7 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterParty.cpp create mode 100644 src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterParty.hpp diff --git a/CMakeLists.txt.angelscript.in b/CMakeLists.txt.angelscript.in index 9c2ef89..c229e60 100644 --- a/CMakeLists.txt.angelscript.in +++ b/CMakeLists.txt.angelscript.in @@ -32,10 +32,17 @@ function(include_angelscript) SET(BUILD_SHARED_LIBS ${SHARED}) SET(LINK_STD_STATICALLY ${STATICC}) SET(CMAKE_BUILD_WITH_INSTALL_RPATH ON) + if (WINDOWS) + SET(MSVC 1) + endif() add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/Angelscript/src/AngelscriptProj/angelscript/projects/cmake ${CMAKE_CURRENT_BINARY_DIR}/Angelscript/bin EXCLUDE_FROM_ALL) + if (WINDOWS) + set_target_properties(angelscript PROPERTIES SUFFIX ".dll") + endif (WINDOWS) + execute_process(COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/Angelscript/include) include_directories(SYSTEM ${CMAKE_CURRENT_BINARY_DIR}/Angelscript/src/AngelscriptProj/angelscript/include) endfunction() \ No newline at end of file diff --git a/src/Battling/Pokemon/Pokemon.cpp b/src/Battling/Pokemon/Pokemon.cpp index a94feb0..55efc28 100644 --- a/src/Battling/Pokemon/Pokemon.cpp +++ b/src/Battling/Pokemon/Pokemon.cpp @@ -12,8 +12,8 @@ void PkmnLib::Battling::Pokemon::Evolve(ArbUt::BorrowedPtrGetGenderRate() != -1) { // If we are currently in battle, use the battle random so we can get predictable events. - if (_battle.HasValue()) { - _gender = _species->GetRandomGender(_battle.GetValue()->GetRandom()->GetRNG()); + if (_battleData.Battle.HasValue()) { + _gender = _species->GetRandomGender(_battleData.Battle.GetValue()->GetRandom()->GetRNG()); } // Else create a new random. else { @@ -34,8 +34,8 @@ void PkmnLib::Battling::Pokemon::SetStatus(const ArbUt::StringView& name) { } _statusScript = std::unique_ptr( _library->LoadScript(static_cast(PkmnScriptCategory::Status), name)); - if (_battle.HasValue()) { - _battle.GetValue()->TriggerEventListener(this, name); + if (_battleData.Battle.HasValue()) { + _battleData.Battle.GetValue()->TriggerEventListener(this, name); } } void PkmnLib::Battling::Pokemon::ClearStatus() { @@ -43,8 +43,8 @@ void PkmnLib::Battling::Pokemon::ClearStatus() { return; _statusScript->OnRemove(); _statusScript = nullptr; - if (_battle.HasValue()) { - _battle.GetValue()->TriggerEventListener(this, ""_cnc); + if (_battleData.Battle.HasValue()) { + _battleData.Battle.GetValue()->TriggerEventListener(this, ""_cnc); } } @@ -69,9 +69,10 @@ CreatureLib::Battling::Creature* PkmnLib::Battling::Pokemon::Clone() const { c->_statBoost = _statBoost; c->_flatStats = _flatStats; c->_boostedStats = _boostedStats; - c->_battle = _battle; - c->_side = _side; - c->_onBattleField = _onBattleField; + c->_battleData.Battle = _battleData.Battle; + c->_battleData.Side = _battleData.Side; + c->_battleData.OnBattleField = _battleData.OnBattleField; + c->_battleData.Index = _battleData.Index; if (_activeTalent != nullptr) { c->_activeTalent = std::unique_ptr(_activeTalent->Clone()); } diff --git a/src/Battling/Pokemon/PokemonParty.hpp b/src/Battling/Pokemon/PokemonParty.hpp index c795fba..6125646 100644 --- a/src/Battling/Pokemon/PokemonParty.hpp +++ b/src/Battling/Pokemon/PokemonParty.hpp @@ -12,7 +12,7 @@ namespace PkmnLib::Battling { : CreatureLib::Battling::CreatureParty(party) {} PokemonParty(size_t size) : CreatureLib::Battling::CreatureParty(size) {} - ArbUt::OptionalBorrowedPtr GetAtIndex(int index) const { + ArbUt::OptionalBorrowedPtr GetAtIndex(size_t index) const { return CreatureLib::Battling::CreatureParty::GetAtIndex(index).As(); } diff --git a/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp b/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp index d66986e..692552b 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp +++ b/src/ScriptResolving/AngelScript/AngelScriptResolver.cpp @@ -13,6 +13,7 @@ #include "TypeRegistry/Battling/RegisterBattleClass.hpp" #include "TypeRegistry/Battling/RegisterBattleLibrary.hpp" #include "TypeRegistry/Battling/RegisterExecutingAttack.hpp" +#include "TypeRegistry/Battling/RegisterParty.hpp" #include "TypeRegistry/Battling/RegisterPokemonClass.hpp" #include "TypeRegistry/Battling/RegisterTurnChoices.hpp" #include "TypeRegistry/ConstString.hpp" @@ -120,6 +121,7 @@ void AngelScriptResolver::RegisterTypes() { Ensure(r >= 0); RegisterPokemonClass::Register(_engine); + RegisterParty::Register(_engine); RegisterExecutingAttack::Register(_engine); RegisterTurnChoices::Register(_engine); RegisterBattleLibrary::Register(_engine); diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp index 16325bf..65ac6e2 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp @@ -49,11 +49,22 @@ void RegisterBattleClass::RegisterBattleRandom(asIScriptEngine* engine) { } BORROWED_PTR_GETTER_FUNC(PkmnLib::Battling::Battle, CreatureLib::Battling::ChoiceQueue, GetCurrentTurnQueue); - CreatureLib::Battling::BattleSide* GetBattleSideWrapper(PkmnLib::Battling::Battle* battle, u8 index) { return battle->GetSides()[index]; } +CreatureLib::Battling::BattleParty* GetPartyWrapper(PkmnLib::Battling::Battle* battle, u8 index) { + return battle->GetParties()[index]; +} + +CreatureLib::Battling::BattleParty* FindPartyWrapper(PkmnLib::Battling::Battle* battle, PkmnLib::Battling::Pokemon* p) { + auto v = battle->FindPartyForCreature(p); + if (v.HasValue()) { + return v.GetValue(); + } + return {}; +} + static CScriptHandle AddVolatileWrapper(PkmnLib::Battling::Battle* obj, const ArbUt::StringView& name) { auto handle = CScriptHandle(); auto* resolver = static_cast(obj->GetLibrary()->GetScriptResolver().get()); @@ -117,4 +128,10 @@ void RegisterBattleClass::RegisterBattle(asIScriptEngine* engine) { r = engine->RegisterObjectMethod("Battle", "BattleSide@ GetBattleSide(uint8 index)", asFUNCTION(GetBattleSideWrapper), asCALL_CDECL_OBJFIRST); Ensure(r >= 0); + r = engine->RegisterObjectMethod("Battle", "Party@ GetParty(uint8 index)", asFUNCTION(GetPartyWrapper), + asCALL_CDECL_OBJFIRST); + Ensure(r >= 0); + r = engine->RegisterObjectMethod("Battle", "Party@ FindPartyForPokemon(Pokemon@ pokemon)", + asFUNCTION(FindPartyWrapper), asCALL_CDECL_OBJFIRST); + Ensure(r >= 0); } diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterParty.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterParty.cpp new file mode 100644 index 0000000..e60048a --- /dev/null +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterParty.cpp @@ -0,0 +1,36 @@ +#include "RegisterParty.hpp" +#include "../../../../Battling/Pokemon/PokemonParty.hpp" +#include "../HelperFile.hpp" +#include + +void RegisterParty::Register(asIScriptEngine* engine) { + RegisterPartyClass(engine); + RegisterBattleParty(engine); +} + +static PkmnLib::Battling::Pokemon* GetAtIndexWrapper(PkmnLib::Battling::PokemonParty* obj, size_t i) { + auto v = obj->GetAtIndex(i); + if (v.HasValue()) { + return v.GetValue(); + } + return {}; +} + +void RegisterParty::RegisterPartyClass(asIScriptEngine* engine) { + auto r = engine->RegisterObjectType("Party", 0, asOBJ_REF | asOBJ_NOCOUNT); + Ensure(r >= 0); + r = engine->RegisterObjectMethod("Party", "Pokemon@ GetAtIndex(int index) const", asFUNCTION(GetAtIndexWrapper), + asCALL_CDECL_OBJFIRST); + Ensure(r >= 0); + r = engine->RegisterObjectMethod("Party", "int get_Length() const property", + asMETHOD(PkmnLib::Battling::PokemonParty, GetLength), asCALL_THISCALL); + Ensure(r >= 0); +} + +void RegisterParty::RegisterBattleParty(asIScriptEngine* engine) { + auto r = engine->RegisterObjectType("BattleParty", 0, asOBJ_REF | asOBJ_NOCOUNT); + Ensure(r >= 0); + r = engine->RegisterObjectMethod("BattleParty", "Party@ get_Party() const property", + asMETHOD(CreatureLib::Battling::BattleParty, GetParty), asCALL_THISCALL); + Ensure(r >= 0); +} diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterParty.hpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterParty.hpp new file mode 100644 index 0000000..d7c024c --- /dev/null +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterParty.hpp @@ -0,0 +1,13 @@ +#ifndef PKMNLIB_REGISTERPARTY_HPP +#define PKMNLIB_REGISTERPARTY_HPP + +#include +class RegisterParty { + static void RegisterPartyClass(asIScriptEngine* engine); + static void RegisterBattleParty(asIScriptEngine* engine); + +public: + static void Register(asIScriptEngine* engine); +}; + +#endif // PKMNLIB_REGISTERPARTY_HPP