init.lua168 lines
--- does the enemy see one of our units. two answers kept apart: `seen` comes from a source
--- that knows (a modifier on the unit, the team mask when the client fills it), `maybe` from
--- geometry over enemies we can see ourselves. the client gets no "seen by enemy" bit for a
--- visible unit (#533, #1062: the mask reads 0, no other field follows the server), so a ward
--- hidden in the fog is never caught here; pass found ward spots in `opts.wards` to cover them.
---@class seen
local M = {}
local floor, sqrt = math.floor, math.sqrt
--- the enemy has vision of the target and true sight on it
M.EXACT_SEEN = { "modifier_bounty_hunter_track", "modifier_slardar_amplify_damage" }
--- true sight on an invisible unit; without one an invisible unit is hidden
M.TRUESIGHT = { "modifier_truesight", "modifier_item_dustofappearance" }
--- seconds `maybe` holds after the last viewer left, so a viewer on the range edge does not blink
M.HOLD = 0.25
--- seconds a unit's answer is reused (a draw asks every frame)
M.CACHE = 0.1
--- seconds the enemy list is reused
M.SCAN = 0.1
local S = { units = setmetatable({}, { __mode = "k" }), list = nil, list_t = -1e9, list_team = nil }
local function clock()
local ok, t = pcall(function() return globals.curtime() end)
return (ok and type(t) == "number") and t or os.clock()
end
local function level_of_z(z)
return type(z) == "number" and floor((z + 32) / 128) or nil
end
local function call(e, name)
local ok, v = pcall(function() return e[name](e) end)
if ok then return v end
return nil
end
local function has_any(e, names)
local ok, i = pcall(function() return e:has_any_modifier(names) end)
return ok and type(i) == "number" and i > 0
end
local function opposing(team)
if team == 2 then return 3 end
if team == 3 then return 2 end
return nil
end
-- visible, alive units of the opposing team that give vision
local function viewers(team, now)
if S.list and S.list_team == team and now - S.list_t < M.SCAN and now >= S.list_t then
return S.list
end
local opp, out = opposing(team), {}
local ok, all = pcall(function() return entities.npcs({ alive = true }) end)
if opp and ok and type(all) == "table" then
for _, e in ipairs(all) do
if call(e, "team") == opp and call(e, "is_dormant") == false then
local r = call(e, "current_vision_range")
if type(r) == "number" and r > 0 then
local p = call(e, "position")
if p then out[#out + 1] = { e = e, x = p.x, y = p.y, l = level_of_z(p.z), r = r } end
end
end
end
end
S.list, S.list_t, S.list_team = out, now, team
return out
end
--- the answer a source that knows gives, nil when none applies.
--- true: seen (team mask, track, corrosive haze, true sight on an invisible unit, slark's
--- passive). false: hidden (invisible with no true sight on it, slark's regen).
---@param u any Entity
---@return boolean|nil
---@return string|nil why
function M.exact(u)
if call(u, "visibility_tracked") == true then
local v = call(u, "visible_to_enemy")
if v == true or v == false then return v, "mask" end
end
if has_any(u, M.EXACT_SEEN) then return true, "modifier" end
if call(u, "is_invisible") == true then
if has_any(u, M.TRUESIGHT) then return true, "truesight" end
return false, "invisible"
end
-- the regen buff is on while the enemy does not see slark; it lags the loss of sight
if call(u, "unit_name") == "npc_dota_hero_slark" and (call(u, "level") or 0) >= 6 then
if has_any(u, { "modifier_slark_shadow_dance_passive_regen" }) then return false, "slark" end
if has_any(u, { "modifier_slark_shadow_dance_passive" }) then return true, "slark" end
end
return nil
end
--- the nearest enemy we can see whose vision range covers the unit, or a ward spot from
--- `wards` that does. range only, plus high ground: a viewer below the unit's level does not
--- count. trees are not traced, so this over-reports behind them.
---@param u any Entity
---@param wards? {x: number, y: number, z?: number, range: number}[]
---@param now? number
---@return any|nil viewer Entity, or the ward table
---@return number|nil dist
function M.viewer(u, wards, now)
now = now or clock()
local p, team = call(u, "position"), call(u, "team")
if not p or not team then return nil end
local ul = level_of_z(p.z)
local hull = call(u, "hull_radius") or 0
local best, bd
for _, v in ipairs(viewers(team, now)) do
local dx, dy = v.x - p.x, v.y - p.y
local d = sqrt(dx * dx + dy * dy)
if d <= v.r + hull and (bd == nil or d < bd) and not (ul and v.l and v.l < ul) and v.e ~= u then
best, bd = v.e, d
end
end
if wards then
for _, w in ipairs(wards) do
local dx, dy = w.x - p.x, w.y - p.y
local d = sqrt(dx * dx + dy * dy)
local wl = level_of_z(w.z)
if d <= (w.range or 0) + hull and (bd == nil or d < bd) and not (ul and wl and wl < ul) then
best, bd = w, d
end
end
end
return best, bd
end
--- `"seen"` from a source that knows, `"hidden"` when one says no, `"maybe"` when only
--- geometry says yes (held `HOLD` seconds), `"clear"` otherwise.
---@param u any Entity
---@param opts? {wards?: table[], now?: number}
---@return "seen"|"maybe"|"hidden"|"clear" state
---@return string|nil why
function M.state(u, opts)
opts = opts or {}
local now = opts.now or clock()
local c = S.units[u]
if c and now >= c.t and now - c.t < M.CACHE and c.wards == opts.wards then return c.state, c.why end
c = c or {}
local st, why
local ex, w = M.exact(u)
if ex == true then st, why = "seen", w
elseif ex == false then st, why = "hidden", w
else
local v = M.viewer(u, opts.wards, now)
if v then c.maybe_t = now end
if c.maybe_t and now >= c.maybe_t and now - c.maybe_t <= M.HOLD then
st, why = "maybe", v and (type(v) == "table" and "ward" or "unit") or "hold"
else
st = "clear"
end
end
c.t, c.state, c.why, c.wards = now, st, why, opts.wards
S.units[u] = c
return st, why
end
--- forget held answers (a new game, a reload).
function M.reset()
S.units = setmetatable({}, { __mode = "k" })
S.list = nil
end
return M