-- comment key [P] denotes a performance improvement (however small) -- [L] denotes a 'Lua'ism, i.e, a language feature function PlayerLookingAt (e, dist, fov) -- localise the global list entry for this entity [P] local Ent = g_Entity[e] if Ent == nil then return false end -- how to provide default inputs [L] dist = dist or 3000 fov = fov or 1 -- alternate way of doing ['value'] [L] local Dx = Ent.x - g_PlayerPosX local Dz = Ent.z - g_PlayerPosZ -- high performance distance calculation, avoids need for square root -- only continue function if horizontal distance to entity less than -- specified limit (or default is one wasn't specified) [P] if (Dx*Dx + Dz*Dz) > dist*dist then return false end -- define functions on the fly! [L] local function limitAngle (Angle) if Angle <= 0 then Angle = 360 + Angle elseif Angle > 360 then Angle = Angle - 360 end return Angle end -- define local variable holding value of horizontal angle from -- player to entity local angle = limitAngle(math.atan2(Dx, Dz) * (180.0 / math.pi)) -- localise the global holding Player Angle [P] local pAng = g_PlayerAngY -- In some versions of GameGuru the player angle isn't limited -- to 360 degrees so this fixes it if needed. while pAng < 0 or pAng > 360 do if pAng <= 0 then pAng = 360 + pAng elseif pAng > 360 then pAng = pAng - 360 end end -- Calculate left and right angles from user specified field of view -- value (or default is one wasn't specified) local L = limitAngle(angle - fov / 2) local R = limitAngle(angle + fov / 2) -- Is player angle within left and right limits? -- (if you find this bit confusing grab some paper and a pencil and -- draw it) if L < R and (pAng > L and pAng < R) or L > R and (pAng > L or pAng < R) then return true else return false end end