From 98c3bdea1a890ee70d5eb1e58473edf3563c4b6e Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 16 Feb 2020 16:43:37 +0100 Subject: [PATCH] Register many new types and properties in AngelScript. --- conanfile.py | 2 +- .../AngelScript/AngelScripResolver.cpp | 20 ++++-- .../Battling/RegisterBattleClass.cpp | 46 ++++++++++++++ .../Battling/RegisterBattleClass.hpp | 12 ++++ .../Battling/RegisterBattleLibrary.cpp | 26 +++++++- .../Battling/RegisterBattleLibrary.hpp | 1 + .../Battling/RegisterTurnChoices.cpp | 63 +++++++++++++++++++ .../Battling/RegisterTurnChoices.hpp | 13 ++++ .../AngelScript/TypeRegistry/RefCast.hpp | 10 +++ 9 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp create mode 100644 src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.hpp create mode 100644 src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.cpp create mode 100644 src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.hpp create mode 100644 src/ScriptResolving/AngelScript/TypeRegistry/RefCast.hpp diff --git a/conanfile.py b/conanfile.py index fdc1010..4272ce6 100644 --- a/conanfile.py +++ b/conanfile.py @@ -44,7 +44,7 @@ class PkmnLibConan(ConanFile): self.options["AngelScript"].link_std_statically = True def requirements(self): - self.requires("CreatureLib/54e366fc1b3a44b200de41421707bfe4802aaecb@creaturelib/master") + self.requires("CreatureLib/f3b5f9e8f929a10867512f71f54ff02448b58f5c@creaturelib/master") if self.options.script_handler == "angelscript": self.requires("AngelScript/2.34@AngelScript/Deukhoofd") else: diff --git a/src/ScriptResolving/AngelScript/AngelScripResolver.cpp b/src/ScriptResolving/AngelScript/AngelScripResolver.cpp index a9e33ce..bcdabd5 100644 --- a/src/ScriptResolving/AngelScript/AngelScripResolver.cpp +++ b/src/ScriptResolving/AngelScript/AngelScripResolver.cpp @@ -1,12 +1,16 @@ #include "AngelScripResolver.hpp" +#include +#include #include "../../../extern/angelscript_addons/scriptarray/scriptarray.h" #include "../../../extern/angelscript_addons/scripthandle/scripthandle.h" #include "../../../extern/angelscript_addons/scripthelper/scripthelper.h" #include "../../../extern/angelscript_addons/scriptstdstring/scriptstdstring.h" -#include #include "TypeRegistry/BasicScriptClass.hpp" +#include "TypeRegistry/Battling/RegisterBattleClass.hpp" +#include "TypeRegistry/Battling/RegisterBattleLibrary.hpp" #include "TypeRegistry/Battling/RegisterExecutingAttack.hpp" #include "TypeRegistry/Battling/RegisterPokemonClass.hpp" +#include "TypeRegistry/Battling/RegisterTurnChoices.hpp" #include "TypeRegistry/Library/RegisterGrowthRateTypes.hpp" #include "TypeRegistry/Library/RegisterItemTypes.hpp" #include "TypeRegistry/Library/RegisterMoveTypes.hpp" @@ -33,8 +37,7 @@ void AngelScripResolver::Initialize(CreatureLib::Battling::BattleLibrary* arg) { _engine->SetEngineProperty(asEP_AUTO_GARBAGE_COLLECT, false); _engine->SetEngineProperty(asEP_REQUIRE_ENUM_SCOPE, true); _engine->SetEngineProperty(asEP_PROPERTY_ACCESSOR_MODE, 2); - _engine->SetEngineProperty(asEP_COMPILER_WARNINGS , 2); - + _engine->SetEngineProperty(asEP_COMPILER_WARNINGS, 2); RegisterStdString(_engine); @@ -69,6 +72,13 @@ void AngelScripResolver::RegisterTypes() { // Register battle types RegisterPokemonClass::Register(_engine); RegisterExecutingAttack::Register(_engine); + RegisterTurnChoices::Register(_engine); + RegisterBattleLibrary::Register(_engine); + RegisterBattleClass::Register(_engine); + [[maybe_unused]] int r = + _engine->RegisterObjectMethod("Pokemon", "const Battle@ get_Battle() const property", + asMETHOD(CreatureLib::Battling::Creature, GetBattle), asCALL_THISCALL); + assert(r >= 0); // Register base script BasicScriptClass::Register(_engine); @@ -97,8 +107,8 @@ void AngelScripResolver::MessageCallback(const asSMessageInfo* msg, void* param) printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message); } -static constexpr const char* GetCategoryNamespace(AngelScripResolver::ScriptCategory category){ - switch (category){ +static constexpr const char* GetCategoryNamespace(AngelScripResolver::ScriptCategory category) { + switch (category) { case CreatureLib::Battling::ScriptResolver::ScriptCategory::Attack: return "Moves"; case CreatureLib::Battling::ScriptResolver::ScriptCategory::Talent: return "Abilities"; case CreatureLib::Battling::ScriptResolver::ScriptCategory::Status: return "Status"; diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp new file mode 100644 index 0000000..81c4b37 --- /dev/null +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp @@ -0,0 +1,46 @@ +#include "RegisterBattleClass.hpp" +#include +#include +#include + +void RegisterBattleClass::Register(asIScriptEngine* engine) { + RegisterBattleRandom(engine); + RegisterBattle(engine); +} +void RegisterBattleClass::RegisterBattleRandom(asIScriptEngine* engine) { + [[maybe_unused]] int r = engine->RegisterObjectType("BattleRandom", 0, asOBJ_REF | asOBJ_NOCOUNT); + assert(r >= 0); + r = engine->RegisterObjectMethod("BattleRandom", + "bool EffectChance(int16 chance, ExecutingMove@ move, Pokemon@ target )", + asMETHOD(CreatureLib::Battling::BattleRandom, EffectChance), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("BattleRandom", "int Get()", + asMETHODPR(CreatureLib::Battling::BattleRandom, Get, (), int32_t), + asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("BattleRandom", "int Get(int max)", + asMETHODPR(CreatureLib::Battling::BattleRandom, Get, (int32_t), int32_t), + asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("BattleRandom", "int Get(int min, int max)", + asMETHODPR(CreatureLib::Battling::BattleRandom, Get, (int32_t, int32_t), int32_t), + asCALL_THISCALL); + assert(r >= 0); +} + +void RegisterBattleClass::RegisterBattle(asIScriptEngine* engine) { + [[maybe_unused]] int r = engine->RegisterObjectType("Battle", 0, asOBJ_REF | asOBJ_NOCOUNT); + assert(r >= 0); + r = engine->RegisterObjectMethod("Battle", "const BattleLibrary@ get_Library() const property", + asMETHOD(CreatureLib::Battling::Battle, GetLibrary), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("Battle", "bool CanUse(BaseTurnChoice@ choice)", + asMETHOD(CreatureLib::Battling::Battle, CanUse), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("Battle", "bool get_CanFlee() const property", + asMETHOD(CreatureLib::Battling::Battle, CanFlee), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("Battle", "BattleRandom& get_Random() const property", + asMETHOD(CreatureLib::Battling::Battle, GetRandom), asCALL_THISCALL); + assert(r >= 0); +} diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.hpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.hpp new file mode 100644 index 0000000..342250c --- /dev/null +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.hpp @@ -0,0 +1,12 @@ +#ifndef PKMNLIB_REGISTERBATTLECLASS_HPP +#define PKMNLIB_REGISTERBATTLECLASS_HPP + +#include +class RegisterBattleClass { + static void RegisterBattle(asIScriptEngine* engine); + static void RegisterBattleRandom(asIScriptEngine* engine); +public: + static void Register(asIScriptEngine* engine); +}; + +#endif // PKMNLIB_REGISTERBATTLECLASS_HPP diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleLibrary.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleLibrary.cpp index e08bef7..664e471 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleLibrary.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleLibrary.cpp @@ -1,9 +1,10 @@ #include "RegisterBattleLibrary.hpp" #include -#include "../../../../Battling/Library/DamageLibrary.hpp" +#include "../../../../Battling/Library/BattleLibrary.hpp" void RegisterBattleLibrary::Register(asIScriptEngine* engine) { RegisterDamageLibrary(engine); + RegisterLibrary(engine); } void RegisterBattleLibrary::RegisterDamageLibrary(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("DamageLibrary", 0, asOBJ_REF | asOBJ_NOCOUNT); @@ -12,3 +13,26 @@ void RegisterBattleLibrary::RegisterDamageLibrary(asIScriptEngine* engine) { asMETHOD(PkmnLib::Battling::DamageLibrary, GetDamage), asCALL_THISCALL); assert(r >= 0); } + +void RegisterBattleLibrary::RegisterLibrary(asIScriptEngine* engine) { + [[maybe_unused]] int r = engine->RegisterObjectType("BattleLibrary", 0, asOBJ_REF | asOBJ_NOCOUNT); + assert(r >= 0); + r = engine->RegisterObjectMethod("BattleLibrary", "const LibrarySettings@ get_Settings() const property", + asMETHOD(PkmnLib::Battling::BattleLibrary, GetSettings), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("BattleLibrary", "const StaticLibrary@ get_StaticLibrary() const property", + asMETHOD(PkmnLib::Battling::BattleLibrary, GetStaticLib), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("BattleLibrary", "const SpeciesLibrary@ get_SpeciesLibrary() const property", + asMETHOD(PkmnLib::Battling::BattleLibrary, GetSpeciesLibrary), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("BattleLibrary", "const MoveLibrary@ get_MoveLibrary() const property", + asMETHOD(PkmnLib::Battling::BattleLibrary, GetMoveLibrary), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("BattleLibrary", "const ItemLibrary@ get_ItemLibrary() const property", + asMETHOD(PkmnLib::Battling::BattleLibrary, GetItemLibrary), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("BattleLibrary", "const DamageLibrary@ get_DamageLibrary() const property", + asMETHOD(PkmnLib::Battling::BattleLibrary, GetDamageLibrary), asCALL_THISCALL); + assert(r >= 0); +} diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleLibrary.hpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleLibrary.hpp index 080fb62..a2baf0f 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleLibrary.hpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleLibrary.hpp @@ -4,6 +4,7 @@ #include class RegisterBattleLibrary { static void RegisterDamageLibrary(asIScriptEngine* engine); + static void RegisterLibrary(asIScriptEngine* engine); public: static void Register(asIScriptEngine* engine); }; diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.cpp new file mode 100644 index 0000000..123e1e4 --- /dev/null +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.cpp @@ -0,0 +1,63 @@ +#include "RegisterTurnChoices.hpp" +#include +#include +#include +#include "../RefCast.hpp" + +void RegisterTurnChoices::Register(asIScriptEngine* engine) { + RegisterTurnChoiceKindEnum(engine); + RegisterBaseTurnChoice(engine); + RegisterMoveTurnChoice(engine); +} +void RegisterTurnChoices::RegisterTurnChoiceKindEnum(asIScriptEngine* engine) { + [[maybe_unused]] int r = engine->RegisterEnum("TurnChoiceKind"); + assert(r >= 0); + r = engine->RegisterEnumValue("TurnChoiceKind", "Pass", (int)CreatureLib::Battling::TurnChoiceKind::Pass); + assert(r >= 0); + r = engine->RegisterEnumValue("TurnChoiceKind", "Attack", (int)CreatureLib::Battling::TurnChoiceKind::Attack); + assert(r >= 0); + r = engine->RegisterEnumValue("TurnChoiceKind", "Item", (int)CreatureLib::Battling::TurnChoiceKind::Item); + assert(r >= 0); + r = engine->RegisterEnumValue("TurnChoiceKind", "Switch", (int)CreatureLib::Battling::TurnChoiceKind::Switch); + assert(r >= 0); + r = engine->RegisterEnumValue("TurnChoiceKind", "Flee", (int)CreatureLib::Battling::TurnChoiceKind::Flee); + assert(r >= 0); +} +void RegisterTurnChoices::RegisterBaseTurnChoice(asIScriptEngine* engine) { + [[maybe_unused]] int r = engine->RegisterObjectType("BaseTurnChoice", 0, asOBJ_REF | asOBJ_NOCOUNT); + assert(r >= 0); + r = engine->RegisterObjectMethod("BaseTurnChoice", "TurnChoiceKind get_Kind() const property", + asMETHOD(CreatureLib::Battling::BaseTurnChoice, GetKind), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("BaseTurnChoice", "Pokemon@ get_User() const property", + asMETHOD(CreatureLib::Battling::BaseTurnChoice, GetUser), asCALL_THISCALL); + assert(r >= 0); +} + +void RegisterTurnChoices::RegisterMoveTurnChoice(asIScriptEngine* engine) { + [[maybe_unused]] int r = engine->RegisterObjectType("MoveTurnChoice", 0, asOBJ_REF | asOBJ_NOCOUNT); + assert(r >= 0); + r = engine->RegisterObjectMethod("MoveTurnChoice", "TurnChoiceKind get_Kind() const property", + asMETHOD(CreatureLib::Battling::AttackTurnChoice, GetKind), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("MoveTurnChoice", "Pokemon@ get_User() const property", + asMETHOD(CreatureLib::Battling::AttackTurnChoice, GetUser), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("MoveTurnChoice", "LearnedMove@ get_Move() const property", + asMETHOD(CreatureLib::Battling::AttackTurnChoice, GetAttack), asCALL_THISCALL); + assert(r >= 0); + r = engine->RegisterObjectMethod("MoveTurnChoice", "int8 get_Priority() const property", + asMETHOD(CreatureLib::Battling::AttackTurnChoice, GetPriority), asCALL_THISCALL); + assert(r >= 0); + + r = engine->RegisterObjectMethod( + "BaseTurnChoice", "MoveTurnChoice@ opCast()", + asFUNCTION((refCast)), + asCALL_CDECL_OBJLAST); + assert(r >= 0); + r = engine->RegisterObjectMethod( + "MoveTurnChoice", "BaseTurnChoice@ opImplCast()", + asFUNCTION((refCast)), + asCALL_CDECL_OBJLAST); + assert(r >= 0); +} diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.hpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.hpp new file mode 100644 index 0000000..71cec61 --- /dev/null +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterTurnChoices.hpp @@ -0,0 +1,13 @@ +#ifndef PKMNLIB_REGISTERTURNCHOICES_HPP +#define PKMNLIB_REGISTERTURNCHOICES_HPP + +#include +class RegisterTurnChoices { + static void RegisterTurnChoiceKindEnum(asIScriptEngine* engine); + static void RegisterBaseTurnChoice(asIScriptEngine* engine); + static void RegisterMoveTurnChoice(asIScriptEngine* engine); +public: + static void Register(asIScriptEngine* engine); +}; + +#endif // PKMNLIB_REGISTERTURNCHOICES_HPP diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/RefCast.hpp b/src/ScriptResolving/AngelScript/TypeRegistry/RefCast.hpp new file mode 100644 index 0000000..b38b884 --- /dev/null +++ b/src/ScriptResolving/AngelScript/TypeRegistry/RefCast.hpp @@ -0,0 +1,10 @@ +#ifndef PKMNLIB_REFCAST_HPP +#define PKMNLIB_REFCAST_HPP +template +B* refCast(A* a) +{ + if( !a ) return 0; + B* b = dynamic_cast(a); + return b; +} +#endif // PKMNLIB_REFCAST_HPP