evolve
商店/共享库

seen

Does the enemy see one of our units.

hunnyc0 个脚本在用
支持源码公开
在脚本中使用
pack.json
"requires": { "seen": "^1.0" }
你的 lua
local seen = require("lib/seen")

玩家无需安装:脚本会自动带上库,取范围内的任一版本。发布前可在开发会话中修改 scripts/lib/seen/ 来试验。

API
seen.exact(u: any): boolean|nil, string|nil#

the answer a source that knows gives, nil when none applies.

参数
u any Entity
返回
boolean|nil
string|nil

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).

seen.viewer(u: any, wards?: {x: number, y: number, z?: number, range: number}[], now?: number): any|nil, number|nil#

the nearest enemy we can see whose vision range covers the unit, or a ward spot from

参数
u any Entity
wards {x: number, y: number, z?: number, range: number}[]
now number
返回
any|nil Entity, or the ward table
number|nil

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.

seen.state(u: any, opts?: {wards?: table[], now?: number}): "seen"|"maybe"|"hidden"|"clear", string|nil#

"seen" from a source that knows, "hidden" when one says no, "maybe" when only

参数
u any Entity
opts {wards?: table[], now?: number}
返回
"seen"|"maybe"|"hidden"|"clear"
string|nil

geometry says yes (held HOLD seconds), "clear" otherwise.

seen.reset()#

forget held answers (a new game, a reload).

介绍

lib/seen

Does the enemy see one of our units. Two answers are kept apart: seen comes from a source that knows, maybe from geometry over enemies we can see ourselves.

local Seen = require("lib/seen")
local st, why = Seen.state(me, { wards = spots })  -- "seen" | "hidden" | "maybe" | "clear"
local yes, src = Seen.exact(me)                     -- true / false / nil, "mask" | "modifier" | "truesight" | "invisible" | "slark"
local v, d = Seen.viewer(me, spots)                 -- the nearest enemy (or spot) with us in range
Seen.reset()

spots is optional: {x, y, z?, range} tables, for example m.ward_finder.spots from uvbe (lib/ward_finder wards backed by two or more sightings).

sources

  • seen: Track, Corrosive Haze (M.EXACT_SEEN); true sight or dust on an invisible unit (M.TRUESIGHT); Slark's passive at level 6+; the team mask when the client fills it.
  • hidden: an invisible unit with no true sight on it; Slark's regen buff.
  • maybe: a visible enemy unit with current_vision_range() covering us (plus our hull), on our high-ground level or above, or a spot from wards. Held M.HOLD (0.25 s) after the last viewer leaves.

The client gets no "seen by enemy" bit for a visible unit: m_iTeamVisibilityBitmask is not in the network serializer (#1075, demo send tables), so it reads 0 (#533) and no other replicated field follows the server (#1062, checked against CanBeSeenByAnyOpposingTeam() in the lab). C_DOTA_DataNonSpectator::m_bNPCVisibleState (one bit per entity index) is networked, but only our own team's data entity reaches the client and its bits are our team's vision: our hero's bit stays set with no enemy on the map, an enemy's clears when it leaves our vision (#1075, lab). e:visible_to_* answer nil. So a ward hidden in the fog is never caught unless lib/ward_finder located it, and trees are not traced, so maybe over-reports behind them.

measured (#1062, lab, server truth every frame)

maybe turns on within -1..3 ticks of the server and off 2..8 ticks after (the hold); 0 false episodes over 36 trials; one 2.8 s miss at the edge of a creep's range (773 u against 750). Logs: E:/evolve/logs/1062/.

readers (#1062)

Nothing outside this lib reads the team mask; tools/utea_harness/test_seen_readers_1062.lua greps for it and runs the readers below over a scene class.

reader asks why
uvbe exact + state the SEEN ring and the maybe ring
sand_king CM.cover exact false unseen drops unit-target threats and counts as cover
invoker Targeting.IsInvisible exact false, why invisible an enemy we cannot click
phantom_lancer seen_by_enemy seen or maybe the blend and the shuffle price a watcher
autopilot act_farm (camp_seen) seen or maybe leave a threatened camp a watcher covers
autopilot objectives EX.hunt seen or maybe no smoke on a body someone has in range
compat NPC.IsVisibleToEnemies exact, else true the safe direction, logged once
riki detection exact true while invisible SEEN refuses the commit; nil reads "unknown"
bounty_hunter detection exact true while invisible the same, plus the VISIBILITY UNKNOWN banner

Test: tools/lua/lua.exe scripts/lib/seen/selftest.lua (22 checks, a 225-case viewer grid).

源码

1.0.0 版本的代码,即客户端拿到的内容。包内字节码由这些文件编译而来。

init.lua168 行
--- 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
pack.json2 行
{ "kind": "lib", "version": "1.0.0" }
更新日志

这个作品没有版本说明。

使用它的脚本

还没有已发布的脚本需要它。

该作者的其他作品

该作者的其他作品

lotus

作者 hunnyc

The lotus pools: how many lotuses each holds and when the next one grows.

pack.json
"requires": { "lotus": "^1.0" }
你的 lua
local lotus = require("lib/lotus")
0 个脚本在用还没有已发布的版本

order_watch

作者 hunnyc

Watches the orders our scripts send and reports three invariants as a red line with the module names (#1184):

pack.json
"requires": { "order_watch": "^1.0" }
你的 lua
local order_watch = require("lib/order_watch")
0 个脚本在用v1.0.0

order_class

作者 hunnyc

The class an order carries into `hk_order` (#1103): reaction, chain, window, background, owner.

pack.json
"requires": { "order_class": "^1.0" }
你的 lua
local order_class = require("lib/order_class")
0 个脚本在用v1.0.0