1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-21 05:39:42 +00:00

Add Lua functions to get the current load order and search objects by RefNum/FormId

This commit is contained in:
Petr Mikheev 2023-05-30 01:44:09 +02:00
parent 773669e69b
commit a778dff61d
5 changed files with 74 additions and 1 deletions

View file

@ -88,7 +88,7 @@ namespace MWLua
{
auto* lua = context.mLua;
sol::table api(lua->sol(), sol::create);
api["API_REVISION"] = 38;
api["API_REVISION"] = 39;
api["quit"] = [lua]() {
Log(Debug::Warning) << "Quit requested by a Lua script.\n" << lua->debugTraceback();
MWBase::Environment::get().getStateManager()->requestQuit();
@ -97,6 +97,28 @@ namespace MWLua
context.mLuaEvents->addGlobalEvent(
{ std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer) });
};
api["getContentList"] = [](sol::this_state lua) -> sol::table {
const std::vector<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
sol::table res(lua, sol::create);
int i = 1;
for (const std::string& s : contentList)
res[i++] = Misc::StringUtils::lowerCase(s);
return res;
};
api["getContentFileIndex"] = [](std::string_view contentFile) -> sol::optional<int> {
const std::vector<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
for (size_t i = 0; i < contentList.size(); ++i)
if (Misc::StringUtils::ciEqual(contentList[i], contentFile))
return i + 1;
return sol::nullopt;
};
api["getFormId"] = [](std::string_view contentFile, unsigned int index) -> std::string {
const std::vector<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
for (size_t i = 0; i < contentList.size(); ++i)
if (Misc::StringUtils::ciEqual(contentList[i], contentFile))
return ESM::RefId(ESM::FormIdRefId(ESM::FormId{ index, int(i) })).serializeText();
throw std::runtime_error("Content file not found: " + std::string(contentFile));
};
addTimeBindings(api, context, false);
api["magic"] = initCoreMagicBindings(context);
api["l10n"] = LuaUtil::initL10nLoader(lua->sol(), MWBase::Environment::get().getL10nManager());
@ -189,6 +211,12 @@ namespace MWLua
MWWorld::Ptr newPtr = ptr.getClass().copyToCell(ptr, *cell, count.value_or(1));
return GObject(newPtr);
};
api["getObjectByFormId"] = [](std::string_view formIdStr) -> GObject {
ESM::RefId refId = ESM::RefId::deserializeText(formIdStr);
if (!refId.is<ESM::FormIdRefId>())
throw std::runtime_error("FormId expected, got " + std::string(formIdStr) + "; use core.getFormId");
return GObject(refId.getIf<ESM::FormIdRefId>()->getValue());
};
// Creates a new record in the world database.
api["createRecord"] = sol::overload(

View file

@ -124,6 +124,13 @@ namespace MWLua
});
};
api["getObjectByFormId"] = [](std::string_view formIdStr) -> LObject {
ESM::RefId refId = ESM::RefId::deserializeText(formIdStr);
if (!refId.is<ESM::FormIdRefId>())
throw std::runtime_error("FormId expected, got " + std::string(formIdStr) + "; use core.getFormId");
return LObject(refId.getIf<ESM::FormIdRefId>()->getValue());
};
api["activators"] = LObjectList{ worldView->getActivatorsInScene() };
api["actors"] = LObjectList{ worldView->getActorsInScene() };
api["containers"] = LObjectList{ worldView->getContainersInScene() };

View file

@ -102,6 +102,30 @@
-- print( myMsg('Hello {name}!', {name='World'}) )
---
-- Return the current load order (list of content file names).
-- @function [parent=#core] getContentList
-- @return #list<#string>
---
-- Return the index of a specific content file in the load order (or `nil` if there is no such content file).
-- @function [parent=#core] getContentFileIndex
-- @param #string contentFile
-- @return #number
---
-- Construct FormId string from content file name and the index in the file.
-- @function [parent=#core] getFormId
-- @param #string contentFile
-- @param #number index
-- @return #string
-- @usage if obj.recordId == core.getFormId('Skyrim.esm', 0x4d7da) then ... end
-- @usage -- local scripts
-- local obj = nearby.getObjectByFormId(core.getFormId('Morrowind.esm', 128964))
-- @usage -- global scripts
-- local obj = world.getObjectByFormId(core.getFormId('Morrowind.esm', 128964))
---
-- Any object that exists in the game world and has a specific location.
-- Player, actors, items, and statics are game objects.

View file

@ -26,6 +26,13 @@
-- Everything that can be picked up in the nearby.
-- @field [parent=#nearby] openmw.core#ObjectList items
---
-- Return an object by RefNum/FormId.
-- @function [parent=#nearby] getObjectByFormId
-- @param #string formId String returned by `core.getFormId`
-- @return openmw.core#GameObject
-- @usage local obj = nearby.getObjectByFormId(core.getFormId('Morrowind.esm', 128964))
---
-- @type COLLISION_TYPE
-- @field [parent=#COLLISION_TYPE] #number World

View file

@ -65,6 +65,13 @@
-- @function [parent=#world] isWorldPaused
-- @return #boolean
---
-- Return an object by RefNum/FormId.
-- @function [parent=#world] getObjectByFormId
-- @param #string formId String returned by `core.getFormId`
-- @return openmw.core#GameObject
-- @usage local obj = world.getObjectByFormId(core.getFormId('Morrowind.esm', 128964))
---
-- Create a new instance of the given record.
-- After creation the object is in the disabled state. Use :teleport to place to the world or :moveInto to put it into a container or an inventory.