From 40c87837902358cff5033fb59bf02a0760073f4e Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Tue, 26 Sep 2023 18:09:52 +0400 Subject: [PATCH] Handle actors processing range in Lua --- apps/openmw/mwlua/types/actor.cpp | 20 ++++++++++++++++++++ files/lua_api/openmw/types.lua | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/apps/openmw/mwlua/types/actor.cpp b/apps/openmw/mwlua/types/actor.cpp index 6edb363cd0..58c4d83cf4 100644 --- a/apps/openmw/mwlua/types/actor.cpp +++ b/apps/openmw/mwlua/types/actor.cpp @@ -4,9 +4,11 @@ #include #include +#include #include "apps/openmw/mwbase/mechanicsmanager.hpp" #include "apps/openmw/mwbase/windowmanager.hpp" +#include "apps/openmw/mwmechanics/actorutil.hpp" #include "apps/openmw/mwmechanics/creaturestats.hpp" #include "apps/openmw/mwmechanics/drawstate.hpp" #include "apps/openmw/mwworld/class.hpp" @@ -374,6 +376,24 @@ namespace MWLua result["halfExtents"] = agentBounds.mHalfExtents; return result; }; + actor["isInActorsProcessingRange"] = [](const Object& o) { + const MWWorld::Ptr player = MWMechanics::getPlayer(); + const auto& target = o.ptr(); + if (target == player) + return true; + + if (!target.getClass().isActor()) + throw std::runtime_error("Actor expected"); + + if (target.getCell()->getCell()->getWorldSpace() != player.getCell()->getCell()->getWorldSpace()) + return false; + + const int actorsProcessingRange = Settings::game().mActorsProcessingRange; + const osg::Vec3f playerPos = player.getRefData().getPosition().asVec3(); + + const float dist = (playerPos - target.getRefData().getPosition().asVec3()).length(); + return dist <= actorsProcessingRange; + }; addActorStatsBindings(actor, context); addActorMagicBindings(actor, context); diff --git a/files/lua_api/openmw/types.lua b/files/lua_api/openmw/types.lua index df6e17c6f4..e86ae993c6 100644 --- a/files/lua_api/openmw/types.lua +++ b/files/lua_api/openmw/types.lua @@ -15,6 +15,12 @@ -- @param openmw.core#GameObject actor -- @return #table with `shapeType` and `halfExtents` +--- +-- Check if given actor is in the actors processing range. +-- @function [parent=#Actor] isInActorsProcessingRange +-- @param openmw.core#GameObject actor +-- @return #boolean + --- -- Whether the object is an actor. -- @function [parent=#Actor] objectIsInstance