diff --git a/apps/openmw/mwlua/types/actor.hpp b/apps/openmw/mwlua/types/actor.hpp new file mode 100644 index 0000000000..409559475f --- /dev/null +++ b/apps/openmw/mwlua/types/actor.hpp @@ -0,0 +1,52 @@ +#ifndef MWLUA_ACTOR_H +#define MWLUA_ACTOR_H + +#include + +#include +#include + +#include "apps/openmw/mwworld/esmstore.hpp" +#include +#include +#include +#include + +#include "../context.hpp" +#include "../object.hpp" +namespace MWLua +{ + + template + void addActorServicesBindings(sol::usertype& record, const Context& context) + { + record["servicesOffered"] = sol::readonly_property([context](const T& rec) -> sol::table { + sol::state_view& lua = context.mLua->sol(); + sol::table providedServices(lua, sol::create); + constexpr std::array, 19> serviceNames = { { { ESM::NPC::Spells, + "Spells" }, + { ESM::NPC::Spellmaking, "Spellmaking" }, { ESM::NPC::Enchanting, "Enchanting" }, + { ESM::NPC::Training, "Training" }, { ESM::NPC::Repair, "Repair" }, { ESM::NPC::AllItems, "Barter" }, + { ESM::NPC::Weapon, "Weapon" }, { ESM::NPC::Armor, "Armor" }, { ESM::NPC::Clothing, "Clothing" }, + { ESM::NPC::Books, "Books" }, { ESM::NPC::Ingredients, "Ingredients" }, { ESM::NPC::Picks, "Picks" }, + { ESM::NPC::Probes, "Probes" }, { ESM::NPC::Lights, "Lights" }, { ESM::NPC::Apparatus, "Apparatus" }, + { ESM::NPC::RepairItem, "RepairItem" }, { ESM::NPC::Misc, "Misc" }, { ESM::NPC::Potions, "Potions" }, + { ESM::NPC::MagicItems, "MagicItems" } } }; + + int services = rec.mAiData.mServices; + if constexpr (std::is_same_v) + { + if (rec.mFlags & ESM::NPC::Autocalc) + services + = MWBase::Environment::get().getESMStore()->get().find(rec.mClass)->mData.mServices; + } + for (const auto& [flag, name] : serviceNames) + { + providedServices[name] = (services & flag) != 0; + } + providedServices["Travel"] = !rec.getTransport().empty(); + return providedServices; + }); + } +} +#endif // MWLUA_ACTOR_H diff --git a/apps/openmw/mwlua/types/creature.cpp b/apps/openmw/mwlua/types/creature.cpp index 08c0dd021c..332a9b9b14 100644 --- a/apps/openmw/mwlua/types/creature.cpp +++ b/apps/openmw/mwlua/types/creature.cpp @@ -1,3 +1,4 @@ +#include "actor.hpp" #include "types.hpp" #include @@ -48,5 +49,6 @@ namespace MWLua record["soulValue"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mSoul; }); record["type"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mType; }); record["baseGold"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mGold; }); + addActorServicesBindings(record, context); } } diff --git a/apps/openmw/mwlua/types/npc.cpp b/apps/openmw/mwlua/types/npc.cpp index d1a91cabc9..e612f228e0 100644 --- a/apps/openmw/mwlua/types/npc.cpp +++ b/apps/openmw/mwlua/types/npc.cpp @@ -1,3 +1,4 @@ +#include "actor.hpp" #include "types.hpp" #include @@ -48,6 +49,7 @@ namespace MWLua = sol::readonly_property([](const ESM::NPC& rec) -> std::string { return rec.mHead.serializeText(); }); record["isMale"] = sol::readonly_property([](const ESM::NPC& rec) -> bool { return rec.isMale(); }); record["baseGold"] = sol::readonly_property([](const ESM::NPC& rec) -> int { return rec.mNpdt.mGold; }); + addActorServicesBindings(record, context); // This function is game-specific, in future we should replace it with something more universal. npc["isWerewolf"] = [](const Object& o) { diff --git a/files/lua_api/openmw/types.lua b/files/lua_api/openmw/types.lua index ea5368efde..557bbabd86 100644 --- a/files/lua_api/openmw/types.lua +++ b/files/lua_api/openmw/types.lua @@ -701,6 +701,7 @@ -- @field #number soulValue The soul value of the creature record -- @field #number type The @{#Creature.TYPE} of the creature -- @field #number baseGold The base barter gold of the creature +-- @field #list<#string> servicesOffered The services of the creature, in a table. Value is if the service is provided or not, and they are indexed by: Spells, Spellmaking, Enchanting, Training, Repair, Barter, Weapon, Armor, Clothing, Books, Ingredients, Picks, Probes, Lights, Apparatus, RepairItems, Misc, Potions, MagicItems, Travel. --- @{#NPC} functions @@ -750,6 +751,7 @@ -- @field #number baseGold The base barter gold of the NPC -- @field #number baseDisposition NPC's starting disposition -- @field #bool isMale The gender setting of the NPC +-- @field #list<#string> servicesOffered The services of the NPC, in a table. Value is if the service is provided or not, and they are indexed by: Spells, Spellmaking, Enchanting, Training, Repair, Barter, Weapon, Armor, Clothing, Books, Ingredients, Picks, Probes, Lights, Apparatus, RepairItems, Misc, Potions, MagicItems, Travel. --------------------------------------------------------------------------------