evolve
Store/Shared libraries

order_class

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

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

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

API
order_class.current(): integer#

class an order issued now carries: the top scope, else the fallback, else 0

Returns
integer
order_class.scoped(): integer#

top scope only (0 with no scope open), ignoring the fallback: "did a caller decide?"

Returns
integer
order_class.depth(): integer#

current scope depth, for a caller that must restore it whatever ran inside

Returns
integer
order_class.fallback(c: string|integer): integer#

set the fallback class (0 clears); returns the previous one to restore

Parameters
c string|integer
Returns
integer
order_class.current_name(): string|nil#

name of the current class, nil outside every scope

Returns
string|nil
order_class.push(c: string|integer, src?: string|nil): integer#

open a scope; returns the depth to hand back to pop

Parameters
c string|integer class name or id
src string|nil who opened it, for diagnostics
Returns
integer
order_class.pop(at?: integer|nil)#

close scopes back to a depth push returned (also drops any scope leaked above it)

Parameters
at integer|nil
order_class.with(c: string|integer, fn: function, ...)#

run fn inside a class scope; the scope closes even if fn throws

Parameters
c string|integer
fn function
order_class.default(c: string|integer, fn: function, ...)#

same, but keeps an outer class when one is already open (a default, not an override)

Parameters
c string|integer
fn function
order_class.note(src?: string|nil): integer#

the class an order issued now carries; counts it. the harness calls it per recorded order.

Parameters
src string|nil caller, kept for untagged orders
Returns
integer
order_class.stats()#

counters: lua-side (harness) and the dll's own when it has them

order_class.reset_stats()#
order_class.install_tables(tables?: table|nil): boolean#

hand the tables to the dll; false when the dll has no humanizer.class_table

Parameters
tables table|nil defaults to M.TABLES
Returns
boolean
order_class.expect(c: string|integer, p?: number|nil): number|nil#

quantile p of class c's delay in seconds as the dll will draw it; nil when the dll has no

Parameters
c string|integer
p number|nil default 0.5
Returns
number|nil

table for c (lattice) or predates humanizer.expect. a window planner leads by this.

order_class.last_delay(c: string|integer): number|nil#

decision-to-send of the last released order of class c, seconds; nil before one

Parameters
c string|integer
Returns
number|nil
order_class.scope_owner(id: integer): integer|nil#

the sticky owner every order issued from now on carries unless a one-shot humanizer.owner

Parameters
id integer
Returns
integer|nil

claims it (#1103 3b); the dispatcher sets one per module, so a module is one fifo family. returns the previous owner to restore, nil on a dll without it

order_class.install_series(s?: table|false|nil): boolean#

hand the series hold to the dll; false turns it off. false when the dll has no humanizer.series

Parameters
s table|false|nil defaults to M.SERIES
Returns
boolean
Overview

lib/order_class

The class an order carries into hk_order (#1103): reaction, chain, window, background, owner. Policy lives here; the dll stamps the class on the order, counts it and, once a table is installed, samples the delay from it.

local OC = require("lib/order_class")
OC.with("reaction", Perform.execute, cmd)   -- every order inside carries reaction
local prev = OC.fallback("background")      -- under every scope, for orders nobody classed
OC.fallback(prev)
OC.stats()   -- lua counters (harness) plus humanizer.class_stats() from the dll

push/pop(at) for a scope that spans a callback, default keeps an outer class. install_tables() hands M.TABLES (placeholder quantiles, ms) to humanizer.class_table; nothing calls it yet. On a dll without humanizer.set_class the lib still keeps its stack and counters and the dll sees every order as untagged.

Test: tools/lua/lua.exe scripts/lib/order_class/selftest.lua.

Source

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

init.lua259 lines
-- lib/order_class: the class an order carries into hk_order (#1103).
-- a scope stack in lua, mirrored into the dll through humanizer.set_class when the dll has it.
-- an order issued inside a scope carries the top class; outside every scope it is untagged.
-- classes and numbers are policy and live here; the dll only samples what it is handed.

do
    local g = rawget(_G, "__EVOLVE_LIB_REG")
    if g and g.order_class then return g.order_class end
end

local M = {}

---@enum OrderClassId
M.CLASS = {
    reaction   = 1,   -- answer to an event (dodge, save, interrupt)
    chain      = 2,   -- next step of a committed sequence
    window     = 3,   -- a timed window: last hit, animation cancel
    background = 4,   -- farm, walk, stack, items, summons
    owner      = 5,   -- the owner pressed it (combo key, intercepted manual order)
}
M.NAME = {}
for k, v in pairs(M.CLASS) do M.NAME[v] = k end
M.N = 5

local stack, srcs = {}, {}
local depth = 0
local fallback = 0     -- a caller's default under every scope (module, intercept); not a scope
local mirrored = -1
local stats = { untagged = 0, by = {}, untagged_src = {} }
for i = 1, M.N do stats.by[i] = 0 end

local function native()
    local h = rawget(_G, "humanizer")
    local f = type(h) == "table" and rawget(h, "set_class")
    return type(f) == "function" and f or nil
end
local set_native = native()

local function mirror()
    local c = depth > 0 and stack[depth] or fallback
    if c ~= mirrored then
        mirrored = c
        if set_native then set_native(c) end
    end
end

local function id_of(c)
    if type(c) == "number" then return (c >= 1 and c <= M.N) and c or nil end
    return M.CLASS[c]
end

--- class an order issued now carries: the top scope, else the fallback, else 0
---@return integer
function M.current() return depth > 0 and stack[depth] or fallback end

--- top scope only (0 with no scope open), ignoring the fallback: "did a caller decide?"
---@return integer
function M.scoped() return depth > 0 and stack[depth] or 0 end

--- current scope depth, for a caller that must restore it whatever ran inside
---@return integer
function M.depth() return depth end

--- set the fallback class (0 clears); returns the previous one to restore
---@param c string|integer
---@return integer
function M.fallback(c)
    local prev = fallback
    fallback = (c == 0 or c == nil) and 0 or (id_of(c) or 0)
    if fallback ~= prev then mirror() end
    return prev
end

--- name of the current class, nil outside every scope
---@return string|nil
function M.current_name() return M.NAME[M.current()] end

--- open a scope; returns the depth to hand back to pop
---@param c string|integer class name or id
---@param src string|nil who opened it, for diagnostics
---@return integer
function M.push(c, src)
    local id = id_of(c)
    if not id then error("order_class: unknown class " .. tostring(c), 2) end
    local at = depth
    depth = depth + 1
    stack[depth], srcs[depth] = id, src
    mirror()
    return at
end

--- close scopes back to a depth push returned (also drops any scope leaked above it)
---@param at integer|nil
function M.pop(at)
    at = at or (depth - 1)
    if at < 0 then at = 0 end
    for i = depth, at + 1, -1 do stack[i], srcs[i] = nil, nil end
    depth = at
    mirror()
end

local function close(at, ok, ...)
    M.pop(at)
    if not ok then error((...), 0) end
    return ...
end

--- run fn inside a class scope; the scope closes even if fn throws
---@param c string|integer
---@param fn function
function M.with(c, fn, ...)
    local at = M.push(c)
    return close(at, pcall(fn, ...))
end

--- same, but keeps an outer class when one is already open (a default, not an override)
---@param c string|integer
---@param fn function
function M.default(c, fn, ...)
    if M.current() ~= 0 then return fn(...) end
    return M.with(c, fn, ...)
end

--- the class an order issued now carries; counts it. the harness calls it per recorded order.
---@param src string|nil caller, kept for untagged orders
---@return integer
function M.note(src)
    local id = M.current()
    if id == 0 then
        stats.untagged = stats.untagged + 1
        if src then stats.untagged_src[src] = (stats.untagged_src[src] or 0) + 1 end
    else
        stats.by[id] = stats.by[id] + 1
    end
    return id
end

--- counters: lua-side (harness) and the dll's own when it has them
function M.stats()
    local out = { untagged = stats.untagged, untagged_src = stats.untagged_src }
    for i = 1, M.N do out[M.NAME[i]] = stats.by[i] end
    local h = rawget(_G, "humanizer")
    local f = type(h) == "table" and rawget(h, "class_stats")
    if type(f) == "function" then
        local ok, t = pcall(f)
        if ok and type(t) == "table" then out.native = t end
    end
    return out
end

function M.reset_stats()
    stats.untagged, stats.untagged_src = 0, {}
    for i = 1, M.N do stats.by[i] = 0 end
end

-- delay tables per class, ms quantiles at even probabilities; scripts/humanizer.lua installs them.
-- hi 0 = no queue at all: the dll sends the order on the next tick, no draw, no hold, no re-clicks.
-- nil = the dll's own sampler (lattice + speed dial) keeps the class.
local ZERO = { q = { 0, 0 }, lo = 0, hi = 0 }
M.TABLES = {
    -- stays on the lattice until the dial is rebuilt (#1103 step 4): the immortal reaction median
    -- is ~1 s (data/humanizer_ref_1103.json slider), a table from it would slow the dodger down
    reaction   = nil,
    -- motor step: chain.phase_end_to_next of data/humanizer_ref_1103.json (n 33464) cut to
    -- [0, 400] ms, P(x <= 400) = 0.806; Q(0.806 u) at u = 0, 0.1 .. 1. the cut drops the
    -- thinking pauses, which the bot does not have
    chain      = { q = { 0, 0, 0, 0, 19, 41, 62, 101, 144, 187, 400 }, lo = 0, hi = 400 },
    window     = ZERO,
    background = ZERO,
    owner      = ZERO,
}

--- hand the tables to the dll; false when the dll has no humanizer.class_table
---@param tables table|nil defaults to M.TABLES
---@return boolean
function M.install_tables(tables)
    local h = rawget(_G, "humanizer")
    local f = type(h) == "table" and rawget(h, "class_table")
    if type(f) ~= "function" then return false end
    tables = tables or M.TABLES
    for name, id in pairs(M.CLASS) do
        local t = tables[name]
        if t then f(id, t.q, t.lo, t.hi) else f(id, nil) end
    end
    -- id 0: an order nobody classed answers an event (APPROACH_HUMANIZER_1103)
    local u = tables.untagged or tables.reaction
    if u then f(0, u.q, u.lo, u.hi) else f(0, nil) end
    return true
end

local function hz_fn(name)
    local h = rawget(_G, "humanizer")
    local f = type(h) == "table" and rawget(h, name)
    return type(f) == "function" and f or nil
end

--- quantile p of class c's delay in seconds as the dll will draw it; nil when the dll has no
--- table for c (lattice) or predates humanizer.expect. a window planner leads by this.
---@param c string|integer
---@param p number|nil default 0.5
---@return number|nil
function M.expect(c, p)
    local id, f = id_of(c), hz_fn("expect")
    if not id or not f then return nil end
    local ok, v = pcall(f, id, p or 0.5)
    return ok and type(v) == "number" and v / 1000 or nil
end

--- decision-to-send of the last released order of class c, seconds; nil before one
---@param c string|integer
---@return number|nil
function M.last_delay(c)
    local id, f = id_of(c), hz_fn("last_delay")
    if not id or not f then return nil end
    local ok, v = pcall(f, id)
    return ok and type(v) == "number" and v / 1000 or nil
end

--- the sticky owner every order issued from now on carries unless a one-shot humanizer.owner
--- claims it (#1103 3b); the dispatcher sets one per module, so a module is one fifo family.
--- returns the previous owner to restore, nil on a dll without it
---@param id integer
---@return integer|nil
function M.scope_owner(id)
    local f = hz_fn("scope_owner")
    if not f then return nil end
    return f(id or 0)
end

-- a family whose order went out keeps the queue until its queued orders leave (#1103 3b): a combo
-- step is not cut by a neighbour module's order. capped at ms from the series' first release (under
-- the dll's 1000 ms stale sweep); the owner's own press is never held.
M.SERIES = { ms = 700, pass = { "owner" } }

--- hand the series hold to the dll; false turns it off. false when the dll has no humanizer.series
---@param s table|false|nil defaults to M.SERIES
---@return boolean
function M.install_series(s)
    local f = hz_fn("series")
    if not f then return false end
    if s == false then f(0, 0); return true end
    s = s or M.SERIES
    local mask = 0
    for _, c in ipairs(s.pass or {}) do
        local id = id_of(c)
        if id then mask = mask | (1 << id) end
    end
    f(s.ms or 0, mask)
    return true
end

-- a reload resets the stack: no scope survives the callback that opened it
mirror()

local reg = rawget(_G, "__EVOLVE_LIB_REG") or {}
rawset(_G, "__EVOLVE_LIB_REG", reg)
reg.order_class = 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

fog_creeps

by hunnyc

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

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