evolve
Store/Shared libraries

fx

Shader effects for a script, one table per call: an expanding wave, an arc or cone, a ground marker, a zone with a gradient or noise, a line with a band at its front, and three screen rect effects (gl

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

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

API
fx.on(): boolean#

true while world shapes go through the shader (the dll has world_fx and it has not thrown).

Returns
boolean
fx.wave(id, pos, radius, o): boolean#

rings running out from pos to radius and fading: n rings (1..4) dur s each, once or

Returns
boolean

again every period s. o = { n, dur, period, width (world), t0, delay, color, fill, rim, glow, gradient, noise, ttl, layer }

fx.arc(id, pos, radius, o): boolean#

a pie or an annular sector: dir degrees or a Vector it points along, spread degrees

Returns
boolean

(90; 360 a full turn), inner world radius (0). o also takes the zone fields and front

fx.mark(id, pos, radius, o): boolean#

a marker on the ground: style "diamond" | "cross" | "ring" | "dot", pulse s per ring

Returns
boolean

running out to 2.3 R (0 off), spin deg/s. o also takes color, fill, rim, glow, ttl, layer

fx.zone(id, pos, radius, o): boolean#

a zone (renderer.world_fx "circle") with every field: gradient = {c1, c2[, c3, c4],

Returns
boolean

axis = "radial" | "around"}, noise = {scale, speed, amount}, front = {..., band, trail}

fx.line(id, from, to, o): boolean#

a line from -> to, w0/w1 widths (a cone when they differ). front = {..., band = world

Returns
boolean

width of a bright band at the head, trail = 0..1 wake behind it}

fx.clear(id)#

drops a world effect and its flat strokes.

fx.glow(x, y, w, h, o): boolean#

light round a screen rect (an icon, a button): o = { color, radius (px), glow (px), glow_a,

Returns
boolean

rim, fill, pulse (s), gradient }

fx.wipe(x, y, w, h, progress, o): boolean#

dims what is still on cooldown, clockwise from the top: progress 0..1 is the part done.

Returns
boolean

o = { color (the hand), color2 (the dim, black), fill (0.55), radius }

fx.spot(x, y, w, h, o): boolean#

dims the screen outside a rect, a lit rim round it: o = { color (rim), color2 (dim, black),

Returns
boolean

fill (0.55), radius, feather (px, 24), glow }

Overview

lib/fx

Shader effects for a script, one table per call: an expanding wave, an arc or cone, a ground marker, a zone with a gradient or noise, a line with a band at its front, and three screen rect effects (glow, cooldown wipe, spotlight). Each call goes through renderer.world_fx or renderer.fx (#561 step 3). On a client without them, or after the shader call threw once (one log.warn, then strokes for the rest of the session), the same shape is drawn as imgui strokes. Every call returns true when the shader took it.

local fx = require("lib/fx")

-- world effects are retained like renderer.world_circle: re-send each frame or give ttl
fx.wave("me.w", pos, 400, { n = 3, dur = 0.8, period = 2, color = color(255, 80, 60, 230) })
fx.arc("me.a", pos, 600, { dir = 30, spread = 70, inner = 150, glow = 30 })   -- dir: degrees or a Vector
fx.mark("me.m", pos, 80, { style = "diamond", pulse = 1.2, spin = 45 })       -- diamond, cross, ring, dot
fx.zone("me.z", pos, 300, { gradient = { color(255, 70, 60), color(255, 220, 60), axis = "around" },
                            noise = { scale = 80, speed = 0.4, amount = 0.6 } })
fx.line("me.l", from, to, { w0 = 200, w1 = 260, front = { from = 0.2, dur = 1, band = 160, trail = 0.2 } })
fx.clear("me.w")

-- screen effects draw now: call them from on_draw
fx.glow(x, y, 64, 64, { color = tone, radius = 6, glow = 12, pulse = 1 })
fx.wipe(x, y, 64, 64, done_share, { radius = 6 })                 -- dims what is still on cooldown
fx.spot(x, y, w, h, { color = tone, feather = 30 })               -- dims the screen outside the rect
fx.on()                                                           -- false: strokes from now on

The fields of wave, arc and mark (n dur period width t0 delay, dir spread inner, style pulse spin t0 delay) go into the effect's own sub-table; everything else is the renderer.world_fx spec as docs/AI_API_REFERENCE.md describes it (color fill edge rim glow glow_a add gradient noise front ttl layer lift mask clip prio). The flat strokes are the outline of the same shape: a wave's rings growing and fading, an arc's pie, a diamond or a cross, the zone's disc and edge, the line's quad, a screen rect's outline, a wipe as a bar shrinking from the top.

Test: tools\lua\lua.exe scripts\lib\fx\selftest.lua runs tools/utea_harness/test_fx_lib_561.lua over a shader client, a client without one and one whose shader throws.

Source

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

init.lua272 lines
--- shader effects for scripts (#561 step 3): an expanding wave, an arc or cone, a marker, a
--- toned or noisy zone, a line with a band front, and screen rects (glow, cooldown wipe,
--- spotlight). each call takes one table and draws through `renderer.world_fx` / `renderer.fx`.
--- a client without them, or one where the call threw (one warn, then strokes for the session),
--- gets the same shape as plain imgui strokes. every call returns true when the shader drew it.
--- world calls are retained like `renderer.world_circle` (re-send each frame or give `ttl`);
--- screen calls draw now, from on_draw.
---@class fx
local M = {}

local cos, sin, max, min, floor = math.cos, math.sin, math.max, math.min, math.floor

local S = { dead = false, warned = false, t0 = {} }

local function clock()
    local ok, t = pcall(function() return globals.realtime() end)
    return (ok and type(t) == "number") and t or os.clock()
end

local function rnd() return rawget(_G, "renderer") end

--- true while world shapes go through the shader (the dll has `world_fx` and it has not thrown).
---@return boolean
function M.on()
    local r = rnd()
    return not S.dead and type(r) == "table" and type(r.world_fx) == "function"
end

local function die(err)
    S.dead = true
    if S.warned then return end
    S.warned = true
    local lg = rawget(_G, "log")
    if type(lg) == "table" and lg.warn then pcall(lg.warn, "[lib/fx] shader call failed, flat shapes from now on: " .. tostring(err)) end
end

local function call(fn, ...)
    local ok, err = pcall(fn, ...)
    if not ok then die(err) end
    return ok
end

-- the fields that belong to an effect's own sub-table; the rest go to the spec as they are
local OWN = {
    wave = { n = true, dur = true, period = true, width = true, t0 = true, delay = true },
    arc = { dir = true, spread = true, inner = true },
    mark = { style = true, pulse = true, spin = true, t0 = true, delay = true },
}

local function split(kind, o)
    local spec, sub = {}, {}
    local own = OWN[kind]
    for k, v in pairs(o or {}) do
        if own and own[k] then sub[k] = v else spec[k] = v end
    end
    if own then spec[kind] = sub end
    return spec, sub
end

-- Color, {r,g,b[,a]} or nil -> Color for the strokes
local function tone(c, a_mul)
    local mk = rawget(_G, "color")
    if type(c) == "table" and type(c[1]) == "number" then
        return mk and mk(c[1], c[2] or 255, c[3] or 255, floor((c[4] or 255) * (a_mul or 1))) or c
    end
    if c == nil then return mk and mk(255, 255, 255, floor(255 * (a_mul or 1))) end
    if a_mul and a_mul ~= 1 and type(c) ~= "number" then
        local ok, r, g, b, a = pcall(function() return c.r, c.g, c.b, c.a end)
        if ok and mk and type(r) == "number" then return mk(r, g, b, floor((a or 255) * a_mul)) end
    end
    return c
end

local function xyz(p)
    if p == nil then return nil end
    local ok, x, y, z = pcall(function()
        if p.GetX then return p:GetX(), p:GetY(), p:GetZ() end
        return p.x, p.y, p.z
    end)
    if ok and type(x) == "number" then return x, y, z or 0 end
end

local function vec(x, y, z)
    local V = rawget(_G, "Vector") or rawget(_G, "vec")
    return V and V(x, y, z) or { x = x, y = y, z = z }
end

local function wopts(o, th, filled)
    return { ttl = o.ttl or 0.25, layer = o.layer, thickness = th, filled = filled, segments = 64 }
end

local function start_of(id, sub)
    if type(sub.t0) == "number" then return sub.t0 + (sub.delay or 0) end
    local t = S.t0[id]
    if not t then t = clock() + (sub.delay or 0); S.t0[id] = t end
    return t
end

local function world(kind, id, a, b, o, flat)
    local spec, sub = split(kind, o)
    if M.on() and call(rnd().world_fx, id, kind, a, b, spec) then return true end
    local r = rnd()
    if type(r) == "table" and type(r.world_circle) == "function" then flat(r, spec, sub) end
    return false
end

--- rings running out from `pos` to `radius` and fading: `n` rings (1..4) `dur` s each, once or
--- again every `period` s. o = { n, dur, period, width (world), t0, delay, color, fill, rim,
--- glow, gradient, noise, ttl, layer }
---@return boolean
function M.wave(id, pos, radius, o)
    o = o or {}
    return world("wave", id, pos, radius, o, function(r, spec, sub)
        local x, y, z = xyz(pos)
        if not x then return end
        local now, t0 = clock(), start_of(id, sub)
        local n, dur, per = min(max(sub.n or 1, 1), 4), max(sub.dur or 1, 0.05), sub.period or 0
        for i = 1, n do
            local age = now - t0 - (i - 1) * dur / n
            if per > 0 and age > 0 then age = age % per end
            local k = age / dur
            if k > 0 and k < 1 then
                r.world_circle(id .. ":w" .. i, vec(x, y, z), radius * k, tone(spec.color, 1 - k), wopts(o, 2))
            else
                r.world_clear(id .. ":w" .. i)
            end
        end
        r.world_circle(id .. ":e", vec(x, y, z), radius, tone(spec.color, 0.5), wopts(o, 1))
    end)
end

--- a pie or an annular sector: `dir` degrees or a Vector it points along, `spread` degrees
--- (90; 360 a full turn), `inner` world radius (0). o also takes the zone fields and `front`
---@return boolean
function M.arc(id, pos, radius, o)
    o = o or {}
    return world("arc", id, pos, radius, o, function(r, spec, sub)
        local x, y, z = xyz(pos)
        if not x or type(r.world_poly) ~= "function" then return end
        local d = sub.dir or 0
        if type(d) ~= "number" then
            local dx, dy = xyz(d)
            d = dx and math.deg(math.atan(dy, dx)) or 0
        end
        local half = math.rad(min(max(sub.spread or 90, 1), 360)) * 0.5
        local ri, pts = min(max(sub.inner or 0, 0), radius), {}
        for i = 0, 32 do
            local t = math.rad(d) - half + 2 * half * i / 32
            pts[#pts + 1] = vec(x + radius * cos(t), y + radius * sin(t), z)
        end
        if ri < 1 then pts[#pts + 1] = vec(x, y, z)
        else
            for i = 32, 0, -1 do
                local t = math.rad(d) - half + 2 * half * i / 32
                pts[#pts + 1] = vec(x + ri * cos(t), y + ri * sin(t), z)
            end
        end
        local op = wopts(o, 1.5); op.closed = true
        r.world_poly(id .. ":o", pts, tone(spec.color), op)
    end)
end

local MARK_STYLES = { diamond = true, cross = true, ring = true, dot = true }

--- a marker on the ground: `style` "diamond" | "cross" | "ring" | "dot", `pulse` s per ring
--- running out to 2.3 R (0 off), `spin` deg/s. o also takes color, fill, rim, glow, ttl, layer
---@return boolean
function M.mark(id, pos, radius, o)
    o = o or {}
    return world("mark", id, pos, radius, o, function(r, spec, sub)
        local x, y, z = xyz(pos)
        if not x then return end
        local st = MARK_STYLES[sub.style or "diamond"] and (sub.style or "diamond") or "diamond"
        if st == "ring" or st == "dot" or type(r.world_poly) ~= "function" then
            r.world_circle(id .. ":m", vec(x, y, z), radius * (st == "dot" and 0.5 or 1), tone(spec.color), wopts(o, 2, st == "dot"))
            return
        end
        local q = st == "cross" and radius or radius * 0.8
        local op = wopts(o, 2.5); op.closed = st == "diamond"
        if st == "diamond" then
            r.world_poly(id .. ":m", { vec(x + q, y, z), vec(x, y + q, z), vec(x - q, y, z), vec(x, y - q, z) }, tone(spec.color), op)
        else
            r.world_poly(id .. ":m", { vec(x - q, y - q, z), vec(x + q, y + q, z) }, tone(spec.color), op)
            r.world_poly(id .. ":n", { vec(x - q, y + q, z), vec(x + q, y - q, z) }, tone(spec.color), op)
        end
    end)
end

--- a zone (`renderer.world_fx` "circle") with every field: gradient = {c1, c2[, c3, c4],
--- axis = "radial" | "around"}, noise = {scale, speed, amount}, front = {..., band, trail}
---@return boolean
function M.zone(id, pos, radius, o)
    o = o or {}
    return world("circle", id, pos, radius, o, function(r, spec)
        r.world_circle(id .. ":z", pos, radius, tone(spec.color, min(max((spec.fill or 0.12) * 1.5, 0.05), 1)), wopts(o, nil, true))
        r.world_circle(id .. ":e", pos, radius, tone(spec.color), wopts(o, 1.5))
    end)
end

--- a line `from` -> `to`, `w0`/`w1` widths (a cone when they differ). front = {..., band = world
--- width of a bright band at the head, trail = 0..1 wake behind it}
---@return boolean
function M.line(id, from, to, o)
    o = o or {}
    return world("line", id, from, to, o, function(r, spec)
        local ax, ay, az = xyz(from)
        local bx, by, bz = xyz(to)
        if not ax or not bx or type(r.world_poly) ~= "function" then return end
        local dx, dy = bx - ax, by - ay
        local len = max((dx * dx + dy * dy) ^ 0.5, 1)
        local nx, ny = -dy / len, dx / len
        local h0, h1 = (spec.w0 or 100) * 0.5, (spec.w1 or spec.w0 or 100) * 0.5
        local op = wopts(o, 1.5); op.closed = true
        r.world_poly(id .. ":o", { vec(ax + nx * h0, ay + ny * h0, az), vec(bx + nx * h1, by + ny * h1, bz),
                                   vec(bx - nx * h1, by - ny * h1, bz), vec(ax - nx * h0, ay - ny * h0, az) }, tone(spec.color), op)
    end)
end

--- drops a world effect and its flat strokes.
function M.clear(id)
    local r = rnd()
    S.t0[id] = nil
    if type(r) ~= "table" then return end
    if type(r.world_clear) == "function" then r.world_clear(id) end
    if type(r.world_clear_prefix) == "function" then r.world_clear_prefix(id .. ":") end
end

-- screen rects
local function screen(kind, x, y, w, h, o, flat)
    local r = rnd()
    if not S.dead and type(r) == "table" and type(r.fx) == "function" and call(r.fx, kind, x, y, w, h, o) then return true end
    if type(r) == "table" and type(r.rounded_rect_outline) == "function" then flat(r) end
    return false
end

--- light round a screen rect (an icon, a button): o = { color, radius (px), glow (px), glow_a,
--- rim, fill, pulse (s), gradient }
---@return boolean
function M.glow(x, y, w, h, o)
    o = o or {}
    return screen("glow", x, y, w, h, o, function(r)
        r.rounded_rect_outline(x - 1, y - 1, w + 2, h + 2, o.radius or 0, tone(o.color), max(o.rim or 1.5, 1))
    end)
end

--- dims what is still on cooldown, clockwise from the top: `progress` 0..1 is the part done.
--- o = { color (the hand), color2 (the dim, black), fill (0.55), radius }
---@return boolean
function M.wipe(x, y, w, h, progress, o)
    local spec = {}
    for k, v in pairs(o or {}) do spec[k] = v end
    spec.progress = progress
    return screen("wipe", x, y, w, h, spec, function(r)
        if type(r.rect) ~= "function" or (progress or 0) >= 1 then return end
        -- strokes: the dim as a bar shrinking from the top
        local left = 1 - min(max(progress or 0, 0), 1)
        r.rect(x, y, w, h * left, tone(spec.color2 or { 0, 0, 0, 255 }, spec.fill or 0.55))
    end)
end

--- dims the screen outside a rect, a lit rim round it: o = { color (rim), color2 (dim, black),
--- fill (0.55), radius, feather (px, 24), glow }
---@return boolean
function M.spot(x, y, w, h, o)
    o = o or {}
    return screen("spot", x, y, w, h, o, function(r)
        r.rounded_rect_outline(x, y, w, h, o.radius or 0, tone(o.color), max(o.rim or 2, 1))
    end)
end

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