evolve
商店/共享库

ward_finder

Where an enemy ward we cannot see stands, from the places where the enemy had vision of us and the places where it did not.

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

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

API
ward_finder.reading(exact?: boolean|nil, why?: string|nil): boolean|nil#

the seen argument of add from lib/seen's exact answer: a mask or slark's passive is about

参数
exact boolean|nil Seen.exact(u) first return
why string|nil Seen.exact(u) second return
返回
boolean|nil

vision in general, so it counts; track, haze, true sight and invisibility depend on a hero, a sentry or the unit itself, not on a ward's range, so they give nil (no sample).

ward_finder.add(s: table, p: Vector|table, seen?: boolean|nil, t: number, explained: boolean?): boolean#

one observation of our unit at p

参数
s table state from new
p Vector|table position: Vector, {x=, y=} or {x, y}
seen boolean|nil true: an enemy we cannot see had vision of us; false: nobody had; nil is ignored
t number game time in seconds
explained boolean? true on a seen sample a visible enemy accounts for (a creep, a hero, a tower in range); dropped
返回
boolean

a sample next to the last stored one on the same side is held back and stored only when the side flips, so both sides of every edge of the ward's sight are kept.

ward_finder.clear(s: table)#

forget every sample

参数
s table state from new
ward_finder.solve(s: table, now: number, max_wards: integer?): ward_finder.Result#

the wards the evidence points at, best first; cached until the next stored sample

参数
s table state from new
now number game time in seconds
max_wards integer? default 4
返回
ward_finder.Result

one ward per group of seen samples it explains; a seen sample no ward explains starts another. samples older than the lifetime are dropped here.

ward_finder.Opts

字段
range: number?ward sight radius, default 1600 (observer)
cell: number?grid cell, default 64
lifetime: number?seconds a sample stays, default 360 (observer lifetime)
negative_step: number?min distance between two stored samples on the same side, default 96
max_samples: number?cap per side, default 600
Opts.level(x: number, y: number): number|nil?#

terrain level at a point, usually lib/ground.level_reader()

参数
x number
y number
返回
number|nil?

ward_finder.Ward

字段
x: numberthe spot
y: numberthe spot
radius: numberhow far the true ward may stand from the spot
cells: table[]the 64 u cells that fit the evidence, {x, y, s}
score: numberthe best cell's score, 0 = fits every sample
explains: integerseen samples within this ward's range

ward_finder.Result

字段
wards: ward_finder.Ward[]best first
positives: integerstored seen samples
negatives: integerstored not-seen samples
介绍

lib/ward_finder

Where an enemy ward we cannot see stands, from the places where the enemy had vision of us and the places where it did not. Pure: positions in, a list of probable ward spots out. The only engine call is the optional level reader the caller passes (lib/ground.level_reader()).

local WF = require("lib/ward_finder")
local st = WF.new({ level = require("lib/ground").level_reader() })

-- every scan of our unit, e.g. 4 times a second
WF.add(st, Entity.GetAbsOrigin(me), seen, game_time, explained)

local r = WF.solve(st, game_time)     -- cached until the next stored sample
for _, w in ipairs(r.wards) do
    -- w.x, w.y: the spot; w.radius: how far the ward may stand from it;
    -- w.cells: the 64 u cells that fit the evidence ({x, y, s}); w.explains: seen samples it covers
end
WF.clear(st)

seen: true when an enemy we cannot see had vision of us, false when nobody had. explained: true on a seen sample that a visible enemy (a creep, a hero, a tower in range) accounts for; it is dropped and breaks the edge chain, so creep sight never moves the answer. A nil seen (the caller does not know) is ignored.

WF.reading(Seen.exact(unit)) gives the seen argument from lib/seen: the team mask and slark's passive count; every other exact source (track, haze, true sight, invisibility) depends on a hero or the unit itself and gives nil. The client never fills the mask (#1075), so for a plain hero the reading is nil and nothing is stored: no wards, no spots, and Seen.state(u, { wards = spots }) stays clear or maybe. Test: tools\utea_harness\test_ward_finder_seen_1083.lua.

the model

An observer ward sees 1600 u day and night (M.RANGE, npc_dota_observer_wards.txt) and lives 360 s (M.LIFETIME; older samples are dropped by solve). Each 64 u cell is scored against every sample in log-likelihood units (M.TUNE):

  • a seen sample further than the range from the cell costs miss_pos (4.0), softened over the last 5% past the range. A cell on a lower level than the sample (level(x, y)) cannot have seen it either.
  • a not-seen sample inside the range costs miss_neg (1.5), less near the border (edge, 10% of the range): trees, cliffs and the unit's hull hide units a ward has in range, nothing makes a ward see past it.

The best cell's plateau (every cell within 1.0 of the best) gives the spot (weighted centre) and radius (2 x rms + half a cell). Seen samples the first ward does not cover start a second one, up to 4; each ward is then re-solved on the samples the others leave, twice.

Samples are thinned: a sample within negative_step (96 u) of the last one on the same side is held back, and stored only when the side flips, so both sides of every edge of the ward's sight are kept. At most max_samples (600) per side.

A coarse pass at 192 u picks the region and the fine pass scores 64 u cells around it: 1 ms with 25 samples a side, 5 ms with 180 (desktop lua; JR is about 3x slower, jr-cpu-3x-slower-lua-1016). Call solve on a new edge, not every frame.

test

tools\lua\lua.exe scripts\lib\ward_finder\selftest.lua [-v]

Generated scenes over the real map heights (lib/ground data): 1 to 3 observers, a hero walking 9 random legs past them, sampled every 0.25 s. The noisy set adds tree clumps that hide him inside a ward's range, a visible creep walking with him (explained), a creep on high ground in the fog (not explained) and a phantom seen of 1 s. The old finder (centre of the intersection of 1600 discs around each seen-episode start) runs beside it. WF_SCENES=N sets the scene count (150).

set, after 3 seen episodes within 200 of a ward median miss
clean, new 0.70 79
clean, old 0.09 1241
noisy, new 0.46 222
noisy, old 0.01 1665

At the end of the walk 71% (clean) and 54% (noisy) of all wards have a guess within 200.

源码

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

init.lua338 行
-- lib/ward_finder: where an unseen enemy ward stands, from where we were seen and where we were not.
-- pure: positions in, a solution out. no engine calls except the optional level reader the caller
-- passes. README.md has the model and the api.

local M = {}

local floor, sqrt, max, min = math.floor, math.sqrt, math.max, math.min

M.RANGE = 1600   -- observer ward day and night vision (npc_dota_observer_wards.txt)
M.CELL = 64
M.LIFETIME = 360 -- observer ward lifetime: evidence older than this cannot be about the same ward

local function xy(p)
    if not p then return nil end
    if p.x then return p.x, p.y end
    if p.GetX then return p:GetX(), p:GetY() end
    return p[1], p[2]
end

---@class ward_finder.Opts
---@field range number? ward sight radius, default 1600 (observer)
---@field cell number? grid cell, default 64
---@field lifetime number? seconds a sample stays, default 360 (observer lifetime)
---@field level fun(x: number, y: number): number|nil? terrain level at a point, usually lib/ground.level_reader()
---@field negative_step number? min distance between two stored samples on the same side, default 96
---@field max_samples number? cap per side, default 600

---@class ward_finder.Ward
---@field x number the spot
---@field y number the spot
---@field radius number how far the true ward may stand from the spot
---@field cells table[] the 64 u cells that fit the evidence, {x, y, s}
---@field score number the best cell's score, 0 = fits every sample
---@field explains integer seen samples within this ward's range

---@class ward_finder.Result
---@field wards ward_finder.Ward[] best first
---@field positives integer stored seen samples
---@field negatives integer stored not-seen samples

--- the `seen` argument of add from lib/seen's exact answer: a mask or slark's passive is about
--- vision in general, so it counts; track, haze, true sight and invisibility depend on a hero, a
--- sentry or the unit itself, not on a ward's range, so they give nil (no sample).
---@param exact boolean|nil Seen.exact(u) first return
---@param why string|nil Seen.exact(u) second return
---@return boolean|nil seen
function M.reading(exact, why)
    if exact == nil then return nil end
    if why == "mask" or why == "slark" then return exact end
    return nil
end

--- a fresh finder state
---@param opts ward_finder.Opts?
---@return table state
function M.new(opts)
    opts = opts or {}
    return {
        range = opts.range or M.RANGE,
        cell = opts.cell or M.CELL,
        lifetime = opts.lifetime or M.LIFETIME,
        level = opts.level,
        negative_step = opts.negative_step or 96,
        max_samples = opts.max_samples or 600,
        pos = {},      -- positives: { x, y, t }
        neg = {},      -- negatives: { x, y, t }
        last_seen = nil,
        dirty = true,
    }
end

local function trim(list, now, life, cap)
    local keep = {}
    for _, s in ipairs(list) do
        if not now or now - s.t <= life then keep[#keep + 1] = s end
    end
    while #keep > cap do table.remove(keep, 1) end
    return keep
end

--- one observation of our unit at `p`
--- a sample next to the last stored one on the same side is held back and stored only when
--- the side flips, so both sides of every edge of the ward's sight are kept.
---@param s table state from new
---@param p Vector|table position: Vector, {x=, y=} or {x, y}
---@param seen boolean|nil true: an enemy we cannot see had vision of us; false: nobody had; nil is ignored
---@param t number game time in seconds
---@param explained boolean? true on a seen sample a visible enemy accounts for (a creep, a hero, a tower in range); dropped
---@return boolean stored
function M.add(s, p, seen, t, explained)
    local x, y = xy(p)
    if not x or seen == nil then return false end
    t = t or 0
    if seen and explained then
        -- a visible enemy accounts for it: no evidence either way, and no edge across it
        s.last_seen, s.held = nil, nil
        return false
    end
    local list = seen and s.pos or s.neg
    local smp = { x = x, y = y, t = t, l = s.level and s.level(x, y) or nil }
    if s.last_seen ~= seen then
        -- an edge: the last sample of the other side (held back by the step) is the one closest
        -- to the border of the ward's sight, so it goes in too
        if s.held then
            local other = seen and s.neg or s.pos
            other[#other + 1] = s.held
        end
        s.held = nil
        list[#list + 1] = smp
        s.last_seen = seen
        s.dirty = true
        return true
    end
    local last = list[#list]
    if last then
        local dx, dy = x - last.x, y - last.y
        if dx * dx + dy * dy < s.negative_step * s.negative_step then
            s.held = smp
            return false
        end
    end
    s.held = nil
    list[#list + 1] = smp
    s.dirty = true
    return true
end

--- forget every sample
---@param s table state from new
function M.clear(s)
    s.pos, s.neg, s.last_seen, s.held, s.dirty, s.result = {}, {}, nil, nil, true, nil
end

-- penalties in log-likelihood units. a "seen" the cell cannot explain costs more than a "not seen"
-- inside its sight: a tree or a cliff can hide us from a ward in range, nothing makes a ward see
-- past its range
M.TUNE = { miss_pos = 4.0, miss_neg = 1.5, edge = 0.10, coarse_margin = 6 }   -- edge: soft border, fraction of the range

local function cell_score(s, cx, cy, lc, pos, neg)
    local MISS_POS, MISS_NEG, EDGE = M.TUNE.miss_pos, M.TUNE.miss_neg, M.TUNE.edge
    local R = s.range
    local inner, outer = R * (1 - EDGE), R * (1 + EDGE * 0.5)
    local inner2, outer2 = inner * inner, outer * outer
    local score, hits = 0, 0
    for i = 1, #pos do
        local p = pos[i]
        local dx, dy = p.x - cx, p.y - cy
        local d2 = dx * dx + dy * dy
        local blocked = lc and (p.l or 0) > lc
        if d2 <= outer2 and not blocked then
            hits = hits + 1
            if d2 > R * R then score = score - MISS_POS * (sqrt(d2) - R) / (outer - R) end
        else
            score = score - MISS_POS
        end
    end
    for i = 1, #neg do
        local p = neg[i]
        local dx, dy = p.x - cx, p.y - cy
        local d2 = dx * dx + dy * dy
        if d2 < R * R then
            local blocked = lc and (p.l or 0) > lc
            if not blocked then
                if d2 <= inner2 then score = score - MISS_NEG
                else score = score - MISS_NEG * (R - sqrt(d2)) / (R - inner) end
            end
        end
    end
    return score, hits
end

-- best cells for the positives in `pos` against every negative. returns the cluster: centre,
-- radius, cells (list of {x, y}), the positives it explains
local function solve_one(s, pos, neg)
    local R, C = s.range, s.cell
    local x0, y0, x1, y1 = math.huge, math.huge, -math.huge, -math.huge
    for _, p in ipairs(pos) do
        x0, y0 = min(x0, p.x), min(y0, p.y)
        x1, y1 = max(x1, p.x), max(y1, p.y)
    end
    -- anchor: the positive with the most others within 2R (a phantom seen is alone). the ward
    -- that saw it stands within R of it
    local a, an = pos[1], -1
    for i = 1, #pos, math.max(1, #pos // 40) do
        local p, n = pos[i], 0
        for j = 1, #pos do
            local dx, dy = pos[j].x - p.x, pos[j].y - p.y
            if dx * dx + dy * dy <= 4 * R * R then n = n + 1 end
        end
        if n > an then a, an = p, n end
    end
    x0, y0 = max(x0 - R, a.x - R), max(y0 - R, a.y - R)
    x1, y1 = min(x1 + R, a.x + R), min(y1 + R, a.y + R)
    -- only samples within 2R of the anchor can tell two candidate cells apart
    local near_p, near_n, lim2 = {}, {}, (2 * R + 2 * C) ^ 2
    for _, p in ipairs(pos) do
        local dx, dy = p.x - a.x, p.y - a.y
        if dx * dx + dy * dy <= lim2 then near_p[#near_p + 1] = p end
    end
    for _, p in ipairs(neg) do
        local dx, dy = p.x - a.x, p.y - a.y
        if dx * dx + dy * dy <= lim2 then near_n[#near_n + 1] = p end
    end
    local lv = s.level
    local function score_at(cx, cy)
        local lc = lv and lv(cx, cy) or nil
        return (cell_score(s, cx, cy, lc, near_p, near_n))
    end
    -- coarse pass at 3 cells, then the fine pass around every coarse cell close to the best
    local K = 3
    local CC = C * K
    local coarse, cbest = {}, -math.huge
    for gx = floor(x0 / CC), floor(x1 / CC) do
        for gy = floor(y0 / CC), floor(y1 / CC) do
            local cx, cy = (gx + 0.5) * CC, (gy + 0.5) * CC
            local dx, dy = cx - a.x, cy - a.y
            if dx * dx + dy * dy <= (R + CC) ^ 2 then
                local sc = score_at(cx, cy)
                coarse[#coarse + 1] = { gx = gx, gy = gy, s = sc }
                if sc > cbest then cbest = sc end
            end
        end
    end
    local seen_cell, cells, best = {}, {}, -math.huge
    for _, c in ipairs(coarse) do
        if c.s >= cbest - M.TUNE.coarse_margin then
            -- the coarse cell and its ring, at fine resolution
            for fx = (c.gx - 1) * K, (c.gx + 2) * K - 1 do
                for fy = (c.gy - 1) * K, (c.gy + 2) * K - 1 do
                    local key = fx * 65536 + fy
                    if not seen_cell[key] then
                        seen_cell[key] = true
                        local cx, cy = (fx + 0.5) * C, (fy + 0.5) * C
                        local dx, dy = cx - a.x, cy - a.y
                        if dx * dx + dy * dy <= R * R then
                            local sc = score_at(cx, cy)
                            cells[#cells + 1] = { x = cx, y = cy, s = sc }
                            if sc > best then best = sc end
                        end
                    end
                end
            end
        end
    end
    -- the plateau: every cell within one unit of the best
    local keep, sx, sy, sw = {}, 0, 0, 0
    for _, c in ipairs(cells) do
        if c.s >= best - 1.0 then
            local w = 1 / (1 + (best - c.s) * 4)
            keep[#keep + 1] = c
            sx, sy, sw = sx + c.x * w, sy + c.y * w, sw + w
        end
    end
    if sw == 0 then return nil end
    local mx, my = sx / sw, sy / sw
    local r2 = 0
    for _, c in ipairs(keep) do
        local dx, dy = c.x - mx, c.y - my
        r2 = r2 + dx * dx + dy * dy
    end
    local spread = sqrt(r2 / #keep) * 2 + C * 0.5
    -- which positives this ward explains
    local explained, rest = {}, {}
    local outer2 = (R * (1 + M.TUNE.edge * 0.5)) ^ 2
    for _, p in ipairs(pos) do
        local dx, dy = p.x - mx, p.y - my
        if dx * dx + dy * dy <= outer2 then explained[#explained + 1] = p else rest[#rest + 1] = p end
    end
    return { x = mx, y = my, radius = spread, cells = keep, score = best,
             explains = #explained }, rest
end

--- the wards the evidence points at, best first; cached until the next stored sample
--- one ward per group of seen samples it explains; a seen sample no ward explains starts
--- another. samples older than the lifetime are dropped here.
---@param s table state from new
---@param now number game time in seconds
---@param max_wards integer? default 4
---@return ward_finder.Result
function M.solve(s, now, max_wards)
    if not s.dirty and s.result then return s.result end
    s.pos = trim(s.pos, now, s.lifetime, s.max_samples)
    s.neg = trim(s.neg, now, s.lifetime, s.max_samples)
    local wards, left = {}, s.pos
    for _ = 1, (max_wards or 4) do
        if #left == 0 then break end
        local w, rest = solve_one(s, left, s.neg)
        if not w then break end
        wards[#wards + 1] = w
        if #rest == #left then break end
        left = rest
    end
    -- greedy picks a compromise spot between two wards first; re-solve each ward on the seen
    -- samples the others do not explain, twice
    if #wards > 1 then
        local o2 = (s.range * (1 + M.TUNE.edge * 0.5)) ^ 2
        for _ = 1, 2 do
            for i = 1, #wards do
                local own = {}
                for _, p in ipairs(s.pos) do
                    local other = false
                    for j = 1, #wards do
                        if j ~= i then
                            local dx, dy = p.x - wards[j].x, p.y - wards[j].y
                            if dx * dx + dy * dy <= o2 then other = true break end
                        end
                    end
                    if not other then own[#own + 1] = p end
                end
                if #own > 0 then
                    local w = solve_one(s, own, s.neg)
                    if w then wards[i] = w end
                end
            end
        end
        -- drop a ward that ended up explaining nothing the others do not
        local keep = {}
        for i, w in ipairs(wards) do
            local dup = false
            for j = 1, i - 1 do
                local dx, dy = w.x - wards[j].x, w.y - wards[j].y
                if dx * dx + dy * dy < (s.cell * 3) ^ 2 then dup = true break end
            end
            if not dup then keep[#keep + 1] = w end
        end
        wards = keep
    end
    table.sort(wards, function(p, q)
        if p.explains ~= q.explains then return p.explains > q.explains end
        return p.score > q.score
    end)
    s.result = { wards = wards, positives = #s.pos, negatives = #s.neg }
    s.dirty = false
    return s.result
end

return M
pack.json2 行
{ "kind": "lib", "version": "1.0.1" }
更新日志
1.0.12026-11-29
  • Added a library that finds enemy wards we cannot see: probable spots with a radius from where the enemy saw our unit and where it did not
  • Added the observer-only model: it lives 360 s and sees 1600, sight from creeps, heroes and towers is not counted
使用它的脚本

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

该作者的其他作品

该作者的其他作品

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