From 4734504e2cc561036111d10e2ada39664a7f8d10 Mon Sep 17 00:00:00 2001 From: Kindi Date: Sun, 12 Feb 2023 00:19:08 +0800 Subject: [PATCH 1/2] Lua binding for Clothing --- apps/openmw/CMakeLists.txt | 2 +- apps/openmw/mwlua/types/clothing.cpp | 68 ++++++++++++++++++++++++++++ apps/openmw/mwlua/types/types.cpp | 2 +- apps/openmw/mwlua/types/types.hpp | 1 + files/lua_api/openmw/types.lua | 40 +++++++++++++++- 5 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 apps/openmw/mwlua/types/clothing.cpp diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 294dc9e7b6..11e5700c49 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -62,7 +62,7 @@ add_openmw_dir (mwlua luamanagerimp object worldview userdataserializer eventqueue luabindings localscripts playerscripts objectbindings cellbindings asyncbindings camerabindings uibindings inputbindings nearbybindings postprocessingbindings stats debugbindings - types/types types/door types/actor types/container types/weapon types/npc types/creature types/activator types/book types/lockpick types/probe types/apparatus types/potion types/ingredient types/misc types/repair types/armor types/light types/static + types/types types/door types/actor types/container types/weapon types/npc types/creature types/activator types/book types/lockpick types/probe types/apparatus types/potion types/ingredient types/misc types/repair types/armor types/light types/static types/clothing worker ) diff --git a/apps/openmw/mwlua/types/clothing.cpp b/apps/openmw/mwlua/types/clothing.cpp new file mode 100644 index 0000000000..b6110fb90d --- /dev/null +++ b/apps/openmw/mwlua/types/clothing.cpp @@ -0,0 +1,68 @@ +#include "types.hpp" + +#include +#include +#include +#include + +#include +#include +#include + +namespace sol +{ + template <> + struct is_automagical : std::false_type + { + }; +} + +namespace MWLua +{ + void addClothingBindings(sol::table clothing, const Context& context) + { + clothing["TYPE"] = LuaUtil::makeStrictReadOnly(context.mLua->tableFromPairs({ + { "Amulet", ESM::Clothing::Amulet }, + { "Belt", ESM::Clothing::Belt }, + { "LGlove", ESM::Clothing::LGlove }, + { "Pants", ESM::Clothing::Pants }, + { "RGlove", ESM::Clothing::RGlove }, + { "Ring", ESM::Clothing::Ring }, + { "Robe", ESM::Clothing::Robe }, + { "Shirt", ESM::Clothing::Shirt }, + { "Shoes", ESM::Clothing::Shoes }, + { "Skirt", ESM::Clothing::Skirt }, + })); + + auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS(); + + const MWWorld::Store* store + = &MWBase::Environment::get().getWorld()->getStore().get(); + clothing["record"] = sol::overload( + [](const Object& obj) -> const ESM::Clothing* { return obj.ptr().get()->mBase; }, + [store](const std::string& recordId) -> const ESM::Clothing* { + return store->find(ESM::RefId::stringRefId(recordId)); + }); + sol::usertype record = context.mLua->sol().new_usertype("ESM3_Clothing"); + record[sol::meta_function::to_string] + = [](const ESM::Clothing& rec) -> std::string { return "ESM3_Clothing[" + rec.mId.getRefIdString() + "]"; }; + record["id"] + = sol::readonly_property([](const ESM::Clothing& rec) -> std::string { return rec.mId.getRefIdString(); }); + record["name"] = sol::readonly_property([](const ESM::Clothing& rec) -> std::string { return rec.mName; }); + record["model"] = sol::readonly_property([vfs](const ESM::Clothing& rec) -> std::string { + return Misc::ResourceHelpers::correctMeshPath(rec.mModel, vfs); + }); + record["icon"] = sol::readonly_property([vfs](const ESM::Clothing& rec) -> std::string { + return Misc::ResourceHelpers::correctIconPath(rec.mIcon, vfs); + }); + record["enchant"] = sol::readonly_property( + [](const ESM::Clothing& rec) -> std::string { return rec.mEnchant.getRefIdString(); }); + record["mwscript"] = sol::readonly_property( + [](const ESM::Clothing& rec) -> std::string { return rec.mScript.getRefIdString(); }); + record["weight"] = sol::readonly_property([](const ESM::Clothing& rec) -> float { return rec.mData.mWeight; }); + record["value"] = sol::readonly_property([](const ESM::Clothing& rec) -> int { return rec.mData.mValue; }); + record["type"] = sol::readonly_property([](const ESM::Clothing& rec) -> int { return rec.mData.mType; }); + record["enchantCapacity"] + = sol::readonly_property([](const ESM::Clothing& rec) -> float { return rec.mData.mEnchant * 0.1f; }); + } +} diff --git a/apps/openmw/mwlua/types/types.cpp b/apps/openmw/mwlua/types/types.cpp index 02db36e885..2ac72e710e 100644 --- a/apps/openmw/mwlua/types/types.cpp +++ b/apps/openmw/mwlua/types/types.cpp @@ -159,7 +159,7 @@ namespace MWLua addType(ObjectTypeName::Player, { ESM::REC_INTERNAL_PLAYER }, ObjectTypeName::NPC); addArmorBindings(addType(ObjectTypeName::Armor, { ESM::REC_ARMO }, ObjectTypeName::Item), context); - addType(ObjectTypeName::Clothing, { ESM::REC_CLOT }, ObjectTypeName::Item); + addClothingBindings(addType(ObjectTypeName::Clothing, { ESM::REC_CLOT }, ObjectTypeName::Item), context); addIngredientBindings(addType(ObjectTypeName::Ingredient, { ESM::REC_INGR }, ObjectTypeName::Item), context); addLightBindings(addType(ObjectTypeName::Light, { ESM::REC_LIGH }, ObjectTypeName::Item), context); addMiscellaneousBindings(addType(ObjectTypeName::MiscItem, { ESM::REC_MISC }, ObjectTypeName::Item), context); diff --git a/apps/openmw/mwlua/types/types.hpp b/apps/openmw/mwlua/types/types.hpp index 730ee26fcb..2ce6d3e9a2 100644 --- a/apps/openmw/mwlua/types/types.hpp +++ b/apps/openmw/mwlua/types/types.hpp @@ -42,6 +42,7 @@ namespace MWLua void addPotionBindings(sol::table potion, const Context& context); void addIngredientBindings(sol::table Ingredient, const Context& context); void addArmorBindings(sol::table armor, const Context& context); + void addClothingBindings(sol::table clothing, const Context& context); void addStaticBindings(sol::table stat, const Context& context); void addLightBindings(sol::table light, const Context& context); } diff --git a/files/lua_api/openmw/types.lua b/files/lua_api/openmw/types.lua index 8537979151..5088e38b9f 100644 --- a/files/lua_api/openmw/types.lua +++ b/files/lua_api/openmw/types.lua @@ -649,9 +649,9 @@ -- @field #boolean isScroll -- @field #number enchantCapacity + + --- @{#Clothing} functions - - -- @field [parent=#types] #Clothing Clothing --- @@ -665,6 +665,42 @@ -- @param openmw.core#GameObject object -- @return #boolean +--- Clothing.TYPE +-- @type ClothingTYPE +-- @field #number Amulet +-- @field #number Belt +-- @field #number LGlove +-- @field #number Pants +-- @field #number RGlove +-- @field #number Ring +-- @field #number Robe +-- @field #number Shirt +-- @field #number Shoes +-- @field #number Skirt + +--- @{#ClothingTYPE} +-- @field [parent=#Clothing] #ClothingTYPE TYPE + +--- +-- Returns the read-only @{#ClothingRecord} of a Clothing +-- @function [parent=#Clothing] record +-- @param #any objectOrRecordId +-- @return #ClothingRecord + +--- +-- @type ClothingRecord +-- @field #string id Record id +-- @field #string name Name of the clothing +-- @field #string model VFS path to the model +-- @field #string mwscript MWScript on this armor (can be empty) +-- @field #string icon VFS path to the icon +-- @field #string enchant The enchantment ID of this armor (can be empty) +-- @field #number weight +-- @field #number value +-- @field #number type See @{#Clothing.TYPE} +-- @field #number enchantCapacity + + --- @{#Ingredient} functions From 1d35a83452d94ac30204aa7a87396ea91eba6028 Mon Sep 17 00:00:00 2001 From: Kindi <2538602-Kuyondo@users.noreply.gitlab.com> Date: Sun, 12 Feb 2023 00:26:45 +0000 Subject: [PATCH 2/2] Update types.lua --- files/lua_api/openmw/types.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/files/lua_api/openmw/types.lua b/files/lua_api/openmw/types.lua index 5088e38b9f..9d87a1ba31 100644 --- a/files/lua_api/openmw/types.lua +++ b/files/lua_api/openmw/types.lua @@ -692,9 +692,9 @@ -- @field #string id Record id -- @field #string name Name of the clothing -- @field #string model VFS path to the model --- @field #string mwscript MWScript on this armor (can be empty) +-- @field #string mwscript MWScript on this clothing (can be empty) -- @field #string icon VFS path to the icon --- @field #string enchant The enchantment ID of this armor (can be empty) +-- @field #string enchant The enchantment ID of this clothing (can be empty) -- @field #number weight -- @field #number value -- @field #number type See @{#Clothing.TYPE}