From 86bb3195a0ab098d80838ea5a0ed08b139db02c6 Mon Sep 17 00:00:00 2001 From: Christian Haro <8536818-c-haro@users.noreply.gitlab.com> Date: Sun, 31 Jul 2022 20:31:29 +0000 Subject: [PATCH] Lua Bindings for Ingredient Records --- apps/openmw/CMakeLists.txt | 2 +- apps/openmw/mwlua/luabindings.cpp | 2 +- apps/openmw/mwlua/types/ingredient.cpp | 33 ++++++++++++++++++++++++++ apps/openmw/mwlua/types/types.cpp | 2 +- apps/openmw/mwlua/types/types.hpp | 1 + files/lua_api/openmw/types.lua | 16 +++++++++++++ 6 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 apps/openmw/mwlua/types/ingredient.cpp diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index ed742fcbc9..e9aaa11087 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/misc types/repair + 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 ) add_openmw_dir (mwsound diff --git a/apps/openmw/mwlua/luabindings.cpp b/apps/openmw/mwlua/luabindings.cpp index d7fc00d07d..66f7ffee9c 100644 --- a/apps/openmw/mwlua/luabindings.cpp +++ b/apps/openmw/mwlua/luabindings.cpp @@ -54,7 +54,7 @@ namespace MWLua { auto* lua = context.mLua; sol::table api(lua->sol(), sol::create); - api["API_REVISION"] = 28; + api["API_REVISION"] = 29; api["quit"] = [lua]() { Log(Debug::Warning) << "Quit requested by a Lua script.\n" << lua->debugTraceback(); diff --git a/apps/openmw/mwlua/types/ingredient.cpp b/apps/openmw/mwlua/types/ingredient.cpp new file mode 100644 index 0000000000..bbf2fc700e --- /dev/null +++ b/apps/openmw/mwlua/types/ingredient.cpp @@ -0,0 +1,33 @@ +#include "types.hpp" + +#include + +#include + +#include "../luabindings.hpp" + +namespace sol +{ + template <> + struct is_automagical : std::false_type {}; +} + +namespace MWLua +{ + void addIngredientBindings(sol::table ingredient, const Context& context) + { + const MWWorld::Store* store = &MWBase::Environment::get().getWorld()->getStore().get(); + ingredient["record"] = sol::overload( + [](const Object& obj)-> const ESM::Ingredient* { return obj.ptr().get()->mBase; }, + [store](const std::string& recordID)-> const ESM::Ingredient* {return store->find(recordID); }); + sol::usertype record = context.mLua->sol().new_usertype(("ESM3_Ingredient")); + record[sol::meta_function::to_string] = [](const ESM::Potion& rec) {return "ESM3_Ingredient[" + rec.mId + "]"; }; + record["id"] = sol::readonly_property([](const ESM::Ingredient& rec) -> std::string {return rec.mId; }); + record["name"] = sol::readonly_property([](const ESM::Ingredient& rec) -> std::string {return rec.mName; }); + record["model"] = sol::readonly_property([](const ESM::Ingredient& rec) -> std::string {return rec.mModel; }); + record["mwscript"] = sol::readonly_property([](const ESM::Ingredient& rec) -> std::string {return rec.mScript; }); + record["icon"] = sol::readonly_property([](const ESM::Ingredient& rec) -> std::string {return rec.mIcon; }); + record["weight"] = sol::readonly_property([](const ESM::Ingredient& rec) -> float {return rec.mData.mWeight; }); + record["value"] = sol::readonly_property([](const ESM::Ingredient& rec) -> int{return rec.mData.mValue; }); + } +} diff --git a/apps/openmw/mwlua/types/types.cpp b/apps/openmw/mwlua/types/types.cpp index 6235216670..b9ddce4a96 100644 --- a/apps/openmw/mwlua/types/types.cpp +++ b/apps/openmw/mwlua/types/types.cpp @@ -159,7 +159,7 @@ namespace MWLua addType(ObjectTypeName::Armor, {ESM::REC_ARMO}, ObjectTypeName::Item); addType(ObjectTypeName::Clothing, {ESM::REC_CLOT}, ObjectTypeName::Item); - addType(ObjectTypeName::Ingredient, {ESM::REC_INGR}, ObjectTypeName::Item); + addIngredientBindings(addType(ObjectTypeName::Ingredient, { ESM::REC_INGR }, ObjectTypeName::Item), context); addType(ObjectTypeName::Light, {ESM::REC_LIGH}, ObjectTypeName::Item); addMiscellaneousBindings(addType(ObjectTypeName::MiscItem, {ESM::REC_MISC}, ObjectTypeName::Item), context); addPotionBindings(addType(ObjectTypeName::Potion, {ESM::REC_ALCH}, ObjectTypeName::Item), context); diff --git a/apps/openmw/mwlua/types/types.hpp b/apps/openmw/mwlua/types/types.hpp index c4b9b63a45..f4446b09c9 100644 --- a/apps/openmw/mwlua/types/types.hpp +++ b/apps/openmw/mwlua/types/types.hpp @@ -41,6 +41,7 @@ namespace MWLua void addRepairBindings(sol::table repair, const Context& context); void addMiscellaneousBindings(sol::table miscellaneous, const Context& context); void addPotionBindings(sol::table potion, const Context& context); + void addIngredientBindings(sol::table Ingredient, const Context& context); } #endif // MWLUA_TYPES_H diff --git a/files/lua_api/openmw/types.lua b/files/lua_api/openmw/types.lua index 26a29746b5..ad37335ae9 100644 --- a/files/lua_api/openmw/types.lua +++ b/files/lua_api/openmw/types.lua @@ -624,6 +624,22 @@ -- @param openmw.core#GameObject object -- @return #boolean +--- +-- Returns the read-only @{#IngredientRecord} of a Ingredient +-- @function [parent=#Ingredient] record +-- @param #any objectOrRecordId +-- @return #IngredientRecord + +--- +-- @type IngredientRecord +-- @field #string id Record id +-- @field #string name Human-readable name +-- @field #string model VFS path to the model +-- @field #string mwscript MWScript on this potion (can be empty) +-- @field #string icon VFS path to the icon +-- @field #number weight +-- @field #number value + --- @{#Light} functions