info = { type = "image", title = "Circuit coin", category = "btc", author = "BRRRO", version = "1.0", license = "MIT", desc = "Light pulses race along a board to a coin", api = 44, } local tr = ({ sk = { ["Pulse colour"] = "Farba impulzov", ["Gold"] = "Zlatá", ["Turquoise"] = "Tyrkysová", ["Green"] = "Zelená", ["Speed"] = "Rýchlosť" }, cs = { ["Pulse colour"] = "Barva impulzů", ["Gold"] = "Zlatá", ["Turquoise"] = "Tyrkysová", ["Green"] = "Zelená", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "color", label = T("Pulse colour"), type = "int", default = "gold", opts = { keys = { "gold", "cyan", "green" }, T("Gold"), T("Turquoise"), T("Green") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, min, max = math.floor, math.sin, math.cos, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local CX, CY, R = 32, 32, 17 local NT = 12 local PULSE = { gold = { 255, 206, 96 }, cyan = { 80, 230, 226 }, green = { 110, 255, 120 } } local function lerp(a, b, t) return a + (b - a) * t end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local DX = { 1, 1, 0, -1, -1, -1, 0, 1 } local DY = { 0, 1, 1, 1, 0, -1, -1, -1 } local TX, TY, TL, BEND, RX, RY, SPD, OFF, GAP = {}, {}, {}, {}, {}, {}, {}, {}, {} local function route(i) local a = (i - 1) * TAU / NT + 0.26 local h = hash32(i * 0x9E3779B1 + 7) local d = floor(a / 0.7854 + 0.5) % 8 + 1 local turn = (h >> 6) & 1 == 0 and 1 or -1 local plan = { d, 3 + umod(h, 4), (d - 1 + turn) % 8 + 1, 4 + umod(h >> 3, 6), d, 99 } local x, y = floor(CX + (R + 2) * cos(a)), floor(CY + (R + 2) * sin(a)) local xs, ys, bends, n = { x }, { y }, {}, 1 for s = 1, 5, 2 do local dir = plan[s] for _ = 1, plan[s + 1] do x, y = x + DX[dir], y + DY[dir] if x < 0 or x >= W or y < 0 or y >= H then return xs, ys, bends, n, a end n = n + 1 xs[n], ys[n] = x, y end bends[#bends + 1] = n end return xs, ys, bends, n, a end for i = 1, NT do local xs, ys, bends, n, a = route(i) local fx, fy, fb = {}, {}, {} for k = 1, n do fx[k], fy[k] = xs[n + 1 - k], ys[n + 1 - k] end for k = 1, #bends - 1 do fb[k] = n + 1 - bends[k] end TX[i], TY[i], TL[i], BEND[i] = fx, fy, n, fb local rx, ry = {}, {} for k = -2, 2 do rx[k + 3], ry[k + 3] = floor(CX + (R + 0.9) * cos(a + k * 0.1)), floor(CY + (R + 0.9) * sin(a + k * 0.1)) end RX[i], RY[i] = rx, ry local h = hash32(i * 0x85EBCA6B) SPD[i] = 0.85 + umod(h, 30) / 100 GAP[i] = 14 + umod(h >> 8, 30) OFF[i] = umod(h >> 14, 1000) / 1000 * (n + GAP[i]) end local function board() for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, rgb(lerp(46, 28, t), lerp(30, 18, t), lerp(18, 11, t))) end for i = 1, 40 do local h = hash32(i * 0xC2B2AE35) px(umod(h, W), umod(h >> 8, H), 58, 38, 22) end end local function glow(k0, k1) for k = k0, k1 do local r = R + 11 - k for dy = -r, r do local hw = floor(sqrt(r * r - dy * dy) + 0.5) blend_rect(CX - hw, CY + dy, 2 * hw, 1, 190, 124, 64, 0.12) end end end local function chip(x, y, w, h) rect(x, y, w, h, 14, 12, 14) rect(x, y, w, 1, 52, 48, 54) for k = 1, w - 2, 2 do px(x + k, y - 1, 150, 96, 52); px(x + k, y + h, 150, 96, 52) end px(x + 1, y + 2, 70, 66, 74) end local function decor() chip(2, 3, 11, 7) chip(51, 54, 11, 7) for i = 1, 16 do local h = hash32(i * 0x27D4EB2F) local x, y, len = umod(h, 56), umod(h >> 6, 56), 5 + umod(h >> 12, 12) local horiz = umod(h >> 16, 2) == 0 local mx, my = x + (horiz and len / 2 or 0), y + (horiz and 0 or len / 2) if sqrt((mx - CX) ^ 2 + (my - CY) ^ 2) > R + 8 then if horiz then rect(x, y, len, 1, 84, 52, 28); rect(x + len, y - 1, 2, 3, 150, 96, 52) else rect(x, y, 1, len, 84, 52, 28); rect(x - 1, y + len, 3, 2, 150, 96, 52) end end end end local function traces(i0, i1) for i = i0, i1 do local xs, ys = TX[i], TY[i] for k = 1, TL[i] do local x, y = xs[k], ys[k] local t = min(1, max(0, (sqrt((x + 0.5 - CX) ^ 2 + (y + 0.5 - CY) ^ 2) - R) / 16)) px(x, y, rgb(lerp(205, 112, t), lerp(134, 70, t), lerp(76, 38, t))) end for _, k in ipairs(BEND[i]) do rect(xs[k], ys[k], 2, 2, 226, 160, 96) end end end local RAMP = { { 58, 36, 14 }, { 176, 120, 48 }, { 255, 222, 146 } } local function shade(v) v = min(1, max(0, v)) local a, b, t = RAMP[1], RAMP[2], v * 2 if v >= 0.5 then a, b, t = RAMP[2], RAMP[3], (v - 0.5) * 2 end return rgb(lerp(a[1], b[1], t), lerp(a[2], b[2], t), lerp(a[3], b[3], t)) end local function coin(ya, yb) for y = ya, yb do local dy = y + 0.5 - CY for x = CX - R, CX + R - 1 do local dx = x + 0.5 - CX local d = sqrt(dx * dx + dy * dy) / R if d <= 1 then local lit = -(dx * 0.55 + dy * 0.83) / R local v if d > 0.9 then v = 0.66 + 0.34 * lit - (d > 0.97 and 0.1 or 0) elseif d > 0.82 then v = 0.34 + 0.1 * lit else v = 0.56 + 0.2 * lit + 0.16 * (1 - d) + umod(hash32(x * 64 + y), 7) * 0.008 end px(x, y, shade(v)) end end end end local BTC = { "..#.#...", "######..", ".##..##.", ".##..##.", ".#####..", ".##...##", ".##...##", "#######.", "..#.#..." } local function btc_sign(x0, y0, s, r, g, b) for i = 1, 9 do local c = 1 while true do local a, e = BTC[i]:find("#+", c) if not a then break end rect(x0 + (a - 1) * s, y0 + (i - 1) * s, (e - a + 1) * s, s, r, g, b) c = e + 1 end end end local function symbol() btc_sign(CX - 7, CY - 8, 2, 255, 226, 150) btc_sign(CX - 8, CY - 9, 2, 96, 60, 22) for i = 0, 79 do local a = i * TAU / 80 blend(floor(CX + (R + 0.6) * cos(a)), floor(CY + (R + 0.6) * sin(a)), 255, 226, 160, 0.4 - 0.25 * sin(a - 0.8)) end end local STAGES = { board, function() glow(0, 4) end, function() glow(5, 9) end, decor, function() traces(1, 6) end, function() traces(7, 12) end, function() coin(15, 23) end, function() coin(24, 32) end, function() coin(33, 41) end, function() coin(42, 50) end, symbol, } local STAGE, PART, BG = 1, nil, nil function render() if not BG then if PART then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() PART = snapshot(0, 0, W, H) if STAGE == #STAGES then BG = PART end STAGE = STAGE + 1 if not BG then gfx.ready(false) end return end blit_rgb(BG, 0, 0, W, H) local t = frame / FPS local pc = PULSE[cfg.color] or PULSE.gold local v = 12 + 6 * (cfg.speed or 3) local pr, pg, pb = pc[1], pc[2], pc[3] for i = 1, NT do local L = TL[i] local h = (t * v * SPD[i] + OFF[i]) % (L + GAP[i]) local xs, ys = TX[i], TY[i] for k = max(1, floor(h) - 6), min(L, floor(h) + 1) do local d = h - k local a = d < 0 and 1 + d or 1 - d / 6 if a > 0 then local m = a * a * a * 0.6 blend(xs[k], ys[k], pr + (255 - pr) * m, pg + (255 - pg) * m, pb + (255 - pb) * m, a * 0.95) end end local g = h > L and 1 - (h - L) / 12 or 0 if g > 0 then local rx, ry = RX[i], RY[i] blend(rx[1], ry[1], pr, pg, pb, g * 0.3) blend(rx[2], ry[2], pr, pg, pb, g * 0.6) blend(rx[3], ry[3], 255, 250, 235, g * 0.85) blend(rx[4], ry[4], pr, pg, pb, g * 0.6) blend(rx[5], ry[5], pr, pg, pb, g * 0.3) end end end