Register Move Types.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-01-13 19:43:34 +01:00
parent 574776dbe0
commit 797044aa8c
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 94 additions and 5 deletions

View File

@ -2,9 +2,10 @@
#include "../../extern/angelscript_addons/scripthelper/scripthelper.h" #include "../../extern/angelscript_addons/scripthelper/scripthelper.h"
#include "../../extern/angelscript_addons/scriptstdstring/scriptstdstring.h" #include "../../extern/angelscript_addons/scriptstdstring/scriptstdstring.h"
#include "TypeRegistry/RegisterItemTypes.hpp" #include "TypeRegistry/RegisterItemTypes.hpp"
#include "TypeRegistry/RegisterMoveTypes.hpp"
#include "TypeRegistry/RegisterPokemonTypes.hpp" #include "TypeRegistry/RegisterPokemonTypes.hpp"
CreatureLib::Battling::ScriptResolver* PkmnLib::Battling::BattleLibrary::CreateScriptResolver(){ CreatureLib::Battling::ScriptResolver* PkmnLib::Battling::BattleLibrary::CreateScriptResolver() {
return new AngelScripResolver(); return new AngelScripResolver();
} }
@ -22,12 +23,14 @@ void AngelScripResolver::Initialize(CreatureLib::Battling::BattleLibrary* librar
RegisterStdString(_engine); RegisterStdString(_engine);
r = _engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(Print), asCALL_CDECL); r = _engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(Print), asCALL_CDECL);
if (r < 0) throw CreatureException("Registering print function failed."); if (r < 0)
throw CreatureException("Registering print function failed.");
RegisterExceptionRoutines(_engine); RegisterExceptionRoutines(_engine);
RegisterPokemonTypes::Register(_engine); RegisterPokemonTypes::Register(_engine);
RegisterItemTypes::Register(_engine); RegisterItemTypes::Register(_engine);
RegisterMoveTypes::Register(_engine);
_mainModule = _engine->GetModule("pkmn", asGM_ALWAYS_CREATE); _mainModule = _engine->GetModule("pkmn", asGM_ALWAYS_CREATE);
@ -35,11 +38,11 @@ void AngelScripResolver::Initialize(CreatureLib::Battling::BattleLibrary* librar
} }
AngelScriptTypeInfo* AngelScripResolver::GetTypeInfo(const std::string& name) { AngelScriptTypeInfo* AngelScripResolver::GetTypeInfo(const std::string& name) {
auto find = _types.find(name); auto find = _types.find(name);
if (find != _types.end()){ if (find != _types.end()) {
return find->second; return find->second;
} }
auto type = _mainModule->GetTypeInfoByDecl(name.c_str()); auto type = _mainModule->GetTypeInfoByDecl(name.c_str());
if (type == nullptr){ if (type == nullptr) {
_types.insert({name, nullptr}); _types.insert({name, nullptr});
return nullptr; return nullptr;
} }
@ -57,7 +60,8 @@ void AngelScripResolver::MessageCallback(const asSMessageInfo* msg, void* param)
} }
CreatureLib::Battling::Script* AngelScripResolver::LoadScript(ScriptCategory category, const std::string& scriptName) { CreatureLib::Battling::Script* AngelScripResolver::LoadScript(ScriptCategory category, const std::string& scriptName) {
auto typeInfo = GetTypeInfo(scriptName); auto typeInfo = GetTypeInfo(scriptName);
if (typeInfo == nullptr) return nullptr; if (typeInfo == nullptr)
return nullptr;
auto ctx = _contextPool->RequestContext(); auto ctx = _contextPool->RequestContext();
auto obj = typeInfo->Instantiate(ctx); auto obj = typeInfo->Instantiate(ctx);
_contextPool->ReturnContextToPool(ctx); _contextPool->ReturnContextToPool(ctx);

View File

@ -0,0 +1,72 @@
#include "RegisterMoveTypes.hpp"
#include <cassert>
#include "../../Library/Moves/MoveData.hpp"
void RegisterMoveTypes::Register(asIScriptEngine* engine) {
RegisterMoveCategory(engine);
RegisterMoveTarget(engine);
RegisterMoveType(engine);
}
#define REGISTER_ENUM_VALUE(asName, cName, valueName) \
r = engine->RegisterEnumValue(#asName, #valueName, (int)cName::valueName); \
assert(r >= 0);
void RegisterMoveTypes::RegisterMoveCategory(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterEnum("MoveCategory");
assert(r >= 0);
REGISTER_ENUM_VALUE(MoveCategory, PkmnLib::Library::MoveCategory, Physical)
REGISTER_ENUM_VALUE(MoveCategory, PkmnLib::Library::MoveCategory, Special)
REGISTER_ENUM_VALUE(MoveCategory, PkmnLib::Library::MoveCategory, Status)
}
void RegisterMoveTypes::RegisterMoveTarget(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterEnum("MoveTarget");
assert(r >= 0);
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, Adjacent)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, AdjacentAlly)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, AdjacentAllySelf)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, AdjacentOpponent)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, All)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, AllAdjacent)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, AllAdjacentOpponent)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, AllAlly)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, AllOpponent)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, Any)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, RandomOpponent)
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, Self)
}
void RegisterMoveTypes::RegisterMoveType(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterObjectType("MoveData", 0, asOBJ_REF | asOBJ_NOCOUNT);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "const string& get_Name() const property",
asMETHOD(PkmnLib::Library::MoveData, GetName), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "const string& get_Type() const property",
asMETHOD(PkmnLib::Library::MoveData, GetType), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "MoveCategory get_Category() const property",
asMETHOD(PkmnLib::Library::MoveData, GetCategory), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "uint8 get_BasePower() const property",
asMETHOD(PkmnLib::Library::MoveData, GetBasePower), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "uint8 get_Accuracy() const property",
asMETHOD(PkmnLib::Library::MoveData, GetAccuracy), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "uint8 get_BaseUsages() const property",
asMETHOD(PkmnLib::Library::MoveData, GetBaseUsages), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "MoveTarget get_Target() const property",
asMETHOD(PkmnLib::Library::MoveData, GetTarget), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "int8 get_Priority() const property",
asMETHOD(PkmnLib::Library::MoveData, GetTarget), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "bool HasFlag(const string &in flag) const",
asMETHOD(PkmnLib::Library::MoveData, HasFlag), asCALL_THISCALL);
assert(r >= 0);
}
#undef REGISTER_ENUM_VALUE

View File

@ -0,0 +1,13 @@
#ifndef PKMNLIB_REGISTERMOVETYPES_HPP
#define PKMNLIB_REGISTERMOVETYPES_HPP
#include <angelscript.h>
class RegisterMoveTypes {
static void RegisterMoveCategory(asIScriptEngine* engine);
static void RegisterMoveTarget(asIScriptEngine* engine);
static void RegisterMoveType(asIScriptEngine* engine);
public:
static void Register(asIScriptEngine* engine);
};
#endif // PKMNLIB_REGISTERMOVETYPES_HPP