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