diff --git a/apps/openmw/mwlua/classbindings.cpp b/apps/openmw/mwlua/classbindings.cpp index 8eb11a9056..237aaae291 100644 --- a/apps/openmw/mwlua/classbindings.cpp +++ b/apps/openmw/mwlua/classbindings.cpp @@ -1,5 +1,6 @@ #include "classbindings.hpp" - +#include "stats.hpp" +#include "types/types.hpp" #include #include @@ -27,27 +28,12 @@ namespace sol namespace MWLua { - using classStore = MWWorld::Store; - void initCoreClassBindings(const Context& context) + sol::table initCoreClassBindings(const Context& context) { sol::state_view& lua = context.mLua->sol(); - sol::usertype classStoreT = lua.new_usertype("ESM3_ClassStore"); - classStoreT[sol::meta_function::to_string] = [](const classStore& store) { - return "ESM3_ClassStore{" + std::to_string(store.getSize()) + " classes}"; - }; - classStoreT[sol::meta_function::length] = [](const classStore& store) { return store.getSize(); }; - classStoreT[sol::meta_function::index] = sol::overload( - [](const classStore& store, size_t index) -> const ESM::Class* { - if (index == 0 || index > store.getSize()) - return nullptr; - return store.at(index - 1); - }, - [](const classStore& store, std::string_view classId) -> const ESM::Class* { - return store.search(ESM::RefId::deserializeText(classId)); - }); - classStoreT[sol::meta_function::pairs] = lua["ipairsForArray"].template get(); - classStoreT[sol::meta_function::ipairs] = lua["ipairsForArray"].template get(); + sol::table classes(context.mLua->sol(), sol::create); + addRecordFunctionBinding(classes, context); // class record auto classT = lua.new_usertype("ESM3_Class"); classT[sol::meta_function::to_string] @@ -92,15 +78,10 @@ namespace MWLua return res; }); - classT["specialization"] = sol::readonly_property([](const ESM::Class& rec) -> std::string_view { - if (rec.mData.mSpecialization == ESM::Class::Stealth) - return "stealth"; - else if (rec.mData.mSpecialization == ESM::Class::Magic) - return "magic"; - else - return "combat"; - }); + classT["specialization"] = sol::readonly_property( + [](const ESM::Class& rec) -> std::string_view { return getSpecialization(rec.mData.mSpecialization); }); classT["isPlayable"] = sol::readonly_property([](const ESM::Class& rec) -> bool { return rec.mData.mIsPlayable; }); + return classes; } } diff --git a/apps/openmw/mwlua/classbindings.hpp b/apps/openmw/mwlua/classbindings.hpp index c907e5ea94..9dd9befae4 100644 --- a/apps/openmw/mwlua/classbindings.hpp +++ b/apps/openmw/mwlua/classbindings.hpp @@ -7,7 +7,7 @@ namespace MWLua { - void initCoreClassBindings(const Context& context); + sol::table initCoreClassBindings(const Context& context); } #endif // MWLUA_CLASSBINDINGS_H diff --git a/apps/openmw/mwlua/luabindings.cpp b/apps/openmw/mwlua/luabindings.cpp index 448a57da61..b55bf3ca27 100644 --- a/apps/openmw/mwlua/luabindings.cpp +++ b/apps/openmw/mwlua/luabindings.cpp @@ -162,8 +162,7 @@ namespace MWLua initCoreFactionBindings(context); character["factions"] = &MWBase::Environment::get().getWorld()->getStore().get(); - initCoreClassBindings(context); - character["classes"] = &MWBase::Environment::get().getWorld()->getStore().get(); + character["classes"] = initCoreClassBindings(context); api["character"] = character; api["l10n"] = LuaUtil::initL10nLoader(lua->sol(), MWBase::Environment::get().getL10nManager()); const MWWorld::Store* gmstStore diff --git a/apps/openmw/mwlua/stats.cpp b/apps/openmw/mwlua/stats.cpp index 5c1e536dd6..1cc1902c49 100644 --- a/apps/openmw/mwlua/stats.cpp +++ b/apps/openmw/mwlua/stats.cpp @@ -60,6 +60,16 @@ namespace namespace MWLua { + std::string_view getSpecialization(const int var) + { + if (var == ESM::Class::Stealth) + return "stealth"; + else if (var == ESM::Class::Magic) + return "magic"; + else + return "combat"; + } + static void addStatUpdateAction(MWLua::LuaManager* manager, const SelfObject& obj) { if (!obj.mStatsCache.empty()) @@ -446,6 +456,8 @@ namespace MWLua skillT["name"] = sol::readonly_property([](const ESM::Skill& rec) -> std::string_view { return rec.mName; }); skillT["description"] = sol::readonly_property([](const ESM::Skill& rec) -> std::string_view { return rec.mDescription; }); + skillT["specialization"] = sol::readonly_property( + [](const ESM::Skill& rec) -> std::string_view { return getSpecialization(rec.mData.mSpecialization); }); skillT["icon"] = sol::readonly_property([vfs](const ESM::Skill& rec) -> std::string { return Misc::ResourceHelpers::correctIconPath(rec.mIcon, vfs); }); diff --git a/apps/openmw/mwlua/stats.hpp b/apps/openmw/mwlua/stats.hpp index 4ce2f6b5eb..6cbf486c7b 100644 --- a/apps/openmw/mwlua/stats.hpp +++ b/apps/openmw/mwlua/stats.hpp @@ -6,7 +6,7 @@ namespace MWLua { struct Context; - + std::string_view getSpecialization(const int val); void addActorStatsBindings(sol::table& actor, const Context& context); void addNpcStatsBindings(sol::table& npc, const Context& context); sol::table initCoreStatsBindings(const Context& context); diff --git a/files/lua_api/openmw/core.lua b/files/lua_api/openmw/core.lua index 280d5f07bd..c1d9be3169 100644 --- a/files/lua_api/openmw/core.lua +++ b/files/lua_api/openmw/core.lua @@ -12,14 +12,18 @@ --- -- A read-only list of all @{#FactionRecord}s in the world database. --- @field [parent=#core] #list<#FactionRecord> factions +-- @field [parent=#Character] #list<#FactionRecord> factions --- @{#Character}: Class and Character Data -- @field [parent=#core] #Character character + +--- @{#Classes}: Class Data +-- @field [parent=#Character] #Classes classes + --- -- A read-only list of all @{#ClassRecord}s in the world database. --- @field [parent=#Character] #list<#ClassRecord> classes +-- @field [parent=#Classes] #list<#ClassRecord> records --- -- Terminates the game and quits to the OS. Should be used only for testing purposes.