1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-05-13 17:41:27 +00:00

Revise record store, add specialization function

This commit is contained in:
Zackhasacat 2023-10-25 21:38:42 -05:00
parent 909be9cf35
commit 2f16a104dc
6 changed files with 29 additions and 33 deletions

View file

@ -1,5 +1,6 @@
#include "classbindings.hpp" #include "classbindings.hpp"
#include "stats.hpp"
#include "types/types.hpp"
#include <components/esm3/loadclas.hpp> #include <components/esm3/loadclas.hpp>
#include <components/lua/luastate.hpp> #include <components/lua/luastate.hpp>
@ -27,27 +28,12 @@ namespace sol
namespace MWLua namespace MWLua
{ {
using classStore = MWWorld::Store<ESM::Class>;
void initCoreClassBindings(const Context& context) sol::table initCoreClassBindings(const Context& context)
{ {
sol::state_view& lua = context.mLua->sol(); sol::state_view& lua = context.mLua->sol();
sol::usertype<classStore> classStoreT = lua.new_usertype<classStore>("ESM3_ClassStore"); sol::table classes(context.mLua->sol(), sol::create);
classStoreT[sol::meta_function::to_string] = [](const classStore& store) { addRecordFunctionBinding<ESM::Class>(classes, context);
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<sol::function>();
classStoreT[sol::meta_function::ipairs] = lua["ipairsForArray"].template get<sol::function>();
// class record // class record
auto classT = lua.new_usertype<ESM::Class>("ESM3_Class"); auto classT = lua.new_usertype<ESM::Class>("ESM3_Class");
classT[sol::meta_function::to_string] classT[sol::meta_function::to_string]
@ -92,15 +78,10 @@ namespace MWLua
return res; return res;
}); });
classT["specialization"] = sol::readonly_property([](const ESM::Class& rec) -> std::string_view { classT["specialization"] = sol::readonly_property(
if (rec.mData.mSpecialization == ESM::Class::Stealth) [](const ESM::Class& rec) -> std::string_view { return getSpecialization(rec.mData.mSpecialization); });
return "stealth";
else if (rec.mData.mSpecialization == ESM::Class::Magic)
return "magic";
else
return "combat";
});
classT["isPlayable"] classT["isPlayable"]
= sol::readonly_property([](const ESM::Class& rec) -> bool { return rec.mData.mIsPlayable; }); = sol::readonly_property([](const ESM::Class& rec) -> bool { return rec.mData.mIsPlayable; });
return classes;
} }
} }

View file

@ -7,7 +7,7 @@
namespace MWLua namespace MWLua
{ {
void initCoreClassBindings(const Context& context); sol::table initCoreClassBindings(const Context& context);
} }
#endif // MWLUA_CLASSBINDINGS_H #endif // MWLUA_CLASSBINDINGS_H

View file

@ -162,8 +162,7 @@ namespace MWLua
initCoreFactionBindings(context); initCoreFactionBindings(context);
character["factions"] = &MWBase::Environment::get().getWorld()->getStore().get<ESM::Faction>(); character["factions"] = &MWBase::Environment::get().getWorld()->getStore().get<ESM::Faction>();
initCoreClassBindings(context); character["classes"] = initCoreClassBindings(context);
character["classes"] = &MWBase::Environment::get().getWorld()->getStore().get<ESM::Class>();
api["character"] = character; api["character"] = character;
api["l10n"] = LuaUtil::initL10nLoader(lua->sol(), MWBase::Environment::get().getL10nManager()); api["l10n"] = LuaUtil::initL10nLoader(lua->sol(), MWBase::Environment::get().getL10nManager());
const MWWorld::Store<ESM::GameSetting>* gmstStore const MWWorld::Store<ESM::GameSetting>* gmstStore

View file

@ -60,6 +60,16 @@ namespace
namespace MWLua 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) static void addStatUpdateAction(MWLua::LuaManager* manager, const SelfObject& obj)
{ {
if (!obj.mStatsCache.empty()) 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["name"] = sol::readonly_property([](const ESM::Skill& rec) -> std::string_view { return rec.mName; });
skillT["description"] skillT["description"]
= sol::readonly_property([](const ESM::Skill& rec) -> std::string_view { return rec.mDescription; }); = 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 { skillT["icon"] = sol::readonly_property([vfs](const ESM::Skill& rec) -> std::string {
return Misc::ResourceHelpers::correctIconPath(rec.mIcon, vfs); return Misc::ResourceHelpers::correctIconPath(rec.mIcon, vfs);
}); });

View file

@ -6,7 +6,7 @@
namespace MWLua namespace MWLua
{ {
struct Context; struct Context;
std::string_view getSpecialization(const int val);
void addActorStatsBindings(sol::table& actor, const Context& context); void addActorStatsBindings(sol::table& actor, const Context& context);
void addNpcStatsBindings(sol::table& npc, const Context& context); void addNpcStatsBindings(sol::table& npc, const Context& context);
sol::table initCoreStatsBindings(const Context& context); sol::table initCoreStatsBindings(const Context& context);

View file

@ -12,14 +12,18 @@
--- ---
-- A read-only list of all @{#FactionRecord}s in the world database. -- 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 --- @{#Character}: Class and Character Data
-- @field [parent=#core] #Character character -- @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. -- 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. -- Terminates the game and quits to the OS. Should be used only for testing purposes.