evolve
Store/Shared libraries

fog_creeps

Every enemy lane creep, in sight and in the fog, and the waves they make.

hunnyc0 scripts use it
SupportSource is readable
Use it in a script
pack.json
"requires": { "fog_creeps": "^1.0" }
your lua
local fog_creeps = require("lib/fog_creeps")

Players install nothing: the script brings the library, any version inside the range. Edit scripts/lib/fog_creeps/ in a dev session to try a change before you publish.

API
fog_creeps.new()#

a fresh tracker

fog_creeps.position(st, c, now)#
fog_creeps.update(st, now, view)#

one look at the game.

view = { team = our team, creeps = { {id, x, y, hp, ms, sub} in sight }, allies = { {x, y, r} }, gone = { id known dead or deleted } }

fog_creeps.creeps(st)#

tracked creeps: { [id] = c } (c.lane, c.vis, c.key, c.hp, c.sub; M.position for where)

fog_creeps.waves(st, now)#
fog_creeps.display(st, now)#

the marks to draw at time now: { lane, x, y, id, key, speed, count, seen, guess, dx, dy }.

a mark keeps its id while its wave lives and moves at most CFG.disp_k creep speeds; a mark whose wave merged into another glides onto it and goes

fog_creeps.read_client(st)#

read the client into a view -> view, now (nil when not in a game)

fog_creeps.live()#

the shared tracker fed from the client (one per session, read at most every 0.1 s game time)

Overview

lib/fog_creeps

Every enemy lane creep, in sight and in the fog, and the waves they make. A creep is tracked by its handle from the first time we see it until it dies or the client stops listing it. In the fog it walks its lane at its own move speed and stops at the edge of our vision (CFG.eye_k of the vision range of every unit of our team, minus CFG.margin): we do not see it, so it is not inside our sight. A creep last seen a little off the lane walks back to the lane first. One last seen more than CFG.off_hold off it (pulled, chasing) holds there up to CFG.hold_t seconds or until we see the spot empty, then walks back; held creeps form a wave of their own. A wave nobody has seen walks from its barracks on the 30 s spawn clock until its first creep shows up and takes its key.

local FC = require("lib/fog_creeps")

local st, now = FC.live()          -- the shared tracker, fed from the client at most every 0.1 s
for _, w in ipairs(FC.display(st, now)) do
    -- w.lane, w.x, w.y, w.id (stable per mark), w.count (nil for a guess), w.seen, w.guess, w.dx, w.dy
end
for id, c in pairs(FC.creeps(st)) do
    local x, y = FC.position(st, c, now)   -- c.vis, c.lane, c.key, c.hp
end
FC.waves(st, now)                  -- the raw clusters: creeps closer than CFG.gap_s along the lane

display marks move at most CFG.disp_k creep speeds (1.9 x 325 u/s), so a mark never jumps when a wave splits, merges, or a fog estimate is corrected by a creep coming into view. A mark whose wave merged into another glides onto it if that wave is ahead, else it goes.

A mark stands on the rear creep of its wave that we see (all in the fog: the middle of the wave), so creeps dying at the front do not move it. Along its lane it moves only toward our base, and in a fight it steps up only past CFG.dead_f. It moves back only when the creeps in sight walk back faster than CFG.back_v (median) for CFG.back_hold seconds, and then no further than they walked. A mark left more than CFG.corr_d ahead of its wave's front for CFG.corr_t seconds (its creeps died, or a fog estimate was corrected) starts again at the wave under a new id with reborn = true instead of walking back; so does an estimate more than CFG.corr_far behind it. m.why says why a mark stepped back on this call. tools/fog_waves/jitter.py counts the back-travel on demos (#1349).

The core takes a plain view table (FC.new(), FC.update(st, now, view) with view = { team, creeps = {{id, x, y, ms}}, allies = {{x, y, r}}, gone = {id} }), so tools/fog_waves/replay.lua runs the same code over demos offline. FC.read_client(st) builds that view from entities.npcs: a dormant creep stays tracked, a creep the client no longer lists alive is dropped.

Bench: python tools/fog_waves/fog_eval.py over 14 demos (28 team views), error against the demo's true positions by seconds in the fog; the commands and the numbers are in notes/fog_waves_1169.md. Vision there is the plain radius: trees and high ground are not modelled, so the client sees less than the bench assumes. CFG.eye_k = 0.8 trusts 80% of each radius for that reason; the notes have the bench at 80% sight.

Test: tools\lua\lua.exe scripts\lib\fog_creeps\selftest.lua.

Source

The code of 1.1.0 as the client gets it. The bundle's bytecode is built from these files.

init.lua619 lines
-- lib/fog_creeps: every enemy lane creep, in sight and in the fog, and the waves they make (#1169).
--
-- a creep is tracked by its handle from the first time we see it until it dies or the client drops
-- it. in the fog it walks its lane at its own speed and stops at the edge of our vision: we do not
-- see it, so it is not inside the vision of any of our units. a wave we have not seen yet walks from
-- its barracks on the spawn clock (every 30 s) until its first creep shows up and takes its id.
-- waves are clusters of creeps on a lane; the display marks follow them at most at twice creep
-- speed, so a mark never jumps.
--
-- the core takes a plain view table, so tools/fog_waves/replay.lua runs it over demos offline;
-- FC.live() feeds one shared tracker from the client for every script that asks.
local M = {}

-- ------------------------------------------------------------------ map
-- lanes as polylines from the radiant base (s = 0) to the dire base; the uTEA LANE_WAYPOINTS
local LANES = {
    top = { -5708,-4883, -6664,-4205, -6720,-3206, -6663,-2016, -6612,-754, -6587,646, -6545,1926,
        -6541,3741, -6382,5300, -5260,6203, -3388,6191, -1533,6201, -118,6252, 1732,6020, 3563,5962,
        4515,5934, 5076,4829 },
    mid = { -5560,-4861, -4611,-3970, -3958,-3410, -3305,-2895, -2485,-2091, -1567,-1311, -493,-336,
        576,581, 1530,1346, 2487,2212, 3334,2971, 4233,3806, 5014,4643 },
    bot = { -5559,-5062, -4838,-6029, -3874,-6034, -1856,-6200, -400,-6200, 1212,-6306, 2742,-6324,
        4984,-6374, 6116,-5419, 6350,-3647, 6419,-2229, 6418,-885, 6518,417, 6441,1714, 6394,3075,
        6345,4086, 5194,4690 },
}
-- where a team's creeps appear (demo first sightings, the uTEA SPAWN_POS)
local SPAWN = {
    [2] = { top = { -6650, -4014 }, mid = { -4979, -4456 }, bot = { -3655, -6121 } },
    [3] = { top = { 3202, 5792 }, mid = { 4128, 3633 }, bot = { 6329, 3656 } },
}
local LANE_NAMES = { "top", "mid", "bot" }

local CFG = {
    wave_every = 30,        -- spawn clock, dota time
    speed = 325,            -- lane creep speed when the entity does not say
    margin = 100,           -- a fog estimate stops this far inside our vision edge
    eye_k = 0.8,            -- share of a unit's vision radius we trust as seen (trees, high ground)
    step = 25,              -- vision edge search step along the lane
    join_r = 700,           -- a new creep joins the wave of a tracked creep this close
    take_s = 2500,          -- ... else takes the id of the guessed wave this far along the lane
    gap_s = 800,            -- creeps further apart along the lane are two waves
    stall_drop = 20,        -- a fog creep stuck at our vision edge this long is gone
    guess_stall_drop = 15,  -- same for a wave we never saw
    fog_ttl = 120,          -- a creep unseen this long is gone
    disp_k = 1.9,           -- display marks move at most this many creep speeds
    lane_switch = 900,      -- a tracked creep this far off its lane is assigned again
    off_hold = 600,         -- a creep last seen this far off its lane (pulled, chasing) holds there
    hold_t = 40,            -- ... this long or until we see the spot empty, then walks back (0: at once)
    coast = 1,              -- ... after coasting on its last velocity this long
    still_v = 60,           -- a creep last seen slower than this was fighting:
    still_wait = 0,         -- ... it starts walking this much later
    split_off = 700,        -- creeps this far off the lane (held in a pull) are a wave of their own
}
M.CFG = CFG
M.LANES_XY = LANES

-- per lane: points, cumulative length
local G = {}
for name, f in pairs(LANES) do
    local xs, ys, cum = {}, {}, { 0 }
    for i = 1, #f, 2 do xs[#xs + 1] = f[i]; ys[#ys + 1] = f[i + 1] end
    for i = 2, #xs do cum[i] = cum[i - 1] + math.sqrt((xs[i] - xs[i - 1]) ^ 2 + (ys[i] - ys[i - 1]) ^ 2) end
    G[name] = { x = xs, y = ys, cum = cum, len = cum[#cum], n = #xs }
end

local sqrt, floor, huge = math.sqrt, math.floor, math.huge

--- the point at arc length s of a lane
local function point(lane, s)
    local g = G[lane]
    if s <= 0 then return g.x[1], g.y[1] end
    if s >= g.len then return g.x[g.n], g.y[g.n] end
    local cum = g.cum
    local lo, hi = 1, g.n
    while hi - lo > 1 do
        local mid = floor((lo + hi) / 2)
        if cum[mid] <= s then lo = mid else hi = mid end
    end
    local k = (s - cum[lo]) / (cum[hi] - cum[lo])
    return g.x[lo] + (g.x[hi] - g.x[lo]) * k, g.y[lo] + (g.y[hi] - g.y[lo]) * k
end
M.point = point

--- arc length of the nearest lane point, the distance to it, and the offset vector
local function project(lane, x, y)
    local g = G[lane]
    local best, bs, bx, by = huge, 0, 0, 0
    for i = 1, g.n - 1 do
        local ax, ay = g.x[i], g.y[i]
        local dx, dy = g.x[i + 1] - ax, g.y[i + 1] - ay
        local l2 = dx * dx + dy * dy
        local k = l2 > 0 and ((x - ax) * dx + (y - ay) * dy) / l2 or 0
        if k < 0 then k = 0 elseif k > 1 then k = 1 end
        local px, py = ax + dx * k, ay + dy * k
        local d2 = (x - px) ^ 2 + (y - py) ^ 2
        if d2 < best then best, bs, bx, by = d2, g.cum[i] + k * sqrt(l2), px, py end
    end
    return bs, sqrt(best), x - bx, y - by
end
M.project = project

local function nearest_lane(x, y)
    local bl, bd, bs, ox, oy = nil, huge, 0, 0, 0
    for _, name in ipairs(LANE_NAMES) do
        local s, d, dx, dy = project(name, x, y)
        if d < bd then bl, bd, bs, ox, oy = name, d, s, dx, dy end
    end
    return bl, bs, bd, ox, oy
end
M.nearest_lane = nearest_lane

--- unit heading of the lane at s for a walk in direction dir (+1 toward dire, -1 toward radiant)
local function heading(lane, s, dir)
    local ax, ay = point(lane, s - 60)
    local bx, by = point(lane, s + 60)
    local dx, dy = (bx - ax) * dir, (by - ay) * dir
    local l = sqrt(dx * dx + dy * dy)
    if l < 1e-6 then return nil end
    return dx / l, dy / l
end
M.heading = heading

-- ------------------------------------------------------------------ state
--- a fresh tracker
function M.new()
    return { c = {}, guess = {}, spawned = {}, disp = {}, eyes = {}, key_n = 0, team = nil, t = nil,
             dir = -1 }
end

local function new_key(st)
    st.key_n = st.key_n + 1
    return st.key_n
end

-- any of our units sees (x, y)
local function seen(st, x, y)
    local eyes, m, k = st.eyes, CFG.margin, CFG.eye_k
    for i = 1, #eyes do
        local e = eyes[i]
        local r = e.r * k - m
        if r > 0 then
            local dx, dy = x - e.x, y - e.y
            if dx * dx + dy * dy <= r * r then return true end
        end
    end
    return false
end

-- move an estimate from s_from by ds along dir, stopping at our vision edge.
-- -> s, stalled (the walk was cut)
local function walk(st, lane, s_from, ds, dir)
    local len = G[lane].len
    local s1 = s_from + ds * dir
    if s1 < 0 then s1 = 0 elseif s1 > len then s1 = len end
    local x, y = point(lane, s1)
    if not seen(st, x, y) then return s1, false end
    -- back off toward where it was
    local step = CFG.step
    local s = s1
    local n = floor(math.abs(s1 - s_from) / step)
    for _ = 1, n do
        s = s - step * dir
        x, y = point(lane, s)
        if not seen(st, x, y) then return s, true end
    end
    x, y = point(lane, s_from)
    if not seen(st, x, y) then return s_from, true end
    -- the old estimate is in sight too: the creep is somewhere else. the nearest lane point out of sight
    for k = 1, 100 do
        local d = k * step
        for _, sg in ipairs({ dir, -dir }) do
            local sk = s_from + d * sg
            if sk >= 0 and sk <= len then
                x, y = point(lane, sk)
                if not seen(st, x, y) then return sk, true end
            end
        end
    end
    return s1, true
end

--- where a tracked creep is at time now -> x, y
local function held(c, now)
    local k = math.min(math.min(now, c.hold) - c.tr, CFG.coast)
    if k < 0 then k = 0 end
    return c.x + c.vx * k, c.y + c.vy * k
end

function M.position(st, c, now)
    if c.vis then return c.x, c.y end
    if c.hold then
        local x, y = held(c, now)
        return x, y, c.s
    end
    local s = c.s
    if not c.stalled then
        local from = c.ts > c.tb and c.ts or c.tb
        if now > from then s = s + c.ms * (now - from) * st.dir end
        local len = G[c.lane].len
        if s < 0 then s = 0 elseif s > len then s = len end
    end
    local x, y = point(c.lane, s)
    if now < c.tb then
        local w = 1 - (now - c.tr) / (c.tb - c.tr)
        x, y = x + c.ox * w, y + c.oy * w
    end
    return x, y, s
end

local function guess_pos(st, g, now)
    local s = g.s
    if not g.stalled then
        s = s + CFG.speed * (now - g.ts) * st.dir
        local len = G[g.lane].len
        if s < 0 then s = 0 elseif s > len then s = len end
    end
    local x, y = point(g.lane, s)
    return x, y, s
end

-- the key a creep first seen at (lane, s, x, y) belongs to
local function key_for(st, lane, s, x, y, now)
    local jr2, best, bk = CFG.join_r ^ 2, huge, nil
    for _, c in pairs(st.c) do
        if c.lane == lane and c.key then
            local cx, cy = M.position(st, c, now)
            local d2 = (cx - x) ^ 2 + (cy - y) ^ 2
            if d2 < jr2 and d2 < best then best, bk = d2, c.key end
        end
    end
    if bk then return bk end
    local bi, bd = nil, CFG.take_s
    for i, g in ipairs(st.guess) do
        if g.lane == lane then
            local _, _, gs = guess_pos(st, g, now)
            local d = math.abs(gs - s)
            if d < bd then bi, bd = i, d end
        end
    end
    if bi then
        local g = table.remove(st.guess, bi)
        return g.key
    end
    return new_key(st)
end

--- one look at the game.
--- view = { team = our team, creeps = { {id, x, y, hp, ms, sub} in sight }, allies = { {x, y, r} },
---          gone = { id known dead or deleted } }
function M.update(st, now, view)
    local team = view.team or 2
    if st.team ~= team then
        st.c, st.guess, st.spawned, st.disp = {}, {}, {}, {}
        st.team, st.dir = team, team == 2 and -1 or 1
    end
    if st.t and now < st.t - 1 then         -- clock went back: a new game
        st.c, st.guess, st.spawned, st.disp = {}, {}, {}, {}
    end
    st.t = now
    st.eyes = view.allies or {}
    local dir = st.dir

    for _, id in ipairs(view.gone or {}) do st.c[id] = nil end

    local inview = {}
    for _, v in ipairs(view.creeps or {}) do
        local id = v.id
        inview[id] = true
        local c = st.c[id]
        if not c then
            local lane, s, _, ox, oy = nearest_lane(v.x, v.y)
            c = { id = id, lane = lane }
            c.key = key_for(st, lane, s, v.x, v.y, now)
            st.c[id] = c
        end
        local s, d, ox, oy = project(c.lane, v.x, v.y)
        if d > CFG.lane_switch then
            local lane, s2, d2, ox2, oy2 = nearest_lane(v.x, v.y)
            if lane ~= c.lane and d2 < d then c.lane, s, ox, oy = lane, s2, ox2, oy2 end
        end
        c.ms = (v.ms and v.ms > 0) and v.ms or CFG.speed
        -- velocity over >= 0.25 s of sight, capped at its speed
        if not c.vis or not c.pt then
            c.px, c.py, c.pt, c.vx, c.vy, c.vok = v.x, v.y, now, 0, 0, false
        elseif now - c.pt >= 0.25 then
            local dt = now - c.pt
            local vx, vy = (v.x - c.px) / dt, (v.y - c.py) / dt
            local sp = sqrt(vx * vx + vy * vy)
            if sp > c.ms then vx, vy = vx * c.ms / sp, vy * c.ms / sp end
            c.vx, c.vy, c.px, c.py, c.pt = vx, vy, v.x, v.y, now
            c.vok = true
        end
        c.vis, c.x, c.y = true, v.x, v.y
        c.s, c.ts, c.t0, c.tr = s, now, now, now
        c.ox, c.oy = ox, oy
        -- seen off the lane: a pull or a chase holds there a while, then it walks back to the lane
        local off = sqrt(ox * ox + oy * oy)
        if CFG.hold_t > 0 and off > CFG.off_hold then
            c.hold = now + CFG.hold_t
            c.tb = c.hold + off / c.ms
        else
            c.hold = nil
            local wait = 0
            if c.vok and c.vx * c.vx + c.vy * c.vy < CFG.still_v * CFG.still_v then wait = CFG.still_wait end
            c.tb = now + math.max(off / c.ms, wait)
        end
        c.hp, c.sub = v.hp, v.sub
        c.stalled, c.stall_t = false, nil
    end

    for id, c in pairs(st.c) do
        if not inview[id] then
            if c.vis then c.vis = false end
            if c.hold then
                local hx, hy = held(c, now)
                local empty = seen(st, hx, hy)
                if empty or now >= c.hold then
                    -- walk back to the lane from where it held
                    local t1 = empty and now or c.hold
                    local s, d, ox, oy = project(c.lane, hx, hy)
                    c.s, c.ox, c.oy, c.tr, c.ts = s, ox, oy, t1, t1
                    c.tb = t1 + d / c.ms
                    c.hold = nil
                end
            end
            local from = c.ts > c.tb and c.ts or c.tb
            local s, stalled = c.s, false
            if not c.hold and now > from then s, stalled = walk(st, c.lane, c.s, c.ms * (now - from), dir) end
            c.s, c.ts = s, now
            c.stalled = stalled
            if stalled then c.stall_t = c.stall_t or now else c.stall_t = nil end
            if now - c.t0 > CFG.fog_ttl or (c.stall_t and now - c.stall_t > CFG.stall_drop) then
                st.c[id] = nil
            end
        end
    end

    -- waves on the spawn clock we have not seen: from the enemy barracks
    local enemy = 5 - team
    local every = CFG.wave_every
    local k1 = floor(now / every)
    local k0 = math.max(0, floor((now - 75) / every))
    for k = k0, k1 do
        if not st.spawned[k] then
            st.spawned[k] = true
            for _, lane in ipairs(LANE_NAMES) do
                local sp = SPAWN[enemy][lane]
                local s = project(lane, sp[1], sp[2])
                st.guess[#st.guess + 1] = { lane = lane, s = s, ts = k * every, key = new_key(st), wave = k }
            end
        end
    end
    for k in pairs(st.spawned) do if k < k0 - 2 then st.spawned[k] = nil end end
    local i = 1
    while i <= #st.guess do
        local g = st.guess[i]
        local drop = false
        if now >= g.ts then
            local s, stalled = walk(st, g.lane, g.s, CFG.speed * (now - g.ts), dir)
            g.s, g.ts, g.stalled = s, now, stalled
            if stalled then g.stall_t = g.stall_t or now else g.stall_t = nil end
            local len = G[g.lane].len
            drop = (g.stall_t and now - g.stall_t > CFG.guess_stall_drop) or s <= 0 or s >= len
        end
        if drop then table.remove(st.guess, i) else i = i + 1 end
    end
end

--- tracked creeps: { [id] = c } (c.lane, c.vis, c.key, c.hp, c.sub; M.position for where)
function M.creeps(st) return st.c end

-- per lane, the creeps and guesses sorted along the lane, cut into waves
local _ent = {}
function M.waves(st, now)
    local out = {}
    for _, lane in ipairs(LANE_NAMES) do
        local es = {}
        for _, c in pairs(st.c) do
            if c.lane == lane then
                local x, y = M.position(st, c, now)
                local s, d = project(lane, x, y)
                es[#es + 1] = { s = s, x = x, y = y, key = c.key, vis = c.vis, real = true, c = c,
                                off = d > CFG.split_off }
            end
        end
        for _, g in ipairs(st.guess) do
            if g.lane == lane and now >= g.ts then
                local x, y, s = guess_pos(st, g, now)
                es[#es + 1] = { s = s, x = x, y = y, key = g.key, real = false }
            end
        end
        -- on-lane creeps first, then off-lane ones: each side is cut by the gap along the lane
        table.sort(es, function(a, b)
            if (a.off or false) ~= (b.off or false) then return not a.off end
            return a.s < b.s
        end)
        local clusters = {}
        local i = 1
        while i <= #es do
            local j = i
            while j < #es and (es[j + 1].off or false) == (es[i].off or false)
                and es[j + 1].s - es[j].s <= CFG.gap_s do j = j + 1 end
            local n, sx, sy, ss, seen_, keys = 0, 0, 0, 0, false, {}
            local gk, gx, gy, gs
            for k = i, j do
                local e = es[k]
                if e.real then
                    n = n + 1
                    sx, sy, ss = sx + e.x, sy + e.y, ss + e.s
                    if e.vis then seen_ = true end
                    keys[e.key] = (keys[e.key] or 0) + 1
                elseif not gk or e.key < gk then
                    gk, gx, gy, gs = e.key, e.x, e.y, e.s
                end
            end
            local w
            if n > 0 then
                local bk, bn = nil, -1
                for kk, cnt in pairs(keys) do
                    if cnt > bn or (cnt == bn and kk < bk) then bk, bn = kk, cnt end
                end
                w = { lane = lane, x = sx / n, y = sy / n, s = ss / n, key = bk, count = n, seen = seen_,
                      guess = false, i0 = i, i1 = j }
            else
                w = { lane = lane, x = gx, y = gy, s = gs, key = gk, count = nil, seen = false, guess = true }
            end
            w.dx, w.dy = heading(lane, w.s, st.dir)
            clusters[#clusters + 1] = w
            i = j + 1
        end
        -- a wave that split: the bigger part keeps the key, the smaller part's creeps get a new one
        local order = {}
        for k = 1, #clusters do order[k] = clusters[k] end
        table.sort(order, function(a, b)
            if (a.count or 0) ~= (b.count or 0) then return (a.count or 0) > (b.count or 0) end
            return a.s < b.s
        end)
        local owned = {}
        for _, w in ipairs(order) do
            if owned[w.key] and w.count then
                local old, nk = w.key, new_key(st)
                for k = w.i0, w.i1 do
                    local e = es[k]
                    if e.real and e.c.key == old then e.c.key = nk end
                end
                w.key = nk
            end
            owned[w.key] = true
        end
        for k = 1, #clusters do
            local w = clusters[k]
            w.i0, w.i1 = nil, nil
            out[#out + 1] = w
        end
    end
    return out
end

--- the marks to draw at time now: { lane, x, y, id, key, speed, count, seen, guess, dx, dy }.
--- a mark keeps its id while its wave lives and moves at most CFG.disp_k creep speeds; a mark whose
--- wave merged into another glides onto it and goes
function M.display(st, now)
    local targets = M.waves(st, now)
    local disp = st.disp
    local vmax = CFG.disp_k * CFG.speed
    local used, out = {}, {}
    local function move(d, tx, ty)
        local dt = now - (d.t or now)
        d.t = now
        if dt <= 0 then return end
        local dx, dy = tx - d.x, ty - d.y
        local l = sqrt(dx * dx + dy * dy)
        local m = vmax * dt
        if dt > 0.5 or l <= m then d.x, d.y = tx, ty else d.x, d.y = d.x + dx / l * m, d.y + dy / l * m end
    end
    local pending = {}
    for _, w in ipairs(targets) do
        local d = disp[w.key]
        if d and not used[w.key] then
            used[w.key] = true
            d.w = w
            move(d, w.x, w.y)
        else
            pending[#pending + 1] = w
        end
    end
    -- a wave whose key has no mark takes the nearest orphan mark on its lane, else a new mark
    for _, w in ipairs(pending) do
        local bk, bd = nil, 1500 ^ 2
        for k, d in pairs(disp) do
            if not used[k] and d.lane == w.lane then
                local dd = (d.x - w.x) ^ 2 + (d.y - w.y) ^ 2
                if dd < bd then bk, bd = k, dd end
            end
        end
        local d
        if bk then
            d = disp[bk]
            disp[bk] = nil
        else
            st.disp_n = (st.disp_n or 0) + 1
            d = { id = st.disp_n, lane = w.lane, x = w.x, y = w.y, t = now }
        end
        disp[w.key] = d
        used[w.key] = true
        d.w = w
        move(d, w.x, w.y)
    end
    for k, d in pairs(disp) do
        if used[k] then
            local w = d.w
            out[#out + 1] = { lane = w.lane, x = d.x, y = d.y, id = d.id, key = k, speed = CFG.speed,
                              count = w.count, seen = w.seen, guess = w.guess, dx = w.dx, dy = w.dy }
        else
            -- merged or gone: glide onto the nearest wave of the lane, else drop
            local bw, bd = nil, 1500 ^ 2
            for _, w in ipairs(targets) do
                if w.lane == d.lane then
                    local dd = (d.x - w.x) ^ 2 + (d.y - w.y) ^ 2
                    if dd < bd then bw, bd = w, dd end
                end
            end
            if bw and bd > 60 * 60 then
                move(d, bw.x, bw.y)
                out[#out + 1] = { lane = d.lane, x = d.x, y = d.y, id = d.id, key = k, speed = CFG.speed,
                                  count = bw.count, seen = bw.seen, guess = bw.guess, dx = bw.dx, dy = bw.dy,
                                  merging = true }
            else
                disp[k] = nil
            end
        end
    end
    table.sort(out, function(a, b) return a.id < b.id end)
    return out
end

-- ------------------------------------------------------------------ live client
local function call(o, m, ...)
    local f = o and o[m]
    if type(f) ~= "function" then return nil end
    local ok, v = pcall(f, o, ...)
    if ok then return v end
    return nil
end

local reg = rawget(_G, "__EVOLVE_LIB_REG") or {}
rawset(_G, "__EVOLVE_LIB_REG", reg)

local LIVE_EVERY = 0.1
local _view = { creeps = {}, allies = {}, gone = {} }

--- read the client into a view -> view, now (nil when not in a game)
function M.read_client(st)
    local gr = rawget(_G, "gamerules")
    local cl = rawget(_G, "client")
    local ents = rawget(_G, "entities")
    if not (gr and cl and ents) then return nil end
    local now = call(gr, "game_time")
    local me = cl.local_hero and cl.local_hero()
    if type(now) ~= "number" or not me then return nil end
    local team = call(me, "team")
    if team ~= 2 and team ~= 3 then return nil end
    local ok, all = pcall(ents.npcs, { alive = true })
    if not ok or type(all) ~= "table" then return nil end
    local v = _view
    local cr, al, gone = {}, {}, {}
    local present = {}
    for _, e in ipairs(all) do
        local et = call(e, "team")
        if et == team then
            local r = call(e, "current_vision_range")
            if type(r) == "number" and r > 0 then
                local p = call(e, "position")
                if p then al[#al + 1] = { x = p.x, y = p.y, r = r } end
            end
        elseif (et == 2 or et == 3) and call(e, "is_lane_creep") then
            local h = call(e, "handle")
            if h then
                present[h] = true
                if call(e, "is_dormant") == false then
                    local p = call(e, "position")
                    if p then
                        cr[#cr + 1] = { id = h, x = p.x, y = p.y, hp = call(e, "health"),
                                        ms = call(e, "move_speed"), sub = call(e, "is_ranged") and 2 or 1 }
                    end
                end
            end
        end
    end
    -- a tracked creep the client no longer lists alive died or was deleted
    for id in pairs(st.c) do
        if not present[id] then gone[#gone + 1] = id end
    end
    v.team, v.creeps, v.allies, v.gone = team, cr, al, gone
    return v, now
end

--- the shared tracker fed from the client (one per session, read at most every 0.1 s game time)
function M.live()
    local L = reg.fog_creeps_live
    if not L then
        L = { st = M.new(), next = -huge }
        reg.fog_creeps_live = L
    end
    local gr = rawget(_G, "gamerules")
    local now = gr and call(gr, "game_time")
    if type(now) == "number" and (now >= L.next or now < L.next - 2 * LIVE_EVERY) then
        local view, t = M.read_client(L.st)
        if view then
            M.update(L.st, t, view)
            L.next = t + LIVE_EVERY
        end
    end
    return L.st, now
end

reg.fog_creeps = M
return M
pack.json2 lines
{ "kind": "lib", "version": "1.0.0" }
Changelog

No release notes recorded for this listing.

Used by

No published script needs it yet.

More from this author

More from this author

lotus

by hunnyc

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

pack.json
"requires": { "lotus": "^1.0" }
your lua
local lotus = require("lib/lotus")
0 scripts use itNo published version yet

order_watch

by 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" }
your lua
local order_watch = require("lib/order_watch")
0 scripts use itv1.0.0

order_class

by hunnyc

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

pack.json
"requires": { "order_class": "^1.0" }
your lua
local order_class = require("lib/order_class")
0 scripts use itv1.0.0