PkmnLib/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp

185 lines
11 KiB
C++
Raw Normal View History

#include "RegisterPokemonClass.hpp"
#include <Arbutils/Assert.hpp>
2020-04-28 12:48:56 +00:00
#include <CreatureLib/Battling/Models/LearnedAttack.hpp>
2020-02-02 11:23:50 +00:00
#include "../../../../../extern/angelscript_addons/scriptarray/scriptarray.h"
#include "../../../../Battling/PkmnDamageSource.hpp"
2020-04-28 12:48:56 +00:00
#include "../../../../Battling/Pokemon/Pokemon.hpp"
2020-05-27 15:26:25 +00:00
#include "../HelperFile.hpp"
// Hack to handle AngelScript not recognizing different sized enums on fields, and returning invalid values due to it.
#define ENUM__SIZE_WRAPPER(name, type, func) \
int32_t name(type* obj) { return static_cast<int32_t>(obj->func()); }
void RegisterPokemonClass::Register(asIScriptEngine* engine) {
RegisterDamageSource(engine);
RegisterMoveLearnMethod(engine);
RegisterLearnedAttack(engine);
RegisterPokemonType(engine);
}
void RegisterPokemonClass::RegisterDamageSource(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterEnum("DamageSource");
Assert(r >= 0);
2020-04-28 12:48:56 +00:00
for (auto v : CreatureLib::Battling::DamageSourceHelper::GetValues()) {
r = engine->RegisterEnumValue("DamageSource", CreatureLib::Battling::DamageSourceHelper::ToString(v), (int)v);
}
2020-04-28 12:48:56 +00:00
for (auto v : PkmnDamageSourceHelper::GetValues()) {
r = engine->RegisterEnumValue("DamageSource", PkmnDamageSourceHelper::ToString(v), (int)v);
}
Assert(r >= 0);
}
void RegisterPokemonClass::RegisterMoveLearnMethod(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterEnum("MoveLearnMethod");
Assert(r >= 0);
r = engine->RegisterEnumValue("MoveLearnMethod", "Unknown", (int)CreatureLib::Battling::AttackLearnMethod::Unknown);
Assert(r >= 0);
r = engine->RegisterEnumValue("MoveLearnMethod", "Level", (int)CreatureLib::Battling::AttackLearnMethod::Level);
Assert(r >= 0);
}
ENUM__SIZE_WRAPPER(LearnedMove_LearnMethodWrapper, CreatureLib::Battling::LearnedAttack, GetLearnMethod)
SMART_PTR_GETTER_FUNC(CreatureLib::Battling::LearnedAttack, const CreatureLib::Library::AttackData, GetAttack);
void RegisterPokemonClass::RegisterLearnedAttack(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterObjectType("LearnedMove", 0, asOBJ_REF | asOBJ_NOCOUNT);
Assert(r >= 0);
r = engine->RegisterObjectMethod("LearnedMove", "const MoveData@ get_MoveData() const property",
asFUNCTION(GetAttackWrapper), asCALL_CDECL_OBJFIRST);
Assert(r >= 0);
r = engine->RegisterObjectMethod("LearnedMove", "uint8 get_MaxUses() const property",
asMETHOD(CreatureLib::Battling::LearnedAttack, GetMaxUses), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("LearnedMove", "uint8 get_RemainingUses() const property",
asMETHOD(CreatureLib::Battling::LearnedAttack, GetRemainingUses), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("LearnedMove", "Gender get_LearnMethod() const property",
asFUNCTION(LearnedMove_LearnMethodWrapper), asCALL_CDECL_OBJLAST);
Assert(r >= 0);
}
ENUM__SIZE_WRAPPER(Pkmn_GenderWrapper, PkmnLib::Battling::Pokemon, GetGender)
CScriptArray* GetMoves(const PkmnLib::Battling::Pokemon* obj) {
asIScriptContext* ctx = asGetActiveContext();
if (ctx) {
asIScriptEngine* engine = ctx->GetEngine();
asITypeInfo* t = engine->GetTypeInfoByDecl("array<LearnedMove@>");
auto a = obj->GetMoves();
2020-04-18 11:43:05 +00:00
CScriptArray* arr = CScriptArray::Create(t, a.Count());
for (size_t i = 0; i < a.Count(); i++) {
arr->SetValue(i, &a[i]);
}
return arr;
}
return nullptr;
}
static bool HasHeldItem(const PkmnLib::Battling::Pokemon* obj, const ConstString& str) {
return obj->HasHeldItem(str.GetHash());
}
2020-05-27 15:26:25 +00:00
SMART_PTR_GETTER_FUNC(PkmnLib::Battling::Pokemon, const CreatureLib::Library::CreatureSpecies, GetSpecies);
SMART_PTR_GETTER_FUNC(PkmnLib::Battling::Pokemon, const PkmnLib::Library::PokemonForme, GetForme);
SMART_PTR_GETTER_FUNC(PkmnLib::Battling::Pokemon, const CreatureLib::Library::Item, GetHeldItem);
void RegisterPokemonClass::RegisterPokemonType(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterObjectType("Pokemon", 0, asOBJ_REF | asOBJ_NOCOUNT);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "const Species@ get_Species() const property",
2020-05-27 15:26:25 +00:00
asFUNCTION(GetSpeciesWrapper), asCALL_CDECL_OBJLAST);
Assert(r >= 0);
2020-05-27 15:26:25 +00:00
r = engine->RegisterObjectMethod("Pokemon", "const Forme@ get_Forme() const property", asFUNCTION(GetFormeWrapper),
asCALL_CDECL_OBJLAST);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint8 get_Level() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetLevel), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint32 get_Experience() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetExperience), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "Gender get_Gender() const property", asFUNCTION(Pkmn_GenderWrapper),
asCALL_CDECL_OBJLAST);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint8 get_Coloring() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetColoring), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "bool get_Shiny() const property",
asMETHOD(PkmnLib::Battling::Pokemon, IsShiny), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "const Item@ get_HeldItem() const property",
2020-05-27 15:26:25 +00:00
asFUNCTION(GetHeldItemWrapper), asCALL_CDECL_OBJLAST);
Assert(r >= 0);
2020-04-07 10:04:23 +00:00
r = engine->RegisterObjectMethod("Pokemon", "bool HasHeldItem(const constString &in name) const",
asFUNCTION(HasHeldItem), asCALL_CDECL_OBJFIRST);
Assert(r >= 0);
r = engine->RegisterObjectMethod(
"Pokemon", "void SetHeldItem(const string &in name)",
2020-05-27 15:26:25 +00:00
asMETHODPR(PkmnLib::Battling::Pokemon, SetHeldItem, (const ArbUt::CaseInsensitiveConstString&), void),
asCALL_THISCALL);
Assert(r >= 0);
2020-05-27 15:26:25 +00:00
r = engine->RegisterObjectMethod("Pokemon", "void SetHeldItem(const Item@ item)",
asMETHODPR(PkmnLib::Battling::Pokemon, SetHeldItem,
(const ArbUt::BorrowedPtr<const CreatureLib::Library::Item>&), void),
asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint32 get_CurrentHealth() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetCurrentHealth), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "const string& get_Nickname() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetNickname), asCALL_THISCALL);
Assert(r >= 0);
2020-04-07 10:04:23 +00:00
r = engine->RegisterObjectMethod("Pokemon", "const constString& get_ActiveAbility() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetActiveTalent), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "bool get_IsFainted() const property",
asMETHOD(PkmnLib::Battling::Pokemon, IsFainted), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "bool HasType(uint8 type) const",
asMETHOD(PkmnLib::Battling::Pokemon, HasType), asCALL_THISCALL);
Assert(r >= 0);
2020-02-13 14:15:07 +00:00
r = engine->RegisterObjectMethod("Pokemon", "uint32 get_MaxHealth() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetMaxHealth), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void Damage(uint32 type, DamageSource source)",
asMETHOD(PkmnLib::Battling::Pokemon, Damage), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void Heal(uint32 type)", asMETHOD(PkmnLib::Battling::Pokemon, Heal),
asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void OverrideActiveAbility(const string &in ability)",
asMETHOD(PkmnLib::Battling::Pokemon, OverrideActiveTalent), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "LearnedMove@[]@ GetMoves() const", asFUNCTION(GetMoves),
asCALL_CDECL_OBJLAST);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void ChangeStatBoost(Statistic stat, int8 amount)",
asMETHOD(PkmnLib::Battling::Pokemon, ChangeStatBoost), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "const Species@ get_DisplaySpecies() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetDisplaySpecies), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "const Species@ get_DisplayForme() const property",
asMETHOD(PkmnLib::Battling::Pokemon, GetDisplayVariant), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint32 GetFlatStat(Statistic stat) const",
asMETHOD(PkmnLib::Battling::Pokemon, GetFlatStat), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint32 GetBoostedStat(Statistic stat) const",
asMETHOD(PkmnLib::Battling::Pokemon, GetBoostedStat), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint32 GetBaseStat(Statistic stat) const",
asMETHOD(PkmnLib::Battling::Pokemon, GetBaseStat), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "int8 GetStatBoost(Statistic stat) const",
asMETHOD(PkmnLib::Battling::Pokemon, GetStatBoost), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod(
2020-04-07 10:04:23 +00:00
"Pokemon", "void AddVolatile(const constString &in name) const",
asMETHODPR(PkmnLib::Battling::Pokemon, AddVolatileScript, (const ConstString&), void), asCALL_THISCALL);
Assert(r >= 0);
r = engine->RegisterObjectMethod(
2020-04-07 10:04:23 +00:00
"Pokemon", "void RemoveVolatile(const constString &in name) const",
asMETHODPR(PkmnLib::Battling::Pokemon, RemoveVolatileScript, (const ConstString&), void), asCALL_THISCALL);
Assert(r >= 0);
}