<aside> ⚙

original code is by skrow42 from
https://www.nexusmods.com/morrowind/mods/57747 (SHOP (Thieving AI Overhaul)) under GNU Affero General Public License

</aside>

This is generalised case taking any angle. for example looker vodunius nuccius is used in seyda neen square. cone_angle_deg = 90 means that cone is 45 degrees of both sides of viewing direction. it’s generally half.

local self = require('openmw.self')
local nearby = require('openmw.nearby')
local util = require('openmw.util')

local function is_in_cone(caster, target,  cone_angle_deg, max_range)
    
    local toPlayer = caster.position - target.position
    local distance = toPlayer:length()

    if distance > max_range then return false end

    local npcForward = caster.rotation:apply(util.vector3(0, 1, 0))
    local angleToPlayer = npcForward:dot(toPlayer:normalize())
    
    local cone_angle_rad = math.rad(cone_angle_deg)
   
    --debug messages 
    --print(angleToPlayer)
    --print( -1 + cone_angle_rad/math.pi )
    
    return angleToPlayer < -1 + cone_angle_rad/math.pi
end

local function onUpdate()
  local targets = nearby.actors
  local caster = self.position
  local cone_angle_deg = 90
  local max_range = 300
  
  for a,_ in pairs(targets) do
    if targets[a].recordId == "vodunius nuccius" then
      local in_cone = is_in_cone( targets[a], self, cone_angle_deg  , max_range )
        if in_cone == true then
        print("looker", targets[a].recordId, "Target in cone:", in_cone, self.recordId )
      end
    end
  end
end

return { engineHandlers = { onUpdate = onUpdate } }