1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-04-30 19:11:23 +00:00

[Lua] Split obj.inventory into Actor.inventory(obj) and Container.content(obj)

This commit is contained in:
Petr Mikheev 2022-03-10 00:31:44 +01:00
parent d251c4e2a1
commit 43bed7f0d2
8 changed files with 33 additions and 18 deletions

View file

@ -60,7 +60,7 @@ add_openmw_dir (mwlua
luamanagerimp actions object worldview userdataserializer eventqueue query luamanagerimp actions object worldview userdataserializer eventqueue query
luabindings localscripts playerscripts objectbindings cellbindings asyncbindings settingsbindings luabindings localscripts playerscripts objectbindings cellbindings asyncbindings settingsbindings
camerabindings uibindings inputbindings nearbybindings camerabindings uibindings inputbindings nearbybindings
types/types types/door types/actor types/types types/door types/actor types/container
) )
add_openmw_dir (mwsound add_openmw_dir (mwsound

View file

@ -391,6 +391,7 @@ namespace MWLua
{ {
assert(mInitialized); assert(mInitialized);
assert(flag != ESM::LuaScriptCfg::sGlobal); assert(flag != ESM::LuaScriptCfg::sGlobal);
assert(ptr.getType() != ESM::REC_STAT);
std::shared_ptr<LocalScripts> scripts; std::shared_ptr<LocalScripts> scripts;
if (flag == ESM::LuaScriptCfg::sPlayer) if (flag == ESM::LuaScriptCfg::sPlayer)
{ {

View file

@ -118,6 +118,9 @@ namespace MWLua
using GObjectList = ObjectList<GObject>; using GObjectList = ObjectList<GObject>;
using LObjectList = ObjectList<LObject>; using LObjectList = ObjectList<LObject>;
template <typename Obj>
struct Inventory { Obj mObj; };
} }
#endif // MWLUA_OBJECT_H #endif // MWLUA_OBJECT_H

View file

@ -12,15 +12,6 @@
#include "luamanagerimp.hpp" #include "luamanagerimp.hpp"
#include "types/types.hpp" #include "types/types.hpp"
namespace MWLua
{
template <typename ObjectT>
struct Inventory
{
ObjectT mObj;
};
}
namespace sol namespace sol
{ {
template <> template <>
@ -149,6 +140,8 @@ namespace MWLua
throw std::runtime_error("Unknown script: " + std::string(path)); throw std::runtime_error("Unknown script: " + std::string(path));
if (!(cfg[*scriptId].mFlags & ESM::LuaScriptCfg::sCustom)) if (!(cfg[*scriptId].mFlags & ESM::LuaScriptCfg::sCustom))
throw std::runtime_error("Script without CUSTOM tag can not be added dynamically: " + std::string(path)); throw std::runtime_error("Script without CUSTOM tag can not be added dynamically: " + std::string(path));
if (object.ptr().getType() == ESM::REC_STAT)
throw std::runtime_error("Attaching scripts to Static is not allowed: " + std::string(path));
luaManager->addCustomLocalScript(object.ptr(), *scriptId); luaManager->addCustomLocalScript(object.ptr(), *scriptId);
}; };
objectT["hasScript"] = [lua=context.mLua](const GObject& object, std::string_view path) objectT["hasScript"] = [lua=context.mLua](const GObject& object, std::string_view path)
@ -200,7 +193,6 @@ namespace MWLua
using InventoryT = Inventory<ObjectT>; using InventoryT = Inventory<ObjectT>;
sol::usertype<InventoryT> inventoryT = context.mLua->sol().new_usertype<InventoryT>(prefix + "Inventory"); sol::usertype<InventoryT> inventoryT = context.mLua->sol().new_usertype<InventoryT>(prefix + "Inventory");
objectT["inventory"] = sol::readonly_property([](const ObjectT& o) { return InventoryT{o}; });
inventoryT[sol::meta_function::to_string] = inventoryT[sol::meta_function::to_string] =
[](const InventoryT& inv) { return "Inventory[" + inv.mObj.toString() + "]"; }; [](const InventoryT& inv) { return "Inventory[" + inv.mObj.toString() + "]"; };

View file

@ -83,7 +83,11 @@ namespace MWLua
return MWBase::Environment::get().getWorld()->isSwimming(o.ptr()); return MWBase::Environment::get().getWorld()->isSwimming(o.ptr());
}; };
actor["getEquipment"] = [context](const Object& o) actor["inventory"] = sol::overload(
[](const LObject& o) { return Inventory<LObject>{o}; },
[](const GObject& o) { return Inventory<GObject>{o}; }
);
actor["equipment"] = [context](const Object& o)
{ {
const MWWorld::Ptr& ptr = o.ptr(); const MWWorld::Ptr& ptr = o.ptr();
sol::table equipment(context.mLua->sol(), sol::create); sol::table equipment(context.mLua->sol(), sol::create);
@ -109,12 +113,8 @@ namespace MWLua
MWWorld::InventoryStore& store = ptr.getClass().getInventoryStore(ptr); MWWorld::InventoryStore& store = ptr.getClass().getInventoryStore(ptr);
return store.isEquipped(item.ptr()); return store.isEquipped(item.ptr());
}; };
actor["setEquipment"] = [context](const sol::object& luaObj, const sol::table& equipment) actor["setEquipment"] = [context](const SelfObject& obj, const sol::table& equipment)
{ {
if (!luaObj.is<GObject>() && !luaObj.is<SelfObject>())
throw std::runtime_error("Incorrect type of the first argument. "
"Can be either self (in local scripts) or game object (in global scripts)");
const Object& obj = luaObj.as<Object>();
if (!obj.ptr().getClass().hasInventoryStore(obj.ptr())) if (!obj.ptr().getClass().hasInventoryStore(obj.ptr()))
{ {
if (!equipment.empty()) if (!equipment.empty())

View file

@ -0,0 +1,18 @@
#include "types.hpp"
#include "../luabindings.hpp"
namespace MWLua
{
static const MWWorld::Ptr& containerPtr(const Object& o) { return verifyType(ESM::REC_CONT, o.ptr()); }
void addContainerBindings(sol::table container, const Context& context)
{
container["content"] = sol::overload(
[](const LObject& o) { containerPtr(o); return Inventory<LObject>{o}; },
[](const GObject& o) { containerPtr(o); return Inventory<GObject>{o}; }
);
}
}

View file

@ -173,7 +173,7 @@ namespace MWLua
addType(ObjectTypeName::Repair, {ESM::REC_REPA}, ObjectTypeName::Item); addType(ObjectTypeName::Repair, {ESM::REC_REPA}, ObjectTypeName::Item);
addType(ObjectTypeName::Activator, {ESM::REC_ACTI}); addType(ObjectTypeName::Activator, {ESM::REC_ACTI});
addType(ObjectTypeName::Container, {ESM::REC_CONT}); addContainerBindings(addType(ObjectTypeName::Container, {ESM::REC_CONT}), context);
addDoorBindings(addType(ObjectTypeName::Door, {ESM::REC_DOOR}), context); addDoorBindings(addType(ObjectTypeName::Door, {ESM::REC_DOOR}), context);
addType(ObjectTypeName::Static, {ESM::REC_STAT}); addType(ObjectTypeName::Static, {ESM::REC_STAT});

View file

@ -24,6 +24,7 @@ namespace MWLua
sol::table initTypesPackage(const Context& context); sol::table initTypesPackage(const Context& context);
// used in initTypesPackage // used in initTypesPackage
void addContainerBindings(sol::table door, const Context& context);
void addDoorBindings(sol::table door, const Context& context); void addDoorBindings(sol::table door, const Context& context);
void addActorBindings(sol::table actor, const Context& context); void addActorBindings(sol::table actor, const Context& context);
} }