1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 18:35:20 +00:00

Merge branch 'lua' into 'master'

Replace `inventory:get<Type>()` with `inventory:getAll(<Type>)`

See merge request OpenMW/openmw!1624
This commit is contained in:
psi29a 2022-02-03 22:35:36 +00:00
commit c3e07619a4
5 changed files with 141 additions and 172 deletions

View File

@ -16,10 +16,10 @@
namespace MWLua
{
static sol::table definitionList(LuaUtil::LuaState& lua, std::initializer_list<std::string> values)
static sol::table definitionList(LuaUtil::LuaState& lua, std::initializer_list<std::string_view> values)
{
sol::table res(lua.sol(), sol::create);
for (const std::string& v : values)
for (const std::string_view& v : values)
res[v] = v;
return LuaUtil::makeReadOnly(res);
}
@ -49,7 +49,7 @@ namespace MWLua
{
auto* lua = context.mLua;
sol::table api(lua->sol(), sol::create);
api["API_REVISION"] = 15;
api["API_REVISION"] = 16;
api["quit"] = [lua]()
{
Log(Debug::Warning) << "Quit requested by a Lua script.\n" << lua->debugTraceback();
@ -62,8 +62,11 @@ namespace MWLua
addTimeBindings(api, context, false);
api["OBJECT_TYPE"] = definitionList(*lua,
{
"Activator", "Armor", "Book", "Clothing", "Creature", "Door", "Ingredient",
"Light", "Miscellaneous", "NPC", "Player", "Potion", "Static", "Weapon"
ObjectTypeName::Activator, ObjectTypeName::Armor, ObjectTypeName::Book, ObjectTypeName::Clothing,
ObjectTypeName::Creature, ObjectTypeName::Door, ObjectTypeName::Ingredient, ObjectTypeName::Light,
ObjectTypeName::MiscItem, ObjectTypeName::NPC, ObjectTypeName::Player, ObjectTypeName::Potion,
ObjectTypeName::Static, ObjectTypeName::Weapon, ObjectTypeName::Activator, ObjectTypeName::Lockpick,
ObjectTypeName::Probe, ObjectTypeName::Repair
});
api["EQUIPMENT_SLOT"] = LuaUtil::makeReadOnly(context.mLua->tableFromPairs<std::string_view, int>({
{"Helmet", MWWorld::InventoryStore::Slot_Helmet},

View File

@ -17,20 +17,24 @@ namespace MWLua
};
const static std::unordered_map<ESM::RecNameInts, LuaObjectTypeInfo> luaObjectTypeInfo = {
{ESM::REC_ACTI, {"Activator", ESM::LuaScriptCfg::sActivator}},
{ESM::REC_ARMO, {"Armor", ESM::LuaScriptCfg::sArmor}},
{ESM::REC_BOOK, {"Book", ESM::LuaScriptCfg::sBook}},
{ESM::REC_CLOT, {"Clothing", ESM::LuaScriptCfg::sClothing}},
{ESM::REC_CONT, {"Container", ESM::LuaScriptCfg::sContainer}},
{ESM::REC_CREA, {"Creature", ESM::LuaScriptCfg::sCreature}},
{ESM::REC_DOOR, {"Door", ESM::LuaScriptCfg::sDoor}},
{ESM::REC_INGR, {"Ingredient", ESM::LuaScriptCfg::sIngredient}},
{ESM::REC_LIGH, {"Light", ESM::LuaScriptCfg::sLight}},
{ESM::REC_MISC, {"Miscellaneous", ESM::LuaScriptCfg::sMiscItem}},
{ESM::REC_NPC_, {"NPC", ESM::LuaScriptCfg::sNPC}},
{ESM::REC_ALCH, {"Potion", ESM::LuaScriptCfg::sPotion}},
{ESM::REC_STAT, {"Static"}},
{ESM::REC_WEAP, {"Weapon", ESM::LuaScriptCfg::sWeapon}},
{ESM::REC_ACTI, {ObjectTypeName::Activator, ESM::LuaScriptCfg::sActivator}},
{ESM::REC_ARMO, {ObjectTypeName::Armor, ESM::LuaScriptCfg::sArmor}},
{ESM::REC_BOOK, {ObjectTypeName::Book, ESM::LuaScriptCfg::sBook}},
{ESM::REC_CLOT, {ObjectTypeName::Clothing, ESM::LuaScriptCfg::sClothing}},
{ESM::REC_CONT, {ObjectTypeName::Container, ESM::LuaScriptCfg::sContainer}},
{ESM::REC_CREA, {ObjectTypeName::Creature, ESM::LuaScriptCfg::sCreature}},
{ESM::REC_DOOR, {ObjectTypeName::Door, ESM::LuaScriptCfg::sDoor}},
{ESM::REC_INGR, {ObjectTypeName::Ingredient, ESM::LuaScriptCfg::sIngredient}},
{ESM::REC_LIGH, {ObjectTypeName::Light, ESM::LuaScriptCfg::sLight}},
{ESM::REC_MISC, {ObjectTypeName::MiscItem, ESM::LuaScriptCfg::sMiscItem}},
{ESM::REC_NPC_, {ObjectTypeName::NPC, ESM::LuaScriptCfg::sNPC}},
{ESM::REC_ALCH, {ObjectTypeName::Potion, ESM::LuaScriptCfg::sPotion}},
{ESM::REC_STAT, {ObjectTypeName::Static}},
{ESM::REC_WEAP, {ObjectTypeName::Weapon, ESM::LuaScriptCfg::sWeapon}},
{ESM::REC_APPA, {ObjectTypeName::Apparatus}},
{ESM::REC_LOCK, {ObjectTypeName::Lockpick}},
{ESM::REC_PROB, {ObjectTypeName::Probe}},
{ESM::REC_REPA, {ObjectTypeName::Repair}},
};
std::string_view getLuaObjectTypeName(ESM::RecNameInts type, std::string_view fallback)

View File

@ -14,6 +14,31 @@
namespace MWLua
{
namespace ObjectTypeName
{
// Names of object types in Lua.
// These names are part of OpenMW Lua API.
constexpr std::string_view Activator = "Activator";
constexpr std::string_view Armor = "Armor";
constexpr std::string_view Book = "Book";
constexpr std::string_view Clothing = "Clothing";
constexpr std::string_view Container = "Container";
constexpr std::string_view Creature = "Creature";
constexpr std::string_view Door = "Door";
constexpr std::string_view Ingredient = "Ingredient";
constexpr std::string_view Light = "Light";
constexpr std::string_view MiscItem = "Miscellaneous";
constexpr std::string_view NPC = "NPC";
constexpr std::string_view Player = "Player";
constexpr std::string_view Potion = "Potion";
constexpr std::string_view Static = "Static";
constexpr std::string_view Weapon = "Weapon";
constexpr std::string_view Apparatus = "Apparatus";
constexpr std::string_view Lockpick = "Lockpick";
constexpr std::string_view Probe = "Probe";
constexpr std::string_view Repair = "Repair";
}
// ObjectId is a unique identifier of a game object.
// It can change only if the order of content files was change.
using ObjectId = ESM::RefNum;

View File

@ -309,8 +309,38 @@ namespace MWLua
inventoryT[sol::meta_function::to_string] =
[](const InventoryT& inv) { return "Inventory[" + inv.mObj.toString() + "]"; };
auto getWithMask = [context](const InventoryT& inventory, int mask)
inventoryT["getAll"] = [worldView=context.mWorldView](const InventoryT& inventory, sol::optional<std::string_view> type)
{
int mask;
if (!type.has_value())
mask = MWWorld::ContainerStore::Type_All;
else if (*type == ObjectTypeName::Potion)
mask = MWWorld::ContainerStore::Type_Potion;
else if (*type == ObjectTypeName::Armor)
mask = MWWorld::ContainerStore::Type_Armor;
else if (*type == ObjectTypeName::Book)
mask = MWWorld::ContainerStore::Type_Book;
else if (*type == ObjectTypeName::Clothing)
mask = MWWorld::ContainerStore::Type_Clothing;
else if (*type == ObjectTypeName::Ingredient)
mask = MWWorld::ContainerStore::Type_Ingredient;
else if (*type == ObjectTypeName::Light)
mask = MWWorld::ContainerStore::Type_Light;
else if (*type == ObjectTypeName::MiscItem)
mask = MWWorld::ContainerStore::Type_Miscellaneous;
else if (*type == ObjectTypeName::Weapon)
mask = MWWorld::ContainerStore::Type_Weapon;
else if (*type == ObjectTypeName::Apparatus)
mask = MWWorld::ContainerStore::Type_Apparatus;
else if (*type == ObjectTypeName::Lockpick)
mask = MWWorld::ContainerStore::Type_Lockpick;
else if (*type == ObjectTypeName::Probe)
mask = MWWorld::ContainerStore::Type_Probe;
else if (*type == ObjectTypeName::Repair)
mask = MWWorld::ContainerStore::Type_Repair;
else
throw std::runtime_error(std::string("inventory:getAll doesn't support type " + std::string(*type)));
const MWWorld::Ptr& ptr = inventory.mObj.ptr();
MWWorld::ContainerStore& store = ptr.getClass().getContainerStore(ptr);
ObjectIdList list = std::make_shared<std::vector<ObjectId>>();
@ -318,39 +348,12 @@ namespace MWLua
while (it.getType() != -1)
{
const MWWorld::Ptr& item = *(it++);
context.mWorldView->getObjectRegistry()->registerPtr(item);
worldView->getObjectRegistry()->registerPtr(item);
list->push_back(getId(item));
}
return ObjectList<ObjectT>{list};
};
inventoryT["getAll"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_All); };
inventoryT["getPotions"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Potion); };
inventoryT["getApparatuses"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Apparatus); };
inventoryT["getArmor"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Armor); };
inventoryT["getBooks"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Book); };
inventoryT["getClothing"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Clothing); };
inventoryT["getIngredients"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Ingredient); };
inventoryT["getLights"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Light); };
inventoryT["getLockpicks"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Lockpick); };
inventoryT["getMiscellaneous"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Miscellaneous); };
inventoryT["getProbes"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Probe); };
inventoryT["getRepairKits"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Repair); };
inventoryT["getWeapons"] =
[getWithMask](const InventoryT& inventory) { return getWithMask(inventory, MWWorld::ContainerStore::Type_Weapon); };
inventoryT["countOf"] = [](const InventoryT& inventory, const std::string& recordId)
{
const MWWorld::Ptr& ptr = inventory.mObj.ptr();

View File

@ -88,21 +88,24 @@
-------------------------------------------------------------------------------
-- @type OBJECT_TYPE
-- @field [parent=#OBJECT_TYPE] #string Activator "Activator"
-- @field [parent=#OBJECT_TYPE] #string Armor "Armor"
-- @field [parent=#OBJECT_TYPE] #string Book "Book"
-- @field [parent=#OBJECT_TYPE] #string Clothing "Clothing"
-- @field [parent=#OBJECT_TYPE] #string Container "Container"
-- @field [parent=#OBJECT_TYPE] #string Creature "Creature"
-- @field [parent=#OBJECT_TYPE] #string Door "Door"
-- @field [parent=#OBJECT_TYPE] #string Ingredient "Ingredient"
-- @field [parent=#OBJECT_TYPE] #string Light "Light"
-- @field [parent=#OBJECT_TYPE] #string Miscellaneous "Miscellaneous"
-- @field [parent=#OBJECT_TYPE] #string NPC "NPC"
-- @field [parent=#OBJECT_TYPE] #string Player "Player"
-- @field [parent=#OBJECT_TYPE] #string Potion "Potion"
-- @field [parent=#OBJECT_TYPE] #string Static "Static"
-- @field [parent=#OBJECT_TYPE] #string Weapon "Weapon"
-- @field #string Activator "Activator"
-- @field #string Armor "Armor"
-- @field #string Book "Book"
-- @field #string Clothing "Clothing"
-- @field #string Container "Container"
-- @field #string Creature "Creature"
-- @field #string Door "Door"
-- @field #string Ingredient "Ingredient"
-- @field #string Light "Light"
-- @field #string Miscellaneous "Miscellaneous"
-- @field #string NPC "NPC"
-- @field #string Player "Player"
-- @field #string Potion "Potion"
-- @field #string Static "Static"
-- @field #string Apparatus "Apparatus"
-- @field #string Lockpick "Lockpick"
-- @field #string Probe "Probe"
-- @field #string Repair "Repair"
-------------------------------------------------------------------------------
-- Possible `object.type` values.
@ -111,25 +114,25 @@
-------------------------------------------------------------------------------
-- @type EQUIPMENT_SLOT
-- @field [parent=#EQUIPMENT_SLOT] #number Helmet
-- @field [parent=#EQUIPMENT_SLOT] #number Cuirass
-- @field [parent=#EQUIPMENT_SLOT] #number Greaves
-- @field [parent=#EQUIPMENT_SLOT] #number LeftPauldron
-- @field [parent=#EQUIPMENT_SLOT] #number RightPauldron
-- @field [parent=#EQUIPMENT_SLOT] #number LeftGauntlet
-- @field [parent=#EQUIPMENT_SLOT] #number RightGauntlet
-- @field [parent=#EQUIPMENT_SLOT] #number Boots
-- @field [parent=#EQUIPMENT_SLOT] #number Shirt
-- @field [parent=#EQUIPMENT_SLOT] #number Pants
-- @field [parent=#EQUIPMENT_SLOT] #number Skirt
-- @field [parent=#EQUIPMENT_SLOT] #number Robe
-- @field [parent=#EQUIPMENT_SLOT] #number LeftRing
-- @field [parent=#EQUIPMENT_SLOT] #number RightRing
-- @field [parent=#EQUIPMENT_SLOT] #number Amulet
-- @field [parent=#EQUIPMENT_SLOT] #number Belt
-- @field [parent=#EQUIPMENT_SLOT] #number CarriedRight
-- @field [parent=#EQUIPMENT_SLOT] #number CarriedLeft
-- @field [parent=#EQUIPMENT_SLOT] #number Ammunition
-- @field #number Helmet
-- @field #number Cuirass
-- @field #number Greaves
-- @field #number LeftPauldron
-- @field #number RightPauldron
-- @field #number LeftGauntlet
-- @field #number RightGauntlet
-- @field #number Boots
-- @field #number Shirt
-- @field #number Pants
-- @field #number Skirt
-- @field #number Robe
-- @field #number LeftRing
-- @field #number RightRing
-- @field #number Amulet
-- @field #number Belt
-- @field #number CarriedRight
-- @field #number CarriedLeft
-- @field #number Ammunition
-------------------------------------------------------------------------------
-- Available equipment slots. Used in `object:getEquipment` and `object:setEquipment`.
@ -140,17 +143,17 @@
-- Any object that exists in the game world and has a specific location.
-- Player, actors, items, and statics are game objects.
-- @type GameObject
-- @field [parent=#GameObject] openmw.util#Vector3 position Object position.
-- @field [parent=#GameObject] openmw.util#Vector3 rotation Object rotation (ZXY order).
-- @field [parent=#GameObject] #Cell cell The cell where the object currently is. During loading a game and for objects in an inventory or a container `cell` is nil.
-- @field [parent=#GameObject] #string type Type of the object (see @{openmw.core#OBJECT_TYPE}).
-- @field [parent=#GameObject] #number count Count (makes sense if holded in a container).
-- @field [parent=#GameObject] #string recordId Record ID.
-- @field [parent=#GameObject] #Inventory inventory Inventory of an Player/NPC or content of an container.
-- @field [parent=#GameObject] #boolean isTeleport `True` if it is a teleport door (only if `object.type` == "Door").
-- @field [parent=#GameObject] openmw.util#Vector3 destPosition Destination (only if a teleport door).
-- @field [parent=#GameObject] openmw.util#Vector3 destRotation Destination rotation (only if a teleport door).
-- @field [parent=#GameObject] #string destCell Destination cell (only if a teleport door).
-- @field openmw.util#Vector3 position Object position.
-- @field openmw.util#Vector3 rotation Object rotation (ZXY order).
-- @field #Cell cell The cell where the object currently is. During loading a game and for objects in an inventory or a container `cell` is nil.
-- @field #string type Type of the object (see @{openmw.core#OBJECT_TYPE}).
-- @field #number count Count (makes sense if holded in a container).
-- @field #string recordId Record ID.
-- @field #Inventory inventory Inventory of an Player/NPC or content of an container.
-- @field #boolean isTeleport `True` if it is a teleport door (only if `object.type` == "Door").
-- @field openmw.util#Vector3 destPosition Destination (only if a teleport door).
-- @field openmw.util#Vector3 destRotation Destination rotation (only if a teleport door).
-- @field #string destCell Destination cell (only if a teleport door).
-------------------------------------------------------------------------------
-- Is the object still exists/available.
@ -306,12 +309,12 @@
-------------------------------------------------------------------------------
-- A cell of the game world.
-- @type Cell
-- @field [parent=#Cell] #string name Name of the cell (can be empty string).
-- @field [parent=#Cell] #string region Region of the cell.
-- @field [parent=#Cell] #boolean isExterior Is it exterior or interior.
-- @field [parent=#Cell] #number gridX Index of the cell by X (only for exteriors).
-- @field [parent=#Cell] #number gridY Index of the cell by Y (only for exteriors).
-- @field [parent=#Cell] #boolean hasWater True if the cell contains water.
-- @field #string name Name of the cell (can be empty string).
-- @field #string region Region of the cell.
-- @field #boolean isExterior Is it exterior or interior.
-- @field #number gridX Index of the cell by X (only for exteriors).
-- @field #number gridY Index of the cell by Y (only for exteriors).
-- @field #boolean hasWater True if the cell contains water.
-------------------------------------------------------------------------------
-- Returns true either if the cell contains the object or if the cell is an exterior and the object is also in an exterior.
@ -347,82 +350,13 @@
-- @return #number
-------------------------------------------------------------------------------
-- Get all content of the inventory.
-- Get all items of given type from the inventory.
-- @function [parent=#Inventory] getAll
-- @param self
-- @param type (optional) items type (see @{openmw.core#OBJECT_TYPE})
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all potions.
-- @function [parent=#Inventory] getPotions
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all apparatuses.
-- @function [parent=#Inventory] getApparatuses
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all armor.
-- @function [parent=#Inventory] getArmor
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all books.
-- @function [parent=#Inventory] getBooks
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all clothing.
-- @function [parent=#Inventory] getClothing
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all ingredients.
-- @function [parent=#Inventory] getIngredients
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all lights.
-- @function [parent=#Inventory] getLights
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all lockpicks.
-- @function [parent=#Inventory] getLockpicks
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all miscellaneous items.
-- @function [parent=#Inventory] getMiscellaneous
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all probes.
-- @function [parent=#Inventory] getProbes
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all repair kits.
-- @function [parent=#Inventory] getRepairKits
-- @param self
-- @return #ObjectList
-------------------------------------------------------------------------------
-- Get all weapon.
-- @function [parent=#Inventory] getWeapons
-- @param self
-- @return #ObjectList
-- @usage local all = inventory:getAll()
-- local weapons = inventory:getAll(core.OBJECT_TYPE.Weapon)
return nil