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

221 lines
13 KiB
C++

#include "RegisterPokemonClass.hpp"
#include <CreatureLib/Battling/Models/LearnedAttack.hpp>
#include <scriptarray/scriptarray.h>
#include <scripthandle/scripthandle.h>
#include "../../../../Battling/PkmnDamageSource.hpp"
#include "../../../../Battling/Pokemon/Pokemon.hpp"
#include "../../AngelScriptResolver.hpp"
#include "../../AngelScriptScript.hpp"
#include "../../AngelscriptUserdata.hpp"
#include "../HelperFile.hpp"
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");
Ensure(r >= 0);
for (auto v : CreatureLib::Battling::DamageSourceHelper::GetValues()) {
r = engine->RegisterEnumValue("DamageSource", CreatureLib::Battling::DamageSourceHelper::ToString(v).c_str(),
(int)v);
}
for (auto v : PkmnDamageSourceHelper::GetValues()) {
r = engine->RegisterEnumValue("DamageSource", PkmnDamageSourceHelper::ToString(v).c_str(), (int)v);
}
Ensure(r >= 0);
}
void RegisterPokemonClass::RegisterMoveLearnMethod(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterEnum("MoveLearnMethod");
Ensure(r >= 0);
r = engine->RegisterEnumValue("MoveLearnMethod", "Unknown", (int)CreatureLib::Battling::AttackLearnMethod::Unknown);
Ensure(r >= 0);
r = engine->RegisterEnumValue("MoveLearnMethod", "Level", (int)CreatureLib::Battling::AttackLearnMethod::Level);
Ensure(r >= 0);
}
void RegisterPokemonClass::RegisterLearnedAttack(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterObjectType("LearnedMove", 0, asOBJ_REF | asOBJ_NOCOUNT);
Ensure(r >= 0);
REGISTER_GETTER("LearnedMove", "const MoveData@ get_MoveData() const property",
CreatureLib::Battling::LearnedAttack, GetAttack);
REGISTER_GETTER("LearnedMove", "uint8 get_MaxUses() const property", CreatureLib::Battling::LearnedAttack,
GetMaxUses);
REGISTER_GETTER("LearnedMove", "uint8 get_RemainingUses() const property", CreatureLib::Battling::LearnedAttack,
GetRemainingUses);
REGISTER_GETTER("LearnedMove", "Gender get_LearnMethod() const property", CreatureLib::Battling::LearnedAttack,
GetLearnMethod);
}
static bool HasHeldItem(const PkmnLib::Battling::Pokemon* obj, const ArbUt::StringView& str) {
return obj->HasHeldItem(str.GetHash());
}
static void SetHeldItemWrapper(PkmnLib::Battling::Pokemon* obj, const ArbUt::StringView& str) {
return obj->SetHeldItem(str);
}
static std::string NicknameWrapper(const PkmnLib::Battling::Pokemon* obj) {
if (!obj->GetNickname().has_value()) {
return "";
}
return std::string(obj->GetNickname().value());
}
static const ArbUt::StringView& GetActiveAbilityWrapper(PkmnLib::Battling::Pokemon* p) {
return p->GetActiveTalent()->GetName();
}
static size_t GetTypesLengthWrapper(PkmnLib::Battling::Pokemon* p) { return p->GetTypes().size(); }
static uint8_t GetTypeWrapper(PkmnLib::Battling::Pokemon* p, size_t index) { return p->GetTypes()[index]; }
#if defined(__clang__)
// Angelscript aligns this wrong (aligned on 4 bytes, wants 8). While this is a slight performance hit, we can ignore it for now.
[[clang::no_sanitize("alignment")]]
#endif
static CScriptHandle AddVolatileWrapper(PkmnLib::Battling::Pokemon* obj, const ArbUt::StringView& name) {
auto* resolver = static_cast<AngelScriptResolver*>(obj->GetLibrary()->GetScriptResolver().get());
auto* script = static_cast<AngelScriptScript*>(obj->AddVolatileScript(name))->GetRawAngelscriptObject();
return {script, resolver->GetBaseType("PkmnScript")};
}
static bool HasTypeStringWrapper(PkmnLib::Battling::Pokemon* p, const ArbUt::StringView& sv) {
auto typeId = p->GetLibrary()->GetTypeLibrary()->GetTypeId(sv);
return p->HasType(typeId);
}
static float GetEffectivenessHelper(CreatureLib::Library::TypeLibrary* typeLib, u8 attackingType,
PkmnLib::Battling::Pokemon* pokemon) {
return typeLib->GetEffectiveness(attackingType, pokemon->GetTypes());
}
void RegisterPokemonClass::RegisterPokemonType(asIScriptEngine* engine) {
Ensure(engine->RegisterObjectType("Pokemon", 0, asOBJ_REF | asOBJ_NOCOUNT) >= 0);
REGISTER_GETTER("Pokemon", "const Species@ get_Species() const property", CreatureLib::Battling::Creature,
GetSpecies);
REGISTER_GETTER("Pokemon", "const Forme@ get_Forme() const property", CreatureLib::Battling::Creature, GetVariant);
REGISTER_GETTER("Pokemon", "uint8 get_Level() const property", CreatureLib::Battling::Creature, GetLevel);
REGISTER_GETTER("Pokemon", "uint32 get_Experience() const property", CreatureLib::Battling::Creature,
GetExperience);
REGISTER_GETTER("Pokemon", "Gender get_Gender() const property", CreatureLib::Battling::Creature, GetGender);
REGISTER_GETTER("Pokemon", "uint8 get_Coloring() const property", CreatureLib::Battling::Creature, GetColoring);
REGISTER_GETTER("Pokemon", "bool get_Shiny() const property", PkmnLib::Battling::Pokemon, IsShiny);
REGISTER_GETTER("Pokemon", "const Item@ get_HeldItem() const property", CreatureLib::Battling::Creature,
GetHeldItem);
REGISTER_GETTER("Pokemon", "uint32 get_CurrentHealth() const property", CreatureLib::Battling::Creature,
GetCurrentHealth);
REGISTER_GETTER("Pokemon", "bool get_IsFainted() const property", CreatureLib::Battling::Creature, IsFainted);
REGISTER_GETTER("Pokemon", "uint32 get_MaxHealth() const property", CreatureLib::Battling::Creature, GetMaxHealth);
REGISTER_GETTER("Pokemon", "const Species@ get_DisplaySpecies() const property", CreatureLib::Battling::Creature,
GetDisplaySpecies);
REGISTER_GETTER("Pokemon", "const Forme@ get_DisplayForme() const property", CreatureLib::Battling::Creature,
GetDisplayVariant);
REGISTER_GETTER("Pokemon", "Battle@ get_Battle() const property", CreatureLib::Battling::Creature, GetBattle);
REGISTER_GETTER("Pokemon", "BattleSide@ get_BattleSide() const property", CreatureLib::Battling::Creature,
GetBattleSide);
REGISTER_GETTER("Pokemon", "const constString& get_Status() const property", CreatureLib::Battling::Creature,
GetStatusName);
REGISTER_GETTER("Pokemon", "const narray<LearnedMove@>@ get_Moves() const property", PkmnLib::Battling::Pokemon,
GetMoves);
REGISTER_GETTER("Pokemon", "float get_Weight() const property", CreatureLib::Battling::Creature, GetWeight);
REGISTER_GETTER("Pokemon", "float get_Height() const property", CreatureLib::Battling::Creature, GetHeight);
auto r = engine->RegisterObjectMethod("Pokemon", "void set_Weight(float value) property",
asMETHOD(PkmnLib::Battling::Pokemon, SetWeight), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "const constString& get_ActiveAbility() const property",
asFUNCTION(GetActiveAbilityWrapper), asCALL_CDECL_OBJFIRST);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint64 get_TypesLength() const property",
asFUNCTION(GetTypesLengthWrapper), asCALL_CDECL_OBJFIRST);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint8 GetType(uint64 index) const", asFUNCTION(GetTypeWrapper),
asCALL_CDECL_OBJFIRST);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void SetType(uint8 index, uint8 type)",
asMETHOD(PkmnLib::Battling::Pokemon, SetType), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void set_Height(float value) property",
asMETHOD(PkmnLib::Battling::Pokemon, SetHeight), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "bool HasHeldItem(const constString &in name) const",
asFUNCTION(HasHeldItem), asCALL_CDECL_OBJFIRST);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void SetHeldItem(const constString &in name)",
asFUNCTION(SetHeldItemWrapper), asCALL_CDECL_OBJFIRST);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void SetHeldItem(const Item@ item)",
asMETHODPR(PkmnLib::Battling::Pokemon, SetHeldItem,
(const ArbUt::BorrowedPtr<const CreatureLib::Library::Item>&), void),
asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "string get_Nickname() const property", asFUNCTION(NicknameWrapper),
asCALL_CDECL_OBJFIRST);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "bool HasType(uint8 type) const",
asMETHOD(PkmnLib::Battling::Pokemon, HasType), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "bool HasType(const constString &in type) const",
asFUNCTION(HasTypeStringWrapper), asCALL_CDECL_OBJFIRST);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void Damage(uint32 type, DamageSource source)",
asMETHOD(PkmnLib::Battling::Pokemon, Damage), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void Heal(uint32 type)", asMETHOD(PkmnLib::Battling::Pokemon, Heal),
asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void OverrideActiveAbility(const string &in ability)",
asMETHOD(PkmnLib::Battling::Pokemon, OverrideActiveTalent), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void ChangeStatBoost(Statistic stat, int8 amount, bool selfInflicted)",
asMETHOD(PkmnLib::Battling::Pokemon, ChangeStatBoost), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint32 GetFlatStat(Statistic stat) const",
asMETHOD(PkmnLib::Battling::Pokemon, GetFlatStat), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint32 GetBoostedStat(Statistic stat) const",
asMETHOD(PkmnLib::Battling::Pokemon, GetBoostedStat), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "uint32 GetBaseStat(Statistic stat) const",
asMETHOD(PkmnLib::Battling::Pokemon, GetBaseStat), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "int8 GetStatBoost(Statistic stat) const",
asMETHOD(PkmnLib::Battling::Pokemon, GetStatBoost), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "bool HasVolatile(const constString &in name) const",
asMETHOD(PkmnLib::Battling::Pokemon, HasVolatileScript), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "ref AddVolatile(const constString &in name)",
asFUNCTION(AddVolatileWrapper), asCALL_CDECL_OBJFIRST);
Ensure(r >= 0);
r = engine->RegisterObjectMethod(
"Pokemon", "void RemoveVolatile(const constString &in name) const",
asMETHODPR(PkmnLib::Battling::Pokemon, RemoveVolatileScript, (const ArbUt::BasicStringView&), void),
asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void ClearStatus() const",
asMETHOD(PkmnLib::Battling::Pokemon, ClearStatus), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Pokemon", "void SetStatus(const constString &in name)",
asMETHOD(PkmnLib::Battling::Pokemon, SetStatus), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod(
"Pokemon", "void ChangeForme(const constString &in name)",
asMETHODPR(PkmnLib::Battling::Pokemon, ChangeVariant, (const ArbUt::StringView&), void), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("TypeLibrary",
"float GetEffectiveness(uint8 attackingType, Pokemon@ defender) const",
asFUNCTION(GetEffectivenessHelper), asCALL_CDECL_OBJFIRST);
Ensure(r >= 0);
}