diff --git a/apps/openmw/mwlua/luabindings.cpp b/apps/openmw/mwlua/luabindings.cpp index 153b753790..46fb613233 100644 --- a/apps/openmw/mwlua/luabindings.cpp +++ b/apps/openmw/mwlua/luabindings.cpp @@ -222,6 +222,7 @@ namespace MWLua addTimeBindings(api, context, true); addCellGetters(api, context); api["activeActors"] = GObjectList{ worldView->getActorsInScene() }; + api["players"] = GObjectList{ worldView->getPlayers() }; api["createObject"] = [](std::string_view recordId, sol::optional count) -> GObject { // Doesn't matter which cell to use because the new object will be in disabled state. MWWorld::CellStore* cell = MWBase::Environment::get().getWorldScene()->getCurrentCell(); diff --git a/apps/openmw/mwlua/luamanagerimp.cpp b/apps/openmw/mwlua/luamanagerimp.cpp index 9bd8138206..74bd678005 100644 --- a/apps/openmw/mwlua/luamanagerimp.cpp +++ b/apps/openmw/mwlua/luamanagerimp.cpp @@ -246,6 +246,7 @@ namespace MWLua return; if (!mPlayer.isEmpty()) throw std::logic_error("Player is initialized twice"); + mWorldView.setPlayer(ptr); mWorldView.objectAddedToScene(ptr); mPlayer = ptr; LocalScripts* localScripts = ptr.getRefData().getLuaScripts(); diff --git a/apps/openmw/mwlua/nearbybindings.cpp b/apps/openmw/mwlua/nearbybindings.cpp index 88c4907d81..876cffb3d7 100644 --- a/apps/openmw/mwlua/nearbybindings.cpp +++ b/apps/openmw/mwlua/nearbybindings.cpp @@ -136,6 +136,7 @@ namespace MWLua api["containers"] = LObjectList{ worldView->getContainersInScene() }; api["doors"] = LObjectList{ worldView->getDoorsInScene() }; api["items"] = LObjectList{ worldView->getItemsInScene() }; + api["players"] = LObjectList{ worldView->getPlayers() }; api["NAVIGATOR_FLAGS"] = LuaUtil::makeStrictReadOnly(context.mLua->tableFromPairs({ diff --git a/apps/openmw/mwlua/worldview.hpp b/apps/openmw/mwlua/worldview.hpp index 5929429409..7efb7c0f01 100644 --- a/apps/openmw/mwlua/worldview.hpp +++ b/apps/openmw/mwlua/worldview.hpp @@ -51,10 +51,13 @@ namespace MWLua ObjectIdList getContainersInScene() const { return mContainersInScene.mList; } ObjectIdList getDoorsInScene() const { return mDoorsInScene.mList; } ObjectIdList getItemsInScene() const { return mItemsInScene.mList; } + ObjectIdList getPlayers() const { return mPlayers; } void objectAddedToScene(const MWWorld::Ptr& ptr); void objectRemovedFromScene(const MWWorld::Ptr& ptr); + void setPlayer(const MWWorld::Ptr& player) { *mPlayers = { getId(player) }; } + void load(ESM::ESMReader& esm); void save(ESM::ESMWriter& esm) const; @@ -78,6 +81,7 @@ namespace MWLua ObjectGroup mContainersInScene; ObjectGroup mDoorsInScene; ObjectGroup mItemsInScene; + ObjectIdList mPlayers = std::make_shared>(); double mSimulationTime = 0; bool mPaused = false; diff --git a/files/data/scripts/omw/cellhandlers.lua b/files/data/scripts/omw/cellhandlers.lua index dc93af7e05..03dbc55e34 100644 --- a/files/data/scripts/omw/cellhandlers.lua +++ b/files/data/scripts/omw/cellhandlers.lua @@ -20,11 +20,7 @@ local function getRandomOffset() end local function getPlayerLevel() - for i, actor in pairs(world.activeActors) do - if (types.Player.objectIsInstance(actor)) then - return types.Player.stats.level(actor).current - end - end + return types.Player.stats.level(world.players[1]).current end local function spawnFish(cell) diff --git a/files/lua_api/openmw/nearby.lua b/files/lua_api/openmw/nearby.lua index bec284a10b..6f99418a07 100644 --- a/files/lua_api/openmw/nearby.lua +++ b/files/lua_api/openmw/nearby.lua @@ -26,6 +26,10 @@ -- Everything that can be picked up in the nearby. -- @field [parent=#nearby] openmw.core#ObjectList items +--- +-- List of nearby players. Currently (since multiplayer is not yet implemented) always has one element. +-- @field [parent=#nearby] openmw.core#ObjectList players + --- -- Return an object by RefNum/FormId. -- Note: the function always returns @{openmw.core#GameObject} and doesn't validate that diff --git a/files/lua_api/openmw/world.lua b/files/lua_api/openmw/world.lua index 5f7a46f284..494d0e9353 100644 --- a/files/lua_api/openmw/world.lua +++ b/files/lua_api/openmw/world.lua @@ -10,6 +10,10 @@ -- List of currently active actors. -- @field [parent=#world] openmw.core#ObjectList activeActors +--- +-- List of players. Currently (since multiplayer is not yet implemented) always has one element. +-- @field [parent=#world] openmw.core#ObjectList players + --- -- Loads a named cell -- @function [parent=#world] getCellByName